-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test_result_fetcher.py
executable file
·1393 lines (1203 loc) · 58.1 KB
/
unit_test_result_fetcher.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/local/bin/python3
import json
import logging
import os
import sys
import time
import traceback
import urllib
import urllib.request
import urllib.error as url_error
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import List, Dict, Set, Tuple, Any, Callable
from googleapiwrapper.common import ServiceType
from googleapiwrapper.google_auth import GoogleApiAuthorizer
from googleapiwrapper.google_drive import (
DriveApiWrapper,
DriveApiScope,
DuplicateFileWriteResolutionMode,
DriveApiWrapperSessionSettings,
FileFindMode,
DriveApiWrapperSingleOperationSettings,
SearchResultHandlingMode,
DriveApiFile,
)
from pythoncommons.constants import ExecutionMode
from pythoncommons.date_utils import DateUtils
from pythoncommons.email import EmailService, EmailMimeType
from pythoncommons.file_utils import FileUtils, JsonFileUtils, FindResultType
from pythoncommons.logging_setup import SimpleLoggingSetup
from pythoncommons.network_utils import NetworkUtils
from pythoncommons.object_utils import PickleUtils
from pythoncommons.os_utils import OsUtils
from pythoncommons.project_utils import PROJECTS_BASEDIR_NAME, ProjectUtils
from pythoncommons.string_utils import auto_str
from yarndevtools.cdsw.constants import SECRET_PROJECTS_DIR
from yarndevtools.commands_common import CommandAbs, EmailArguments
from yarndevtools.common.shared_command_utils import FullEmailConfig, CommandType
from yarndevtools.constants import YARNDEVTOOLS_MODULE_NAME
CACHED_DATA_DIRNAME = "cached_data"
LOG = logging.getLogger(__name__)
EMAIL_SUBJECT_PREFIX = "YARN Daily unit test report:"
CACHED_DATA_FILENAME = "pickled_unit_test_reporter_data.obj"
SECONDS_PER_DAY = 86400
DEFAULT_REQUEST_LIMIT = 999
JENKINS_BUILDS_EXAMINE_UNLIMITIED_VAL = "jenkins_examine_unlimited_builds"
DAY_THRESHOLD_FOR_LAST_BUILDS = 2
class UnitTestResultFetcherMode(Enum):
JENKINS_MASTER = (
"jenkins_master",
"https://master-02.jenkins.cloudera.com/",
[
"cdpd-master-Hadoop-Common-Unit",
"cdpd-master-Hadoop-HDFS-Unit",
"cdpd-master-Hadoop-MR-Unit",
"cdpd-master-Hadoop-YARN-Unit",
"CDH-7.1-maint-Hadoop-Common-Unit",
"CDH-7.1-maint-Hadoop-HDFS-Unit",
"CDH-7.1-maint-Hadoop-MR-Unit",
"CDH-7.1-maint-Hadoop-YARN-Unit",
"CDH-7.1.7.1000-Hadoop-Common-Unit",
"CDH-7.1.7.1000-Hadoop-HDFS-Unit",
"CDH-7.1.7.1000-Hadoop-MR-Unit",
"CDH-7.1.7.1000-Hadoop-YARN-Unit",
],
)
MAWO = ("MAWO", "http://build.infra.cloudera.com/", ["Mawo-UT-hadoop-CDPD-7.x", "Mawo-UT-hadoop-CDPD-7.1.x"])
def __init__(self, mode_name: str, jenkins_base_url: str, job_names: List[str]):
self.mode_name = mode_name
self.jenkins_base_url = jenkins_base_url
self.job_names = job_names
class UnitTestResultFetcherCacheType(Enum):
FILE = "FILE"
GOOGLE_DRIVE = "GOOGLE_DRIVE"
@auto_str
class DownloadProgress:
# TODO Store awaiting download / awaiting cache load separately
# TODO Decide on startup: What build need to be downloaded, what is in the cache, etc.
def __init__(self, number_of_failed_builds):
self.all_builds: int = number_of_failed_builds
self.current_build_idx = 0
def process_next_build(self):
self.current_build_idx += 1
def short_str(self):
return f"{self.current_build_idx + 1}/{self.all_builds}"
@dataclass
class TestcaseFilter:
project_name: str
filter_expr: str
@property
def as_filter_spec(self):
return f"{self.project_name}:{self.filter_expr}"
@auto_str
class FailedJenkinsBuild:
def __init__(self, full_url_of_job: str, timestamp: int, job_name):
self.url = full_url_of_job
self.urls = JenkinsJobInstanceUrls(full_url_of_job)
self.build_number = full_url_of_job.rsplit("/")[-2]
self.timestamp = timestamp
self.job_name: str = job_name
class JobBuildDataStatus(Enum):
# Invalid statuses
EMPTY = "Report does not contain testcase data"
NO_JSON_DATA_FOUND = "No JSON data found for build report"
CANNOT_FETCH = "Cannot fetch build report"
ALL_GREEN = "Build report contains tests but all are green"
# Valid statuses
HAVE_FAILED_TESTCASES = "Valid build report. Contains some failed tests"
class JobBuildData:
def __init__(self, failed_build: FailedJenkinsBuild, counters, testcases, status: JobBuildDataStatus):
self._failed_build: FailedJenkinsBuild = failed_build
self.counters = counters
self.testcases: List[str] = testcases
self.filtered_testcases: List[FilteredResult] = []
self.filtered_testcases_by_expr: Dict[str, List[str]] = {}
self.no_of_failed_filtered_tc = None
self.unmatched_testcases: Set[str] = set()
self.status: JobBuildDataStatus = status
# TODO Save this to separate pickled object, so when JobBuildData's structure changes, we don't lose sent state for all jobs
self.mail_sent = False
self.sent_date = None
def has_failed_testcases(self):
return len(self.testcases) > 0
def filter_testcases(self, tc_filters: List[TestcaseFilter]):
matched_testcases = set()
for tcf in tc_filters:
matched_for_filter = list(filter(lambda tc: tcf.filter_expr in tc, self.testcases))
self.filtered_testcases.append(FilteredResult(tcf, matched_for_filter))
if tcf.filter_expr not in self.filtered_testcases_by_expr:
self.filtered_testcases_by_expr[tcf.filter_expr] = []
self.filtered_testcases_by_expr[tcf.filter_expr].extend(matched_for_filter)
matched_testcases.update(matched_for_filter)
self.no_of_failed_filtered_tc = sum([len(fr.testcases) for fr in self.filtered_testcases])
self.unmatched_testcases = set(self.testcases).difference(matched_testcases)
@property
def build_number(self):
return self._failed_build.build_number
@property
def build_url(self):
return self._failed_build.url
@property
def job_name(self):
return self._failed_build.job_name
@property
def is_valid(self):
return self.status == JobBuildDataStatus.HAVE_FAILED_TESTCASES
@property
def is_mail_sent(self):
return self.mail_sent
@property
def tc_filters(self):
return [res.filter for res in self.filtered_testcases]
def __str__(self):
if self.is_valid:
return self._str_normal_report()
else:
return self._str_invalid_report()
def _str_invalid_report(self):
return (
f"Build number: {self.build_number}\n"
f"Build URL: {self.build_url}\n"
f"Invalid report! Details: {self.status.value}\n"
)
def _str_normal_report(self):
filtered_testcases: str = ""
if self.tc_filters:
for idx, ftcs in enumerate(self.filtered_testcases):
filtered_testcases += f"\nFILTER #{idx + 1}\n{str(ftcs)}\n"
if filtered_testcases:
filtered_testcases = f"\n{filtered_testcases}\n"
all_failed_testcases = "\n".join(self.testcases)
unmatched_testcases = "\n".join(self.unmatched_testcases)
return (
f"Counters:\n"
f"{self.counters}, "
f"Build number: {self.build_number}\n"
f"Build URL: {self.build_url}\n"
f"Matched testcases: {self.no_of_failed_filtered_tc}\n"
f"Unmatched testcases: {len(self.unmatched_testcases)}\n"
f"{filtered_testcases}\n"
f"Unmatched testcases:\n{unmatched_testcases}\n"
f"ALL Failed testcases:\n{all_failed_testcases}"
)
class JenkinsJobUrls:
def __init__(self, jenkins_base_url, job_name):
self.jenkins_base_url = jenkins_base_url
self.list_builds = self._get_jenkins_list_builds_url(job_name)
def _get_jenkins_list_builds_url(self, job_name: str) -> str:
jenkins_url = self.jenkins_base_url
if jenkins_url.endswith("/"):
jenkins_url = jenkins_url[:-1]
return f"{jenkins_url}/job/{job_name}/api/json?tree=builds[url,result,timestamp]"
class JenkinsJobInstanceUrls:
# Example URL: http://build.infra.cloudera.com/job/Mawo-UT-hadoop-CDPD-7.x/191/
def __init__(self, full_url):
self.full_url = full_url
self.job_console_output_url = self._append_to_url(full_url, "Console")
self.test_report_url = self._append_to_url(full_url, "testReport")
self.test_report_api_json_url = self.test_report_url + "/api/json?pretty=true"
@staticmethod
def _append_to_url(full_url, to_append):
if not full_url[-1] == "/":
full_url += "/"
return full_url + to_append
class JenkinsApiConverter:
def __init__(self, user, password):
self.user = user
self.password = password
def convert(self, job_name: str, jenkins_urls: JenkinsJobUrls, days: int):
all_builds: List[Dict[str, str]] = self._list_builds(jenkins_urls)
last_n_builds: List[Dict[str, str]] = JenkinsApiConverter._filter_builds_last_n_days(all_builds, days=days)
last_n_failed_build_tuples: List[Tuple[str, int]] = JenkinsApiConverter._get_failed_build_urls_with_timestamps(
last_n_builds
)
failed_build_data: List[Tuple[str, int]] = sorted(
last_n_failed_build_tuples, key=lambda tup: tup[1], reverse=True
)
failed_builds = [
FailedJenkinsBuild(
full_url_of_job=tup[0],
timestamp=JenkinsApiConverter._convert_to_unix_timestamp(tup[1]),
job_name=job_name,
)
for tup in failed_build_data
]
total_no_of_builds = len(all_builds)
LOG.info(
f"There are {len(failed_build_data)} builds "
f"(out of {total_no_of_builds}) that have failed tests "
f"in the past {days} days. "
f"Listing builds: {failed_build_data}"
)
return failed_builds, total_no_of_builds
def _list_builds(self, urls: JenkinsJobUrls) -> List[Any]:
"""List all builds of the target project."""
url = urls.list_builds
try:
LOG.info("Fetching builds from Jenkins in url: %s", url)
data = self.safe_fetch_json(url)
# In case job does not exist (HTTP 404), data will be None
if data:
return data["builds"]
return []
except Exception:
LOG.error(f"Could not fetch: {url}")
raise
@staticmethod
def _filter_builds_last_n_days(builds, days):
# Select only those in the last N days
min_time = int(time.time()) - SECONDS_PER_DAY * days
return [b for b in builds if (JenkinsApiConverter._convert_to_unix_timestamp_from_json(b)) > min_time]
@staticmethod
def _get_failed_build_urls_with_timestamps(builds):
return [(b["url"], b["timestamp"]) for b in builds if (b["result"] in ("UNSTABLE", "FAILURE"))]
@staticmethod
def _convert_to_unix_timestamp_from_json(build_json):
timestamp_str = build_json["timestamp"]
return JenkinsApiConverter._convert_to_unix_timestamp(int(timestamp_str))
@staticmethod
def _convert_to_unix_timestamp(ts: int):
# Jenkins' uses milliseconds format to store the timestamp, divide it by 1000
# See: https://stackoverflow.com/a/24308978/1106893
return int(ts / 1000)
@staticmethod
def parse_job_data(data, failed_build: FailedJenkinsBuild) -> JobBuildData:
failed_testcases = set()
found_testcases: int = 0
for suite in data["suites"]:
for tc in suite["cases"]:
found_testcases += 1
status = tc["status"]
err_details = tc["errorDetails"]
if status == "REGRESSION" or status == "FAILED" or (err_details is not None):
failed_testcases.add(f"{tc['className']}.{tc['name']}")
if len(failed_testcases) == 0:
if found_testcases:
LOG.info(
f"No failed tests in test report, check {failed_build.urls.job_console_output_url} for why it was reported failed."
)
return JobBuildData(failed_build, None, failed_testcases, status=JobBuildDataStatus.ALL_GREEN)
else:
return JobBuildData(failed_build, None, failed_testcases, status=JobBuildDataStatus.EMPTY)
else:
counters = JobBuildDataCounters(data["failCount"], data["passCount"], data["skipCount"])
return JobBuildData(
failed_build, counters, failed_testcases, status=JobBuildDataStatus.HAVE_FAILED_TESTCASES
)
def download_test_report(self, failed_build: FailedJenkinsBuild, download_progress: DownloadProgress):
url = failed_build.urls.test_report_api_json_url
LOG.info(f"Loading test report from URL: {url}. Download progress: {download_progress.short_str()}")
return self.safe_fetch_json(url)
def safe_fetch_json(self, url):
def retry_fetch(url):
LOG.error("URL '%s' cannot be fetched (HTTP 502 Proxy Error):", url)
self.safe_fetch_json(url)
# HTTP 404 should be logged
# HTTP Error 502: Proxy Error is just calls this function again (retry) with the same args, indefinitely
data = JenkinsApiConverter.fetch_json(
url,
user=self.user,
password=self.password,
do_not_raise_http_statuses={404, 502},
http_callbacks={
404: lambda: LOG.error("URL '%s' cannot be fetched (HTTP 404):", url),
502: lambda: retry_fetch(url),
},
)
return data
# TODO Could be moved to network_utils (pythoncommons)
@staticmethod
def fetch_json(
url,
user=None,
password=None,
strict=False,
do_not_raise_http_statuses: Set[int] = None,
http_callbacks: Dict[int, Callable] = None,
):
"""Load data from specified url"""
try:
LOG.debug("Making request to URL: %s", url)
auth_handler = urllib.request.HTTPBasicAuthHandler(
password_mgr=urllib.request.HTTPPasswordMgrWithPriorAuth()
)
auth_handler.add_password(
realm="test",
uri=UnitTestResultFetcherMode.JENKINS_MASTER.jenkins_base_url,
user=user,
passwd=password,
is_authenticated=True,
)
opener = urllib.request.build_opener(auth_handler)
ourl = opener.open(url)
codec = ourl.info().get_param("charset")
content = ourl.read().decode(codec)
except urllib.error.HTTPError as e:
if do_not_raise_http_statuses and e.code in do_not_raise_http_statuses:
if http_callbacks and e.code in http_callbacks:
http_callbacks[e.code]()
return {}
else:
error_message = e.read()
LOG.error("Printing error response received from server: %s", error_message)
raise e
return json.loads(content, strict=strict)
@dataclass
class FilteredResult:
filter: TestcaseFilter
testcases: List[str]
def __str__(self):
tcs = "\n".join(self.testcases)
s = f"Project: {self.filter.project_name}\n"
s += f"Filter expression: {self.filter.filter_expr}\n"
s += f"Number of failed testcases: {len(self.testcases)}\n"
s += f"Failed testcases (fully qualified name):\n{tcs}"
return s
@auto_str
class JenkinsJobReport:
def __init__(self, job_build_datas, all_failing_tests, total_no_of_builds: int, num_builds_per_config: int):
self.jobs_by_url: Dict[str, JobBuildData] = {job.build_url: job for job in job_build_datas}
# Sort by URL, descending
self._job_urls = list(sorted(self.jobs_by_url.keys(), reverse=True))
self._sorted_build_info = self._sort_build_info(job_build_datas)
self.all_failing_tests: Dict[str, int] = all_failing_tests
self.total_no_of_builds: int = total_no_of_builds
self.actual_num_builds = self._determine_actual_number_of_builds(num_builds_per_config)
self._index = 0
def start_processing(self):
LOG.info(f"Report list contains build results: {self._job_urls}")
LOG.info(f"Processing {self.actual_num_builds} reports")
def __len__(self):
return self.actual_num_builds
def __iter__(self):
return self
def __next__(self):
if self._index == self.actual_num_builds:
raise StopIteration
result = self.jobs_by_url[self._job_urls[self._index]]
self._index += 1
return result
def _determine_actual_number_of_builds(self, num_builds_per_config):
build_data_count = len(self.jobs_by_url)
total_no_of_builds = self.total_no_of_builds
if build_data_count < total_no_of_builds:
LOG.warning(
"Report contains less builds than total number of builds. " "Report has: %d, Total: %d",
build_data_count,
total_no_of_builds,
)
actual_num_builds = min(num_builds_per_config, build_data_count)
else:
actual_num_builds = min(num_builds_per_config, self.total_no_of_builds)
return actual_num_builds
@property
def known_build_urls(self):
return self.jobs_by_url.keys()
def are_all_mail_sent(self):
return all(job_data.mail_sent for job_data in self.jobs_by_url.values())
def reset_mail_sent_state(self):
for job_data in self.jobs_by_url.values():
job_data.sent_date = None
job_data.mail_sent = False
def mark_sent(self, build_url):
job_data = self.jobs_by_url[build_url]
job_data.sent_date = DateUtils.get_current_datetime()
job_data.mail_sent = True
def get_job_data(self, build_url: str):
return self.jobs_by_url[build_url]
def print_report(self, build_data):
LOG.info(f"\nPRINTING REPORT: \n\n{build_data}")
LOG.info(f"\nAmong {self.total_no_of_builds} runs examined, all failed tests <#failedRuns: testName>:")
# Print summary section: all failed tests sorted by how many times they failed
LOG.info("TESTCASE SUMMARY:")
for tn in sorted(self.all_failing_tests, key=self.all_failing_tests.get, reverse=True):
LOG.info(f"{self.all_failing_tests[tn]}: {tn}")
def get_latest_build_info(self) -> Tuple[str, str, int]:
if len(self._sorted_build_info) > 0:
return self._sorted_build_info[0]
return None
@staticmethod
def _sort_build_info(job_build_datas):
# EXAMPLE:
# {
# '300': ('https://master-02.jenkins.cloudera.com/job/cdpd-master-Hadoop-Common-Unit/300/', 1695393655),
# '299': ('https://master-02.jenkins.cloudera.com/job/cdpd-master-Hadoop-Common-Unit/299/', 1695220835),
# '298': ('https://master-02.jenkins.cloudera.com/job/cdpd-master-Hadoop-Common-Unit/298/', 1694788967),
# ...
# }
build_dict: Dict[str, Tuple[str, int]] = {
job._failed_build.build_number: (job._failed_build.url, job._failed_build.timestamp)
for job in job_build_datas
}
tmp = []
for build_no, tup in build_dict.items():
build_url = tup[0]
build_ts = tup[1]
tmp.append((build_no, build_url, build_ts))
return list(sorted(tmp, key=lambda x: x[0], reverse=True))
@dataclass
class JobBuildDataCounters:
failed: int
passed: int
skipped: int
def __str__(self):
return f"Failed: {self.failed}, Passed: {self.passed}, Skipped: {self.skipped}"
@dataclass(frozen=True)
class CachedBuildKey:
job_name: str
build_number: str
@dataclass
class CachedBuild:
build_key: CachedBuildKey
full_report_file_path: str
class Cache(ABC):
@abstractmethod
def initialize(self) -> Dict[str, JenkinsJobReport]:
pass
@abstractmethod
def is_build_data_in_cache(self, cached_build_key: CachedBuildKey):
pass
@abstractmethod
def load_reports_meta(self) -> Dict[str, JenkinsJobReport]:
pass
@abstractmethod
def save_reports_meta(self, reports: Dict[str, JenkinsJobReport], log: bool = False):
pass
@abstractmethod
def save_report(self, data, cached_build_key: CachedBuildKey):
pass
@abstractmethod
def load_report(self, cached_build_key: CachedBuildKey) -> Dict[Any, Any]:
pass
@property
@abstractmethod
def meta_file_path(self):
pass
@staticmethod
def generate_job_dirname(cached_build_key: CachedBuildKey):
return Cache.escape_job_name(cached_build_key.job_name)
@staticmethod
def escape_job_name(job_name: str):
return job_name.replace(".", "_")
@staticmethod
def generate_report_filename(cached_build_key: CachedBuildKey):
return f"{cached_build_key.build_number}-testreport.json"
class FileCache(Cache):
def __init__(self, config):
self.config: CacheConfig = config
def initialize(self) -> Dict[str, JenkinsJobReport]:
report_files = FileUtils.find_files(
self.config.reports_dir,
find_type=FindResultType.FILES,
single_level=False,
full_path_result=True,
extension="json",
)
self.cached_builds: Dict[CachedBuildKey, CachedBuild] = self._load_cached_builds_from_fs(report_files)
LOG.info("Loaded cached builds: %s", self.cached_builds)
return self.load_reports_meta()
def _load_cached_builds_from_fs(self, report_files):
cached_builds: Dict[CachedBuildKey, CachedBuild] = {}
for report_file in report_files:
orig_file_path = report_file
# Example: CDH-7_1-maint-Hadoop-Common-Unit/1-testreport.json
if report_file.startswith(self.config.reports_dir):
report_file = report_file[len(self.config.reports_dir) :]
comps = report_file.split(os.sep)
comps = [c for c in comps if c]
job_name = comps[0]
report_filename = comps[1]
build_number = report_filename.split("-")[0]
key = CachedBuildKey(job_name, build_number)
cached_builds[key] = CachedBuild(key, orig_file_path)
return cached_builds
def _generate_file_name_for_report(self, cached_build_key: CachedBuildKey):
job_dir_path = FileUtils.join_path(self.config.reports_dir, self.generate_job_dirname(cached_build_key))
job_dir_path = FileUtils.ensure_dir_created(job_dir_path)
return FileUtils.join_path(job_dir_path, self.generate_report_filename(cached_build_key))
def is_build_data_in_cache(self, cached_build_key: CachedBuildKey):
if cached_build_key in self.cached_builds:
LOG.debug(
"Build found in cache. Job name: %s, Build number: %s",
cached_build_key.job_name,
cached_build_key.build_number,
)
return True
return False
def load_reports_meta(self) -> Dict[str, JenkinsJobReport]:
LOG.info("Trying to load cached data from file: %s", self.meta_file_path)
if FileUtils.does_file_exist(self.meta_file_path):
reports: Dict[str, JenkinsJobReport] = PickleUtils.load(self.meta_file_path)
LOG.info("Printing email send status for jobs and builds...")
for job_name, jenkins_job_report in reports.items():
for job_url, job_build_data in jenkins_job_report.jobs_by_url.items():
LOG.info("Job URL: %s, email sent: %s", job_url, job_build_data.mail_sent)
LOG.info("Loaded cached data from: %s", self.meta_file_path)
return reports
else:
LOG.info("Cached data file not found in: %s", self.meta_file_path)
return {}
def save_reports_meta(self, reports: Dict[str, JenkinsJobReport], log: bool = False):
if log:
LOG.debug("Final cached data object: %s", reports)
LOG.info("Dumping %s object to file %s", JenkinsJobReport.__name__, self.meta_file_path)
PickleUtils.dump(reports, self.meta_file_path)
def save_report(self, data, cached_build_key: CachedBuildKey):
report_file_path = self._generate_file_name_for_report(cached_build_key)
LOG.info(f"Saving test report response JSON to file cache: {report_file_path}")
JsonFileUtils.write_data_to_file_as_json(report_file_path, data)
return report_file_path
def load_report(self, cached_build_key: CachedBuildKey) -> Dict[Any, Any]:
report_file_path = self._generate_file_name_for_report(cached_build_key)
LOG.info(f"Loading cached test report from file: {report_file_path}")
return JsonFileUtils.load_data_from_json_file(report_file_path)
@property
def meta_file_path(self):
return self.config.data_file_path
class GoogleDriveCache(Cache):
DRIVE_FINAL_CACHE_DIR = CommandType.UNIT_TEST_RESULT_FETCHER.output_dir_name + "_" + CACHED_DATA_DIRNAME
# TODO implement throttling: Too many requests to Google Drive?
def __init__(self, config):
self.config: CacheConfig = config
self.file_cache: FileCache = FileCache(config)
self.authorizer = GoogleApiAuthorizer(
ServiceType.DRIVE,
project_name=CommandType.UNIT_TEST_RESULT_FETCHER.output_dir_name,
secret_basedir=SECRET_PROJECTS_DIR,
account_email="snemeth@cloudera.com",
scopes=[DriveApiScope.DRIVE_PER_FILE_ACCESS.value],
)
session_settings = DriveApiWrapperSessionSettings(
FileFindMode.JUST_UNTRASHED, DuplicateFileWriteResolutionMode.ADD_NEW_REVISION, enable_path_cache=True
)
self.drive_wrapper = DriveApiWrapper(self.authorizer, session_settings=session_settings)
self.drive_meta_dir_path = FileUtils.join_path(
PROJECTS_BASEDIR_NAME, YARNDEVTOOLS_MODULE_NAME, self.DRIVE_FINAL_CACHE_DIR
)
self.drive_reports_basedir = FileUtils.join_path(
PROJECTS_BASEDIR_NAME, YARNDEVTOOLS_MODULE_NAME, self.DRIVE_FINAL_CACHE_DIR, "reports"
)
def initialize(self):
reports = self.file_cache.initialize()
self._sync_from_file_cache()
return reports
def _sync_from_file_cache(self):
all_report_files: List[DriveApiFile] = self.drive_wrapper.get_files("*-testreport.json")
found_builds: Set[CachedBuildKey] = set()
for report_drive_file in all_report_files:
job_name = report_drive_file._parent.name
components = report_drive_file.name.split("-")
if len(components) != 2:
LOG.error("Found test report with unexpected name: %s", job_name)
continue
found_builds.add(CachedBuildKey(job_name, components[0]))
LOG.debug("Found %d builds from Google Drive: %s", len(found_builds), found_builds)
builds_to_check_from_drive = {
key: value for (key, value) in self.file_cache.cached_builds.items() if key not in found_builds
}
LOG.debug("Will check these builds in Google Drive: %s", builds_to_check_from_drive)
# TODO Implement sync from GDrive -> Filesystem (other way around)
# TODO Create progressTracker object to show current status of Google Drive uploads / queries
for cached_build_key, cached_build in builds_to_check_from_drive.items():
drive_report_file_path = self._generate_file_name_for_report(cached_build_key)
settings: DriveApiWrapperSingleOperationSettings = DriveApiWrapperSingleOperationSettings(
file_find_mode=None,
duplicate_file_handling_mode=DuplicateFileWriteResolutionMode.FAIL_FAST,
search_result_handling_mode=SearchResultHandlingMode.SINGLE_FILE_PER_SEARCH_RESULT,
)
exist = self.drive_wrapper.does_file_exist(drive_report_file_path, op_settings=settings)
if not exist:
settings: DriveApiWrapperSingleOperationSettings = DriveApiWrapperSingleOperationSettings(
file_find_mode=None, duplicate_file_handling_mode=DuplicateFileWriteResolutionMode.FAIL_FAST
)
self.drive_wrapper.upload_file(
cached_build.full_report_file_path, drive_report_file_path, op_settings=settings
)
def _generate_file_name_for_report(self, cached_build_key: CachedBuildKey):
return FileUtils.join_path(
self.drive_reports_basedir,
self.generate_job_dirname(cached_build_key),
self.generate_report_filename(cached_build_key),
)
def is_build_data_in_cache(self, cached_build_key: CachedBuildKey):
# TODO Check in Drive and if not successful, decide based on local file cache
return self.file_cache.is_build_data_in_cache(cached_build_key)
def load_reports_meta(self) -> Dict[str, JenkinsJobReport]:
# TODO Load from Drive and if not successful, load from local file cache
return self.file_cache.load_reports_meta()
def save_reports_meta(self, reports: Dict[str, JenkinsJobReport], log: bool = False):
# TODO implement throttling: Too many requests to Google Drive
self.file_cache.save_reports_meta(reports)
drive_path = FileUtils.join_path(self.drive_meta_dir_path, CACHED_DATA_FILENAME)
self.drive_wrapper.upload_file(self.meta_file_path, drive_path)
def save_report(self, data, cached_build_key: CachedBuildKey):
saved_report_file_path = self.file_cache.save_report(data, cached_build_key)
drive_path = self._generate_file_name_for_report(cached_build_key)
self.drive_wrapper.upload_file(saved_report_file_path, drive_path)
def load_report(self, cached_build_key: CachedBuildKey) -> Dict[Any, Any]:
cache_hit = self.file_cache.is_build_data_in_cache(cached_build_key)
if cache_hit:
return self.file_cache.load_report(cached_build_key)
else:
filename = self._generate_file_name_for_report(cached_build_key)
self.drive_wrapper.get_file(filename)
# TODO missing return
# TODO Load from Drive and if not successful, load from local file cache
# TODO IF report.json is only found in local cache, save it to Drive
@property
def meta_file_path(self):
return self.file_cache.meta_file_path
class CacheConfig:
def __init__(self, args, output_dir):
self.enabled: bool = not args.disable_file_cache if hasattr(args, "disable_file_cache") else False
self.reports_dir = FileUtils.ensure_dir_created(FileUtils.join_path(output_dir, "reports"))
self.cached_data_dir = FileUtils.ensure_dir_created(FileUtils.join_path(output_dir, CACHED_DATA_DIRNAME))
self.download_uncached_job_data: bool = (
args.download_uncached_job_data if hasattr(args, "download_uncached_job_data") else False
)
self.cache_type: UnitTestResultFetcherCacheType = (
UnitTestResultFetcherCacheType(args.cache_type.upper())
if hasattr(args, "cache_type") and args.cache_type
else UnitTestResultFetcherCacheType.FILE
)
@property
def data_file_path(self):
return FileUtils.join_path(self.cached_data_dir, CACHED_DATA_FILENAME)
class EmailConfig:
def __init__(self, args):
self.full_email_conf: FullEmailConfig = FullEmailConfig(args, allow_empty_subject=True)
skip_email = args.skip_email if hasattr(args, "skip_email") else False
self.force_send_email = args.force_send_email if hasattr(args, "force_send_email") else False
self.send_mail: bool = not skip_email or self.force_send_email
self.reset_email_sent_state: List[str] = (
args.reset_sent_state_for_jobs if hasattr(args, "reset_sent_state_for_jobs") else []
)
if not self.send_mail:
LOG.info("Skip sending emails, as per configuration.")
def validate(self, job_names: List[str]):
if not all([reset in job_names for reset in self.reset_email_sent_state]):
raise ValueError(
"Not all jobs are recognized while trying to reset email sent state for jobs! "
"Valid job names: {}, Current job names: {}".format(job_names, self.reset_email_sent_state)
)
class Email:
def __init__(self, config):
self.config: EmailConfig = config
self.email_service = EmailService(config.full_email_conf.email_conf)
def initialize(self, reports: Dict[str, JenkinsJobReport]):
# Try to reset email sent state of asked jobs
if self.config.reset_email_sent_state:
LOG.info("Resetting email sent state to False on these jobs: %s", self.config.reset_email_sent_state)
for job_name in self.config.reset_email_sent_state:
# Reports can be empty at this point if cache was empty for this job or not found
if job_name in reports:
reports[job_name].reset_mail_sent_state()
def send_mail(self, build_data: JobBuildData):
# TODO Add MailSendProgress class to track how many emails were sent
LOG.info("Sending report in email for job: %s", build_data.build_url)
self.email_service.send_mail(
sender=self.config.full_email_conf.sender,
subject=self._get_email_subject(build_data),
body=str(build_data),
recipients=self.config.full_email_conf.recipients,
body_mimetype=EmailMimeType.PLAIN,
)
LOG.info("Finished sending report in email for job: %s", build_data.build_url)
def send_warning_mail(self, build_data: JobBuildData, ts, dt, diff_in_days):
LOG.info("Sending warning email for job: %s", build_data.job_name)
mail_body = (
"Latest build URL: {}\n"
"Latest build timestamp: {}\n"
"Latest build date: {}\n"
"Diff to today: {}".format(build_data.build_url, ts, dt, diff_in_days)
)
self.email_service.send_mail(
sender=self.config.full_email_conf.sender,
subject="ATTENTION: No new builds detected for job: " + build_data.job_name,
body=str(mail_body),
recipients=self.config.full_email_conf.recipients,
body_mimetype=EmailMimeType.PLAIN,
)
LOG.info("Finished sending warning email for job: %s", build_data.job_name)
@staticmethod
def _get_email_subject(build_data: JobBuildData):
if build_data.is_valid:
email_subject = f"{EMAIL_SUBJECT_PREFIX} Failed tests with build: {build_data.build_url}"
else:
email_subject = f"{EMAIL_SUBJECT_PREFIX} Error with test report, build is invalid: {build_data.build_url}"
return email_subject
def process_job_data(self, build_data, report):
if self.config.send_mail:
if not build_data.is_mail_sent or self.config.force_send_email:
self.send_mail(build_data)
report.mark_sent(build_data.build_url)
else:
LOG.info(
"Not sending report of job URL %s, as it was already sent before on %s.",
build_data.build_url,
build_data.sent_date,
)
def process_report(self, report):
if self.config.send_mail:
latest_build: Tuple[str, str, int] = report.get_latest_build_info()
if latest_build:
now = DateUtils.now()
build_url = latest_build[1]
timestamp = latest_build[2]
dt = DateUtils.create_datetime_from_timestamp(timestamp)
formatted_date: str = DateUtils.format_unix_timestamp(timestamp)
timedelta = now - dt
job_name = report.get_job_data(build_url).job_name
LOG.info(
"Latest build for %s: %s, current date: %s, diff in days (timedelta): %s",
job_name,
formatted_date,
now,
timedelta,
)
if timedelta.days >= DAY_THRESHOLD_FOR_LAST_BUILDS:
job_data = report.get_job_data(build_url)
self.send_warning_mail(job_data, timestamp, dt, timedelta)
else:
LOG.debug("Latest build for %s has new builds, it is within threshold", job_name)
class UnitTestResultFetcherConfig:
def __init__(self, output_dir: str, args):
self.args = args
self.cache: CacheConfig = CacheConfig(args, output_dir)
self.email: EmailConfig = EmailConfig(args)
self.request_limit = args.req_limit if hasattr(args, "req_limit") and args.req_limit else 1
self.jenkins_mode: UnitTestResultFetcherMode = (
UnitTestResultFetcherMode[args.jenkins_mode.upper()]
if hasattr(args, "jenkins_mode") and args.jenkins_mode
else None
)
self.jenkins_base_url = args.jenkins_url
self.job_names: List[str] = args.job_names.split(",")
self.num_builds: int = self._determine_number_of_builds_to_examine(args.num_builds, self.request_limit)
tc_filters_raw = args.tc_filters if hasattr(args, "tc_filters") and args.tc_filters else []
self.tc_filters: List[TestcaseFilter] = [TestcaseFilter(*tcf.split(":")) for tcf in tc_filters_raw]
self.full_cmd: str = OsUtils.determine_full_command_filtered(filter_password=True)
self.force_download_mode = args.force_download_mode if hasattr(args, "force_download_mode") else False
self.omit_job_summary: bool = args.omit_job_summary if hasattr(args, "omit_job_summary") else False
self.fail_on_all_green_report: bool = False # TODO hardcoded
self.fail_on_empty_report: bool = False # TODO hardcoded
self.fail_reports_with_no_data: bool = False # TODO hardcoded
self.reset_job_build_data_for_jobs: List[str] = (
args.reset_job_build_data_for_jobs if hasattr(args, "reset_job_build_data_for_jobs") else []
)
self.jenkins_user = args.jenkins_user if hasattr(args, "jenkins_user") and args.jenkins_user else None
self.jenkins_password = (
args.jenkins_password if hasattr(args, "jenkins_password") and args.jenkins_password else None
)
# Validation
if not self.tc_filters:
LOG.warning("TESTCASE FILTER IS NOT SET!")
if self.jenkins_mode and (self.jenkins_base_url or self.job_names):
LOG.warning(
"Jenkins mode is set to %s. \n"
"Specified values for Jenkins URL: %s\n"
"Specified values for job names: %s\n"
"Jenkins mode will take precedence!",
self.jenkins_mode,
self.jenkins_base_url,
self.job_names,
)
self.jenkins_base_url = self.jenkins_mode.jenkins_base_url
self.job_names = self.jenkins_mode.job_names
if not all([reset in self.job_names for reset in self.reset_job_build_data_for_jobs]):
raise ValueError(
"Not all jobs are recognized while trying to reset job build data for jobs! "
"Valid job names: {}, Current job names: {}".format(self.job_names, self.reset_job_build_data_for_jobs)
)
if not self.jenkins_user:
raise ValueError("Jenkins user should be specified for API authentication")
if not self.jenkins_password:
raise ValueError("Jenkins password should be specified for API authentication")
self.email.validate(self.job_names)
@staticmethod
def _determine_number_of_builds_to_examine(config_value, request_limit) -> int:
if config_value == JENKINS_BUILDS_EXAMINE_UNLIMITIED_VAL:
return sys.maxsize
no_of_builds = int(config_value)
if request_limit < no_of_builds:
LOG.warning("Limiting the number of builds to fetch by the request limit: %s", request_limit)
return min(no_of_builds, request_limit)
def __str__(self):
# TODO Add all config properties
return (
f"Full command was: {self.full_cmd}\n"
f"Jenkins URL: {self.jenkins_base_url}\n"
f"Jenkins job names: {self.job_names}\n"
f"Number of builds to check: {self.num_builds}\n"
f"Testcase filters: {self.tc_filters}\n"
f"Force download mode: {self.force_download_mode}\n"
f"Force email send mode: {self.email.force_send_email}\n"
)
# TODO Separate all download functionality: Progress of downloads, code that fetches API, etc.
class UnitTestResultFetcher(CommandAbs):
def __init__(self, args, output_dir):
self.config = UnitTestResultFetcherConfig(output_dir, args)
self.jenkins_api_converter = JenkinsApiConverter(self.config.jenkins_user, self.config.jenkins_password)
self.reports: Dict[str, JenkinsJobReport] = {} # key is the Jenkins job name
self.cache: Cache = self._create_cache(self.config)
self.email: Email = Email(self.config.email)
self.sent_requests: int = 0
@staticmethod
def create_parser(subparsers):
parser = subparsers.add_parser(
CommandType.UNIT_TEST_RESULT_FETCHER.name,
help="Fetches, parses and sends unit test result reports from Jenkins in email."
"Example: "
"--mode jenkins_master "
"--jenkins-url {jenkins_base_url} "
"--job-names {job_names} "
"--testcase-filter org.apache.hadoop.yarn "