Skip to content

Commit 003ed5d

Browse files
authored
ci: add auto test cleanup for hosted_extractors.destinations (#2641)
1 parent 8f0c3e3 commit 003ed5d

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

tests/tests_integration/test_api/test_hosted_extractors/conftest.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,41 @@
2121
UPDATE_SOURCE_PREFIX = "to-update-"
2222
TEST_SOURCE_PREFIXES = (HUB_SOURCE_PREFIX, MQTT_SOURCE_PREFIX, UPDATE_SOURCE_PREFIX)
2323

24+
DESTINATION_PREFIX = "myNewDestination-"
25+
DESTINATION_FOR_TESTING_PREFIX = "myNewDestinationForTesting-"
26+
UPDATE_DESTINATION_PREFIX = "toupdate-"
27+
TEST_DESTINATION_PREFIXES = (DESTINATION_PREFIX, DESTINATION_FOR_TESTING_PREFIX, UPDATE_DESTINATION_PREFIX)
28+
29+
# Sources and Destinatinos are repeat offenders for hitting max limits because they are
30+
# quite low. We add specific cleanup for anything older than...:
31+
DELETE_THRESHOLD = 1 * 60 * 60 * 1000 # 1 hour in ms
32+
NOW_MS = int(time.time() * 1000)
33+
34+
35+
@pytest.fixture(scope="session", autouse=True)
36+
def cleanup_stale_test_destinations(cognite_client: CogniteClient) -> None:
37+
# max is low number like e.g. 50 per project, safe to list all:
38+
all_destinations = cognite_client.hosted_extractors.destinations.list(limit=None)
39+
stale = [
40+
d.external_id
41+
for d in all_destinations
42+
if d.external_id
43+
and d.external_id.startswith(TEST_DESTINATION_PREFIXES)
44+
and NOW_MS - d.created_time >= DELETE_THRESHOLD
45+
]
46+
if stale:
47+
cognite_client.hosted_extractors.destinations.delete(stale, force=True, ignore_unknown_ids=True)
48+
2449

2550
@pytest.fixture(scope="session", autouse=True)
2651
def cleanup_stale_test_sources(cognite_client: CogniteClient) -> None:
2752
all_sources = cognite_client.hosted_extractors.sources.list(limit=None)
28-
now_ms = int(time.time() * 1000)
29-
# Clean up anyting older than 3 hours:
3053
stale = [
3154
s.external_id
3255
for s in all_sources
3356
if s.external_id
3457
and s.external_id.startswith(TEST_SOURCE_PREFIXES)
35-
and now_ms - s.created_time >= 3 * 60 * 60 * 1000 # type: ignore [attr-defined]
58+
and NOW_MS - s.created_time >= DELETE_THRESHOLD # type: ignore [attr-defined]
3659
]
3760
if stale:
3861
cognite_client.hosted_extractors.sources.delete(stale, force=True, ignore_unknown_ids=True)
@@ -58,7 +81,7 @@ def a_data_set(cognite_client: CogniteClient) -> DataSet:
5881
@pytest.fixture
5982
def one_destination(cognite_client: CogniteClient, fresh_session: SessionWrite) -> Iterator[Destination]:
6083
my_dest = DestinationWrite(
61-
external_id=f"myNewDestination-{random_string(10)}",
84+
external_id=f"{DESTINATION_PREFIX}{random_string(10)}",
6285
credentials=fresh_session,
6386
)
6487
created = cognite_client.hosted_extractors.destinations.create(my_dest)

tests/tests_integration/test_api/test_hosted_extractors/test_destinations.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
)
1414
from cognite.client.exceptions import CogniteAPIError
1515
from cognite.client.utils._text import random_string
16+
from tests.tests_integration.test_api.test_hosted_extractors.conftest import (
17+
DESTINATION_FOR_TESTING_PREFIX,
18+
UPDATE_DESTINATION_PREFIX,
19+
)
1620

1721

1822
class TestDestinations:
1923
def test_create_update_retrieve_delete(
2024
self, cognite_client: CogniteClient, fresh_session: SessionWrite, a_data_set: DataSet
2125
) -> None:
2226
my_dest = DestinationWrite(
23-
external_id=f"myNewDestinationForTesting-{random_string(10)}",
27+
external_id=f"{DESTINATION_FOR_TESTING_PREFIX}{random_string(10)}",
2428
credentials=fresh_session,
2529
)
2630
created: Destination | None = None
@@ -56,7 +60,7 @@ def test_update_using_write_object(
5660
self, cognite_client: CogniteClient, fresh_session: SessionWrite, a_data_set: DataSet
5761
) -> None:
5862
my_dest = DestinationWrite(
59-
external_id=f"toupdate-{random_string(10)}",
63+
external_id=f"{UPDATE_DESTINATION_PREFIX}{random_string(10)}",
6064
credentials=fresh_session,
6165
)
6266

0 commit comments

Comments
 (0)