· 6 years ago · Mar 09, 2020, 03:06 PM
1/**
2 *
3 * Workflow for KaaS bootstrap on VMs and run KaaS UI test
4 *
5 * Expected parameters:
6 * ENV_NAME Name to create Heat stack and management KaaS cluster
7 *
8 * # Variables used to render KaaS bootstrap templates
9 * KAAS_EXTERNAL_NETWORK_ID ID of 'public' network, example for EU cloud: bf6b85a1-39db-4582-b0d1-f4291dddb9cf
10 * KAAS_MANAGEMENT_CLUSTER_DNS1 DNS to resolve internal resources, example: 172.18.224.4
11 * KAAS_MANAGEMENT_CLUSTER_FLAVOR Flavor to create KaaS management cluster instances, example: kaas.small
12 * KAAS_MANAGEMENT_CLUSTER_IMAGE Image to create KaaS management cluster instances, example: bionic-server-cloudimg-amd64-20190612
13 * KAAS_MANAGEMENT_CLUSTER_NODES Kubernetes nodes for KaaS management cluster, example: 3
14 *
15 * # Variables used to render clouds.yaml for OpenStack
16 * OS_AUTH_URL OpenStack auth URL, example for EU cloud: https://ic-eu.ssl.mirantis.net:5000/v3
17 * OS_IDENTITY_API_VERSION OpenStack auth API version, example: 3
18 * OS_INTERFACE OpenStack endpoints interface, example: public
19 * OS_PROJECT_ID OpenStack project ID, used for bootstrap.sh, example for 'systest-team': 7fd2d1904af849ccb2d72fcfb8469c97
20 * OS_PROJECT_NAME OpenStack project name, used for si-tests, example: systest-team
21 * OS_REGION_NAME OpenStack region name, example: RegionOne
22 * OS_USER_DOMAIN_NAME OpenStack domain name, example: default
23 *
24 * # Remote access parameters
25 * OPENSTACK_API_CREDENTIALS Jenkins credential ID with OpenStack OS_USERNAME and OS_PASSWORD, example: system-integration-team-ci
26 * GERRIT_MCP_CREDENTIALS_ID Jenkins credential ID with username and SSH private key to access gerrit.mcp.mirantis.com
27 * CLOUD_KEY_NAME OpenStack keypair name to create the 'seed' node in the OpenStack, example: kaas-vm-seed-node
28 * SEED_PRIVATE_KEY_CREDENTIAL_ID Jenkins credential ID with username and private key to access the 'seed' node created with CLOUD_KEY_NAME
29 *
30 * # Pytest test name(s)
31 * RUN_TESTS Space separated list of tests and options
32 * Example: si_tests/tests/deployment/management-cluster/test_provision_mgm_cluster.py
33 *
34 * Optional parameters:
35 * BOOTSTRAP_TIMEOUT Bootstrap timeout, default: 7800
36 * SLAVE_NODE Jenkins node label to run the job, default: 'python'
37 * SI_TESTS_REFSPEC Refspec of patchset in gerrit.mcp.mirantis.com for the kaas/si-tests repository
38 * SEND_LOGS_TO_ELK Send logs to http://logcollector.mcp.mirantis.net:5601
39 */
40
41def gerrit = new com.mirantis.mk.Gerrit()
42def python = new com.mirantis.mk.Python()
43
44def slaveNode = env.SLAVE_NODE ?: 'python'
45def artifacts_dir = "artifacts"
46def testrail_report_job_name = "si-test-testrail-report"
47def report_filename = "${artifacts_dir}/bootstrap_kaas_result.xml"
48def virtualenv = ".venv-si-tests"
49def job_timeout = env.BOOTSTRAP_TIMEOUT ?: 7800
50def kaas_bootstrap_license_credentials = "kaas-bootstrap-si-license"
51def runEnvs = []
52
53timeout(time: job_timeout.toInteger(), unit: 'SECONDS') {
54 node (slaveNode) {
55 def repo_url
56 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
57 credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
58 keyFileVariable: "GERRIT_KEY",
59 usernameVariable: "GERRIT_USERNAME",
60 passwordVariable: "GERRIT_PASSWORD",],]) {
61
62 def GERRIT_SCHEME = 'ssh'
63 def GERRIT_HOST = 'gerrit.mcp.mirantis.com'
64 def GERRIT_PORT = '29418'
65 def GERRIT_PROJECT = 'kaas/si-tests'
66 repo_url = "${GERRIT_SCHEME}://${GERRIT_USERNAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
67 release_repo_url = "${GERRIT_SCHEME}://${GERRIT_USERNAME}@${GERRIT_HOST}:${GERRIT_PORT}/kaas/releases"
68 }
69
70 stage('Checkout kaas/si-tests, create Python virtualenv and install requirements') {
71 deleteDir() // TODO(ddmitriev): do more selective cleanup to keep the git repo and virtual env
72 gerrit.gerritPatchsetCheckout(repo_url,
73 env.SI_TESTS_REFSPEC,
74 'FETCH_HEAD',
75 env.GERRIT_MCP_CREDENTIALS_ID)
76
77 python.runCmd("virtualenv --python=python3.5 ${virtualenv}", '', false)
78 python.runCmd("mkdir ${artifacts_dir}")
79 python.runCmd("pip install -q -r ./si_tests/requirements.txt", virtualenv)
80 }
81
82 stage('Deterniming kaas release') {
83 runEnvs.add("KAAS_RELEASES_FOLDER=other_releases/")
84 dir('other_releases') {
85 if (env.KAAS_RELEASE_REPO_URL) {
86 def depth = KAAS_RELEASE_REPO_URL.replaceAll('^(http|https)://', '').split('/').size()
87 sh("wget -r --no-parent -nH --cut-dirs=${depth} --reject \'index.html*\' ${KAAS_RELEASE_REPO_URL}")
88
89 // KAAS_RELEASE_REPO_URL should contain dir called `templates_${VERSION}` in root dir, to define custom templates, otherwise SI tests will use own templates.
90 // set destination directory for ceed node and specify relative path to bootstrap templates similar to current `/other_releases`
91 runEnvs.addAll(["KAAS_BOOTSTRAP_TEMPLATES_DIR=kaas_releases/", "KAAS_RELEASES_REMOTE_FOLDER=kaas_releases/"])
92 } else {
93 gerrit.gerritPatchsetCheckout(release_repo_url,
94 KAAS_RELEASES_REFSPEC,
95 'FETCH_HEAD',
96 GERRIT_MCP_CREDENTIALS_ID)
97 python.runCmd("rm -rfv hack/vendor")
98 }
99 }
100 }
101
102 try {
103 stage('Run bootstrap kaas on vm test') {
104 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
105 credentialsId: env.SEED_PRIVATE_KEY_CREDENTIAL_ID,
106 keyFileVariable: "SEED_SSH_PRIV_KEY_FILE",
107 usernameVariable: "SEED_SSH_PRIV_KEY_USERNAME",
108 passwordVariable: "SEED_SSH_PRIV_KEY_PASSWORD",],]) {
109
110 withCredentials([
111 [$class : 'UsernamePasswordMultiBinding',
112 credentialsId : env.OPENSTACK_API_CREDENTIALS,
113 passwordVariable: 'OS_PASSWORD',
114 usernameVariable: 'OS_USERNAME',],
115 file(credentialsId: kaas_bootstrap_license_credentials, variable: 'KAAS_BOOTSTRAP_LICENSE_FILE'),
116 ]) {
117 // Run bootstrap test
118 withEnv(runEnvs) {
119 python.runCmd("py.test --junit-xml=${report_filename} ${env.RUN_TESTS} 2>&1", virtualenv)
120 }
121 }
122 }
123 } // stage
124 } finally {
125 stage("Archive xml report and bootstrap artifacts") {
126 archiveArtifacts artifacts: "${artifacts_dir}/*"
127 } // stage
128 stage("Collect info for description") {
129 currentBuild.description = " ENV_NAME=${ENV_NAME}<br>"
130 SEED_NODE_IP = python.runCmd("[ -f ${artifacts_dir}/seed_node_ip ] && cat ${artifacts_dir}/seed_node_ip")['stdout'].trim().split().last()
131 if (SEED_NODE_IP) {
132 currentBuild.description += " ssh ubuntu@${SEED_NODE_IP} # Seed node, password: qalab<br>"
133 }
134 if (fileExists("${artifacts_dir}/management_kubeconfig")) {
135 currentBuild.description += " wget ${BUILD_URL}/artifact/${artifacts_dir}/management_kubeconfig -O kubeconfig; export KUBECONFIG=./kubeconfig # Get KaaS management cluster kubeconfig<br>"
136 }
137 if (fileExists("${artifacts_dir}/management_public_urls")) {
138 public_urls = readFile("${artifacts_dir}/management_public_urls").split('\n')
139 for (public_url in public_urls) {
140 currentBuild.description += " ${public_url}<br>"
141 }
142 }
143 } // stage
144
145 if (env.SEND_LOGS_TO_ELK.toBoolean()) {
146 // send logs to ELK
147 logstashSend()
148 }
149 if (env.REPORT_TO_TESTRAIL.toBoolean()) {
150 stage("Report SI tests results ${report_filename}") {
151
152 DeployedKaasVersion = sh(script: "cat ${artifacts_dir}/management_version", returnStdout: true).trim()
153
154 parameters = [
155 string(name: "ENV_NAME", value: env.ENV_NAME),
156 string(name: "KAAS_VERSION", value: DeployedKaasVersion),
157 text(name: "REPORTS_LIST", value: "REPORT_SI_TESTS: ${BUILD_URL}artifact/${report_filename}\n"),
158 ]
159 job_info = build job: testrail_report_job_name, parameters: parameters
160 build_description = job_info.getDescription()
161 if (build_description) {
162 currentBuild.description += build_description
163 }
164 }
165 } // if
166 } // try
167 } // node
168} // timeout