def test_run_instances_provider_context_good_subnet(self): """ this tests that the instance create function adds the runtime_properties """ vpc = self.create_vpc_client().create_vpc('10.10.10.0/16') subnet = self.create_vpc_client().create_subnet( vpc.id, '10.10.10.0/24', availability_zone=TEST_AVAILABILITY_ZONE) ctx = self.mock_ctx('test_run_instances_provider_context_good_subnet') ctx.provider_context['resources'] = { constants.VPC['AWS_RESOURCE_TYPE']: { 'id': vpc.id, 'external_resource': True }, constants.SUBNET['AWS_RESOURCE_TYPE']: { 'id': subnet.id, 'external_resource': True }, constants.AGENTS_AWS_INSTANCE_PARAMETERS: { 'placement': TEST_AVAILABILITY_ZONE } } current_ctx.set(ctx=ctx) instance.Instance().create(ctx=ctx) self.assertIn('aws_resource_id', ctx.instance.runtime_properties.keys()) self.assertIn('subnet_id', ctx.instance.runtime_properties.keys()) self.assertEquals(subnet.id, ctx.instance.runtime_properties['subnet_id']) self.assertIn('vpc_id', ctx.instance.runtime_properties.keys()) self.assertEquals(vpc.id, ctx.instance.runtime_properties['vpc_id']) self.assertIn('placement', ctx.instance.runtime_properties.keys()) self.assertEquals(TEST_AVAILABILITY_ZONE, ctx.instance.runtime_properties['placement'])
def test_bootstrap_context_key_pair(self, *_): """tests finding a key file via bootstrap context """ name = 'test_bootstrap_context_key_pair' private_key_dir = tempfile.mkdtemp() private_key_path = '{0}/{1}.pem' \ .format(private_key_dir, name) open(private_key_path, 'w').close() cloudify_agent = MockContext() cloudify_agent['agent_key_path'] = private_key_path bootstrap_ctx = BootstrapContext({'cloudify_agent': cloudify_agent}) ctx = MockCloudifyContext(bootstrap_context=bootstrap_ctx, node_name=name) current_ctx.set(ctx=ctx) with mock.patch( 'cloudify_aws.utils.get_single_connected_node_by_type') \ as mock_get_connected: with mock.patch('cloudify_aws.ec2.instance.Instance.__init__') \ as mock_instance_init: mock_instance_init.return_value = None mock_get_connected.return_value = None output = instance.Instance()._get_private_key('') self.assertEqual(private_key_path, output)
def test_creation_validation_image_id(self): """This tests that creation validation gets to image_id it will return NonRecoverableError if the image_id doesn't exist. """ ctx = self.mock_ctx('test_creation_validation_image_id') current_ctx.set(ctx=ctx) ctx.node.properties['image_id'] = 'abc' ctx.node.properties['instance_type'] = 'efg' with self.assertRaisesRegexp(NonRecoverableError, 'Invalid id:'): instance.Instance().creation_validation(ctx=ctx)
def test_run_instances_bad_subnet_id(self): """ This tests that the NonRecoverableError is triggered when an non existing subnet_id is included in the create statement """ ctx = self.mock_ctx('test_run_instances_bad_subnet_id') ctx.node.properties['parameters']['subnet_id'] = 'test' current_ctx.set(ctx=ctx) ex = self.assertRaises(NonRecoverableError, instance.Instance().create, ctx=ctx) self.assertIn('InvalidSubnetID.NotFound', ex.message)
def test_run_instances_external_resource(self): """ this tests that the instance create function adds the runtime_properties """ ctx = self.mock_ctx('test_run_instances_external_resource') current_ctx.set(ctx=ctx) ec2_client = connection.EC2ConnectionClient().client() reservation = ec2_client.run_instances( TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) instance_id = reservation.instances[0].id ctx.node.properties['use_external_resource'] = True ctx.node.properties['resource_id'] = instance_id instance.Instance().create(ctx=ctx) self.assertIn('aws_resource_id', ctx.instance.runtime_properties.keys())
def test_terminate_bad_id(self): """ this tests that a terminate fails when given an invalid instance_id and the proper error is raised """ ctx = self.mock_ctx('test_terminate_bad_id') current_ctx.set(ctx=ctx) ctx.instance.runtime_properties['aws_resource_id'] = 'bad_id' ctx.instance.runtime_properties['private_dns_name'] = '0.0.0.0' ctx.instance.runtime_properties['public_dns_name'] = '0.0.0.0' ctx.instance.runtime_properties['public_ip_address'] = '0.0.0.0' ctx.instance.runtime_properties['ip'] = '0.0.0.0' ex = self.assertRaises(NonRecoverableError, instance.Instance().delete, ctx=ctx) self.assertIn('InvalidInstanceID.NotFound', ex.message)
def test_run_instances_provider_context_bad_subnet(self): """ this tests that the instance create function adds the runtime_properties """ ctx = self.mock_ctx('test_run_instances_provider_context_bad_subnet') ctx.provider_context['resources'] = { constants.SUBNET['AWS_RESOURCE_TYPE']: { 'id': SUBNET_ID, 'external_resource': True } } current_ctx.set(ctx=ctx) e = self.assertRaises(NonRecoverableError, instance.Instance().create, ctx=ctx) self.assertIn(SUBNET_ID, e.message)
def test_terminate_clean(self): """ this tests that the instance.terminate function terminates the instance """ ctx = self.mock_ctx('test_terminate_clean') current_ctx.set(ctx=ctx) ec2_client = connection.EC2ConnectionClient().client() reservation = ec2_client.run_instances( TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) instance_id = reservation.instances[0].id ctx.instance.runtime_properties['aws_resource_id'] = instance_id instance.Instance().delete(ctx=ctx) reservations = ec2_client.get_all_reservations(instance_id) instance_object = reservations[0].instances[0] state = instance_object.update() self.assertEqual(state, 'terminated')
def test_windows_runtime_property_created(self, *_): """tests that the generated windows password is saved as a runtime property """ ctx = self.mock_ctx('test_windows_runtime_property_created') current_ctx.set(ctx=ctx) ec2_client = connection.EC2ConnectionClient().client() reservation = ec2_client.run_instances( TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) instance_id = reservation.instances[0].id ctx.instance.runtime_properties['aws_resource_id'] = instance_id ctx.node.properties['use_password'] = True with mock.patch( 'cloudify_aws.ec2.instance.Instance._get_windows_password') \ as mock_get_windows_password: mock_get_windows_password.return_value = 'pass' instance.Instance().start(ctx=ctx) self.assertEqual( ctx.instance.runtime_properties[ constants.ADMIN_PASSWORD_PROPERTY], 'pass')
def test_stop_clean(self): """ this tests that the instance stop function stops the isntance """ ctx = self.mock_ctx('test_stop_clean') current_ctx.set(ctx=ctx) ec2_client = connection.EC2ConnectionClient().client() reservation = ec2_client.run_instances( TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) instance_id = reservation.instances[0].id ctx.instance.runtime_properties['aws_resource_id'] = instance_id ctx.instance.runtime_properties['private_dns_name'] = '0.0.0.0' ctx.instance.runtime_properties['public_dns_name'] = '0.0.0.0' ctx.instance.runtime_properties['public_ip_address'] = '0.0.0.0' ctx.instance.runtime_properties['ip'] = '0.0.0.0' ctx.instance.runtime_properties['placement'] = TEST_AVAILABILITY_ZONE instance.Instance().stop(ctx=ctx) reservations = ec2_client.get_all_reservations(instance_id) instance_object = reservations[0].instances[0] state = instance_object.update() self.assertEqual(state, 'stopped')
def create_instance_for_checking(self): return instance.Instance()