lava: use babeltrace2 for lttng >= 2.14 (master) for testing
[lttng-ci.git] / scripts / system-tests / lava2-submit.py
CommitLineData
21fec189 1#!/usr/bin/python3
878b4840
JR
2# Copyright (C) 2016 - Francis Deslauriers <francis.deslauriers@efficios.com>
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import argparse
878b4840
JR
18import json
19import os
20import random
9356eef7 21import re
878b4840
JR
22import sys
23import time
24import xmlrpc.client
6d3950a9
JR
25from urllib.parse import urljoin
26from urllib.request import urlretrieve
21fec189
JR
27import yaml
28from jinja2 import Environment, FileSystemLoader
878b4840 29
ef84c6ec
JR
30USERNAME = 'lava-jenkins'
31HOSTNAME = 'lava-master-02.internal.efficios.com'
0425e1dd 32OBJSTORE_URL = "https://obj.internal.efficios.com/lava/results/"
878b4840 33
cf1271bb
JR
34def parse_stable_version(stable_version_string):
35 # Get the major and minor version numbers from the lttng version string.
36 version_match = re.search('stable-(\d).(\d\d)', stable_version_string)
37
38 if version_match is not None:
39 major_version = int(version_match.group(1))
40 minor_version = int(version_match.group(2))
41 else:
42 # Setting to zero to make the comparison below easier.
43 major_version = 0
44 minor_version = 0
45 return major_version, minor_version
46
f5f36c68
FD
47
48class TestType:
21fec189 49 """ Enum like for test type """
f5f36c68 50
523e784d
FD
51 baremetal_tests = 1
52 kvm_tests = 2
878b4840 53 values = {
f5f36c68
FD
54 'baremetal-tests': baremetal_tests,
55 'kvm-tests': kvm_tests,
878b4840
JR
56 }
57
f5f36c68
FD
58
59class DeviceType:
21fec189 60 """ Enum like for device type """
f5f36c68 61
4cb5cc4f 62 x86 = 'x86'
f9a184a9 63 kvm = 'qemu'
f5f36c68
FD
64 values = {'kvm': kvm, 'x86': x86}
65
4cb5cc4f 66
878b4840
JR
67def get_job_bundle_content(server, job):
68 try:
69 bundle_sha = server.scheduler.job_status(str(job))['bundle_sha1']
70 bundle = server.dashboard.get(bundle_sha)
21fec189
JR
71 except xmlrpc.client.Fault as error:
72 print('Error while fetching results bundle', error.faultString)
73 raise error
878b4840
JR
74
75 return json.loads(bundle['content'])
76
f5f36c68 77
878b4840 78def check_job_all_test_cases_state_count(server, job):
21fec189
JR
79 """
80 Parse the results bundle to see the run-tests testcase
81 of the lttng-kernel-tests passed successfully
82 """
0425e1dd
JR
83 print("Testcase result:")
84 content = server.results.get_testjob_results_yaml(str(job))
c2f8bcb9 85 testcases = yaml.unsafe_load(content)
878b4840 86
21fec189
JR
87 passed_tests = 0
88 failed_tests = 0
0425e1dd
JR
89 for testcase in testcases:
90 if testcase['result'] != 'pass':
f5f36c68
FD
91 print(
92 "\tFAILED {}\n\t\t See http://{}{}".format(
93 testcase['name'], HOSTNAME, testcase['url']
94 )
95 )
21fec189 96 failed_tests += 1
0425e1dd 97 else:
21fec189 98 passed_tests += 1
878b4840
JR
99 return (passed_tests, failed_tests)
100
f5f36c68 101
878b4840 102def print_test_output(server, job):
21fec189
JR
103 """
104 Parse the attachment of the testcase to fetch the stdout of the test suite
105 """
0425e1dd 106 job_finished, log = server.scheduler.jobs.logs(str(job))
be7f51b6 107 logs = yaml.unsafe_load(log.data.decode('ascii'))
0425e1dd
JR
108 print_line = False
109 for line in logs:
110 if line['lvl'] != 'target':
111 continue
112 if line['msg'] == '<LAVA_SIGNAL_STARTTC run-tests>':
113 print('---- TEST SUITE OUTPUT BEGIN ----')
114 print_line = True
115 continue
116 if line['msg'] == '<LAVA_SIGNAL_ENDTC run-tests>':
117 print('----- TEST SUITE OUTPUT END -----')
d132001f
FD
118 print_line = False
119 continue
0425e1dd
JR
120 if print_line:
121 print("{} {}".format(line['dt'], line['msg']))
878b4840 122
f5f36c68
FD
123
124def get_vlttng_cmd(
9356eef7 125 lttng_version, lttng_tools_url, lttng_tools_commit, lttng_ust_url=None, lttng_ust_commit=None
f5f36c68 126):
21fec189
JR
127 """
128 Return vlttng cmd to be used in the job template for setup.
129 """
0dd62728
JR
130
131 major_version, minor_version = parse_stable_version(lttng_version)
132
f1d85a63
JR
133 urcu_profile = ""
134 if lttng_version == 'master' or (major_version >= 2 and minor_version >= 11):
135 urcu_profile = "urcu-master"
136 else:
137 urcu_profile = "urcu-stable-0.12"
878b4840 138
30989989
JR
139 # Starting with 2.14, babeltrace2 is the reader for testing.
140 if lttng_version == 'master' or (major_version >= 2 and minor_version >= 14):
141 babeltrace_profile = " --profile babeltrace2-stable-2.0 --profile babeltrace2-python"
142 else:
143 babeltrace_profile = " --profile babeltrace-stable-1.5 --profile babeltrace-python"
144
f5f36c68 145 vlttng_cmd = (
f1d85a63
JR
146 'vlttng --jobs=$(nproc) --profile ' + urcu_profile
147 + ' --override projects.babeltrace.build-env.PYTHON=python3'
f5f36c68 148 ' --override projects.babeltrace.build-env.PYTHON_CONFIG=python3-config'
30989989
JR
149 + babeltrace_profile
150 + ' --profile lttng-tools-master'
f5f36c68
FD
151 ' --override projects.lttng-tools.source='
152 + lttng_tools_url
153 + ' --override projects.lttng-tools.checkout='
154 + lttng_tools_commit
155 + ' --profile lttng-tools-no-man-pages'
156 )
878b4840
JR
157
158 if lttng_ust_commit is not None:
f5f36c68
FD
159 vlttng_cmd += (
160 ' --profile lttng-ust-master '
161 ' --override projects.lttng-ust.source='
162 + lttng_ust_url
163 + ' --override projects.lttng-ust.checkout='
164 + lttng_ust_commit
165 + ' --profile lttng-ust-no-man-pages'
166 )
878b4840 167
9356eef7
FD
168
169 if lttng_version == 'master' or (major_version >= 2 and minor_version >= 11):
170 vlttng_cmd += (
171 ' --override projects.lttng-tools.configure+=--enable-test-sdt-uprobe'
172 )
173
888b31de 174 vlttng_path = '/tmp/virtenv'
c11ec858 175
4cb5cc4f 176 vlttng_cmd += ' ' + vlttng_path
878b4840 177
4cb5cc4f 178 return vlttng_cmd
878b4840 179
f5f36c68 180
878b4840 181def main():
559b83b3 182 send_retry_limit = 10
9a49d69d 183 nfsrootfs = "https://obj.internal.efficios.com/lava/rootfs/rootfs_amd64_xenial_2018-12-05.tar.gz"
878b4840
JR
184 test_type = None
185 parser = argparse.ArgumentParser(description='Launch baremetal test using Lava')
186 parser.add_argument('-t', '--type', required=True)
9356eef7 187 parser.add_argument('-lv', '--lttng-version', required=True)
878b4840
JR
188 parser.add_argument('-j', '--jobname', required=True)
189 parser.add_argument('-k', '--kernel', required=True)
878b4840 190 parser.add_argument('-lm', '--lmodule', required=True)
eb5bdbeb 191 parser.add_argument('-tu', '--tools-url', required=True)
878b4840 192 parser.add_argument('-tc', '--tools-commit', required=True)
6b35e57c 193 parser.add_argument('-id', '--build-id', required=True)
eb5bdbeb 194 parser.add_argument('-uu', '--ust-url', required=False)
878b4840 195 parser.add_argument('-uc', '--ust-commit', required=False)
f23dc688 196 parser.add_argument('-d', '--debug', required=False, action='store_true')
878b4840
JR
197 args = parser.parse_args()
198
199 if args.type not in TestType.values:
200 print('argument -t/--type {} unrecognized.'.format(args.type))
201 print('Possible values are:')
202 for k in TestType.values:
203 print('\t {}'.format(k))
204 return -1
878b4840
JR
205
206 lava_api_key = None
f23dc688
JR
207 if not args.debug:
208 try:
ef84c6ec 209 lava_api_key = os.environ['LAVA2_JENKINS_TOKEN']
21fec189 210 except Exception as error:
f5f36c68
FD
211 print(
212 'LAVA2_JENKINS_TOKEN not found in the environment variable. Exiting...',
213 error,
214 )
f23dc688 215 return -1
878b4840 216
4cb5cc4f 217 jinja_loader = FileSystemLoader(os.path.dirname(os.path.realpath(__file__)))
f5f36c68 218 jinja_env = Environment(loader=jinja_loader, trim_blocks=True, lstrip_blocks=True)
4cb5cc4f 219 jinja_template = jinja_env.get_template('template_lava_job.jinja2')
4cb5cc4f
JR
220
221 test_type = TestType.values[args.type]
222
523e784d 223 if test_type is TestType.baremetal_tests:
4cb5cc4f 224 device_type = DeviceType.x86
878b4840 225 else:
4cb5cc4f 226 device_type = DeviceType.kvm
e640b6d8
JR
227
228 vlttng_path = '/tmp/virtenv'
4cb5cc4f 229
f5f36c68 230 vlttng_cmd = get_vlttng_cmd(
9356eef7 231 args.lttng_version, args.tools_url, args.tools_commit, args.ust_url, args.ust_commit
f5f36c68 232 )
4cb5cc4f 233
cf1271bb
JR
234 if args.lttng_version == "master":
235 lttng_version_string = "master"
7ce9e417
JR
236 elif args.lttng_version == "canary":
237 lttng_version_string = "2.10"
cf1271bb
JR
238 else:
239 major, minor = parse_stable_version(args.lttng_version)
240 lttng_version_string = str(major) + "." + str(minor)
241
242
4cb5cc4f
JR
243 context = dict()
244 context['DeviceType'] = DeviceType
245 context['TestType'] = TestType
246
247 context['job_name'] = args.jobname
248 context['test_type'] = test_type
4cb5cc4f
JR
249 context['random_seed'] = random.randint(0, 1000000)
250 context['device_type'] = device_type
251
252 context['vlttng_cmd'] = vlttng_cmd
253 context['vlttng_path'] = vlttng_path
cf1271bb 254 context['lttng_version_string'] = lttng_version_string
4cb5cc4f
JR
255
256 context['kernel_url'] = args.kernel
257 context['nfsrootfs_url'] = nfsrootfs
258 context['lttng_modules_url'] = args.lmodule
6b35e57c 259 context['jenkins_build_id'] = args.build_id
4cb5cc4f
JR
260
261 context['kprobe_round_nb'] = 10
262
ef84c6ec
JR
263 render = jinja_template.render(context)
264
ef84c6ec
JR
265 print('Job to be submitted:')
266
267 print(render)
878b4840 268
f23dc688 269 if args.debug:
f23dc688
JR
270 return 0
271
f5f36c68
FD
272 server = xmlrpc.client.ServerProxy(
273 'http://%s:%s@%s/RPC2' % (USERNAME, lava_api_key, HOSTNAME)
274 )
878b4840 275
559b83b3 276 for attempt in range(1, send_retry_limit + 1):
21fec189
JR
277 try:
278 jobid = server.scheduler.submit_job(render)
279 except xmlrpc.client.ProtocolError as error:
f5f36c68
FD
280 print(
281 'Protocol error on submit, sleeping and retrying. Attempt #{}'.format(
282 attempt
283 )
284 )
21fec189
JR
285 time.sleep(5)
286 continue
287 else:
288 break
559b83b3
JR
289 # Early exit when the maximum number of retry is reached.
290 if attempt == send_retry_limit:
291 print(
292 'Protocol error on submit, maximum number of retry reached ({})'.format(
293 attempt
294 )
295 )
296 return -1
878b4840
JR
297
298 print('Lava jobid:{}'.format(jobid))
f5f36c68
FD
299 print(
300 'Lava job URL: http://lava-master-02.internal.efficios.com/scheduler/job/{}'.format(
301 jobid
302 )
303 )
878b4840 304
f5f36c68 305 # Check the status of the job every 30 seconds
0425e1dd
JR
306 jobstatus = server.scheduler.job_state(jobid)['job_state']
307 running = False
21fec189 308 while jobstatus in ['Submitted', 'Scheduling', 'Scheduled', 'Running']:
0425e1dd 309 if not running and jobstatus == 'Running':
878b4840 310 print('Job started running')
0425e1dd 311 running = True
878b4840 312 time.sleep(30)
26cbe60b
JR
313 try:
314 jobstatus = server.scheduler.job_state(jobid)['job_state']
21fec189
JR
315 except xmlrpc.client.ProtocolError as error:
316 print('Protocol error, retrying')
317 continue
878b4840 318 print('Job ended with {} status.'.format(jobstatus))
0425e1dd
JR
319
320 if jobstatus != 'Finished':
878b4840 321 return -1
878b4840 322
0425e1dd
JR
323 if test_type is TestType.kvm_tests or test_type is TestType.baremetal_tests:
324 print_test_output(server, jobid)
0425e1dd 325
21fec189 326 passed, failed = check_job_all_test_cases_state_count(server, jobid)
0425e1dd
JR
327 print('With {} passed and {} failed Lava test cases.'.format(passed, failed))
328
21fec189 329 if failed != 0:
0425e1dd 330 return -1
878b4840 331
21fec189
JR
332 return 0
333
f5f36c68 334
878b4840
JR
335if __name__ == "__main__":
336 sys.exit(main())
This page took 0.042187 seconds and 4 git commands to generate.