blob: 3e40b0f60c6aa4b4805e6f4d4bc1b950eaac1987 [file] [log] [blame]
chnydaf14ea2a2017-05-26 15:07:47 +02001package com.mirantis.mk
2
3/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +04004 * Setup Docker to run some tests. Returns true/false based on
5 were tests successful or not.
6 * @param config - LinkedHashMap with configuration params:
7 * dockerHostname - (required) Hostname to use for Docker container.
azvyagintsevabcf42e2018-10-05 20:40:27 +03008 * distribRevision - (optional) Revision of packages to use (default proposed).
Denis Egorenko6fd79ac2018-09-12 13:28:21 +04009 * runCommands - (optional) Dict with closure structure of body required tests. For example:
10 * [ '001_Test': { sh("./run-some-test") }, '002_Test': { sh("./run-another-test") } ]
11 * Before execution runCommands will be sorted by key names. Alpabetical order is preferred.
12 * runFinally - (optional) Dict with closure structure of body required commands, which should be
13 * executed in any case of test results. Same format as for runCommands
14 * updateRepo - (optional) Whether to run common repo update step.
15 * dockerContainerName - (optional) Docker container name.
16 * dockerImageName - (optional) Docker image name
17 * dockerMaxCpus - (optional) Number of CPUS to use in Docker.
18 * dockerExtraOpts - (optional) Array of Docker extra opts for container
19 * envOpts - (optional) Array of variables that should be passed as ENV vars to Docker container.
20 * Return true | false
21 */
22
23def setupDockerAndTest(LinkedHashMap config) {
24 def common = new com.mirantis.mk.Common()
25 def TestMarkerResult = false
26 // setup options
27 def defaultContainerName = 'test-' + UUID.randomUUID().toString()
28 def dockerHostname = config.get('dockerHostname', defaultContainerName)
azvyagintsevabcf42e2018-10-05 20:40:27 +030029 def distribRevision = config.get('distribRevision', 'proposed')
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040030 def runCommands = config.get('runCommands', [:])
31 def runFinally = config.get('runFinally', [:])
32 def baseRepoPreConfig = config.get('baseRepoPreConfig', true)
33 def dockerContainerName = config.get('dockerContainerName', defaultContainerName)
34 def dockerImageName = config.get('image', "mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
35 def dockerMaxCpus = config.get('dockerMaxCpus', 4)
36 def dockerExtraOpts = config.get('dockerExtraOpts', [])
37 def envOpts = config.get('envOpts', [])
azvyagintsevabcf42e2018-10-05 20:40:27 +030038 envOpts.add("DISTRIB_REVISION=${distribRevision}")
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040039 def dockerBaseOpts = [
40 '-u root:root',
41 "--hostname=${dockerHostname}",
42 '--ulimit nofile=4096:8192',
43 "--name=${dockerContainerName}",
44 "--cpus=${dockerMaxCpus}"
45 ]
46
Denis Egorenko649cf7d2018-10-18 16:36:33 +040047 // extra repo on mirror.mirantis.net, which is not supported before 2018.11.0 release
48 def extraRepoSource = "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/extra/xenial xenial main"
49 try {
50 def releaseNaming = 'yyyy.MM.dd'
51 def repoDateUsed = new Date().parse(releaseNaming, distribRevision)
52 def extraAvailableFrom = new Date().parse(releaseNaming, '2018.11.0')
53 if (repoDateUsed < extraAvailableFrom) {
54 extraRepoSource = "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} extra"
55 }
56 } catch (Exception e) {
57 common.warningMsg(e)
58 if ( !(distribRevision in [ 'nightly', 'proposed', 'testing' ] )) {
59 extraRepoSource = "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} extra"
60 }
61 }
62
63 def dockerOptsFinal = (dockerBaseOpts + dockerExtraOpts).join(' ')
64 def defaultExtraReposYaml = """
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000065---
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000066aprConfD: |-
67 APT::Get::AllowUnauthenticated 'true';
68 APT::Get::Install-Suggests 'false';
69 APT::Get::Install-Recommends 'false';
70repo:
71 mcp_saltstack:
Denis Egorenko395aa212018-10-11 15:11:28 +040072 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/saltstack-2017.7/xenial xenial main"
Denis Egorenkoe02a1b22018-10-19 17:47:53 +040073 pin:
74 - package: "libsodium18"
75 pin: "release o=SaltStack"
76 priority: 50
77 - package: "*"
78 pin: "release o=SaltStack"
79 priority: "1100"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000080 mcp_extra:
Denis Egorenko3c752a52018-10-12 12:21:29 +040081 source: "${extraRepoSource}"
Denis Egorenko395aa212018-10-11 15:11:28 +040082 mcp_saltformulas:
83 source: "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} salt salt-latest"
84 repo_key: "http://apt.mcp.mirantis.net:8085/public.gpg"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000085 ubuntu:
Denis Egorenko395aa212018-10-11 15:11:28 +040086 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial main restricted universe"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000087 ubuntu-upd:
Denis Egorenko395aa212018-10-11 15:11:28 +040088 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-updates main restricted universe"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000089 ubuntu-sec:
Denis Egorenko395aa212018-10-11 15:11:28 +040090 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-security main restricted universe"
91"""
Denis Egorenkod54f60f2018-10-10 19:38:03 +040092
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040093 def img = docker.image(dockerImageName)
Denis Egorenko649cf7d2018-10-18 16:36:33 +040094 def extraReposYaml = config.get('extraReposYaml', defaultExtraReposYaml)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000095
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040096 img.pull()
97
98 try {
99 img.inside(dockerOptsFinal) {
100 withEnv(envOpts) {
101 try {
102 // Currently, we don't have any other point to install
103 // runtime dependencies for tests.
104 if (baseRepoPreConfig) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000105 // Warning! POssible point of 'allow-downgrades' issue
106 // Probably, need to add such flag into apt.prefs
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400107 sh("""#!/bin/bash -xe
108 echo "Installing extra-deb dependencies inside docker:"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000109 echo > /etc/apt/sources.list
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400110 rm -vf /etc/apt/sources.list.d/* || true
Denis Egorenkoe02a1b22018-10-19 17:47:53 +0400111 rm -vf /etc/apt/preferences.d/* || true
Aleksey Zvyagintsevc5453342018-10-05 15:03:59 +0000112 """)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000113 common.debianExtraRepos(extraReposYaml)
114 sh('''#!/bin/bash -xe
115 apt-get update
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400116 apt-get install -y python-netaddr
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000117 ''')
118
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400119 }
120 runCommands.sort().each { command, body ->
121 common.warningMsg("Running command: ${command}")
122 // doCall is the closure implementation in groovy, allow to pass arguments to closure
123 body.call()
124 }
125 // If we didn't dropped for now - test has been passed.
126 TestMarkerResult = true
127 }
128 finally {
129 runFinally.sort().each { command, body ->
130 common.warningMsg("Running ${command} command.")
131 // doCall is the closure implementation in groovy, allow to pass arguments to closure
132 body.call()
133 }
134 }
135 }
136 }
137 }
138 catch (Exception er) {
139 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
140 }
141
142 try {
143 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
144 timeout(time: 10, unit: 'SECONDS') {
145 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
146 }
147 timeout(time: 10, unit: 'SECONDS') {
148 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
149 }
150 }
151 catch (Exception er) {
152 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
153 }
154
155 if (TestMarkerResult) {
156 common.infoMsg("Test finished: SUCCESS")
157 } else {
158 common.warningMsg("Test finished: FAILURE")
159 }
160 return TestMarkerResult
161}
162
163/**
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000164 * Wrapper around setupDockerAndTest, to run checks against new Reclass version
165 * that current model is compatible with new Reclass.
166 *
167 * @param config - LinkedHashMap with configuration params:
168 * dockerHostname - (required) Hostname to use for Docker container.
169 * distribRevision - (optional) Revision of packages to use (default proposed).
170 * extraRepo - (optional) Extra repo to use to install new Reclass version. Has
171 * high priority on distribRevision
172 * targetNodes - (required) List nodes to check pillar data.
Denis Egorenkob090a762018-09-12 19:25:41 +0400173 */
174def compareReclassVersions(config) {
175 def common = new com.mirantis.mk.Common()
176 def salt = new com.mirantis.mk.Salt()
177 common.infoMsg("Going to test new reclass for CFG node")
178 def distribRevision = config.get('distribRevision', 'proposed')
179 def venv = config.get('venv')
180 def extraRepo = config.get('extraRepo', '')
181 def extraRepoKey = config.get('extraRepoKey', '')
182 def targetNodes = config.get('targetNodes')
183 sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
184 sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
185 def configRun = [
azvyagintsevabcf42e2018-10-05 20:40:27 +0300186 'distribRevision': distribRevision,
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000187 'dockerExtraOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400188 "-v /srv/salt/reclass:/srv/salt/reclass:ro",
189 "-v /etc/salt:/etc/salt:ro",
190 "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro"
191 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000192 'envOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400193 "WORKSPACE=${env.WORKSPACE}",
194 "NODES_LIST=${targetNodes.join(' ')}"
195 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000196 'runCommands' : [
197 '001_Update_Reclass_package' : {
198 sh('apt-get update && apt-get install -y reclass')
Denis Egorenkob090a762018-09-12 19:25:41 +0400199 },
200 '002_Test_Reclass_Compatibility': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000201 sh('''
Denis Egorenkob090a762018-09-12 19:25:41 +0400202 reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1
203 for node in $NODES_LIST; do
204 reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1
205 done
206 ''')
207 }
208 ]
209 ]
210 if (extraRepo) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000211 // FIXME
Denis Egorenkob090a762018-09-12 19:25:41 +0400212 configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = {
213 sh("""
214 echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list
215 [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add -
216 """)
217 }
Denis Egorenkob090a762018-09-12 19:25:41 +0400218 }
219 if (setupDockerAndTest(configRun)) {
220 common.infoMsg("New reclass version is compatible with current model: SUCCESS")
221 def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0]
222 // [0..-31] to exclude 'echo Salt command execution success' from output
223 writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31])
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000224 for (String node in targetNodes) {
Denis Egorenkob090a762018-09-12 19:25:41 +0400225 def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0]
226 writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31])
227 }
228 def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '')
229 currentBuild.description = reclassDiff
230 if (reclassDiff != '<b>No job changes</b>') {
231 throw new RuntimeException("Pillars with new reclass version has been changed: FAILED")
232 } else {
233 common.infoMsg("Pillars not changed with new reclass version: SUCCESS")
234 }
235 } else {
236 throw new RuntimeException("New reclass version is not compatible with current model: FAILED")
237 }
238}
239
240/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400241 * Wrapper over setupDockerAndTest, to test CC model.
242 *
243 * @param config - dict with params:
244 * dockerHostname - (required) salt master's name
245 * clusterName - (optional) model cluster name
246 * extraFormulas - (optional) extraFormulas to install. DEPRECATED
247 * formulasSource - (optional) formulas source (git or pkg, default pkg)
248 * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master)
249 * reclassEnv - (require) directory of model
250 * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false)
251 * aptRepoUrl - (optional) package repository with salt formulas
252 * aptRepoGPG - (optional) GPG key for apt repository with formulas
253 * testContext - (optional) Description of test
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000254 Return: true\exception
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400255 */
256
257def testNode(LinkedHashMap config) {
258 def common = new com.mirantis.mk.Common()
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400259 def dockerHostname = config.get('dockerHostname')
260 def reclassEnv = config.get('reclassEnv')
261 def clusterName = config.get('clusterName', "")
262 def formulasSource = config.get('formulasSource', 'pkg')
263 def extraFormulas = config.get('extraFormulas', 'linux')
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400264 def ignoreClassNotfound = config.get('ignoreClassNotfound', false)
265 def aptRepoUrl = config.get('aptRepoUrl', "")
266 def aptRepoGPG = config.get('aptRepoGPG', "")
267 def testContext = config.get('testContext', 'test')
268 config['envOpts'] = [
269 "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5",
270 "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}",
271 "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}",
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400272 "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400273 "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1",
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400274 "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}"
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400275 ]
276
277 config['runCommands'] = [
278 '001_Clone_salt_formulas_scripts': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000279 sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400280 },
281
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000282 '002_Prepare_something' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400283 sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
284 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
285 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
286 ''')
287 },
288
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400289 '003_Install_Reclass_package' : {
290 sh('apt-get install -y reclass')
291 },
292
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000293 '004_Run_tests' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400294 def testTimeout = 40 * 60
295 timeout(time: testTimeout, unit: 'SECONDS') {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000296 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400297 source /srv/salt/scripts/bootstrap.sh
298 cd /srv/salt/scripts
299 source_local_envs
300 configure_salt_master
301 configure_salt_minion
302 install_salt_formula_pkg
303 source /srv/salt/scripts/bootstrap.sh
304 cd /srv/salt/scripts
305 saltservice_restart''')
306
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000307 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400308 source /srv/salt/scripts/bootstrap.sh
309 cd /srv/salt/scripts
310 source_local_envs
311 saltmaster_init''')
312
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000313 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400314 source /srv/salt/scripts/bootstrap.sh
315 cd /srv/salt/scripts
316 verify_salt_minions''')
317 }
318 }
319 ]
320 config['runFinally'] = [
321 '001_Archive_artefacts': {
322 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
323 archiveArtifacts artifacts: "nodesinfo.tar.gz"
324 }
325 ]
326 testResult = setupDockerAndTest(config)
327 if (testResult) {
328 common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS")
329 } else {
330 throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE")
331 }
332 return testResult
333}
334
335/**
chnydaf14ea2a2017-05-26 15:07:47 +0200336 * setup and test salt-master
337 *
azvyagintsevb4e0c442018-09-12 17:00:04 +0300338 * @param masterName salt master's name
339 * @param clusterName model cluster name
340 * @param extraFormulas extraFormulas to install. DEPRECATED
341 * @param formulasSource formulas source (git or pkg)
342 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
343 * @param testDir directory of model
344 * @param formulasSource Salt formulas source type (optional, default pkg)
345 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +0200346 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +0300347 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
348 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
349 * @param aptRepoUrl package repository with salt formulas
350 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +0300351 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +0200352 */
353
azvyagintsevb4e0c442018-09-12 17:00:04 +0300354def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +0000355 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +0300356 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300357 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300358 // TODO
359 common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' +
360 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
361 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300362 // timeout for test execution (40min)
363 def testTimeout = 40 * 60
364 def TestMarkerResult = false
365 def saltOpts = "--retcode-passthrough --force-color"
366 def workspace = common.getWorkspace()
367 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
368 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +0200369
azvyagintsev635affb2018-09-13 13:02:54 +0300370 if (formulasSource == 'pkg') {
371 if (extraFormulas) {
372 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
373 "\n It would be ignored, and all formulas would be installed anyway")
374 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300375 }
376 if (!dockerContainerName) {
377 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
378 }
379 def dockerMaxCpusOpt = "--cpus=4"
380 if (dockerMaxCpus > 0) {
381 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
382 }
383 try {
384 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
azvyagintsev635affb2018-09-13 13:02:54 +0300385 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
azvyagintsevb4e0c442018-09-12 17:00:04 +0300386 "DISTRIB_REVISION=${formulasRevision}",
387 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
388 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
389 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
390 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
391 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
392 try {
393 // Currently, we don't have any other point to install
394 // runtime dependencies for tests.
395 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +0200396 echo "Installing extra-deb dependencies inside docker:"
397 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
398 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
399 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
400 rm -vf /etc/apt/sources.list.d/* || true
401 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
402 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
403 apt-get update
404 apt-get install -y python-netaddr
405 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300406 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
407 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +0200408 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
409 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
410 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300411 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
412 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +0200413 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
414 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
415 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +0300416 timeout(time: testTimeout, unit: 'SECONDS') {
417 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200418 source /srv/salt/scripts/bootstrap.sh
419 cd /srv/salt/scripts
420 source_local_envs
421 configure_salt_master
422 configure_salt_minion
423 install_salt_formula_pkg
424 source /srv/salt/scripts/bootstrap.sh
425 cd /srv/salt/scripts
426 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300427 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200428 source /srv/salt/scripts/bootstrap.sh
429 cd /srv/salt/scripts
430 source_local_envs
431 saltmaster_init''')
432
azvyagintsevb4e0c442018-09-12 17:00:04 +0300433 if (!legacyTestingMode.toBoolean()) {
434 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200435 source /srv/salt/scripts/bootstrap.sh
436 cd /srv/salt/scripts
437 verify_salt_minions
438 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300439 }
440 }
441 // If we didn't dropped for now - test has been passed.
442 TestMarkerResult = true
443 }
444
445 finally {
446 // Collect rendered per-node data.Those info could be simply used
447 // for diff processing. Data was generated via reclass.cli --nodeinfo,
448 /// during verify_salt_minions.
449 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
450 archiveArtifacts artifacts: "nodesinfo.tar.gz"
451 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200452 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200453 }
chnydaf14ea2a2017-05-26 15:07:47 +0200454 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300455 catch (Exception er) {
456 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
457 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300458
azvyagintsevb4e0c442018-09-12 17:00:04 +0300459 if (legacyTestingMode.toBoolean()) {
460 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
461 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
462 for (minion in nodes.tokenize()) {
463 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
464 if (!basename.trim().contains(masterName)) {
465 testMinion(basename.trim())
466 }
467 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300468 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300469
azvyagintsevb4e0c442018-09-12 17:00:04 +0300470 try {
471 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
472 timeout(time: 10, unit: 'SECONDS') {
473 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
474 }
475 timeout(time: 10, unit: 'SECONDS') {
476 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
477 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300478 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300479 catch (Exception er) {
480 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300481 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300482
azvyagintsevb4e0c442018-09-12 17:00:04 +0300483 if (TestMarkerResult) {
484 common.infoMsg("Test finished: SUCCESS")
485 } else {
486 common.warningMsg("Test finished: FAILURE")
487 }
488 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300489
chnydaf14ea2a2017-05-26 15:07:47 +0200490}
491
492/**
493 * Test salt-minion
494 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300495 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200496 */
497
azvyagintsev28fa9d92018-06-26 14:31:49 +0300498def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300499 sh(script: "bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'", returnStdout: true)
Jakub Joseffa6ad8d2017-06-26 18:29:55 +0200500}
azvyagintsev2b279d82018-08-07 17:22:54 +0200501
azvyagintsev2b279d82018-08-07 17:22:54 +0200502/**
503 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300504 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
505 Flow: grab all data, and pass to setupAndTestNode function
506 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200507 *
508 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300509 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
510 modelFile
511 DockerCName directly passed to setupAndTestNode
512 EXTRA_FORMULAS directly passed to setupAndTestNode
513 DISTRIB_REVISION directly passed to setupAndTestNode
514 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200515
azvyagintsevb4e0c442018-09-12 17:00:04 +0300516 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200517 */
518
519def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300520 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300521 common.errorMsg('You are using deprecated function!Please migrate to "testNode".' +
522 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
523 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300524 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
525 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
526 // unpack data
527 sh(script: "tar -xzf models.tar.gz ")
528 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
529 content = readFile(file: cfg.modelFile)
530 templateContext = readYaml text: content
531 clusterName = templateContext.default_context.cluster_name
532 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200533
azvyagintsevb4e0c442018-09-12 17:00:04 +0300534 def testResult = false
535 testResult = setupAndTestNode(
536 "cfg01.${clusterDomain}",
537 clusterName,
538 '',
539 cfg.testReclassEnv, // Sync into image exactly one env
540 'pkg',
541 cfg.DISTRIB_REVISION,
542 cfg.reclassVersion,
543 0,
544 false,
545 false,
546 '',
547 '',
548 cfg.DockerCName)
549 if (testResult) {
550 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
551 } else {
552 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
553 }
554 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200555}