blob: 6d7b9aded232eedc2f7e3cd8603589843afc8dcb [file] [log] [blame]
chnydaf14ea2a2017-05-26 15:07:47 +02001package com.mirantis.mk
2
3/**
4 * setup and test salt-master
5 *
azvyagintsevb4e0c442018-09-12 17:00:04 +03006 * @param masterName salt master's name
7 * @param clusterName model cluster name
8 * @param extraFormulas extraFormulas to install. DEPRECATED
9 * @param formulasSource formulas source (git or pkg)
10 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
11 * @param testDir directory of model
12 * @param formulasSource Salt formulas source type (optional, default pkg)
13 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +020014 * @param ignoreClassNotfound Ignore missing classes for reclass model
azvyagintsevb4e0c442018-09-12 17:00:04 +030015 * @param dockerMaxCpus max cpus passed to docker (default 0, disabled)
16 * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models)
17 * @param aptRepoUrl package repository with salt formulas
18 * @param aptRepoGPG GPG key for apt repository with formulas
azvyagintsev28fa9d92018-06-26 14:31:49 +030019 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +020020 */
21
azvyagintsevb4e0c442018-09-12 17:00:04 +030022def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg',
Vasyl Saienko369ed902018-07-23 11:49:32 +000023 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +030024 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsevb4e0c442018-09-12 17:00:04 +030025 def common = new com.mirantis.mk.Common()
26 // timeout for test execution (40min)
27 def testTimeout = 40 * 60
28 def TestMarkerResult = false
29 def saltOpts = "--retcode-passthrough --force-color"
30 def workspace = common.getWorkspace()
31 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
32 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +020033
azvyagintsevb4e0c442018-09-12 17:00:04 +030034 if (!extraFormulas.contains('*') && formulasSource == 'pkg') {
35 common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " +
36 "\n It would be ignored, and all formulas would be installed anyway")
37 }
38 if (!dockerContainerName) {
39 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
40 }
41 def dockerMaxCpusOpt = "--cpus=4"
42 if (dockerMaxCpus > 0) {
43 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
44 }
45 try {
46 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
47 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}",
48 "DISTRIB_REVISION=${formulasRevision}",
49 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
50 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
51 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
52 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
53 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
54 try {
55 // Currently, we don't have any other point to install
56 // runtime dependencies for tests.
57 sh("""#!/bin/bash -xe
azvyagintsev1bfe6842018-08-09 18:40:17 +020058 echo "Installing extra-deb dependencies inside docker:"
59 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
60 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
61 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
62 rm -vf /etc/apt/sources.list.d/* || true
63 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
64 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
65 apt-get update
66 apt-get install -y python-netaddr
67 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +030068 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
69 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
azvyagintsev1bfe6842018-08-09 18:40:17 +020070 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
71 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
72 """)
azvyagintsevb4e0c442018-09-12 17:00:04 +030073 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
74 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev1bfe6842018-08-09 18:40:17 +020075 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
76 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
77 done""")
azvyagintsevb4e0c442018-09-12 17:00:04 +030078 timeout(time: testTimeout, unit: 'SECONDS') {
79 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +020080 source /srv/salt/scripts/bootstrap.sh
81 cd /srv/salt/scripts
82 source_local_envs
83 configure_salt_master
84 configure_salt_minion
85 install_salt_formula_pkg
86 source /srv/salt/scripts/bootstrap.sh
87 cd /srv/salt/scripts
88 saltservice_restart''')
azvyagintsevb4e0c442018-09-12 17:00:04 +030089 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +020090 source /srv/salt/scripts/bootstrap.sh
91 cd /srv/salt/scripts
92 source_local_envs
93 saltmaster_init''')
94
azvyagintsevb4e0c442018-09-12 17:00:04 +030095 if (!legacyTestingMode.toBoolean()) {
96 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +020097 source /srv/salt/scripts/bootstrap.sh
98 cd /srv/salt/scripts
99 verify_salt_minions
100 ''')
azvyagintsevb4e0c442018-09-12 17:00:04 +0300101 }
102 }
103 // If we didn't dropped for now - test has been passed.
104 TestMarkerResult = true
105 }
106
107 finally {
108 // Collect rendered per-node data.Those info could be simply used
109 // for diff processing. Data was generated via reclass.cli --nodeinfo,
110 /// during verify_salt_minions.
111 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
112 archiveArtifacts artifacts: "nodesinfo.tar.gz"
113 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200114 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200115 }
chnydaf14ea2a2017-05-26 15:07:47 +0200116 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300117 catch (Exception er) {
118 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
119 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300120
azvyagintsevb4e0c442018-09-12 17:00:04 +0300121 if (legacyTestingMode.toBoolean()) {
122 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
123 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
124 for (minion in nodes.tokenize()) {
125 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
126 if (!basename.trim().contains(masterName)) {
127 testMinion(basename.trim())
128 }
129 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300130 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300131
azvyagintsevb4e0c442018-09-12 17:00:04 +0300132 try {
133 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
134 timeout(time: 10, unit: 'SECONDS') {
135 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
136 }
137 timeout(time: 10, unit: 'SECONDS') {
138 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
139 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300140 }
azvyagintsevb4e0c442018-09-12 17:00:04 +0300141 catch (Exception er) {
142 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
azvyagintsev28fa9d92018-06-26 14:31:49 +0300143 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300144
azvyagintsevb4e0c442018-09-12 17:00:04 +0300145 if (TestMarkerResult) {
146 common.infoMsg("Test finished: SUCCESS")
147 } else {
148 common.warningMsg("Test finished: FAILURE")
149 }
150 return TestMarkerResult
azvyagintsev28fa9d92018-06-26 14:31:49 +0300151
chnydaf14ea2a2017-05-26 15:07:47 +0200152}
153
154/**
155 * Test salt-minion
156 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300157 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200158 */
159
azvyagintsev28fa9d92018-06-26 14:31:49 +0300160def testMinion(minionName) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300161 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 +0200162}
azvyagintsev2b279d82018-08-07 17:22:54 +0200163
azvyagintsev2b279d82018-08-07 17:22:54 +0200164/**
165 * Wrapper over setupAndTestNode, to test exactly one CC model.
azvyagintsevb4e0c442018-09-12 17:00:04 +0300166 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
167 Flow: grab all data, and pass to setupAndTestNode function
168 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
azvyagintsev2b279d82018-08-07 17:22:54 +0200169 *
170 * @param cfg - dict with params:
azvyagintsevb4e0c442018-09-12 17:00:04 +0300171 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
172 modelFile
173 DockerCName directly passed to setupAndTestNode
174 EXTRA_FORMULAS directly passed to setupAndTestNode
175 DISTRIB_REVISION directly passed to setupAndTestNode
176 reclassVersion directly passed to setupAndTestNode
azvyagintsev2b279d82018-08-07 17:22:54 +0200177
azvyagintsevb4e0c442018-09-12 17:00:04 +0300178 Return: true\exception
azvyagintsev2b279d82018-08-07 17:22:54 +0200179 */
180
181def testCCModel(cfg) {
azvyagintsevb4e0c442018-09-12 17:00:04 +0300182 def common = new com.mirantis.mk.Common()
183 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
184 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
185 // unpack data
186 sh(script: "tar -xzf models.tar.gz ")
187 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
188 content = readFile(file: cfg.modelFile)
189 templateContext = readYaml text: content
190 clusterName = templateContext.default_context.cluster_name
191 clusterDomain = templateContext.default_context.cluster_domain
azvyagintsev2b279d82018-08-07 17:22:54 +0200192
azvyagintsevb4e0c442018-09-12 17:00:04 +0300193 def testResult = false
194 testResult = setupAndTestNode(
195 "cfg01.${clusterDomain}",
196 clusterName,
197 '',
198 cfg.testReclassEnv, // Sync into image exactly one env
199 'pkg',
200 cfg.DISTRIB_REVISION,
201 cfg.reclassVersion,
202 0,
203 false,
204 false,
205 '',
206 '',
207 cfg.DockerCName)
208 if (testResult) {
209 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
210 } else {
211 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
212 }
213 return testResult
azvyagintsev2b279d82018-08-07 17:22:54 +0200214}