/**
 * 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: 1, unit: 'HOURS') {
    node("python&&docker") {
      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")
        }
      }
    }
  }
}
