"""Lambda test module.""" from unittest.mock import patch import boto3 import pytest from moto import mock_aws import config from src import app as index @pytest.fixture() def mock_account_id(): """Return an AWS account id.""" return '123456789000' @pytest.fixture() def mock_role(): """Return an AWS role name.""" return 'test-role' @pytest.fixture() @mock_aws def mock_ecs_client(): """Return a mock ECS client.""" return boto3.client('ecs', region_name=config.AWS_DEFAULT_REGION) @mock_aws def test_assume_target_account_role(mock_account_id, mock_role, monkeypatch): """Test assume_target_account_role function.""" aws_credentials = ['AccessKeyId', 'SecretAccessKey', 'SessionToken'] monkeypatch.setattr(index.config, 'EXTERNAL_ID', 'abc1234') result = index.assume_target_account_role(mock_account_id, mock_role) for credential in aws_credentials: assert result[credential] @mock_aws def test_scale_service(mock_ecs_client): """Test scale_service function.""" mock_ecs_client.create_cluster(clusterName='test-cluster') mock_ecs_client.create_service( cluster='test-cluster', serviceName='test-service', desiredCount=1) index.scale_service( ecs_client=mock_ecs_client, cluster_name='test-cluster', service_name='test-service', desired_count=0) response = mock_ecs_client.describe_services( cluster='test-cluster', services=['test-service'] ) assert response['services'][0]['desiredCount'] == 0 @mock_aws def test_scale_out_service(mock_ecs_client): """Test scale_service function.""" mock_ecs_client.create_cluster(clusterName='test-cluster') mock_ecs_client.create_service( cluster='test-cluster', serviceName='test-service', desiredCount=0) index.scale_service( ecs_client=mock_ecs_client, cluster_name='test-cluster', service_name='test-service', desired_count=1, wait=False ) response = mock_ecs_client.describe_services( cluster='test-cluster', services=['test-service'] ) assert response['services'][0]['desiredCount'] == 1 @mock_aws def test_scale_out_dead_service(mock_ecs_client): """Test scale_out_service function.""" mock_ecs_client.create_cluster(clusterName='test-cluster') mock_ecs_client.create_service( cluster='test-cluster', serviceName='test-service', desiredCount=0) event = { 'cluster_name': 'test-cluster', 'service_name': 'test-service', 'task_count': 0 } index.scale_out_service( ecs_client=mock_ecs_client, event=event, wait=False ) response = mock_ecs_client.describe_services( cluster='test-cluster', services=['test-service'] ) assert response['services'][0]['desiredCount'] == 1 @mock_aws def test_scale_out_normal_service(mock_ecs_client): """Test scale_out_service function.""" mock_ecs_client.create_cluster(clusterName='test-cluster') mock_ecs_client.create_service( cluster='test-cluster', serviceName='test-service', desiredCount=0) event = { 'cluster_name': 'test-cluster', 'service_name': 'test-service', 'task_count': 2 } index.scale_out_service( ecs_client=mock_ecs_client, event=event, wait=False ) response = mock_ecs_client.describe_services( cluster='test-cluster', services=['test-service'] ) assert response['services'][0]['desiredCount'] == 2 @mock_aws def test_scale_service_not_found(mock_ecs_client): """Test scale_service function when service does not exist.""" mock_ecs_client.create_cluster(clusterName='test-cluster') with pytest.raises(Exception) as e: index.scale_service( ecs_client=mock_ecs_client, cluster_name='test-cluster', service_name='test-service', desired_count=0) assert str( e) == 'Service test-service does not exist in cluster test-cluster' # noqa @patch('requests.delete') def test_delete_connector_no_debezium_service(mock_call): """Test delete_connector function.""" event = { 'bootstrap_servers': 'localhost:9092', 'topics': ['topic-1', 'topic-2'] } index.delete_connector(event) mock_call.assert_not_called() @patch('requests.delete') def test_delete_connector_with_debezium(mock_call, mocker): """Test delete_connector function.""" mock_response = mock_call.return_value mock_response.status_code = 200 mock_response.json.return_value = {'success': 'result'} service_name = 'qa-kafka-connect-debezium-ar' event = { 'bootstrap_servers': 'localhost:9092', 'topics': ['topic-1', 'topic-2'], 'scale_action': 'in', 'cluster_name': service_name, 'account_id': '437795906767', 'role': 'qa-rds-refresh-restore-role', 'service_name': service_name, } index.delete_connector(event) mock_call.assert_called_once_with( f'https://{service_name}.theorchard.io/connectors/debezium_mysql_source') # noqa def test_reset_kafka_topics(mocker): """Test reset_kafka_topics function.""" mock_kafka_client = mocker.MagicMock() mock_kafka_client.list_topics.return_value = ['topic-2'] mock_delete_topics = mocker.MagicMock() mock_delete_topics.to_object.return_value = {'deleted': 'topic-2'} mock_kafka_client.delete_topics.return_value = mock_delete_topics event = { 'bootstrap_servers': 'localhost:9092', 'topics': ['topic-1', 'topic-2'] } index.reset_kafka_topics(event, mock_kafka_client) mock_kafka_client.delete_topics.assert_called_once_with( topics=['topic-2'] ) def test_reset_kafka_topics_config(mocker): """Test reset_kafka_topics_config function.""" mock_kafka_client = mocker.MagicMock() mock_kafka_client.list_topics.return_value = ['topic-2'] mock_result = mocker.MagicMock() mock_result.to_object.return_value = {'result': 'success'} mock_kafka_client.alter_configs.return_value = mock_result event = { 'bootstrap_servers': 'localhost:9092', 'topics': ['topic-1', 'topic-2'] } index.reset_kafka_topics_config(event, mock_kafka_client) mock_kafka_client.alter_configs.assert_called_once() def test_reset_kafka_topics_config_no_topics(mocker): """Test reset_kafka_topics_config function.""" mock_kafka_client = mocker.MagicMock() mock_kafka_client.list_topics.return_value = [] mock_result = mocker.MagicMock() mock_result.to_object.return_value = {'result': 'success'} mock_kafka_client.alter_configs.return_value = mock_result event = { 'bootstrap_servers': 'localhost:9092', 'topics': ['topic-1', 'topic-2'] } index.reset_kafka_topics_config(event, mock_kafka_client) mock_kafka_client.alter_configs.assert_not_called()