Beispiel #1
0
def test_describe_load_balancers():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2

    elb_client = boto3.client('elb', region_name='us-east-1')
    elb_client.create_load_balancer(
        LoadBalancerName='my-lb',
        Listeners=[
            {'Protocol': 'tcp', 'LoadBalancerPort': 80, 'InstancePort': 8080}],
        AvailabilityZones=['us-east-1a', 'us-east-1b']
    )

    client = boto3.client('autoscaling', region_name='us-east-1')
    client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        LoadBalancerNames=['my-lb'],
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        Tags=[{
            "ResourceId": 'test_asg',
            "Key": 'test_key',
            "Value": 'test_value',
            "PropagateAtLaunch": True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    response = client.describe_load_balancers(AutoScalingGroupName='test_asg')
    list(response['LoadBalancers']).should.have.length_of(1)
    response['LoadBalancers'][0]['LoadBalancerName'].should.equal('my-lb')
Beispiel #2
0
def test_create_elb_and_autoscaling_group_no_relationship():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2
    ELB_NAME = 'my-elb'

    elb_client = boto3.client('elb', region_name='us-east-1')
    elb_client.create_load_balancer(
        LoadBalancerName=ELB_NAME,
        Listeners=[{
            'Protocol': 'tcp',
            'LoadBalancerPort': 80,
            'InstancePort': 8080
        }],
        AvailabilityZones=['us-east-1a', 'us-east-1b'])

    client = boto3.client('autoscaling', region_name='us-east-1')
    client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')

    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    # autoscaling group and elb should have no relationship
    response = client.describe_load_balancers(AutoScalingGroupName='test_asg')
    list(response['LoadBalancers']).should.have.length_of(0)
    response = elb_client.describe_load_balancers(LoadBalancerNames=[ELB_NAME])
    list(response['LoadBalancerDescriptions'][0]
         ['Instances']).should.have.length_of(0)
def test_describe_load_balancers():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2

    elb_client = boto3.client('elb', region_name='us-east-1')
    elb_client.create_load_balancer(
        LoadBalancerName='my-lb',
        Listeners=[
            {'Protocol': 'tcp', 'LoadBalancerPort': 80, 'InstancePort': 8080}],
        AvailabilityZones=['us-east-1a', 'us-east-1b']
    )

    client = boto3.client('autoscaling', region_name='us-east-1')
    client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        LoadBalancerNames=['my-lb'],
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        Tags=[{
            "ResourceId": 'test_asg',
            "Key": 'test_key',
            "Value": 'test_value',
            "PropagateAtLaunch": True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    response = client.describe_load_balancers(AutoScalingGroupName='test_asg')
    list(response['LoadBalancers']).should.have.length_of(1)
    response['LoadBalancers'][0]['LoadBalancerName'].should.equal('my-lb')
def test_terminate_instance_in_autoscaling_group():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=1,
        MaxSize=20,
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=False)

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg'])
    original_instance_id = next(
        instance['InstanceId']
        for instance in response['AutoScalingGroups'][0]['Instances'])
    ec2_client = boto3.client('ec2', region_name='us-east-1')
    ec2_client.terminate_instances(InstanceIds=[original_instance_id])

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg'])
    replaced_instance_id = next(
        instance['InstanceId']
        for instance in response['AutoScalingGroups'][0]['Instances'])
    replaced_instance_id.should_not.equal(original_instance_id)
Beispiel #5
0
def test_update_autoscaling_group_boto3():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking["subnet1"],
        NewInstancesProtectedFromScaleIn=True,
    )

    _ = client.update_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        MinSize=1,
        VPCZoneIdentifier="{subnet1},{subnet2}".format(
            subnet1=mocked_networking["subnet1"],
            subnet2=mocked_networking["subnet2"]),
        NewInstancesProtectedFromScaleIn=False,
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    group = response["AutoScalingGroups"][0]
    group["MinSize"].should.equal(1)
    set(group["AvailabilityZones"]).should.equal({"us-east-1a", "us-east-1b"})
    group["NewInstancesProtectedFromScaleIn"].should.equal(False)
Beispiel #6
0
def test_describe_autoscaling_instances_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=True,
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"]
    )
    instance_ids = [
        instance['InstanceId']
        for instance in response['AutoScalingGroups'][0]['Instances']
    ]

    response = client.describe_auto_scaling_instances(InstanceIds=instance_ids)
    for instance in response['AutoScalingInstances']:
        instance['AutoScalingGroupName'].should.equal('test_asg')
        instance['AvailabilityZone'].should.equal('us-east-1a')
        instance['ProtectedFromScaleIn'].should.equal(True)
Beispiel #7
0
def test_set_instance_health():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=2,
        MaxSize=4,
        DesiredCapacity=2,
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])

    instance1 = response["AutoScalingGroups"][0]["Instances"][0]
    instance1["HealthStatus"].should.equal("Healthy")

    client.set_instance_health(InstanceId=instance1["InstanceId"],
                               HealthStatus="Unhealthy")

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])

    instance1 = response["AutoScalingGroups"][0]["Instances"][0]
    instance1["HealthStatus"].should.equal("Unhealthy")
Beispiel #8
0
def test_suspend_processes():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    client.create_launch_configuration(
        LaunchConfigurationName='lc',
    )
    client.create_auto_scaling_group(
        LaunchConfigurationName='lc',
        AutoScalingGroupName='test-asg',
        MinSize=1,
        MaxSize=1,
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    # When we suspend the 'Launch' process on the ASG client
    client.suspend_processes(
        AutoScalingGroupName='test-asg',
        ScalingProcesses=['Launch']
    )

    res = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test-asg']
    )

    # The 'Launch' process should, in fact, be suspended
    launch_suspended = False
    for proc in res['AutoScalingGroups'][0]['SuspendedProcesses']:
        if proc.get('ProcessName') == 'Launch':
            launch_suspended = True

    assert launch_suspended is True
Beispiel #9
0
def test_create_autoscaling_group_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    response = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        Tags=[
            {'ResourceId': 'test_asg',
             'ResourceType': 'auto-scaling-group',
             'Key': 'propogated-tag-key',
             'Value': 'propogate-tag-value',
             'PropagateAtLaunch': True
             },
            {'ResourceId': 'test_asg',
             'ResourceType': 'auto-scaling-group',
             'Key': 'not-propogated-tag-key',
             'Value': 'not-propogate-tag-value',
             'PropagateAtLaunch': False
             }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=False,
    )
    response['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
Beispiel #10
0
def test_set_desired_capacity_up_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=True,
    )

    _ = client.set_desired_capacity(
        AutoScalingGroupName='test_asg',
        DesiredCapacity=10,
    )

    response = client.describe_auto_scaling_groups(AutoScalingGroupNames=['test_asg'])
    instances = response['AutoScalingGroups'][0]['Instances']
    instances.should.have.length_of(10)
    for instance in instances:
        instance['ProtectedFromScaleIn'].should.equal(True)
Beispiel #11
0
def test_create_autoscaling_group_boto3():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    response = client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        Tags=[
            {
                "ResourceId": "test_asg",
                "ResourceType": "auto-scaling-group",
                "Key": "propogated-tag-key",
                "Value": "propagate-tag-value",
                "PropagateAtLaunch": True,
            },
            {
                "ResourceId": "test_asg",
                "ResourceType": "auto-scaling-group",
                "Key": "not-propogated-tag-key",
                "Value": "not-propagate-tag-value",
                "PropagateAtLaunch": False,
            },
        ],
        VPCZoneIdentifier=mocked_networking["subnet1"],
        NewInstancesProtectedFromScaleIn=False,
    )
    response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
Beispiel #12
0
def test_list_many_autoscaling_groups():
    mocked_networking = setup_networking()
    conn = boto3.client("autoscaling", region_name="us-east-1")
    conn.create_launch_configuration(LaunchConfigurationName="TestLC")

    conn.create_auto_scaling_group(
        AutoScalingGroupName="TestGroup1",
        MinSize=1,
        MaxSize=2,
        LaunchConfigurationName="TestLC",
        Tags=[{
            "ResourceId": "TestGroup1",
            "ResourceType": "auto-scaling-group",
            "PropagateAtLaunch": True,
            "Key": "TestTagKey1",
            "Value": "TestTagValue1",
        }],
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    ec2 = boto3.client("ec2", region_name="us-east-1")
    instances = ec2.describe_instances()

    tags = instances["Reservations"][0]["Instances"][0]["Tags"]
    tags.should.contain({"Value": "TestTagValue1", "Key": "TestTagKey1"})
    tags.should.contain({
        "Value": "TestGroup1",
        "Key": "aws:autoscaling:groupName"
    })
Beispiel #13
0
def test_create_elb_and_autoscaling_group_no_relationship():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2
    ELB_NAME = "my-elb"

    elb_client = boto3.client("elb", region_name="us-east-1")
    elb_client.create_load_balancer(
        LoadBalancerName=ELB_NAME,
        Listeners=[{
            "Protocol": "tcp",
            "LoadBalancerPort": 80,
            "InstancePort": 8080
        }],
        AvailabilityZones=["us-east-1a", "us-east-1b"],
    )

    client = boto3.client("autoscaling", region_name="us-east-1")
    client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")

    client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    # autoscaling group and elb should have no relationship
    response = client.describe_load_balancers(AutoScalingGroupName="test_asg")
    list(response["LoadBalancers"]).should.have.length_of(0)
    response = elb_client.describe_load_balancers(LoadBalancerNames=[ELB_NAME])
    list(response["LoadBalancerDescriptions"][0]
         ["Instances"]).should.have.length_of(0)
Beispiel #14
0
def test_terminate_instance_in_autoscaling_group():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=1,
        MaxSize=20,
        VPCZoneIdentifier=mocked_networking["subnet1"],
        NewInstancesProtectedFromScaleIn=False,
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    original_instance_id = next(
        instance["InstanceId"]
        for instance in response["AutoScalingGroups"][0]["Instances"])
    ec2_client = boto3.client("ec2", region_name="us-east-1")
    ec2_client.terminate_instances(InstanceIds=[original_instance_id])

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    replaced_instance_id = next(
        instance["InstanceId"]
        for instance in response["AutoScalingGroups"][0]["Instances"])
    replaced_instance_id.should_not.equal(original_instance_id)
Beispiel #15
0
def test_list_many_autoscaling_groups():
    mocked_networking = setup_networking()
    conn = boto3.client("autoscaling", region_name="us-east-1")
    conn.create_launch_configuration(LaunchConfigurationName="TestLC")

    for i in range(51):
        conn.create_auto_scaling_group(
            AutoScalingGroupName="TestGroup%d" % i,
            MinSize=1,
            MaxSize=2,
            LaunchConfigurationName="TestLC",
            VPCZoneIdentifier=mocked_networking["subnet1"],
        )

    response = conn.describe_auto_scaling_groups()
    groups = response["AutoScalingGroups"]
    marker = response["NextToken"]
    groups.should.have.length_of(50)
    marker.should.equal(groups[-1]["AutoScalingGroupName"])

    response2 = conn.describe_auto_scaling_groups(NextToken=marker)

    groups.extend(response2["AutoScalingGroups"])
    groups.should.have.length_of(51)
    assert "NextToken" not in response2.keys()
Beispiel #16
0
def test_suspend_processes():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    client.create_launch_configuration(LaunchConfigurationName="lc")
    client.create_auto_scaling_group(
        LaunchConfigurationName="lc",
        AutoScalingGroupName="test-asg",
        MinSize=1,
        MaxSize=1,
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    # When we suspend the 'Launch' process on the ASG client
    client.suspend_processes(AutoScalingGroupName="test-asg",
                             ScalingProcesses=["Launch"])

    res = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test-asg"])

    # The 'Launch' process should, in fact, be suspended
    launch_suspended = False
    for proc in res["AutoScalingGroups"][0]["SuspendedProcesses"]:
        if proc.get("ProcessName") == "Launch":
            launch_suspended = True

    assert launch_suspended is True
Beispiel #17
0
def test_list_many_autoscaling_groups():
    mocked_networking = setup_networking()
    conn = boto3.client('autoscaling', region_name='us-east-1')
    conn.create_launch_configuration(LaunchConfigurationName='TestLC')

    conn.create_auto_scaling_group(
        AutoScalingGroupName='TestGroup1',
        MinSize=1,
        MaxSize=2,
        LaunchConfigurationName='TestLC',
        Tags=[{
            "ResourceId": 'TestGroup1',
            "ResourceType": "auto-scaling-group",
            "PropagateAtLaunch": True,
            "Key": 'TestTagKey1',
            "Value": 'TestTagValue1'
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'])

    ec2 = boto3.client('ec2', region_name='us-east-1')
    instances = ec2.describe_instances()

    tags = instances['Reservations'][0]['Instances'][0]['Tags']
    tags.should.contain({u'Value': 'TestTagValue1', u'Key': 'TestTagKey1'})
    tags.should.contain({
        u'Value': 'TestGroup1',
        u'Key': 'aws:autoscaling:groupName'
    })
Beispiel #18
0
def test_describe_autoscaling_instances_boto3():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking["subnet1"],
        NewInstancesProtectedFromScaleIn=True,
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    instance_ids = [
        instance["InstanceId"]
        for instance in response["AutoScalingGroups"][0]["Instances"]
    ]

    response = client.describe_auto_scaling_instances(InstanceIds=instance_ids)
    for instance in response["AutoScalingInstances"]:
        instance["AutoScalingGroupName"].should.equal("test_asg")
        instance["AvailabilityZone"].should.equal("us-east-1a")
        instance["ProtectedFromScaleIn"].should.equal(True)
Beispiel #19
0
def test_set_instance_health():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=2,
        MaxSize=4,
        DesiredCapacity=2,
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg']
    )

    instance1 = response['AutoScalingGroups'][0]['Instances'][0]
    instance1['HealthStatus'].should.equal('Healthy')

    client.set_instance_health(InstanceId=instance1['InstanceId'], HealthStatus='Unhealthy')

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg']
    )

    instance1 = response['AutoScalingGroups'][0]['Instances'][0]
    instance1['HealthStatus'].should.equal('Unhealthy')
Beispiel #20
0
def test_autoscaling_taqs_update_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        Tags=[{
            "ResourceId": 'test_asg',
            "Key": 'test_key',
            "Value": 'test_value',
            "PropagateAtLaunch": True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    client.create_or_update_tags(Tags=[{
        "ResourceId": 'test_asg',
        "Key": 'test_key',
        "Value": 'updated_test_value',
        "PropagateAtLaunch": True
    }, {
        "ResourceId": 'test_asg',
        "Key": 'test_key2',
        "Value": 'test_value2',
        "PropagateAtLaunch": False
    }])

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    response['AutoScalingGroups'][0]['Tags'].should.have.length_of(2)
Beispiel #21
0
def test_update_autoscaling_group_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=True,
    )

    _ = client.update_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        MinSize=1,
        VPCZoneIdentifier="{subnet1},{subnet2}".format(
            subnet1=mocked_networking['subnet1'],
            subnet2=mocked_networking['subnet2'],
        ),
        NewInstancesProtectedFromScaleIn=False,
    )

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"]
    )
    group = response['AutoScalingGroups'][0]
    group['MinSize'].should.equal(1)
    set(group['AvailabilityZones']).should.equal({'us-east-1a', 'us-east-1b'})
    group['NewInstancesProtectedFromScaleIn'].should.equal(False)
Beispiel #22
0
def test_attach_one_instance():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')
    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=4,
        DesiredCapacity=2,
        Tags=[{
            'ResourceId': 'test_asg',
            'ResourceType': 'auto-scaling-group',
            'Key': 'propogated-tag-key',
            'Value': 'propogate-tag-value',
            'PropagateAtLaunch': True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )
    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg'])

    ec2 = boto3.resource('ec2', 'us-east-1')
    instances_to_add = [
        x.id for x in ec2.create_instances(ImageId='', MinCount=1, MaxCount=1)
    ]

    response = client.attach_instances(AutoScalingGroupName='test_asg',
                                       InstanceIds=instances_to_add)
    response['ResponseMetadata']['HTTPStatusCode'].should.equal(200)

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg'])
    response['AutoScalingGroups'][0]['Instances'].should.have.length_of(3)
Beispiel #23
0
def test_set_instance_protection():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=False,
    )

    response = client.describe_auto_scaling_groups(AutoScalingGroupNames=['test_asg'])
    instance_ids = [
        instance['InstanceId']
        for instance in response['AutoScalingGroups'][0]['Instances']
    ]
    protected = instance_ids[:3]

    _ = client.set_instance_protection(
        AutoScalingGroupName='test_asg',
        InstanceIds=protected,
        ProtectedFromScaleIn=True,
    )

    response = client.describe_auto_scaling_groups(AutoScalingGroupNames=['test_asg'])
    for instance in response['AutoScalingGroups'][0]['Instances']:
        instance['ProtectedFromScaleIn'].should.equal(
            instance['InstanceId'] in protected
        )
Beispiel #24
0
def test_autoscaling_describe_policies_boto3():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration"
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        Tags=[
            {
                "ResourceId": "test_asg",
                "Key": "test_key",
                "Value": "test_value",
                "PropagateAtLaunch": True,
            }
        ],
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    client.put_scaling_policy(
        AutoScalingGroupName="test_asg",
        PolicyName="test_policy_down",
        PolicyType="SimpleScaling",
        AdjustmentType="PercentChangeInCapacity",
        ScalingAdjustment=-10,
        Cooldown=60,
        MinAdjustmentMagnitude=1,
    )
    client.put_scaling_policy(
        AutoScalingGroupName="test_asg",
        PolicyName="test_policy_up",
        PolicyType="SimpleScaling",
        AdjustmentType="PercentChangeInCapacity",
        ScalingAdjustment=10,
        Cooldown=60,
        MinAdjustmentMagnitude=1,
    )

    response = client.describe_policies()
    response["ScalingPolicies"].should.have.length_of(2)

    response = client.describe_policies(AutoScalingGroupName="test_asg")
    response["ScalingPolicies"].should.have.length_of(2)

    response = client.describe_policies(PolicyTypes=["StepScaling"])
    response["ScalingPolicies"].should.have.length_of(0)

    response = client.describe_policies(
        AutoScalingGroupName="test_asg",
        PolicyNames=["test_policy_down"],
        PolicyTypes=["SimpleScaling"],
    )
    response["ScalingPolicies"].should.have.length_of(1)
    response["ScalingPolicies"][0]["PolicyName"].should.equal("test_policy_down")
Beispiel #25
0
def test_autoscaling_describe_policies_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        Tags=[{
            "ResourceId": 'test_asg',
            "Key": 'test_key',
            "Value": 'test_value',
            "PropagateAtLaunch": True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    client.put_scaling_policy(
        AutoScalingGroupName='test_asg',
        PolicyName='test_policy_down',
        PolicyType='SimpleScaling',
        AdjustmentType='PercentChangeInCapacity',
        ScalingAdjustment=-10,
        Cooldown=60,
        MinAdjustmentMagnitude=1)
    client.put_scaling_policy(
        AutoScalingGroupName='test_asg',
        PolicyName='test_policy_up',
        PolicyType='SimpleScaling',
        AdjustmentType='PercentChangeInCapacity',
        ScalingAdjustment=10,
        Cooldown=60,
        MinAdjustmentMagnitude=1)

    response = client.describe_policies()
    response['ScalingPolicies'].should.have.length_of(2)

    response = client.describe_policies(AutoScalingGroupName='test_asg')
    response['ScalingPolicies'].should.have.length_of(2)

    response = client.describe_policies(PolicyTypes=['StepScaling'])
    response['ScalingPolicies'].should.have.length_of(0)

    response = client.describe_policies(
        AutoScalingGroupName='test_asg',
        PolicyNames=['test_policy_down'],
        PolicyTypes=['SimpleScaling']
    )
    response['ScalingPolicies'].should.have.length_of(1)
    response['ScalingPolicies'][0][
        'PolicyName'].should.equal('test_policy_down')
Beispiel #26
0
def test_detach_all_target_groups():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2
    client = boto3.client("autoscaling", region_name="us-east-1")
    elbv2_client = boto3.client("elbv2", region_name="us-east-1")

    response = elbv2_client.create_target_group(
        Name="a-target",
        Protocol="HTTP",
        Port=8080,
        VpcId=mocked_networking["vpc"],
        HealthCheckProtocol="HTTP",
        HealthCheckPort="8080",
        HealthCheckPath="/",
        HealthCheckIntervalSeconds=5,
        HealthCheckTimeoutSeconds=5,
        HealthyThresholdCount=5,
        UnhealthyThresholdCount=2,
        Matcher={"HttpCode": "200"},
    )
    target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]

    client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration"
    )

    client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        TargetGroupARNs=[target_group_arn],
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    response = client.describe_load_balancer_target_groups(
        AutoScalingGroupName="test_asg"
    )
    list(response["LoadBalancerTargetGroups"]).should.have.length_of(1)

    response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn)
    list(response["TargetHealthDescriptions"]).should.have.length_of(INSTANCE_COUNT)

    response = client.detach_load_balancer_target_groups(
        AutoScalingGroupName="test_asg", TargetGroupARNs=[target_group_arn]
    )

    response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn)
    list(response["TargetHealthDescriptions"]).should.have.length_of(0)
    response = client.describe_load_balancer_target_groups(
        AutoScalingGroupName="test_asg"
    )
    list(response["LoadBalancerTargetGroups"]).should.have.length_of(0)
Beispiel #27
0
def test_detach_one_instance_decrement():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=2,
        DesiredCapacity=2,
        Tags=[{
            'ResourceId': 'test_asg',
            'ResourceType': 'auto-scaling-group',
            'Key': 'propogated-tag-key',
            'Value': 'propogate-tag-value',
            'PropagateAtLaunch': True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )
    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg']
    )
    instance_to_detach = response['AutoScalingGroups'][0]['Instances'][0]['InstanceId']
    instance_to_keep = response['AutoScalingGroups'][0]['Instances'][1]['InstanceId']

    ec2_client = boto3.client('ec2', region_name='us-east-1')

    response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])

    response = client.detach_instances(
        AutoScalingGroupName='test_asg',
        InstanceIds=[instance_to_detach],
        ShouldDecrementDesiredCapacity=True
    )
    response['ResponseMetadata']['HTTPStatusCode'].should.equal(200)

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg']
    )
    response['AutoScalingGroups'][0]['Instances'].should.have.length_of(1)

    # test to ensure tag has been removed
    response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])
    tags = response['Reservations'][0]['Instances'][0]['Tags']
    tags.should.have.length_of(1)

    # test to ensure tag is present on other instance
    response = ec2_client.describe_instances(InstanceIds=[instance_to_keep])
    tags = response['Reservations'][0]['Instances'][0]['Tags']
    tags.should.have.length_of(2)
Beispiel #28
0
def test_detach_one_instance_decrement():
    mocked_networking = setup_networking()
    client = boto3.client("autoscaling", region_name="us-east-1")
    _ = client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=2,
        DesiredCapacity=2,
        Tags=[{
            "ResourceId": "test_asg",
            "ResourceType": "auto-scaling-group",
            "Key": "propogated-tag-key",
            "Value": "propagate-tag-value",
            "PropagateAtLaunch": True,
        }],
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )
    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    instance_to_detach = response["AutoScalingGroups"][0]["Instances"][0][
        "InstanceId"]
    instance_to_keep = response["AutoScalingGroups"][0]["Instances"][1][
        "InstanceId"]

    ec2_client = boto3.client("ec2", region_name="us-east-1")

    response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])

    response = client.detach_instances(
        AutoScalingGroupName="test_asg",
        InstanceIds=[instance_to_detach],
        ShouldDecrementDesiredCapacity=True,
    )
    response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    response["AutoScalingGroups"][0]["Instances"].should.have.length_of(1)

    # test to ensure tag has been removed
    response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])
    tags = response["Reservations"][0]["Instances"][0]["Tags"]
    tags.should.have.length_of(1)

    # test to ensure tag is present on other instance
    response = ec2_client.describe_instances(InstanceIds=[instance_to_keep])
    tags = response["Reservations"][0]["Instances"][0]["Tags"]
    tags.should.have.length_of(2)
Beispiel #29
0
def test_detach_all_target_groups():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2
    client = boto3.client('autoscaling', region_name='us-east-1')
    elbv2_client = boto3.client('elbv2', region_name='us-east-1')

    response = elbv2_client.create_target_group(
        Name='a-target',
        Protocol='HTTP',
        Port=8080,
        VpcId=mocked_networking['vpc'],
        HealthCheckProtocol='HTTP',
        HealthCheckPort='8080',
        HealthCheckPath='/',
        HealthCheckIntervalSeconds=5,
        HealthCheckTimeoutSeconds=5,
        HealthyThresholdCount=5,
        UnhealthyThresholdCount=2,
        Matcher={'HttpCode': '200'})
    target_group_arn = response['TargetGroups'][0]['TargetGroupArn']

    client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')

    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        TargetGroupARNs=[target_group_arn],
        VPCZoneIdentifier=mocked_networking['subnet1'])

    response = client.describe_load_balancer_target_groups(
        AutoScalingGroupName='test_asg')
    list(response['LoadBalancerTargetGroups']).should.have.length_of(1)

    response = elbv2_client.describe_target_health(
        TargetGroupArn=target_group_arn)
    list(response['TargetHealthDescriptions']).should.have.length_of(INSTANCE_COUNT)

    response = client.detach_load_balancer_target_groups(
        AutoScalingGroupName='test_asg',
        TargetGroupARNs=[target_group_arn])

    response = elbv2_client.describe_target_health(
        TargetGroupArn=target_group_arn)
    list(response['TargetHealthDescriptions']).should.have.length_of(0)
    response = client.describe_load_balancer_target_groups(
        AutoScalingGroupName='test_asg')
    list(response['LoadBalancerTargetGroups']).should.have.length_of(0)
Beispiel #30
0
def test_detach_all_target_groups():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2
    client = boto3.client('autoscaling', region_name='us-east-1')
    elbv2_client = boto3.client('elbv2', region_name='us-east-1')

    response = elbv2_client.create_target_group(Name='a-target',
                                                Protocol='HTTP',
                                                Port=8080,
                                                VpcId=mocked_networking['vpc'],
                                                HealthCheckProtocol='HTTP',
                                                HealthCheckPort='8080',
                                                HealthCheckPath='/',
                                                HealthCheckIntervalSeconds=5,
                                                HealthCheckTimeoutSeconds=5,
                                                HealthyThresholdCount=5,
                                                UnhealthyThresholdCount=2,
                                                Matcher={'HttpCode': '200'})
    target_group_arn = response['TargetGroups'][0]['TargetGroupArn']

    client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')

    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        TargetGroupARNs=[target_group_arn],
        VPCZoneIdentifier=mocked_networking['vpc'])

    response = client.describe_load_balancer_target_groups(
        AutoScalingGroupName='test_asg')
    list(response['LoadBalancerTargetGroups']).should.have.length_of(1)

    response = elbv2_client.describe_target_health(
        TargetGroupArn=target_group_arn)
    list(response['TargetHealthDescriptions']).should.have.length_of(
        INSTANCE_COUNT)

    response = client.detach_load_balancer_target_groups(
        AutoScalingGroupName='test_asg', TargetGroupARNs=[target_group_arn])

    response = elbv2_client.describe_target_health(
        TargetGroupArn=target_group_arn)
    list(response['TargetHealthDescriptions']).should.have.length_of(0)
    response = client.describe_load_balancer_target_groups(
        AutoScalingGroupName='test_asg')
    list(response['LoadBalancerTargetGroups']).should.have.length_of(0)
Beispiel #31
0
def test_attach_load_balancer():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2

    elb_client = boto3.client("elb", region_name="us-east-1")
    elb_client.create_load_balancer(
        LoadBalancerName="my-lb",
        Listeners=[{
            "Protocol": "tcp",
            "LoadBalancerPort": 80,
            "InstancePort": 8080
        }],
        AvailabilityZones=["us-east-1a", "us-east-1b"],
    )

    client = boto3.client("autoscaling", region_name="us-east-1")
    client.create_launch_configuration(
        LaunchConfigurationName="test_launch_configuration")
    client.create_auto_scaling_group(
        AutoScalingGroupName="test_asg",
        LaunchConfigurationName="test_launch_configuration",
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        Tags=[{
            "ResourceId": "test_asg",
            "Key": "test_key",
            "Value": "test_value",
            "PropagateAtLaunch": True,
        }],
        VPCZoneIdentifier=mocked_networking["subnet1"],
    )

    response = client.attach_load_balancers(AutoScalingGroupName="test_asg",
                                            LoadBalancerNames=["my-lb"])
    response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)

    response = elb_client.describe_load_balancers(LoadBalancerNames=["my-lb"])
    list(response["LoadBalancerDescriptions"][0]
         ["Instances"]).should.have.length_of(INSTANCE_COUNT)

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    list(response["AutoScalingGroups"][0]
         ["LoadBalancerNames"]).should.have.length_of(1)
Beispiel #32
0
def test_describe_autoscaling_groups_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration')
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )
    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"])
    response['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
    response['AutoScalingGroups'][0]['AutoScalingGroupName'].should.equal(
        'test_asg')
Beispiel #33
0
def test_describe_autoscaling_groups_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )
    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"]
    )
    response['ResponseMetadata']['HTTPStatusCode'].should.equal(200)
    response['AutoScalingGroups'][0][
        'AutoScalingGroupName'].should.equal('test_asg')
Beispiel #34
0
def test_set_desired_capacity_down_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=True,
    )

    response = client.describe_auto_scaling_groups(AutoScalingGroupNames=['test_asg'])
    instance_ids = [
        instance['InstanceId']
        for instance in response['AutoScalingGroups'][0]['Instances']
    ]
    unprotected, protected = instance_ids[:2], instance_ids[2:]

    _ = client.set_instance_protection(
        AutoScalingGroupName='test_asg',
        InstanceIds=unprotected,
        ProtectedFromScaleIn=False,
    )

    _ = client.set_desired_capacity(
        AutoScalingGroupName='test_asg',
        DesiredCapacity=1,
    )

    response = client.describe_auto_scaling_groups(AutoScalingGroupNames=['test_asg'])
    group = response['AutoScalingGroups'][0]
    group['DesiredCapacity'].should.equal(1)
    instance_ids = {instance['InstanceId'] for instance in group['Instances']}
    set(protected).should.equal(instance_ids)
    set(unprotected).should_not.be.within(instance_ids)  # only unprotected killed
Beispiel #35
0
def test_attach_one_instance():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=4,
        DesiredCapacity=2,
        Tags=[{
            'ResourceId': 'test_asg',
            'ResourceType': 'auto-scaling-group',
            'Key': 'propogated-tag-key',
            'Value': 'propogate-tag-value',
            'PropagateAtLaunch': True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
        NewInstancesProtectedFromScaleIn=True,
    )

    ec2 = boto3.resource('ec2', 'us-east-1')
    instances_to_add = [x.id for x in ec2.create_instances(ImageId='', MinCount=1, MaxCount=1)]

    response = client.attach_instances(
        AutoScalingGroupName='test_asg',
        InstanceIds=instances_to_add
    )
    response['ResponseMetadata']['HTTPStatusCode'].should.equal(200)

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=['test_asg']
    )
    instances = response['AutoScalingGroups'][0]['Instances']
    instances.should.have.length_of(3)
    for instance in instances:
        instance['ProtectedFromScaleIn'].should.equal(True)
Beispiel #36
0
def test_list_many_autoscaling_groups():
    mocked_networking = setup_networking()
    conn = boto3.client('autoscaling', region_name='us-east-1')
    conn.create_launch_configuration(LaunchConfigurationName='TestLC')

    for i in range(51):
        conn.create_auto_scaling_group(AutoScalingGroupName='TestGroup%d' % i,
                                       MinSize=1,
                                       MaxSize=2,
                                       LaunchConfigurationName='TestLC',
                                       VPCZoneIdentifier=mocked_networking['subnet1'])

    response = conn.describe_auto_scaling_groups()
    groups = response["AutoScalingGroups"]
    marker = response["NextToken"]
    groups.should.have.length_of(50)
    marker.should.equal(groups[-1]['AutoScalingGroupName'])

    response2 = conn.describe_auto_scaling_groups(NextToken=marker)

    groups.extend(response2["AutoScalingGroups"])
    groups.should.have.length_of(51)
    assert 'NextToken' not in response2.keys()
Beispiel #37
0
def test_autoscaling_taqs_update_boto3():
    mocked_networking = setup_networking()
    client = boto3.client('autoscaling', region_name='us-east-1')
    _ = client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )
    _ = client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=20,
        DesiredCapacity=5,
        Tags=[{
            "ResourceId": 'test_asg',
            "Key": 'test_key',
            "Value": 'test_value',
            "PropagateAtLaunch": True
        }],
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    client.create_or_update_tags(Tags=[{
        "ResourceId": 'test_asg',
        "Key": 'test_key',
        "Value": 'updated_test_value',
        "PropagateAtLaunch": True
    }, {
        "ResourceId": 'test_asg',
        "Key": 'test_key2',
        "Value": 'test_value2',
        "PropagateAtLaunch": False
    }])

    response = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=["test_asg"]
    )
    response['AutoScalingGroups'][0]['Tags'].should.have.length_of(2)
Beispiel #38
0
def test_create_elb_and_autoscaling_group_no_relationship():
    mocked_networking = setup_networking()
    INSTANCE_COUNT = 2
    ELB_NAME = 'my-elb'

    elb_client = boto3.client('elb', region_name='us-east-1')
    elb_client.create_load_balancer(
        LoadBalancerName=ELB_NAME,
        Listeners=[
            {'Protocol': 'tcp', 'LoadBalancerPort': 80, 'InstancePort': 8080}],
        AvailabilityZones=['us-east-1a', 'us-east-1b']
    )

    client = boto3.client('autoscaling', region_name='us-east-1')
    client.create_launch_configuration(
        LaunchConfigurationName='test_launch_configuration'
    )

    client.create_auto_scaling_group(
        AutoScalingGroupName='test_asg',
        LaunchConfigurationName='test_launch_configuration',
        MinSize=0,
        MaxSize=INSTANCE_COUNT,
        DesiredCapacity=INSTANCE_COUNT,
        VPCZoneIdentifier=mocked_networking['subnet1'],
    )

    # autoscaling group and elb should have no relationship
    response = client.describe_load_balancers(
        AutoScalingGroupName='test_asg'
    )
    list(response['LoadBalancers']).should.have.length_of(0)
    response = elb_client.describe_load_balancers(
        LoadBalancerNames=[ELB_NAME]
    )
    list(response['LoadBalancerDescriptions'][0]['Instances']).should.have.length_of(0)
Beispiel #39
0
def test_list_many_autoscaling_groups():
    mocked_networking = setup_networking()
    conn = boto3.client('autoscaling', region_name='us-east-1')
    conn.create_launch_configuration(LaunchConfigurationName='TestLC')

    conn.create_auto_scaling_group(AutoScalingGroupName='TestGroup1',
                                   MinSize=1,
                                   MaxSize=2,
                                   LaunchConfigurationName='TestLC',
                                   Tags=[{
                                       "ResourceId": 'TestGroup1',
                                       "ResourceType": "auto-scaling-group",
                                       "PropagateAtLaunch": True,
                                       "Key": 'TestTagKey1',
                                       "Value": 'TestTagValue1'
                                   }],
                                   VPCZoneIdentifier=mocked_networking['subnet1'])

    ec2 = boto3.client('ec2', region_name='us-east-1')
    instances = ec2.describe_instances()

    tags = instances['Reservations'][0]['Instances'][0]['Tags']
    tags.should.contain({u'Value': 'TestTagValue1', u'Key': 'TestTagKey1'})
    tags.should.contain({u'Value': 'TestGroup1', u'Key': 'aws:autoscaling:groupName'})