def test_detach_random_volume_asg_invalid_tags(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    tags = [{"Key": "TargetKey", "Value": "TargetValue"}]
    client.describe_instances.return_value = {"Reservations": []}

    with pytest.raises(FailedActivity) as x:
        detach_random_volume(tags=tags)
    assert "No ASG(s) found with matching tag(s): %s" % tags in str(x.value)
def test_detach_random_volume_asg_invalid_name(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    asg_names = ["AutoScalingGroup-A"]
    client.describe_auto_scaling_groups.return_value = {
        "AutoScalingGroups": []
    }

    with pytest.raises(FailedActivity) as x:
        detach_random_volume(asg_names=asg_names)
    assert "Unable to locate ASG(s): %s" % asg_names in str(x.value)
示例#3
0
def test_detach_random_volume_asg_tags(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    asg_names = ['AutoScalingGroup-A']
    tags = [{'Key': 'TargetKey', 'Value': 'TargetValue'}]
    client.get_paginator.return_value.paginate.return_value = [{
        'Tags': [{
            'ResourceId': 'AutoScalingGroup-A',
            'ResourceType': 'auto-scaling-group',
            'Key': 'TargetKey',
            'Value': 'TargetValue',
            'PropagateAtLaunch': False
        }]
    }]
    client.describe_auto_scaling_groups.return_value = {
        "AutoScalingGroups": [{
            "AutoScalingGroupName":
            "AutoScalingGroup-A",
            "Instances": [{
                "InstanceId": "i-00000000000000001"
            }]
        }]
    }
    client.describe_instances.return_value = {
        'Reservations': [{
            'Instances': [{
                'InstanceId':
                'i-00000000000000001',
                'BlockDeviceMappings': [{
                    'DeviceName': '/dev/xvda',
                    'Ebs': {
                        'VolumeId': 'vol-00000001'
                    }
                }, {
                    'DeviceName': '/dev/sdb',
                    'Ebs': {
                        'VolumeId': 'vol-00000002'
                    }
                }]
            }]
        }]
    }
    client.detach_volume.return_value = {
        'Device': '/dev/sdb',
        'InstanceId': 'i-00000000000000001',
        'State': 'detaching',
        'VolumeId': 'vol-00000002'
    }

    results = detach_random_volume(tags=tags)

    client.describe_auto_scaling_groups.assert_called_with(
        AutoScalingGroupNames=asg_names)
    client.describe_instances.assert_called_with(
        InstanceIds=['i-00000000000000001'])
    client.detach_volume.assert_called_with(Device='/dev/sdb',
                                            Force=True,
                                            InstanceId='i-00000000000000001',
                                            VolumeId='vol-00000002')
    assert results[0]['Device'] == '/dev/sdb'
def test_detach_random_volume_asg_name(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    asg_names = ["AutoScalingGroup-A"]
    client.describe_auto_scaling_groups.return_value = {
        "AutoScalingGroups": [{
            "AutoScalingGroupName":
            "AutoScalingGroup-A",
            "Instances": [{
                "InstanceId": "i-00000000000000001"
            }],
        }]
    }
    client.describe_instances.return_value = {
        "Reservations": [{
            "Instances": [{
                "InstanceId":
                "i-00000000000000001",
                "BlockDeviceMappings": [
                    {
                        "DeviceName": "/dev/xvda",
                        "Ebs": {
                            "VolumeId": "vol-00000001"
                        },
                    },
                    {
                        "DeviceName": "/dev/sdc",
                        "Ebs": {
                            "VolumeId": "vol-00000002"
                        },
                    },
                ],
            }]
        }]
    }
    client.detach_volume.return_value = {
        "Device": "/dev/sdc",
        "InstanceId": "i-00000000000000001",
        "State": "detaching",
        "VolumeId": "vol-00000002",
    }

    results = detach_random_volume(asg_names=asg_names)

    client.describe_auto_scaling_groups.assert_called_with(
        AutoScalingGroupNames=asg_names)
    client.describe_instances.assert_called_with(
        InstanceIds=["i-00000000000000001"])
    client.detach_volume.assert_called_with(
        Device="/dev/sdc",
        Force=True,
        InstanceId="i-00000000000000001",
        VolumeId="vol-00000002",
    )
    assert results[0]["Device"] == "/dev/sdc"
示例#5
0
def test_detach_random_volume_asg_name(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    asg_names = ['AutoScalingGroup-A']
    client.describe_auto_scaling_groups.return_value = {
        "AutoScalingGroups": [{
            "AutoScalingGroupName":
            "AutoScalingGroup-A",
            "Instances": [{
                "InstanceId": "i-00000000000000001"
            }]
        }]
    }
    client.describe_instances.return_value = {
        'Reservations': [{
            'Instances': [{
                'InstanceId':
                'i-00000000000000001',
                'BlockDeviceMappings': [{
                    'DeviceName': '/dev/xvda',
                    'Ebs': {
                        'VolumeId': 'vol-00000001'
                    }
                }, {
                    'DeviceName': '/dev/sdc',
                    'Ebs': {
                        'VolumeId': 'vol-00000002'
                    }
                }]
            }]
        }]
    }
    client.detach_volume.return_value = {
        'Device': '/dev/sdc',
        'InstanceId': 'i-00000000000000001',
        'State': 'detaching',
        'VolumeId': 'vol-00000002'
    }

    results = detach_random_volume(asg_names=asg_names)

    client.describe_auto_scaling_groups.assert_called_with(
        AutoScalingGroupNames=asg_names)
    client.describe_instances.assert_called_with(
        InstanceIds=['i-00000000000000001'])
    client.detach_volume.assert_called_with(Device='/dev/sdc',
                                            Force=True,
                                            InstanceId='i-00000000000000001',
                                            VolumeId='vol-00000002')
    assert results[0]['Device'] == '/dev/sdc'
def test_detach_random_volume_asg_tags(aws_client):
    client = MagicMock()
    aws_client.return_value = client
    asg_names = ["AutoScalingGroup-A"]
    tags = [{"Key": "TargetKey", "Value": "TargetValue"}]
    client.get_paginator.return_value.paginate.return_value = [{
        "Tags": [{
            "ResourceId": "AutoScalingGroup-A",
            "ResourceType": "auto-scaling-group",
            "Key": "TargetKey",
            "Value": "TargetValue",
            "PropagateAtLaunch": False,
        }]
    }]
    client.describe_auto_scaling_groups.return_value = {
        "AutoScalingGroups": [{
            "AutoScalingGroupName":
            "AutoScalingGroup-A",
            "Instances": [{
                "InstanceId": "i-00000000000000001"
            }],
        }]
    }
    client.describe_instances.return_value = {
        "Reservations": [{
            "Instances": [{
                "InstanceId":
                "i-00000000000000001",
                "BlockDeviceMappings": [
                    {
                        "DeviceName": "/dev/xvda",
                        "Ebs": {
                            "VolumeId": "vol-00000001"
                        },
                    },
                    {
                        "DeviceName": "/dev/sdb",
                        "Ebs": {
                            "VolumeId": "vol-00000002"
                        },
                    },
                ],
            }]
        }]
    }
    client.detach_volume.return_value = {
        "Device": "/dev/sdb",
        "InstanceId": "i-00000000000000001",
        "State": "detaching",
        "VolumeId": "vol-00000002",
    }

    results = detach_random_volume(tags=tags)

    client.describe_auto_scaling_groups.assert_called_with(
        AutoScalingGroupNames=asg_names)
    client.describe_instances.assert_called_with(
        InstanceIds=["i-00000000000000001"])
    client.detach_volume.assert_called_with(
        Device="/dev/sdb",
        Force=True,
        InstanceId="i-00000000000000001",
        VolumeId="vol-00000002",
    )
    assert results[0]["Device"] == "/dev/sdb"