-
Notifications
You must be signed in to change notification settings - Fork 9
Fix sending mail in case of success #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -127,6 +127,9 @@ def __init__(self): | |||||||||||||||||||
| self.available_test_case = TEST_CASES | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def parse_args(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Parse command line arguments. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| parser = argparse.ArgumentParser( | ||||||||||||||||||||
| description="NightlyTestsReport program report all failures" | ||||||||||||||||||||
| "over all OS and Tests (tests, test-pytest, test-openshift-pytest)." | ||||||||||||||||||||
|
|
@@ -150,11 +153,17 @@ def parse_args(self): | |||||||||||||||||||
| return parser.parse_args() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def return_plan_name(self, item) -> str: | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Return the plan name for a given item. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| return "".join( | ||||||||||||||||||||
| [x[1] for x in self.available_test_case if item.startswith(x[0])] | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def load_mails_from_environment(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Load email addresses from environment variables. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| print(os.environ) | ||||||||||||||||||||
| if "DB_MAILS" in os.environ: | ||||||||||||||||||||
| SCLORG_MAILS["mariadb-container"] = os.environ["DB_MAILS"].split(",") | ||||||||||||||||||||
|
|
@@ -186,6 +195,12 @@ def load_mails_from_environment(self): | |||||||||||||||||||
| print(f"Send email: '{self.send_email}'") | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def send_file_to_pastebin(self, log_path, log_name: Path): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Send file to pastebin using send_to_paste_bin.sh script. | ||||||||||||||||||||
| It is used for sending logs from TMT command in case of TMT failures. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| :param log_path: Path to log file to send | ||||||||||||||||||||
| :param log_name: Path to file where pastebin link will be stored""" | ||||||||||||||||||||
| if not os.path.exists(log_path): | ||||||||||||||||||||
| return | ||||||||||||||||||||
| cmd = f'{SEND_PASTE_BIN} "{log_path}" "{str(log_name)}"' | ||||||||||||||||||||
|
|
@@ -200,6 +215,11 @@ def send_file_to_pastebin(self, log_path, log_name: Path): | |||||||||||||||||||
| time.sleep(1) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def get_pastebin_url(self, log_name: str) -> str: | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Get pastebin URL from file where send_to_paste_bin.sh | ||||||||||||||||||||
| script stored it after sending logs to pastebin. | ||||||||||||||||||||
| :param log_name: Path to file where pastebin link is stored | ||||||||||||||||||||
| :return: URL as string or empty string if URL is not found in file""" | ||||||||||||||||||||
| with open(log_name, "r") as f: | ||||||||||||||||||||
| lines = f.read() | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -210,36 +230,51 @@ def get_pastebin_url(self, log_name: str) -> str: | |||||||||||||||||||
| return "" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def store_tmt_logs_to_dict( | ||||||||||||||||||||
| self, path_dir: Path, test_case, is_running=False, not_exists=False | ||||||||||||||||||||
| self, | ||||||||||||||||||||
| path_dir: Path, | ||||||||||||||||||||
| test_case, | ||||||||||||||||||||
| is_running=False, | ||||||||||||||||||||
| not_exists=False, | ||||||||||||||||||||
| is_failed=False, | ||||||||||||||||||||
| ): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Store TMT logs in a dictionary for reporting purposes. | ||||||||||||||||||||
| :param path_dir: Path to the directory containing TMT logs | ||||||||||||||||||||
| :param test_case: Name of the test case | ||||||||||||||||||||
| :param is_running: Flag indicating if the test is still running | ||||||||||||||||||||
| :param not_exists: Flag indicating if the data directory does not exist | ||||||||||||||||||||
| :param is_failed: Flag indicating if the test has failed | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| log_path = self.reports_dir / test_case / "tmt-verbose-log" | ||||||||||||||||||||
| log_name = path_dir / "tmt-verbose-log.txt" | ||||||||||||||||||||
| self.send_file_to_pastebin(log_path=log_path, log_name=log_name) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if not (is_running or is_failed or not_exists): | ||||||||||||||||||||
| return | ||||||||||||||||||||
| if not_exists: | ||||||||||||||||||||
| msg = ( | ||||||||||||||||||||
| f"Data dir for test case {test_case} does not exist." | ||||||||||||||||||||
| f"Look at log in attachment called '{test_case}-log.txt'." | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| if is_running: | ||||||||||||||||||||
| msg = ( | ||||||||||||||||||||
| f"tmt tests for case {test_case} is still running." | ||||||||||||||||||||
| f"Look at log in attachment called '{test_case}-log.txt'." | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| msg = ( | ||||||||||||||||||||
| f"tmt command has failed for test case {test_case}." | ||||||||||||||||||||
| f"Look at log in attachment called '{test_case}-log.txt'." | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| if is_running: | ||||||||||||||||||||
| msg = ( | ||||||||||||||||||||
| f"tmt tests for case {test_case} is still running." | ||||||||||||||||||||
| f"Look at log in attachment called '{test_case}-log.txt'." | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| if is_failed: | ||||||||||||||||||||
| msg = ( | ||||||||||||||||||||
| f"tmt command has failed for test case {test_case}." | ||||||||||||||||||||
| f"Look at log in attachment called '{test_case}-log.txt'." | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| self.data_dict["tmt"]["msg"].append(msg) | ||||||||||||||||||||
phracek marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| if is_running: | ||||||||||||||||||||
| dictionary_key = "tmt_running" | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| if is_failed: | ||||||||||||||||||||
| dictionary_key = "tmt_failed" | ||||||||||||||||||||
| self.data_dict["tmt"][dictionary_key].append(test_case) | ||||||||||||||||||||
| log_path = self.reports_dir / test_case / "tmt-verbose-log" | ||||||||||||||||||||
| log_name = path_dir / "tmt-verbose-log.txt" | ||||||||||||||||||||
| self.send_file_to_pastebin(log_path=log_path, log_name=log_name) | ||||||||||||||||||||
| if log_name.exists(): | ||||||||||||||||||||
| with open(log_name) as f: | ||||||||||||||||||||
| print(f.readlines()) | ||||||||||||||||||||
| self.data_dict["tmt"][dictionary_key].append(test_case) | ||||||||||||||||||||
phracek marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| self.data_dict["tmt"]["logs"].append((test_case, log_path, log_name)) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def collect_data(self): | ||||||||||||||||||||
|
|
@@ -277,7 +312,7 @@ def collect_data(self): | |||||||||||||||||||
| # /var/tmp/daily_scl_tests/<test_case>/log.txt file | ||||||||||||||||||||
| if (path_dir / "tmt_failed").exists(): | ||||||||||||||||||||
| print(f"tmt command has failed for test case {test_case}.") | ||||||||||||||||||||
| self.store_tmt_logs_to_dict(path_dir, test_case) | ||||||||||||||||||||
| self.store_tmt_logs_to_dict(path_dir, test_case, is_failed=True) | ||||||||||||||||||||
| failed_tests = True | ||||||||||||||||||||
| continue | ||||||||||||||||||||
| data_dir = path_dir / "results" | ||||||||||||||||||||
|
|
@@ -293,7 +328,7 @@ def collect_data(self): | |||||||||||||||||||
| print(success_logs) | ||||||||||||||||||||
| for suc in success_logs: | ||||||||||||||||||||
| self.store_tmt_logs_to_dict( | ||||||||||||||||||||
| path_dir=path_dir, test_case=test_case | ||||||||||||||||||||
| path_dir=path_dir, test_case=test_case, is_failed=False | ||||||||||||||||||||
| ) | ||||||||||||||||||||
phracek marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
329
to
332
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid repeated pastebin uploads in success loop. Lines [329]-[332] call ♻️ Suggested fix- for suc in success_logs:
- self.store_tmt_logs_to_dict(
- path_dir=path_dir, test_case=test_case, is_failed=False
- )
+ self.store_tmt_logs_to_dict(
+ path_dir=path_dir,
+ test_case=test_case,
+ )📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.2)[warning] 329-329: Loop control variable Rename unused (B007) 🤖 Prompt for AI Agents |
||||||||||||||||||||
| self.data_dict["SUCCESS_DATA"].extend( | ||||||||||||||||||||
| [(test_case, str(f), str(f.name)) for f in success_logs] | ||||||||||||||||||||
|
|
@@ -313,6 +348,11 @@ def collect_data(self): | |||||||||||||||||||
| pprint.pprint(self.data_dict) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def generate_email_body(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Generate email body based on collected data. | ||||||||||||||||||||
| It contains information about failed containers | ||||||||||||||||||||
| and logs from TMT command in case of TMT failures. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| if self.args.upstream_tests: | ||||||||||||||||||||
| body_failure = "<b>NodeJS upstream tests failures:</b><br>" | ||||||||||||||||||||
| body_success = ( | ||||||||||||||||||||
|
|
@@ -346,6 +386,9 @@ def generate_email_body(self): | |||||||||||||||||||
| print(f"Body to email: {self.body}") | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def generate_failed_containers(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Generate email body for failed containers. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| print("GENERATE FAILED CONTAINERS") | ||||||||||||||||||||
| for test_case, plan, msg in self.available_test_case: | ||||||||||||||||||||
| if test_case not in self.data_dict: | ||||||||||||||||||||
|
|
@@ -365,6 +408,9 @@ def generate_failed_containers(self): | |||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def generate_success_containers(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Generate email body for successful containers. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| print("GENERATE SUCCESS CONTAINERS") | ||||||||||||||||||||
| for test_case, cont_path, log_name in self.data_dict["SUCCESS_DATA"]: | ||||||||||||||||||||
| if os.path.exists(log_name): | ||||||||||||||||||||
|
|
@@ -374,6 +420,9 @@ def generate_success_containers(self): | |||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def generate_tmt_logs_containers(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Generate email body for TMT logs. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| for test_case, cont_path, log_name in self.data_dict["tmt"]["logs"]: | ||||||||||||||||||||
| print(f"generate_tmt_logs_containers: {test_case}, {cont_path}, {log_name}") | ||||||||||||||||||||
| if os.path.exists(log_name): | ||||||||||||||||||||
|
|
@@ -390,6 +439,9 @@ def generate_tmt_logs_containers(self): | |||||||||||||||||||
| self.body += "<br>" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def generate_emails(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Generate email list based on collected data and predefined email lists for each container. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| print("generate_emails: ", self.data_dict) | ||||||||||||||||||||
| for test_case, plan, _ in self.available_test_case: | ||||||||||||||||||||
| if test_case not in self.data_dict: | ||||||||||||||||||||
|
|
@@ -405,6 +457,9 @@ def generate_emails(self): | |||||||||||||||||||
| print(f"generate_emails: Additional emails: {DEFAULT_MAILS}") | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def send_emails(self): | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Send emails with the test results. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| if not self.send_email: | ||||||||||||||||||||
| print("Sending email is not allowed") | ||||||||||||||||||||
| return | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.