From c7e563ef1262e541b25cb4bcce63597164dde6ce Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Mar 16 2020 11:39:12 +0000
Subject: [PATCH 1/2] Use openshift project created by role


To enable paralel runs jobs should be run in new openshift projects.
This also removes necessarity of cleanup since openshift project is
deleted after defined time.

---

diff --git a/openshift/pipelines/snippets/waiverdb-build-and-test.groovy b/openshift/pipelines/snippets/waiverdb-build-and-test.groovy
index edde49d..dc25bf4 100644
--- a/openshift/pipelines/snippets/waiverdb-build-and-test.groovy
+++ b/openshift/pipelines/snippets/waiverdb-build-and-test.groovy
@@ -128,30 +128,32 @@ stage('Build container') {
       env.TEMP_TAG = versions[1] + '-jenkins-' + currentBuild.id
 
       openshift.withCluster() {
-        // OpenShift BuildConfig doesn't support specifying a tag name at build time.
-        // We have to create a new BuildConfig for each container build.
-        // Create a BuildConfig from a seperated Template.
-        echo 'Creating a BuildConfig for container build...'
-        def template = readYaml file: 'openshift/waiverdb-container-template.yaml'
-        def processed = openshift.process(template,
-          "-p", "NAME=${env.BUILDCONFIG_INSTANCE_ID}",
-          '-p', "WAIVERDB_GIT_REPO=${params.GIT_REPO}",
-          // A pull-request branch, like pull/123/head, cannot be built with commit ID
-          // because refspec cannot be customized in an OpenShift build .
-          '-p', "WAIVERDB_GIT_REF=${env.PR_NO ? env.GIT_REPO_REF : env.GIT_COMMIT}",
-          '-p', "WAIVERDB_IMAGE_TAG=${env.TEMP_TAG}",
-          '-p', "WAIVERDB_VERSION=${waiverdb_version}",
-          '-p', "WAIVERDB_IMAGESTREAM_NAME=${params.IMAGESTREAM_NAME}",
-          '-p', "WAIVERDB_IMAGESTREAM_NAMESPACE=${params.IMAGESTREAM_NAMESPACE}",
-        )
-        def build = c3i.buildAndWait(script: this, objs: processed)
-        echo 'Container build succeeds.'
-        def ocpBuild = build.object()
-        env.RESULTING_IMAGE_REF = ocpBuild.status.outputDockerImageReference
-        env.RESULTING_IMAGE_DIGEST = ocpBuild.status.output.to.imageDigest
-        def imagestream = openshift.selector('is', ['app': env.BUILDCONFIG_INSTANCE_ID]).object()
-        env.RESULTING_IMAGE_REPOS = imagestream.status.dockerImageRepository
-        env.RESULTING_TAG = env.TEMP_TAG
+        openshift.withProject(env.PIPELINE_ID) {
+          // OpenShift BuildConfig doesn't support specifying a tag name at build time.
+          // We have to create a new BuildConfig for each container build.
+          // Create a BuildConfig from a seperated Template.
+          echo 'Creating a BuildConfig for container build...'
+          def template = readYaml file: 'openshift/waiverdb-container-template.yaml'
+          def processed = openshift.process(template,
+            "-p", "NAME=${env.BUILDCONFIG_INSTANCE_ID}",
+            '-p', "WAIVERDB_GIT_REPO=${params.GIT_REPO}",
+            // A pull-request branch, like pull/123/head, cannot be built with commit ID
+            // because refspec cannot be customized in an OpenShift build .
+            '-p', "WAIVERDB_GIT_REF=${env.PR_NO ? env.GIT_REPO_REF : env.GIT_COMMIT}",
+            '-p', "WAIVERDB_IMAGE_TAG=${env.TEMP_TAG}",
+            '-p', "WAIVERDB_VERSION=${waiverdb_version}",
+            '-p', "WAIVERDB_IMAGESTREAM_NAME=waiverdb",
+            '-p', "WAIVERDB_IMAGESTREAM_NAMESPACE=${env.PIPELINE_ID}",
+          )
+          def build = c3i.buildAndWait(script: this, objs: processed)
+          echo 'Container build succeeds.'
+          def ocpBuild = build.object()
+          env.RESULTING_IMAGE_REF = ocpBuild.status.outputDockerImageReference
+          env.RESULTING_IMAGE_DIGEST = ocpBuild.status.output.to.imageDigest
+          def imagestream = openshift.selector('is', ['app': env.BUILDCONFIG_INSTANCE_ID]).object()
+          env.RESULTING_IMAGE_REPOS = imagestream.status.dockerImageRepository
+          env.RESULTING_TAG = env.TEMP_TAG
+        }
       }
     }
   }
@@ -159,17 +161,6 @@ stage('Build container') {
     failure {
       echo "Failed to build container image ${env.TEMP_TAG}."
     }
-    cleanup {
-      script {
-        openshift.withCluster() {
-          echo 'Tearing down...'
-          openshift.selector('bc', [
-            'app': env.BUILDCONFIG_INSTANCE_ID,
-            'template': 'waiverdb-container-template',
-            ]).delete()
-        }
-      }
-    }
   }
 }
 stage("Functional tests phase") {
@@ -181,18 +172,6 @@ stage("Functional tests phase") {
         }
       }
     }
-    stage('Cleanup') {
-      // Cleanup all test environments that were created 1 hour ago in case of failures of previous cleanups.
-      steps {
-        script {
-          openshift.withCluster() {
-            openshift.withProject(env.PIPELINE_ID) {
-              c3i.cleanup(script: this, age: 60, 'waiverdb')
-            }
-          }
-        }
-      }
-    }
     stage('Run functional tests') {
       environment {
         // Jenkins BUILD_TAG could be too long (> 63 characters) for OpenShift to consume
@@ -202,24 +181,26 @@ stage("Functional tests phase") {
         echo "Container image ${env.IMAGE} will be tested."
         script {
           openshift.withCluster() {
-            // Don't set ENVIRONMENT_LABEL in the environment block! Otherwise you will get 2 different UUIDs.
-            env.ENVIRONMENT_LABEL = "test-${env.TEST_ID}"
-            def template = readYaml file: 'openshift/waiverdb-test-template.yaml'
-            def webPodReplicas = 1 // The current quota in UpShift is agressively limited
-            echo "Creating testing environment with TEST_ID=${env.TEST_ID}..."
-            def models = openshift.process(template,
-              '-p', "TEST_ID=${env.TEST_ID}",
-              '-p', "WAIVERDB_APP_IMAGE=${env.IMAGE}",
-              '-p', "WAIVERDB_REPLICAS=${webPodReplicas}",
-            )
-            c3i.deployAndWait(script: this, objs: models, timeout: 15)
-            def appPod = openshift.selector('pods', ['environment': env.ENVIRONMENT_LABEL, 'service': 'web']).object()
-            env.IMAGE_DIGEST = appPod.status.containerStatuses[0].imageID.split('@')[1]
-            // Run functional tests
-            def route_hostname = openshift.selector('routes', ['environment': env.ENVIRONMENT_LABEL]).object().spec.host
-            echo "Running tests against https://${route_hostname}/"
-            withEnv(["WAIVERDB_TEST_URL=https://${route_hostname}/"]) {
-              sh 'py.test-3 -v --junitxml=junit-functional-tests.xml functional-tests/'
+            openshift.withProject(env.PIPELINE_ID) {
+              // Don't set ENVIRONMENT_LABEL in the environment block! Otherwise you will get 2 different UUIDs.
+              env.ENVIRONMENT_LABEL = "test-${env.TEST_ID}"
+              def template = readYaml file: 'openshift/waiverdb-test-template.yaml'
+              def webPodReplicas = 1 // The current quota in UpShift is agressively limited
+              echo "Creating testing environment with TEST_ID=${env.TEST_ID}..."
+              def models = openshift.process(template,
+                '-p', "TEST_ID=${env.TEST_ID}",
+                '-p', "WAIVERDB_APP_IMAGE=${env.IMAGE}",
+                '-p', "WAIVERDB_REPLICAS=${webPodReplicas}",
+              )
+              c3i.deployAndWait(script: this, objs: models, timeout: 15)
+              def appPod = openshift.selector('pods', ['environment': env.ENVIRONMENT_LABEL, 'service': 'web']).object()
+              env.IMAGE_DIGEST = appPod.status.containerStatuses[0].imageID.split('@')[1]
+              // Run functional tests
+              def route_hostname = openshift.selector('routes', ['environment': env.ENVIRONMENT_LABEL]).object().spec.host
+              echo "Running tests against https://${route_hostname}/"
+              withEnv(["WAIVERDB_TEST_URL=https://${route_hostname}/"]) {
+                sh 'py.test-3 -v --junitxml=junit-functional-tests.xml functional-tests/'
+              }
             }
           }
         }
@@ -230,21 +211,9 @@ stage("Functional tests phase") {
             junit 'junit-functional-tests.xml'
             archiveArtifacts artifacts: 'junit-functional-tests.xml'
             openshift.withCluster() {
-              /* Extract logs for debugging purposes */
-              openshift.selector('deploy,pods', ['environment': env.ENVIRONMENT_LABEL]).logs()
-            }
-          }
-        }
-        cleanup {
-          script {
-            openshift.withCluster() {
-              /* Tear down everything we just created */
-              echo "Tearing down test resources..."
-              try {
-                openshift.selector('dc,deploy,rc,configmap,secret,svc,route',
-                      ['environment': env.ENVIRONMENT_LABEL]).delete()
-              } catch (e) {
-                echo "Failed to tear down test resources: ${e.message}"
+              openshift.withProject(env.PIPELINE_ID) {
+                /* Extract logs for debugging purposes */
+                openshift.selector('deploy,pods', ['environment': env.ENVIRONMENT_LABEL]).logs()
               }
             }
           }
@@ -259,67 +228,14 @@ stage("Functional tests phase") {
           // Don't send a message if the job fails before getting the image digest.
           return;
         }
-        if (!env.MESSAGING_PROVIDER) {
-          // Don't send a message if messaging provider is not configured
-          return
-        }
-        // currentBuild.result == null || currentBuild.result == 'SUCCESS' indicates a successful build,
-        // because it's possible that the pipeline engine hasn't set the value nor seen an error when reaching to this line.
-        // See example code in https://jenkins.io/doc/book/pipeline/jenkinsfile/#deploy
-        def sendResult = sendCIMessage \
-          providerName: params.MESSAGING_PROVIDER, \
-          overrides: [topic: 'VirtualTopic.eng.ci.container-image.test.complete'], \
-          messageType: 'Custom', \
-          messageProperties: '', \
-          messageContent: """
-          {
-            "ci": {
-              "name": "C3I Jenkins",
-              "team": "DevOps",
-              "url": "${env.JENKINS_URL}",
-              "docs": "https://pagure.io/waiverdb/blob/master/f/openshift",
-              "irc": "#pnt-devops-dev",
-              "email": "pnt-factory2-devel@redhat.com",
-              "environment": "stage"
-            },
-            "run": {
-              "url": "${env.BUILD_URL}",
-              "log": "${env.BUILD_URL}/console",
-              "debug": "",
-              "rebuild": "${env.BUILD_URL}/rebuild/parametrized"
-            },
-            "artifact": {
-              "type": "container-image",
-              "repository": "factory2/waiverdb",
-              "digest": "${env.IMAGE_DIGEST}",
-              "nvr": "${env.IMAGE}",
-              "issuer": "c3i-jenkins",
-              "scratch": ${params.GIT_REPO_REF != params.PAGURE_MAIN_BRANCH},
-              "id": "waiverdb@${env.IMAGE_DIGEST}"
-            },
-            "system":
-               [{
-                  "os": "${params.JENKINS_AGENT_IMAGE}",
-                  "provider": "openshift",
-                  "architecture": "x86_64"
-               }],
-            "type": "integration",
-            "category": "dev",
-            "status": "${currentBuild.result == null || currentBuild.result == 'SUCCESS' ? 'passed':'failed'}",
-            "xunit": "${env.BUILD_URL}/artifacts/junit-functional-tests.xml",
-            "generated_at": "${new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))}",
-            "namespace": "c3i",
-            "version": "0.1.0"
-          }
-          """
-        if (sendResult.getMessageId()) {
-          // echo sent message id and content
-          echo 'Successfully sent the test result to ResultsDB.'
-          echo "Message ID: ${sendResult.getMessageId()}"
-          echo "Message content: ${sendResult.getMessageContent()}"
-        } else {
-          echo 'Failed to sent the test result to ResultsDB.'
-        }
+        c3i.sendResultToMessageBus(
+          imageRef: env.IMAGE,
+          digest: env.IMAGE_DIGEST,
+          environment: 'dev',
+          scratch: params.GIT_REPO_REF != params.PAGURE_MAIN_BRANCH,
+          docs: 'https://pagure.io/waiverdb/blob/master/f/openshift',
+          xunit: "${env.BUILD_URL}/artifacts/junit-functional-tests.xml"
+        )
       }
     }
   }
diff --git a/openshift/waiverdb-test-template.yaml b/openshift/waiverdb-test-template.yaml
index 1627b3e..3b0ff95 100644
--- a/openshift/waiverdb-test-template.yaml
+++ b/openshift/waiverdb-test-template.yaml
@@ -172,7 +172,7 @@ objects:
 - apiVersion: v1
   kind: Route
   metadata:
-    name: "waiverdb-test-${TEST_ID}-web"
+    name: "waiverdb-test-web"
     labels:
       environment: "test-${TEST_ID}"
       app: waiverdb

From b65b7986c357137e04a1f7ca39ce3aace72d039b Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Mar 16 2020 11:39:12 +0000
Subject: [PATCH 2/2] Use tag instead digest for request pipeline


Waiverdb playbooks define imagestream with does not work with digests.

---

diff --git a/openshift/pipelines/snippets/waiverdb-full-integration-test.groovy b/openshift/pipelines/snippets/waiverdb-full-integration-test.groovy
index d0ae046..e9a79eb 100644
--- a/openshift/pipelines/snippets/waiverdb-full-integration-test.groovy
+++ b/openshift/pipelines/snippets/waiverdb-full-integration-test.groovy
@@ -7,7 +7,7 @@ stage('Run integration tests') {
             openshift.withProject(params.PIPELINE_AS_A_SERVICE_BUILD_NAMESPACE) {
               c3i.buildAndWait(script: this, objs: "bc/pipeline-as-a-service",
                 '-e', "DEFAULT_IMAGE_TAG=${env.ENVIRONMENT}",
-                '-e', "WAIVERDB_IMAGE=${env.IMAGE}",
+                '-e', "WAIVERDB_IMAGE=${env.TRACKED_CONTAINER_REPO}:${env.TRACKED_TAG}",
                 '-e', "PIPELINE_ID=${env.PIPELINE_ID}",
                 '-e', "PAAS_DOMAIN=${env.PAAS_DOMAIN}",
                 '-e', "SERVICES_TO_DEPLOY='resultsdb-updater datanommer greenwave resultsdb umb waiverdb datagrepper krb5 ldap koji-hub'",

