blob: 9983c3431bee69174894ab5ce27c637b7518fdfc [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 ]
Denis Egorenko3c752a52018-10-12 12:21:29 +040046 // extra repo on mirror.mirantis.net, which is not supported before 2018.11.0 release
47 def extraRepoSource = "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/extra/xenial xenial main"
48 try {
49 def releaseNaming = 'yyyy.MM.dd'
50 def repoDateUsed = new Date().parse(releaseNaming, distribRevision)
51 def extraAvailableFrom = new Date().parse(releaseNaming, '2018.11.0')
52 if (repoDateUsed < extraAvailableFrom) {
53 extraRepoSource = "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} extra"
54 }
55 } catch (Exception e) {
56 common.warningMsg(e)
57 if ( !(distribRevision in [ 'nightly', 'proposed', 'testing' ] )) {
58 extraRepoSource = "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} extra"
59 }
60 }
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040061
62 def dockerOptsFinal = (dockerBaseOpts + dockerExtraOpts).join(' ')
Denis Egorenko395aa212018-10-11 15:11:28 +040063 def defaultExtraReposYaml = """
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000064---
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000065aprConfD: |-
66 APT::Get::AllowUnauthenticated 'true';
67 APT::Get::Install-Suggests 'false';
68 APT::Get::Install-Recommends 'false';
69repo:
70 mcp_saltstack:
Denis Egorenko395aa212018-10-11 15:11:28 +040071 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/saltstack-2017.7/xenial xenial main"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000072 pinning: |-
73 Package: libsodium18
74 Pin: release o=SaltStack
75 Pin-Priority: 50
76
77 Package: *
78 Pin: release o=SaltStack
79 Pin-Priority: 1100
80 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 Egorenko6fd79ac2018-09-12 13:28:21 +040092 def img = docker.image(dockerImageName)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000093 def extraReposYaml = config.get('extraReposYaml', defaultExtraReposYaml)
94
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040095 img.pull()
96
97 try {
98 img.inside(dockerOptsFinal) {
99 withEnv(envOpts) {
100 try {
101 // Currently, we don't have any other point to install
102 // runtime dependencies for tests.
103 if (baseRepoPreConfig) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000104 // Warning! POssible point of 'allow-downgrades' issue
105 // Probably, need to add such flag into apt.prefs
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400106 sh("""#!/bin/bash -xe
107 echo "Installing extra-deb dependencies inside docker:"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000108 echo > /etc/apt/sources.list
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400109 rm -vf /etc/apt/sources.list.d/* || true
Aleksey Zvyagintsevc5453342018-10-05 15:03:59 +0000110 """)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000111 common.debianExtraRepos(extraReposYaml)
112 sh('''#!/bin/bash -xe
113 apt-get update
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400114 apt-get install -y python-netaddr
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000115 ''')
116
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400117 }
118 runCommands.sort().each { command, body ->
119 common.warningMsg("Running command: ${command}")
120 // doCall is the closure implementation in groovy, allow to pass arguments to closure
121 body.call()
122 }
123 // If we didn't dropped for now - test has been passed.
124 TestMarkerResult = true
125 }
126 finally {
127 runFinally.sort().each { command, body ->
128 common.warningMsg("Running ${command} command.")
129 // doCall is the closure implementation in groovy, allow to pass arguments to closure
130 body.call()
131 }
132 }
133 }
134 }
135 }
136 catch (Exception er) {
137 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
138 }
139
140 try {
141 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
142 timeout(time: 10, unit: 'SECONDS') {
143 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
144 }
145 timeout(time: 10, unit: 'SECONDS') {
146 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
147 }
148 }
149 catch (Exception er) {
150 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
151 }
152
153 if (TestMarkerResult) {
154 common.infoMsg("Test finished: SUCCESS")
155 } else {
156 common.warningMsg("Test finished: FAILURE")
157 }
158 return TestMarkerResult
159}
160
161/**
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000162 * Wrapper around setupDockerAndTest, to run checks against new Reclass version
163 * that current model is compatible with new Reclass.
164 *
165 * @param config - LinkedHashMap with configuration params:
166 * dockerHostname - (required) Hostname to use for Docker container.
167 * distribRevision - (optional) Revision of packages to use (default proposed).
168 * extraRepo - (optional) Extra repo to use to install new Reclass version. Has
169 * high priority on distribRevision
170 * targetNodes - (required) List nodes to check pillar data.
Denis Egorenkob090a762018-09-12 19:25:41 +0400171 */
172def compareReclassVersions(config) {
173 def common = new com.mirantis.mk.Common()
174 def salt = new com.mirantis.mk.Salt()
175 common.infoMsg("Going to test new reclass for CFG node")
176 def distribRevision = config.get('distribRevision', 'proposed')
177 def venv = config.get('venv')
178 def extraRepo = config.get('extraRepo', '')
179 def extraRepoKey = config.get('extraRepoKey', '')
180 def targetNodes = config.get('targetNodes')
181 sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
182 sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
183 def configRun = [
azvyagintsevabcf42e2018-10-05 20:40:27 +0300184 'distribRevision': distribRevision,
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000185 'dockerExtraOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400186 "-v /srv/salt/reclass:/srv/salt/reclass:ro",
187 "-v /etc/salt:/etc/salt:ro",
188 "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro"
189 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000190 'envOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400191 "WORKSPACE=${env.WORKSPACE}",
192 "NODES_LIST=${targetNodes.join(' ')}"
193 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000194 'runCommands' : [
195 '001_Update_Reclass_package' : {
196 sh('apt-get update && apt-get install -y reclass')
Denis Egorenkob090a762018-09-12 19:25:41 +0400197 },
198 '002_Test_Reclass_Compatibility': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000199 sh('''
Denis Egorenkob090a762018-09-12 19:25:41 +0400200 reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1
201 for node in $NODES_LIST; do
202 reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1
203 done
204 ''')
205 }
206 ]
207 ]
208 if (extraRepo) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000209 // FIXME
Denis Egorenkob090a762018-09-12 19:25:41 +0400210 configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = {
211 sh("""
212 echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list
213 [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add -
214 """)
215 }
Denis Egorenkob090a762018-09-12 19:25:41 +0400216 }
217 if (setupDockerAndTest(configRun)) {
218 common.infoMsg("New reclass version is compatible with current model: SUCCESS")
219 def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0]
220 // [0..-31] to exclude 'echo Salt command execution success' from output
221 writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31])
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000222 for (String node in targetNodes) {
Denis Egorenkob090a762018-09-12 19:25:41 +0400223 def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0]
224 writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31])
225 }
226 def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '')
227 currentBuild.description = reclassDiff
228 if (reclassDiff != '<b>No job changes</b>') {
229 throw new RuntimeException("Pillars with new reclass version has been changed: FAILED")
230 } else {
231 common.infoMsg("Pillars not changed with new reclass version: SUCCESS")
232 }
233 } else {
234 throw new RuntimeException("New reclass version is not compatible with current model: FAILED")
235 }
236}
237
238/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400239 * Wrapper over setupDockerAndTest, to test CC model.
240 *
241 * @param config - dict with params:
242 * dockerHostname - (required) salt master's name
243 * clusterName - (optional) model cluster name
244 * extraFormulas - (optional) extraFormulas to install. DEPRECATED
245 * formulasSource - (optional) formulas source (git or pkg, default pkg)
246 * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master)
247 * reclassEnv - (require) directory of model
248 * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false)
249 * aptRepoUrl - (optional) package repository with salt formulas
250 * aptRepoGPG - (optional) GPG key for apt repository with formulas
251 * testContext - (optional) Description of test
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000252 Return: true\exception
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400253 */
254
255def testNode(LinkedHashMap config) {
256 def common = new com.mirantis.mk.Common()
257 def result = ''
258 def dockerHostname = config.get('dockerHostname')
259 def reclassEnv = config.get('reclassEnv')
260 def clusterName = config.get('clusterName', "")
261 def formulasSource = config.get('formulasSource', 'pkg')
262 def extraFormulas = config.get('extraFormulas', 'linux')
263 def reclassVersion = config.get('reclassVersion', 'master')
264 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}",
272 "EXTRA_FORMULAS=${extraFormulas}", "RECLASS_VERSION=${reclassVersion}",
273 "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1",
274 "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}",
275 "EXTRA_FORMULAS_PKG_ALL=true"
276 ]
277
278 config['runCommands'] = [
279 '001_Clone_salt_formulas_scripts': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000280 sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400281 },
282
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000283 '002_Prepare_something' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400284 sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
285 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
286 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
287 ''')
288 },
289
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400290 '003_Install_Reclass_package' : {
291 sh('apt-get install -y reclass')
292 },
293
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000294 '004_Run_tests' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400295 def testTimeout = 40 * 60
296 timeout(time: testTimeout, unit: 'SECONDS') {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000297 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400298 source /srv/salt/scripts/bootstrap.sh
299 cd /srv/salt/scripts
300 source_local_envs
301 configure_salt_master
302 configure_salt_minion
303 install_salt_formula_pkg
304 source /srv/salt/scripts/bootstrap.sh
305 cd /srv/salt/scripts
306 saltservice_restart''')
307
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000308 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400309 source /srv/salt/scripts/bootstrap.sh
310 cd /srv/salt/scripts
311 source_local_envs
312 saltmaster_init''')
313
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000314 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400315 source /srv/salt/scripts/bootstrap.sh
316 cd /srv/salt/scripts
317 verify_salt_minions''')
318 }
319 }
320 ]
321 config['runFinally'] = [
322 '001_Archive_artefacts': {
323 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
324 archiveArtifacts artifacts: "nodesinfo.tar.gz"
325 }
326 ]
327 testResult = setupDockerAndTest(config)
328 if (testResult) {
329 common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS")
330 } else {
331 throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE")
332 }
333 return testResult
334}
335
336/**
chnydaf14ea2a2017-05-26 15:07:47 +0200337 * setup and test salt-master
338 *
azvyagintsevb4e0c442018-09-12 17:00:04 +0300339 * @param masterName salt master's name
340 * @param clusterName model cluster name
341 * @param extraFormulas extraFormulas to install. DEPRECATED
342 * @param formulasSource formulas source (git or pkg)
343 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
344 * @param testDir directory of model
345 * @param formulasSource Salt formulas source type (optional, default pkg)
346 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +0200347 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +0300348 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
349 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
350 * @param aptRepoUrl package repository with salt formulas
351 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +0300352 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +0200353 */
354
azvyagintsevb4e0c442018-09-12 17:00:04 +0300355def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +0000356 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +0300357 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300358 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300359 // TODO
360 common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' +
361 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
362 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300363 // timeout for test execution (40min)
364 def testTimeout = 40 * 60
365 def TestMarkerResult = false
366 def saltOpts = "--retcode-passthrough --force-color"
367 def workspace = common.getWorkspace()
368 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
369 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +0200370
azvyagintsev635affb2018-09-13 13:02:54 +0300371 if (formulasSource == 'pkg') {
372 if (extraFormulas) {
373 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
374 "\n It would be ignored, and all formulas would be installed anyway")
375 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300376 }
377 if (!dockerContainerName) {
378 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
379 }
380 def dockerMaxCpusOpt = "--cpus=4"
381 if (dockerMaxCpus > 0) {
382 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
383 }
384 try {
385 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
azvyagintsev635affb2018-09-13 13:02:54 +0300386 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
azvyagintsevb4e0c442018-09-12 17:00:04 +0300387 "DISTRIB_REVISION=${formulasRevision}",
388 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
389 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
390 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
391 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
392 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
393 try {
394 // Currently, we don't have any other point to install
395 // runtime dependencies for tests.
396 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +0200397 echo "Installing extra-deb dependencies inside docker:"
398 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
399 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
400 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
401 rm -vf /etc/apt/sources.list.d/* || true
402 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
403 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
404 apt-get update
405 apt-get install -y python-netaddr
406 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300407 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
408 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +0200409 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
410 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
411 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300412 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
413 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +0200414 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
415 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
416 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +0300417 timeout(time: testTimeout, unit: 'SECONDS') {
418 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200419 source /srv/salt/scripts/bootstrap.sh
420 cd /srv/salt/scripts
421 source_local_envs
422 configure_salt_master
423 configure_salt_minion
424 install_salt_formula_pkg
425 source /srv/salt/scripts/bootstrap.sh
426 cd /srv/salt/scripts
427 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300428 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200429 source /srv/salt/scripts/bootstrap.sh
430 cd /srv/salt/scripts
431 source_local_envs
432 saltmaster_init''')
433
azvyagintsevb4e0c442018-09-12 17:00:04 +0300434 if (!legacyTestingMode.toBoolean()) {
435 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200436 source /srv/salt/scripts/bootstrap.sh
437 cd /srv/salt/scripts
438 verify_salt_minions
439 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300440 }
441 }
442 // If we didn't dropped for now - test has been passed.
443 TestMarkerResult = true
444 }
445
446 finally {
447 // Collect rendered per-node data.Those info could be simply used
448 // for diff processing. Data was generated via reclass.cli --nodeinfo,
449 /// during verify_salt_minions.
450 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
451 archiveArtifacts artifacts: "nodesinfo.tar.gz"
452 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200453 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200454 }
chnydaf14ea2a2017-05-26 15:07:47 +0200455 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300456 catch (Exception er) {
457 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
458 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300459
azvyagintsevb4e0c442018-09-12 17:00:04 +0300460 if (legacyTestingMode.toBoolean()) {
461 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
462 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
463 for (minion in nodes.tokenize()) {
464 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
465 if (!basename.trim().contains(masterName)) {
466 testMinion(basename.trim())
467 }
468 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300469 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300470
azvyagintsevb4e0c442018-09-12 17:00:04 +0300471 try {
472 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
473 timeout(time: 10, unit: 'SECONDS') {
474 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
475 }
476 timeout(time: 10, unit: 'SECONDS') {
477 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
478 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300479 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300480 catch (Exception er) {
481 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300482 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300483
azvyagintsevb4e0c442018-09-12 17:00:04 +0300484 if (TestMarkerResult) {
485 common.infoMsg("Test finished: SUCCESS")
486 } else {
487 common.warningMsg("Test finished: FAILURE")
488 }
489 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300490
chnydaf14ea2a2017-05-26 15:07:47 +0200491}
492
493/**
494 * Test salt-minion
495 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300496 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200497 */
498
azvyagintsev28fa9d92018-06-26 14:31:49 +0300499def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300500 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 +0200501}
azvyagintsev2b279d82018-08-07 17:22:54 +0200502
azvyagintsev2b279d82018-08-07 17:22:54 +0200503/**
504 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300505 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
506 Flow: grab all data, and pass to setupAndTestNode function
507 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200508 *
509 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300510 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
511 modelFile
512 DockerCName directly passed to setupAndTestNode
513 EXTRA_FORMULAS directly passed to setupAndTestNode
514 DISTRIB_REVISION directly passed to setupAndTestNode
515 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200516
azvyagintsevb4e0c442018-09-12 17:00:04 +0300517 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200518 */
519
520def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300521 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300522 common.errorMsg('You are using deprecated function!Please migrate to "testNode".' +
523 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
524 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300525 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
526 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
527 // unpack data
528 sh(script: "tar -xzf models.tar.gz ")
529 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
530 content = readFile(file: cfg.modelFile)
531 templateContext = readYaml text: content
532 clusterName = templateContext.default_context.cluster_name
533 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200534
azvyagintsevb4e0c442018-09-12 17:00:04 +0300535 def testResult = false
536 testResult = setupAndTestNode(
537 "cfg01.${clusterDomain}",
538 clusterName,
539 '',
540 cfg.testReclassEnv, // Sync into image exactly one env
541 'pkg',
542 cfg.DISTRIB_REVISION,
543 cfg.reclassVersion,
544 0,
545 false,
546 false,
547 '',
548 '',
549 cfg.DockerCName)
550 if (testResult) {
551 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
552 } else {
553 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
554 }
555 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200556}