def test_attach_volume_ec2_filter(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    instance_ids = ['i-987654321fedcba']
    filters = [{'Name': 'instance-state-name', 'Values': ['running']}]
    client.describe_instances.return_value = {
        'Reservations': [{
            'Instances': [{
                'InstanceId': 'i-987654321fedcba',
                'State': {
                    'Code': 16,
                    'Name': 'running'
                }
            }]
        }]
    }

    client.get_paginator.return_value.paginate.return_value = [{
        'Volumes': [{
            'VolumeId':
            'vol-00000001',
            'Tags': [{
                'Key':
                'ChaosToolkitDetached',
                'Value':
                'DeviceName=/dev/sdb;InstanceId=%s' % (instance_ids[0])
            }]
        }, {
            'VolumeId':
            'vol-00000002',
            'Tags': [{
                'Key':
                'ChaosToolkitDetached',
                'Value':
                'DeviceName=/dev/sdb;InstanceId='
                'i-987654321fabcde'
            }]
        }]
    }]

    client.attach_volume.return_value = {
        'DeviceName': '/dev/sdb',
        'InstanceId': instance_ids[0],
        'State': 'attaching',
        'VolumeId': 'vol-00000001'
    }

    results = attach_volume(filters=filters)

    client.describe_instances.assert_called_with(Filters=filters)
    client.get_paginator.return_value.paginate.assert_called_with(
        Filters=[{
            'Name': 'tag-key',
            'Values': ['ChaosToolkitDetached']
        }])
    client.attach_volume.assert_called_with(Device='/dev/sdb',
                                            InstanceId=instance_ids[0],
                                            VolumeId='vol-00000001')
    assert results[0]['DeviceName'] == '/dev/sdb'
示例#2
0
def test_attach_volume_ec2_id(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    instance_ids = ["i-987654321fedcba"]
    client.describe_instances.return_value = {
        "Reservations": [{
            "Instances": [{
                "InstanceId": "i-987654321fedcba"
            }]
        }]
    }

    client.get_paginator.return_value.paginate.return_value = [{
        "Volumes": [
            {
                "VolumeId":
                "vol-00000001",
                "Tags": [{
                    "Key":
                    "ChaosToolkitDetached",
                    "Value":
                    "DeviceName=/dev/sdc;InstanceId=%s" % (instance_ids[0]),
                }],
            },
            {
                "VolumeId":
                "vol-00000002",
                "Tags": [{
                    "Key":
                    "ChaosToolkitDetached",
                    "Value":
                    "DeviceName=/dev/sdb;InstanceId="
                    "i-987654321fabcde",
                }],
            },
        ]
    }]

    client.attach_volume.return_value = {
        "DeviceName": "/dev/sdc",
        "InstanceId": instance_ids[0],
        "State": "attaching",
        "VolumeId": "vol-00000001",
    }

    results = attach_volume(instance_ids=instance_ids)

    client.describe_instances.assert_called_with(InstanceIds=instance_ids)
    client.get_paginator.return_value.paginate.assert_called_with(
        Filters=[{
            "Name": "tag-key",
            "Values": ["ChaosToolkitDetached"]
        }])
    client.attach_volume.assert_called_with(Device="/dev/sdc",
                                            InstanceId=instance_ids[0],
                                            VolumeId="vol-00000001")
    assert results[0]["DeviceName"] == "/dev/sdc"