def test_create_observation_db(self, mock_rds, mock_secrets_client): os.environ['STAGE'] = 'TEST' os.environ['CAN_DELETE_DB'] = 'true' my_secret_string = json.dumps({ "KMS_KEY_ID": "kms", "DB_SUBGROUP_NAME": "subgroup", "VPC_SECURITY_GROUP_ID": "vpc_id" }) mock_secret_payload = {"SecretString": my_secret_string} mock_secrets_client.get_secret_value.return_value = mock_secret_payload two_days_ago = datetime.datetime.now() - datetime.timedelta(2) date_str = _get_date_string(two_days_ago) mock_rds.describe_db_snapshots.return_value = { 'DBSnapshots': [{ "DBInstanceIdentifier": 'observations-prod-external-2', "DBSnapshotIdentifier": f"rds:observations-prod-external-2-{date_str}" }] } db_create_handler.create_observation_db({}, {}) mock_rds.restore_db_instance_from_db_snapshot.assert_called_once_with( DBInstanceIdentifier='observations-test', DBSnapshotIdentifier=f"observationSnapshotTESTTemp", DBInstanceClass='db.r5.2xlarge', Port=5432, DBSubnetGroupName='subgroup', Iops=0, MultiAZ=False, Engine='postgres', VpcSecurityGroupIds=['vpc_id'], Tags=OBSERVATION_INSTANCE_TAGS)
def test_copy_observation_db_snapshot(self, mock_rds, mock_secrets_client): os.environ['CAN_DELETE_DB'] = 'true' os.environ['STAGE'] = 'TEST' my_secret_string = json.dumps({ "KMS_KEY_ID": "kms", "DB_SUBGROUP_NAME": "subgroup", "VPC_SECURITY_GROUP_ID": "vpc_id" }) mock_secret_payload = {"SecretString": my_secret_string} mock_secrets_client.get_secret_value.return_value = mock_secret_payload two_days_ago = datetime.datetime.now() - datetime.timedelta(2) date_str = _get_date_string(two_days_ago) mock_rds.describe_db_snapshots.return_value = { 'DBSnapshots': [{ "DBInstanceIdentifier": 'observations-prod-external-2', "DBSnapshotIdentifier": f"rds:observations-prod-external-2-{date_str}" }] } db_create_handler.copy_observation_db_snapshot({}, {}) mock_rds.copy_db_snapshot.assert_called_once_with( SourceDBSnapshotIdentifier=_get_observation_snapshot_identifier(), TargetDBSnapshotIdentifier=f"observationSnapshotTESTTemp", KmsKeyId='kms')
def test_create_observation_db(self, mock_rds, mock_secrets_client): os.environ['STAGE'] = 'TEST' os.environ['CAN_DELETE_DB'] = 'true' my_secret_string = json.dumps({ "KMS_KEY_ID": "kms", "DB_SUBGROUP_NAME": "subgroup", "VPC_SECURITY_GROUP_ID": "vpc_id" }) mock_secret_payload = {"SecretString": my_secret_string} mock_secrets_client.get_secret_value.return_value = mock_secret_payload two_days_ago = datetime.datetime.now() - datetime.timedelta(2) date_str = _get_date_string(two_days_ago) mock_rds.describe_db_snapshots.return_value = { 'DBSnapshots': [{ "DBInstanceIdentifier": 'observations-db-legacy-production-external', "DBSnapshotIdentifier": f"rds:observations-db-legacy-production-external-{date_str}" }] } db_create_handler.create_observation_db({}, {}) mock_rds.restore_db_instance_from_db_snapshot.assert_called_once()
def test_copy_observation_db_snapshot_invalid_tier(self, mock_rds, mock_secrets_client): os.environ['CAN_DELETE_DB'] = 'false' os.environ['STAGE'] = 'TEST' my_secret_string = json.dumps({ "KMS_KEY_ID": "kms", "DB_SUBGROUP_NAME": "subgroup", "VPC_SECURITY_GROUP_ID": "vpc_id" }) mock_secret_payload = {"SecretString": my_secret_string} mock_secrets_client.get_secret_value.return_value = mock_secret_payload two_days_ago = datetime.datetime.now() - datetime.timedelta(2) date_str = _get_date_string(two_days_ago) mock_rds.describe_db_snapshots.return_value = { 'DBSnapshots': [{ "DBInstanceIdentifier": 'observations-prod-external-2', "DBSnapshotIdentifier": f"rds:observations-prod-external-2-{date_str}" }] } with self.assertRaises(Exception) as context: db_create_handler.copy_observation_db_snapshot({}, {}) mock_rds.copy_db_snapshot.assert_not_called()
def test_get_date_string(self): jan_1 = datetime.datetime(2020, 1, 1) date_str = db_create_handler._get_date_string(jan_1) assert date_str == "2020-01-01"