/**
 * Test salt formulas pipeline
 *  DEFAULT_GIT_REF
 *  DEFAULT_GIT_URL
 *  CREDENTIALS_ID
 *  KITCHEN_TESTS_PARALLEL
 */
def 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

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")
          }
        }
      } 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")
          ruby.ensureRubyEnv()
          def kitchenEnvs = []
          def filteredEnvs = []
          if(fileExists(".travis.yml")){
            common.infoMsg(".travis.yml found, running custom kitchen init")
            def kitchenConfigYML = readYaml(file: ".travis.yml")
            if(kitchenConfigYML.containsKey("env")){
              kitchenEnvs=kitchenConfigYML["env"]
            }
            def kitchenInit = kitchenConfigYML["install"]
            def kitchenInstalled = false
            if(kitchenInit && !kitchenInit.isEmpty()){
              for(int i=0; i<kitchenInit.size(); i++){
                if(kitchenInit[i].trim().startsWith("test -e Gemfile")){ //found Gemfile config
                  common.infoMsg("Custom Gemfile configuration found, using them")
                  ruby.installKitchen(kitchenInit[i].trim())
                  kitchenInstalled = true
                }
              }
            }
            if(!kitchenInstalled){
              ruby.installKitchen()
            }
          }else{
            common.infoMsg(".travis.yml not found, running default kitchen init")
            ruby.installKitchen()
          }
          common.infoMsg("Running kitchen testing, parallel mode: " + KITCHEN_TESTS_PARALLEL.toBoolean())

          if(CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != ''){
              filteredEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
            } else {
              filteredEnvs = ruby.filterKitchenEnvs(kitchenEnvs).unique()
            }
            // Allow custom filteredEnvs in case of empty kitchenEnvs
          if((kitchenEnvs && !kitchenEnvs.isEmpty() && !filteredEnvs.isEmpty()) || ((kitchenEnvs==null || kitchenEnvs=='') && !filteredEnvs.isEmpty())){
            for(int i=0; i<filteredEnvs.size(); i++){
              common.infoMsg("Found " + filteredEnvs.size() + " environment, kitchen running with env number " + (i+1) + ": " + filteredEnvs[i].trim())
              ruby.runKitchenTests(filteredEnvs[i].trim(), KITCHEN_TESTS_PARALLEL.toBoolean())
            }
          } else {
            ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean())
          }
        } else {
          common.infoMsg(".kitchen.yml not found")
        }
      }
    }
  } catch (Throwable e) {
     // If there was an error or exception thrown, the build failed
     currentBuild.result = "FAILURE"
     ruby.runKitchenCommand("destroy")
     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"])
  }
}

