"""Lambda test module.""" from unittest.mock import patch import pytest from fargate_redeploy import app @pytest.fixture() def patch_ecs_client(ecs_client, monkeypatch): """Patch the global ECS client with a mocked one.""" monkeypatch.setattr(app, "ecs_client", ecs_client) return ecs_client def test_force_redeploy_service(patch_ecs_client, ecs_service, update_ecs_service_response): cluster, service = ecs_service with patch.object(patch_ecs_client, "update_service", return_value=update_ecs_service_response): deployment_id = app.force_redeploy_service(cluster=cluster, service=service) assert deployment_id == "ecs-svc/1234567" def test_is_deploying_true(patch_ecs_client, ecs_service, describe_services_response): cluster, service = ecs_service with patch.object(patch_ecs_client, "describe_services", return_value=describe_services_response): is_deploying = app.is_deploying(cluster=cluster, service=service) assert is_deploying def test_is_deploying_false(patch_ecs_client, ecs_service): cluster, service = ecs_service is_deploying = app.is_deploying(cluster=cluster, service=service) assert not is_deploying def test_wait_deployment(patch_ecs_client, ecs_service): cluster, service = ecs_service with patch.object(patch_ecs_client, "get_waiter") as mock_get_waiter: mock_waiter = mock_get_waiter.return_value mock_waiter.wait.return_value = None app.wait_deployment(cluster=cluster, service=service) mock_get_waiter.assert_called_with("services_stable") mock_waiter.wait.assert_called_once() """ def test_get_ip_addresses_invalid(patch_s3_client, s3_bucket_name, invalid_s3_object): with pytest.raises(ValueError) as exc_info: _ = app.get_ip_addresses(s3_bucket_name, invalid_s3_object) assert "does not appear to be an IPv4 or IPv6 network" in str(exc_info.value) def test_get_ip_set_by_name_cloudfront(wafv2_client, cloudfront_ip_set): ip_set = app.get_ip_set_by_name(wafv2_client, cloudfront_ip_set["Name"], "CLOUDFRONT") assert ip_set["ARN"].startswith(f"arn:aws:wafv2:us-east-1:123456789012:global/ipset/{Config.DEFAULT_IP_SET_NAME}") assert ip_set["Name"] == Config.DEFAULT_IP_SET_NAME def test_get_ip_set_by_name_regional(wafv2_client, regional_ip_set): ip_set = app.get_ip_set_by_name(wafv2_client, regional_ip_set["Name"], "REGIONAL") assert ip_set["ARN"].startswith(f"arn:aws:wafv2:us-east-1:123456789012:regional/ipset/{Config.DEFAULT_IP_SET_NAME}") assert ip_set["Name"] == Config.DEFAULT_IP_SET_NAME def test_get_ip_set_by_name_empty(wafv2_client): with pytest.raises(ValueError) as exc_info: _ = app.get_ip_set_by_name(wafv2_client, "non-existent-set", "REGIONAL") assert "IP Set non-existent-set is not found." in str(exc_info.value) def test_update_waf_ip_set_cloudfront(wafv2_client, cloudfront_ip_set, valid_ip_list): app.update_waf_ip_set(wafv2_client, valid_ip_list, "CLOUDFRONT", cloudfront_ip_set["Name"]) ip_set = wafv2_client.get_ip_set(Name=cloudfront_ip_set["Name"], Id=cloudfront_ip_set["Id"], Scope="CLOUDFRONT") assert ip_set["IPSet"]["Addresses"] == valid_ip_list def test_update_waf_ip_set_regional(wafv2_client, regional_ip_set, valid_ip_list): app.update_waf_ip_set(wafv2_client, valid_ip_list, "REGIONAL", regional_ip_set["Name"]) ip_set = wafv2_client.get_ip_set(Name=regional_ip_set["Name"], Id=regional_ip_set["Id"], Scope="REGIONAL") assert ip_set["IPSet"]["Addresses"] == valid_ip_list assert ip_set["IPSet"]["Description"] == "Test set" def test_update_waf_ip_set_regional_empty_desc(wafv2_client, regional_ip_set_empty_desc, valid_ip_list): app.update_waf_ip_set(wafv2_client, valid_ip_list, "REGIONAL", regional_ip_set_empty_desc["Name"]) ip_set = wafv2_client.get_ip_set( Name=regional_ip_set_empty_desc["Name"], Id=regional_ip_set_empty_desc["Id"], Scope="REGIONAL" ) assert ip_set["IPSet"]["Addresses"] == valid_ip_list assert "Description" not in ip_set["IPSet"] """