From 9ff9999e7c3421ca78899909ee8953974c723e43 Mon Sep 17 00:00:00 2001 From: nolan1999 Date: Mon, 13 Oct 2025 23:47:16 +0200 Subject: [PATCH 1/4] [FIX] Fix AWS tests cleanup --- tests/conftest.py | 24 ++++++++++++++++++++---- tests/integration/conftest.py | 6 ++---- tests/unit/core/test_filesystem.py | 2 +- tests/unit/core/test_queue.py | 1 + 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c000d9f..c1da77f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -143,6 +143,9 @@ def cleanup(self) -> None: self.ec2_client.revoke_security_group_ingress(**self.vpc_sg_rule_params) def delete(self) -> None: + # never used (AWS tests skipped) + if not hasattr(self, "rds_client"): + return self.rds_client.delete_db_instance( DBInstanceIdentifier=self.db_name, SkipFinalSnapshot=True, @@ -189,20 +192,33 @@ def cleanup(self) -> bool: return True def delete(self) -> None: + # never used (AWS tests skipped) + if not hasattr(self, "s3_client"): + return exists = self.cleanup() if exists: self.s3_client.delete_bucket(Bucket=self.bucket_name) @pytest.fixture(scope="session") -def rds_testing_instance() -> RDSTestingInstance: - return RDSTestingInstance("decodecloudintegrationtestsworkerapi") +def rds_testing_instance() -> Generator[RDSTestingInstance, Any, None]: + # tests themselves must create the instance by calling instance.create(); + # this way, if no test that needs the DB is run, no RDS instance is created + # instance.delete() only deletes the RDS instance if it was created + instance = RDSTestingInstance("decodecloudintegrationtestsworkerapi") + yield instance + instance.delete() @pytest.fixture(scope="session") -def s3_testing_bucket() -> S3TestingBucket: +def s3_testing_bucket() -> Generator[S3TestingBucket, Any, None]: + # tests themselves must create the bucket by calling bucket.create(); + # this way, if no test that needs the bucket is run, no S3 bucket is created + # bucket.delete() only deletes the S3 bucket if it was created bucket_suffix = datetime.datetime.now(datetime.UTC).strftime("%Y%m%d%H%M%S") - return S3TestingBucket(bucket_suffix) + bucket = S3TestingBucket(bucket_suffix) + yield bucket + bucket.delete() @pytest.mark.aws diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 8bb0c4c..5102442 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -48,8 +48,8 @@ def env( s3_testing_bucket.create() yield env if env == "aws": - rds_testing_instance.delete() - s3_testing_bucket.delete() + rds_testing_instance.cleanup() + s3_testing_bucket.cleanup() @pytest.fixture(scope="session") @@ -104,8 +104,6 @@ def queue( queue.create(err_on_exists=True) yield queue queue.delete() - if env == "aws": - rds_testing_instance.cleanup() @pytest.fixture(scope="session", autouse=True) diff --git a/tests/unit/core/test_filesystem.py b/tests/unit/core/test_filesystem.py index 504d11d..80f847e 100644 --- a/tests/unit/core/test_filesystem.py +++ b/tests/unit/core/test_filesystem.py @@ -240,7 +240,7 @@ def base_filesystem( yield S3Filesystem( s3_testing_bucket.s3_client, s3_testing_bucket.bucket_name ) - s3_testing_bucket.delete() + s3_testing_bucket.cleanup() @pytest.fixture(scope="class", autouse=True) def data_file1( diff --git a/tests/unit/core/test_queue.py b/tests/unit/core/test_queue.py index 32d2b7d..96bd109 100644 --- a/tests/unit/core/test_queue.py +++ b/tests/unit/core/test_queue.py @@ -304,5 +304,6 @@ class TestRDSAWSQueue(_TestRDSQueue): def base_queue( self, rds_testing_instance: RDSTestingInstance ) -> Generator[RDSJobQueue, Any, None]: + rds_testing_instance.create() yield RDSJobQueue(rds_testing_instance.db_url) rds_testing_instance.cleanup() From 89012c004eee43661eccd5d256502d3bb35d73e7 Mon Sep 17 00:00:00 2001 From: nolan1999 Date: Sun, 2 Nov 2025 23:36:44 +0100 Subject: [PATCH 2/4] fix --- tests/integration/endpoints/test_jobs.py | 13 +++++++++++-- tests/unit/core/test_queue.py | 8 +++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/integration/endpoints/test_jobs.py b/tests/integration/endpoints/test_jobs.py index 275cad0..3243c44 100644 --- a/tests/integration/endpoints/test_jobs.py +++ b/tests/integration/endpoints/test_jobs.py @@ -9,6 +9,7 @@ import requests from fastapi.testclient import TestClient +from tests.conftest import RDSTestingInstance from tests.integration.endpoints.conftest import EndpointParams, _TestEndpoint from workerfacing_api.core.filesystem import FileSystem, LocalFilesystem, S3Filesystem from workerfacing_api.core.queue import RDSJobQueue @@ -60,8 +61,16 @@ def passing_params(self) -> list[EndpointParams]: return [EndpointParams("get", params={"memory": 1})] @pytest.fixture(scope="function", autouse=True) - def cleanup_queue(self, queue: RDSJobQueue) -> None: - queue.delete() + def cleanup_queue( + self, + queue: RDSJobQueue, + env: str, + rds_testing_instance: RDSTestingInstance, + ) -> None: + if env == "local": + queue.delete() + else: + rds_testing_instance.cleanup() queue.create() @pytest.fixture(scope="function") diff --git a/tests/unit/core/test_queue.py b/tests/unit/core/test_queue.py index 96bd109..605c4fc 100644 --- a/tests/unit/core/test_queue.py +++ b/tests/unit/core/test_queue.py @@ -65,7 +65,6 @@ def queue( ) -> Generator[JobQueue, Any, None]: # function-scoped, clears the queue (delete) # and re-initializes it (create) before every test - base_queue.delete() success = False for _ in range(10): # i.p. SQS, RDS, etc. might need some time to delete try: @@ -77,7 +76,6 @@ def queue( if not success: raise RuntimeError("Could not create queue") yield base_queue - base_queue.delete() @pytest.fixture def job_filter(self) -> JobFilter: @@ -168,6 +166,7 @@ def base_queue( queue_path = str(tmpdir_factory.mktemp("queue") / "queue.pkl") base_queue = LocalJobQueue(queue_path) yield base_queue + base_queue.delete() class TestSQSQueue(_TestJobQueue): @@ -181,7 +180,9 @@ def mock_aws_(self, request: pytest.FixtureRequest) -> bool: def base_queue(self, mock_aws_: bool) -> Generator[SQSJobQueue, Any, None]: context_manager = mock_aws if mock_aws_ else nullcontext with context_manager(): - yield SQSJobQueue(boto3.client("sqs", "eu-central-1")) + base_queue = SQSJobQueue(boto3.client("sqs", "eu-central-1")) + yield base_queue + base_queue.delete() class _TestRDSQueue(_TestJobQueue, abc.ABC): @@ -296,6 +297,7 @@ def base_queue( ) -> Generator[RDSJobQueue, Any, None]: base_queue = RDSJobQueue(f"sqlite:///{tmpdir_factory.mktemp('queue')}/local.db") yield base_queue + base_queue.delete() @pytest.mark.aws From 4e3ee60425f91fdc689171f5f0e2526cc5eeff13 Mon Sep 17 00:00:00 2001 From: nolan1999 Date: Thu, 1 Jan 2026 20:31:27 +0100 Subject: [PATCH 3/4] re-add necessary delete calls --- tests/unit/core/test_queue.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/unit/core/test_queue.py b/tests/unit/core/test_queue.py index 605c4fc..5d527f7 100644 --- a/tests/unit/core/test_queue.py +++ b/tests/unit/core/test_queue.py @@ -65,6 +65,7 @@ def queue( ) -> Generator[JobQueue, Any, None]: # function-scoped, clears the queue (delete) # and re-initializes it (create) before every test + base_queue.delete() success = False for _ in range(10): # i.p. SQS, RDS, etc. might need some time to delete try: @@ -76,6 +77,7 @@ def queue( if not success: raise RuntimeError("Could not create queue") yield base_queue + base_queue.delete() @pytest.fixture def job_filter(self) -> JobFilter: @@ -180,9 +182,7 @@ def mock_aws_(self, request: pytest.FixtureRequest) -> bool: def base_queue(self, mock_aws_: bool) -> Generator[SQSJobQueue, Any, None]: context_manager = mock_aws if mock_aws_ else nullcontext with context_manager(): - base_queue = SQSJobQueue(boto3.client("sqs", "eu-central-1")) - yield base_queue - base_queue.delete() + yield SQSJobQueue(boto3.client("sqs", "eu-central-1")) class _TestRDSQueue(_TestJobQueue, abc.ABC): @@ -295,9 +295,7 @@ class TestRDSLocalQueue(_TestRDSQueue): def base_queue( self, tmpdir_factory: pytest.TempdirFactory ) -> Generator[RDSJobQueue, Any, None]: - base_queue = RDSJobQueue(f"sqlite:///{tmpdir_factory.mktemp('queue')}/local.db") - yield base_queue - base_queue.delete() + yield RDSJobQueue(f"sqlite:///{tmpdir_factory.mktemp('queue')}/local.db") @pytest.mark.aws From 22b4a06da7e78f4d8289827741385599327749c3 Mon Sep 17 00:00:00 2001 From: nolan1999 Date: Thu, 1 Jan 2026 23:00:40 +0100 Subject: [PATCH 4/4] minimal fix --- tests/integration/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5102442..fd58219 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -103,7 +103,6 @@ def queue( queue = RDSJobQueue(rds_testing_instance.db_url) queue.create(err_on_exists=True) yield queue - queue.delete() @pytest.fixture(scope="session", autouse=True)