Merge in master.

This commit is contained in:
Steve Pulec 2017-03-05 09:58:39 -05:00
commit 3b4ef2cf15
26 changed files with 565 additions and 65 deletions

View file

@ -236,6 +236,10 @@ template = {
"Ref": "AWS::StackId"
},
"Key": "Application"
},
{
"Value": "Bar",
"Key": "Foo"
}
],
"SecurityGroupIds": [

View file

@ -12,7 +12,7 @@ import sure # noqa
import tests.backport_assert_raises # noqa
from nose.tools import assert_raises
from moto import mock_cloudformation_deprecated, mock_s3_deprecated
from moto import mock_cloudformation_deprecated, mock_s3_deprecated, mock_route53_deprecated
from moto.cloudformation import cloudformation_backends
dummy_template = {
@ -69,6 +69,57 @@ def test_create_stack():
})
@mock_cloudformation_deprecated
@mock_route53_deprecated
def test_create_stack_hosted_zone_by_id():
conn = boto.connect_cloudformation()
dummy_template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Stack 1",
"Parameters": {
},
"Resources": {
"Bar": {
"Type" : "AWS::Route53::HostedZone",
"Properties" : {
"Name" : "foo.bar.baz",
}
},
},
}
dummy_template2 = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Stack 2",
"Parameters": {
"ZoneId": { "Type": "String" }
},
"Resources": {
"Foo": {
"Properties": {
"HostedZoneId": {"Ref": "ZoneId"},
"RecordSets": []
},
"Type": "AWS::Route53::RecordSetGroup"
}
},
}
conn.create_stack(
"test_stack",
template_body=json.dumps(dummy_template),
parameters={}.items()
)
r53_conn = boto.connect_route53()
zone_id = r53_conn.get_zones()[0].id
conn.create_stack(
"test_stack",
template_body=json.dumps(dummy_template2),
parameters={"ZoneId": zone_id}.items()
)
stack = conn.describe_stacks()[0]
assert stack.list_resources()
@mock_cloudformation_deprecated
def test_creating_stacks_across_regions():
west1_conn = boto.cloudformation.connect_to_region("us-west-1")
@ -281,6 +332,60 @@ def test_cloudformation_params():
param.value.should.equal('testing123')
@mock_cloudformation_deprecated
def test_cloudformation_params_conditions_and_resources_are_distinct():
dummy_template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Stack 1",
"Conditions": {
"FooEnabled": {
"Fn::Equals": [
{
"Ref": "FooEnabled"
},
"true"
]
},
"FooDisabled": {
"Fn::Not": [
{
"Fn::Equals": [
{
"Ref": "FooEnabled"
},
"true"
]
}
]
}
},
"Parameters": {
"FooEnabled": {
"Type": "String",
"AllowedValues": [
"true",
"false"
]
}
},
"Resources": {
"Bar": {
"Properties": {
"CidrBlock": "192.168.0.0/16",
},
"Condition": "FooDisabled",
"Type": "AWS::EC2::VPC"
}
}
}
dummy_template_json = json.dumps(dummy_template)
cfn = boto.connect_cloudformation()
cfn.create_stack('test_stack1', template_body=dummy_template_json, parameters=[('FooEnabled', 'true')])
stack = cfn.describe_stacks('test_stack1')[0]
resources = stack.list_resources()
assert not [resource for resource in resources if resource.logical_resource_id == 'Bar']
@mock_cloudformation_deprecated
def test_stack_tags():
conn = boto.connect_cloudformation()
@ -341,6 +446,42 @@ def test_update_stack():
})
@mock_cloudformation_deprecated
def test_update_stack_with_parameters():
dummy_template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Stack",
"Resources": {
"VPC": {
"Properties": {
"CidrBlock": {"Ref": "Bar"}
},
"Type": "AWS::EC2::VPC"
}
},
"Parameters": {
"Bar": {
"Type": "String"
}
}
}
dummy_template_json = json.dumps(dummy_template)
conn = boto.connect_cloudformation()
conn.create_stack(
"test_stack",
template_body=dummy_template_json,
parameters=[("Bar", "192.168.0.0/16")]
)
conn.update_stack(
"test_stack",
template_body=dummy_template_json,
parameters=[("Bar", "192.168.0.1/16")]
)
stack = conn.describe_stacks()[0]
assert stack.parameters[0].value == "192.168.0.1/16"
@mock_cloudformation_deprecated
def test_update_stack_when_rolled_back():
conn = boto.connect_cloudformation()
@ -374,16 +515,21 @@ def test_describe_stack_events_shows_create_update_and_delete():
events[0].resource_type.should.equal("AWS::CloudFormation::Stack")
events[-1].resource_type.should.equal("AWS::CloudFormation::Stack")
# testing ordering of stack events without assuming resource events will
# not exist
# testing ordering of stack events without assuming resource events will not exist
# the AWS API returns events in reverse chronological order
stack_events_to_look_for = iter([
("CREATE_IN_PROGRESS", "User Initiated"), ("CREATE_COMPLETE", None),
("UPDATE_IN_PROGRESS", "User Initiated"), ("UPDATE_COMPLETE", None),
("DELETE_IN_PROGRESS", "User Initiated"), ("DELETE_COMPLETE", None)])
("DELETE_COMPLETE", None),
("DELETE_IN_PROGRESS", "User Initiated"),
("UPDATE_COMPLETE", None),
("UPDATE_IN_PROGRESS", "User Initiated"),
("CREATE_COMPLETE", None),
("CREATE_IN_PROGRESS", "User Initiated"),
])
try:
for event in events:
event.stack_id.should.equal(stack_id)
event.stack_name.should.equal("test_stack")
event.event_id.should.match(r"[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}")
if event.resource_type == "AWS::CloudFormation::Stack":
event.logical_resource_id.should.equal("test_stack")

View file

@ -354,16 +354,21 @@ def test_stack_events():
events[0].resource_type.should.equal("AWS::CloudFormation::Stack")
events[-1].resource_type.should.equal("AWS::CloudFormation::Stack")
# testing ordering of stack events without assuming resource events will
# not exist
# testing ordering of stack events without assuming resource events will not exist
# the AWS API returns events in reverse chronological order
stack_events_to_look_for = iter([
("CREATE_IN_PROGRESS", "User Initiated"), ("CREATE_COMPLETE", None),
("UPDATE_IN_PROGRESS", "User Initiated"), ("UPDATE_COMPLETE", None),
("DELETE_IN_PROGRESS", "User Initiated"), ("DELETE_COMPLETE", None)])
("DELETE_COMPLETE", None),
("DELETE_IN_PROGRESS", "User Initiated"),
("UPDATE_COMPLETE", None),
("UPDATE_IN_PROGRESS", "User Initiated"),
("CREATE_COMPLETE", None),
("CREATE_IN_PROGRESS", "User Initiated"),
])
try:
for event in events:
event.stack_id.should.equal(stack.stack_id)
event.stack_name.should.equal("test_stack")
event.event_id.should.match(r"[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}")
if event.resource_type == "AWS::CloudFormation::Stack":
event.logical_resource_id.should.equal("test_stack")

View file

@ -548,6 +548,7 @@ def test_autoscaling_group_with_elb():
"LaunchConfigurationName": {"Ref": "my-launch-config"},
"MinSize": "2",
"MaxSize": "2",
"DesiredCapacity": "2",
"LoadBalancerNames": [{"Ref": "my-elb"}]
},
},
@ -631,6 +632,7 @@ def test_autoscaling_group_update():
"LaunchConfigurationName": {"Ref": "my-launch-config"},
"MinSize": "2",
"MaxSize": "2",
"DesiredCapacity": "2"
},
},
@ -655,6 +657,7 @@ def test_autoscaling_group_update():
asg = autoscale_conn.get_all_groups()[0]
asg.min_size.should.equal(2)
asg.max_size.should.equal(2)
asg.desired_capacity.should.equal(2)
asg_template['Resources']['my-as-group']['Properties']['MaxSize'] = 3
asg_template_json = json.dumps(asg_template)
@ -665,6 +668,7 @@ def test_autoscaling_group_update():
asg = autoscale_conn.get_all_groups()[0]
asg.min_size.should.equal(2)
asg.max_size.should.equal(3)
asg.desired_capacity.should.equal(2)
@mock_ec2_deprecated()
@ -693,6 +697,7 @@ def test_vpc_single_instance_in_subnet():
ec2_conn = boto.ec2.connect_to_region("us-west-1")
reservation = ec2_conn.get_all_instances()[0]
instance = reservation.instances[0]
instance.tags["Foo"].should.equal("Bar")
# Check that the EIP is attached the the EC2 instance
eip = ec2_conn.get_all_addresses()[0]
eip.domain.should.equal('vpc')
@ -714,7 +719,7 @@ def test_vpc_single_instance_in_subnet():
eip_resource = [
resource for resource in resources if resource.resource_type == 'AWS::EC2::EIP'][0]
eip_resource.physical_resource_id.should.equal(eip.allocation_id)
eip_resource.physical_resource_id.should.equal(eip.public_ip)
@mock_cloudformation()
@ -1027,7 +1032,7 @@ def test_vpc_eip():
resources = stack.describe_resources()
cfn_eip = [
resource for resource in resources if resource.resource_type == 'AWS::EC2::EIP'][0]
cfn_eip.physical_resource_id.should.equal(eip.allocation_id)
cfn_eip.physical_resource_id.should.equal(eip.public_ip)
@mock_ec2_deprecated()

View file

@ -622,6 +622,58 @@ def test_describe_container_instances():
for arn in test_instance_arns:
response_arns.should.contain(arn)
@mock_ec2
@mock_ecs
def test_update_container_instances_state():
ecs_client = boto3.client('ecs', region_name='us-east-1')
ec2 = boto3.resource('ec2', region_name='us-east-1')
test_cluster_name = 'test_ecs_cluster'
_ = ecs_client.create_cluster(
clusterName=test_cluster_name
)
instance_to_create = 3
test_instance_arns = []
for i in range(0, instance_to_create):
test_instance = ec2.create_instances(
ImageId="ami-1234abcd",
MinCount=1,
MaxCount=1,
)[0]
instance_id_document = json.dumps(
ec2_utils.generate_instance_identity_document(test_instance)
)
response = ecs_client.register_container_instance(
cluster=test_cluster_name,
instanceIdentityDocument=instance_id_document)
test_instance_arns.append(response['containerInstance']['containerInstanceArn'])
test_instance_ids = list(map((lambda x: x.split('/')[1]), test_instance_arns))
response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='DRAINING')
len(response['failures']).should.equal(0)
len(response['containerInstances']).should.equal(instance_to_create)
response_statuses = [ci['status'] for ci in response['containerInstances']]
for status in response_statuses:
status.should.equal('DRAINING')
response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='DRAINING')
len(response['failures']).should.equal(0)
len(response['containerInstances']).should.equal(instance_to_create)
response_statuses = [ci['status'] for ci in response['containerInstances']]
for status in response_statuses:
status.should.equal('DRAINING')
response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='ACTIVE')
len(response['failures']).should.equal(0)
len(response['containerInstances']).should.equal(instance_to_create)
response_statuses = [ci['status'] for ci in response['containerInstances']]
for status in response_statuses:
status.should.equal('ACTIVE')
ecs_client.update_container_instances_state.when.called_with(cluster=test_cluster_name, containerInstances=test_instance_ids, status='test_status').should.throw(Exception)
@mock_ec2
@mock_ecs

View file

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import boto
import boto3
import sure # noqa
from nose.tools import assert_raises
@ -72,3 +73,40 @@ def test_get_groups_for_user():
groups = conn.get_groups_for_user(
'my-user')['list_groups_for_user_response']['list_groups_for_user_result']['groups']
groups.should.have.length_of(2)
@mock_iam_deprecated()
def test_put_group_policy():
conn = boto.connect_iam()
conn.create_group('my-group')
conn.put_group_policy('my-group', 'my-policy', '{"some": "json"}')
@mock_iam_deprecated()
def test_get_group_policy():
conn = boto.connect_iam()
conn.create_group('my-group')
with assert_raises(BotoServerError):
conn.get_group_policy('my-group', 'my-policy')
conn.put_group_policy('my-group', 'my-policy', '{"some": "json"}')
policy = conn.get_group_policy('my-group', 'my-policy')
@mock_iam_deprecated()
def test_get_all_group_policies():
conn = boto.connect_iam()
conn.create_group('my-group')
policies = conn.get_all_group_policies('my-group')['list_group_policies_response']['list_group_policies_result']['policy_names']
assert policies == []
conn.put_group_policy('my-group', 'my-policy', '{"some": "json"}')
policies = conn.get_all_group_policies('my-group')['list_group_policies_response']['list_group_policies_result']['policy_names']
assert policies == ['my-policy']
@mock_iam()
def test_list_group_policies():
conn = boto3.client('iam')
conn.create_group(GroupName='my-group')
policies = conn.list_group_policies(GroupName='my-group')['PolicyNames'].should.be.empty
conn.put_group_policy(GroupName='my-group', PolicyName='my-policy', PolicyDocument='{"some": "json"}')
policies = conn.list_group_policies(GroupName='my-group')['PolicyNames'].should.equal(['my-policy'])

View file

@ -77,7 +77,7 @@ def test_create_queues_in_multiple_region():
list(west2_conn.list_queues()['QueueUrls']).should.have.length_of(1)
west1_conn.list_queues()['QueueUrls'][0].should.equal(
'http://sqs.us-west-1.amazonaws.com/123456789012/blah')
'https://us-west-1.queue.amazonaws.com/123456789012/blah')
@mock_sqs
@ -92,7 +92,7 @@ def test_get_queue_with_prefix():
queue = conn.list_queues(QueueNamePrefix="test-")['QueueUrls']
queue.should.have.length_of(1)
queue[0].should.equal(
"http://sqs.us-west-1.amazonaws.com/123456789012/test-queue")
"https://us-west-1.queue.amazonaws.com/123456789012/test-queue")
@mock_sqs