Skip to content

Commit

Permalink
Merge pull request rancher#45071 from slickwarren/cwarren/2.9/provisi…
Browse files Browse the repository at this point in the history
…oning-sanity-tests

[2.9] adding new jenkinsfile for QA release validation
  • Loading branch information
slickwarren authored Apr 10, 2024
2 parents ac41922 + 93f27ba commit 86f85b2
Showing 1 changed file with 208 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#!groovy

// RANCHER_VERSION resolution is first via Jenkins Build Parameter RANCHER_VERSION fed in from console,
// then from $DOCKER_TRIGGER_TAG which is sourced from the Docker Hub Jenkins plugin webhook.

def rancher_version() {
try { if ('' != RANCHER_VERSION) { return RANCHER_VERSION } }
catch (MissingPropertyException e) {}

try { return DOCKER_TRIGGER_TAG }
catch (MissingPropertyException e) {}

echo 'Neither RANCHER_VERSION nor DOCKER_TRIGGER_TAG have been specified!'
error()
}

def lastBuildResult() {
def previous_build = currentBuild.getPreviousBuild()
if ( null != previous_build ) { return previous_build.result } else { return 'UNKNOWN' }
}

def via_webhook() {
try {
def foo = DOCKER_TRIGGER_TAG
return true
} catch(MissingPropertyException) {
return false
}
}

// Filter out Docker Hub tags like 'latest', 'master', 'enterprise'.
// Just want things like v1.2*
def branch = "v2.1"
def rancher_version = rancher_version()
def String rancher_version_regex = "^v[\\d]\\.[\\d]\\.[\\d][\\-rc\\d]+\$"

if ( true == via_webhook() && (!(rancher_version ==~ rancher_version_regex)) ) {
println("Received RANCHER_VERSION \'${rancher_version}\' via webhook which does not match regex \'${rancher_version_regex}\'.")
println("** This will **not** result in a pipeline run.")
currentBuild.result = lastBuildResult()
} else {
if (rancher_version.startsWith("v2.2") || rancher_version.startsWith("v2.3") || rancher_version == "master-head") {
branch = "release/v2.9"
}
if (env.BRANCH) {
branch = "${BRANCH}"
}
try {
node {
def rootPath = "/src/rancher-validation/"
def job_name = "${JOB_NAME}"
if (job_name.contains('/')) {
job_names = job_name.split('/')
job_name = job_names[job_names.size() - 1]
}

def setupContainer = "${job_name}${env.BUILD_NUMBER}_setup"
def deleteContainer = "${job_name}${env.BUILD_NUMBER}_delete"

def deployPytestOptions = "-k test_deploy_rancher_server"

def deletePytestOptions = "-k test_delete_rancher_server"

def setupResultsOut = "setup-results.xml"
def deleteResultsOut = "delete-results.xml"

def imageName = "rancher-validation-${job_name}${env.BUILD_NUMBER}"
def testsDir = "tests/v3_api/"

def envFile = ".env"
def rancherConfig = "rancher_env.config"

def TIMEOUT = "6h"

wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm', 'defaultFg': 2, 'defaultBg':1]) {
withFolderProperties {
withCredentials([ string(credentialsId: 'AWS_ACCESS_KEY_ID', variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'AWS_SECRET_ACCESS_KEY', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: 'AWS_SSH_PEM_KEY', variable: 'AWS_SSH_PEM_KEY'),
string(credentialsId: 'AZURE_CLIENT_ID', variable: 'AZURE_CLIENT_ID'),
string(credentialsId: 'AZURE_CLIENT_SECRET', variable: 'AZURE_CLIENT_SECRET'),
string(credentialsId: 'AZURE_SUBSCRIPTION_ID', variable: 'AZURE_SUBSCRIPTION_ID'),
string(credentialsId: 'ADMIN_PASSWORD', variable: 'ADMIN_PASSWORD'),
string(credentialsId: 'USER_PASSWORD', variable: 'USER_PASSWORD')]) {

println "Branch: ${branch}"
stage('Checkout') {
deleteDir()
checkout([
$class: 'GitSCM',
branches: [[name: "*/${branch}"]],
extensions: scm.extensions + [[$class: 'CleanCheckout']],
userRemoteConfigs: scm.userRemoteConfigs
])
}

dir ("tests/validation/") {
stage('Configure and Build') {
if (env.AWS_SSH_PEM_KEY && env.AWS_SSH_KEY_NAME) {
dir(".ssh/") {
def decoded = new String(env.AWS_SSH_PEM_KEY.decodeBase64())
writeFile file: env.AWS_SSH_KEY_NAME, text: decoded
}
}
sh "./tests/v3_api/scripts/configure.sh"
sh "./tests/v3_api/scripts/build.sh"
}

try {
stage('Deploy Rancher server 1') {
try {
// deploy rancher server
sh "docker run --name ${setupContainer} -t --env-file ${envFile} " +
"${imageName} /bin/bash -c \'export RANCHER_SERVER_VERSION=${rancher_version} && " +
"pytest -v -s --junit-xml=${setupResultsOut} " +
"${deployPytestOptions} ${testsDir}\'"

// copy file containing CATTLE_TEST_URL, ADMIN_TOKEN, USER_TOKEN and load into environment variables
sh "docker cp ${setupContainer}:${rootPath}${testsDir}${rancherConfig} ."
load rancherConfig

} catch(err) {
echo "Error: " + err
echo 'Test run had failures. Collecting results...'
currentBuild.result = 'FAILURE'
}
}

// AWS_USER and AWS_AMI are currently not set in the environmental variables.
def GO_CATTLE_TEST_URL = env.CATTLE_TEST_URL.replace('https://', '')
env.CONFIG = env.CONFIG.replace('${CATTLE_TEST_URL}', "${GO_CATTLE_TEST_URL}")
env.CONFIG = env.CONFIG.replace('${ADMIN_TOKEN}', env.ADMIN_TOKEN)
env.CONFIG = env.CONFIG.replace('${AZURE_CLIENT_ID}', env.AZURE_CLIENT_ID)
env.CONFIG = env.CONFIG.replace('${AZURE_CLIENT_SECRET}', env.AZURE_CLIENT_SECRET)
env.CONFIG = env.CONFIG.replace('${AZURE_SUBSCRIPTION_ID}', env.AZURE_SUBSCRIPTION_ID)
env.CONFIG = env.CONFIG.replace('${AWS_SECRET_ACCESS_KEY}', env.AWS_SECRET_ACCESS_KEY)
env.CONFIG = env.CONFIG.replace('${AWS_ACCESS_KEY_ID}', env.AWS_ACCESS_KEY_ID)
env.CONFIG = env.CONFIG.replace('${AWS_IAM_PROFILE}', env.AWS_IAM_PROFILE)
env.CONFIG = env.CONFIG.replace('${AWS_REGION}', env.AWS_REGION)
env.CONFIG = env.CONFIG.replace('${AWS_INSTANCE_TYPE}', env.AWS_INSTANCE_TYPE)
env.CONFIG = env.CONFIG.replace('${AWS_VPC}', env.AWS_VPC)
env.CONFIG = env.CONFIG.replace('${AWS_SECURITY_GROUPS}', env.AWS_SECURITY_GROUPS)
env.CONFIG = env.CONFIG.replace('${AWS_SSH_PEM_KEY}', env.AWS_SSH_KEY_NAME)
env.CONFIG = env.CONFIG.replace('${RANCHER_LINODE_TOKEN}', env.RANCHER_LINODE_TOKEN)

stage('Execute subjobs') {
try {
jobs = [:]
println "Branch: ${branch}"

goParams = [ string(name: 'TIMEOUT', value: "${TIMEOUT}"),
text(name: 'CONFIG', value: "${env.CONFIG}"),
string(name: 'REPO', value: "https://github.com/rancher/rancher.git"),
string(name: 'BRANCH', value: branch),
string(name: 'TEST_PACKAGE', value: "provisioning/..."),
string(name: 'GOTEST_TESTCASE', value: "${env.GOTEST_TESTCASE}") ]

// Rancher server 1 is used to test RKE1, RKE2 and K3s clusters.
jobs["go-provisioning"] = { build job: 'rancher_ontag_go_certification', parameters: goParams }

parallel jobs
} catch(err) {
echo "Error: " + err
}
}

stage('Delete Rancher Server') {
if (env.RANCHER_DELETE_SERVER.toLowerCase() == "true") {
try {
sh "docker run --name ${deleteContainer} -t --env-file ${envFile} " +
"${imageName} /bin/bash -c \'export CATTLE_TEST_URL=${env.CATTLE_TEST_URL} && " +
"export ADMIN_TOKEN=${env.ADMIN_TOKEN} && export USER_TOKEN=${env.USER_TOKEN} &&"+
"pytest -v -s --junit-xml=${deleteResultsOut} " +
"${deletePytestOptions} ${testsDir}\'"
} catch(err) {
echo "Error: " + err
}
}
}

stage('Test Report') {
// copy and archive test results
sh "docker cp ${setupContainer}:${rootPath}${setupResultsOut} ."
sh "docker cp ${deleteContainer}:${rootPath}${deleteResultsOut} ."
step([$class: 'JUnitResultArchiver', testResults: "**/${setupResultsOut}"])
step([$class: 'JUnitResultArchiver', testResults: "**/${deleteResultsOut}"])
sh "docker stop ${setupContainer}"
sh "docker rm -v ${setupContainer}"
sh "docker stop ${deleteContainer}"
sh "docker rm -v ${deleteContainer}"
sh "docker rmi ${imageName}"
}
} catch(err){
sh "docker stop ${setupContainer}"
sh "docker rm -v ${setupContainer}"
sh "docker stop ${deleteContainer}"
sh "docker rm -v ${deleteContainer}"
sh "docker rmi ${imageName}"
}
}
}
}
}
}
} catch(err) {
echo "Error: " + err
}
}

0 comments on commit 86f85b2

Please sign in to comment.