From 843534f480bb6a4d8d23676809b7de5f5fa102c1 Mon Sep 17 00:00:00 2001 From: hugowschneider Date: Fri, 10 Jan 2020 09:06:09 +0100 Subject: [PATCH] Continuous Integration Workflow for docker-plain quickstarter Adding 'tests' folder with: utils: go code with utilities to help writing tests scripts: some utility scripts docker-plain: docker-plain tests setup-tests: go code for setting up missing infrastructure for QS tests Docker plain tests works as follows: First deploy the QS using the webhook-proxy Trigger build by calling webhook-proxy Test existence of resources Test if commit status is flagged as successful --- .../continuous-integration-workflow.yml | 96 +++++++++ tests/.gitignore | 1 + tests/Makefile | 12 ++ tests/docker-plain/jenkinsfile_test.go | 96 +++++++++ tests/go.mod | 17 ++ tests/go.sum | 197 ++++++++++++++++++ tests/scripts/setup-mocked-ods-repo.sh | 81 +++++++ tests/test-setup/main.go | 62 ++++++ tests/utils/apps.go | 17 ++ tests/utils/builds.go | 17 ++ tests/utils/images.go | 29 +++ tests/utils/jenkinsfile.go | 164 +++++++++++++++ tests/utils/ods-env.go | 33 +++ tests/utils/resources.go | 135 ++++++++++++ tests/utils/services.go | 48 +++++ tests/utils/types.go | 22 ++ 16 files changed, 1027 insertions(+) create mode 100644 .github/workflows/continuous-integration-workflow.yml create mode 100644 tests/.gitignore create mode 100644 tests/Makefile create mode 100644 tests/docker-plain/jenkinsfile_test.go create mode 100644 tests/go.mod create mode 100644 tests/go.sum create mode 100755 tests/scripts/setup-mocked-ods-repo.sh create mode 100644 tests/test-setup/main.go create mode 100644 tests/utils/apps.go create mode 100644 tests/utils/builds.go create mode 100644 tests/utils/images.go create mode 100644 tests/utils/jenkinsfile.go create mode 100644 tests/utils/ods-env.go create mode 100644 tests/utils/resources.go create mode 100644 tests/utils/services.go create mode 100644 tests/utils/types.go diff --git a/.github/workflows/continuous-integration-workflow.yml b/.github/workflows/continuous-integration-workflow.yml new file mode 100644 index 000000000..42e195861 --- /dev/null +++ b/.github/workflows/continuous-integration-workflow.yml @@ -0,0 +1,96 @@ +name: Continous Integration Tests +on: + pull_request: + branches: + - master +jobs: + test: + name: Docker plain provisioning + runs-on: ubuntu-16.04 + steps: + - + name: GitHub context + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + run: jq . <<< "${GITHUB_CONTEXT}" + - + name: Checkout QuickStarters repository + uses: actions/checkout@v2.0.0 + with: + fetch-depth: 0 + path: ods-quickstarters + - + name: Checkout ODS Core + uses: actions/checkout@v2.0.0 + with: + fetch-depth: 0 + repository: opendevstack/ods-core + ref: master + path: ods-core + - + name: Checkout ODS Jenkins Shared Library + uses: actions/checkout@v2.0.0 + with: + fetch-depth: 0 + repository: BIX-Digital/ods-jenkins-shared-library + ref: invalid/curl-http-only + path: ods-jenkins-shared-library + - + name: Setup Go 1.13 + uses: actions/setup-go@v1.0.0 + with: + version: 1.13 + - + name: Download OpenShift Client + run: | + wget https://github.com/openshift/origin/releases/download/v3.11.0/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz + tar -xzvf openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz + sudo mv openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit/oc /usr/local/bin/oc + - + name: Download Tailor + run: | + curl -LO "https://github.com/opendevstack/tailor/releases/download/v0.11.0/tailor-linux-amd64" + chmod +x tailor-linux-amd64 + sudo mv tailor-linux-amd64 /usr/local/bin/tailor + - + name: Tailor version + run: tailor version + - + name: OpenShift client version + run: oc version + - + name: jq version + run: jq --version + - + name: golang version + run: go version + - + name: Docker version + run: docker --version + - + name: Network before changes + run: ifconfig + - + name: Configure docker network and insecure registries + run: ls -la && ls -la ./tests && ./tests/scripts/apply-docker-settings.sh + working-directory: ${{ format('{0}/ods-core', github.workspace) }} + - + name: Start OC cluster + run: oc cluster up --base-dir=${HOME}/openshift.local.clusterup --routing-suffix 172.17.0.1.nip.io --public-hostname 172.17.0.1 --enable=centos-imagestreams --enable=persistent-volumes --enable=registry --enable=router + - + name: Login into the cluster + run: oc login -u system:admin + - + name: Create test infrastructure + run: | + mkdir -p ods-config + ./tests/scripts/recreate-test-infrastructure.sh + working-directory: ${{ format('{0}/ods-core', github.workspace) }} + - + name: Test setup + run: make setup-tests + working-directory: ${{ format('{0}/ods-quickstarters/tests', github.workspace) }} + - + name: Run tests + run: make test + working-directory: ${{ format('{0}/ods-quickstarters/tests', github.workspace) }} diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 000000000..e036749ae --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +test-setup.bin diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 000000000..3f3302c13 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,12 @@ +.PHONY: test lint + +test: docker-plain/jenkinsfile_test.go go.mod go.sum + go test -v github.com/opendevstack/ods-quickstarters/tests/docker-plain + +setup-tests: test-setup/main.go + go build -o test-setup.bin test-setup/main.go + ./test-setup.bin + ./scripts/setup-mocked-ods-repo.sh --ods-ref cicdtests +lint: + echo "Checking code ..." + golangci-lint run diff --git a/tests/docker-plain/jenkinsfile_test.go b/tests/docker-plain/jenkinsfile_test.go new file mode 100644 index 000000000..193b55d2e --- /dev/null +++ b/tests/docker-plain/jenkinsfile_test.go @@ -0,0 +1,96 @@ +package docker_plain + +import ( + "encoding/json" + "fmt" + coreUtils "github.com/opendevstack/ods-core/tests/utils" + "github.com/opendevstack/ods-quickstarters/tests/utils" + "log" + "net/http" + "strings" + "testing" +) + +func TestJenkinsFile(t *testing.T) { + + values, err := utils.ReadConfiguration() + if err != nil { + t.Fatal(err) + } + + const component_id = "docker-plain-test" + err = utils.RunJenkinsFile( + "ods-quickstarters", + "opendevstack", + "cicdtests", + coreUtils.PROJECT_NAME, + "docker-plain/Jenkinsfile", + "unitt-cd", + coreUtils.EnvPair{ + Name: "COMPONENT_ID", + Value: component_id, + }, + coreUtils.EnvPair{ + Name: "GIT_URL_HTTP", + Value: fmt.Sprintf("%s/unitt/docker-plain-test.git", values["REPO_BASE"]), + }, + ) + if err != nil { + t.Fatal(err) + } + + err = utils.RunJenkinsFile( + component_id, + "unitt", + "master", + coreUtils.PROJECT_NAME, + "Jenkinsfile", + "unitt-cd", + coreUtils.EnvPair{ + Name: "COMPONENT_ID", + Value: component_id, + }, + ) + if err != nil { + t.Fatal(err) + } + + resourcesInTest := utils.Resources{ + Namespace: coreUtils.PROJECT_NAME_TEST, + ImageTags: []utils.ImageTag{{Name: component_id, Tag: "latest"}}, + BuildConfigs: []string{component_id}, + DeploymentConfigs: []string{component_id}, + Services: []string{component_id}, + ImageStreams: []string{component_id}, + } + + utils.CheckResources(resourcesInTest, t) + + stdout, stderr, err := coreUtils.RunCommand("docker", []string{"exec", "mockbucket", "sh", "-c", "cd /scm/unitt/docker-plain-test.git && git rev-parse HEAD"}, []string{}) + if err != nil { + t.Fatalf("Docker exec failed:%s\nStdOut: %s\nStdErr: %s", err, stdout, stderr) + } + + client := &http.Client{} + url := fmt.Sprintf("http://%s/rest/build-status/1.0/commits/%s", values["BITBUCKET_HOST"], strings.TrimSuffix(stdout, "\n")) + request, err := http.NewRequest("GET", url, nil) + if err != nil { + log.Fatal(err) + } + request.SetBasicAuth(values["CD_USER_ID"], values["CD_USER_PWD"]) + response, err := client.Do(request) + if err != nil { + log.Fatal(err) + } + + commitStatus := utils.CommitStatus{} + err = json.NewDecoder(response.Body).Decode(&commitStatus) + if err != nil { + t.Fatal(err) + } + + if commitStatus.State != "SUCCESSFUL" { + t.Error("The commit status should have been SUCCESSFUL") + } + +} diff --git a/tests/go.mod b/tests/go.mod new file mode 100644 index 000000000..15a7fb4ef --- /dev/null +++ b/tests/go.mod @@ -0,0 +1,17 @@ +module github.com/opendevstack/ods-quickstarters/tests + +go 1.13 + +require ( + github.com/google/btree v1.0.0 // indirect + github.com/googleapis/gnostic v0.3.1 // indirect + github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect + github.com/opendevstack/mockbucket v0.0.0-20191220083823-53b37b3fa3fd // indirect + github.com/opendevstack/ods-core/tests v0.0.0-20200110113746-2e6aa267ea88 + github.com/openshift/api v3.9.0+incompatible + github.com/openshift/client-go v3.9.0+incompatible + github.com/peterbourgon/diskv v2.0.1+incompatible // indirect + k8s.io/api v0.0.0-20190222213804-5cb15d344471 + k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628 + k8s.io/client-go v0.0.0-20190228174230-b40b2a5939e4 +) diff --git a/tests/go.sum b/tests/go.sum new file mode 100644 index 000000000..735cb6296 --- /dev/null +++ b/tests/go.sum @@ -0,0 +1,197 @@ +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= +github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= +github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= +github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= +github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= +github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= +github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0= +github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg= +github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc= +github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I= +github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= +github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= +github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= +github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJhYk= +github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU= +github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs= +github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ= +github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.8 h1:QiWkFLKq0T7mpzwOTu6BzNDbfTE8OLrYhVKYMLF46Ok= +github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= +github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opendevstack/mockbucket v0.0.0-20191220083823-53b37b3fa3fd h1:HNkcGc7xmSVFlsKPTYK2nLoOaZN+va+L9yO/8DHV+j8= +github.com/opendevstack/mockbucket v0.0.0-20191220083823-53b37b3fa3fd/go.mod h1:il+b+smQQ6TsLwwkxa+jgqgJDEyo6Z0w1/8JJUYR2/w= +github.com/opendevstack/mockbucket v0.0.0-20200113101121-a544f76e854a h1:pmSXAlOAK8CYy9VSgjf6vQskeuFY+XdMGswv7sqo+M0= +github.com/opendevstack/mockbucket v0.0.0-20200113101121-a544f76e854a/go.mod h1:il+b+smQQ6TsLwwkxa+jgqgJDEyo6Z0w1/8JJUYR2/w= +github.com/opendevstack/ods-core/tests v0.0.0-20200110113746-2e6aa267ea88 h1:I78ik2dTa5eamdfqX2U+A3FElXtCmi/CoB1tWoCMLz4= +github.com/opendevstack/ods-core/tests v0.0.0-20200110113746-2e6aa267ea88/go.mod h1:pn5pfqL7ujGCZxRAlgbOLETCC0MoRcygYczae9GGwfU= +github.com/opendevstack/ods-core/tests v0.0.0-20200110160616-c89cc4c33ac4 h1:h13OpQjl5zH836rjeNSPuJoCFpVfPrP8RqTTJ8V3uWs= +github.com/opendevstack/ods-core/tests v0.0.0-20200110160616-c89cc4c33ac4/go.mod h1:pn5pfqL7ujGCZxRAlgbOLETCC0MoRcygYczae9GGwfU= +github.com/openshift/api v3.9.0+incompatible h1:fJ/KsefYuZAjmrr3+5U9yZIZbTOpVkDDLDLFresAeYs= +github.com/openshift/api v3.9.0+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY= +github.com/openshift/client-go v3.9.0+incompatible h1:13k3Ok0B7TA2hA3bQW2aFqn6y04JaJWdk7ITTyg+Ek0= +github.com/openshift/client-go v3.9.0+incompatible/go.mod h1:6rzn+JTr7+WYS2E1TExP4gByoABxMznR6y2SnUIkmxk= +github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= +github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ= +github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q= +github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= +github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g= +golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 h1:nVJ3guKA9qdkEQ3TUdXI9QSINo2CUPM/cySEvw2w8I0= +golang.org/x/crypto v0.0.0-20200109152110-61a87790db17/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 h1:pE8b58s1HRDMi8RDc79m0HISf9D4TzseP40cEA6IGfs= +golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200113162924-86b910548bc1 h1:gZpLHxUX5BdYLA08Lj4YCJNN/jk7KtquiArPoeX0WvA= +golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= +google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +k8s.io/api v0.0.0-20190222213804-5cb15d344471 h1:MzQGt8qWQCR+39kbYRd0uQqsvSidpYqJLFeWiJ9l4OE= +k8s.io/api v0.0.0-20190222213804-5cb15d344471/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= +k8s.io/api v0.17.0 h1:H9d/lw+VkZKEVIUc8F3wgiQ+FUXTTr21M87jXLU7yqM= +k8s.io/api v0.17.0/go.mod h1:npsyOePkeP0CPwyGfXDHxvypiYMJxBWAMpQxCaJ4ZxI= +k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628 h1:UYfHH+KEF88OTg+GojQUwFTNxbxwmoktLwutUzR0GPg= +k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0= +k8s.io/apimachinery v0.17.0 h1:xRBnuie9rXcPxUkDizUsGvPf1cnlZCFu210op7J7LJo= +k8s.io/apimachinery v0.17.0/go.mod h1:b9qmWdKlLuU9EBh+06BtLcSf/Mu89rWL33naRxs1uZg= +k8s.io/client-go v0.0.0-20190228174230-b40b2a5939e4 h1:aE8wOCKuoRs2aU0OP/Rz8SXiAB0FTTku3VtGhhrkSmc= +k8s.io/client-go v0.0.0-20190228174230-b40b2a5939e4/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s= +k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o= +k8s.io/client-go v11.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s= +k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= +k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= +k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= +k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= +k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= +k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a/go.mod h1:1TqjTSzOxsLGIKfj0lK8EeCP7K1iUG65v09OM0/WG5E= +k8s.io/utils v0.0.0-20200109141947-94aeca20bf09 h1:sz6xjn8QP74104YNmJpzLbJ+a3ZtHt0tkD0g8vpdWNw= +k8s.io/utils v0.0.0-20200109141947-94aeca20bf09/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew= +sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI= +sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= +sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/tests/scripts/setup-mocked-ods-repo.sh b/tests/scripts/setup-mocked-ods-repo.sh new file mode 100755 index 000000000..8c2ebfbf1 --- /dev/null +++ b/tests/scripts/setup-mocked-ods-repo.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -eu + +function usage { + printf "usage: %s [options]\n", $0 + printf "\t-h|--help\tPrints the usage\n" + printf "\t-v|--verbose\tVerbose output\n" + printf "\t-b|--ods-ref\tReference to be created in the mocked git repo.\n" + +} + +urlencode() { + # urlencode + + local length="${#1}" + for (( i = 0; i < length; i++ )); do + local c="${1:i:1}" + case $c in + [a-zA-Z0-9.~_-]) printf "$c" ;; + *) printf '%%%02X' "'$c" ;; + esac + done + + +} + +REF="" + +URL=$(oc config view --minify -o jsonpath='{.clusters[*].cluster.server}') +if [ ${URL} != "https://172.17.0.1:8443" ]; then + echo "You are not in a local cluster. Stopping now!!!" + exit 1 +fi + +while [[ "$#" -gt 0 ]]; do case $1 in + + -v|--verbose) set -x;; + + -h|--help) usage; exit 0;; + + -b=*|--ods-ref=*) REF="${1#*=}";; + -b|--ods-ref) REF="$2"; shift;; + + *) echo "Unknown parameter passed: $1"; usage; exit 1;; + esac; shift; done + +if git remote -v | grep mockbucket; then + git remote remove mockbucket +fi + + +if [ -z "${REF}" ]; then + echo "Reference --ods-ref must be provided" + exit 1 +fi + +source ${BASH_SOURCE%/*}/../../../ods-core/ods-config/ods-core.env + +docker ps | grep mockbucket + +# git checkout -b "${REF}" +HEAD=$(git rev-parse --abbrev-ref HEAD) +if [ "${HEAD}" = "HEAD" ]; then + HEAD="cicdtests" + git checkout -b ${HEAD} +fi +git remote add mockbucket "http://$(urlencode ${CD_USER_ID}):$(urlencode ${CD_USER_PWD})@${BITBUCKET_HOST}/scm/opendevstack/ods-quickstarters.git" +git -c http.sslVerify=false push mockbucket --set-upstream "${HEAD}:${REF}" +git remote remove mockbucket + + +cd ${BASH_SOURCE%/*}/../../../ods-jenkins-shared-library +HEAD=$(git rev-parse --abbrev-ref HEAD) +if [ "${HEAD}" = "HEAD" ]; then + HEAD="cicdtests" + git checkout -b ${HEAD} +fi +git remote add mockbucket "http://$(urlencode ${CD_USER_ID}):$(urlencode ${CD_USER_PWD})@${BITBUCKET_HOST}/scm/opendevstack/ods-jenkins-shared-library.git" +git -c http.sslVerify=false push mockbucket --set-upstream "${HEAD}:${REF}" +git remote remove mockbucket +cd - \ No newline at end of file diff --git a/tests/test-setup/main.go b/tests/test-setup/main.go new file mode 100644 index 000000000..0dbee01eb --- /dev/null +++ b/tests/test-setup/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + mockbucket "github.com/opendevstack/mockbucket/api" + coreUtils "github.com/opendevstack/ods-core/tests/utils" + "github.com/opendevstack/ods-quickstarters/tests/utils" + "io/ioutil" + "log" + "net/http" +) + +func main() { + err := coreUtils.RemoveAllTestOCProjects() + err = utils.RunJenkinsFile("ods-core", "opendevstack", "cicdtests", coreUtils.PROJECT_NAME, "create-projects/Jenkinsfile", "prov-cd") + if err != nil { + log.Fatalf("Error running JenkinsFile : %s", err) + } + + fmt.Printf("Project %s is create to support testing. Be sure not to delete the namespaces '%s', '%s' and '%s' during your tests ", + coreUtils.PROJECT_NAME, + coreUtils.PROJECT_NAME_CD, + coreUtils.PROJECT_NAME_TEST, + coreUtils.PROJECT_NAME_DEV) + + values, err := utils.ReadConfiguration() + if err != nil { + log.Fatalf("Error reading ods-core.env: %s", err) + } + + repository := mockbucket.Repository{ + Name: "docker-plain-test", + } + repositoryJson, err := json.Marshal(repository) + if err != nil { + log.Fatal(err) + } + client := &http.Client{} + url := fmt.Sprintf("http://%s/rest/api/1.0/projects/%s/repos", values["BITBUCKET_HOST"], coreUtils.PROJECT_NAME) + request, err := http.NewRequest("POST", url, bytes.NewBuffer(repositoryJson)) + if err != nil { + log.Fatal(err) + } + request.SetBasicAuth(values["CD_USER_ID"], values["CD_USER_PWD"]) + response, err := client.Do(request) + if err != nil { + log.Fatal(err) + } + + bodyBytes, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Fatal(err) + } + + if response.StatusCode != http.StatusCreated { + log.Fatal(string(bodyBytes)) + } else { + log.Print(string(bodyBytes)) + } +} diff --git a/tests/utils/apps.go b/tests/utils/apps.go new file mode 100644 index 000000000..c32fc9215 --- /dev/null +++ b/tests/utils/apps.go @@ -0,0 +1,17 @@ +package utils + +import ( + "fmt" + v1 "github.com/openshift/api/apps/v1" +) + +func FindDeploymentConfig(depoymentConfigs *v1.DeploymentConfigList, depoymentConfigName string) error { + for _, buildConfig := range depoymentConfigs.Items { + + if depoymentConfigName == buildConfig.Name { + return nil + } + } + + return fmt.Errorf("Deployment config '%s' not found.", depoymentConfigName) +} diff --git a/tests/utils/builds.go b/tests/utils/builds.go new file mode 100644 index 000000000..3bcdb221c --- /dev/null +++ b/tests/utils/builds.go @@ -0,0 +1,17 @@ +package utils + +import ( + "fmt" + v1 "github.com/openshift/api/build/v1" +) + +func FindBuildConfig(buildConfigs *v1.BuildConfigList, buildConfigName string) error { + for _, buildConfig := range buildConfigs.Items { + + if buildConfigName == buildConfig.Name { + return nil + } + } + + return fmt.Errorf("Build config '%s' not found.", buildConfigName) +} diff --git a/tests/utils/images.go b/tests/utils/images.go new file mode 100644 index 000000000..eb6e1d9f5 --- /dev/null +++ b/tests/utils/images.go @@ -0,0 +1,29 @@ +package utils + +import ( + "fmt" + v1 "github.com/openshift/api/image/v1" +) + +func FindImageTag(imageStreamList *v1.ImageStreamList, imageTag ImageTag) error { + for _, imageStream := range imageStreamList.Items { + if imageStream.Name != imageTag.Name { + continue + } + for _, tag := range imageStream.Status.Tags { + if tag.Tag == imageTag.Tag { + return nil + } + } + } + return fmt.Errorf("Image tag '%s:%s' not found.", imageTag.Name, imageTag.Tag) +} + +func FindImageStream(imageStreamList *v1.ImageStreamList, imageStreamName string) error { + for _, imageStream := range imageStreamList.Items { + if imageStream.Name == imageStreamName { + return nil + } + } + return fmt.Errorf("Image stream '%s' not found.", imageStreamName) +} diff --git a/tests/utils/jenkinsfile.go b/tests/utils/jenkinsfile.go new file mode 100644 index 000000000..80131db22 --- /dev/null +++ b/tests/utils/jenkinsfile.go @@ -0,0 +1,164 @@ +package utils + +import ( + "bytes" + "crypto/tls" + "encoding/json" + "fmt" + coreUtils "github.com/opendevstack/ods-core/tests/utils" + v1 "github.com/openshift/api/build/v1" + buildClientV1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1" + "io/ioutil" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "net/http" + "os" + "strings" + "time" +) + +func RunJenkinsFile(repository string, repositoryProject string, branch string, projectName string, jenkinsFile string, jenkinsNamespace string, envVars ...coreUtils.EnvPair) error { + values, err := ReadConfiguration() + if err != nil { + return err + } + + request := coreUtils.RequestBuild{ + Repository: repository, + Branch: branch, + Project: repositoryProject, + Env: append([]coreUtils.EnvPair{ + { + Name: "PROJECT_ID", + Value: projectName, + }, + { + Name: "CD_USER_TYPE", + Value: "general", + }, + { + Name: "CD_USER_ID_B64", + Value: values["CD_USER_ID_B64"], + }, + { + Name: "PIPELINE_TRIGGER_SECRET", + Value: values["PIPELINE_TRIGGER_SECRET_B64"], + }, + { + Name: "ODS_GIT_REF", + Value: "cicdtests", + }, + { + Name: "ODS_IMAGE_TAG", + Value: values["ODS_IMAGE_TAG"], + }, + }, envVars...), + } + + body, err := json.Marshal(request) + if err != nil { + return fmt.Errorf("Could not marchal json: %s", err) + } + + pipelineNamePrefix := strings.ToLower(strings.Split(jenkinsFile, "/")[0]) + pipelineName := fmt.Sprintf("ods-corejob-%s-%s", pipelineNamePrefix, projectName) + buildName := fmt.Sprintf("%s-%s-1", pipelineName, branch) + + println(buildName) + + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + response, err := http.Post( + fmt.Sprintf("https://webhook-proxy-%s.172.17.0.1.nip.io/build?trigger_secret=%s&jenkinsfile_path=%s&component=%s", + jenkinsNamespace, + values["PIPELINE_TRIGGER_SECRET"], + jenkinsFile, + pipelineName), + "application/json", + bytes.NewBuffer(body)) + + if err != nil { + return err + } + + if response.StatusCode >= http.StatusAccepted { + bodyBytes, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + return fmt.Errorf("Could not post request: %s", string(bodyBytes)) + } + + if response.StatusCode >= http.StatusAccepted { + bodyBytes, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + return fmt.Errorf("Could not post request: %s", string(bodyBytes)) + } + + config, err := coreUtils.GetOCClient() + if err != nil { + return fmt.Errorf("Error creating OC config: %s", err) + } + + buildClient, err := buildClientV1.NewForConfig(config) + if err != nil { + return fmt.Errorf("Error creating Build client: %s", err) + } + + time.Sleep(10 * time.Second) + build, err := buildClient.Builds(jenkinsNamespace).Get(buildName, metav1.GetOptions{}) + count := 0 + max := 240 + for (err != nil || build.Status.Phase == v1.BuildPhaseNew || build.Status.Phase == v1.BuildPhasePending || build.Status.Phase == v1.BuildPhaseRunning) && count < max { + build, err = buildClient.Builds(jenkinsNamespace).Get(buildName, metav1.GetOptions{}) + time.Sleep(2 * time.Second) + if err != nil { + fmt.Printf("Build is still not available\n") + } else { + fmt.Printf("Waiting for build. Current status: %s\n", build.Status.Phase) + } + count++ + } + + stdout, stderr, err := coreUtils.RunCommand( + "oc", + []string{ + "project", jenkinsNamespace, + }, []string{}) + + workspace, ok := os.LookupEnv("GITHUB_WORKSPACE") + var script string + if ok { + script = fmt.Sprintf("%s/ods-core/tests/scripts/utils/print-jenkins-log.sh", workspace) + } else { + script = "../../ods-core/tests/scripts/utils/print-jenkins-log.sh" + } + + stdout, stderr, err = coreUtils.RunCommand( + script, + []string{ + buildName, + }, []string{}) + + if err != nil { + panic(err) + } + + if count >= max || build.Status.Phase != v1.BuildPhaseComplete { + + if count >= max { + return fmt.Errorf( + "Timeout during build: \nStdOut: %s\nStdErr: %s", + stdout, + stderr) + } else { + return fmt.Errorf( + "Error during build: \nStdOut: %s\nStdErr: %s", + stdout, + stderr) + } + + } + + return nil +} diff --git a/tests/utils/ods-env.go b/tests/utils/ods-env.go new file mode 100644 index 000000000..ca7405c16 --- /dev/null +++ b/tests/utils/ods-env.go @@ -0,0 +1,33 @@ +package utils + +import ( + "bufio" + "os" + "path" + "runtime" + "strings" +) + +func ReadConfiguration() (map[string]string, error) { + + _, filename, _, _ := runtime.Caller(0) + dir := path.Join(path.Dir(filename), "..", "..", "..", "ods-configuration", "ods-core.env") + + file, err := os.Open(dir) + if err != nil { + return nil, err + } + defer file.Close() + values := map[string]string{} + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + parts := strings.SplitN(line, "=", 2) + if len(parts) == 2 { + values[parts[0]] = parts[1] + } + } + + return values, nil +} diff --git a/tests/utils/resources.go b/tests/utils/resources.go new file mode 100644 index 000000000..1bc1216e0 --- /dev/null +++ b/tests/utils/resources.go @@ -0,0 +1,135 @@ +package utils + +import ( + coreUtils "github.com/opendevstack/ods-core/tests/utils" + appsClientV1 "github.com/openshift/client-go/apps/clientset/versioned/typed/apps/v1" + buildClientV1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1" + imageClientV1 "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "log" + "testing" +) + +func CheckImageTags(namespace string, imageTags []ImageTag, config *rest.Config, t *testing.T) { + if len(imageTags) == 0 { + return + } + + imageClient, err := imageClientV1.NewForConfig(config) + if err != nil { + t.Fatal(err) + } + + images, err := imageClient.ImageStreams(namespace).List(metav1.ListOptions{}) + + for _, imageTag := range imageTags { + if err = FindImageTag(images, imageTag); err != nil { + t.Error(err) + } + } +} + +func CheckImageStreams(namespace string, imageStreams []string, config *rest.Config, t *testing.T) { + + if len(imageStreams) == 0 { + return + } + + imageClient, err := imageClientV1.NewForConfig(config) + if err != nil { + t.Fatal(err) + } + + images, err := imageClient.ImageStreams(namespace).List(metav1.ListOptions{}) + + for _, imageStream := range imageStreams { + if err = FindImageStream(images, imageStream); err != nil { + t.Error(err) + } + } +} + +func CheckBuildConfigs(namespace string, buildConfigs []string, config *rest.Config, t *testing.T) { + + if len(buildConfigs) == 0 { + return + } + + buildClient, err := buildClientV1.NewForConfig(config) + if err != nil { + log.Fatal(err) + } + + buildConfigList, err := buildClient.BuildConfigs(namespace).List(metav1.ListOptions{}) + if err != nil { + log.Fatal(err) + } + + for _, buildConfig := range buildConfigs { + if err = FindBuildConfig(buildConfigList, buildConfig); err != nil { + t.Error(err) + } + } +} + +func CheckDeploymentConfigs(namespace string, deploymentConfigs []string, config *rest.Config, t *testing.T) { + + if len(deploymentConfigs) == 0 { + return + } + + appsClient, err := appsClientV1.NewForConfig(config) + if err != nil { + log.Fatal(err) + } + + deploymentsConfigs, err := appsClient.DeploymentConfigs(namespace).List(metav1.ListOptions{}) + if err != nil { + log.Fatal(err) + } + + for _, deploymentsConfig := range deploymentConfigs { + if err = FindDeploymentConfig(deploymentsConfigs, deploymentsConfig); err != nil { + t.Error(err) + } + } +} + +func CheckServices(namespace string, services []string, config *rest.Config, t *testing.T) { + + if len(services) == 0 { + return + } + + clientset, err := kubernetes.NewForConfig(config) + + if err != nil { + t.Fatal(err) + } + + serviceClient := clientset.CoreV1().Services(namespace) + serviceList, err := serviceClient.List(metav1.ListOptions{}) + + for _, service := range services { + if err = FindService(serviceList, service); err != nil { + t.Error(err) + } + } +} + +func CheckResources(resources Resources, t *testing.T) { + + config, err := coreUtils.GetOCClient() + if err != nil { + t.Fatal(err) + } + + CheckImageStreams(resources.Namespace, resources.ImageStreams, config, t) + CheckImageTags(resources.Namespace, resources.ImageTags, config, t) + CheckBuildConfigs(resources.Namespace, resources.BuildConfigs, config, t) + CheckDeploymentConfigs(resources.Namespace, resources.DeploymentConfigs, config, t) + CheckServices(resources.Namespace, resources.Services, config, t) + +} diff --git a/tests/utils/services.go b/tests/utils/services.go new file mode 100644 index 000000000..ed13d087e --- /dev/null +++ b/tests/utils/services.go @@ -0,0 +1,48 @@ +package utils + +import ( + "fmt" + coreUtils "github.com/opendevstack/ods-core/tests/utils" + v1 "k8s.io/api/core/v1" + v12 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "strings" +) + +func FindService(services *v1.ServiceList, serviceName string) error { + for _, service := range services.Items { + if service.Name == serviceName { + + config, err := coreUtils.GetOCClient() + clientset, err := kubernetes.NewForConfig(config) + + if err != nil { + return err + } + + podClient := clientset.CoreV1().Pods(service.Namespace) + + selector := []string{} + for key, value := range service.Spec.Selector { + selector = append(selector, fmt.Sprintf("%s=%s", key, value)) + } + pods, err := podClient.List( + v12.ListOptions{ + LabelSelector: strings.Join(selector, ","), + }, + ) + + if err != nil { + return err + } + if len(pods.Items) == 0 { + return fmt.Errorf("Service %s has a selector '%s' which returns no pods", serviceName, strings.Join(selector, ",")) + } + + return nil + } + + } + + return fmt.Errorf("Service '%s' not found.", serviceName) +} diff --git a/tests/utils/types.go b/tests/utils/types.go new file mode 100644 index 000000000..0f0e165ba --- /dev/null +++ b/tests/utils/types.go @@ -0,0 +1,22 @@ +package utils + +type ImageTag struct { + Name string + Tag string +} + +type Resources struct { + Namespace string + ImageTags []ImageTag + BuildConfigs []string + DeploymentConfigs []string + Services []string + ImageStreams []string +} + +type CommitStatus struct { + State string `json:"state"` + Key string `json:"key"` + Name string `json:"name"` + URL string `json:"url"` +}