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

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

def checkouted = false

throttle(['test-formula']) {
  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_REF is null")
        }
      }
      stage("cleanup") {
        if (checkouted) {
          sh("make clean")
        }
      }
      stage("kitchen") {
        if (checkouted) {
          if (fileExists(".kitchen.yml")) {
            common.infoMsg(".kitchen.yml found, running kitchen tests")
            ruby.ensureRubyEnv()
            if (fileExists(".travis.yml")) {
              common.infoMsg(".travis.yml found, running custom kitchen init")
              def kitchenConfigYML = readYaml(file: ".travis.yml")
              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 part of kitchen test")
            if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
              def cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
              sh("find . -type f -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;")
              sh("find . -type f -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;")
              def suite = ruby.getSuiteName(KITCHEN_ENV)
              if (suite && suite != "") {
                common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
                ruby.runKitchenTests(cleanEnv, suite)
              } else {
                common.warningMsg("No SUITE was found. Running with all suites.")
                ruby.runKitchenTests(cleanEnv, "")
              }
            } else {
              throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
            }
          } else {
            throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
          }
        }
      }
    } 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")
      }
    }
  }
}
