blob: e1ed62dbcfa67b1c82282638ac4ca87026c8160f [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.
Denis Egorenkob090a762018-09-12 19:25:41 +04008 * formulasRevision - (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)
29 def formulasRevision = config.get('formulasRevision', 'proposed')
30 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', [])
38 envOpts.add("DISTRIB_REVISION=${formulasRevision}")
39 def dockerBaseOpts = [
40 '-u root:root',
41 "--hostname=${dockerHostname}",
42 '--ulimit nofile=4096:8192',
43 "--name=${dockerContainerName}",
44 "--cpus=${dockerMaxCpus}"
45 ]
46
47 def dockerOptsFinal = (dockerBaseOpts + dockerExtraOpts).join(' ')
azvyagintsev9aadf522018-10-05 14:35:37 +030048 def defaultExtraReposYaml = '''
49---
50distrib_revision: 'nightly'
51aprConfD: |-
52 APT::Get::AllowUnauthenticated 'true';
53 APT::Get::Install-Suggests 'false';
54 APT::Get::Install-Recommends 'false';
55repo:
56 mcp_saltstack:
57 source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/saltstack-2017.7/xenial xenial main"
58 pinning: |-
59 Package: libsodium18
60 Pin: release o=SaltStack
61 Pin-Priority: 50
62
63 Package: *
64 Pin: release o=SaltStack
65 Pin-Priority: 1100
66 mcp_extra:
67 source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/extra/xenial xenial main"
68 ubuntu:
69 source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/ubuntu xenial main restricted universe"
70 ubuntu-upd:
71 source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/ubuntu xenial-updates main restricted universe"
72 ubuntu-sec:
73 source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/ubuntu xenial-security main restricted universe"
74'''
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040075 def img = docker.image(dockerImageName)
azvyagintsev9aadf522018-10-05 14:35:37 +030076 def extraReposYaml = config.get('extraReposYaml', defaultExtraReposYaml)
77
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040078 img.pull()
79
80 try {
81 img.inside(dockerOptsFinal) {
82 withEnv(envOpts) {
83 try {
84 // Currently, we don't have any other point to install
85 // runtime dependencies for tests.
86 if (baseRepoPreConfig) {
azvyagintsev9aadf522018-10-05 14:35:37 +030087 // Warning! POssible point of 'allow-downgrades' issue
88 // Probably, need to add such flag into apt.prefs
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040089 sh("""#!/bin/bash -xe
90 echo "Installing extra-deb dependencies inside docker:"
azvyagintsev9aadf522018-10-05 14:35:37 +030091 echo > /etc/apt/sources.list
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040092 rm -vf /etc/apt/sources.list.d/* || true
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040093 """)
azvyagintsev9aadf522018-10-05 14:35:37 +030094 common.debianExtraRepos(extraReposYaml)
95 sh('''#!/bin/bash -xe
96 apt-get update
97 apt-get install -y python-netaddr reclass
98 ''')
99
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400100 }
101 runCommands.sort().each { command, body ->
102 common.warningMsg("Running command: ${command}")
103 // doCall is the closure implementation in groovy, allow to pass arguments to closure
104 body.call()
105 }
106 // If we didn't dropped for now - test has been passed.
107 TestMarkerResult = true
108 }
109 finally {
110 runFinally.sort().each { command, body ->
111 common.warningMsg("Running ${command} command.")
112 // doCall is the closure implementation in groovy, allow to pass arguments to closure
113 body.call()
114 }
115 }
116 }
117 }
118 }
119 catch (Exception er) {
120 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
121 }
122
123 try {
124 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
125 timeout(time: 10, unit: 'SECONDS') {
126 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
127 }
128 timeout(time: 10, unit: 'SECONDS') {
129 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
130 }
131 }
132 catch (Exception er) {
133 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
134 }
135
136 if (TestMarkerResult) {
137 common.infoMsg("Test finished: SUCCESS")
138 } else {
139 common.warningMsg("Test finished: FAILURE")
140 }
141 return TestMarkerResult
142}
143
144/**
azvyagintsev9aadf522018-10-05 14:35:37 +0300145 * Wrapper around setupDockerAndTest, to run checks against new Reclass version
146 * that current model is compatible with new Reclass.
147 *
148 * @param config - LinkedHashMap with configuration params:
149 * dockerHostname - (required) Hostname to use for Docker container.
150 * distribRevision - (optional) Revision of packages to use (default proposed).
151 * extraRepo - (optional) Extra repo to use to install new Reclass version. Has
152 * high priority on distribRevision
153 * targetNodes - (required) List nodes to check pillar data.
Denis Egorenkob090a762018-09-12 19:25:41 +0400154 */
155def compareReclassVersions(config) {
156 def common = new com.mirantis.mk.Common()
157 def salt = new com.mirantis.mk.Salt()
158 common.infoMsg("Going to test new reclass for CFG node")
159 def distribRevision = config.get('distribRevision', 'proposed')
160 def venv = config.get('venv')
161 def extraRepo = config.get('extraRepo', '')
162 def extraRepoKey = config.get('extraRepoKey', '')
163 def targetNodes = config.get('targetNodes')
164 sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
165 sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
166 def configRun = [
167 'formulasRevision': distribRevision,
azvyagintsev9aadf522018-10-05 14:35:37 +0300168 'dockerExtraOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400169 "-v /srv/salt/reclass:/srv/salt/reclass:ro",
170 "-v /etc/salt:/etc/salt:ro",
171 "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro"
172 ],
azvyagintsev9aadf522018-10-05 14:35:37 +0300173 'envOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400174 "WORKSPACE=${env.WORKSPACE}",
175 "NODES_LIST=${targetNodes.join(' ')}"
176 ],
azvyagintsev9aadf522018-10-05 14:35:37 +0300177 'runCommands' : [
178 '001_Update_Reclass_package' : {
179 sh('apt-get update && apt-get install -y reclass')
Denis Egorenkob090a762018-09-12 19:25:41 +0400180 },
181 '002_Test_Reclass_Compatibility': {
azvyagintsev9aadf522018-10-05 14:35:37 +0300182 sh('''
Denis Egorenkob090a762018-09-12 19:25:41 +0400183 reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1
184 for node in $NODES_LIST; do
185 reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1
186 done
187 ''')
188 }
189 ]
190 ]
191 if (extraRepo) {
192 configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = {
193 sh("""
194 echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list
195 [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add -
196 """)
197 }
Denis Egorenkob090a762018-09-12 19:25:41 +0400198 }
199 if (setupDockerAndTest(configRun)) {
200 common.infoMsg("New reclass version is compatible with current model: SUCCESS")
201 def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0]
202 // [0..-31] to exclude 'echo Salt command execution success' from output
203 writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31])
azvyagintsev9aadf522018-10-05 14:35:37 +0300204 for (String node in targetNodes) {
Denis Egorenkob090a762018-09-12 19:25:41 +0400205 def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0]
206 writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31])
207 }
208 def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '')
209 currentBuild.description = reclassDiff
210 if (reclassDiff != '<b>No job changes</b>') {
211 throw new RuntimeException("Pillars with new reclass version has been changed: FAILED")
212 } else {
213 common.infoMsg("Pillars not changed with new reclass version: SUCCESS")
214 }
215 } else {
216 throw new RuntimeException("New reclass version is not compatible with current model: FAILED")
217 }
218}
219
220/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400221 * Wrapper over setupDockerAndTest, to test CC model.
222 *
223 * @param config - dict with params:
224 * dockerHostname - (required) salt master's name
225 * clusterName - (optional) model cluster name
226 * extraFormulas - (optional) extraFormulas to install. DEPRECATED
227 * formulasSource - (optional) formulas source (git or pkg, default pkg)
228 * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master)
229 * reclassEnv - (require) directory of model
230 * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false)
231 * aptRepoUrl - (optional) package repository with salt formulas
232 * aptRepoGPG - (optional) GPG key for apt repository with formulas
233 * testContext - (optional) Description of test
azvyagintsev9aadf522018-10-05 14:35:37 +0300234 Return: true\exception
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400235 */
236
237def testNode(LinkedHashMap config) {
238 def common = new com.mirantis.mk.Common()
239 def result = ''
240 def dockerHostname = config.get('dockerHostname')
241 def reclassEnv = config.get('reclassEnv')
242 def clusterName = config.get('clusterName', "")
243 def formulasSource = config.get('formulasSource', 'pkg')
244 def extraFormulas = config.get('extraFormulas', 'linux')
245 def reclassVersion = config.get('reclassVersion', 'master')
246 def ignoreClassNotfound = config.get('ignoreClassNotfound', false)
247 def aptRepoUrl = config.get('aptRepoUrl', "")
248 def aptRepoGPG = config.get('aptRepoGPG', "")
249 def testContext = config.get('testContext', 'test')
250 config['envOpts'] = [
251 "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5",
252 "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}",
253 "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}",
254 "EXTRA_FORMULAS=${extraFormulas}", "RECLASS_VERSION=${reclassVersion}",
255 "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1",
256 "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}",
257 "EXTRA_FORMULAS_PKG_ALL=true"
258 ]
259
260 config['runCommands'] = [
261 '001_Clone_salt_formulas_scripts': {
azvyagintsev9aadf522018-10-05 14:35:37 +0300262 sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400263 },
264
azvyagintsev9aadf522018-10-05 14:35:37 +0300265 '002_Prepare_something' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400266 sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
267 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
268 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
269 ''')
270 },
271
azvyagintsev9aadf522018-10-05 14:35:37 +0300272 '004_Run_tests' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400273 def testTimeout = 40 * 60
274 timeout(time: testTimeout, unit: 'SECONDS') {
azvyagintsev9aadf522018-10-05 14:35:37 +0300275 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400276 source /srv/salt/scripts/bootstrap.sh
277 cd /srv/salt/scripts
278 source_local_envs
279 configure_salt_master
280 configure_salt_minion
281 install_salt_formula_pkg
282 source /srv/salt/scripts/bootstrap.sh
283 cd /srv/salt/scripts
284 saltservice_restart''')
285
azvyagintsev9aadf522018-10-05 14:35:37 +0300286 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400287 source /srv/salt/scripts/bootstrap.sh
288 cd /srv/salt/scripts
289 source_local_envs
290 saltmaster_init''')
291
azvyagintsev9aadf522018-10-05 14:35:37 +0300292 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400293 source /srv/salt/scripts/bootstrap.sh
294 cd /srv/salt/scripts
295 verify_salt_minions''')
296 }
297 }
298 ]
299 config['runFinally'] = [
300 '001_Archive_artefacts': {
301 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
302 archiveArtifacts artifacts: "nodesinfo.tar.gz"
303 }
304 ]
305 testResult = setupDockerAndTest(config)
306 if (testResult) {
307 common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS")
308 } else {
309 throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE")
310 }
311 return testResult
312}
313
314/**
chnydaf14ea2a2017-05-26 15:07:47 +0200315 * setup and test salt-master
316 *
azvyagintsevb4e0c442018-09-12 17:00:04 +0300317 * @param masterName salt master's name
318 * @param clusterName model cluster name
319 * @param extraFormulas extraFormulas to install. DEPRECATED
320 * @param formulasSource formulas source (git or pkg)
321 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
322 * @param testDir directory of model
323 * @param formulasSource Salt formulas source type (optional, default pkg)
324 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +0200325 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +0300326 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
327 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
328 * @param aptRepoUrl package repository with salt formulas
329 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +0300330 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +0200331 */
332
azvyagintsevb4e0c442018-09-12 17:00:04 +0300333def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +0000334 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +0300335 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300336 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300337 // TODO
338 common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' +
339 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
340 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300341 // timeout for test execution (40min)
342 def testTimeout = 40 * 60
343 def TestMarkerResult = false
344 def saltOpts = "--retcode-passthrough --force-color"
345 def workspace = common.getWorkspace()
346 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
347 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +0200348
azvyagintsev635affb2018-09-13 13:02:54 +0300349 if (formulasSource == 'pkg') {
350 if (extraFormulas) {
351 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
352 "\n It would be ignored, and all formulas would be installed anyway")
353 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300354 }
355 if (!dockerContainerName) {
356 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
357 }
358 def dockerMaxCpusOpt = "--cpus=4"
359 if (dockerMaxCpus > 0) {
360 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
361 }
362 try {
363 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
azvyagintsev635affb2018-09-13 13:02:54 +0300364 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
azvyagintsevb4e0c442018-09-12 17:00:04 +0300365 "DISTRIB_REVISION=${formulasRevision}",
366 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
367 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
368 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
369 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
370 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
371 try {
372 // Currently, we don't have any other point to install
373 // runtime dependencies for tests.
374 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +0200375 echo "Installing extra-deb dependencies inside docker:"
376 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
377 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
378 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
379 rm -vf /etc/apt/sources.list.d/* || true
380 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
381 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
382 apt-get update
383 apt-get install -y python-netaddr
384 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300385 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
386 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +0200387 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
388 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
389 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300390 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
391 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +0200392 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
393 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
394 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +0300395 timeout(time: testTimeout, unit: 'SECONDS') {
396 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200397 source /srv/salt/scripts/bootstrap.sh
398 cd /srv/salt/scripts
399 source_local_envs
400 configure_salt_master
401 configure_salt_minion
402 install_salt_formula_pkg
403 source /srv/salt/scripts/bootstrap.sh
404 cd /srv/salt/scripts
405 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300406 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200407 source /srv/salt/scripts/bootstrap.sh
408 cd /srv/salt/scripts
409 source_local_envs
410 saltmaster_init''')
411
azvyagintsevb4e0c442018-09-12 17:00:04 +0300412 if (!legacyTestingMode.toBoolean()) {
413 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200414 source /srv/salt/scripts/bootstrap.sh
415 cd /srv/salt/scripts
416 verify_salt_minions
417 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300418 }
419 }
420 // If we didn't dropped for now - test has been passed.
421 TestMarkerResult = true
422 }
423
424 finally {
425 // Collect rendered per-node data.Those info could be simply used
426 // for diff processing. Data was generated via reclass.cli --nodeinfo,
427 /// during verify_salt_minions.
428 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
429 archiveArtifacts artifacts: "nodesinfo.tar.gz"
430 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200431 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200432 }
chnydaf14ea2a2017-05-26 15:07:47 +0200433 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300434 catch (Exception er) {
435 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
436 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300437
azvyagintsevb4e0c442018-09-12 17:00:04 +0300438 if (legacyTestingMode.toBoolean()) {
439 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
440 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
441 for (minion in nodes.tokenize()) {
442 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
443 if (!basename.trim().contains(masterName)) {
444 testMinion(basename.trim())
445 }
446 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300447 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300448
azvyagintsevb4e0c442018-09-12 17:00:04 +0300449 try {
450 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
451 timeout(time: 10, unit: 'SECONDS') {
452 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
453 }
454 timeout(time: 10, unit: 'SECONDS') {
455 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
456 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300457 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300458 catch (Exception er) {
459 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300460 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300461
azvyagintsevb4e0c442018-09-12 17:00:04 +0300462 if (TestMarkerResult) {
463 common.infoMsg("Test finished: SUCCESS")
464 } else {
465 common.warningMsg("Test finished: FAILURE")
466 }
467 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300468
chnydaf14ea2a2017-05-26 15:07:47 +0200469}
470
471/**
472 * Test salt-minion
473 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300474 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200475 */
476
azvyagintsev28fa9d92018-06-26 14:31:49 +0300477def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300478 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 +0200479}
azvyagintsev2b279d82018-08-07 17:22:54 +0200480
azvyagintsev2b279d82018-08-07 17:22:54 +0200481/**
482 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300483 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
484 Flow: grab all data, and pass to setupAndTestNode function
485 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200486 *
487 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300488 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
489 modelFile
490 DockerCName directly passed to setupAndTestNode
491 EXTRA_FORMULAS directly passed to setupAndTestNode
492 DISTRIB_REVISION directly passed to setupAndTestNode
493 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200494
azvyagintsevb4e0c442018-09-12 17:00:04 +0300495 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200496 */
497
498def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300499 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300500 common.errorMsg('You are using deprecated function!Please migrate to "testNode".' +
501 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
502 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300503 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
504 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
505 // unpack data
506 sh(script: "tar -xzf models.tar.gz ")
507 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
508 content = readFile(file: cfg.modelFile)
509 templateContext = readYaml text: content
510 clusterName = templateContext.default_context.cluster_name
511 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200512
azvyagintsevb4e0c442018-09-12 17:00:04 +0300513 def testResult = false
514 testResult = setupAndTestNode(
515 "cfg01.${clusterDomain}",
516 clusterName,
517 '',
518 cfg.testReclassEnv, // Sync into image exactly one env
519 'pkg',
520 cfg.DISTRIB_REVISION,
521 cfg.reclassVersion,
522 0,
523 false,
524 false,
525 '',
526 '',
527 cfg.DockerCName)
528 if (testResult) {
529 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
530 } else {
531 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
532 }
533 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200534}