From d727175423659b639f0b2dae3f38e767ec0a23d9 Mon Sep 17 00:00:00 2001 From: Christoph Kuhnke Date: Mon, 3 Jun 2024 11:21:28 +0200 Subject: [PATCH] #18: Implemented keep alive for saas-database (#20) --- pytest-saas/doc/changes/unreleased.md | 8 +++++++ pytest-saas/exasol/pytest_saas/__init__.py | 15 ++++++------ .../test/integration/pytest_saas_test.py | 24 +++++++++++++++++-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/pytest-saas/doc/changes/unreleased.md b/pytest-saas/doc/changes/unreleased.md index 79e701b..d66fd04 100644 --- a/pytest-saas/doc/changes/unreleased.md +++ b/pytest-saas/doc/changes/unreleased.md @@ -1 +1,9 @@ # Unreleased + +## Feature + +* #18: Implemented keep alive for saas-database + +## Bugfixes + +* #19: Fixed removing ip whitelist rule at the end of the test session diff --git a/pytest-saas/exasol/pytest_saas/__init__.py b/pytest-saas/exasol/pytest_saas/__init__.py index 5527d27..4b788ab 100644 --- a/pytest-saas/exasol/pytest_saas/__init__.py +++ b/pytest-saas/exasol/pytest_saas/__init__.py @@ -14,20 +14,20 @@ def pytest_addoption(parser): parser.addoption( - f"--saas-database-id", + "--saas-database-id", help="""ID of the instance of an existing SaaS database to be used during the current pytest session instead of creating a dedicated instance temporarily.""", ) parser.addoption( - f"--keep-saas-database", + "--keep-saas-database", action="store_true", default=False, help="""Keep the SaaS database instance created for the current pytest session for subsequent inspection or reuse.""", ) parser.addoption( - f"--project-short-tag", + "--project-short-tag", help="""Short tag aka. "abbreviation" for your current project. See docstring in project_short_tag.py for more details. pytest plugin for exasol-saas-api will include this short tag into @@ -86,16 +86,17 @@ def saas_database( will not be operational. The startup takes about 20 minutes. """ db_id = request.config.getoption("--saas-database-id") + keep = request.config.getoption("--keep-saas-database") if db_id: yield api_access.get_database(db_id) return - with api_access.database(database_name) as db: + with api_access.database(database_name, keep) as db: yield db @pytest.fixture(scope="session") def operational_saas_database_id(api_access, saas_database) -> str: db = saas_database - api_access.add_allowed_ip() - api_access.wait_until_running(db.id) - return db.id + with api_access.allowed_ip(): + api_access.wait_until_running(db.id) + yield db.id diff --git a/pytest-saas/test/integration/pytest_saas_test.py b/pytest-saas/test/integration/pytest_saas_test.py index ab17aab..1a69dbe 100644 --- a/pytest-saas/test/integration/pytest_saas_test.py +++ b/pytest-saas/test/integration/pytest_saas_test.py @@ -111,7 +111,7 @@ def test_id_of_existing_database(request, pytester, capsys): because that there is no database with the specified ID. """ testname = request.node.name - pytester.makepyfile(** _testfile( f""" + pytester.makepyfile(** _testfile(f""" def {testname}(saas_database): pass """)) @@ -124,9 +124,29 @@ def {testname}(saas_database): @pytest.mark.slow def test_operational_database(request, pytester): testname = request.node.name - pytester.makepyfile(** _testfile( f""" + pytester.makepyfile(** _testfile(f""" def {testname}(operational_saas_database_id): assert operational_saas_database_id is not None """)) result = pytester.runpytest() assert result.ret == pytest.ExitCode.OK + + +def test_keep_database(request, pytester, api_access, capsys): + testname = request.node.name + pytester.makepyfile(** _testfile(f""" + def {testname}(saas_database): + db = saas_database + print(f"\\ndatabase-id: {{db.id}}") + """)) + id = None + try: + result = pytester.runpytest("--keep-saas-database", "-s") + assert result.ret == pytest.ExitCode.OK + captured = capsys.readouterr() + for line in captured.out.splitlines(): + if line.startswith("database-id: "): + id = line.split()[1] + finally: + if id: + api_access.delete_database(id)