blob: a4c90f115b562ba680bc02aa72b51f4396bba602 [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 Egorenkod54f60f2018-10-10 19:38:03 +040046 if (baseRepoPreConfig) {
47 // 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 }
Denis Egorenko3c752a52018-10-12 12:21:29 +040061 }
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040062
Denis Egorenkod54f60f2018-10-10 19:38:03 +040063 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"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000073 pinning: |-
74 Package: libsodium18
75 Pin: release o=SaltStack
76 Pin-Priority: 50
77
78 Package: *
79 Pin: release o=SaltStack
80 Pin-Priority: 1100
81 mcp_extra:
Denis Egorenko3c752a52018-10-12 12:21:29 +040082 source: "${extraRepoSource}"
Denis Egorenko395aa212018-10-11 15:11:28 +040083 mcp_saltformulas:
84 source: "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} salt salt-latest"
85 repo_key: "http://apt.mcp.mirantis.net:8085/public.gpg"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000086 ubuntu:
Denis Egorenko395aa212018-10-11 15:11:28 +040087 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial main restricted universe"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000088 ubuntu-upd:
Denis Egorenko395aa212018-10-11 15:11:28 +040089 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-updates main restricted universe"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000090 ubuntu-sec:
Denis Egorenko395aa212018-10-11 15:11:28 +040091 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-security main restricted universe"
92"""
Denis Egorenkod54f60f2018-10-10 19:38:03 +040093 def extraRepoMergeStrategy = config.get('extraRepoMergeStrategy', 'merge')
94 def extraReposYaml = null
Denis Egorenkoc0a492a2018-10-18 16:14:05 +040095 if (extraRepoMergeStrategy == 'merge') {
Denis Egorenkoc3e8e362018-10-18 16:21:55 +040096 def extraReposYamlConfig = config.get('extraReposYaml', '').trim()
Denis Egorenkod54f60f2018-10-10 19:38:03 +040097 def defaultRepos = readYaml text: defaultExtraReposYaml
Denis Egorenkoc3e8e362018-10-18 16:21:55 +040098 if (extraReposYamlConfig) {
99 def extraRepos = readYaml text: extraReposYamlConfig
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400100
Denis Egorenkoc3e8e362018-10-18 16:21:55 +0400101 Map.metaClass.mergeNested = { Map rhs ->
102 def lhs = delegate
103 rhs.each { k, v -> lhs[k] = lhs[k] in Map ? lhs[k].addNested(v) : v }
104 lhs
105 }
106
107 extraReposYaml = defaultRepos.mergeNested(extraRepos)
108 } else {
109 extraReposYaml = defaultRepos
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400110 }
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400111 } else {
112 extraReposYaml = config.get('extraReposYaml', defaultExtraReposYaml)
113 }
114 }
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400115 def img = docker.image(dockerImageName)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000116
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400117 img.pull()
118
119 try {
120 img.inside(dockerOptsFinal) {
121 withEnv(envOpts) {
122 try {
123 // Currently, we don't have any other point to install
124 // runtime dependencies for tests.
125 if (baseRepoPreConfig) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000126 // Warning! POssible point of 'allow-downgrades' issue
127 // Probably, need to add such flag into apt.prefs
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400128 sh("""#!/bin/bash -xe
129 echo "Installing extra-deb dependencies inside docker:"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000130 echo > /etc/apt/sources.list
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400131 rm -vf /etc/apt/sources.list.d/* || true
Aleksey Zvyagintsevc5453342018-10-05 15:03:59 +0000132 """)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000133 common.debianExtraRepos(extraReposYaml)
134 sh('''#!/bin/bash -xe
135 apt-get update
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400136 apt-get install -y python-netaddr
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000137 ''')
138
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400139 }
140 runCommands.sort().each { command, body ->
141 common.warningMsg("Running command: ${command}")
142 // doCall is the closure implementation in groovy, allow to pass arguments to closure
143 body.call()
144 }
145 // If we didn't dropped for now - test has been passed.
146 TestMarkerResult = true
147 }
148 finally {
149 runFinally.sort().each { command, body ->
150 common.warningMsg("Running ${command} command.")
151 // doCall is the closure implementation in groovy, allow to pass arguments to closure
152 body.call()
153 }
154 }
155 }
156 }
157 }
158 catch (Exception er) {
159 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
160 }
161
162 try {
163 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
164 timeout(time: 10, unit: 'SECONDS') {
165 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
166 }
167 timeout(time: 10, unit: 'SECONDS') {
168 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
169 }
170 }
171 catch (Exception er) {
172 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
173 }
174
175 if (TestMarkerResult) {
176 common.infoMsg("Test finished: SUCCESS")
177 } else {
178 common.warningMsg("Test finished: FAILURE")
179 }
180 return TestMarkerResult
181}
182
183/**
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000184 * Wrapper around setupDockerAndTest, to run checks against new Reclass version
185 * that current model is compatible with new Reclass.
186 *
187 * @param config - LinkedHashMap with configuration params:
188 * dockerHostname - (required) Hostname to use for Docker container.
189 * distribRevision - (optional) Revision of packages to use (default proposed).
190 * extraRepo - (optional) Extra repo to use to install new Reclass version. Has
191 * high priority on distribRevision
192 * targetNodes - (required) List nodes to check pillar data.
Denis Egorenkob090a762018-09-12 19:25:41 +0400193 */
194def compareReclassVersions(config) {
195 def common = new com.mirantis.mk.Common()
196 def salt = new com.mirantis.mk.Salt()
197 common.infoMsg("Going to test new reclass for CFG node")
198 def distribRevision = config.get('distribRevision', 'proposed')
199 def venv = config.get('venv')
200 def extraRepo = config.get('extraRepo', '')
201 def extraRepoKey = config.get('extraRepoKey', '')
202 def targetNodes = config.get('targetNodes')
203 sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
204 sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
205 def configRun = [
azvyagintsevabcf42e2018-10-05 20:40:27 +0300206 'distribRevision': distribRevision,
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000207 'dockerExtraOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400208 "-v /srv/salt/reclass:/srv/salt/reclass:ro",
209 "-v /etc/salt:/etc/salt:ro",
210 "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro"
211 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000212 'envOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400213 "WORKSPACE=${env.WORKSPACE}",
214 "NODES_LIST=${targetNodes.join(' ')}"
215 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000216 'runCommands' : [
217 '001_Update_Reclass_package' : {
218 sh('apt-get update && apt-get install -y reclass')
Denis Egorenkob090a762018-09-12 19:25:41 +0400219 },
220 '002_Test_Reclass_Compatibility': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000221 sh('''
Denis Egorenkob090a762018-09-12 19:25:41 +0400222 reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1
223 for node in $NODES_LIST; do
224 reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1
225 done
226 ''')
227 }
228 ]
229 ]
230 if (extraRepo) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000231 // FIXME
Denis Egorenkob090a762018-09-12 19:25:41 +0400232 configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = {
233 sh("""
234 echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list
235 [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add -
236 """)
237 }
Denis Egorenkob090a762018-09-12 19:25:41 +0400238 }
239 if (setupDockerAndTest(configRun)) {
240 common.infoMsg("New reclass version is compatible with current model: SUCCESS")
241 def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0]
242 // [0..-31] to exclude 'echo Salt command execution success' from output
243 writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31])
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000244 for (String node in targetNodes) {
Denis Egorenkob090a762018-09-12 19:25:41 +0400245 def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0]
246 writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31])
247 }
248 def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '')
249 currentBuild.description = reclassDiff
250 if (reclassDiff != '<b>No job changes</b>') {
251 throw new RuntimeException("Pillars with new reclass version has been changed: FAILED")
252 } else {
253 common.infoMsg("Pillars not changed with new reclass version: SUCCESS")
254 }
255 } else {
256 throw new RuntimeException("New reclass version is not compatible with current model: FAILED")
257 }
258}
259
260/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400261 * Wrapper over setupDockerAndTest, to test CC model.
262 *
263 * @param config - dict with params:
264 * dockerHostname - (required) salt master's name
265 * clusterName - (optional) model cluster name
266 * extraFormulas - (optional) extraFormulas to install. DEPRECATED
267 * formulasSource - (optional) formulas source (git or pkg, default pkg)
268 * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master)
269 * reclassEnv - (require) directory of model
270 * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false)
271 * aptRepoUrl - (optional) package repository with salt formulas
272 * aptRepoGPG - (optional) GPG key for apt repository with formulas
273 * testContext - (optional) Description of test
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000274 Return: true\exception
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400275 */
276
277def testNode(LinkedHashMap config) {
278 def common = new com.mirantis.mk.Common()
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400279 def dockerHostname = config.get('dockerHostname')
280 def reclassEnv = config.get('reclassEnv')
281 def clusterName = config.get('clusterName', "")
282 def formulasSource = config.get('formulasSource', 'pkg')
283 def extraFormulas = config.get('extraFormulas', 'linux')
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400284 def ignoreClassNotfound = config.get('ignoreClassNotfound', false)
285 def aptRepoUrl = config.get('aptRepoUrl', "")
286 def aptRepoGPG = config.get('aptRepoGPG', "")
287 def testContext = config.get('testContext', 'test')
288 config['envOpts'] = [
289 "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5",
290 "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}",
291 "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}",
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400292 "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400293 "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1",
Denis Egorenkod54f60f2018-10-10 19:38:03 +0400294 "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}"
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400295 ]
296
297 config['runCommands'] = [
298 '001_Clone_salt_formulas_scripts': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000299 sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400300 },
301
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000302 '002_Prepare_something' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400303 sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
304 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
305 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
306 ''')
307 },
308
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400309 '003_Install_Reclass_package' : {
310 sh('apt-get install -y reclass')
311 },
312
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000313 '004_Run_tests' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400314 def testTimeout = 40 * 60
315 timeout(time: testTimeout, unit: 'SECONDS') {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000316 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400317 source /srv/salt/scripts/bootstrap.sh
318 cd /srv/salt/scripts
319 source_local_envs
320 configure_salt_master
321 configure_salt_minion
322 install_salt_formula_pkg
323 source /srv/salt/scripts/bootstrap.sh
324 cd /srv/salt/scripts
325 saltservice_restart''')
326
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000327 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400328 source /srv/salt/scripts/bootstrap.sh
329 cd /srv/salt/scripts
330 source_local_envs
331 saltmaster_init''')
332
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000333 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400334 source /srv/salt/scripts/bootstrap.sh
335 cd /srv/salt/scripts
336 verify_salt_minions''')
337 }
338 }
339 ]
340 config['runFinally'] = [
341 '001_Archive_artefacts': {
342 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
343 archiveArtifacts artifacts: "nodesinfo.tar.gz"
344 }
345 ]
346 testResult = setupDockerAndTest(config)
347 if (testResult) {
348 common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS")
349 } else {
350 throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE")
351 }
352 return testResult
353}
354
355/**
chnydaf14ea2a2017-05-26 15:07:47 +0200356 * setup and test salt-master
357 *
azvyagintsevb4e0c442018-09-12 17:00:04 +0300358 * @param masterName salt master's name
359 * @param clusterName model cluster name
360 * @param extraFormulas extraFormulas to install. DEPRECATED
361 * @param formulasSource formulas source (git or pkg)
362 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
363 * @param testDir directory of model
364 * @param formulasSource Salt formulas source type (optional, default pkg)
365 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +0200366 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +0300367 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
368 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
369 * @param aptRepoUrl package repository with salt formulas
370 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +0300371 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +0200372 */
373
azvyagintsevb4e0c442018-09-12 17:00:04 +0300374def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +0000375 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +0300376 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300377 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300378 // TODO
379 common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' +
380 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
381 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300382 // timeout for test execution (40min)
383 def testTimeout = 40 * 60
384 def TestMarkerResult = false
385 def saltOpts = "--retcode-passthrough --force-color"
386 def workspace = common.getWorkspace()
387 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
388 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +0200389
azvyagintsev635affb2018-09-13 13:02:54 +0300390 if (formulasSource == 'pkg') {
391 if (extraFormulas) {
392 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
393 "\n It would be ignored, and all formulas would be installed anyway")
394 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300395 }
396 if (!dockerContainerName) {
397 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
398 }
399 def dockerMaxCpusOpt = "--cpus=4"
400 if (dockerMaxCpus > 0) {
401 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
402 }
403 try {
404 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
azvyagintsev635affb2018-09-13 13:02:54 +0300405 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
azvyagintsevb4e0c442018-09-12 17:00:04 +0300406 "DISTRIB_REVISION=${formulasRevision}",
407 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
408 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
409 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
410 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
411 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
412 try {
413 // Currently, we don't have any other point to install
414 // runtime dependencies for tests.
415 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +0200416 echo "Installing extra-deb dependencies inside docker:"
417 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
418 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
419 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
420 rm -vf /etc/apt/sources.list.d/* || true
421 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
422 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
423 apt-get update
424 apt-get install -y python-netaddr
425 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300426 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
427 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +0200428 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
429 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
430 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300431 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
432 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +0200433 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
434 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
435 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +0300436 timeout(time: testTimeout, unit: 'SECONDS') {
437 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200438 source /srv/salt/scripts/bootstrap.sh
439 cd /srv/salt/scripts
440 source_local_envs
441 configure_salt_master
442 configure_salt_minion
443 install_salt_formula_pkg
444 source /srv/salt/scripts/bootstrap.sh
445 cd /srv/salt/scripts
446 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300447 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200448 source /srv/salt/scripts/bootstrap.sh
449 cd /srv/salt/scripts
450 source_local_envs
451 saltmaster_init''')
452
azvyagintsevb4e0c442018-09-12 17:00:04 +0300453 if (!legacyTestingMode.toBoolean()) {
454 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200455 source /srv/salt/scripts/bootstrap.sh
456 cd /srv/salt/scripts
457 verify_salt_minions
458 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300459 }
460 }
461 // If we didn't dropped for now - test has been passed.
462 TestMarkerResult = true
463 }
464
465 finally {
466 // Collect rendered per-node data.Those info could be simply used
467 // for diff processing. Data was generated via reclass.cli --nodeinfo,
468 /// during verify_salt_minions.
469 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
470 archiveArtifacts artifacts: "nodesinfo.tar.gz"
471 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200472 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200473 }
chnydaf14ea2a2017-05-26 15:07:47 +0200474 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300475 catch (Exception er) {
476 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
477 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300478
azvyagintsevb4e0c442018-09-12 17:00:04 +0300479 if (legacyTestingMode.toBoolean()) {
480 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
481 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
482 for (minion in nodes.tokenize()) {
483 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
484 if (!basename.trim().contains(masterName)) {
485 testMinion(basename.trim())
486 }
487 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300488 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300489
azvyagintsevb4e0c442018-09-12 17:00:04 +0300490 try {
491 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
492 timeout(time: 10, unit: 'SECONDS') {
493 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
494 }
495 timeout(time: 10, unit: 'SECONDS') {
496 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
497 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300498 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300499 catch (Exception er) {
500 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300501 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300502
azvyagintsevb4e0c442018-09-12 17:00:04 +0300503 if (TestMarkerResult) {
504 common.infoMsg("Test finished: SUCCESS")
505 } else {
506 common.warningMsg("Test finished: FAILURE")
507 }
508 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300509
chnydaf14ea2a2017-05-26 15:07:47 +0200510}
511
512/**
513 * Test salt-minion
514 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300515 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200516 */
517
azvyagintsev28fa9d92018-06-26 14:31:49 +0300518def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300519 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 +0200520}
azvyagintsev2b279d82018-08-07 17:22:54 +0200521
azvyagintsev2b279d82018-08-07 17:22:54 +0200522/**
523 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300524 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
525 Flow: grab all data, and pass to setupAndTestNode function
526 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200527 *
528 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300529 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
530 modelFile
531 DockerCName directly passed to setupAndTestNode
532 EXTRA_FORMULAS directly passed to setupAndTestNode
533 DISTRIB_REVISION directly passed to setupAndTestNode
534 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200535
azvyagintsevb4e0c442018-09-12 17:00:04 +0300536 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200537 */
538
539def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300540 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300541 common.errorMsg('You are using deprecated function!Please migrate to "testNode".' +
542 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
543 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300544 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
545 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
546 // unpack data
547 sh(script: "tar -xzf models.tar.gz ")
548 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
549 content = readFile(file: cfg.modelFile)
550 templateContext = readYaml text: content
551 clusterName = templateContext.default_context.cluster_name
552 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200553
azvyagintsevb4e0c442018-09-12 17:00:04 +0300554 def testResult = false
555 testResult = setupAndTestNode(
556 "cfg01.${clusterDomain}",
557 clusterName,
558 '',
559 cfg.testReclassEnv, // Sync into image exactly one env
560 'pkg',
561 cfg.DISTRIB_REVISION,
562 cfg.reclassVersion,
563 0,
564 false,
565 false,
566 '',
567 '',
568 cfg.DockerCName)
569 if (testResult) {
570 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
571 } else {
572 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
573 }
574 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200575}