Esempio n. 1
0
def test_perform_fanout_all_regions_replication(mocker):
    """Test for method of the same name."""

    # make a dummy SNS topic
    mocks.create_sns_topic('ReplicationSnapshotTopic')
    expected_sns_topic = utils.get_topic_arn('ReplicationSnapshotTopic', 'us-east-1')

    dummy_regions = ['us-west-2', 'us-east-1']

    # make some dummy snapshots in two regions
    snapshot_map = {}
    for dummy_region in dummy_regions:
        client = boto3.client('ec2', region_name=dummy_region)
        volume = client.create_volume(Size=100, AvailabilityZone=dummy_region + "a")
        snapshot = client.create_snapshot(VolumeId=volume['VolumeId'])
        snapshot_map[snapshot['SnapshotId']] = dummy_region

    # patch the final message sender method
    ctx = utils.MockContext()
    mocker.patch('ebs_snapper.replication.send_fanout_message')
    replication.perform_fanout_all_regions(ctx)

    # fan out, and be sure we touched every instance we created before
    for r in dummy_regions:
        replication.send_fanout_message.assert_any_call(
            context=ctx,
            region=r,
            sns_topic=expected_sns_topic,
            cli=False)  # pylint: disable=E1103
Esempio n. 2
0
def test_get_topic_arn():
    """Test for method of the same name."""
    topic_name = 'please-dont-exist'

    # make an SNS topic
    mocks.create_sns_topic(topic_name, region_name='us-west-2')

    # see if our code can find it!
    found_arn = utils.get_topic_arn(topic_name, default_region='us-west-2')
    assert 'us-west-2' in found_arn
    assert topic_name in found_arn
Esempio n. 3
0
def test_send_fanout_message_clean(mocker):
    """Test for method of the same name."""

    mocks.create_sns_topic('testing-topic')
    expected_sns_topic = utils.get_topic_arn('testing-topic', 'us-east-1')
    ctx = utils.MockContext()

    mocker.patch('ebs_snapper.utils.sns_publish')
    clean.send_fanout_message(ctx, region='us-west-2', topic_arn=expected_sns_topic)
    utils.sns_publish.assert_any_call(  # pylint: disable=E1103
        TopicArn=expected_sns_topic,
        Message=json.dumps({'region': 'us-west-2'}))
Esempio n. 4
0
def test_perform_fanout_all_regions_snapshot(mocker):
    """Test for method of the same name."""

    # make a dummy SNS topic
    mocks.create_event_rule('ScheduledRuleReplicationFunction')
    mocks.create_sns_topic('CreateSnapshotTopic')
    expected_sns_topic = utils.get_topic_arn('CreateSnapshotTopic',
                                             'us-east-1')

    dummy_regions = ['us-west-2', 'us-east-1']

    # make some dummy instances in two regions
    instance_maps = {}
    for dummy_region in dummy_regions:
        client = boto3.client('ec2', region_name=dummy_region)
        create_results = client.run_instances(ImageId='ami-123abc',
                                              MinCount=5,
                                              MaxCount=5)
        for instance_data in create_results['Instances']:
            instance_maps[instance_data['InstanceId']] = dummy_region

    # need to filter instances, so need dynamodb present
    mocks.create_dynamodb('us-east-1')
    config_data = {
        "match": {
            "instance-id": instance_maps.keys()
        },
        "snapshot": {
            "retention": "3 days",
            "minimum": 4,
            "frequency": "11 hours"
        }
    }
    dynamo.store_configuration('us-east-1', 'some_unique_id', AWS_MOCK_ACCOUNT,
                               config_data)

    # patch the final message sender method
    ctx = utils.MockContext()
    mocker.patch('ebs_snapper.snapshot.send_fanout_message')
    snapshot.perform_fanout_all_regions(ctx)

    # fan out, and be sure we touched every instance we created before
    for r in dummy_regions:
        snapshot.send_fanout_message.assert_any_call(
            context=ctx, region=r, sns_topic=expected_sns_topic, cli=False)  # pylint: disable=E1103
Esempio n. 5
0
def test_perform_fanout_all_regions_clean(mocker):
    """Test for method of the same name."""
    mocks.create_sns_topic('CleanSnapshotTopic')
    mocks.create_dynamodb()

    expected_regions = utils.get_regions()
    for r in expected_regions:  # must have an instance in the region to clean it
        mocks.create_instances(region=r)
    expected_sns_topic = utils.get_topic_arn('CleanSnapshotTopic', 'us-east-1')

    ctx = utils.MockContext()
    mocker.patch('ebs_snapper.clean.send_fanout_message')

    # fan out, and be sure we touched every region
    clean.perform_fanout_all_regions(ctx)
    for r in expected_regions:
        clean.send_fanout_message.assert_any_call(  # pylint: disable=E1103
            ctx,
            cli=False,
            region=r,
            topic_arn=expected_sns_topic)