· 6 years ago · Mar 06, 2020, 08: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"
51
52timeout(time: job_timeout.toInteger(), unit: 'SECONDS') {
53 node (slaveNode) {
54 def repo_url
55 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
56 credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
57 keyFileVariable: "GERRIT_KEY",
58 usernameVariable: "GERRIT_USERNAME",
59 passwordVariable: "GERRIT_PASSWORD",],]) {
60
61 def GERRIT_SCHEME = 'ssh'
62 def GERRIT_HOST = 'gerrit.mcp.mirantis.com'
63 def GERRIT_PORT = '29418'
64 def GERRIT_PROJECT = 'kaas/si-tests'
65 repo_url = "${GERRIT_SCHEME}://${GERRIT_USERNAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
66 release_repo_url = "${GERRIT_SCHEME}://${GERRIT_USERNAME}@${GERRIT_HOST}:${GERRIT_PORT}/kaas/releases"
67 }
68
69 stage('Checkout kaas/si-tests, create Python virtualenv and install requirements') {
70 deleteDir() // TODO(ddmitriev): do more selective cleanup to keep the git repo and virtual env
71 gerrit.gerritPatchsetCheckout(repo_url,
72 env.SI_TESTS_REFSPEC,
73 'FETCH_HEAD',
74 env.GERRIT_MCP_CREDENTIALS_ID)
75
76 python.runCmd("virtualenv --python=python3.5 ${virtualenv}", '', false)
77 python.runCmd("mkdir ${artifacts_dir}")
78 python.runCmd("pip install -q -r ./si_tests/requirements.txt", virtualenv)
79
80 def customRelease = false
81 dir('other_releases') {
82 if (params.KAAS_RELEASES_REFSPEC) {
83 gerrit.gerritPatchsetCheckout(release_repo_url,
84 env.KAAS_RELEASES_REFSPEC,
85 'FETCH_HEAD',
86 env.GERRIT_MCP_CREDENTIALS_ID)
87 python.runCmd("rm -rfv hack/vendor")
88 customRelease = true
89 } else if (env.KAAS_RELEASE_REPO_URL) {
90 def depth = KAAS_RELEASE_REPO_URL.replaceAll('^(http|https)://','').split('/').size()
91 sh("wget -r --no-parent -nH --cut-dirs=${depth} --reject \'index.html*\' ${KAAS_RELEASE_REPO_URL}")
92 customRelease = true
93 }
94 }
95 }
96
97 try {
98 stage('Run bootstrap kaas on vm test') {
99 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
100 credentialsId: env.SEED_PRIVATE_KEY_CREDENTIAL_ID,
101 keyFileVariable: "SEED_SSH_PRIV_KEY_FILE",
102 usernameVariable: "SEED_SSH_PRIV_KEY_USERNAME",
103 passwordVariable: "SEED_SSH_PRIV_KEY_PASSWORD",],]) {
104
105 withCredentials([
106 [$class : 'UsernamePasswordMultiBinding',
107 credentialsId : env.OPENSTACK_API_CREDENTIALS,
108 passwordVariable: 'OS_PASSWORD',
109 usernameVariable: 'OS_USERNAME',],
110 file(credentialsId: kaas_bootstrap_license_credentials, variable: 'KAAS_BOOTSTRAP_LICENSE_FILE'),
111 ]) {
112 // Run bootstrap test
113 def runEnvs = []
114 if (customRelease) {
115 runEnvs << "KAAS_RELEASES_FOLDER=other_releases/"
116 }
117 withEnv(runEnvs) {
118 python.runCmd("py.test --junit-xml=${report_filename} ${env.RUN_TESTS} 2>&1", virtualenv)
119 }
120 }
121 }
122 } // stage
123 } finally {
124 stage("Archive xml report and bootstrap artifacts") {
125 archiveArtifacts artifacts: "${artifacts_dir}/*"
126 } // stage
127 stage("Collect info for description") {
128 currentBuild.description = " ENV_NAME=${ENV_NAME}<br>"
129 SEED_NODE_IP = python.runCmd("[ -f ${artifacts_dir}/seed_node_ip ] && cat ${artifacts_dir}/seed_node_ip")['stdout'].trim().split().last()
130 if (SEED_NODE_IP) {
131 currentBuild.description += " ssh ubuntu@${SEED_NODE_IP} # Seed node, password: qalab<br>"
132 }
133 if (fileExists("${artifacts_dir}/management_kubeconfig")) {
134 currentBuild.description += " wget ${BUILD_URL}/artifact/${artifacts_dir}/management_kubeconfig -O kubeconfig; export KUBECONFIG=./kubeconfig # Get KaaS management cluster kubeconfig<br>"
135 }
136 if (fileExists("${artifacts_dir}/management_public_urls")) {
137 public_urls = readFile("${artifacts_dir}/management_public_urls").split('\n')
138 for (public_url in public_urls) {
139 currentBuild.description += " ${public_url}<br>"
140 }
141 }
142 } // stage
143
144 if (env.SEND_LOGS_TO_ELK.toBoolean()) {
145 // send logs to ELK
146 logstashSend()
147 }
148 if (env.REPORT_TO_TESTRAIL.toBoolean()) {
149 stage("Report SI tests results ${report_filename}") {
150
151 DeployedKaasVersion = sh(script: "cat ${artifacts_dir}/management_version", returnStdout: true).trim()
152
153 parameters = [
154 string(name: "ENV_NAME", value: env.ENV_NAME),
155 string(name: "KAAS_VERSION", value: DeployedKaasVersion),
156 text(name: "REPORTS_LIST", value: "REPORT_SI_TESTS: ${BUILD_URL}artifact/${report_filename}\n"),
157 ]
158 job_info = build job: testrail_report_job_name, parameters: parameters
159 build_description = job_info.getDescription()
160 if (build_description) {
161 currentBuild.description += build_description
162 }
163 }
164 } // if
165 } // try
166 } // node
167} // timeout