From 441d826c80bd8580ae074209ad7bd75c4cc99717 Mon Sep 17 00:00:00 2001 From: Renato Valenzuela Date: Thu, 28 Sep 2023 16:08:53 +0000 Subject: [PATCH] fix: Fix integration tests for remote test events --- .../remote_test_event_integ_base.py | 30 ++++++++++++------- .../test_event/test_remote_test_event.py | 10 +++++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/tests/integration/remote/test_event/remote_test_event_integ_base.py b/tests/integration/remote/test_event/remote_test_event_integ_base.py index 956fdc618d..e4d7672849 100644 --- a/tests/integration/remote/test_event/remote_test_event_integ_base.py +++ b/tests/integration/remote/test_event/remote_test_event_integ_base.py @@ -32,17 +32,7 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): # Delete remaining test events if there were not deleted during tests - for _, resource in cls.stack_resource_summaries.items(): - if resource.resource_type == AWS_LAMBDA_FUNCTION: - schema_name = f"_{resource.physical_resource_id}-schema" - try: - cls.schemas_client.delete_schema( - RegistryName=LAMBDA_TEST_EVENT_REGISTRY, - SchemaName=schema_name, - ) - LOG.info("Deleted lingering schema for test events: %s", schema_name) - except Exception as e: # Ignore if it doesn't exist (it was correctly deleted during tests) - pass + cls.delete_all_test_events() # Delete the deployed stack cls.cfn_client.delete_stack(StackName=cls.stack_name) @@ -59,6 +49,24 @@ def create_resources_and_boto_clients(cls): cls.cfn_client = boto_client_provider("cloudformation") cls.schemas_client = boto_client_provider("schemas") + @classmethod + def delete_all_test_events(cls, logical_id=None): + for _, resource in cls.stack_resource_summaries.items(): + if resource.resource_type == AWS_LAMBDA_FUNCTION: + # If a logical id is passed, delete only that one + if logical_id and logical_id != resource.logical_resource_id: + continue + schema_name = f"_{resource.physical_resource_id}-schema" + try: + cls.schemas_client.delete_schema( + RegistryName=LAMBDA_TEST_EVENT_REGISTRY, + SchemaName=schema_name, + ) + LOG.info("Deleted lingering schema for test events: %s", schema_name) + except Exception as e: # Ignore if it doesn't exist (it was correctly deleted during tests) + LOG.debug("No events deleted (this is good) %s", e) + pass + @staticmethod def get_command_list( subcommand, diff --git a/tests/integration/remote/test_event/test_remote_test_event.py b/tests/integration/remote/test_event/test_remote_test_event.py index 9b12bd0b86..a2858d2771 100644 --- a/tests/integration/remote/test_event/test_remote_test_event.py +++ b/tests/integration/remote/test_event/test_remote_test_event.py @@ -25,6 +25,8 @@ def setUpClass(cls): def test_no_events(self): function_name = "HelloWorldFunction1" + # Make sure the state is clean + TestRemoteTestEvent.delete_all_test_events(function_name) self.list_events_and_check( self.stack_name, function_name, @@ -41,6 +43,8 @@ def test_no_events(self): def test_event_workflow(self): function_to_check = "HelloWorldFunction2" + # Make sure the state is clean + TestRemoteTestEvent.delete_all_test_events(function_to_check) event_contents1 = {"key1": "Hello", "key2": "serverless", "key3": "world"} event_contents2 = {"a": "A", "b": "B", "c": "C"} @@ -99,7 +103,8 @@ def list_events_and_check(self, stack_name, resource_id, expected_output, expect output = list_result.stdout.strip() error_output = list_result.stderr.strip() self.assertEqual(output.decode("utf-8"), expected_output) - self.assertEqual(error_output.decode("utf-8"), expected_error) + if expected_error: + self.assertIn(expected_error, error_output.decode("utf-8")) def delete_event_and_check(self, stack_name, resource_id, test_event_name): command_list = self.get_command_list( @@ -121,4 +126,5 @@ def get_event_and_check(self, stack_name, resource_id, event_name, expected_outp output = list_result.stdout.strip() error_output = list_result.stderr.strip() self.assertEqual(output.decode("utf-8"), expected_output) - self.assertEqual(error_output.decode("utf-8"), expected_error) + if expected_error: + self.assertIn(expected_error, error_output.decode("utf-8"))