Пример #1
0
def test_perform_snapshot_ignore_instance(mocker):
    """Test for method of the same name."""
    # some default settings for this test
    region = 'us-west-2'

    snapshot_settings = {
        'snapshot': {
            'minimum': 5,
            'frequency': '2 hours',
            'retention': '5 days'
        },
        'match': {
            'tag:backup': 'yes'
        },
        'ignore': []
    }

    # create an instance and record the id
    instance_id = mocks.create_instances(region, count=1)[0]
    snapshot_settings['ignore'].append(instance_id)

    # need to filter instances, so need dynamodb present
    mocks.create_dynamodb('us-east-1')
    dynamo.store_configuration('us-east-1', 'some_unique_id', '111122223333',
                               snapshot_settings)

    # patch the final method that takes a snapshot
    mocker.patch('ebs_snapper.utils.snapshot_and_tag')

    # since there are no snapshots, we should expect this to trigger one
    ctx = utils.MockContext()
    snapshot.perform_snapshot(ctx, region)

    # test results
    utils.snapshot_and_tag.assert_not_called()  # pylint: disable=E1103
Пример #2
0
def lambda_snapshot(event, context):
    """Snapshot a single instance when called by AWS Lambda."""

    # baseline logging for lambda
    utils.configure_logging(context, LOG)

    if not (event and event.get('Records')):
        LOG.warn('lambda_snapshot must be invoked from an SNS topic: %s',
                 str(event))
        return

    records = event.get('Records')
    for record in records:
        sns = record.get('Sns')
        if not sns:
            LOG.warn('lambda_snapshot missing an SNS section: %s', str(event))
            continue

        message = sns.get('Message')
        if not message:
            LOG.warn('lambda_snapshot missing a message section: %s',
                     str(event))
            continue

        message_json = json.loads(message)

        if 'region' not in message_json:
            LOG.warn('lambda_snapshot missing specific keys: %s', str(event))
            continue

        # call the snapshot perform method
        snapshot.perform_snapshot(context, message_json['region'])

        LOG.info('Function lambda_snapshot completed')
Пример #3
0
def test_perform_snapshot_skipped(mocker):
    """Test for method of the same name."""
    # some default settings for this test
    region = 'us-west-2'
    snapshot_settings = {
        'snapshot': {
            'minimum': 5,
            'frequency': '2 hours',
            'retention': '5 days'
        },
        'match': {
            'tag:backup': 'yes'
        }
    }
    mocks.create_dynamodb('us-east-1')
    dynamo.store_configuration('us-east-1', 'some_unique_id', '111122223333',
                               snapshot_settings)

    # create an instance and record the id
    instance_id = mocks.create_instances(region, count=1)[0]

    # figure out the EBS volume that came with our instance
    instance_details = utils.get_instance(instance_id, region)
    block_devices = instance_details.get('BlockDeviceMappings', [])
    volume_id = block_devices[0]['Ebs']['VolumeId']

    # determine what we should be tagging the snapshot
    ret, freq = utils.parse_snapshot_settings(snapshot_settings)  # pylint: disable=unused-variable
    now = datetime.datetime.now(dateutil.tz.tzutc())
    delete_on_dt = now + ret
    delete_on = delete_on_dt.strftime('%Y-%m-%d')

    # now take a snapshot, so we expect this next one to be skipped
    utils.snapshot_and_tag(instance_id, 'ami-123abc', volume_id, delete_on,
                           region)

    # patch the final method that takes a snapshot
    mocker.patch('ebs_snapper.utils.snapshot_and_tag')

    # since there are no snapshots, we should expect this to trigger one
    ctx = utils.MockContext()
    snapshot.perform_snapshot(ctx, region)

    # test results (should not create a second snapshot)
    utils.snapshot_and_tag.assert_not_called()  # pylint: disable=E1103
Пример #4
0
def test_perform_snapshot_ignore_volume(mocker):
    """Test for method of the same name."""
    # some default settings for this test
    region = 'us-west-2'

    snapshot_settings = {
        'snapshot': {
            'minimum': 5,
            'frequency': '2 hours',
            'retention': '5 days'
        },
        'match': {
            'tag:backup': 'yes'
        },
        'ignore': []
    }

    # create an instance and record the id
    instance_id = mocks.create_instances(region, count=1)[0]

    # need to filter instances, so need dynamodb present
    mocks.create_dynamodb('us-east-1')
    dynamo.store_configuration('us-east-1', 'some_unique_id', AWS_MOCK_ACCOUNT,
                               snapshot_settings)

    # figure out the EBS volume that came with our instance
    instance_details = utils.get_instance(instance_id, region)
    block_devices = instance_details.get('BlockDeviceMappings', [])
    volume_id = block_devices[0]['Ebs']['VolumeId']
    snapshot_settings['ignore'].append(volume_id)

    # patch the final method that takes a snapshot
    mocker.patch('ebs_snapper.utils.snapshot_and_tag')

    # since there are no snapshots, we should expect this to trigger one
    ctx = utils.MockContext()
    snapshot.perform_snapshot(ctx, region)

    # test results
    utils.snapshot_and_tag.assert_not_called()  # pylint: disable=E1103
Пример #5
0
def test_perform_snapshot(mocker):
    """Test for method of the same name."""
    # some default settings for this test
    region = 'us-west-2'

    snapshot_settings = {
        'snapshot': {'minimum': 5, 'frequency': '2 hours', 'retention': '5 days'},
        'match': {'tag:backup': 'yes'}
    }

    # create an instance and record the id
    instance_id = mocks.create_instances(region, count=1)[0]

    # need to filter instances, so need dynamodb present
    mocks.create_dynamodb('us-east-1')
    dynamo.store_configuration('us-east-1', 'some_unique_id', '111122223333', snapshot_settings)

    # figure out the EBS volume that came with our instance
    instance_details = utils.get_instance(instance_id, region)
    block_devices = instance_details.get('BlockDeviceMappings', [])
    volume_id = block_devices[0]['Ebs']['VolumeId']

    # determine what we should be tagging the snapshot
    ret, freq = utils.parse_snapshot_settings(snapshot_settings)  # pylint: disable=unused-variable
    now = datetime.datetime.now(dateutil.tz.tzutc())
    delete_on_dt = now + ret
    delete_on = delete_on_dt.strftime('%Y-%m-%d')

    # apply some tags
    client = boto3.client('ec2', region_name=region)
    instance_tags = [
        {'Key': 'Name', 'Value': 'Foo'},
        {'Key': 'Service', 'Value': 'Bar'},
        {'Key': 'backup', 'Value': 'yes'},
    ]
    client.create_tags(DryRun=False, Resources=[instance_id], Tags=instance_tags)

    # override one of the tags
    volume_tags = [{'Key': 'Service', 'Value': 'Baz'}]
    client.create_tags(DryRun=False, Resources=[volume_id], Tags=volume_tags)

    # when combined, we expect tags to be this.
    tags = [
        {'Key': 'Name', 'Value': 'Foo'},
        {'Key': 'Service', 'Value': 'Baz'},
        {'Key': 'backup', 'Value': 'yes'},
    ]

    # patch the final method that takes a snapshot
    mocker.patch('ebs_snapper.utils.snapshot_and_tag')

    # since there are no snapshots, we should expect this to trigger one
    ctx = utils.MockContext()
    snapshot.perform_snapshot(ctx, region)

    # test results
    utils.snapshot_and_tag.assert_any_call(  # pylint: disable=E1103
        instance_id,
        'ami-123abc',
        volume_id,
        delete_on,
        region,
        additional_tags=tags)