def config = jobConfig {
    cron = '@midnight'
    nodeLabel = 'docker-oraclejdk8'
    realJobPrefixes = ['system-test-python-client']
    owner = 'client'
    slackChannel = 'clients-eng'
    timeoutHours = 3
}

def job = {
    configureGitSSH("github/confluent_jenkins", "private_key")
    withVaultEnv([["artifactory/tools_jenkins", "user", "TOOLS_ARTIFACTORY_USER"],
        ["artifactory/tools_jenkins", "password", "TOOLS_ARTIFACTORY_PASSWORD"],
        ["sonatype/confluent", "user", "SONATYPE_OSSRH_USER"],
        ["sonatype/confluent", "password", "SONATYPE_OSSRH_PASSWORD"]]) {
        withVaultFile([["maven/jenkins_maven_global_settings", "settings_xml",
            "/home/jenkins/.m2/settings.xml", "MAVEN_GLOBAL_SETTINGS"],
            ["muckrake/2017-06-01", "pem", "muckrake-2017-06-01.pem", "MUCKRAKE_PEM"]]) {
            stage("Run tests") {
                sh 'tests/system/run-tests.sh'
            }
        }
    }
}

def post = {
    withVaultFile([["muckrake/2017-06-01", "pem", "muckrake-2017-06-01.pem", "MUCKRAKE_PEM"]]) {
        stage('Cleanup tests') {
            sh 'tests/system/cleanup-tests.sh'
        }

        if (config.isRealJob) {
            stage('Upload test results') {
                def s3Bucket = sh(
                    script: 'python jenkins-common/scripts/system-tests/test-result-storage/get_upload_location.py ' +
                            '--project muckrake --branch python-client-' + env.BRANCH_NAME, returnStdout: true).trim()
                def resultsDir = sh(script: 'ls -tr results | tail -1', returnStdout: true).trim()

                config.testResultSpecs['ducktape'] = "results/$resultsDir/report.json"
                config.testResultSpecs['ducktape_results_url'] = "s3://$s3Bucket/$resultsDir"

                sh '''
                    tests/system/upload-tests.sh muckrake python-client-$BRANCH_NAME \
                        https://github.com/confluentinc/confluent-kafka-python tests/kafkatest/tests/client
                '''
            }
        }
    }
}

runJob config, job, post
