-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Irene Simo Munoz
committed
Dec 20, 2024
1 parent
7062b41
commit 68f491a
Showing
1 changed file
with
53 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from pathlib import Path | ||
from autosubmit.notifications.mail_notifier import MailNotifier | ||
from autosubmitconfigparser.config.basicconfig import BasicConfig | ||
from log.log import Log | ||
|
||
@pytest.fixture | ||
def mock_basic_config(): | ||
|
@@ -43,8 +44,27 @@ def test_attach_files(mail_notifier): | |
mail_notifier._attach_files(message, mock_files) | ||
|
||
assert message.attach.call_count == len(mock_files) | ||
|
||
def test_notify_experiment_status(mail_notifier): | ||
@pytest.mark.parametrize( | ||
"mock_glob_side_effect, send_mail_side_effect, expected_log_message, expected_call_count", | ||
[ | ||
# Normal case: No errors, should not log anything | ||
(None, None, None, 0), # No logs are expected, everything works fine | ||
# Log connection error: Simulate an error while sending email | ||
(None, Exception("SMTP server error"), | ||
'An error has occurred while sending a mail for warn about remote_platform', 1), | ||
# Log attachment error: Simulate failure in file listing (glob raises exception) | ||
(Exception("Failed to list files"), None, | ||
'An error has occurred while attaching log files to a warning email about remote_platforms ', 1) | ||
], | ||
ids=[ | ||
"Normal case: No errors", | ||
"Log connection error (SMTP server error)", | ||
"Log attachment error (Failed to list files)" | ||
] | ||
) | ||
def test_notify_experiment_status(mail_notifier, mock_glob_side_effect, send_mail_side_effect, expected_log_message, expected_call_count): | ||
original_local_root_dir = BasicConfig.LOCAL_ROOT_DIR | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
BasicConfig.LOCAL_ROOT_DIR = temp_dir | ||
|
@@ -54,30 +74,43 @@ def test_notify_experiment_status(mail_notifier): | |
|
||
file1 = aslogs_dir / "1234_run.log" | ||
file2 = aslogs_dir / "1235_run.log" | ||
|
||
file1.write_text("file data 1") | ||
file2.write_text("file data 2") | ||
|
||
assert file1.exists() | ||
assert file2.exists() | ||
|
||
with mock.patch.object(mail_notifier, '_generate_message_experiment_status', return_value="Test message"): | ||
|
||
with mock.patch.object(mail_notifier, '_send_mail') as mock_send_mail, \ | ||
mock.patch.object(mail_notifier, '_attach_files') as mock_attach_files: | ||
|
||
mail_to = ['[email protected]'] | ||
platform = mock.Mock() | ||
platform.name = "Test Platform" | ||
platform.host = "test.host.com" | ||
|
||
mail_notifier.notify_experiment_status(exp_id, mail_to, platform) | ||
|
||
mail_notifier._generate_message_experiment_status.assert_called_once_with(exp_id, platform) | ||
|
||
mock_send_mail.assert_called_once_with(mail_notifier.config.MAIL_FROM, '[email protected]', mock.ANY) | ||
with mock.patch.object(mail_notifier, '_generate_message_experiment_status', return_value="Test message"), \ | ||
mock.patch.object(mail_notifier, '_send_mail') as mock_send_mail, \ | ||
mock.patch.object(mail_notifier, '_attach_files') as mock_attach_files, \ | ||
mock.patch.object(Log, 'printlog') as mock_printlog: | ||
|
||
mail_to = ['[email protected]'] | ||
platform = mock.Mock() | ||
platform.name = "Test Platform" | ||
platform.host = "test.host.com" | ||
|
||
mock_send_mail.side_effect = send_mail_side_effect | ||
|
||
if mock_glob_side_effect is not None: | ||
# Log attachment error: Simulate failure in file listing (glob raises exception) | ||
with mock.patch.object(Path, 'glob', side_effect = mock_glob_side_effect): | ||
mail_notifier.notify_experiment_status(exp_id, mail_to, platform) | ||
else: | ||
mail_notifier.notify_experiment_status(exp_id, mail_to, platform) | ||
|
||
mail_notifier._generate_message_experiment_status.assert_called_once_with(exp_id, platform) | ||
mock_send_mail.assert_called_once_with(mail_notifier.config.MAIL_FROM, '[email protected]', mock.ANY) | ||
|
||
if mock_glob_side_effect is None: mock_attach_files.assert_called_once_with(mock.ANY, [file1, file2]) | ||
|
||
if expected_log_message: | ||
mock_printlog.assert_called_once_with(expected_log_message, 6011) | ||
log_calls = [call[0][0] for call in mock_printlog.call_args_list] | ||
assert 'Traceback' not in log_calls | ||
else: | ||
mock_printlog.assert_not_called() # No logs should be called for normal execution | ||
|
||
mock_attach_files.assert_called_once_with(mock.ANY, [file1, file2]) | ||
|
||
# Reset the local root dir. | ||
BasicConfig.LOCAL_ROOT_DIR = original_local_root_dir |