-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarize_test_results.py
1155 lines (976 loc) · 39 KB
/
summarize_test_results.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
#
# Copyright The CloudNativePG Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Creates a summary of all the "strategy matrix" branches in GH actions that
are running the E2E Test Suite. Produces a Markdown file which can be fed into
GitHub for the Job Summary, or viewed on its own in file or standard-out.
Each test execution in each "matrix branch" in GH is uploading a JSON artifact
with the test results.
The test artifacts are normalized to avoid some of ginkgo's idiosyncrasies.
This is the JSON format of the test artifacts:
{
"name": "my test",
"state": "passed",
"start_time": "timestamp",
"end_time": "timestamp",
"error": error,
"error_file": errFile,
"error_line": errLine,
"platform": e.g. local / gke / aks…,
"postgres_kind": postgresql / epas,
"matrix_id": GH actions "matrix branch" id,
"postgres_version": semver,
"k8s_version": semver,
"workflow_id": GH actions workflow id,
"repo": git repo,
"branch": git branch,
}
In a final GH action, after all the matrix branches running the E2E Test Suite
are finished, all the artifacts are downloaded to a local directory.
The code in this file iterates over all the collected JSON artifacts to produce
a summary in Markdown, which can then be rendered in GitHub using
[GitHub Job Summaries](https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/)
"""
import argparse
from datetime import datetime
import json
import math
import os
import pathlib
from prettytable import MARKDOWN
from prettytable import PrettyTable
def is_failed(e2e_test):
"""checks if the test failed. In ginkgo, the passing states are
well-defined but ginkgo 1 -> 2 added new failure kinds. So, check
for non-pass
"""
return (
e2e_test["state"] != "passed"
and e2e_test["state"] != "skipped"
and e2e_test["state"] != "ignoreFailed"
)
def is_external_failure(e2e_test):
"""checks if the test failed for an external reason. E.g. because the
whole suite timed out, or the user canceled execution.
For example, in ginkgo, the status "interrupted" is used when the suite
times out
"""
return is_failed(e2e_test) and e2e_test["state"] != "failed"
def is_special_error(e2e_test):
"""checks if the test failed due to one of a set of well-known errors
that may be out-of band and warrant special display in the Test Summary
"""
special_failures = {
"operator was restarted": True,
"operator was renamed": True,
}
return "error" in e2e_test and e2e_test["error"] in special_failures
def is_ginkgo_report_failure(e2e_test):
"""checks if the ginkgo report could not be read"""
special_tests = {
"Open Ginkgo report": True,
}
return "error" in e2e_test and e2e_test["name"] in special_tests
def is_normal_failure(e2e_test):
"""checks the test failed and was not a special kind of failure"""
return (
is_failed(e2e_test)
and not is_special_error(e2e_test)
and not is_external_failure(e2e_test)
and not is_ginkgo_report_failure(e2e_test)
)
def is_test_artifact(test_entry):
should_have = [
"name",
"state",
"start_time",
"end_time",
"error",
"error_file",
"error_line",
"platform",
"postgres_kind",
"matrix_id",
"postgres_version",
"k8s_version",
"workflow_id",
"repo",
"branch",
]
for field in should_have:
if field not in test_entry:
return False
return True
def compress_kubernetes_version(test_entry):
"""ensure the k8s_version field contains only the minor release
of kubernetes, and that the presence or absence of an initial "v" is ignored.
Otherwise, ciclops can over-represent failure percentages and k8s releases tested
"""
k8s = test_entry["k8s_version"]
if k8s[0] == "v":
k8s = k8s[1:]
frags = k8s.split(".")
if len(frags) <= 2:
test_entry["k8s_version"] = k8s
return test_entry
else:
minor = ".".join(frags[0:2])
test_entry["k8s_version"] = minor
return test_entry
def combine_postgres_data(test_entry):
"""combines Postgres kind and version of the test artifact to
a single field called `pg_version`
"""
pgkind = test_entry["postgres_kind"]
pgversion = test_entry["postgres_version"]
test_entry["pg_version"] = f"{pgkind}-{pgversion}"
return test_entry
def track_time_taken(test_results, test_times, suite_times):
"""computes the running shortest and longest duration of
running each kind of test
"""
name = test_results["name"]
# tag abnormal failures, e.g.: "[operator was restarted] Imports with …"
if is_external_failure(test_results):
tag = test_results["state"]
name = f"[{tag}] {name}"
elif is_special_error(test_results):
tag = test_results["error"]
name = f"[{tag}] {name}"
if (
# ignore nullish datetime
test_results["start_time"] == "0001-01-01T00:00:00Z"
or test_results["end_time"] == "0001-01-01T00:00:00Z"
):
return
# chop off the nanoseconds part, which is too much for
# Python `fromisoformat`
start_frags = test_results["start_time"].split(".")
if len(start_frags) != 2:
return
end_frags = test_results["end_time"].split(".")
if len(end_frags) != 2:
return
# track individual test durations. Store min-max, slowest branch
start_time = datetime.fromisoformat(start_frags[0])
end_time = datetime.fromisoformat(end_frags[0])
duration = end_time - start_time
matrix_id = test_results["matrix_id"]
if name not in test_times["max"]:
test_times["max"][name] = duration
if name not in test_times["min"]:
test_times["min"][name] = duration
if name not in test_times["slowest_branch"]:
test_times["slowest_branch"][name] = matrix_id
if duration > test_times["max"][name]:
test_times["max"][name] = duration
test_times["slowest_branch"][name] = matrix_id
if duration < test_times["min"][name]:
test_times["min"][name] = duration
# Track test suite timings.
# For each platform-matrix branch, track the earliest start and the latest end
platform = test_results["platform"]
if platform not in suite_times["start_time"]:
suite_times["start_time"][platform] = {}
if matrix_id not in suite_times["start_time"][platform]:
suite_times["start_time"][platform][matrix_id] = start_time
if platform not in suite_times["end_time"]:
suite_times["end_time"][platform] = {}
if matrix_id not in suite_times["end_time"][platform]:
suite_times["end_time"][platform][matrix_id] = end_time
if start_time < suite_times["start_time"][platform][matrix_id]:
suite_times["start_time"][platform][matrix_id] = start_time
if suite_times["end_time"][platform][matrix_id] < end_time:
suite_times["end_time"][platform][matrix_id] = end_time
def count_bucketed_by_test(test_results, by_test):
"""counts the successes, failures, failing versions of kubernetes,
failing versions of postgres, bucketed by test name.
"""
name = test_results["name"]
if name not in by_test["total"]:
by_test["total"][name] = 0
by_test["total"][name] = 1 + by_test["total"][name]
if is_failed(test_results) and not is_ginkgo_report_failure(test_results):
if name not in by_test["failed"]:
by_test["failed"][name] = 0
if name not in by_test["k8s_versions_failed"]:
by_test["k8s_versions_failed"][name] = {}
if name not in by_test["pg_versions_failed"]:
by_test["pg_versions_failed"][name] = {}
if name not in by_test["platforms_failed"]:
by_test["platforms_failed"][name] = {}
by_test["failed"][name] = 1 + by_test["failed"][name]
k8s_version = test_results["k8s_version"]
pg_version = test_results["pg_version"]
platform = test_results["platform"]
by_test["k8s_versions_failed"][name][k8s_version] = True
by_test["pg_versions_failed"][name][pg_version] = True
by_test["platforms_failed"][name][platform] = True
def count_bucketed_by_code(test_results, by_failing_code):
"""buckets by failed code, with a list of tests where the assertion fails,
and a view of the stack trace.
"""
name = test_results["name"]
if test_results["error"] == "" or test_results["state"] == "ignoreFailed":
return
# it does not make sense to show failing code that is outside the test,
# so we skip special failures
if not is_normal_failure(test_results):
return
errfile = test_results["error_file"]
errline = test_results["error_line"]
err_desc = f"{errfile}:{errline}"
if err_desc not in by_failing_code["total"]:
by_failing_code["total"][err_desc] = 0
by_failing_code["total"][err_desc] = 1 + by_failing_code["total"][err_desc]
if err_desc not in by_failing_code["tests"]:
by_failing_code["tests"][err_desc] = {}
by_failing_code["tests"][err_desc][name] = True
if err_desc not in by_failing_code["errors"]:
by_failing_code["errors"][err_desc] = test_results["error"]
def count_bucketed_by_special_failures(test_results, by_special_failures):
"""counts the successes, failures, failing versions of kubernetes,
failing versions of postgres, bucketed by test name.
"""
if not is_failed(test_results) or is_normal_failure(test_results):
return
failure = ""
if is_external_failure(test_results):
failure = test_results["state"]
if is_special_error(test_results) or is_ginkgo_report_failure(test_results):
failure = test_results["error"]
test_name = test_results["name"]
k8s_version = test_results["k8s_version"]
pg_version = test_results["pg_version"]
platform = test_results["platform"]
if failure not in by_special_failures["total"]:
by_special_failures["total"][failure] = 0
for key in [
"tests_failed",
"k8s_versions_failed",
"pg_versions_failed",
"platforms_failed",
]:
if failure not in by_special_failures[key]:
by_special_failures[key][failure] = {}
by_special_failures["total"][failure] += 1
by_special_failures["tests_failed"][failure][test_name] = True
by_special_failures["k8s_versions_failed"][failure][k8s_version] = True
by_special_failures["pg_versions_failed"][failure][pg_version] = True
by_special_failures["platforms_failed"][failure][platform] = True
def count_bucketized_stats(test_results, buckets, field_id):
"""counts the success/failures onto a bucket. This means there are two
dictionaries: one for `total` tests, one for `failed` tests.
"""
bucket_id = test_results[field_id]
if bucket_id not in buckets["total"]:
buckets["total"][bucket_id] = 0
buckets["total"][bucket_id] = 1 + buckets["total"][bucket_id]
if is_failed(test_results):
if bucket_id not in buckets["failed"]:
buckets["failed"][bucket_id] = 0
buckets["failed"][bucket_id] = 1 + buckets["failed"][bucket_id]
def compute_bucketized_summary(parameter_buckets):
"""counts the number of buckets with failures and the
total number of buckets
returns (num-failed-buckets, num-total-buckets)
"""
failed_buckets_count = 0
total_buckets_count = 0
for _ in parameter_buckets["total"]:
total_buckets_count = 1 + total_buckets_count
for _ in parameter_buckets["failed"]:
failed_buckets_count = 1 + failed_buckets_count
return failed_buckets_count, total_buckets_count
def compute_test_summary(test_dir):
"""iterate over the JSON artifact files in `test_dir`, and
bucket them for comprehension.
Returns a dictionary of dictionaries:
{
"total_run": 0,
"total_failed": 0,
"total_special_fails": 0,
"by_test": { … },
"by_code": { … },
"by_special_failures": { … },
"by_matrix": { … },
"by_k8s": { … },
"by_platform": { … },
"by_postgres": { … },
"test_durations": { … },
"suite_durations": { … },
}
"""
# initialize data structures
############################
total_runs = 0
total_fails = 0
total_special_fails = 0
by_test = {
"total": {},
"failed": {},
"k8s_versions_failed": {},
"pg_versions_failed": {},
"platforms_failed": {},
}
by_failing_code = {
"total": {},
"tests": {},
"errors": {},
}
by_matrix = {"total": {}, "failed": {}}
by_k8s = {"total": {}, "failed": {}}
by_postgres = {"total": {}, "failed": {}}
by_platform = {"total": {}, "failed": {}}
# special failures are not due to the test having failed, but
# to something at a higher level, like the E2E suite having been
# cancelled, timed out, or having executed improperly
by_special_failures = {
"total": {},
"tests_failed": {},
"k8s_versions_failed": {},
"pg_versions_failed": {},
"platforms_failed": {},
}
test_durations = {"max": {}, "min": {}, "slowest_branch": {}}
suite_durations = {"start_time": {}, "end_time": {}}
# start computation of summary
##############################
dir_listing = os.listdir(test_dir)
for file in dir_listing:
if pathlib.Path(file).suffix != ".json":
continue
path = os.path.join(test_dir, file)
with open(path, encoding="utf-8") as json_file:
parsed = json.load(json_file)
if not is_test_artifact(parsed):
# skipping non-artifacts
continue
test_results = combine_postgres_data(parsed)
test_results = compress_kubernetes_version(test_results)
total_runs = 1 + total_runs
if is_failed(test_results):
total_fails = 1 + total_fails
if not is_normal_failure(test_results):
total_special_fails = 1 + total_special_fails
# bucketing by test name
count_bucketed_by_test(test_results, by_test)
# bucketing by failing code
count_bucketed_by_code(test_results, by_failing_code)
# special failures are treated separately
count_bucketed_by_special_failures(test_results, by_special_failures)
# bucketing by matrix ID
count_bucketized_stats(test_results, by_matrix, "matrix_id")
# bucketing by k8s version
count_bucketized_stats(test_results, by_k8s, "k8s_version")
# bucketing by postgres version
count_bucketized_stats(test_results, by_postgres, "pg_version")
# bucketing by platform
count_bucketized_stats(test_results, by_platform, "platform")
track_time_taken(test_results, test_durations, suite_durations)
return {
"total_run": total_runs,
"total_failed": total_fails,
"total_special_fails": total_special_fails,
"by_test": by_test,
"by_code": by_failing_code,
"by_special_failures": by_special_failures,
"by_matrix": by_matrix,
"by_k8s": by_k8s,
"by_platform": by_platform,
"by_postgres": by_postgres,
"test_durations": test_durations,
"suite_durations": suite_durations,
}
def compile_overview(summary):
"""computes the failed vs total count for different buckets"""
unique_failed, unique_run = compute_bucketized_summary(summary["by_test"])
k8s_failed, k8s_run = compute_bucketized_summary(summary["by_k8s"])
postgres_failed, postgres_run = compute_bucketized_summary(summary["by_postgres"])
matrix_failed, matrix_run = compute_bucketized_summary(summary["by_matrix"])
platform_failed, platform_run = compute_bucketized_summary(summary["by_platform"])
return {
"total_run": summary["total_run"],
"total_failed": summary["total_failed"],
"total_special_fails": summary["total_special_fails"],
"unique_run": unique_run,
"unique_failed": unique_failed,
"k8s_run": k8s_run,
"k8s_failed": k8s_failed,
"postgres_run": postgres_run,
"postgres_failed": postgres_failed,
"matrix_failed": matrix_failed,
"matrix_run": matrix_run,
"platform_failed": platform_failed,
"platform_run": platform_run,
}
def metric_name(metric):
metric_type = {
"by_test": "Tests",
"by_k8s": "Kubernetes versions",
"by_postgres": "Postgres versions",
"by_platform": "Platforms",
}
return metric_type[metric]
def compute_systematic_failures_on_metric(summary, metric, embed=True):
"""tests if there are items within the metric that have systematic failures.
For example, in the "by_test" metric, if there is a test with systematic failures.
Returns a boolean to indicate there are systematic failures, and an output string
with the failures.
The `embed` argument controls the output. If True (default) it computes the full list
of alerts for the metric. If False, it will cap at 2 rows with alerts, so as not to
flood the ChatOps client.
"""
output = ""
has_systematic_failure_in_metric = False
counter = 0
for bucket_hits in summary[metric]["failed"].items():
bucket = bucket_hits[0] # the items() call returns (bucket, hits) pairs
failures = summary[metric]["failed"][bucket]
runs = summary[metric]["total"][bucket]
if failures == runs and failures > 1:
if not has_systematic_failure_in_metric:
output += f"{metric_name(metric)} with systematic failures:\n\n"
has_systematic_failure_in_metric = True
if counter >= 2 and not embed:
output += f"- ...and more. See full story in GH Test Summary\n"
break
else:
output += f"- {bucket}: ({failures} out of {runs} tests failed)\n"
counter += 1
if has_systematic_failure_in_metric:
# add a newline after at the end of the list of failures before starting the
# next metric
output += f"\n"
return True, output
return False, ""
def format_alerts(summary, embed=True, file_out=None):
"""print Alerts for tests that have failed systematically
If the `embed` argument is true, it will produce a fragment of Markdown
to be included with the action summary.
Otherwise, it will be output as plain text intended for stand-alone use.
We want to capture:
- all test combinations failed (if this happens, no more investigation needed)
- a test failed systematically (all combinations of this test failed)
- a PG version failed systematically (all tests in that PG version failed)
- a K8s version failed systematically (all tests in that K8s version failed)
- a Platform failed systematically (all tests in that Platform failed)
We require more than 1 "failure" to avoid flooding
"""
has_systematic_failures = False
if summary["total_run"] == summary["total_failed"]:
if embed:
print(f"## Alerts\n", file=file_out)
print(f"All test combinations failed\n", file=file_out)
else:
print("alerts<<EOF", file=file_out)
print(f"All test combinations failed\n", file=file_out)
print("EOF", file=file_out)
return
output = ""
for metric in ["by_test", "by_k8s", "by_postgres", "by_platform"]:
has_alerts, out = compute_systematic_failures_on_metric(summary, metric, embed)
if has_alerts:
has_systematic_failures = True
output += out
if not has_systematic_failures:
return
if embed:
print(f"## Alerts\n", file=file_out)
print(f"{output}", end="", file=file_out)
else:
print("alerts<<EOF", file=file_out)
print(f"{output}", file=file_out)
print("EOF", file=file_out)
def compute_semaphore(success_percent, embed=True):
"""create a semaphore light summarizing the success percent.
If set to `embed`, an emoji will be used. Else, a textual representation
of a Slack emoji is used.
"""
levels = {
"good": {
"emoji": "🟢",
"text": ":large_green_circle:",
},
"average": {
"emoji": "🟡",
"text": ":large_yellow_circle:",
},
"bad": {
"emoji": "🔴",
"text": ":red_circle:",
},
}
form = "emoji"
if not embed:
form = "text"
if success_percent >= 99:
return levels["good"][form]
elif success_percent >= 95:
return levels["average"][form]
else:
return levels["bad"][form]
def compute_thermometer_on_metric(summary, metric, embed=True):
"""computes a summary per item in the metric, with the success percentage
and a color coding based on said percentage
"""
output = f"{metric_name(metric)} thermometer:\n\n"
for bucket_hits in summary[metric]["total"].items():
bucket = bucket_hits[0] # the items() call returns (bucket, hits) pairs
failures = 0
if bucket in summary[metric]["failed"]:
failures = summary[metric]["failed"][bucket]
runs = summary[metric]["total"][bucket]
success_percent = (1 - failures / runs) * 100
color = compute_semaphore(success_percent, embed)
output += (
f"- {color} - {bucket}: {math.floor(success_percent*10)/10}% success.\t"
)
output += f"({failures} out of {runs} tests failed)\n"
output += f"\n"
return output
def format_thermometer(summary, embed=True, file_out=None):
"""print thermometer with the percentage of success for a set of metrics.
e.g. per-platform and per-kubernetes
If the `embed` argument is true, it will produce a fragment of Markdown
to be included with the action summary.
Otherwise, it will be output as plain text intended for stand-alone use.
"""
output = ""
# we only test the "by_platform" metric for the thermometer, at the moment
for metric in ["by_platform"]:
output += compute_thermometer_on_metric(summary, metric, embed)
if embed:
print(f"## Thermometer\n", file=file_out)
print(f"{output}", end="", file=file_out)
else:
print("thermometer<<EOF", file=file_out)
print(f"{output}", file=file_out)
print("EOF", file=file_out)
def format_overview(summary, structure, file_out=None):
"""print general test metrics"""
print("## " + structure["title"] + "\n", file=file_out)
table = PrettyTable(align="l")
table.field_names = structure["header"]
table.set_style(MARKDOWN)
for row in structure["rows"]:
table.add_row([summary[row[1]], summary[row[2]], row[0]])
print(table, file=file_out)
def format_bucket_table(buckets, structure, file_out=None):
"""print table with bucketed metrics, sorted by decreasing
amount of failures.
The structure argument contains the layout directives. E.g.
{
"title": "Failures by platform",
"header": ["failed tests", "total tests", "platform"],
}
"""
title = structure["title"]
anchor = structure["anchor"]
print(f"\n<h2><a name={anchor}>{title}</a></h2>\n", file=file_out)
table = PrettyTable(align="l")
table.field_names = structure["header"]
table.set_style(MARKDOWN)
sorted_by_fail = dict(
sorted(buckets["failed"].items(), key=lambda item: item[1], reverse=True)
)
for bucket in sorted_by_fail:
table.add_row([buckets["failed"][bucket], buckets["total"][bucket], bucket])
print(table, file=file_out)
def format_by_test(summary, structure, file_out=None):
"""print metrics bucketed by test class"""
title = structure["title"]
anchor = structure["anchor"]
print(f"\n<h2><a name={anchor}>{title}</a></h2>\n", file=file_out)
table = PrettyTable(align="l")
table.field_names = structure["header"]
table.set_style(MARKDOWN)
sorted_by_fail = dict(
sorted(
summary["by_test"]["failed"].items(),
key=lambda item: item[1],
reverse=True,
)
)
for bucket in sorted_by_fail:
failed_k8s = ", ".join(summary["by_test"]["k8s_versions_failed"][bucket].keys())
failed_pg = ", ".join(summary["by_test"]["pg_versions_failed"][bucket].keys())
failed_platforms = ", ".join(
summary["by_test"]["platforms_failed"][bucket].keys()
)
table.add_row(
[
summary["by_test"]["failed"][bucket],
summary["by_test"]["total"][bucket],
failed_k8s,
failed_pg,
failed_platforms,
bucket,
]
)
print(table, file=file_out)
def format_by_special_failure(summary, structure, file_out=None):
"""print metrics bucketed by special failure"""
title = structure["title"]
anchor = structure["anchor"]
print(f"\n<h2><a name={anchor}>{title}</a></h2>\n", file=file_out)
table = PrettyTable(align="l")
table.field_names = structure["header"]
table.set_style(MARKDOWN)
sorted_by_count = dict(
sorted(
summary["by_special_failures"]["total"].items(),
key=lambda item: item[1],
reverse=True,
)
)
for bucket in sorted_by_count:
failed_tests = ", ".join(
summary["by_special_failures"]["tests_failed"][bucket].keys()
)
failed_k8s = ", ".join(
summary["by_special_failures"]["k8s_versions_failed"][bucket].keys()
)
failed_pg = ", ".join(
summary["by_special_failures"]["pg_versions_failed"][bucket].keys()
)
failed_platforms = ", ".join(
summary["by_special_failures"]["platforms_failed"][bucket].keys()
)
table.add_row(
[
summary["by_special_failures"]["total"][bucket],
bucket,
failed_tests,
failed_k8s,
failed_pg,
failed_platforms,
]
)
print(table, file=file_out)
def format_by_code(summary, structure, file_out=None):
"""print metrics bucketed by failing code"""
title = structure["title"]
anchor = structure["anchor"]
print(f"\n<h2><a name={anchor}>{title}</a></h2>\n", file=file_out)
table = PrettyTable(align="l")
table.field_names = structure["header"]
table.set_style(MARKDOWN)
sorted_by_code = dict(
sorted(
summary["by_code"]["total"].items(),
key=lambda item: item[1],
reverse=True,
)
)
for bucket in sorted_by_code:
tests = ", ".join(summary["by_code"]["tests"][bucket].keys())
# replace newlines and pipes to avoid interference with Markdown tables
errors = (
summary["by_code"]["errors"][bucket]
.replace("\n", "<br />")
.replace("|", "—")
)
err_cell = f"<details><summary>Click to expand</summary><span>{errors}</span></details>"
table.add_row(
[
summary["by_code"]["total"][bucket],
bucket,
tests,
err_cell,
]
)
print(table, file=file_out)
def format_duration(duration):
"""pretty-print duration"""
minutes = duration.seconds // 60
seconds = duration.seconds % 60
return f"{minutes} min {seconds} sec"
def format_durations_table(test_times, structure, file_out=None):
"""print the table of durations per test"""
title = structure["title"]
anchor = structure["anchor"]
print(f"\n<h2><a name={anchor}>{title}</a></h2>\n", file=file_out)
table = PrettyTable(align="l", max_width=80)
table.set_style(MARKDOWN)
table.field_names = structure["header"]
sorted_by_longest = dict(
sorted(test_times["max"].items(), key=lambda item: item[1], reverse=True)
)
for bucket in sorted_by_longest:
name = bucket
longest = format_duration(test_times["max"][bucket])
shortest = format_duration(test_times["min"][bucket])
branch = test_times["slowest_branch"][bucket]
table.add_row([longest, shortest, branch, name])
print(table, file=file_out)
def format_suite_durations_table(suite_times, structure, file_out=None):
"""print the table of durations for the whole suite, per platform"""
title = structure["title"]
anchor = structure["anchor"]
print(f"\n<h2><a name={anchor}>{title}</a></h2>\n", file=file_out)
table = PrettyTable(align="l", max_width=80)
table.set_style(MARKDOWN)
table.field_names = structure["header"]
# we want to display a table with one row per platform, giving us the
# shortest duration and longest duration of the suite, and the slowest branch
# The dictionaries in `suite_durations` are keyed by platform
suite_durations = {
"min": {},
"max": {},
"slowest_branch": {},
}
for platform in suite_times["start_time"]:
for matrix_id in suite_times["start_time"][platform]:
duration = (
suite_times["end_time"][platform][matrix_id]
- suite_times["start_time"][platform][matrix_id]
)
if platform not in suite_durations["max"]:
suite_durations["max"][platform] = duration
if platform not in suite_durations["min"]:
suite_durations["min"][platform] = duration
if platform not in suite_durations["slowest_branch"]:
suite_durations["slowest_branch"][platform] = matrix_id
if suite_durations["max"][platform] < duration:
suite_durations["max"][platform] = duration
suite_durations["slowest_branch"][platform] = matrix_id
if duration < suite_durations["min"][platform]:
suite_durations["min"][platform] = duration
sorted_by_longest = dict(
sorted(suite_durations["max"].items(), key=lambda item: item[1], reverse=True)
)
for bucket in sorted_by_longest:
name = bucket
longest = format_duration(suite_durations["max"][bucket])
shortest = format_duration(suite_durations["min"][bucket])
branch = suite_durations["slowest_branch"][bucket]
table.add_row([longest, shortest, branch, name])
print(table, file=file_out)
def format_test_failures(summary, file_out=None):
"""creates the part of the test report that drills into the failures"""
if summary["total_special_fails"] > 0:
by_special_failures_section = {
"title": "Special failures",
"anchor": "by_special_failure",
"header": [
"failure count",
"special failure",
"failed tests",
"failed K8s",
"failed PG",
"failed Platforms",
],
}
format_by_special_failure(
summary, by_special_failures_section, file_out=file_out
)
by_test_section = {
"title": "Failures by test",
"anchor": "by_test",
"header": [
"failed runs",
"total runs",
"failed K8s",
"failed PG",
"failed Platforms",
"test",
],
}
format_by_test(summary, by_test_section, file_out=file_out)
by_code_section = {
"title": "Failures by errored code",
"anchor": "by_code",
"header": [
"total failures",
"failing code location",
"in tests",
"error message",
],
}
format_by_code(summary, by_code_section, file_out=file_out)
by_matrix_section = {
"title": "Failures by matrix branch",
"anchor": "by_matrix",
"header": ["failed tests", "total tests", "matrix branch"],
}
format_bucket_table(summary["by_matrix"], by_matrix_section, file_out=file_out)
by_k8s_section = {
"title": "Failures by kubernetes version",
"anchor": "by_k8s",
"header": ["failed tests", "total tests", "kubernetes version"],
}
format_bucket_table(summary["by_k8s"], by_k8s_section, file_out=file_out)
by_postgres_section = {
"title": "Failures by postgres version",
"anchor": "by_postgres",
"header": ["failed tests", "total tests", "postgres version"],
}
format_bucket_table(summary["by_postgres"], by_postgres_section, file_out=file_out)
by_platform_section = {
"title": "Failures by platform",
"anchor": "by_platform",
"header": ["failed tests", "total tests", "platform"],
}
format_bucket_table(summary["by_platform"], by_platform_section, file_out=file_out)
def format_test_summary(summary, file_out=None):
"""creates a Markdown document with several tables rendering test results.
Outputs to stdout like a good 12-factor-app citizen, unless the `file_out`
argument is provided
"""
print(
"Note that there are several tables below: overview, bucketed "
+ "by several parameters, timings.",
file=file_out,
)
print(file=file_out)
if summary["total_failed"] != 0:
print(