blob: 7ba3a4e0d95e7d457c4755db10eec88769125f7a [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
47 def dockerOptsFinal = (dockerBaseOpts + dockerExtraOpts).join(' ')
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000048 def defaultExtraReposYaml = '''
49---
azvyagintsevabcf42e2018-10-05 20:40:27 +030050distribRevision: 'nightly'
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000051aprConfD: |-
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)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000076 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) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000087 // 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:"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000091 echo > /etc/apt/sources.list
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040092 rm -vf /etc/apt/sources.list.d/* || true
Aleksey Zvyagintsevc5453342018-10-05 15:03:59 +000093 """)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000094 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/**
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000145 * 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 = [
azvyagintsevabcf42e2018-10-05 20:40:27 +0300167 'distribRevision': distribRevision,
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000168 '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 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000173 'envOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400174 "WORKSPACE=${env.WORKSPACE}",
175 "NODES_LIST=${targetNodes.join(' ')}"
176 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000177 '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': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000182 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) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000192 // FIXME
Denis Egorenkob090a762018-09-12 19:25:41 +0400193 configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = {
194 sh("""
195 echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list
196 [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add -
197 """)
198 }
Denis Egorenkob090a762018-09-12 19:25:41 +0400199 }
200 if (setupDockerAndTest(configRun)) {
201 common.infoMsg("New reclass version is compatible with current model: SUCCESS")
202 def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0]
203 // [0..-31] to exclude 'echo Salt command execution success' from output
204 writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31])
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000205 for (String node in targetNodes) {
Denis Egorenkob090a762018-09-12 19:25:41 +0400206 def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0]
207 writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31])
208 }
209 def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '')
210 currentBuild.description = reclassDiff
211 if (reclassDiff != '<b>No job changes</b>') {
212 throw new RuntimeException("Pillars with new reclass version has been changed: FAILED")
213 } else {
214 common.infoMsg("Pillars not changed with new reclass version: SUCCESS")
215 }
216 } else {
217 throw new RuntimeException("New reclass version is not compatible with current model: FAILED")
218 }
219}
220
221/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400222 * Wrapper over setupDockerAndTest, to test CC model.
223 *
224 * @param config - dict with params:
225 * dockerHostname - (required) salt master's name
226 * clusterName - (optional) model cluster name
227 * extraFormulas - (optional) extraFormulas to install. DEPRECATED
228 * formulasSource - (optional) formulas source (git or pkg, default pkg)
229 * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master)
230 * reclassEnv - (require) directory of model
231 * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false)
232 * aptRepoUrl - (optional) package repository with salt formulas
233 * aptRepoGPG - (optional) GPG key for apt repository with formulas
234 * testContext - (optional) Description of test
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000235 Return: true\exception
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400236 */
237
238def testNode(LinkedHashMap config) {
239 def common = new com.mirantis.mk.Common()
240 def result = ''
241 def dockerHostname = config.get('dockerHostname')
242 def reclassEnv = config.get('reclassEnv')
243 def clusterName = config.get('clusterName', "")
244 def formulasSource = config.get('formulasSource', 'pkg')
245 def extraFormulas = config.get('extraFormulas', 'linux')
246 def reclassVersion = config.get('reclassVersion', 'master')
247 def ignoreClassNotfound = config.get('ignoreClassNotfound', false)
248 def aptRepoUrl = config.get('aptRepoUrl', "")
249 def aptRepoGPG = config.get('aptRepoGPG', "")
250 def testContext = config.get('testContext', 'test')
251 config['envOpts'] = [
252 "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5",
253 "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}",
254 "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}",
255 "EXTRA_FORMULAS=${extraFormulas}", "RECLASS_VERSION=${reclassVersion}",
256 "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1",
257 "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}",
258 "EXTRA_FORMULAS_PKG_ALL=true"
259 ]
260
261 config['runCommands'] = [
262 '001_Clone_salt_formulas_scripts': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000263 sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400264 },
265
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000266 '002_Prepare_something' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400267 sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
268 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
269 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
270 ''')
271 },
272
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000273 '004_Run_tests' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400274 def testTimeout = 40 * 60
275 timeout(time: testTimeout, unit: 'SECONDS') {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000276 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400277 source /srv/salt/scripts/bootstrap.sh
278 cd /srv/salt/scripts
279 source_local_envs
280 configure_salt_master
281 configure_salt_minion
282 install_salt_formula_pkg
283 source /srv/salt/scripts/bootstrap.sh
284 cd /srv/salt/scripts
285 saltservice_restart''')
286
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000287 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400288 source /srv/salt/scripts/bootstrap.sh
289 cd /srv/salt/scripts
290 source_local_envs
291 saltmaster_init''')
292
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000293 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400294 source /srv/salt/scripts/bootstrap.sh
295 cd /srv/salt/scripts
296 verify_salt_minions''')
297 }
298 }
299 ]
300 config['runFinally'] = [
301 '001_Archive_artefacts': {
302 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
303 archiveArtifacts artifacts: "nodesinfo.tar.gz"
304 }
305 ]
306 testResult = setupDockerAndTest(config)
307 if (testResult) {
308 common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS")
309 } else {
310 throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE")
311 }
312 return testResult
313}
314
315/**
chnydaf14ea2a2017-05-26 15:07:47 +0200316 * setup and test salt-master
317 *
azvyagintsevb4e0c442018-09-12 17:00:04 +0300318 * @param masterName salt master's name
319 * @param clusterName model cluster name
320 * @param extraFormulas extraFormulas to install. DEPRECATED
321 * @param formulasSource formulas source (git or pkg)
322 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
323 * @param testDir directory of model
324 * @param formulasSource Salt formulas source type (optional, default pkg)
325 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +0200326 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +0300327 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
328 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
329 * @param aptRepoUrl package repository with salt formulas
330 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +0300331 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +0200332 */
333
azvyagintsevb4e0c442018-09-12 17:00:04 +0300334def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +0000335 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +0300336 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300337 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300338 // TODO
339 common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' +
340 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
341 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300342 // timeout for test execution (40min)
343 def testTimeout = 40 * 60
344 def TestMarkerResult = false
345 def saltOpts = "--retcode-passthrough --force-color"
346 def workspace = common.getWorkspace()
347 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
348 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +0200349
azvyagintsev635affb2018-09-13 13:02:54 +0300350 if (formulasSource == 'pkg') {
351 if (extraFormulas) {
352 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
353 "\n It would be ignored, and all formulas would be installed anyway")
354 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300355 }
356 if (!dockerContainerName) {
357 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
358 }
359 def dockerMaxCpusOpt = "--cpus=4"
360 if (dockerMaxCpus > 0) {
361 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
362 }
363 try {
364 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
azvyagintsev635affb2018-09-13 13:02:54 +0300365 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
azvyagintsevb4e0c442018-09-12 17:00:04 +0300366 "DISTRIB_REVISION=${formulasRevision}",
367 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
368 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
369 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
370 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
371 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
372 try {
373 // Currently, we don't have any other point to install
374 // runtime dependencies for tests.
375 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +0200376 echo "Installing extra-deb dependencies inside docker:"
377 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
378 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
379 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
380 rm -vf /etc/apt/sources.list.d/* || true
381 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
382 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
383 apt-get update
384 apt-get install -y python-netaddr
385 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300386 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
387 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +0200388 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
389 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
390 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300391 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
392 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +0200393 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
394 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
395 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +0300396 timeout(time: testTimeout, unit: 'SECONDS') {
397 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200398 source /srv/salt/scripts/bootstrap.sh
399 cd /srv/salt/scripts
400 source_local_envs
401 configure_salt_master
402 configure_salt_minion
403 install_salt_formula_pkg
404 source /srv/salt/scripts/bootstrap.sh
405 cd /srv/salt/scripts
406 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300407 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200408 source /srv/salt/scripts/bootstrap.sh
409 cd /srv/salt/scripts
410 source_local_envs
411 saltmaster_init''')
412
azvyagintsevb4e0c442018-09-12 17:00:04 +0300413 if (!legacyTestingMode.toBoolean()) {
414 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200415 source /srv/salt/scripts/bootstrap.sh
416 cd /srv/salt/scripts
417 verify_salt_minions
418 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300419 }
420 }
421 // If we didn't dropped for now - test has been passed.
422 TestMarkerResult = true
423 }
424
425 finally {
426 // Collect rendered per-node data.Those info could be simply used
427 // for diff processing. Data was generated via reclass.cli --nodeinfo,
428 /// during verify_salt_minions.
429 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
430 archiveArtifacts artifacts: "nodesinfo.tar.gz"
431 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200432 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200433 }
chnydaf14ea2a2017-05-26 15:07:47 +0200434 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300435 catch (Exception er) {
436 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
437 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300438
azvyagintsevb4e0c442018-09-12 17:00:04 +0300439 if (legacyTestingMode.toBoolean()) {
440 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
441 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
442 for (minion in nodes.tokenize()) {
443 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
444 if (!basename.trim().contains(masterName)) {
445 testMinion(basename.trim())
446 }
447 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300448 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300449
azvyagintsevb4e0c442018-09-12 17:00:04 +0300450 try {
451 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
452 timeout(time: 10, unit: 'SECONDS') {
453 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
454 }
455 timeout(time: 10, unit: 'SECONDS') {
456 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
457 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300458 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300459 catch (Exception er) {
460 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300461 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300462
azvyagintsevb4e0c442018-09-12 17:00:04 +0300463 if (TestMarkerResult) {
464 common.infoMsg("Test finished: SUCCESS")
465 } else {
466 common.warningMsg("Test finished: FAILURE")
467 }
468 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300469
chnydaf14ea2a2017-05-26 15:07:47 +0200470}
471
472/**
473 * Test salt-minion
474 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300475 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200476 */
477
azvyagintsev28fa9d92018-06-26 14:31:49 +0300478def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300479 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 +0200480}
azvyagintsev2b279d82018-08-07 17:22:54 +0200481
azvyagintsev2b279d82018-08-07 17:22:54 +0200482/**
483 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300484 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
485 Flow: grab all data, and pass to setupAndTestNode function
486 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200487 *
488 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300489 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
490 modelFile
491 DockerCName directly passed to setupAndTestNode
492 EXTRA_FORMULAS directly passed to setupAndTestNode
493 DISTRIB_REVISION directly passed to setupAndTestNode
494 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200495
azvyagintsevb4e0c442018-09-12 17:00:04 +0300496 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200497 */
498
499def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300500 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300501 common.errorMsg('You are using deprecated function!Please migrate to "testNode".' +
502 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
503 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300504 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
505 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
506 // unpack data
507 sh(script: "tar -xzf models.tar.gz ")
508 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
509 content = readFile(file: cfg.modelFile)
510 templateContext = readYaml text: content
511 clusterName = templateContext.default_context.cluster_name
512 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200513
azvyagintsevb4e0c442018-09-12 17:00:04 +0300514 def testResult = false
515 testResult = setupAndTestNode(
516 "cfg01.${clusterDomain}",
517 clusterName,
518 '',
519 cfg.testReclassEnv, // Sync into image exactly one env
520 'pkg',
521 cfg.DISTRIB_REVISION,
522 cfg.reclassVersion,
523 0,
524 false,
525 false,
526 '',
527 '',
528 cfg.DockerCName)
529 if (testResult) {
530 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
531 } else {
532 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
533 }
534 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200535}