blob: aeb78743210b7c367950d4ff647693047d7efac7 [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(' ')
Denis Egorenko395aa212018-10-11 15:11:28 +040048 def defaultExtraReposYaml = """
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000049---
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000050aprConfD: |-
51 APT::Get::AllowUnauthenticated 'true';
52 APT::Get::Install-Suggests 'false';
53 APT::Get::Install-Recommends 'false';
54repo:
55 mcp_saltstack:
Denis Egorenko395aa212018-10-11 15:11:28 +040056 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/saltstack-2017.7/xenial xenial main"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000057 pinning: |-
58 Package: libsodium18
59 Pin: release o=SaltStack
60 Pin-Priority: 50
61
62 Package: *
63 Pin: release o=SaltStack
64 Pin-Priority: 1100
65 mcp_extra:
Denis Egorenko395aa212018-10-11 15:11:28 +040066 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/extra/xenial xenial main"
67 mcp_saltformulas:
68 source: "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} salt salt-latest"
69 repo_key: "http://apt.mcp.mirantis.net:8085/public.gpg"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000070 ubuntu:
Denis Egorenko395aa212018-10-11 15:11:28 +040071 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial main restricted universe"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000072 ubuntu-upd:
Denis Egorenko395aa212018-10-11 15:11:28 +040073 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-updates main restricted universe"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000074 ubuntu-sec:
Denis Egorenko395aa212018-10-11 15:11:28 +040075 source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-security main restricted universe"
76"""
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040077 def img = docker.image(dockerImageName)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000078 def extraReposYaml = config.get('extraReposYaml', defaultExtraReposYaml)
79
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040080 img.pull()
81
82 try {
83 img.inside(dockerOptsFinal) {
84 withEnv(envOpts) {
85 try {
86 // Currently, we don't have any other point to install
87 // runtime dependencies for tests.
88 if (baseRepoPreConfig) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000089 // Warning! POssible point of 'allow-downgrades' issue
90 // Probably, need to add such flag into apt.prefs
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040091 sh("""#!/bin/bash -xe
92 echo "Installing extra-deb dependencies inside docker:"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000093 echo > /etc/apt/sources.list
Denis Egorenko6fd79ac2018-09-12 13:28:21 +040094 rm -vf /etc/apt/sources.list.d/* || true
Aleksey Zvyagintsevc5453342018-10-05 15:03:59 +000095 """)
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +000096 common.debianExtraRepos(extraReposYaml)
97 sh('''#!/bin/bash -xe
98 apt-get update
Denis Egorenkoc6b24be2018-10-10 17:36:04 +040099 apt-get install -y python-netaddr
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000100 ''')
101
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400102 }
103 runCommands.sort().each { command, body ->
104 common.warningMsg("Running command: ${command}")
105 // doCall is the closure implementation in groovy, allow to pass arguments to closure
106 body.call()
107 }
108 // If we didn't dropped for now - test has been passed.
109 TestMarkerResult = true
110 }
111 finally {
112 runFinally.sort().each { command, body ->
113 common.warningMsg("Running ${command} command.")
114 // doCall is the closure implementation in groovy, allow to pass arguments to closure
115 body.call()
116 }
117 }
118 }
119 }
120 }
121 catch (Exception er) {
122 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
123 }
124
125 try {
126 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
127 timeout(time: 10, unit: 'SECONDS') {
128 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
129 }
130 timeout(time: 10, unit: 'SECONDS') {
131 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
132 }
133 }
134 catch (Exception er) {
135 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
136 }
137
138 if (TestMarkerResult) {
139 common.infoMsg("Test finished: SUCCESS")
140 } else {
141 common.warningMsg("Test finished: FAILURE")
142 }
143 return TestMarkerResult
144}
145
146/**
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000147 * Wrapper around setupDockerAndTest, to run checks against new Reclass version
148 * that current model is compatible with new Reclass.
149 *
150 * @param config - LinkedHashMap with configuration params:
151 * dockerHostname - (required) Hostname to use for Docker container.
152 * distribRevision - (optional) Revision of packages to use (default proposed).
153 * extraRepo - (optional) Extra repo to use to install new Reclass version. Has
154 * high priority on distribRevision
155 * targetNodes - (required) List nodes to check pillar data.
Denis Egorenkob090a762018-09-12 19:25:41 +0400156 */
157def compareReclassVersions(config) {
158 def common = new com.mirantis.mk.Common()
159 def salt = new com.mirantis.mk.Salt()
160 common.infoMsg("Going to test new reclass for CFG node")
161 def distribRevision = config.get('distribRevision', 'proposed')
162 def venv = config.get('venv')
163 def extraRepo = config.get('extraRepo', '')
164 def extraRepoKey = config.get('extraRepoKey', '')
165 def targetNodes = config.get('targetNodes')
166 sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
167 sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new"
168 def configRun = [
azvyagintsevabcf42e2018-10-05 20:40:27 +0300169 'distribRevision': distribRevision,
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000170 'dockerExtraOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400171 "-v /srv/salt/reclass:/srv/salt/reclass:ro",
172 "-v /etc/salt:/etc/salt:ro",
173 "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro"
174 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000175 'envOpts' : [
Denis Egorenkob090a762018-09-12 19:25:41 +0400176 "WORKSPACE=${env.WORKSPACE}",
177 "NODES_LIST=${targetNodes.join(' ')}"
178 ],
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000179 'runCommands' : [
180 '001_Update_Reclass_package' : {
181 sh('apt-get update && apt-get install -y reclass')
Denis Egorenkob090a762018-09-12 19:25:41 +0400182 },
183 '002_Test_Reclass_Compatibility': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000184 sh('''
Denis Egorenkob090a762018-09-12 19:25:41 +0400185 reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1
186 for node in $NODES_LIST; do
187 reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1
188 done
189 ''')
190 }
191 ]
192 ]
193 if (extraRepo) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000194 // FIXME
Denis Egorenkob090a762018-09-12 19:25:41 +0400195 configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = {
196 sh("""
197 echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list
198 [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add -
199 """)
200 }
Denis Egorenkob090a762018-09-12 19:25:41 +0400201 }
202 if (setupDockerAndTest(configRun)) {
203 common.infoMsg("New reclass version is compatible with current model: SUCCESS")
204 def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0]
205 // [0..-31] to exclude 'echo Salt command execution success' from output
206 writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31])
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000207 for (String node in targetNodes) {
Denis Egorenkob090a762018-09-12 19:25:41 +0400208 def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0]
209 writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31])
210 }
211 def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '')
212 currentBuild.description = reclassDiff
213 if (reclassDiff != '<b>No job changes</b>') {
214 throw new RuntimeException("Pillars with new reclass version has been changed: FAILED")
215 } else {
216 common.infoMsg("Pillars not changed with new reclass version: SUCCESS")
217 }
218 } else {
219 throw new RuntimeException("New reclass version is not compatible with current model: FAILED")
220 }
221}
222
223/**
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400224 * Wrapper over setupDockerAndTest, to test CC model.
225 *
226 * @param config - dict with params:
227 * dockerHostname - (required) salt master's name
228 * clusterName - (optional) model cluster name
229 * extraFormulas - (optional) extraFormulas to install. DEPRECATED
230 * formulasSource - (optional) formulas source (git or pkg, default pkg)
231 * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master)
232 * reclassEnv - (require) directory of model
233 * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false)
234 * aptRepoUrl - (optional) package repository with salt formulas
235 * aptRepoGPG - (optional) GPG key for apt repository with formulas
236 * testContext - (optional) Description of test
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000237 Return: true\exception
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400238 */
239
240def testNode(LinkedHashMap config) {
241 def common = new com.mirantis.mk.Common()
242 def result = ''
243 def dockerHostname = config.get('dockerHostname')
244 def reclassEnv = config.get('reclassEnv')
245 def clusterName = config.get('clusterName', "")
246 def formulasSource = config.get('formulasSource', 'pkg')
247 def extraFormulas = config.get('extraFormulas', 'linux')
248 def reclassVersion = config.get('reclassVersion', 'master')
249 def ignoreClassNotfound = config.get('ignoreClassNotfound', false)
250 def aptRepoUrl = config.get('aptRepoUrl', "")
251 def aptRepoGPG = config.get('aptRepoGPG', "")
252 def testContext = config.get('testContext', 'test')
253 config['envOpts'] = [
254 "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5",
255 "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}",
256 "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}",
257 "EXTRA_FORMULAS=${extraFormulas}", "RECLASS_VERSION=${reclassVersion}",
258 "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1",
259 "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}",
260 "EXTRA_FORMULAS_PKG_ALL=true"
261 ]
262
263 config['runCommands'] = [
264 '001_Clone_salt_formulas_scripts': {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000265 sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400266 },
267
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000268 '002_Prepare_something' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400269 sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
270 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
271 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
272 ''')
273 },
274
Denis Egorenkoc6b24be2018-10-10 17:36:04 +0400275 '003_Install_Reclass_package' : {
276 sh('apt-get install -y reclass')
277 },
278
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000279 '004_Run_tests' : {
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400280 def testTimeout = 40 * 60
281 timeout(time: testTimeout, unit: 'SECONDS') {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000282 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400283 source /srv/salt/scripts/bootstrap.sh
284 cd /srv/salt/scripts
285 source_local_envs
286 configure_salt_master
287 configure_salt_minion
288 install_salt_formula_pkg
289 source /srv/salt/scripts/bootstrap.sh
290 cd /srv/salt/scripts
291 saltservice_restart''')
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 source_local_envs
297 saltmaster_init''')
298
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000299 sh('''#!/bin/bash
Denis Egorenko6fd79ac2018-09-12 13:28:21 +0400300 source /srv/salt/scripts/bootstrap.sh
301 cd /srv/salt/scripts
302 verify_salt_minions''')
303 }
304 }
305 ]
306 config['runFinally'] = [
307 '001_Archive_artefacts': {
308 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
309 archiveArtifacts artifacts: "nodesinfo.tar.gz"
310 }
311 ]
312 testResult = setupDockerAndTest(config)
313 if (testResult) {
314 common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS")
315 } else {
316 throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE")
317 }
318 return testResult
319}
320
321/**
chnydaf14ea2a2017-05-26 15:07:47 +0200322 * setup and test salt-master
323 *
azvyagintsevb4e0c442018-09-12 17:00:04 +0300324 * @param masterName salt master's name
325 * @param clusterName model cluster name
326 * @param extraFormulas extraFormulas to install. DEPRECATED
327 * @param formulasSource formulas source (git or pkg)
328 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
329 * @param testDir directory of model
330 * @param formulasSource Salt formulas source type (optional, default pkg)
331 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +0200332 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +0300333 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
334 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
335 * @param aptRepoUrl package repository with salt formulas
336 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +0300337 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +0200338 */
339
azvyagintsevb4e0c442018-09-12 17:00:04 +0300340def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +0000341 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +0300342 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300343 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300344 // TODO
345 common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' +
346 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
347 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300348 // timeout for test execution (40min)
349 def testTimeout = 40 * 60
350 def TestMarkerResult = false
351 def saltOpts = "--retcode-passthrough --force-color"
352 def workspace = common.getWorkspace()
353 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
354 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +0200355
azvyagintsev635affb2018-09-13 13:02:54 +0300356 if (formulasSource == 'pkg') {
357 if (extraFormulas) {
358 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
359 "\n It would be ignored, and all formulas would be installed anyway")
360 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300361 }
362 if (!dockerContainerName) {
363 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
364 }
365 def dockerMaxCpusOpt = "--cpus=4"
366 if (dockerMaxCpus > 0) {
367 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
368 }
369 try {
370 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
azvyagintsev635affb2018-09-13 13:02:54 +0300371 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true",
azvyagintsevb4e0c442018-09-12 17:00:04 +0300372 "DISTRIB_REVISION=${formulasRevision}",
373 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
374 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
375 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
376 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
377 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
378 try {
379 // Currently, we don't have any other point to install
380 // runtime dependencies for tests.
381 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +0200382 echo "Installing extra-deb dependencies inside docker:"
383 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
384 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
385 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
386 rm -vf /etc/apt/sources.list.d/* || true
387 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
388 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
389 apt-get update
390 apt-get install -y python-netaddr
391 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300392 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
393 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +0200394 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
395 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
396 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +0300397 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
398 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +0200399 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
400 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
401 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +0300402 timeout(time: testTimeout, unit: 'SECONDS') {
403 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200404 source /srv/salt/scripts/bootstrap.sh
405 cd /srv/salt/scripts
406 source_local_envs
407 configure_salt_master
408 configure_salt_minion
409 install_salt_formula_pkg
410 source /srv/salt/scripts/bootstrap.sh
411 cd /srv/salt/scripts
412 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300413 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200414 source /srv/salt/scripts/bootstrap.sh
415 cd /srv/salt/scripts
416 source_local_envs
417 saltmaster_init''')
418
azvyagintsevb4e0c442018-09-12 17:00:04 +0300419 if (!legacyTestingMode.toBoolean()) {
420 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +0200421 source /srv/salt/scripts/bootstrap.sh
422 cd /srv/salt/scripts
423 verify_salt_minions
424 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300425 }
426 }
427 // If we didn't dropped for now - test has been passed.
428 TestMarkerResult = true
429 }
430
431 finally {
432 // Collect rendered per-node data.Those info could be simply used
433 // for diff processing. Data was generated via reclass.cli --nodeinfo,
434 /// during verify_salt_minions.
435 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
436 archiveArtifacts artifacts: "nodesinfo.tar.gz"
437 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200438 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200439 }
chnydaf14ea2a2017-05-26 15:07:47 +0200440 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300441 catch (Exception er) {
442 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
443 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300444
azvyagintsevb4e0c442018-09-12 17:00:04 +0300445 if (legacyTestingMode.toBoolean()) {
446 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
447 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
448 for (minion in nodes.tokenize()) {
449 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
450 if (!basename.trim().contains(masterName)) {
451 testMinion(basename.trim())
452 }
453 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300454 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300455
azvyagintsevb4e0c442018-09-12 17:00:04 +0300456 try {
457 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
458 timeout(time: 10, unit: 'SECONDS') {
459 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
460 }
461 timeout(time: 10, unit: 'SECONDS') {
462 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
463 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300464 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300465 catch (Exception er) {
466 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300467 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300468
azvyagintsevb4e0c442018-09-12 17:00:04 +0300469 if (TestMarkerResult) {
470 common.infoMsg("Test finished: SUCCESS")
471 } else {
472 common.warningMsg("Test finished: FAILURE")
473 }
474 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300475
chnydaf14ea2a2017-05-26 15:07:47 +0200476}
477
478/**
479 * Test salt-minion
480 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300481 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200482 */
483
azvyagintsev28fa9d92018-06-26 14:31:49 +0300484def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300485 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 +0200486}
azvyagintsev2b279d82018-08-07 17:22:54 +0200487
azvyagintsev2b279d82018-08-07 17:22:54 +0200488/**
489 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300490 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
491 Flow: grab all data, and pass to setupAndTestNode function
492 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200493 *
494 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300495 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
496 modelFile
497 DockerCName directly passed to setupAndTestNode
498 EXTRA_FORMULAS directly passed to setupAndTestNode
499 DISTRIB_REVISION directly passed to setupAndTestNode
500 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200501
azvyagintsevb4e0c442018-09-12 17:00:04 +0300502 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200503 */
504
505def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300506 def common = new com.mirantis.mk.Common()
azvyagintsev1cecc092018-09-14 13:19:16 +0300507 common.errorMsg('You are using deprecated function!Please migrate to "testNode".' +
508 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..')
509 sh('sleep 60')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300510 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
511 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
512 // unpack data
513 sh(script: "tar -xzf models.tar.gz ")
514 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
515 content = readFile(file: cfg.modelFile)
516 templateContext = readYaml text: content
517 clusterName = templateContext.default_context.cluster_name
518 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200519
azvyagintsevb4e0c442018-09-12 17:00:04 +0300520 def testResult = false
521 testResult = setupAndTestNode(
522 "cfg01.${clusterDomain}",
523 clusterName,
524 '',
525 cfg.testReclassEnv, // Sync into image exactly one env
526 'pkg',
527 cfg.DISTRIB_REVISION,
528 cfg.reclassVersion,
529 0,
530 false,
531 false,
532 '',
533 '',
534 cfg.DockerCName)
535 if (testResult) {
536 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
537 } else {
538 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
539 }
540 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200541}