-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-core: introduce sections to configurations (#3319)
Solving: #3305 The main change is that now we have sections list in Configuration and ConfigurationBuilder instead of having only key-value lists --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dilyan Marinov <[email protected]>
- Loading branch information
1 parent
0f26793
commit bf71644
Showing
12 changed files
with
763 additions
and
349 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# SPDX-License-Identifier: Apache-2.0 | ||
import os | ||
from email.message import Message | ||
from random import randint | ||
from unittest import mock | ||
|
||
from click.testing import Result | ||
|
@@ -18,7 +19,6 @@ | |
|
||
def __get_smtp_env(smtpd: SMTPDFix): | ||
# https://github.com/bebleo/smtpdfix#using | ||
smtpd.config.host = "127.0.0.1" | ||
env = { | ||
"VDK_NOTIFICATION_ENABLED": "true", | ||
"VDK_ENABLE_ATTEMPT_NOTIFICATIONS": "true", | ||
|
@@ -33,88 +33,101 @@ def __get_smtp_env(smtpd: SMTPDFix): | |
return env | ||
|
||
|
||
def test_run_successfull(smtpd: SMTPDFix): | ||
def test_run_successfull(): | ||
errors.resolvable_context().clear() | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(smtpd), | ||
"VDK_NOTIFIED_ON_JOB_SUCCESS": "[email protected]", | ||
}, | ||
): | ||
runner = CliEntryBasedTestRunner() | ||
|
||
result: Result = runner.invoke(["run", util.job_path("simple-job")]) | ||
port = randint(10000, 20000) | ||
host = "127.0.0.1" | ||
with SMTPDFix(host, port) as server: | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(server), | ||
"VDK_NOTIFIED_ON_JOB_SUCCESS": "[email protected]", | ||
}, | ||
): | ||
runner = CliEntryBasedTestRunner() | ||
|
||
cli_assert_equal(0, result) | ||
result: Result = runner.invoke(["run", util.job_path("simple-job")]) | ||
|
||
assert len(smtpd.messages) == 1 | ||
message: Message = smtpd.messages[0] | ||
assert "[email protected]" == message.get("To") | ||
assert "simple-job" in message.get("Subject") | ||
cli_assert_equal(0, result) | ||
|
||
assert len(server.messages) == 1 | ||
message: Message = server.messages[0] | ||
assert "[email protected]" == message.get("To") | ||
assert "simple-job" in message.get("Subject") | ||
|
||
def test_run_successfull_notify_multiple_users(smtpd: SMTPDFix): | ||
|
||
def test_run_successfull_notify_multiple_users(): | ||
errors.resolvable_context().clear() | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(smtpd), | ||
"VDK_NOTIFIED_ON_JOB_SUCCESS": "[email protected];[email protected];[email protected]", | ||
}, | ||
): | ||
runner = CliEntryBasedTestRunner() | ||
port = randint(10000, 20000) | ||
host = "127.0.0.1" | ||
with SMTPDFix(host, port) as server: | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(server), | ||
"VDK_NOTIFIED_ON_JOB_SUCCESS": "[email protected];[email protected];[email protected]", | ||
}, | ||
): | ||
runner = CliEntryBasedTestRunner() | ||
|
||
result: Result = runner.invoke(["run", util.job_path("simple-job")]) | ||
result: Result = runner.invoke(["run", util.job_path("simple-job")]) | ||
|
||
cli_assert_equal(0, result) | ||
cli_assert_equal(0, result) | ||
|
||
assert len(smtpd.messages) == 1 | ||
message: Message = smtpd.messages[0] | ||
assert "[email protected]" in message.get("To") | ||
assert "[email protected]" in message.get("To") | ||
assert "[email protected]" in message.get("To") | ||
assert len(server.messages) == 1 | ||
message: Message = server.messages[0] | ||
assert "[email protected]" in message.get("To") | ||
assert "[email protected]" in message.get("To") | ||
assert "[email protected]" in message.get("To") | ||
|
||
|
||
@mock.patch.dict(os.environ, {"VDK_DB_DEFAULT_TYPE": DB_TYPE_SQLITE_MEMORY}) | ||
def test_run_query_failed_user_error_notification_sent(smtpd: SMTPDFix): | ||
def test_run_query_failed_user_error_notification_sent(): | ||
errors.resolvable_context().clear() | ||
db_plugin = DecoratedSqLite3MemoryDbPlugin() | ||
runner = CliEntryBasedTestRunner(db_plugin) | ||
|
||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(smtpd), | ||
"VDK_NOTIFIED_ON_JOB_FAILURE_USER_ERROR": "[email protected]", | ||
}, | ||
): | ||
result: Result = runner.invoke( | ||
["run", util.job_path("simple-create-insert-failed")] | ||
) | ||
port = randint(10000, 20000) | ||
host = "127.0.0.1" | ||
with SMTPDFix(host, port) as server: | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(server), | ||
"VDK_NOTIFIED_ON_JOB_FAILURE_USER_ERROR": "[email protected]", | ||
}, | ||
): | ||
result: Result = runner.invoke( | ||
["run", util.job_path("simple-create-insert-failed")] | ||
) | ||
|
||
cli_assert_equal(1, result) | ||
assert len(smtpd.messages) == 1 | ||
cli_assert_equal(1, result) | ||
assert len(server.messages) == 1 | ||
|
||
|
||
@mock.patch.dict(os.environ, {"VDK_DB_DEFAULT_TYPE": DB_TYPE_SQLITE_MEMORY}) | ||
def test_run_query_failed_user_error_no_notification_configured(smtpd: SMTPDFix): | ||
def test_run_query_failed_user_error_no_notification_configured(): | ||
errors.resolvable_context().clear() | ||
db_plugin = DecoratedSqLite3MemoryDbPlugin() | ||
runner = CliEntryBasedTestRunner(db_plugin) | ||
|
||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(smtpd), | ||
# our job will fail with user error but we have configure platform error | ||
# so we should NOT get mail | ||
"NOTIFIED_ON_JOB_FAILURE_PLATFORM_ERROR": "[email protected]", | ||
}, | ||
): | ||
result: Result = runner.invoke( | ||
["run", util.job_path("simple-create-insert-failed")] | ||
) | ||
|
||
cli_assert_equal(1, result) | ||
assert len(smtpd.messages) == 0 | ||
port = randint(10000, 20000) | ||
host = "127.0.0.1" | ||
with SMTPDFix(host, port) as server: | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
**__get_smtp_env(server), | ||
# our job will fail with user error but we have configure platform error | ||
# so we should NOT get mail | ||
"NOTIFIED_ON_JOB_FAILURE_PLATFORM_ERROR": "[email protected]", | ||
}, | ||
): | ||
result: Result = runner.invoke( | ||
["run", util.job_path("simple-create-insert-failed")] | ||
) | ||
|
||
cli_assert_equal(1, result) | ||
assert len(server.messages) == 0 |
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
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
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
Oops, something went wrong.