def test_clean_snapshots_tagged_timeout(mocker): """Test that we _DONT_ clean anything if runtime > 4 minutes""" # default settings region = 'us-east-1' mocks.create_dynamodb(region) ctx = utils.MockContext() ctx.set_remaining_time_in_millis(5) # 5 millis remaining # create an instance and record the id instance_id = mocks.create_instances(region, count=1)[0] # setup the min # snaps for the instance config_data = { "match": {"instance-id": instance_id}, "snapshot": { "retention": "6 days", "minimum": 0, "frequency": "13 hours" } } # put it in the table, be sure it succeeded dynamo.store_configuration(region, 'foo', '111122223333', config_data) # figure out the EBS volume that came with our instance volume_id = utils.get_volumes([instance_id], region)[0]['VolumeId'] # make a snapshot that should be deleted today too now = datetime.datetime.now(dateutil.tz.tzutc()) delete_on = now.strftime('%Y-%m-%d') utils.snapshot_and_tag(instance_id, 'ami-123abc', volume_id, delete_on, region) mocker.patch('ebs_snapper.utils.delete_snapshot') clean.clean_snapshot(ctx, region) # ensure we DO NOT take a snapshot if our runtime was 5 minutes assert not utils.delete_snapshot.called
def lambda_clean(event, context): """Clean up a single region when called by AWS Lambda.""" # baseline logging for lambda logging.basicConfig(level=logging.INFO) LOG.setLevel(logging.INFO) logging.getLogger('botocore').setLevel(logging.WARNING) logging.getLogger('boto3').setLevel(logging.WARNING) if not (event and event.get('Records')): LOG.warn('lambda_clean must be invoked from an SNS topic') return records = event.get('Records') for record in records: sns = record.get('Sns') if not sns: LOG.warn('lambda_clean missing an SNS section: %s', str(event)) continue message = sns.get('Message') if not message: LOG.warn('lambda_clean missing a message section: %s', str(event)) continue message_json = json.loads(message) if 'region' not in message_json: LOG.warn('lambda_clean missing specific keys: %s', str(event)) continue # call the snapshot cleanup method clean.clean_snapshot(context, message_json['region']) LOG.info('Function lambda_clean completed')
def test_clean_tagged_snapshots_ignore_volume(mocker): """Test for method of the same name.""" # default settings region = 'us-east-1' mocks.create_dynamodb(region) # create an instance and record the id instance_id = mocks.create_instances(region, count=1)[0] ctx = utils.MockContext() # setup the min # snaps for the instance config_data = { "match": { "instance-id": instance_id }, "snapshot": { "retention": "6 days", "minimum": 0, "frequency": "13 hours" }, "ignore": [] } # put it in the table, be sure it succeeded dynamo.store_configuration(region, 'foo', AWS_MOCK_ACCOUNT, config_data) # figure out the EBS volume that came with our instance volume_id = utils.get_volumes([instance_id], region)[0]['VolumeId'] config_data["ignore"].append(volume_id) # make a snapshot that should be deleted today too now = datetime.datetime.now(dateutil.tz.tzutc()) delete_on = now.strftime('%Y-%m-%d') utils.snapshot_and_tag(instance_id, 'ami-123abc', volume_id, delete_on, region) snapshot_id = utils.most_recent_snapshot(volume_id, region)['SnapshotId'] mocker.patch('ebs_snapper.utils.delete_snapshot') clean.clean_snapshot(ctx, region) # ensure we deleted this snapshot if it was ready to die today utils.delete_snapshot.assert_any_call(snapshot_id, region) # pylint: disable=E1103 # now raise the minimum, and check to be sure we didn't delete utils.delete_snapshot.reset_mock() # pylint: disable=E1103 config_data['snapshot']['minimum'] = 5 dynamo.store_configuration(region, 'foo', AWS_MOCK_ACCOUNT, config_data) clean.clean_snapshot(ctx, region) utils.delete_snapshot.assert_not_called() # pylint: disable=E1103
def test_clean_tagged_snapshots_ignore_retention(mocker): """Test for method of the same name.""" # default settings region = 'us-east-1' mocks.create_dynamodb(region) # create two instances and record the id of one of them instance_id_list = mocks.create_instances(region, count=2) instance_id = instance_id_list[0] ctx = utils.MockContext() # setup the min # snaps for the instance config_data = { "match": { "instance-id": instance_id }, "snapshot": { "retention": "6 days", "minimum": 5, "frequency": "13 hours" }, "ignore_retention": True } # put it in the table, be sure it succeeded dynamo.store_configuration(region, 'foo', AWS_MOCK_ACCOUNT, config_data) # figure out the EBS volume that came with our instance volume_id = utils.get_volumes([instance_id], region)[0]['VolumeId'] # make a snapshot that should be deleted today (but 5 snap retention) now = datetime.datetime.now(dateutil.tz.tzutc()) delete_on = now.strftime('%Y-%m-%d') utils.snapshot_and_tag(instance_id, 'ami-123abc', volume_id, delete_on, region) snapshot_id = utils.most_recent_snapshot(volume_id, region)['SnapshotId'] # now delete the instance and volume, keep the snapshot client = boto3.client('ec2', region_name=region) client.terminate_instances(InstanceIds=[instance_id]) mocker.patch('ebs_snapper.utils.delete_snapshot') dynamo.store_configuration(region, 'foo', AWS_MOCK_ACCOUNT, config_data) clean.clean_snapshot(ctx, region) utils.delete_snapshot.assert_any_call(snapshot_id, region) # pylint: disable=E1103