def test_get_stack_instances_all(self): stack_name = "our-stack" cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') cfn.filter_stack_instances = mock.Mock() cfn.get_stack_instances(stack_name, running_only=False) cfn.filter_stack_instances.assert_called_once_with(stack_name, filters={})
def test_get_stack_instance_ids(self): stack_name = "our-stack" cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') cfn.get_stack_instances = mock.Mock(return_value=[]) cfn.get_stack_instance_ids(stack_name) cfn.get_stack_instances.assert_called_once_with(stack_name, running_only=True)
def test_get_stack_instances_running_only(self): stack_name = "our-stack" cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') cfn.filter_stack_instances = mock.Mock(return_value=[]) cfn.get_stack_instances(stack_name) cfn.filter_stack_instances.assert_called_once_with( stack_name, filters={'instance-state-name': 'running'})
def test_get_stack_id(self): stack_id = "arn:aws:cloudformation:eu-west-1:123/stack-name/uuid" stack = mock.Mock() type(stack).stack_id = stack_id self.cfn_conn_mock.describe_stacks.return_value = [stack] cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') self.assertEqual(cfn.get_stack_id("stack-name"), stack_id) self.assertEqual(cfn.get_stack_id(stack_id), stack_id)
def test_get_stack_instance_public_ips(self): cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') cloudformation.Cloudformation.get_stack_instance_ids = mock.Mock() ec = ec2.EC2(self.env.aws_profile, 'aws_region') with patch.object(ec, 'get_instance_public_ips', return_value=['1.1.1.1', '2.2.2.2']): x = ec.get_stack_instance_public_ips('mock-stack-name') cfn.get_stack_instance_ids.assert_called_with('mock-stack-name') compare(x, ['1.1.1.1', '2.2.2.2'])
def test_cf_delete(self): cf_mock = mock.Mock() cf_connect_result = mock.Mock(name='cf_connect') cf_mock.return_value = cf_connect_result example_return = {'DeleteStackResponse': {'ResponseMetadata': {'RequestId': 'someuuid'}}} mock_config = {'delete_stack.return_value': example_return} cf_connect_result.configure_mock(**mock_config) boto.cloudformation.connect_to_region = cf_mock cf = cloudformation.Cloudformation(self.env.aws_profile) x = cf.delete(self.stack_name) self.assertTrue('DeleteStackResponse' in x.keys())
def test_stack_not_missing(self): stack_mock = mock.Mock(stack_name='my-stack-name') cf_mock = mock.Mock() cf_connect_result = mock.Mock(name='cf_connect') cf_mock.return_value = cf_connect_result mock_config = {'describe_stacks.return_value': [stack_mock]} cf_connect_result.configure_mock(**mock_config) boto.cloudformation.connect_to_region = cf_mock cf = cloudformation.Cloudformation(self.env.aws_profile) x = cf.stack_missing('my-stack-name') self.assertFalse(x)
def test_wait_for_stack_not_missing(self): stack_mock = mock.Mock(stack_name='my-stack-name') cf_mock = mock.Mock() cf_connect_result = mock.Mock(name='cf_connect') cf_mock.return_value = cf_connect_result mock_config = {'describe_stacks.return_value': [stack_mock]} cf_connect_result.configure_mock(**mock_config) boto.cloudformation.connect_to_region = cf_mock cf = cloudformation.Cloudformation(self.env.aws_profile) with self.assertRaises(errors.CfnTimeoutError): cf.wait_for_stack_missing('my-stack-name', 1, 1)
def test_filter_stack_instances_when_no_stack(self): stack_name = "our-stack" stack_id = "aws:arn:stack-id" cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') cfn.get_stack_id = mock.Mock(return_value=stack_id) cfn.conn_ec2.get_all_reservations = mock.Mock(return_value=[]) self.assertEqual(cfn.filter_stack_instances(stack_name, {}), []) self.assertEqual( cfn.conn_ec2.get_all_reservations.get_all_reservations.called, False, "get_all_reservations should not be called")
def test_stack_not_done(self): stack_evt_mock = mock.Mock() rt = mock.PropertyMock(return_value='AWS::CloudFormation::Stack') rs = mock.PropertyMock(return_value='CREATE_COMPLETE_FAKE') type(stack_evt_mock).resource_type = rt type(stack_evt_mock).resource_status = rs cf_mock = mock.Mock() cf_connect_result = mock.Mock(name='cf_connect') cf_mock.return_value = cf_connect_result mock_config = {'describe_stack_events.return_value': [stack_evt_mock]} cf_connect_result.configure_mock(**mock_config) boto.cloudformation.connect_to_region = cf_mock self.assertFalse(cloudformation.Cloudformation( self.env.aws_profile).stack_done(self.stack_name))
def test_cf_create(self): cf_mock = mock.Mock() cf_connect_result = mock.Mock(name='cf_connect') cf_mock.return_value = cf_connect_result mock_config = {'create_stack.return_value': self.stack_name} cf_connect_result.configure_mock(**mock_config) boto.cloudformation.connect_to_region = cf_mock test_tags = {'Env': 'Dev', 'MiscTag': 'misc'} cf = cloudformation.Cloudformation(self.env.aws_profile) x = cf.create(self.stack_name, '{}', test_tags) # Check we called create with the right values cf_connect_result.create_stack.assert_called_once_with( template_body='{}', stack_name=self.stack_name, capabilities=['CAPABILITY_IAM'], tags=test_tags) self.assertEqual(x, self.stack_name)
def test_stack_wait_for_stack_not_done(self): stack_evt_mock = mock.Mock() rt = mock.PropertyMock(return_value='AWS::CloudFormation::Stack') rs = mock.PropertyMock(return_value='CREATE_COMPLETE_LOL') type(stack_evt_mock).resource_type = rt type(stack_evt_mock).resource_status = rs mock_config = {'describe_stack_events.return_value': [stack_evt_mock]} cf_mock = mock.Mock() cf_connect_result = mock.Mock(name='cf_connect') cf_mock.return_value = cf_connect_result cf_connect_result.configure_mock(**mock_config) boto.cloudformation.connect_to_region = cf_mock with self.assertRaises(errors.CfnTimeoutError): print cloudformation.Cloudformation( self.env.aws_profile).wait_for_stack_done(self.stack_name, 1, 1)
def test_filter_stack_instances(self): """ All we care about testing in this function is that we call boto.ec2.get_all_reservations with the correct filter tags - we trust boto has implemented that function correctly """ stack_name = "our-stack" stack_id = "aws:arn:stack-id" cfn = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region') cfn.get_stack_id = mock.Mock(return_value=stack_id) instance = mock.Mock(name="MockInstance") reservation = mock.Mock() type(reservation).instances = mock.PropertyMock( return_value=[instance]) cfn.conn_ec2.get_all_reservations = mock.Mock( return_value=[reservation]) got = cfn.filter_stack_instances(stack_name, {}) expected = [instance] self.assertEqual(got, expected) # Check we called boto with the right filters cfn.conn_ec2.get_all_reservations.assert_called_once_with( filters={ 'tag:aws:cloudformation:stack-id': stack_id, }) cfn.conn_ec2.get_all_reservations.reset_mock() cfn.conn_ec2.get_all_reservations.return_value = [] self.assertEqual( cfn.filter_stack_instances(stack_name, {'tag:x': '1'}), []) cfn.conn_ec2.get_all_reservations.assert_called_once_with( filters={ 'tag:aws:cloudformation:stack-id': stack_id, 'tag:x': '1', })
def setUp(self): self.work_dir = tempfile.mkdtemp() self.env = mock.Mock() self.env.aws = 'dev' self.env.aws_profile = 'the-profile-name' self.env.environment = 'dev' self.env.application = 'unittest-app' self.env.config = os.path.join(self.work_dir, 'test_config.yaml') config = {'dev': {'ec2': {'auto_scaling': {'desired': 1, 'max': 3, 'min': 0}, 'block_devices': [{'DeviceName': '/dev/sda1', 'VolumeSize': 10}, {'DeviceName': '/dev/sdf', 'VolumeType': 'gp2', 'VolumeSize': 10}, {'DeviceName': '/dev/sdg', 'VolumeType': 'io1', 'VolumeSize': 20, 'Iops': 100}], 'parameters': {'InstanceType': 't2.micro', 'KeyName': 'default'}, 'security_groups': [{'CidrIp': '0.0.0.0/0', 'FromPort': 22, 'IpProtocol': 'tcp', 'ToPort': 22}, {'CidrIp': '0.0.0.0/0', 'FromPort': 80, 'IpProtocol': 'tcp', 'ToPort': 80}], 'tags': {'Apps': 'test', 'Env': 'dev', 'Role': 'docker'}}, 'elb': [{'hosted_zone': 'kyrtest.pf.dsd.io.', 'listeners': [{'InstancePort': 80, 'LoadBalancerPort': 80, 'Protocol': 'TCP'}, {'InstancePort': 443, 'LoadBalancerPort': 443, 'Protocol': 'TCP', 'PolicyNames': 'PinDownSSLNegotiationPolicy201505'}], 'name': 'test-dev-external', 'scheme': 'internet-facing'}, {'hosted_zone': 'kyrtest.pf.dsd.io.', 'listeners': [{'InstancePort': 80, 'LoadBalancerPort': 80, 'Protocol': 'TCP'}], 'name': 'test-dev-internal', 'scheme': 'internet-facing'}], 'rds': {'backup-retention-period': 1, 'db-engine': 'postgres', 'db-engine-version': '9.5.4', 'db-master-password': '******', 'db-master-username': '******', 'db-name': 'test', 'instance-class': 'db.t2.micro', 'multi-az': False, 'storage': 5, 'storage-type': 'gp2'}, 's3': {'static-bucket-name': 'moj-test-dev-static'}}} yaml.dump(config, open(self.env.config, 'w')) self.stack_name = '{0}-{1}'.format(self.env.application, self.env.environment) self.cf = cloudformation.Cloudformation(self.env.aws_profile, 'aws_region')