def test_find_instances(ec2_all):
    ec2 = ec2_all['eu-central-1']
    terminate_instances(ec2)

    create_instances(ec2, PARAMS._replace(role='aaa'), 2)
    create_instances(ec2, PARAMS._replace(role='bbb'), 3)

    aaa = find_instances(ec2, PARAMS.namespace, 'aaa')
    bbb = find_instances(ec2, PARAMS.namespace, 'bbb')
    aaa_and_bbb = find_instances(ec2, PARAMS.namespace)

    assert len(aaa) == 2
    assert len(bbb) == 3
    assert len(aaa_and_bbb) == 5

    assert set(aaa).union(bbb) == set(aaa_and_bbb)
def test_find_instances(ec2_all):
    region = 'eu-central-1'
    launcher = AwsEC2Launcher()
    terminator = AwsEC2Terminator()
    ec2 = ec2_all[region]

    for inst in find_instances(ec2, PARAMS.project, PARAMS.namespace):
        terminator.terminate(inst, region)
    terminator.wait(False)

    launcher.launch(PARAMS._replace(group='aaa'), 2, ec2=ec2)
    launcher.launch(PARAMS._replace(group='bbb'), 3, ec2=ec2)

    aaa = find_instances(ec2, PARAMS.project, PARAMS.namespace, 'aaa')
    bbb = find_instances(ec2, PARAMS.project, PARAMS.namespace, 'bbb')
    aaa_and_bbb = find_instances(ec2, PARAMS.project, PARAMS.namespace)

    assert len(aaa) == 2
    assert len(bbb) == 3
    assert len(aaa_and_bbb) == 5

    assert set(aaa).union(bbb) == set(aaa_and_bbb)
Exemple #3
0
def test_find_instances(ec2ctx, ec2_resources):
    launcher = AwsEC2Launcher()
    terminator = AwsEC2Terminator()

    params1 = ec2_resources._replace(
        group="{}_{}".format(ec2_resources.group, 'aaa'))
    params2 = ec2_resources._replace(
        group="{}_{}".format(ec2_resources.group, 'bbb'))

    for group in (params1.group, params2.group):
        for inst in find_instances(ec2ctx.resource, ec2_resources.project,
                                   ec2_resources.namespace, group):
            terminator.terminate(inst, ec2ctx.region)
    terminator.wait(False)

    launcher.launch(params1, 2, ec2=ec2ctx.resource)
    launcher.launch(params2, 3, ec2=ec2ctx.resource)

    aaa = find_instances(ec2ctx.resource, params1.project, params1.namespace,
                         params1.group)
    bbb = find_instances(ec2ctx.resource, params2.project, params2.namespace,
                         params2.group)
    aaa_and_bbb = [
        i for i in find_instances(ec2ctx.resource, ec2_resources.project,
                                  ec2_resources.namespace)
        if get_tag(i, 'Group') in (params1.group, params2.group)
    ]

    assert len(aaa) == 2
    assert len(bbb) == 3
    assert len(aaa_and_bbb) == 5
    assert set(aaa).union(bbb) == set(aaa_and_bbb)

    for inst in aaa_and_bbb:
        terminator.terminate(inst, ec2ctx.region)
    terminator.wait(False)
def ec2_environment(ec2_all):
    for ec2 in ec2_all.values():
        manage_key_pair(ec2, True)
        manage_security_group(ec2, True)
    yield

    terminator = AwsEC2Terminator()
    for region, ec2 in ec2_all.iteritems():
        for inst in find_instances(ec2, PARAMS.project, PARAMS.namespace):
            terminator.terminate(inst, region)
    terminator.wait(False)

    for ec2 in ec2_all.values():
        manage_key_pair(ec2, False)
        manage_security_group(ec2, False)
Exemple #5
0
def test_manage_instances(ec2ctxs, ec2_resources):
    regions = [ctx.region for ctx in ec2ctxs]

    def check_hosts(hosts):
        assert len(set(host.tag_id for host in hosts)) == len(hosts)
        assert len(set(host.public_ip for host in hosts)) == len(hosts)

    def check_tags(instances):
        for inst_group in instances:
            for inst in inst_group:
                inst_tag_id = get_tag(inst, 'ID')
                assert inst_tag_id is not None
                inst_tag_name = get_tag(inst, 'Name')
                assert inst_tag_name == "{}-{}-{}-{}".format(
                    ec2_resources.project, ec2_resources.namespace,
                    ec2_resources.group, inst_tag_id.zfill(3)).lower()

    res = manage_instances(regions, ec2_resources, 4)
    instances = [
        find_instances(ctx.resource, ec2_resources.project,
                       ec2_resources.namespace, ec2_resources.group)
        for ctx in ec2ctxs
    ]
    assert res.changed
    assert len(res.active) == 4
    assert len(res.terminated) == 0
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 2
    assert len(instances[1]) == 1
    assert len(instances[2]) == 1
    assert set(
        [get_tag(instances[0][0], 'ID'),
         get_tag(instances[0][1], 'ID')]) == set(['1', '4'])
    assert get_tag(instances[1][0], 'ID') == '2'
    assert get_tag(instances[2][0], 'ID') == '3'

    res = manage_instances(regions, ec2_resources, 4)
    instances = [
        find_instances(ctx.resource, ec2_resources.project,
                       ec2_resources.namespace, ec2_resources.group)
        for ctx in ec2ctxs
    ]
    assert not res.changed
    assert len(res.active) == 4
    assert len(res.terminated) == 0
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 2
    assert len(instances[1]) == 1
    assert len(instances[2]) == 1
    assert set(
        [get_tag(instances[0][0], 'ID'),
         get_tag(instances[0][1], 'ID')]) == set(['1', '4'])
    assert get_tag(instances[1][0], 'ID') == '2'
    assert get_tag(instances[2][0], 'ID') == '3'

    res = manage_instances(regions, ec2_resources, 2)
    instances = [
        find_instances(ctx.resource, ec2_resources.project,
                       ec2_resources.namespace, ec2_resources.group)
        for ctx in ec2ctxs
    ]
    assert res.changed
    assert len(res.active) == 2
    assert len(res.terminated) == 2
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 1
    assert len(instances[1]) == 1
    assert len(instances[2]) == 0
    assert get_tag(instances[0][0], 'ID') == '1'
    assert get_tag(instances[1][0], 'ID') == '2'

    res = manage_instances(regions, ec2_resources, 0)
    instances = [
        find_instances(ctx.resource, ec2_resources.project,
                       ec2_resources.namespace, ec2_resources.group)
        for ctx in ec2ctxs
    ]
    assert res.changed
    assert len(res.active) == 0
    assert len(res.terminated) == 2
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 0
    assert len(instances[1]) == 0
    assert len(instances[2]) == 0

    res = manage_instances(regions, ec2_resources, 0)
    instances = [
        find_instances(ctx.resource, ec2_resources.project,
                       ec2_resources.namespace, ec2_resources.group)
        for ctx in ec2ctxs
    ]
    assert not res.changed
    assert len(res.active) == 0
    assert len(res.terminated) == 0
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 0
    assert len(instances[1]) == 0
    assert len(instances[2]) == 0
Exemple #6
0
def ec2_resources(request, regions, ec2):
    def gen_params(group_suffix=None,
                   key_name_suffix=None,
                   security_group_suffix=None):
        def _random(N=7):
            return ''.join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(N))

        return InstanceParams(
            project='Indy-PA-dev',
            add_tags={'Purpose': 'Test Pool Automation'},
            namespace='test_stateful_set',
            group="group_{}".format(
                group_suffix if group_suffix else _random()),
            key_name="test_stateful_set_key_{}".format(
                key_name_suffix if key_name_suffix else _random()),
            security_group="test_stateful_set_security_group_{}".format(
                security_group_suffix if security_group_suffix else _random()),
            type_name='t2.micro',
            # TODO docs
            market_spot=(request.config.getoption("--market-type") == 'spot'),
            spot_max_price=None,
            # TODO docs
            ebs_volume_size=9)

    def manage_key_pair(ec2, present, params):
        count = 0
        for key in ec2.key_pairs.all():
            if key.key_name != params.key_name:
                continue
            if present and count == 0:
                count = 1
            else:
                key.delete()
        if present and count == 0:
            ec2.create_key_pair(KeyName=params.key_name)

    def manage_security_group(ec2, present, params):
        count = 0
        for sgroup in ec2.security_groups.all():
            if sgroup.group_name != params.security_group:
                continue
            if present and count == 0:
                count = 1
            else:
                sgroup.delete()
        if present and count == 0:
            sg = ec2.create_security_group(GroupName=params.security_group,
                                           Description='Test security group')
            sg.create_tags(Tags=[{
                'Key':
                'Name',
                'Value':
                "{}-{}-{}".format(params.project, params.namespace,
                                  params.group)
            }, {
                'Key': 'Project',
                'Value': params.project
            }, {
                'Key': 'Namespace',
                'Value': params.namespace
            }, {
                'Key': 'Group',
                'Value': params.group
            }])

    params = gen_params(group_suffix=request.node.name,
                        key_name_suffix=request.node.name,
                        security_group_suffix=request.node.name)

    for rc in ec2:
        manage_key_pair(rc, True, params)
        manage_security_group(rc, True, params)
    yield params

    terminator = AwsEC2Terminator()
    for region, rc in zip(regions, ec2):
        for inst in find_instances(rc, params.project, params.namespace,
                                   params.group):
            terminator.terminate(inst, region)
    terminator.wait(False)

    for rc in ec2:
        manage_key_pair(rc, False, params)
        manage_security_group(rc, False, params)
def terminate_instances(ec2):
    instances = find_instances(ec2, PARAMS.namespace)
    for inst in instances:
        inst.terminate()
def test_manage_instances(ec2_all):
    regions = ['eu-central-1', 'us-west-1', 'us-west-2']
    connections = [ec2_all[r] for r in regions]
    params = PARAMS._replace(role='test_manage')

    def check_hosts(hosts):
        assert len(set(host.tag_id for host in hosts)) == len(hosts)
        assert len(set(host.public_ip for host in hosts)) == len(hosts)

    def check_tags(instances):
        for group in instances:
            for inst in group:
                check_params(inst, params)
                assert get_tag(inst, 'id') is not None

    changed, hosts = manage_instances(regions, params, 4)
    instances = [find_instances(c, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert changed
    check_hosts(hosts)
    check_tags(instances)
    assert len(hosts) == 4
    assert len(instances[0]) == 2
    assert len(instances[1]) == 1
    assert len(instances[2]) == 1
    assert set([get_tag(instances[0][0], 'id'),
                get_tag(instances[0][1], 'id')]) == set(['1', '4'])
    assert get_tag(instances[1][0], 'id') == '2'
    assert get_tag(instances[2][0], 'id') == '3'

    changed, hosts = manage_instances(regions, params, 4)
    instances = [find_instances(c, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert not changed
    check_hosts(hosts)
    check_tags(instances)
    assert len(hosts) == 4
    assert len(instances[0]) == 2
    assert len(instances[1]) == 1
    assert len(instances[2]) == 1
    assert set([get_tag(instances[0][0], 'id'),
                get_tag(instances[0][1], 'id')]) == set(['1', '4'])
    assert get_tag(instances[1][0], 'id') == '2'
    assert get_tag(instances[2][0], 'id') == '3'

    changed, hosts = manage_instances(regions, params, 2)
    instances = [find_instances(c, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert changed
    check_hosts(hosts)
    check_tags(instances)
    assert len(hosts) == 2
    assert len(instances[0]) == 1
    assert len(instances[1]) == 1
    assert len(instances[2]) == 0
    assert get_tag(instances[0][0], 'id') == '1'
    assert get_tag(instances[1][0], 'id') == '2'

    changed, hosts = manage_instances(regions, params, 0)
    instances = [find_instances(c, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert changed
    check_hosts(hosts)
    check_tags(instances)
    assert len(hosts) == 0
    assert len(instances[0]) == 0
    assert len(instances[1]) == 0
    assert len(instances[2]) == 0

    changed, hosts = manage_instances(regions, params, 0)
    instances = [find_instances(c, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert not changed
    check_hosts(hosts)
    check_tags(instances)
    assert len(hosts) == 0
    assert len(instances[0]) == 0
    assert len(instances[1]) == 0
    assert len(instances[2]) == 0
def test_manage_instances(ec2_all):
    regions = ['us-east-1', 'us-east-2', 'us-west-2']
    connections = [ec2_all[r] for r in regions]
    params = PARAMS._replace(group='test_manage')

    def check_hosts(hosts):
        assert len(set(host.tag_id for host in hosts)) == len(hosts)
        assert len(set(host.public_ip for host in hosts)) == len(hosts)

    def check_tags(instances):
        for group in instances:
            for inst in group:
                check_params(inst, params)
                inst_tag_id = get_tag(inst, 'ID')
                assert inst_tag_id is not None
                inst_tag_name = get_tag(inst, 'Name')
                assert inst_tag_name == "{}-{}-{}-{}".format(
                    params.project,
                    params.namespace,
                    params.group,
                    inst_tag_id.zfill(3)).lower()
                for tag_key, tag_value in params.add_tags.iteritems():
                    assert tag_value == get_tag(inst, tag_key)

    res = manage_instances(regions, params, 4)
    instances = [find_instances(c, PARAMS.project, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert res.changed
    assert len(res.active) == 4
    assert len(res.terminated) == 0
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 2
    assert len(instances[1]) == 1
    assert len(instances[2]) == 1
    assert set([get_tag(instances[0][0], 'ID'),
                get_tag(instances[0][1], 'ID')]) == set(['1', '4'])
    assert get_tag(instances[1][0], 'ID') == '2'
    assert get_tag(instances[2][0], 'ID') == '3'

    res = manage_instances(regions, params, 4)
    instances = [find_instances(c, PARAMS.project, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert not res.changed
    assert len(res.active) == 4
    assert len(res.terminated) == 0
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 2
    assert len(instances[1]) == 1
    assert len(instances[2]) == 1
    assert set([get_tag(instances[0][0], 'ID'),
                get_tag(instances[0][1], 'ID')]) == set(['1', '4'])
    assert get_tag(instances[1][0], 'ID') == '2'
    assert get_tag(instances[2][0], 'ID') == '3'

    res = manage_instances(regions, params, 2)
    instances = [find_instances(c, PARAMS.project, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert res.changed
    assert len(res.active) == 2
    assert len(res.terminated) == 2
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 1
    assert len(instances[1]) == 1
    assert len(instances[2]) == 0
    assert get_tag(instances[0][0], 'ID') == '1'
    assert get_tag(instances[1][0], 'ID') == '2'

    res = manage_instances(regions, params, 0)
    instances = [find_instances(c, PARAMS.project, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert res.changed
    assert len(res.active) == 0
    assert len(res.terminated) == 2
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 0
    assert len(instances[1]) == 0
    assert len(instances[2]) == 0

    res = manage_instances(regions, params, 0)
    instances = [find_instances(c, PARAMS.project, PARAMS.namespace, 'test_manage')
                 for c in connections]
    assert not res.changed
    assert len(res.active) == 0
    assert len(res.terminated) == 0
    check_hosts(res.active + res.terminated)
    check_tags(instances)
    assert len(instances[0]) == 0
    assert len(instances[1]) == 0
    assert len(instances[2]) == 0