· 6 years ago · Mar 09, 2020, 07:34 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 = env.KAAS_MANAGEMENT_CLUSTER_FLAVOR ? ["KAAS_MANAGEMENT_CLUSTER_FLAVOR=${env.KAAS_MANAGEMENT_CLUSTER_FLAVOR}"] : []
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 runEnvs.add("KAAS_BOOTSTRAP_TEMPLATES_DIR=other_releases/")
91 } else {
92 gerrit.gerritPatchsetCheckout(release_repo_url,
93 KAAS_RELEASES_REFSPEC,
94 'FETCH_HEAD',
95 GERRIT_MCP_CREDENTIALS_ID)
96 python.runCmd("rm -rfv hack/vendor")
97 }
98 }
99 }
100
101 try {
102 stage('Run bootstrap kaas on vm test') {
103 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
104 credentialsId: env.SEED_PRIVATE_KEY_CREDENTIAL_ID,
105 keyFileVariable: "SEED_SSH_PRIV_KEY_FILE",
106 usernameVariable: "SEED_SSH_PRIV_KEY_USERNAME",
107 passwordVariable: "SEED_SSH_PRIV_KEY_PASSWORD",],]) {
108
109 withCredentials([
110 [$class : 'UsernamePasswordMultiBinding',
111 credentialsId : env.OPENSTACK_API_CREDENTIALS,
112 passwordVariable: 'OS_PASSWORD',
113 usernameVariable: 'OS_USERNAME',],
114 file(credentialsId: kaas_bootstrap_license_credentials, variable: 'KAAS_BOOTSTRAP_LICENSE_FILE'),
115 ]) {
116 // Run bootstrap test
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