/**
 *  Test salt models pipeline
 *  DEFAULT_GIT_URL default git url (will be used if pipeline run is not triggered by gerrit)
 *  DEFAULT_GIT_RED default git ref (branch,tag,...) (will be used if pipeline run is not triggered by gerrit)
 *  CREDENTIALS_ID Jenkins credetials id for git checkout
 *  EXTRA_FORMULAS extra formulas list for passing to salt bootstrap script
 *  MAX_CPU_PER_JOB max cpu count for one docket test instance
 *  SYSTEM_GIT_URL reclass system git URL (optional)
 *  SYSTEM_GIT_REF reclass system git URL (optional)
 *  TEST_CLUSTER_NAMES list of comma separated cluster names to test (optional, default all cluster levels)
 *  LEGACY_TEST_MODE legacy test mode flag
 *  RECLASS_IGNORE_CLASS_NOTFOUND ignore missing class flag for reclass config
 *  APT_REPOSITORY extra apt repository url
 *  APT_REPOSITORY_GPG extra apt repository url GPG
 */

def gerrit = new com.mirantis.mk.Gerrit()
def ssh = new com.mirantis.mk.Ssh()
def git = new com.mirantis.mk.Git()

def config_node_name_pattern
try {
  config_node_name_pattern = CONFIG_NODE_NAME_PATTERN
} catch (MissingPropertyException e) {
  config_node_name_pattern = "cfg01"
}

def gerritRef
try {
  gerritRef = GERRIT_REFSPEC
} catch (MissingPropertyException e) {
  gerritRef = null
}

def formulasSource
try {
  formulasSource = FORMULAS_SOURCE
} catch (MissingPropertyException e) {
  formulasSource = "pkg"
}

def testClusterNames
try {
  testClusterNames = TEST_CLUSTER_NAMES
} catch (MissingPropertyException e) {
  testClusterNames = ""
}

def defaultGitRef, defaultGitUrl
try {
    defaultGitRef = DEFAULT_GIT_REF
    defaultGitUrl = DEFAULT_GIT_URL
} catch (MissingPropertyException e) {
    defaultGitRef = null
    defaultGitUrl = null
}

def checkouted = false
futureNodes = []
failedNodes = false
common = new com.mirantis.mk.Common()

def setupRunner() {

  def branches = [:]
  for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) {
    branches["Runner ${i}"] = {
      while (futureNodes && !failedNodes) {
        def currentNode = futureNodes[0] ? futureNodes[0] : null
        if (!currentNode) {
          continue
        }

        def clusterName = currentNode[2]
        futureNodes.remove(currentNode)
        try {
            triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4])
        } catch (Exception e) {
          if (e.getMessage().contains("completed with status ABORTED")) {
            common.warningMsg("Test of ${clusterName} failed because the test was aborted :  ${e}")
            futureNodes << currentNode
          } else {
            common.warningMsg("Test of ${clusterName} failed :  ${e}")
            failedNodes = true
          }
        }
      }
    }
  }

  if (branches) {
    parallel branches
  }
}

def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
  common.infoMsg("Test of ${clusterName} starts")
  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: 'CLUSTER_NAME', value: clusterName],
    [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
    [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
    [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
    [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION],
    [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
    [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
    [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
    [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
    [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
    [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()],
    [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY],
    [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG]
  ]
}

def _clusterTestEnabled(infraYMLConfig){
  if (infraYMLConfig["parameters"].containsKey("_jenkins")) {
    if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) {
      return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"];
    }
  }
  // ci tests are enabled by default
  return true;
}

timeout(time: 12, unit: 'HOURS') {
  node("python") {
    try{
      stage("checkout") {
        if (gerritRef) {
          // job is triggered by Gerrit
          // test if change aren't already merged
          def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
          // test if gerrit change is already Verified
          if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
            common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
          // test WIP contains in commit message
          }else if (gerritChange.commitMessage.contains("WIP")) {
            common.successMsg("Commit message contains WIP, skipping tests") // do nothing
          } else {
            def merged = gerritChange.status == "MERGED"
            if(!merged){
              checkouted = gerrit.gerritPatchsetCheckout ([
                credentialsId : CREDENTIALS_ID
              ])
            } else{
              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")
        }
      }

      stage("Check YAML") {
        common.infoMsg("Checking YAML syntax for changed files")
        def syntaxCheckStatus = sh(script:"set +x;git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD  | grep .yml | xargs -I {}  python -c \"import yaml; yaml.load(open('{}', 'r'))\" \\;", returnStatus:true)
        if(syntaxCheckStatus > 0){
          common.errorMsg("YAML syntax check failed!")
        }
      }

      stage("test-nodes") {
        if (checkouted) {
          def modifiedClusters = null
          // testing modified cluster is used only if test was triggered by gerrit
          if (gerritRef) {
            def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
            if (checkChange == 1) {
              modifiedClusters = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize()
            }
          }

          def infraYMLs = []
          // list of cluster names can be explicitly given
          if (testClusterNames != null && testClusterNames != "") {
            common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}")
            def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|")
            infraYMLs = sh(script:"set +x;find ./classes/ -regextype posix-egrep -regex '.*cluster/(${clusterNameRegex}){1}/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
          } else {
            common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested")
            // else we want to test all cluster levels found
            infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
            def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()

            // create a list of cluster names present in cluster folder
            def infraList = []
            for (elt in infraYMLs) {
              infraList << elt.tokenize('/')[3]
            }

            // verify we have all valid clusters loaded
            def commonList = infraList.intersect(clusterDirectories)
            def differenceList = infraList.plus(clusterDirectories)
            differenceList.removeAll(commonList)

            if (!differenceList.isEmpty()) {
              common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
            }
            if (modifiedClusters) {
              infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
              common.infoMsg("Testing only modified clusters: ${infraYMLs}")
            }
          }
          common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] })
          if (infraYMLs.size() > 0) {
            for (int i = 0; i < infraYMLs.size(); i++) {
              def infraYMLConfig = readYaml(file: infraYMLs[i])
              if (_clusterTestEnabled(infraYMLConfig)) {
                  if(!infraYMLConfig["parameters"].containsKey("_param")){
                      common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
                      continue
                  }
                  def infraParams = infraYMLConfig["parameters"]["_param"];
                  if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
                      common.warningMsg("ERROR: Cannot find _param:infra_config_hostname or _param:cluster_name or _param:cluster_domain  in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
                      continue
                  }
                  def clusterName = infraParams["cluster_name"]
                  def clusterDomain = infraParams["cluster_domain"]
                  def configHostname = infraParams["infra_config_hostname"]
                  def testTarget = String.format("%s.%s", configHostname, clusterDomain)

                  futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
              }
            }
          } else {
            common.warningMsg("List of found salt model clusters is empty, no tests will be started!")
          }

          setupRunner()

          if (failedNodes) {
            currentBuild.result = "FAILURE"
          }
        }
      }
    } catch (Throwable e) {
       currentBuild.result = "FAILURE"
       currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
       throw e
    } finally {
       common.sendNotification(currentBuild.result,"",["slack"])
    }
  }
}
