Skip to content

Commit

Permalink
IN-923 Raise exceptions for failed connection tests
Browse files Browse the repository at this point in the history
Why these changes are being introduced:
* Raising exceptions for any failed connection tests stops the application
from proceeding with the workflow once a connection test fails.
This logic makes sense as the (1) a successful connection to the Data Warehouse
is required for any Carbon run and (2) a successful connection
to the Elements FTP server is required for any run Carbon run that involves FTP.

How this addresses that need:
* Exceptions are raised in the connection test functions of
carbon.database.DatabaseEngine and
carbon.app.DatabaseToFtpPipe.
* CLI tests for failed connection tests are properly configured.

Side effects of this change:
* None

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/IN-923
  • Loading branch information
jonavellecuerdo committed Sep 20, 2023
1 parent 49e9155 commit 4744fb3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions carbon/app.py
Expand Up @@ -244,6 +244,7 @@ def run_connection_test(self) -> None:
f"Failed to connect to the Symplectic Elements FTP server: {error}"
)
logger.exception(error_message)
raise
else:
logger.info("Successfully connected to the Symplectic Elements FTP server")
ftps.quit()
1 change: 1 addition & 0 deletions carbon/database.py
Expand Up @@ -114,6 +114,7 @@ def run_connection_test(self) -> None:
except Exception as error:
error_message = f"Failed to connect to the Data Warehouse: {error}"
logger.exception(error_message)
raise
else:
dbapi_connection = connection.connection
version = (
Expand Down
20 changes: 14 additions & 6 deletions tests/test_cli.py
Expand Up @@ -188,11 +188,19 @@ def test_cli_connection_tests_success(caplog, functional_engine, runner):
assert "Successfully connected to the Symplectic Elements FTP server" in caplog.text


def test_cli_connection_tests_fail(
caplog, ftp_server, monkeypatch, nonfunctional_engine, runner
def test_cli_database_connection_test_fails(caplog, nonfunctional_engine, runner):
with patch("carbon.cli.DatabaseEngine") as mocked_engine:
mocked_engine.return_value = nonfunctional_engine
result = runner.invoke(main, ["--run_connection_tests"])
assert result.exit_code == 1

assert "Failed to connect to the Data Warehouse" in caplog.text


def test_cli_ftp_connection_test_fails(
caplog, ftp_server, functional_engine, monkeypatch, runner
):
ftp_socket, _ = ftp_server

monkeypatch.setenv(
"SYMPLECTIC_FTP_JSON",
(
Expand All @@ -202,10 +210,10 @@ def test_cli_connection_tests_fail(
'"SYMPLECTIC_FTP_PASS": "invalid_password"}'
),
)

with patch("carbon.cli.DatabaseEngine") as mocked_engine:
mocked_engine.return_value = nonfunctional_engine
mocked_engine.return_value = functional_engine
result = runner.invoke(main, ["--run_connection_tests"])
assert result.exit_code == 0
assert result.exit_code == 1

assert "Failed to connect to the Data Warehouse" in caplog.text
assert "Failed to connect to the Symplectic Elements FTP server" in caplog.text

0 comments on commit 4744fb3

Please sign in to comment.