// Mainframe regression tests with WND Test (see docs/testing.md).
// Put this Jenkinsfile in the repository that holds your tests (made with:
// wnd-test init mainframe-tests).
//
// Mirror the portal runner ZIP in your internal artifact store.
// Credentials (Manage Jenkins > Credentials):
//   wnd-test-download-url  "Secret text": the internal runner archive URL
//   mainframe-test-login   "Username with password": the user the suite's logon signs on as
//   wnd-terminal-license   "Secret file": your license file. Only tests that connect
//                          to a host need it; tests that play a recording run without one.

pipeline {
  agent { label 'linux' }
  stages {
    stage('Get wnd-test') {
      steps {
        withCredentials([string(credentialsId: 'wnd-test-download-url', variable: 'WND_TEST_DOWNLOAD_URL')]) {
          sh '''
          curl -fsSL -o wnd-test.zip "${WND_TEST_DOWNLOAD_URL:?Set WND_TEST_DOWNLOAD_URL to your internal wnd-test archive URL}"
          rm -rf .wnd-test && unzip -q wnd-test.zip -d .wnd-test
          '''
        }
      }
    }
    stage('Mainframe tests') {
      steps {
        withCredentials([
          usernamePassword(credentialsId: 'mainframe-test-login', usernameVariable: 'TEST_USER', passwordVariable: 'TEST_PASSWORD'),
          file(credentialsId: 'wnd-terminal-license', variable: 'WND_TERMINAL_LICENSE'),
        ]) {
          script {
            // 0: every test passed. 1: a test failed. 2: the suite couldn't run.
            def code = sh(script: './.wnd-test/wnd-test mainframe-tests --junit wnd-test.xml', returnStatus: true)
            if (code == 1) {
              unstable('Some mainframe tests failed. The report says which, with the screen at each failure.')
            } else if (code != 0) {
              error('The mainframe tests couldn\'t run. The console log says why.')
            }
          }
        }
      }
    }
  }
  post {
    always {
      junit allowEmptyResults: true, testResults: 'wnd-test.xml'
      archiveArtifacts allowEmptyArchive: true, artifacts: 'mainframe-tests/wnd-test-report/**'
    }
  }
}
