Skip to content

Commit

Permalink
#33 Added configurable idle time for a SaaS database (#36)
Browse files Browse the repository at this point in the history
* #33 Added configurable idle time for a SaaS database

* #33 Added configurable idle time for a SaaS database

* #33 Added configurable idle time for a SaaS database

* #33 Changed the cli argument name
  • Loading branch information
ahsimb authored Jul 31, 2024
1 parent f94c8a2 commit 3637087
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pytest-saas/doc/changes/unreleased.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Unreleased

## Feature

* #33: Added configurable idle time for saas-database
21 changes: 17 additions & 4 deletions pytest-saas/exasol/pytest_saas/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down
8 changes: 7 additions & 1 deletion pytest-saas/test/integration/pytest_saas_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import mock

import pytest
from exasol.saas import client as saas_client

pytest_plugins = "pytester"

Expand Down Expand Up @@ -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(),
),
Expand All @@ -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",
),
),
])
Expand Down

0 comments on commit 3637087

Please sign in to comment.