/**
 * Test salt formulas pipeline
 *  DEFAULT_GIT_REF
 *  DEFAULT_GIT_URL
 *  CREDENTIALS_ID
 *  KITCHEN_TESTS_PARALLEL
 */
common = new com.mirantis.mk.Common()
def gerrit = new com.mirantis.mk.Gerrit()
def ruby = new com.mirantis.mk.Ruby()

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

futureFormulas = []
failedFormulas = []

def setupRunner(defaultGitRef, defaultGitUrl) {
  def branches = [:]
  for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
    branches["Runner ${i}"] = {
      while (futureFormulas && !failedFormulas) {
        def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
        if (!currentFormula) {
          continue
        }
        futureFormulas.remove(currentFormula)
        try {
          triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
        } catch (Exception e) {
          failedFormulas << currentFormula
          common.warningMsg("Test of ${currentFormula} failed :  ${e}")
        }
      }
    }
  }
  parallel branches
}

def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
  common.infoMsg("Test of ${testEnv} starts")
  build job: "test-salt-formulas-env", parameters: [
    [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
    [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
    [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
    [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
    [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
    [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION]
  ]
}

node("python") {
  try {
    stage("checkout") {
      if (gerritRef) {
        // job is triggered by Gerrit
        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 {
          // test if change aren't already merged
          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 = "${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("test") {
      if (checkouted) {
        sh("make clean")
        sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
      }
    }
    stage("kitchen") {
      if (checkouted) {
        if (fileExists(".kitchen.yml")) {
          common.infoMsg(".kitchen.yml found, running kitchen tests")
          def kitchenEnvs = []
          def filteredEnvs = []
          if (fileExists(".travis.yml")) {
            common.infoMsg(".travis.yml file found.")
            def kitchenConfigYML = readYaml(file: ".travis.yml")
            if (kitchenConfigYML.containsKey("env")) {
              kitchenEnvs = kitchenConfigYML["env"]
            }
          } else {
            common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
          }
          common.infoMsg("Running kitchen testing in parallel mode")
          if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
            kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
            common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
          }
          if (kitchenEnvs != null && kitchenEnvs != '') {
            def acc = 0
            common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
            for (int i = 0; i < kitchenEnvs.size(); i++) {
              futureFormulas << kitchenEnvs[i]
            }
            setupRunner(defaultGitRef, defaultGitUrl)
          } else {
            common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
          }
        }
      }
    }
    if (failedFormulas) {
      currentBuild.result = "FAILURE"
      common.warningMsg("The following tests failed: ${failedFormulas}")
    }
  } catch (Throwable e) {
    // If there was an error or exception thrown, the build failed
    currentBuild.result = "FAILURE"
    currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
    throw e
  } finally {
    if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
      common.errorMsg("----------------KITCHEN LOG:---------------")
      println readFile(".kitchen/logs/kitchen.log")
    }
    common.sendNotification(currentBuild.result, "", ["slack"])
  }
}