def test_AwsEC2Terminator_spot(ec2ctx, ec2_resources): launcher = AwsEC2Launcher() terminator = AwsEC2Terminator() params = ec2_resources._replace(market_spot=True, spot_max_price=None) instances = launcher.launch(params, 1, region=ec2ctx.region, ec2=ec2ctx.resource) launcher.wait() for instance in instances: terminator.terminate(instance) for instance in instances: assert instance.spot_instance_request_id is not None spot_params = ec2ctx.client.describe_spot_instance_requests( SpotInstanceRequestIds=[instance.spot_instance_request_id]) # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-bid-status.html#get-spot-instance-bid-status assert (spot_params['SpotInstanceRequests'][0]['State'] in ('closed', 'cancelled')) assert (spot_params['SpotInstanceRequests'][0]['Status']['Code'] in ('instance-terminated-by-user', 'request-canceled-and-instance-running')) terminator.wait()
def test_AwsEC2Terminator_wait(ec2ctxs, ec2_resources): launcher = AwsEC2Launcher() terminator = AwsEC2Terminator() instances = [] params = ec2_resources._replace(market_spot=False) for ctx in ec2ctxs: _instances = launcher.launch(params, 1, region=ctx.region, ec2=ctx.resource) assert len(_instances) == 1 instances += _instances launcher.wait() for instance in instances: terminator.terminate(instance) assert len(terminator.awaited) > 0 terminator.wait() assert len(terminator.awaited) == 0 for instance in instances: assert instance.state['Name'] == 'terminated'
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)
def test_AwsEC2Terminator(ec2): launcher = AwsEC2Launcher() terminator = AwsEC2Terminator() params = PARAMS._replace(group='test_terminate') instances = launcher.launch(params, 2, ec2=ec2) launcher.wait() for instance in instances: terminator.terminate(instance) assert len(terminator.awaited) > 0 terminator.wait() assert len(terminator.awaited) == 0 for instance in instances: assert instance.state['Name'] == 'terminated'
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)
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_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)