-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestrail_upload.py
executable file
·1192 lines (962 loc) · 34.5 KB
/
testrail_upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
"""
Testrail for
"""
from argparse import ArgumentParser, ArgumentError, RawDescriptionHelpFormatter
from collections import defaultdict, namedtuple
import fnmatch
import getpass
import json
import os
import re
import socket
import subprocess
import sys
import tempfile
import textwrap
import time
from xml.etree.ElementTree import parse
from testrail_utils import (
LOG_FILE,
URL_ARTIFACTS,
URL_ARTIFACTS_PUBLIC,
URL_ARTIFACTS_OLD,
add_plan,
add_plan_entry,
add_testcase,
close_plan,
close_plans,
log,
get_cases,
get_entries_id,
get_suite,
get_section,
get_open_plan,
get_plan,
get_run,
get_sections,
get_tests,
put_results,
update_plan_entry,
)
OS = ('Centos6', 'Centos7', 'Trusty')
# Handle test cases generated with random names
# In testrail, one test case ID == one test case name
RANDOM_TEST_NAMES = {
'rs2':
[
'test.test_simpleflow.test_simpleflow[make bucket',
'test.test_simpleflow.test_simpleflow[list bucket',
'test.test_simpleflow.test_simpleflow[put file',
'test.test_simpleflow.test_simpleflow[get file',
'test.test_simpleflow.test_simpleflow[del file',
'test.test_simpleflow.test_simpleflow[delete bucket',
],
'bizstorenode':
[
'test.test_bizstorenode.Test_NODE.test_NODE_PUT[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_READ[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_INFO[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_INFO_REPLICAS[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_READ_REPLICAS[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_DELETE[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_REWRITE[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_FUZZ[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_RESYNC[nodes-n1(',
'test.test_bizstorenode.Test_NODE.test_NODE_PUT[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_INFO[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_INFO_REPLICAS[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_READ[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_READ_REPLICAS[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_DELETE[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_REWRITE[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_FUZZ[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_RESYNC[nodes-n2(',
'test.test_bizstorenode.Test_NODE.test_NODE_PUT[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_READ[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_INFO[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_INFO_REPLICAS[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_READ_REPLICAS[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_DELETE[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_REWRITE[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_FUZZ[nodes-n3(',
'test.test_bizstorenode.Test_NODE.test_NODE_RESYNC[nodes-n3(',
],
}
STATUS_ID = {
"passed": 1,
"failed": 5,
"skipped": 6,
"known_failed_ok": 7,
"known_failed": 10,
"flaky_passed": 11,
"flaky_failed": 12
}
report_obj = namedtuple('report', ['path', 'section', 'distrib'])
def parse_report(report_path):
"""
:param report_path: path to junit report
:return: list of test cases name (string)
"""
report = parse(report_path)
testcases = ['.'.join([tcase.get('classname', ''),
tcase.get('name', '')])
for tcase in report.findall('.//testcase')]
testcases = [t for t in testcases if t != '.']
return testcases
def modify_testname(test_name, section):
"""
Handle specific test names
:param test_name: string
:param section: test suite section
:type section: string
:return: string
"""
# remove random RING key
test_name = re.sub('[0-F]{40}', 'KEY', test_name)
# remove too long ring name (sprov)
test_name = re.sub('[A]{32}', 'A*32', test_name)
# remove IP address (bizstorenode
test_name = re.sub(
r'\([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.*\)', '', test_name
)
# remove IP address (geos)
test_name = re.sub(
r'[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.*\]', '', test_name
)
# remove date (supervisor)
test_name = re.sub('20[0-9]{2}.[0-9]{1,2}.[0-9]{1,2}', '', test_name)
# Remove distrib in test name (supervisor hack)
for distrib in OS:
if distrib.lower() in test_name.lower():
test_name = test_name.lower()
test_name = re.sub(r'{0}'.format(distrib.lower()), '', test_name)
# Handle random test names if possible
for rand_tests in RANDOM_TEST_NAMES.get(section, []):
if test_name.startswith(rand_tests):
return rand_tests
return test_name
def add_testcases(suite, tests, testrail_cases_name):
"""
Add test case(s) to a test suite
:param suite: testrail test suite (ex: "7.2")
:type suite: string
:param tests: tests to add
:type tests: dictionary, each key is a section and contains a list of tests
:param testrail_cases_name: test cases already in testrail test suite
:type testrail_cases_name: list of string
:return: nb_new_tests
:rtype: integer
"""
start = time.time()
nb_new_tests = 0
for section in tests:
log.info('Section: %s', section)
log.info('Suite: %s', suite)
suite_id = get_suite(suite)
log.info('Suite id: %s', suite_id)
section_id = get_section(suite_id, section)
log.info('Tests to be added: %s', tests)
for test in tests[section]:
test = modify_testname(test, section)
log.info('Adding %s in section %s', test, section)
ret = add_testcase(test, section_id, testrail_cases_name)
if ret == 200:
nb_new_tests += 1
testrail_cases_name.append(test)
else:
log.info('%s test not added', test)
duration = time.time() - start
return nb_new_tests, duration
def add_result(
test_case, tests_db, run, section, version, description, flaky, known_failed
):
"""
:param test_case: test case name found in report
:type test_case: string
:param tests_db: all tests related to current run
:type tests_db: list of strin
:param run: testrail run id
:type: run: integer
:param section: test suite section
:type section: string
:param version: test plan name
:type version: string
:return: result
:rtype: dict
"""
# Building test name
name = test_case.get('name', '')
classname = test_case.get('classname', '')
name = '.'.join([classname, name])
name = modify_testname(name, section)
# Get elapsed time if set
elapsed = test_case.get('time')
# Get test case id
for test in tests_db:
if name == test['title']:
test_id = test['id']
break
else:
log.debug("%s: No test found", name)
return
child = test_case.getchildren()
message = description
# Set test status
if child:
child = child[0]
status = child.tag
if status == 'failure' or status == 'error':
status_id = STATUS_ID["failed"]
elif status == 'skipped':
status_id = STATUS_ID["skipped"]
else:
status_id = STATUS_ID["passed"]
# Get message if exists
attrib = child.attrib.get('message', '').encode('utf-8')
attrib = textwrap.dedent(attrib)
if child.text:
text = child.text.encode('utf-8')
else:
text = "No trace"
message += (
"***\n# Error message\n{0}\n***\n# Traceback\n{1}\n***\n".format(
attrib, text
)
)
message = textwrap.dedent(message)
else:
status_id = STATUS_ID["passed"]
message += 'OK'
# Handle known failed tests
if name in known_failed:
if status_id == STATUS_ID["failed"]:
status_id = STATUS_ID["known_failed"]
message = "*** Known failed test ***\n%s" % message
elif status_id == STATUS_ID["passed"]:
status_id = STATUS_ID["known_failed_ok"]
message = "*** Known failed test PASSED (!)***\n%s" % message
# Handle flaky tests
elif name in flaky:
if status_id == STATUS_ID["passed"]:
status_id = STATUS_ID["flaky_passed"]
message = "*** flaky test OK ***\n%s" % message
elif status_id == STATUS_ID["failed"]:
status_id = STATUS_ID["flaky_failed"]
message = "*** flaky test FAILED ***\n%s" % message
elapsed = int(float(elapsed))
log.debug(message)
log.debug('name: %s test_id: %s status_id:%s message:%s',
name, test_id, status_id, message)
result = {'test_id': test_id,
'status_id': status_id,
'comment': message,
'version': version
}
if elapsed:
result['elapsed'] = str(elapsed) + 's'
log.debug(result)
return result
def build_results(
report, version, run, section, tests_db, description, flaky, known_failed
):
"""
Given a report get a results dict
:param report: path to the report
:type report: string
:param version: testrail test plan
:type version: string
:param run: corresponding test run
:param section: testrail section
:param tests_db: all test cases related to the test run
:param description: tests description
:param flaky: list of case id referenced as "flaky"
:param known_failed: list of case id referenced as "known_failed"
:return results:
:rtype: list of results
"""
report = parse(report.path)
results_l = []
for tcase in report.findall('.//testcase'):
result = add_result(
tcase, tests_db, run, section, version, description, flaky, known_failed
)
results_l.append(result)
# Remove empty result
results_l = [r for r in results_l if r]
return results_l
def get_reports(version, distribs, url_artifacts=URL_ARTIFACTS):
"""
Get all reports from artifacts url
:param version: example: staging-7.1.0.r17062621.69c5697.post-merge.00034526
:type version: string
:param distribs: list of expected OS
:type distribs: list of strings
:return:
"""
start = time.time()
version = ''.join([url_artifacts, version])
url = os.path.join(version) + '/'
tmp_dir = tempfile.mkdtemp()
log.info(url)
# Download all junit/report.xml in odr artifacts repo
cmd = ('wget --tries=50 -l 10 -q -r -P {0} '
'--progress=dot:mega '
'--accept=*.xml,report.json {1}').format(tmp_dir, url)
log.info(cmd)
out = subprocess.call(cmd.split())
log.info("wget output: %s", str(out))
paths = find("*.xml", tmp_dir)
# Filter on distribs
paths = [path for path in paths if any(d.lower() in path for d in distribs)]
# Trick to get global report
if tmp_dir not in paths:
paths.append(tmp_dir)
log.info("Reports downloaded from %s:\n%s",
url, '\n'.join(paths))
duration = time.time() - start
return out, paths, duration
def get_related_artifacts(version, url_artifacts=URL_ARTIFACTS):
"""
Get related artifacts if exists (for postmerge build essentially)
:param version: example: staging-7.1.0.r17062621.69c5697.post-merge.00034526
:type version: string
:param distribs: list of expected OS
:type distribs: list of strings
:return:
"""
version = ''.join([url_artifacts, version])
url = os.path.join(version) + '/.related_artifacts/'
tmp_dir = tempfile.mkdtemp()
log.info(url)
# Download the related artifacts url
cmd = ('wget --tries=50 -l 3 -q -r -P {0} '
'--progress=dot:mega '
'{1}').format(tmp_dir, url)
log.info(cmd)
out = subprocess.call(cmd.split())
log.info("wget output: %s", out)
# Retrieve related artifacts in index.html directly
related_artifacts_index = find("index.html", tmp_dir)
if related_artifacts_index:
index_file = related_artifacts_index[0]
else:
return
related_artifacts = parse_index_file(index_file)
return related_artifacts
def parse_index_file(path):
"""
Parse index.html to retrieve premerge artifacts
:param path: index.html path
:type path: string
:return premerge_name: premerge artifacts name
:rtype: string
"""
with open(path, 'r') as content:
try:
premerge_name = re.search('href="./(.*)">', content.read()).group(1)
except Exception as exc:
log.info(exc)
return
return premerge_name
def find(pattern, path):
"""
Given a pattern, return all the files that match in the given path
:param pattern:
:param path:
:return:
"""
result = []
for root, _, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
def put_results_from_reports(
version, suite, milestone, reports, distribs, description):
"""
:param version:
:param suite:
:param milestone:
:param reports:
:param distribs:
:param description:
:return:
"""
nb_res = 0
start = time.time()
plan = get_open_plan(version)
suite_id = get_suite(suite)
exclude_section = ['unit', 'robot_framework']
sections = get_sections(suite_id)
log.info(sections)
centos_tests = []
flaky = []
known_failed = []
for section in get_sections(suite_id):
if section.get('name') not in exclude_section:
cases_suite = get_cases(suite, section.get('name'))
cases = [case.get('id') for case in cases_suite]
centos_tests.extend(cases)
# Retrieve flaky tests from DB
flaky_cases = [
case.get('title') for case in cases_suite
if case.get('refs', '') is not None
and "flaky" in case.get('refs')
]
flaky.extend(flaky_cases)
# Retrieve known failed tests from DB
failed_cases = [
case.get('title') for case in cases_suite
if case.get('refs', '') is not None
and "known_failed" in case.get('refs')
]
known_failed.extend(failed_cases)
if not plan:
add_plan(version, milestone, description)
plan = get_plan(version)
add_plan_entry(plan, suite_id, [1, 2, 3], centos_tests)
assert plan, "No plan found linked to test suite {0}".format(
version)
entries_id = get_entries_id(plan)
for entry_id, config in entries_id:
log.info('Update config: %s run (entry_id): %s', config, entry_id)
update_plan_entry(plan, entry_id, description)
# Loop on distribution (one distrib per run)
for distrib in distribs:
log.info(distrib)
run = get_run(plan, distrib)
assert run, "No run found linked to plan {0}".format(plan)
tests_db = get_tests(run)
results = []
# Loop on report related to distrib
for report in reports:
if report.distrib == distrib.lower() and report.section:
results_c = build_results(
report, version, run, report.section, tests_db, description, flaky, known_failed
)
#results.extend(results_c)
log.info('%s: %s results', report, len(results_c))
nb_per_slice = 1000
nb_slices = len(results_c) / nb_per_slice + 1
for idx in range(nb_slices):
nb_res_distrib = put_results(
run, results_c[nb_per_slice * idx: nb_per_slice * (idx + 1)], tests_db
)
nb_res += nb_res_distrib
duration = time.time() - start
return nb_res, duration, plan
def check_test_case(report, testrail_names):
"""
Check tests in report are in testrail test suite
:param report: path report
:type report: string
:param testrail_names: test cases already in testrail test suite
:type testrail_names: list of string
:return missing_tests: missing tests
:rtype: list of string
"""
log.info('check test cases in %s', report.path)
test_cases = parse_report(report.path)
missing_tests = [modify_testname(test, None)
for test in test_cases
if modify_testname(test, None) not in testrail_names]
return missing_tests
def check_test_cases(reports, suite):
"""
Check tests in report are present in testrail
:param reports: list of reports
:param suite: testrail suite
:return
missing_tests: tests that are missing,
testrail_name: existing tests
duration
:rtype: tuple (dict, list, integer)
"""
start = time.time()
missing_tests = defaultdict(list)
log.info('Get cases from suite: %s', suite)
testrail_cases = get_cases(suite)
testrail_names = [modify_testname(test['title'], None)
for test in testrail_cases]
for report in reports:
section = report.section
log.info("report: %s", report)
log.info("section: %s", section)
if section:
log.debug('check cases in %s', report)
missing = check_test_case(report, testrail_names)
if missing:
missing_tests[section].extend(missing)
# Avoid doublon
missing_tests[section] = list(set(missing_tests[section]))
duration = time.time() - start
return missing_tests, testrail_names, duration
def arg_parse():
"""
Parse script arguments
"""
epilog = r"""
You could use this script to:
1. Add results
a. directly from an artifact url
[@8b31e ~]$ python {0} -u -c 7.2 -v {3} -a {2}
b. from local junit report(s)
/!\ section and distribution MUST be in the report path
[@8b31e ~]$ python {0} -u -c 7.1 -v {4} -r {1}
2. Close plans according to a pattern
(Close all 7.4.0.0 test plans)
[@8b31e ~]$ python {0} -k 7.4.0.0-
""".format(sys.argv[0],
'reports/report_zimbra_centos7_710_rc5.xml',
'bitbucket::ring:promoted-7.2.0.0_rc2',
'promoted-7.2.0.0_rc2',
'7.1.0_rc5')
parser = ArgumentParser(epilog=epilog,
formatter_class=RawDescriptionHelpFormatter)
parser.add_argument(
'-u', '--add_results',
help='Add results to a testrail test run',
action='store_true',
required=False)
parser.add_argument(
'-c', '--cases',
help='testrail testsuite, ex: "7.1"',
required=False)
parser.add_argument(
'-v', '--version',
help='RING version, linked to a testrail run, ex:"7.1.0_rc5"',
required=False)
parser.add_argument(
'-a', '--artifacts',
help="""
Url artifacts
Example: staging-7.1.0.r170626213221.69c5697.post-merge.00034526""",
required=False)
parser.add_argument(
'-r', '--reports',
help="Path to junit report",
nargs='*',
required=False)
parser.add_argument(
'-d', '--distrib',
help="""
distribution(s),
Example: centos7 centos6""",
nargs='*',
default=OS,
required=False)
parser.add_argument(
'-l', '--artifacts_location',
help="""
artifacts url,
Example: https://artifacts.devsca.com/builds/bitbucket::ring:promoted-5.1.9/""",
nargs='*',
default='',
required=False)
parser.add_argument(
'-k', '--close_plan',
help="""
close the current plan after upload
""",
action='store_true',
required=False
)
parser.add_argument(
'-p', '--close_pattern_plans',
help="""
close plans with the given pattern,
Example: 7.2.0.0-""",
required=False)
parser.add_argument(
'-m', '--milestone',
help="""
Specify testrail milestone
Example: 7.4""",
required=False)
parser.add_argument(
'-o', '--old_artifacts',
help="""
Use the old artifacts url i.e https://artifacts.devsca.com/builds/
""",
action='store_true',
required=False)
parser.add_argument(
'-f', '--linkfile',
help="""
print resulting plan url to filename
""",
default="",
required=False)
parser.add_argument(
'-e', '--exclude_sections',
help="""
exclude testrail sections,
Example: sprov bizstorenode""",
nargs='*',
default="",
required=False)
parser.add_argument(
'-b', '--base_url',
help="""
Artifacts private url""",
required=False)
args = parser.parse_args()
return parser, args
def found_global_report(g_reports_l):
"""
Found valid global report
:param g_reports_l: potential global reports list
:type g_reports_l: list of paths
:return: global report path
:rtype: string
"""
reports = []
log.debug('found_global_report')
log.debug(g_reports_l)
for report in g_reports_l:
valid = True
report_json = json.load(open(report, 'r'))
for task in report_json:
# Check if 'steps' key is there
try:
steps = task['steps']
log.debug(steps)
except (KeyError, TypeError):
log.info('%s not a valid format for global report', report)
valid = False
break
if valid:
reports.append(report)
return reports
def struc_reports(reports, suite, distribs, exclude_sections):
"""
Build report objects
report_obj namedtuple has 3 fields:
- path
- section
- distrib
:param reports: report paths list
:type reports: list
:param suite: test suite name in testrail, e.g. '7.2'
:type suite: string
:param distribs: os name(s)
:type distribs: list of string
:param exclude_sections: sections to ignore
:type exclude_sections: list of strings
:return: list of report object
:rtype: list of `report_obj`
"""
# Handle directory as report argument
if isinstance(reports, str):
reports = [reports]
dirs = [r for r in reports if os.path.isdir(r)]
log.info(dirs)
# Convert each directory to a list of xml report
global_reports = []
for c_dir in dirs:
reports_xml = find("*.xml", c_dir)
global_reports = find("report.json", c_dir)
log.info(reports_xml)
reports.remove(c_dir)
reports.extend(reports_xml)
global_reports = found_global_report(global_reports)
reports_l = []
# Retrieve section names from testrail test suite
suite_id = get_suite(suite)
sections = get_sections(suite_id)
sections_name = [s.get('name') for s in sections
if s.get('name') not in exclude_sections]
log.info('Sections: %s', sections_name)
for report in reports:
c_section = None
c_distrib = None
for section in sections_name:
if 'undelete' in report:
# fuse or cifs could be in the path
c_section = 'undelete'
elif 'versioning' in report:
# fuse or cifs could be in the path
c_section = 'versioning'
elif 'volprot' in report:
# fuse or cifs could be in the path
c_section = 'volprot'
elif 'robot_framework' in report:
# sfused could be in the path
c_section = 'robot_framework'
elif section in report:
c_section = section
for distrib in distribs:
if distrib.lower() in report:
c_distrib = distrib.lower()
break
else:
# Handle particular cases here
if (c_section == 'robot_framework' or
c_section == 'unit' or c_section == 'ucheck'):
# Distrib is not in the path
c_distrib = 'trusty'
reports_l.append(report_obj(report, c_section, c_distrib))
return list(set(reports_l)), global_reports
def parse_global_report(global_report):
"""
Parse global json report
:param global_report: global report path
:type global_report: string
:return failed_steps: all failed steps
:rtype: dictionary
"""
failed_steps = {}
report_json = json.load(open(global_report, 'r'))
log.info(report_json)
for task in report_json:
steps = task.get('steps')
for step in steps:
if step.get('failed'):
step = step['step_name']
infos = task.get('task_infos')
task = infos['task_name']
distrib = infos['permutation']
if step in ('setup', 'requirements'):
key = '%s_%s_%s' % (task, distrib, step)
failed_steps[key] = {
'os': distrib,
'step': step
}
return failed_steps
def mass_tag_failed(failed_steps, version, suite, exclude_sections, desc):
"""
Mass tag failed steps, 'setup' or 'requirements' for ODR
:param failed_steps: failed steps listing
:type failed_steps: dictionary
:param version: testrail version name
:param suite: testrail test suite
:param exclude_sections: sections to be ignored
:param desc: upload description
"""
plan = get_plan(version)
# Retrieve section names from testrail test suite
suite_id = get_suite(suite)
sections = get_sections(suite_id)
sections_name = [s.get('name') for s in sections
if s.get('name') not in exclude_sections]
log.info('Check environment issues')
# Hack for suite like undelete.cifs or undelete.fuse
tricky_sections = ['undelete', 'volprot', 'versioning']
for task, infos in failed_steps.items():
# Initialize results list for this task
results_l = []
for section in sections_name:
if section in task:
for t_section in tricky_sections:
if t_section in task:
s_task = t_section
break
else:
s_task = section
break
else:
log.info('No valid section found in testrail: %s', task)
continue
log.info('task: %s -> section found: %s', task, s_task)
log.info('suite: %s', suite)
# Get testrail section id
cases = get_cases(suite, s_task)
section_id = get_section(suite_id, s_task)
distrib = infos['os']
step = infos['step']
# Handle setup and requirements failures
if step == 'setup':
status_id = 8
elif step == 'requirements':
status_id = 9
else:
raise Exception(
'Mass tag step not handled, must be "setup" or "requirements"'
)
log.info(step)
log.info(distrib)
# Get all tests related to the current test run
run_id = get_run(plan, distrib)
tests = get_tests(run_id)
# List all test cases related to current section
cases_name = [
case['id'] for case in cases if case['section_id'] == section_id
]
# List all tests related to current section
tests = [test for test in tests if test['case_id'] in cases_name]
# Loop on all tests, build dict result and add to results list
for test in tests:
result = {'test_id': test['id'],
'status_id': status_id,
'comment': '{0}\n{1} failed'.format(desc, step),
'version': version}
results_l.append(result)
# Put all results in one POST for this current task
log.info('Put env issues: %s - %s - %s', s_task, distrib, step)
nb_per_slice = 1000
nb_slices = len(results_l) / nb_per_slice + 1
for idx in range(nb_slices):
put_results(
run_id, results_l[nb_per_slice * idx: nb_per_slice * (idx + 1)], tests
)
def print_log_file(func):
"""