"""Lambda test module.""" from unittest import mock import boto3 import pytest from moto import mock_rds import config from common.logic import database import app as index @pytest.fixture() def mock_account_id(): """Return an AWS account id.""" return '123456789000' @pytest.fixture() def mock_rds_standalone_name(): """Return an RDS standalone name.""" return 'prod-ows-standalone' @pytest.fixture() @mock_rds def mock_rds_client(): """Return a mock RDS client.""" client = boto3.client('rds', region_name=config.AWS_DEFAULT_REGION) return client @mock_rds def test_filter_automatic_snapshots_by_date( mock_rds_client, mock_rds_standalone_name): """Test filter_automatic_snapshots_by_date function.""" db_type = 'standalone' mock_rds_client.create_db_instance( AllocatedStorage=5, DBInstanceClass='db.t3.micro', DBInstanceIdentifier=mock_rds_standalone_name, Engine='mysql', MasterUserPassword='MyPassword', MasterUsername='MyUser', Tags=[ { 'Key': 'environment', 'Value': 'prod' }, ] ) # Create some dummy snapshots filtered_snapshot_matched_list = [] for snapshot_iterator in range(config.NUMBER_OF_SNAPSHOTS_TO_RETAIN): snapshot_name = f'{mock_rds_standalone_name}-{snapshot_iterator}' mock_rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_name, DBInstanceIdentifier=mock_rds_standalone_name, Tags=[ { 'Key': 'snapshot_type', 'Value': 'automated_backup' }, ] ) filtered_snapshot_matched_list.append(snapshot_name) snapshots = database.find_matching_snapshots( mock_rds_standalone_name, db_type, client=mock_rds_client) filtered_snapshots = index.filter_automatic_snapshots_by_date( db_type, snapshots) assert len(filtered_snapshots) == config.NUMBER_OF_SNAPSHOTS_TO_RETAIN assert list(filtered_snapshots.keys()) == filtered_snapshot_matched_list @mock_rds def test_create_backup_snapshot( mock_account_id, mock_rds_client, mock_rds_standalone_name): """Test create_backup_snapshot function.""" db_type = 'standalone' mock_rds_client.create_db_instance( AllocatedStorage=5, DBInstanceClass='db.t3.micro', DBInstanceIdentifier=mock_rds_standalone_name, Engine='mysql', MasterUserPassword='MyPassword', MasterUsername='MyUser', Tags=[ { 'Key': 'environment', 'Value': 'prod' }, ] ) initial_snapshot_name = mock_rds_client.create_db_snapshot( DBSnapshotIdentifier=f'{mock_rds_standalone_name}-1234-shared', DBInstanceIdentifier=mock_rds_standalone_name, )['DBSnapshot']['DBSnapshotIdentifier'] """ Moto has not implemented the copy_db_snapshot method, so mock its response here. However, the waiter in the database.copy_snapshot method needs an actual snapshot to exist, so create one matching the expected naming convention so it returns successfully. """ copied_snapshot_name = mock_rds_client.create_db_snapshot( DBSnapshotIdentifier=f'{initial_snapshot_name}-{mock_account_id}', DBInstanceIdentifier=mock_rds_standalone_name, )['DBSnapshot']['DBSnapshotIdentifier'] with mock.patch.object( mock_rds_client, 'copy_db_snapshot', return_value={ 'DBSnapshot': { 'DBSnapshotIdentifier': copied_snapshot_name, 'DBInstanceIdentifier': 'string' } } ): copied_snapshot = index.create_backup_snapshot( db_type, mock_rds_client, mock_account_id, initial_snapshot_name) assert copied_snapshot == copied_snapshot_name @mock_rds def test_manage_snapshot_lifecycle( mock_rds_client, mock_rds_standalone_name): """Test manage_snapshot_lifecycle function.""" db_type = 'standalone' mock_rds_client.create_db_instance( AllocatedStorage=5, DBInstanceClass='db.t3.micro', DBInstanceIdentifier=mock_rds_standalone_name, Engine='mysql', MasterUserPassword='MyPassword', MasterUsername='MyUser', Tags=[ { 'Key': 'environment', 'Value': 'prod' }, ] ) # Create some snapshots, but more than the configured retention number for snapshot_iterator in range(config.NUMBER_OF_SNAPSHOTS_TO_RETAIN + 5): snapshot_name = f'{mock_rds_standalone_name}-{snapshot_iterator}' mock_rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_name, DBInstanceIdentifier=mock_rds_standalone_name, Tags=[ { 'Key': 'snapshot_type', 'Value': 'automated_backup' }, ] ) snapshots = database.find_matching_snapshots( mock_rds_standalone_name, db_type, client=mock_rds_client) filtered_snapshots = index.filter_automatic_snapshots_by_date( db_type, snapshots) """ This should delete the number of snapshots greater than the configured retention number. Since the function also removes the item from the dictionary, compare its length after the call. """ index.manage_snapshot_lifecycle( db_type, mock_rds_client, filtered_snapshots) assert len(filtered_snapshots) == config.NUMBER_OF_SNAPSHOTS_TO_RETAIN