diff --git a/pytest-saas/doc/changes/unreleased.md b/pytest-saas/doc/changes/unreleased.md index 79e701b..41e7ecf 100644 --- a/pytest-saas/doc/changes/unreleased.md +++ b/pytest-saas/doc/changes/unreleased.md @@ -1 +1,5 @@ # Unreleased + +## Feature + +* #33: Added configurable idle time for saas-database diff --git a/pytest-saas/exasol/pytest_saas/__init__.py b/pytest-saas/exasol/pytest_saas/__init__.py index 4b788ab..4f0a506 100644 --- a/pytest-saas/exasol/pytest_saas/__init__.py +++ b/pytest-saas/exasol/pytest_saas/__init__.py @@ -1,8 +1,8 @@ import os -from pathlib import Path +from datetime import timedelta import pytest -from exasol.saas.client import openapi +from exasol.saas import client as saas_client from exasol.saas.client.api_access import ( OpenApiAccess, create_saas_client, @@ -33,6 +33,16 @@ def pytest_addoption(parser): pytest plugin for exasol-saas-api will include this short tag into the names of created database instances.""", ) + parser.addoption( + "--saas-max-idle-hours", + action="store", + default=saas_client.Limits.AUTOSTOP_DEFAULT_IDLE_TIME.total_seconds() / 3600, + help=""" + The SaaS cluster would normally stop after a certain period of inactivity. + The default period is 2 hours. For some tests, this period is too short. + Use this parameter to set a sufficient idle period in the number of hours. + """ + ) def _env(var: str) -> str: @@ -80,17 +90,20 @@ def api_access(saas_host, saas_pat, saas_account_id) -> OpenApiAccess: @pytest.fixture(scope="session") def saas_database( request, api_access, database_name -) -> openapi.models.database.Database: +) -> saas_client.openapi.models.database.Database: """ Note: The SaaS instance database returned by this fixture initially 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") + idle_hours = float(request.config.getoption("--saas-max-idle-hours")) + if db_id: yield api_access.get_database(db_id) return - with api_access.database(database_name, keep) as db: + with api_access.database(database_name, keep, + idle_time=timedelta(hours=idle_hours)) as db: yield db diff --git a/pytest-saas/test/integration/pytest_saas_test.py b/pytest-saas/test/integration/pytest_saas_test.py index 1a69dbe..8da857e 100644 --- a/pytest-saas/test/integration/pytest_saas_test.py +++ b/pytest-saas/test/integration/pytest_saas_test.py @@ -4,6 +4,7 @@ from unittest import mock import pytest +from exasol.saas import client as saas_client pytest_plugins = "pytester" @@ -32,10 +33,13 @@ def _env(**kwargs): @pytest.mark.parametrize( "files,cli_args", [ - ( _testfile(""" + ( _testfile(f""" def test_no_cli_args(request): + import pytest assert not request.config.getoption("--keep-saas-database") assert request.config.getoption("--saas-database-id") is None + assert float(request.config.getoption("--saas-max-idle-hours")) * 3600 == \ + pytest.approx({saas_client.Limits.AUTOSTOP_DEFAULT_IDLE_TIME.total_seconds()}) """), _cli_args(), ), @@ -45,11 +49,13 @@ def test_cli_args(request): assert request.config.getoption("--keep-saas-database") assert "123" == request.config.getoption("--saas-database-id") assert "PST" == request.config.getoption("--project-short-tag") + assert "3.5" == request.config.getoption("--saas-max-idle-hours") """), _cli_args( "--keep-saas-database", "--project-short-tag", "PST", "--saas-database-id", "123", + "--saas-max-idle-hours", "3.5", ), ), ])