Ejemplo n.º 1
0
    def test_validation_file_exists(self):
        """ Tests that an error is raised if you don't use
        external, but the key does exist locally.
        """

        ctx = self.mock_ctx('test_validation_file_exists')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair(
                'test_validation_file_exists')
        ctx.node.properties['use_external_resource'] = False
        ctx.node.properties['resource_id'] = kp.name
        kp = ec2_client.delete_key_pair(
                'test_validation_file_exists')
        key_path = \
            self.create_dummy_key_path(ctx=ctx)
        self.addCleanup(os.remove, key_path)
        with open(key_path, 'w') as dummy_key:
            dummy_key.write('test')
        ex = self.assertRaises(
                NonRecoverableError, keypair.creation_validation, ctx=ctx)
        self.assertIn(
                'but the key file exists locally',
                ex.message)
Ejemplo n.º 2
0
    def test_create_group_rules_both_src_group_id_cidr(self):
        """ This tests that either src_group_id or cidr_ip is
        error is raised when neither is given.
        """

        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(
                'test_create_group_rules_both_src_group_id_or_cidr',
                'this is test')
        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock(
                'test_create_group_rules_both_src_group_id_or_cidr',
                test_properties)
        current_ctx.set(ctx=ctx)
        test_securitygroup = self.create_sg_for_checking()

        group_object = ec2_client.create_security_group(
                'dummy',
                'this is test')
        ctx.node.properties['rules'][0]['src_group_id'] = group_object
        ex = self.assertRaises(
                NonRecoverableError,
                test_securitygroup._create_group_rules,
                group)
        self.assertIn(
                'You need to pass either src_group_id OR cidr_ip.',
                ex.message)
    def test_validation_use_external_not_in_account(self):
        """ Tests that an error is raised if you use external,
        but the key pair doesn't exist in your account.
        """

        ctx = self.mock_ctx('test_validation_use_external_not_in_account')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair(
            'test_validation_use_external_not_in_account')
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = kp.name
        kp = ec2_client.delete_key_pair(
            'test_validation_use_external_not_in_account')
        key_path = \
            self.create_dummy_key_path(ctx=ctx)
        self.addCleanup(os.remove, key_path)
        with open(key_path, 'w') as dummy_key:
            dummy_key.write('test')
        ex = self.assertRaises(NonRecoverableError,
                               keypair.creation_validation,
                               ctx=ctx)
        self.assertIn('the key pair does not exist in the account', ex.message)
        self.assertIn('InvalidKeyPair.NotFound', ex.message)
Ejemplo n.º 4
0
    def test_update_rules(self):
        """This tests that create creates the runtime_properties"""

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_create_duplicate',
                                       test_properties)
        current_ctx.set(ctx=ctx)
        name = ctx.node.properties.get('resource_id')
        description = ctx.node.properties.get('description')
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(name, description)
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = group.id
        with mock.patch(
                'cloudify_aws.ec2.securitygroup.'
                'SecurityGroup.get_resource_state') as securitygroup_state:
            securitygroup_state.return_value = 'available'
            securitygroup.create(ctx=ctx)
        rules = [{
            'ip_protocol': 'udp',
            'from_port': '33333',
            'to_port': '33333',
            'cidr_ip': '192.168.122.40'
        }]
        securitygroup.update_rules(rules=rules, ctx=ctx)
        group_id = \
            ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID]
        group_list = ec2_client.get_all_security_groups(group_ids=group_id)
        group = group_list[0]
        all_rules = rules + ctx.node.properties['rules']
        self.assertEqual(len(all_rules), len(group.rules))
Ejemplo n.º 5
0
    def test_create_group_rules_both_src_group_id_cidr(self):
        """ This tests that either src_group_id or cidr_ip is
        error is raised when neither is given.
        """

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock(
            'test_create_group_rules_both_src_group_id_or_cidr',
            test_properties)
        current_ctx.set(ctx=ctx)
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(
            'test_create_group_rules_both_src_group_id_or_cidr',
            'this is test')
        test_securitygroup = self.create_sg_for_checking()
        group_object = ec2_client.create_security_group(
            'dummy', 'this is test')
        # setattr(group_object, 'vpc_id', 'vpc-abcd1234')
        ctx.node.properties['rules'][0]['src_group_id'] = group_object.id
        with mock.patch('cloudify_aws.ec2.securitygroup.'
                        'SecurityGroup.get_resource') as gr:
            gr.return_value = group
            ex = self.assertRaises(NonRecoverableError,
                                   test_securitygroup._create_group_rules,
                                   group)
            self.assertIn('You cannot pass both cidr_ip and src_group_id',
                          ex.message)
Ejemplo n.º 6
0
    def test_create_group_rules_src_group(self):
        """ This tests that _create_group_rules creates
        the rules and that they match on the way out
        to what when in.
        """

        ec2_client = connection.EC2ConnectionClient().client()
        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_create_group_rules_src_group',
                                       test_properties)
        group_object = ec2_client.create_security_group(
            'dummy', 'this is test')
        ctx.node.properties['rules'][0]['src_group_id'] = group_object.id
        del ctx.node.properties['rules'][0]['cidr_ip']
        current_ctx.set(ctx=ctx)
        with mock.patch(
                'cloudify_aws.ec2.securitygroup.'
                'SecurityGroup.get_resource_state') as securitygroup_state:
            securitygroup_state.return_value = 'available'
            securitygroup.create(ctx=ctx)
        group = ec2_client.get_all_security_groups(
            group_ids=ctx.instance.runtime_properties[
                constants.EXTERNAL_RESOURCE_ID])
        self.assertIn('test_security_group-111122223333',
                      str(group[0].rules[0].grants[0]))
    def test_validation_use_external(self):
        ctx = self.mock_ctx('test_validation_use_external')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair('test_validation_use_external')
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = kp.name
        ex = self.assertRaises(NonRecoverableError,
                               keypair.creation_validation,
                               ctx=ctx)
        self.assertIn('but the key file does not exist locally.', ex.message)
Ejemplo n.º 8
0
    def test_delete(self):
        """This tests that delete removes the runtime_properties"""

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_delete', test_properties)
        current_ctx.set(ctx=ctx)
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group('test',
                                                 'this is test')
        ctx.instance.runtime_properties['aws_resource_id'] = group.id
        securitygroup.SecurityGroup().deleted()
        self.assertNotIn('aws_resource_id', ctx.instance.runtime_properties)
Ejemplo n.º 9
0
 def test_execute(self):
     """ Tests that execute runs and
         returns the expected return value.
     """
     ctx = self.get_mock_ctx('test_execute')
     current_ctx.set(ctx=ctx)
     ec2_client = connection.EC2ConnectionClient().client()
     resource = AwsBase(client=ec2_client)
     output = resource.execute(resource.client.run_instances,
                               dict(image_id='ami-e214778a',
                                    instance_type='t1.micro'))
     reservation = resource.client.get_all_reservations()
     self.assertEqual(output.id, reservation[0].id)
    def test_delete(self):
        """ this tests that keypair delete removes the keypair from
            the account
        """
        ctx = self.mock_ctx('test_delete')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair('test_delete')
        ctx.instance.runtime_properties['aws_resource_id'] = kp.name
        ctx.instance.runtime_properties['key_path'] = \
            self.create_dummy_key_path(ctx=ctx)
        keypair.delete(ctx=ctx)
        self.assertEquals(None, ec2_client.get_key_pair(kp.name))
Ejemplo n.º 11
0
    def test_create_retry(self):
        """This tests that create retry works"""

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_create_retry', test_properties,
                                       retry_number=1)
        current_ctx.set(ctx=ctx)
        name = ctx.node.properties.get('resource_id')
        description = ctx.node.properties.get('description')
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(name, description)
        ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID] = \
            group.id
        securitygroup.create(ctx=ctx)
    def test_create_use_external(self):
        """ This tests that the create keypair function
            adds the key_pair_name to runtime properties.
        """

        ctx = self.mock_ctx('test_create_use_external')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair('test_create_use_external')
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = kp.name
        ex = self.assertRaises(NonRecoverableError, keypair.create, ctx=ctx)
        self.assertIn('External resource, but the key file does not exist',
                      ex.message)
Ejemplo n.º 13
0
    def test_start(self):
        """This tests that start adds tags"""

        ctx = self.mock_ctx('test_start')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        volume = ec2_client.create_volume(TEST_SIZE, TEST_ZONE)
        volume_id = volume.id
        ctx.instance.runtime_properties['aws_resource_id'] = volume_id
        ebs.start(ctx=ctx)
        volume_list = ec2_client.get_all_volumes(volume_ids=volume_id)
        volume_object = volume_list[0]
        self.assertEquals(volume_object.tags.get('resource_id'),
                          ctx.instance.id)
    def test_save_key_pair_missing_property(self):
        """ This tests that _save_key_pair raises an error when
        private_key_path is not set.
        """

        ctx = self.mock_ctx('test_save_key_pair_missing_property')
        current_ctx.set(ctx=ctx)
        test_keypair = self.create_kp_for_checking()

        ec2_client = connection.EC2ConnectionClient().client()
        del (ctx.node.properties['private_key_path'])
        kp = ec2_client.create_key_pair('test_create_use_external')
        ex = self.assertRaises(NonRecoverableError,
                               test_keypair._save_key_pair, kp)
        self.assertIn('Unable to get key file path, private_key_path not set',
                      ex.message)
Ejemplo n.º 15
0
    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())
Ejemplo n.º 16
0
    def test_delete(self):
        """This tests that delete removes the runtime_properties"""

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_delete',
                                       test_properties,
                                       operation_name='delete')
        current_ctx.set(ctx=ctx)
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group('test', 'this is test')
        ctx.instance.runtime_properties['aws_resource_id'] = group.id
        with mock.patch('cloudify_aws.base.AwsBaseNode.get_resource',
                        return_value=[]):
            securitygroup.SecurityGroup().delete_helper()
            self.assertNotIn('aws_resource_id',
                             ctx.instance.runtime_properties)
Ejemplo n.º 17
0
    def test_create_duplicate(self):
        """This tests that when you give a name of an existing
        resource, a NonRecoverableError is raised.
        """

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock(
                'test_create_duplicate', test_properties)
        current_ctx.set(ctx=ctx)
        name = ctx.node.properties.get('resource_id')
        description = ctx.node.properties.get('description')
        ec2_client = connection.EC2ConnectionClient().client()
        ec2_client.create_security_group(name, description)
        ex = self.assertRaises(
                NonRecoverableError, securitygroup.create, ctx=ctx)
        self.assertIn('InvalidGroup.Duplicate', ex.message)
    def test_validation_in_account(self):
        """ Tests that an error is raised if you don't use
        external, but the key does exist in your account.
        """

        ctx = self.mock_ctx('test_validation_in_account')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair('test_validation_in_account')
        ctx.node.properties['use_external_resource'] = False
        ctx.node.properties['resource_id'] = kp.name
        ex = self.assertRaises(NonRecoverableError,
                               keypair.creation_validation,
                               ctx=ctx)
        self.assertIn('but the key pair exists in the account.', ex.message)
Ejemplo n.º 19
0
    def test_create_use_external(self):
        """ This tests that the create keypair function
            adds the key_pair_name to runtime properties.
        """

        ctx = self.mock_ctx('test_create_use_external')
        current_ctx.set(ctx=ctx)
        ec2_client = connection.EC2ConnectionClient().client()
        kp = ec2_client.create_key_pair('test_create_use_external')
        with open(ctx.node.properties['private_key_path'], 'wb') as fp:
            fp.write(kp.material)
            fp.close()
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = kp.name
        keypair.create()
        self.assertIn('aws_resource_id',
                      ctx.instance.runtime_properties.keys())
Ejemplo n.º 20
0
 def test_get_and_filter_resources_by_matcher(self):
     """ Tests that get_and_filter_resources_by_matcher
         returns the right targets_and_types.
     """
     ctx = self.get_mock_ctx('test_get_and_filter_resources_by_matcher')
     current_ctx.set(ctx=ctx)
     ec2_client = connection.EC2ConnectionClient().client()
     resource = AwsBase(client=ec2_client)
     reservation = ec2_client.run_instances(
             'ami-e214778a', instance_type='t1.micro')
     instance_id = reservation.instances[0].id
     list_of_ids = [instance_id]
     filter_function = resource.client.get_all_reservations
     filters = {'instance_ids': list_of_ids}
     self.assertEqual(reservation.id,
                      resource.get_and_filter_resources_by_matcher(
                              filter_function, filters)[0].id)
Ejemplo n.º 21
0
 def test_get_all_matching(self):
     """ Tests that get_all_matching
         returns the right matched resources.
     """
     ctx = self.get_mock_ctx('test_get_all_matching')
     current_ctx.set(ctx=ctx)
     ec2_client = connection.EC2ConnectionClient().client()
     resource = AwsBaseNode('root', [], ec2_client, resource_states=[])
     reservation = ec2_client.run_instances(
             'ami-e214778a', instance_type='t1.micro')
     instance_id = reservation.instances[0].id
     list_of_ids = [instance_id]
     resource.get_all_handler['function'] = \
         resource.client.get_all_reservations
     resource.get_all_handler['argument'] = 'instance_ids'
     self.assertEqual(reservation.id,
                      resource.get_all_matching(list_of_ids)[0].id)
Ejemplo n.º 22
0
    def test_get_private_dns_name(self):
        """ This checks that _get_instance_attribute
        sets the correct runtime property and it is an FQDN
        """

        ctx = self.mock_ctx('test_get_private_dns_name')
        current_ctx.set(ctx=ctx)
        test_instance = self.create_instance_for_checking()

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        ctx.instance.runtime_properties['aws_resource_id'] = \
            reservation.instances[0].id
        property_name = 'private_dns_name'
        dns_name = test_instance._get_instance_attribute(property_name)
        self.assertRegexpMatches(dns_name, FQDN)
Ejemplo n.º 23
0
    def test_create_existing(self):
        """This tests that create creates the runtime_properties"""

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock(
                'test_create_duplicate', test_properties)
        current_ctx.set(ctx=ctx)
        name = ctx.node.properties.get('resource_id')
        description = ctx.node.properties.get('description')
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(name, description)
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = group.id
        securitygroup.create(ctx=ctx)
        self.assertEqual(
                ctx.instance.runtime_properties['aws_resource_id'],
                group.id)
Ejemplo n.º 24
0
    def test_validation_no_external_resource_is(self):
        """ this tests that creation_validation raises an error
        when a use_external_resource is false but a good instance_id
        is given.
        """
        ctx = self.mock_ctx('test_validation_no_external_resource_is')
        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'] = False
        ctx.node.properties['resource_id'] = instance_id
        ex = self.assertRaises(NonRecoverableError,
                               instance.creation_validation,
                               ctx=ctx)
        self.assertIn('Not external resource, but the supplied', ex.message)
Ejemplo n.º 25
0
    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')
Ejemplo n.º 26
0
 def test_delete_existing(self):
     """This tests that security group delete removed the
     runtime_properties
     """
     test_properties = self.get_mock_properties()
     ctx = self.security_group_mock(
             'test_delete_existing', test_properties)
     current_ctx.set(ctx=ctx)
     ec2_client = connection.EC2ConnectionClient().client()
     group = ec2_client.create_security_group('test_delete_existing',
                                              'this is test')
     ctx.node.properties['use_external_resource'] = True
     ctx.node.properties['resource_id'] = group.id
     ctx.instance.runtime_properties['aws_resource_id'] = group.id
     securitygroup.delete(ctx=ctx)
     self.assertNotIn(
             'aws_resource_id',
             ctx.instance.runtime_properties)
Ejemplo n.º 27
0
    def test_delete_deleted(self):
        """This tests that security group delete raises an
        error when the group is already deleted.
        """

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock(
                'test_delete_deleted', test_properties)
        current_ctx.set(ctx=ctx)
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group('test_delete_deleted',
                                                 'this is test')
        ctx.instance.runtime_properties['aws_resource_id'] = group.id
        ec2_client.delete_security_group(group_id=group.id)
        ex = self.assertRaises(
                NonRecoverableError, securitygroup.delete, ctx=ctx)
        self.assertIn('Cannot use_external_resource because resource',
                      ex.message)
Ejemplo n.º 28
0
    def test_create_group_rules_no_src_group_id_or_cidr(self):
        """ This tests that either src_group_id or cidr_ip is
        error is raised when both are given.
        """

        ec2_client = connection.EC2ConnectionClient().client()
        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock(
            'test_create_group_rules_no_src_group_id_or_cidr', test_properties)
        current_ctx.set(ctx=ctx)
        test_securitygroup = self.create_sg_for_checking()

        del ctx.node.properties['rules'][0]['cidr_ip']
        group = ec2_client.create_security_group(
            'test_create_group_rules_no_src_group_id_or_cidr', 'this is test')
        ex = self.assertRaises(NonRecoverableError,
                               test_securitygroup._create_group_rules, group)
        self.assertIn('is not a valid rule target cidr_ip or src_group_ip',
                      ex.message)
Ejemplo n.º 29
0
    def test_creation_validation_not_existing(self):
        """This tests that when use_external_resource is false
        if such a security group exists an error is raised.
        """

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_creation_validation_not_existing',
                                       test_properties)
        current_ctx.set(ctx=ctx)
        ctx.node.properties['use_external_resource'] = False
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(
            'test_creation_validation_not_existing', 'this is a test')
        ctx.node.properties['resource_id'] = group.id
        ex = self.assertRaises(NonRecoverableError,
                               securitygroup.creation_validation,
                               ctx=ctx)
        self.assertIn('Not external resource, but the supplied group',
                      ex.message)
Ejemplo n.º 30
0
    def test_create_retry(self):
        """This tests that create retry works"""

        test_properties = self.get_mock_properties()
        ctx = self.security_group_mock('test_create_retry',
                                       test_properties,
                                       retry_number=1)
        current_ctx.set(ctx=ctx)
        name = ctx.node.properties.get('resource_id')
        description = ctx.node.properties.get('description')
        ec2_client = connection.EC2ConnectionClient().client()
        group = ec2_client.create_security_group(name, description)
        ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID] = \
            group.id
        with mock.patch(
                'cloudify_aws.ec2.securitygroup.'
                'SecurityGroup.get_resource_state') as securitygroup_state:
            securitygroup_state.return_value = 'available'
            securitygroup.create(ctx=ctx)