chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 4 | * 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. |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 8 | * formulasRevision - (optional) Revision of packages to use (default proposed). |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 9 | * 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 | |
| 23 | def 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) |
| 29 | def formulasRevision = config.get('formulasRevision', 'proposed') |
| 30 | 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', []) |
| 38 | envOpts.add("DISTRIB_REVISION=${formulasRevision}") |
| 39 | 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(' ') |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 48 | def defaultExtraReposYaml = ''' |
| 49 | --- |
| 50 | distrib_revision: 'nightly' |
| 51 | aprConfD: |- |
| 52 | APT::Get::AllowUnauthenticated 'true'; |
| 53 | APT::Get::Install-Suggests 'false'; |
| 54 | APT::Get::Install-Recommends 'false'; |
| 55 | repo: |
| 56 | mcp_saltstack: |
| 57 | source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/saltstack-2017.7/xenial xenial main" |
| 58 | pinning: |- |
| 59 | Package: libsodium18 |
| 60 | Pin: release o=SaltStack |
| 61 | Pin-Priority: 50 |
| 62 | |
| 63 | Package: * |
| 64 | Pin: release o=SaltStack |
| 65 | Pin-Priority: 1100 |
| 66 | mcp_extra: |
| 67 | source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/extra/xenial xenial main" |
| 68 | ubuntu: |
| 69 | source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/ubuntu xenial main restricted universe" |
| 70 | ubuntu-upd: |
| 71 | source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/ubuntu xenial-updates main restricted universe" |
| 72 | ubuntu-sec: |
| 73 | source: "deb [arch=amd64] http://mirror.mirantis.com/SUB_DISTRIB_REVISION/ubuntu xenial-security main restricted universe" |
| 74 | ''' |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 75 | def img = docker.image(dockerImageName) |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 76 | def extraReposYaml = config.get('extraReposYaml', defaultExtraReposYaml) |
| 77 | |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 78 | img.pull() |
| 79 | |
| 80 | try { |
| 81 | img.inside(dockerOptsFinal) { |
| 82 | withEnv(envOpts) { |
| 83 | try { |
| 84 | // Currently, we don't have any other point to install |
| 85 | // runtime dependencies for tests. |
| 86 | if (baseRepoPreConfig) { |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 87 | // Warning! POssible point of 'allow-downgrades' issue |
| 88 | // Probably, need to add such flag into apt.prefs |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 89 | sh("""#!/bin/bash -xe |
| 90 | echo "Installing extra-deb dependencies inside docker:" |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 91 | echo > /etc/apt/sources.list |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 92 | rm -vf /etc/apt/sources.list.d/* || true |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 93 | """) |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 94 | common.debianExtraRepos(extraReposYaml) |
| 95 | sh('''#!/bin/bash -xe |
| 96 | apt-get update |
| 97 | apt-get install -y python-netaddr reclass |
| 98 | ''') |
| 99 | |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 100 | } |
| 101 | runCommands.sort().each { command, body -> |
| 102 | common.warningMsg("Running command: ${command}") |
| 103 | // doCall is the closure implementation in groovy, allow to pass arguments to closure |
| 104 | body.call() |
| 105 | } |
| 106 | // If we didn't dropped for now - test has been passed. |
| 107 | TestMarkerResult = true |
| 108 | } |
| 109 | finally { |
| 110 | runFinally.sort().each { command, body -> |
| 111 | common.warningMsg("Running ${command} command.") |
| 112 | // doCall is the closure implementation in groovy, allow to pass arguments to closure |
| 113 | body.call() |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | catch (Exception er) { |
| 120 | common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString()) |
| 121 | } |
| 122 | |
| 123 | try { |
| 124 | common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors") |
| 125 | timeout(time: 10, unit: 'SECONDS') { |
| 126 | sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true) |
| 127 | } |
| 128 | timeout(time: 10, unit: 'SECONDS') { |
| 129 | sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true) |
| 130 | } |
| 131 | } |
| 132 | catch (Exception er) { |
| 133 | common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString()) |
| 134 | } |
| 135 | |
| 136 | if (TestMarkerResult) { |
| 137 | common.infoMsg("Test finished: SUCCESS") |
| 138 | } else { |
| 139 | common.warningMsg("Test finished: FAILURE") |
| 140 | } |
| 141 | return TestMarkerResult |
| 142 | } |
| 143 | |
| 144 | /** |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 145 | * Wrapper around setupDockerAndTest, to run checks against new Reclass version |
| 146 | * that current model is compatible with new Reclass. |
| 147 | * |
| 148 | * @param config - LinkedHashMap with configuration params: |
| 149 | * dockerHostname - (required) Hostname to use for Docker container. |
| 150 | * distribRevision - (optional) Revision of packages to use (default proposed). |
| 151 | * extraRepo - (optional) Extra repo to use to install new Reclass version. Has |
| 152 | * high priority on distribRevision |
| 153 | * targetNodes - (required) List nodes to check pillar data. |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 154 | */ |
| 155 | def compareReclassVersions(config) { |
| 156 | def common = new com.mirantis.mk.Common() |
| 157 | def salt = new com.mirantis.mk.Salt() |
| 158 | common.infoMsg("Going to test new reclass for CFG node") |
| 159 | def distribRevision = config.get('distribRevision', 'proposed') |
| 160 | def venv = config.get('venv') |
| 161 | def extraRepo = config.get('extraRepo', '') |
| 162 | def extraRepoKey = config.get('extraRepoKey', '') |
| 163 | def targetNodes = config.get('targetNodes') |
| 164 | sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new" |
| 165 | sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new" |
| 166 | def configRun = [ |
| 167 | 'formulasRevision': distribRevision, |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 168 | 'dockerExtraOpts' : [ |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 169 | "-v /srv/salt/reclass:/srv/salt/reclass:ro", |
| 170 | "-v /etc/salt:/etc/salt:ro", |
| 171 | "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro" |
| 172 | ], |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 173 | 'envOpts' : [ |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 174 | "WORKSPACE=${env.WORKSPACE}", |
| 175 | "NODES_LIST=${targetNodes.join(' ')}" |
| 176 | ], |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 177 | 'runCommands' : [ |
| 178 | '001_Update_Reclass_package' : { |
| 179 | sh('apt-get update && apt-get install -y reclass') |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 180 | }, |
| 181 | '002_Test_Reclass_Compatibility': { |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 182 | sh(''' |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 183 | reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1 |
| 184 | for node in $NODES_LIST; do |
| 185 | reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1 |
| 186 | done |
| 187 | ''') |
| 188 | } |
| 189 | ] |
| 190 | ] |
| 191 | if (extraRepo) { |
| 192 | configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = { |
| 193 | sh(""" |
| 194 | echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list |
| 195 | [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add - |
| 196 | """) |
| 197 | } |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 198 | } |
| 199 | if (setupDockerAndTest(configRun)) { |
| 200 | common.infoMsg("New reclass version is compatible with current model: SUCCESS") |
| 201 | def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0] |
| 202 | // [0..-31] to exclude 'echo Salt command execution success' from output |
| 203 | writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31]) |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 204 | for (String node in targetNodes) { |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 205 | def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0] |
| 206 | writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31]) |
| 207 | } |
| 208 | def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '') |
| 209 | currentBuild.description = reclassDiff |
| 210 | if (reclassDiff != '<b>No job changes</b>') { |
| 211 | throw new RuntimeException("Pillars with new reclass version has been changed: FAILED") |
| 212 | } else { |
| 213 | common.infoMsg("Pillars not changed with new reclass version: SUCCESS") |
| 214 | } |
| 215 | } else { |
| 216 | throw new RuntimeException("New reclass version is not compatible with current model: FAILED") |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 221 | * Wrapper over setupDockerAndTest, to test CC model. |
| 222 | * |
| 223 | * @param config - dict with params: |
| 224 | * dockerHostname - (required) salt master's name |
| 225 | * clusterName - (optional) model cluster name |
| 226 | * extraFormulas - (optional) extraFormulas to install. DEPRECATED |
| 227 | * formulasSource - (optional) formulas source (git or pkg, default pkg) |
| 228 | * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master) |
| 229 | * reclassEnv - (require) directory of model |
| 230 | * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false) |
| 231 | * aptRepoUrl - (optional) package repository with salt formulas |
| 232 | * aptRepoGPG - (optional) GPG key for apt repository with formulas |
| 233 | * testContext - (optional) Description of test |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 234 | Return: true\exception |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 235 | */ |
| 236 | |
| 237 | def testNode(LinkedHashMap config) { |
| 238 | def common = new com.mirantis.mk.Common() |
| 239 | def result = '' |
| 240 | def dockerHostname = config.get('dockerHostname') |
| 241 | def reclassEnv = config.get('reclassEnv') |
| 242 | def clusterName = config.get('clusterName', "") |
| 243 | def formulasSource = config.get('formulasSource', 'pkg') |
| 244 | def extraFormulas = config.get('extraFormulas', 'linux') |
| 245 | def reclassVersion = config.get('reclassVersion', 'master') |
| 246 | def ignoreClassNotfound = config.get('ignoreClassNotfound', false) |
| 247 | def aptRepoUrl = config.get('aptRepoUrl', "") |
| 248 | def aptRepoGPG = config.get('aptRepoGPG', "") |
| 249 | def testContext = config.get('testContext', 'test') |
| 250 | config['envOpts'] = [ |
| 251 | "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5", |
| 252 | "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}", |
| 253 | "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}", |
| 254 | "EXTRA_FORMULAS=${extraFormulas}", "RECLASS_VERSION=${reclassVersion}", |
| 255 | "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1", |
| 256 | "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}", |
| 257 | "EXTRA_FORMULAS_PKG_ALL=true" |
| 258 | ] |
| 259 | |
| 260 | config['runCommands'] = [ |
| 261 | '001_Clone_salt_formulas_scripts': { |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 262 | sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true) |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 263 | }, |
| 264 | |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 265 | '002_Prepare_something' : { |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 266 | sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts |
| 267 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 268 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 269 | ''') |
| 270 | }, |
| 271 | |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 272 | '004_Run_tests' : { |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 273 | def testTimeout = 40 * 60 |
| 274 | timeout(time: testTimeout, unit: 'SECONDS') { |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 275 | sh('''#!/bin/bash |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 276 | source /srv/salt/scripts/bootstrap.sh |
| 277 | cd /srv/salt/scripts |
| 278 | source_local_envs |
| 279 | configure_salt_master |
| 280 | configure_salt_minion |
| 281 | install_salt_formula_pkg |
| 282 | source /srv/salt/scripts/bootstrap.sh |
| 283 | cd /srv/salt/scripts |
| 284 | saltservice_restart''') |
| 285 | |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 286 | sh('''#!/bin/bash |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 287 | source /srv/salt/scripts/bootstrap.sh |
| 288 | cd /srv/salt/scripts |
| 289 | source_local_envs |
| 290 | saltmaster_init''') |
| 291 | |
azvyagintsev | 9aadf52 | 2018-10-05 14:35:37 +0300 | [diff] [blame^] | 292 | sh('''#!/bin/bash |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 293 | source /srv/salt/scripts/bootstrap.sh |
| 294 | cd /srv/salt/scripts |
| 295 | verify_salt_minions''') |
| 296 | } |
| 297 | } |
| 298 | ] |
| 299 | config['runFinally'] = [ |
| 300 | '001_Archive_artefacts': { |
| 301 | sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true) |
| 302 | archiveArtifacts artifacts: "nodesinfo.tar.gz" |
| 303 | } |
| 304 | ] |
| 305 | testResult = setupDockerAndTest(config) |
| 306 | if (testResult) { |
| 307 | common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS") |
| 308 | } else { |
| 309 | throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE") |
| 310 | } |
| 311 | return testResult |
| 312 | } |
| 313 | |
| 314 | /** |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 315 | * setup and test salt-master |
| 316 | * |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 317 | * @param masterName salt master's name |
| 318 | * @param clusterName model cluster name |
| 319 | * @param extraFormulas extraFormulas to install. DEPRECATED |
| 320 | * @param formulasSource formulas source (git or pkg) |
| 321 | * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master) |
| 322 | * @param testDir directory of model |
| 323 | * @param formulasSource Salt formulas source type (optional, default pkg) |
| 324 | * @param formulasRevision APT revision for formulas (optional default stable) |
Petr Michalec | 6414aa5 | 2017-08-17 14:32:52 +0200 | [diff] [blame] | 325 | * @param ignoreClassNotfound Ignore missing classes for reclass model |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 326 | * @param dockerMaxCpus max cpus passed to docker (default 0, disabled) |
| 327 | * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models) |
| 328 | * @param aptRepoUrl package repository with salt formulas |
| 329 | * @param aptRepoGPG GPG key for apt repository with formulas |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 330 | * Return true | false |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 331 | */ |
| 332 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 333 | def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg', |
Vasyl Saienko | 369ed90 | 2018-07-23 11:49:32 +0000 | [diff] [blame] | 334 | formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0, |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 335 | ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 336 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 1cecc09 | 2018-09-14 13:19:16 +0300 | [diff] [blame] | 337 | // TODO |
| 338 | common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' + |
| 339 | 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..') |
| 340 | sh('sleep 60') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 341 | // timeout for test execution (40min) |
| 342 | def testTimeout = 40 * 60 |
| 343 | def TestMarkerResult = false |
| 344 | def saltOpts = "--retcode-passthrough --force-color" |
| 345 | def workspace = common.getWorkspace() |
| 346 | def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7") |
| 347 | img.pull() |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 348 | |
azvyagintsev | 635affb | 2018-09-13 13:02:54 +0300 | [diff] [blame] | 349 | if (formulasSource == 'pkg') { |
| 350 | if (extraFormulas) { |
| 351 | common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " + |
| 352 | "\n It would be ignored, and all formulas would be installed anyway") |
| 353 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 354 | } |
| 355 | if (!dockerContainerName) { |
| 356 | dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString() |
| 357 | } |
| 358 | def dockerMaxCpusOpt = "--cpus=4" |
| 359 | if (dockerMaxCpus > 0) { |
| 360 | dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}" |
| 361 | } |
| 362 | try { |
| 363 | img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") { |
azvyagintsev | 635affb | 2018-09-13 13:02:54 +0300 | [diff] [blame] | 364 | withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true", |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 365 | "DISTRIB_REVISION=${formulasRevision}", |
| 366 | "DEBUG=1", "MASTER_HOSTNAME=${masterName}", |
| 367 | "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", |
| 368 | "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", |
| 369 | "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5", |
| 370 | "APT_REPOSITORY_GPG=${aptRepoGPG}"]) { |
| 371 | try { |
| 372 | // Currently, we don't have any other point to install |
| 373 | // runtime dependencies for tests. |
| 374 | sh("""#!/bin/bash -xe |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 375 | echo "Installing extra-deb dependencies inside docker:" |
| 376 | echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode |
| 377 | echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode |
| 378 | echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode |
| 379 | rm -vf /etc/apt/sources.list.d/* || true |
| 380 | echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list |
| 381 | echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list |
| 382 | apt-get update |
| 383 | apt-get install -y python-netaddr |
| 384 | """) |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 385 | sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true) |
| 386 | sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 387 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 388 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 389 | """) |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 390 | // FIXME: should be changed to use reclass from mcp_extra_nigtly? |
| 391 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 392 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
| 393 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion}; |
| 394 | done""") |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 395 | timeout(time: testTimeout, unit: 'SECONDS') { |
| 396 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 397 | source /srv/salt/scripts/bootstrap.sh |
| 398 | cd /srv/salt/scripts |
| 399 | source_local_envs |
| 400 | configure_salt_master |
| 401 | configure_salt_minion |
| 402 | install_salt_formula_pkg |
| 403 | source /srv/salt/scripts/bootstrap.sh |
| 404 | cd /srv/salt/scripts |
| 405 | saltservice_restart''') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 406 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 407 | source /srv/salt/scripts/bootstrap.sh |
| 408 | cd /srv/salt/scripts |
| 409 | source_local_envs |
| 410 | saltmaster_init''') |
| 411 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 412 | if (!legacyTestingMode.toBoolean()) { |
| 413 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 414 | source /srv/salt/scripts/bootstrap.sh |
| 415 | cd /srv/salt/scripts |
| 416 | verify_salt_minions |
| 417 | ''') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | // If we didn't dropped for now - test has been passed. |
| 421 | TestMarkerResult = true |
| 422 | } |
| 423 | |
| 424 | finally { |
| 425 | // Collect rendered per-node data.Those info could be simply used |
| 426 | // for diff processing. Data was generated via reclass.cli --nodeinfo, |
| 427 | /// during verify_salt_minions. |
| 428 | sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true) |
| 429 | archiveArtifacts artifacts: "nodesinfo.tar.gz" |
| 430 | } |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 431 | } |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 432 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 433 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 434 | catch (Exception er) { |
| 435 | common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString()) |
| 436 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 437 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 438 | if (legacyTestingMode.toBoolean()) { |
| 439 | common.infoMsg("Running legacy mode test for master hostname ${masterName}") |
| 440 | def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true) |
| 441 | for (minion in nodes.tokenize()) { |
| 442 | def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true) |
| 443 | if (!basename.trim().contains(masterName)) { |
| 444 | testMinion(basename.trim()) |
| 445 | } |
| 446 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 447 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 448 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 449 | try { |
| 450 | common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors") |
| 451 | timeout(time: 10, unit: 'SECONDS') { |
| 452 | sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true) |
| 453 | } |
| 454 | timeout(time: 10, unit: 'SECONDS') { |
| 455 | sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true) |
| 456 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 457 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 458 | catch (Exception er) { |
| 459 | common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString()) |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 460 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 461 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 462 | if (TestMarkerResult) { |
| 463 | common.infoMsg("Test finished: SUCCESS") |
| 464 | } else { |
| 465 | common.warningMsg("Test finished: FAILURE") |
| 466 | } |
| 467 | return TestMarkerResult |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 468 | |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Test salt-minion |
| 473 | * |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 474 | * @param minion salt minion |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 475 | */ |
| 476 | |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 477 | def testMinion(minionName) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 478 | sh(script: "bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'", returnStdout: true) |
Jakub Josef | fa6ad8d | 2017-06-26 18:29:55 +0200 | [diff] [blame] | 479 | } |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 480 | |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 481 | /** |
| 482 | * Wrapper over setupAndTestNode, to test exactly one CC model. |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 483 | Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ |
| 484 | Flow: grab all data, and pass to setupAndTestNode function |
| 485 | under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*` |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 486 | * |
| 487 | * @param cfg - dict with params: |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 488 | MODELS_TARGZ http link to arch with (models|contexts|global_reclass) |
| 489 | modelFile |
| 490 | DockerCName directly passed to setupAndTestNode |
| 491 | EXTRA_FORMULAS directly passed to setupAndTestNode |
| 492 | DISTRIB_REVISION directly passed to setupAndTestNode |
| 493 | reclassVersion directly passed to setupAndTestNode |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 494 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 495 | Return: true\exception |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 496 | */ |
| 497 | |
| 498 | def testCCModel(cfg) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 499 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 1cecc09 | 2018-09-14 13:19:16 +0300 | [diff] [blame] | 500 | common.errorMsg('You are using deprecated function!Please migrate to "testNode".' + |
| 501 | 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..') |
| 502 | sh('sleep 60') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 503 | sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true) |
| 504 | sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}") |
| 505 | // unpack data |
| 506 | sh(script: "tar -xzf models.tar.gz ") |
| 507 | common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}") |
| 508 | content = readFile(file: cfg.modelFile) |
| 509 | templateContext = readYaml text: content |
| 510 | clusterName = templateContext.default_context.cluster_name |
| 511 | clusterDomain = templateContext.default_context.cluster_domain |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 512 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 513 | def testResult = false |
| 514 | testResult = setupAndTestNode( |
| 515 | "cfg01.${clusterDomain}", |
| 516 | clusterName, |
| 517 | '', |
| 518 | cfg.testReclassEnv, // Sync into image exactly one env |
| 519 | 'pkg', |
| 520 | cfg.DISTRIB_REVISION, |
| 521 | cfg.reclassVersion, |
| 522 | 0, |
| 523 | false, |
| 524 | false, |
| 525 | '', |
| 526 | '', |
| 527 | cfg.DockerCName) |
| 528 | if (testResult) { |
| 529 | common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS") |
| 530 | } else { |
| 531 | throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE") |
| 532 | } |
| 533 | return testResult |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 534 | } |