/**
 * 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']) {
  timeout(time: 12, unit: 'HOURS') {
    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")
        }
      }
    }
  }
}
