Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 1 | package com.mirantis.mcp |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Tests providing functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Configure docker image with tests |
| 11 | * |
| 12 | * @param dockerImageLink Docker image link with rally and tempest |
| 13 | * @param target Host to run tests |
| 14 | * @param output_dir Directory for results |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 15 | * @param spt_variables The set of variables for SPT |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 16 | */ |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 17 | def runContainerConfiguration(master, dockerImageLink, target, output_dir, spt_variables){ |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 18 | def salt = new com.mirantis.mk.Salt() |
| 19 | def common = new com.mirantis.mk.Common() |
| 20 | def output_file = 'docker.log' |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 21 | def nodes = getNodeList(master) |
| 22 | def nodes_hw = getNodeList(master, 'G@virtual:physical') |
Dmitrii Kabanov | e38aff4 | 2017-08-16 15:11:44 -0700 | [diff] [blame] | 23 | def controller = salt.minionPresent(master, 'I@salt:master', 'ctl01', true, null, true, 200, 1)['return'][0].values()[0] |
| 24 | def _pillar = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -p ${controller} | " + |
| 25 | "python -c 'import json,sys; print(json.dumps(json.loads(sys.stdin.read())[\"keystone\"][\"server\"]))'") |
| 26 | def keystone = common.parseJSON(_pillar['return'][0].values()[0]) |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 27 | def ssh_key = getFileContent(master, 'I@salt:master', '/root/.ssh/id_rsa') |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 28 | salt.cmdRun(master, target, "docker run -tid --net=host --name=qa_tools " + |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 29 | " ${spt_variables} " + |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 30 | "-e tempest_version=15.0.0 -e OS_USERNAME=${keystone.admin_name} " + |
| 31 | "-e OS_PASSWORD=${keystone.admin_password} -e OS_TENANT_NAME=${keystone.admin_tenant} " + |
| 32 | "-e OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0 " + |
| 33 | "-e OS_REGION_NAME=${keystone.region} -e OS_ENDPOINT_TYPE=admin ${dockerImageLink} /bin/bash") |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 34 | salt.cmdRun(master, target, "docker exec qa_tools bash -c \"sudo mkdir -p /root/.ssh; " + |
| 35 | "echo \'${ssh_key}\' | sudo tee /root/.ssh/id_rsa > /dev/null; " + |
| 36 | "sudo chmod 700 /root/.ssh; sudo chmod 600 /root/.ssh/id_rsa; " + |
| 37 | "echo -e '${nodes}' > nodes.json; echo -e '${nodes_hw}' > nodes_hw.json\"") |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 38 | salt.cmdRun(master, target, "docker exec qa_tools bash -c /opt/devops-qa-tools/deployment/configure.sh > ${output_file}") |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 39 | def file_content = getFileContent(master, target, output_file) |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 40 | writeFile file: "${output_dir}${output_file}", text: file_content |
| 41 | } |
| 42 | |
| 43 | /** |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 44 | * Get file content. Extended version |
| 45 | * |
| 46 | * @param target Compound target (should target only one host) |
| 47 | * @param file File path to read |
| 48 | * @return The content of the file |
| 49 | */ |
| 50 | def getFileContent(master, target, file) { |
| 51 | def salt = new com.mirantis.mk.Salt() |
| 52 | def _result = null |
| 53 | def file_content = null |
| 54 | def result = salt.cmdRun(master, target, "if [ \$(wc -c <${file}) -gt 1048575 ]; then echo 1; fi", false, null, false) |
| 55 | def large_file = result['return'][0].values()[0] |
| 56 | if ( large_file ) { |
| 57 | salt.cmdRun(master, target, "split -b 1MB -d ${file} ${file}__", false, null, false) |
| 58 | def list_files = salt.cmdRun(master, target, "ls ${file}__*", false, null, false) |
| 59 | for ( item in list_files['return'][0].values()[0].tokenize() ) { |
| 60 | _result = salt.cmdRun(master, target, "cat ${item}", false, null, false) |
| 61 | file_content = file_content + _result['return'][0].values()[0].replaceAll('Salt command execution success','') |
| 62 | } |
| 63 | salt.cmdRun(master, target, "rm ${file}__*", false, null, false) |
| 64 | return file_content |
| 65 | } else { |
| 66 | _result = salt.cmdRun(master, target, "cat ${file}", false, null, false) |
| 67 | return _result['return'][0].values()[0].replaceAll('Salt command execution success','') |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get reclass value |
| 73 | * |
| 74 | * @param target The host for which the values will be provided |
| 75 | * @param filter Parameters divided by dots |
| 76 | * @return The pillar data |
| 77 | */ |
| 78 | def getReclassValue(master, target, filter) { |
| 79 | def common = new com.mirantis.mk.Common() |
| 80 | def salt = new com.mirantis.mk.Salt() |
| 81 | def items = filter.tokenize('.') |
| 82 | def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -p ${target} | " + |
| 83 | "python -c 'import json,sys; print(json.dumps(json.loads(sys.stdin.read()).get(\"${items[0]}\")))'", false, null, false) |
| 84 | _result = common.parseJSON(_result['return'][0].values()[0]) |
| 85 | for ( item in items.tail()) { |
| 86 | if ( _result ) { |
| 87 | _result = _result["${item}"] |
| 88 | } |
| 89 | } |
| 90 | return _result |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Create list of nodes in JSON format. |
| 95 | * |
| 96 | * @param filter The Salt's matcher |
| 97 | * @return JSON list of nodes |
| 98 | */ |
| 99 | def getNodeList(master, filter = null) { |
| 100 | def salt = new com.mirantis.mk.Salt() |
| 101 | def common = new com.mirantis.mk.Common() |
| 102 | def builder = new groovy.json.JsonBuilder() |
| 103 | def nodes = [] |
| 104 | def n_counter = 0 |
| 105 | def filtered_list = null |
| 106 | def controllers = salt.getMinions(master, 'I@nova:controller') |
| 107 | def hw_nodes = salt.getMinions(master, 'G@virtual:physical') |
| 108 | def json = builder (ip: '', roles: '', id: '', network_data: [ builder (name: 'management', ip: '')]) |
| 109 | if ( filter ) { |
| 110 | filtered_list = salt.getMinions(master, filter) |
| 111 | filtered_list.addAll(controllers) |
| 112 | } |
| 113 | def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -t", false, null, false) |
| 114 | def reclass_top = common.parseJSON(_result['return'][0].values()[0]) |
| 115 | for (item in reclass_top.base) { |
| 116 | if ( filtered_list ) { |
| 117 | if ( ! filtered_list.contains(item.getKey()) ) { |
| 118 | continue |
| 119 | } |
| 120 | } |
| 121 | n_counter += 1 |
| 122 | json.id = n_counter.toString() |
| 123 | json.ip = getReclassValue(master, item.getKey(), '_param.linux_single_interface.address') |
| 124 | json.network_data[0].ip = json.ip |
| 125 | json.roles = item.getKey().tokenize('.')[0] |
| 126 | if ( controllers.contains(item.getKey()) ) { |
| 127 | json.roles = "${json.roles}, controller" |
| 128 | } |
| 129 | if ( hw_nodes.contains(item.getKey()) ) { |
| 130 | json.roles = "${json.roles}, hw_node" |
| 131 | } |
| 132 | def node = builder.toPrettyString().replace('"', '\\"') |
| 133 | nodes.add(node) |
| 134 | } |
| 135 | return nodes |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 140 | * Execute tempest tests |
| 141 | * |
| 142 | * @param target Host to run tests |
| 143 | * @param pattern If not false, will run only tests matched the pattern |
| 144 | * @param output_dir Directory for results |
| 145 | */ |
| 146 | def runTempestTests(master, target, output_dir, pattern = "false") { |
| 147 | def salt = new com.mirantis.mk.Salt() |
| 148 | def output_file = 'docker-tempest.log' |
| 149 | if (pattern == "false") { |
| 150 | salt.cmdRun(master, target, "docker exec qa_tools rally verify start --pattern set=full " + |
| 151 | "--detailed > ${output_file}") |
| 152 | } |
| 153 | else { |
| 154 | salt.cmdRun(master, target, "docker exec qa_tools rally verify start --pattern ${pattern} " + |
| 155 | "--detailed > ${output_file}") |
| 156 | } |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 157 | def file_content = getFileContent(master, target, output_file) |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 158 | writeFile file: "${output_dir}${output_file}", text: file_content |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Execute rally tests |
| 163 | * |
| 164 | * @param target Host to run tests |
| 165 | * @param pattern If not false, will run only tests matched the pattern |
| 166 | * @param output_dir Directory for results |
| 167 | */ |
| 168 | def runRallyTests(master, target, output_dir, pattern = "false") { |
| 169 | def salt = new com.mirantis.mk.Salt() |
| 170 | def output_file = 'docker-rally.log' |
| 171 | salt.cmdRun(master, target, "docker exec qa_tools rally task start combined_scenario.yaml --task-args-file " + |
| 172 | "/opt/devops-qa-tools/rally-scenarios/task_arguments.yaml | tee ${output_file}") |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 173 | def file_content = getFileContent(master, target, output_file) |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 174 | writeFile file: "${output_dir}${output_file}", text: file_content |
| 175 | } |
| 176 | |
| 177 | /** |
Dmitrii Kabanov | d5f1c5f | 2017-08-30 14:51:41 -0700 | [diff] [blame] | 178 | * Execute SPT tests |
| 179 | * |
| 180 | * @param target Host to run tests |
| 181 | * @param output_dir Directory for results |
| 182 | */ |
| 183 | def runSptTests(master, target, output_dir) { |
| 184 | def salt = new com.mirantis.mk.Salt() |
| 185 | def output_file = 'docker-spt.log' |
| 186 | def report_file = 'report-spt.txt' |
| 187 | def report_file_hw = 'report-spt-hw.txt' |
| 188 | def archive_file = 'results-spt.tar.gz' |
| 189 | salt.cmdRun(master, target, "docker exec qa_tools sudo timmy -c simplified-performance-testing/config.yaml " + |
| 190 | "--nodes-json nodes.json --log-file ${output_file}") |
| 191 | salt.cmdRun(master, target, "docker exec qa_tools ./simplified-performance-testing/SPT_parser.sh > ${report_file}") |
| 192 | salt.cmdRun(master, target, "docker exec qa_tools custom_spt_parser.sh > ${report_file_hw}") |
| 193 | salt.cmdRun(master, target, "docker cp qa_tools:/home/rally/${output_file} ${output_file}") |
| 194 | salt.cmdRun(master, target, "docker cp qa_tools:/tmp/timmy/archives/general.tar.gz ${archive_file}") |
| 195 | def file_content = getFileContent(master, target, output_file) |
| 196 | writeFile file: "${output_dir}${output_file}", text: file_content |
| 197 | file_content = getFileContent(master, target, report_file) |
| 198 | writeFile file: "${output_dir}${report_file}", text: file_content |
| 199 | file_content = getFileContent(master, target, report_file_hw) |
| 200 | writeFile file: "${output_dir}${report_file_hw}", text: file_content |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 205 | * Cleanup |
| 206 | * |
| 207 | * @param target Host to run commands |
| 208 | * @param output_dir Directory for results |
| 209 | */ |
| 210 | def runCleanup(master, target, output_dir) { |
| 211 | def salt = new com.mirantis.mk.Salt() |
Dmitrii Kabanov | 321405a | 2017-08-16 16:38:51 -0700 | [diff] [blame] | 212 | if ( salt.cmdRun(master, target, "docker ps -f name=qa_tools -q", false, null, false)['return'][0].values()[0] ) { |
| 213 | salt.cmdRun(master, target, "docker rm -f qa_tools") |
| 214 | } |
Petr Lomakin | 47fee0a | 2017-08-01 10:46:05 -0700 | [diff] [blame] | 215 | sh "rm -r ${output_dir}" |
| 216 | } |
| 217 | |
| 218 | /** Install docker if needed |
| 219 | * |
| 220 | * @param target Target node to install docker pkg |
| 221 | */ |
| 222 | def installDocker(master, target) { |
| 223 | def salt = new com.mirantis.mk.Salt() |
| 224 | if ( ! salt.runSaltProcessStep(master, target, 'pkg.version', ["docker-engine"]) ) { |
| 225 | salt.runSaltProcessStep(master, target, 'pkg.install', ["docker.io"]) |
| 226 | } |
| 227 | } |