Refactor salt-models testing

The tests take too much time to be executed and they often fail due to lack
of resources. The solution to improve salt models testing is to disitribute
the work among Jenkins slave. The previous approach consisted in doing all the
tests on one slave for a given model.
Fix test for system-reclass by testing the good reclass-system version.

Change-Id: I605355bdb66d7195e42454bb87b5cc0663d13929
diff --git a/test-salt-model-node.groovy b/test-salt-model-node.groovy
new file mode 100644
index 0000000..8ec3936
--- /dev/null
+++ b/test-salt-model-node.groovy
@@ -0,0 +1,62 @@
+
+/**
+ *  Test salt models pipeline
+ *  DEFAULT_GIT_REF
+ *  DEFAULT_GIT_URL
+ *  CREDENTIALS_ID
+ *  EXTRA_FORMULAS
+ *  NODE_TARGET
+ *  SYSTEM_GIT_URL
+ *  SYSTEM_GIT_REF
+ */
+
+def common = new com.mirantis.mk.Common()
+def gerrit = new com.mirantis.mk.Gerrit()
+def saltModelTesting = new com.mirantis.mk.SaltModelTesting()
+
+def defaultGitRef = DEFAULT_GIT_REF
+def defaultGitUrl = DEFAULT_GIT_URL
+
+def checkouted = false
+node("python") {
+  try{
+    stage("checkout") {
+      if(defaultGitRef != "" && defaultGitUrl != "") {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
+      } else {
+        throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_URL or DEFAULT_GIT_REF is null")
+      }
+      if(checkouted) {
+        if (fileExists('classes/system')) {
+          if (SYSTEM_GIT_URL == "") {
+            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")
+          } else {
+            dir('classes/system') {
+              if (!gerrit.gerritPatchsetCheckout(SYSTEM_GIT_URL, SYSTEM_GIT_REF, "HEAD", CREDENTIALS_ID)) {
+                common.errorMsg("Failed to obtain system reclass with url: ${SYSTEM_GIT_URL} and ${SYSTEM_GIT_REF}")
+              }
+            }
+          }
+        }
+      }
+    }
+
+    stage("test node") {
+      if (checkouted) {
+        def workspace = common.getWorkspace()
+        saltModelTesting.setupAndTestNode(NODE_TARGET, EXTRA_FORMULAS, workspace)
+      }
+    }
+  } catch (Throwable e) {
+     // If there was an error or exception thrown, the build failed
+     currentBuild.result = "FAILURE"
+     throw e
+  } finally {
+     common.sendNotification(currentBuild.result,"",["slack"])
+  }
+}
diff --git a/test-salt-models-pipeline.groovy b/test-salt-models-pipeline.groovy
index de6a612..317cadd 100644
--- a/test-salt-models-pipeline.groovy
+++ b/test-salt-models-pipeline.groovy
@@ -5,13 +5,14 @@
  *  DEFAULT_GIT_URL
  *  CREDENTIALS_ID
  *  EXTRA_FORMULAS
+ *  SYSTEM_GIT_URL
+ *  SYSTEM_GIT_REF
  */
 
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
 def ssh = new com.mirantis.mk.Ssh()
 def git = new com.mirantis.mk.Git()
-def saltModelTesting = new com.mirantis.mk.SaltModelTesting()
 
 def gerritRef
 try {
@@ -61,43 +62,38 @@
             common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
           }
         }
+        // defaultGitUrl is passed to the triggered job
+        defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
+        defaultGitRef = GERRIT_REFSPEC
       } else if(defaultGitRef && defaultGitUrl) {
           checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
       } else {
         throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
       }
-      if(checkouted) {
-        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")
-        }
-      }
     }
 
     stage("test-nodes") {
       if(checkouted) {
-        def workspace = common.getWorkspace()
         def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
-        def buildSteps = [:]
-        def partitionSize = (nodes.size() <= PARALLEL_NODE_GROUP_SIZE.toInteger()) ? nodes.size() : PARALLEL_NODE_GROUP_SIZE.toInteger()
-        def partitions = common.partitionList(nodes, partitionSize)
-        for (int i =0; i < partitions.size();i++) {
-          def partition = partitions[i]
-          buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
-          for(int k=0; k < partition.size;k++){
-              def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
-              buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) })
-          }
+        def branches = [:]
+        for (int i = 0; i < nodes.size(); i++) {
+
+          def testTarget = sh(scipt: "basename ${nodes[i]}.yml", returnStdout: true).trim()
+          branches[${testTarget}] = {
+            build job: "test-salt-model-node", parameters: [
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
+              [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
+              [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
+              [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
+              [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
+              [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF]
+            ]}
+          parallel branches
         }
-        common.serial(buildSteps)
       }
     }
   } catch (Throwable e) {
-     // If there was an error or exception thrown, the build failed
      currentBuild.result = "FAILURE"
      throw e
   } finally {
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index 27106fe..a334020 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -25,6 +25,7 @@
 }
 def checkouted = false
 def merged = false
+def systemRefspec = "HEAD"
 try {
   stage("Checkout") {
     node() {
@@ -37,6 +38,7 @@
           checkouted = gerrit.gerritPatchsetCheckout ([
             credentialsId : gerritCredentials
           ])
+          systemRefspec = GERRIT_REFSPEC
         }
         // change defaultGit variables if job triggered from Gerrit
         defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
@@ -60,6 +62,8 @@
               build job: "test-salt-model-${cluster}", parameters: [
                 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
                 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"]
+                [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
+                [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec]
               ]
             }
           }