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