/**
 * Test salt formulas pipeline
 *  DEFAULT_GIT_REF
 *  DEFAULT_GIT_URL
 *  CREDENTIALS_ID
 *  KITCHEN_TESTS_PARALLEL
 *  SMOKE_TEST_DOCKER_IMG  Docker image for run test (default "ubuntu:16.04")
 */
common = new com.mirantis.mk.Common()
def gerrit = new com.mirantis.mk.Gerrit()
def ruby = new com.mirantis.mk.Ruby()

def gerritRef = env.GERRIT_REFSPEC ?: null
def defaultGitRef = env.DEFAULT_GIT_REF ?: null
def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
def saltVersion = env.SALT_VERSION ?: ""
def dockerLib = new com.mirantis.mk.Docker()
def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")

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) {
          if (e.getMessage().contains("completed with status ABORTED")) {
            common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
            futureFormulas << currentFormula
          } else {
            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]
  ]
}
timeout(time: 2, unit: 'HOURS') {
  node(slaveNode) {
    try {
      if (fileExists("tests/build")) {
        common.infoMsg('Cleaning test env')
        sh ("sudo rm -rf tests/build")
      }
      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) {
          try {
            // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
            withEnv(["SALT_VERSION=${saltVersion}"]) {
              img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
                sh('''#!/bin/bash -xe
                      cd /etc/apt/
                      echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial main restricted universe" > sources.list
                      echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial-updates main restricted universe" >> sources.list
                      echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
                      echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
                      echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
                      apt-get update
                      apt-get install -y git-core wget curl apt-transport-https
                      apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
                      cd /formula/
                      make clean
                      make test
                      make clean
                      ''')
              }
            }
          }
          finally {
            if (fileExists("tests/build")) {
              common.infoMsg('Cleaning test env')
              sh ("sudo rm -rf tests/build")
            }
          }
        }

      }
    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")
      }
    }
  }
}
