Refactored test pipelines for possibility of external call gerrit checkout

Change-Id: I3682290aa7d4fa8dd857bc92fe12def66ee51f0a
diff --git a/gating-pipeline.groovy b/gating-pipeline.groovy
index d3feae0..4efb430 100644
--- a/gating-pipeline.groovy
+++ b/gating-pipeline.groovy
@@ -11,9 +11,7 @@
 node("python") {
   try{
     stage("test") {
-      // TEST JOBS ARE DISABLED
-      // because you cannot pass GERRIT_REFSPEC like variables to another pipeline
-      if (false && !SKIP_TEST.equals("true")){
+      if (!SKIP_TEST.equals("true")){
         wrap([$class: 'AnsiColorBuildWrapper']) {
           def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
           def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
@@ -26,12 +24,8 @@
           if (_jobExists(testJob)) {
             common.infoMsg("Test job ${testJob} found, running")
             build job: testJob, parameters: [
-              [$class: 'StringParameterValue', name: 'GERRIT_BRANCH', value: GERRIT_BRANCH],
-              [$class: 'StringParameterValue', name: 'GERRIT_NAME', value: GERRIT_NAME],
-              [$class: 'StringParameterValue', name: 'GERRIT_HOST', value: GERRIT_HOST],
-              [$class: 'StringParameterValue', name: 'GERRIT_PORT', value: GERRIT_PORT],
-              [$class: 'StringParameterValue', name: 'GERRIT_PROJECT', value: GERRIT_PROJECT],
-              [$class: 'StringParameterValue', name: 'GERRIT_REFSPEC', value: GERRIT_REFSPEC]
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"],
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
             ]
           } else {
             common.infoMsg("Test job ${testJob} not found")
@@ -52,7 +46,7 @@
      currentBuild.result = "FAILURE"
      throw e
   } finally {
-     common.sendNotification(currentBuild.result,"",["slack"])
+     //common.sendNotification(currentBuild.result,"",["slack"])
   }
 }
 
diff --git a/test-groovy-pipeline.groovy b/test-groovy-pipeline.groovy
index 45404cb..acf2a6e 100644
--- a/test-groovy-pipeline.groovy
+++ b/test-groovy-pipeline.groovy
@@ -9,30 +9,43 @@
 gerrit = new com.mirantis.mk.Gerrit()
 common = new com.mirantis.mk.Common()
 
+def defaultGitRef, defaultGitUrl
+try {
+    defaultGitRef = DEFAULT_GIT_REF
+    defaultGitUrl = DEFAULT_GIT_URL
+} catch (MissingPropertyException e) {
+    defaultGitRef = null
+    defaultGitUrl = null
+}
+def checkouted = false
+
 node("docker"){
     try {
         stage ('Checkout source code'){
-            gerrit.gerritPatchsetCheckout ([
-              credentialsId : CREDENTIALS_ID,
-              withWipeOut : true,
-              gerritRefSpec: GERRIT_REFSPEC,
-              gerritName: GERRIT_NAME,
-              gerritHost: GERRIT_HOST,
-              gerritPort: GERRIT_PORT,
-              gerritProject: GERRIT_PROJECT,
-              gerritBranch: GERRIT_BRANCH,
+          if (gerritRef) {
+            // job is triggered by Gerrit
+            checkouted = gerrit.gerritPatchsetCheckout ([
+              credentialsId : CREDENTIALS_ID
             ])
+          } else if(defaultGitRef && defaultGitUrl) {
+              checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "master", CREDENTIALS_ID)
+          }
+          if(!checkouted){
+            common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+          }
         }
         stage ('Run Codenarc tests'){
-            def workspace = common.getWorkspace()
-            def jenkinsUID = common.getJenkinsUid()
-            def jenkinsGID = common.getJenkinsGid()
-            def gradle_report = sh (script: "docker run --rm -v ${workspace}:/usr/bin/app:rw -u ${jenkinsUID}:${jenkinsGID} ${GRADLE_IMAGE} ${GRADLE_CMD}", returnStdout: true).trim()
-            // Compilation failure doesn't fail the build
-            // Check gradle output explicitly
-            common.infoMsg(gradle_report)
-            if ( gradle_report =~ /Compilation failed/ ) {
-                throw new Exception("COMPILATION FAILED!")
+            if(checkouted){
+              def workspace = common.getWorkspace()
+              def jenkinsUID = common.getJenkinsUid()
+              def jenkinsGID = common.getJenkinsGid()
+              def gradle_report = sh (script: "docker run --rm -v ${workspace}:/usr/bin/app:rw -u ${jenkinsUID}:${jenkinsGID} ${GRADLE_IMAGE} ${GRADLE_CMD}", returnStdout: true).trim()
+              // Compilation failure doesn't fail the build
+              // Check gradle output explicitly
+              common.infoMsg(gradle_report)
+              if ( gradle_report =~ /Compilation failed/ ) {
+                  throw new Exception("COMPILATION FAILED!")
+              }
             }
         }
 
diff --git a/test-salt-formulas-pipeline.groovy b/test-salt-formulas-pipeline.groovy
index 2a3b2ac..24e5bb3 100644
--- a/test-salt-formulas-pipeline.groovy
+++ b/test-salt-formulas-pipeline.groovy
@@ -1,16 +1,51 @@
+/**
+ * Test salt formulas pipeline
+ *  DEFAULT_GIT_REF
+ *  DEFAULT_GIT_URL
+ *  CREDENTIALS_ID
+ */
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
+
+def gerritRef
+try {
+  gerritRef = GERRIT_REFSPEC
+} catch (MissingPropertyException e) {
+  gerritRef = null
+}
+
+def defaultGitRef, defaultGitUrl
+try {
+    defaultGitRef = DEFAULT_GIT_REF
+    defaultGitUrl = DEFAULT_GIT_URL
+} catch (MissingPropertyException e) {
+    defaultGitRef = null
+    defaultGitUrl = null
+}
+
+def checkouted = false;
+
 node("python") {
   try{
     stage("checkout") {
-      gerrit.gerritPatchsetCheckout ([
+      if (gerritRef) {
+        // job is triggered by Gerrit
+        checkouted = gerrit.gerritPatchsetCheckout ([
           credentialsId : CREDENTIALS_ID
-      ])
+        ])
+      } else if(defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "master", CREDENTIALS_ID)
+      }
+      if(!checkouted){
+        common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+      }
     }
     stage("test") {
-      wrap([$class: 'AnsiColorBuildWrapper']) {
-        sh("make clean")
-        sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
+      if(checkouted){
+        wrap([$class: 'AnsiColorBuildWrapper']) {
+          sh("make clean")
+          sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
+        }
       }
     }
   } catch (Throwable e) {
diff --git a/test-salt-models-pipeline.groovy b/test-salt-models-pipeline.groovy
index 7ac0e81..1a31f88 100644
--- a/test-salt-models-pipeline.groovy
+++ b/test-salt-models-pipeline.groovy
@@ -1,3 +1,11 @@
+
+/**
+ *  Test salt models pipeline
+ *  DEFAULT_GIT_REF
+ *  DEFAULT_GIT_URL
+ *  CREDENTIALS_ID
+ */
+
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
 def ssh = new com.mirantis.mk.Ssh()
@@ -10,53 +18,46 @@
   gerritRef = null
 }
 
-def systemGitRef, systemGitUrl
+def defaultGitRef, defaultGitUrl
 try {
-    systemGitRef = RECLASS_SYSTEM_GIT_REF
-    systemGitUrl = RECLASS_SYSTEM_GIT_URL
+    defaultGitRef = DEFAULT_GIT_REF
+    defaultGitUrl = DEFAULT_GIT_URL
 } catch (MissingPropertyException e) {
-    systemGitRef = null
-    systemGitUrl = null
+    defaultGitRef = null
+    defaultGitUrl = null
 }
+def checkouted = false
 
 node("python") {
   try{
     stage("checkout") {
       if (gerritRef) {
-        gerrit.gerritPatchsetCheckout ([
-          credentialsId : CREDENTIALS_ID,
-          gerritRefSpec: GERRIT_REFSPEC,
-          gerritName: GERRIT_NAME,
-          gerritHost: GERRIT_HOST,
-          gerritPort: GERRIT_PORT,
-          gerritProject: GERRIT_PROJECT,
-          gerritBranch: GERRIT_BRANCH,
+        // job is triggered by Gerrit
+        checkouted = gerrit.gerritPatchsetCheckout ([
+          credentialsId : CREDENTIALS_ID
         ])
-      } else {
-        git.checkoutGitRepository('.', GIT_URL, "master", CREDENTIALS_ID)
+      } else if(defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "master", CREDENTIALS_ID)
       }
-
-      if (fileExists('classes/system')) {
-        ssh.prepareSshAgentKey(CREDENTIALS_ID)
-        dir('classes/system') {
-          remoteUrl = git.getGitRemote()
-          ssh.ensureKnownHosts(remoteUrl)
-        }
-        ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
-
-        if (systemGitRef) {
-          common.infoMsg("Fetching alternate system reclass (${systemGitUrl} ${systemGitRef})")
+      if(checkouted){
+        if (fileExists('classes/system')) {
+          ssh.prepareSshAgentKey(CREDENTIALS_ID)
           dir('classes/system') {
-            ssh.ensureKnownHosts(RECLASS_SYSTEM_GIT_URL)
-            ssh.agentSh("git fetch ${systemGitUrl} ${systemGitRef} && git checkout FETCH_HEAD")
+            remoteUrl = git.getGitRemote()
+            ssh.ensureKnownHosts(remoteUrl)
           }
+          ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
         }
+      }else{
+        common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
       }
     }
     stage("test") {
-      timeout(1440) {
-        wrap([$class: 'AnsiColorBuildWrapper']) {
-          sh("make test")
+      if(checkouted){
+        timeout(1440) {
+          wrap([$class: 'AnsiColorBuildWrapper']) {
+            sh("make test")
+          }
         }
       }
     }
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index 6094b43..a9dcba4 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -25,8 +25,8 @@
       def cluster = testModels[i]
       branches["${cluster}"] = {
         build job: "test-salt-model-${cluster}", parameters: [
-          [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"],
-          [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: GERRIT_REFSPEC]
+          [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"],
+          [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
         ]
       }
     }