예제 #1
0
 def test_attach_detach_volume(self):
     """
     Create a new volume, and attempt to attach it to an instance
     """
     instance_name = "CBVolOps-{0}-{1}".format(
         self.provider.name,
         uuid.uuid4())
     net, _ = helpers.create_test_network(self.provider, instance_name)
     test_instance = helpers.get_test_instance(self.provider, instance_name,
                                               network=net)
     with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
             test_instance, net)):
         name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
         test_vol = self.provider.block_store.volumes.create(
             name, 1, test_instance.zone_id)
         with helpers.cleanup_action(lambda: test_vol.delete()):
             test_vol.wait_till_ready()
             test_vol.attach(test_instance, '/dev/sda2')
             test_vol.wait_for(
                 [VolumeState.IN_USE],
                 terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
             test_vol.detach()
             test_vol.wait_for(
                 [VolumeState.AVAILABLE],
                 terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
    def test_crud_vm_firewall_rules(self):
        name = 'cb_crudfw_rules-{0}'.format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(network=net)):
            net, _ = helpers.create_test_network(self.provider, name)

            fw = None
            with helpers.cleanup_action(lambda: fw.delete()):
                fw = self.provider.security.vm_firewalls.create(
                    name=name, description=name, network_id=net.id)

                def create_fw_rule(name):
                    return fw.rules.create(direction=TrafficDirection.INBOUND,
                                           protocol='tcp',
                                           from_port=1111,
                                           to_port=1111,
                                           cidr='0.0.0.0/0')

                def cleanup_fw_rule(rule):
                    rule.delete()

                sit.check_crud(self,
                               fw.rules,
                               VMFirewallRule,
                               "cb_crudfwrule",
                               create_fw_rule,
                               cleanup_fw_rule,
                               skip_name_check=True)
    def test_vm_firewall_rule_add_twice(self):
        name = 'cb_fwruletwice-{0}'.format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        fw = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, vm_firewall=fw)):

            net, _ = helpers.create_test_network(self.provider, name)
            fw = self.provider.security.vm_firewalls.create(name=name,
                                                            description=name,
                                                            network_id=net.id)

            rule = fw.rules.create(direction=TrafficDirection.INBOUND,
                                   protocol='tcp',
                                   from_port=1111,
                                   to_port=1111,
                                   cidr='0.0.0.0/0')
            # attempting to add the same rule twice should succeed
            same_rule = fw.rules.create(direction=TrafficDirection.INBOUND,
                                        protocol='tcp',
                                        from_port=1111,
                                        to_port=1111,
                                        cidr='0.0.0.0/0')
            self.assertEqual(rule, same_rule)
    def test_volume_properties(self):
        """
        Test volume properties
        """
        instance_name = "CBVolProps-{0}-{1}".format(self.provider.name,
                                                    uuid.uuid4())
        vol_desc = 'newvoldesc1'
        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance, net)):
            net, subnet = helpers.create_test_network(self.provider,
                                                      instance_name)
            test_instance = helpers.get_test_instance(self.provider,
                                                      instance_name,
                                                      subnet=subnet)

            name = "CBUnitTestVolProps-{0}".format(uuid.uuid4())
            test_vol = self.provider.block_store.volumes.create(
                name, 1, test_instance.zone_id, description=vol_desc)
            with helpers.cleanup_action(lambda: test_vol.delete()):
                test_vol.wait_till_ready()
                self.assertTrue(
                    isinstance(test_vol.size, six.integer_types)
                    and test_vol.size >= 0,
                    "Volume.size must be a positive number, but got %s" %
                    test_vol.size)
                self.assertTrue(
                    test_vol.description is None
                    or isinstance(test_vol.description, six.string_types),
                    "Volume.description must be None or a string. Got: %s" %
                    test_vol.description)
                self.assertIsNone(test_vol.source)
                self.assertIsNone(test_vol.source)
                self.assertIsNotNone(test_vol.create_time)
                self.assertIsNotNone(test_vol.zone_id)
                self.assertIsNone(test_vol.attachments)
                test_vol.attach(test_instance, '/dev/sda2')
                test_vol.wait_for(
                    [VolumeState.IN_USE],
                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
                self.assertIsNotNone(test_vol.attachments)
                self.assertIsInstance(test_vol.attachments, AttachmentInfo)
                self.assertEqual(test_vol.attachments.volume, test_vol)
                self.assertEqual(test_vol.attachments.instance_id,
                                 test_instance.id)
                self.assertEqual(test_vol.attachments.device, "/dev/sda2")
                test_vol.detach()
                test_vol.name = 'newvolname1'
                test_vol.wait_for(
                    [VolumeState.AVAILABLE],
                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
                self.assertEqual(test_vol.name, 'newvolname1')
                self.assertEqual(test_vol.description, vol_desc)
                self.assertIsNone(test_vol.attachments)
                test_vol.wait_for(
                    [VolumeState.AVAILABLE],
                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
예제 #5
0
    def test_crud_instance(self):
        name = "cb_instcrud-{0}".format(helpers.get_uuid())
        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        subnet = None

        def create_inst(name):
            return helpers.get_test_instance(self.provider,
                                             name,
                                             subnet=subnet)

        def cleanup_inst(inst):
            inst.terminate()
            inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN])

        def check_deleted(inst):
            deleted_inst = self.provider.compute.instances.get(inst.id)
            self.assertTrue(
                deleted_inst is None or deleted_inst.state
                in (InstanceState.TERMINATED, InstanceState.UNKNOWN),
                "Instance %s should have been deleted but still exists." %
                name)

        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(network=net)):
            net, subnet = helpers.create_test_network(self.provider, name)

            sit.check_crud(self,
                           self.provider.compute.instances,
                           Instance,
                           "cb_instcrud",
                           create_inst,
                           cleanup_inst,
                           custom_check_delete=check_deleted)
예제 #6
0
    def test_security_group_rule_add_twice(self):
        """Test whether adding the same rule twice succeeds."""
        if isinstance(self.provider, TestMockHelperMixin):
            raise unittest.SkipTest(
                "Mock provider returns InvalidParameterValue: "
                "Value security_group is invalid for parameter.")

        name = 'CBTestSecurityGroupC-{0}'.format(uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        sg = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, security_group=sg)):

            net, _ = helpers.create_test_network(self.provider, name)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)

            rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
                               cidr_ip='0.0.0.0/0')
            # attempting to add the same rule twice should succeed
            same_rule = sg.add_rule(ip_protocol='tcp', from_port=1111,
                                    to_port=1111, cidr_ip='0.0.0.0/0')
            self.assertTrue(
                rule == same_rule,
                "Expected rule {0} not found in security group: {0}".format(
                    same_rule, sg.rules))
예제 #7
0
    def test_crud_instance(self):
        name = "CBInstCrud-{0}-{1}".format(self.provider.name, uuid.uuid4())
        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        inst = None
        net = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(inst, net)):
            net, subnet = helpers.create_test_network(self.provider, name)
            inst = helpers.get_test_instance(self.provider,
                                             name,
                                             subnet=subnet)

            all_instances = self.provider.compute.instances.list()

            list_instances = [i for i in all_instances if i.name == name]
            self.assertTrue(
                len(list_instances) == 1,
                "List instances does not return the expected instance %s" %
                name)

            # check iteration
            iter_instances = [
                i for i in self.provider.compute.instances if i.name == name
            ]
            self.assertTrue(
                len(iter_instances) == 1,
                "Iter instances does not return the expected instance %s" %
                name)

            # check find
            find_instances = self.provider.compute.instances.find(name=name)
            self.assertTrue(
                len(find_instances) == 1,
                "Find instances does not return the expected instance %s" %
                name)

            # check non-existent find
            find_instances = self.provider.compute.instances.find(
                name="non_existent")
            self.assertTrue(
                len(find_instances) == 0,
                "Find() for a non-existent image returned %s" % find_instances)

            get_inst = self.provider.compute.instances.get(inst.id)
            self.assertTrue(
                list_instances[0] == get_inst == inst,
                "Objects returned by list: {0} and get: {1} are not as "
                " expected: {2}".format(list_instances[0].id, get_inst.id,
                                        inst.id))
            self.assertTrue(
                list_instances[0].name == get_inst.name == inst.name,
                "Names returned by list: {0} and get: {1} are not as "
                " expected: {2}".format(list_instances[0].name, get_inst.name,
                                        inst.name))
        deleted_inst = self.provider.compute.instances.get(inst.id)
        self.assertTrue(
            deleted_inst is None or deleted_inst.state
            in (InstanceState.TERMINATED, InstanceState.UNKNOWN),
            "Instance %s should have been deleted but still exists." % name)
예제 #8
0
    def test_attach_detach_volume(self):
        """
        Create a new volume, and attempt to attach it to an instance
        """
        name = "cb_attachvol-{0}".format(helpers.get_uuid())
        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        test_instance = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance, net)):
            net, subnet = helpers.create_test_network(self.provider, name)
            test_instance = helpers.get_test_instance(self.provider,
                                                      name,
                                                      subnet=subnet)

            test_vol = self.provider.storage.volumes.create(
                name, 1, test_instance.zone_id)
            with helpers.cleanup_action(lambda: test_vol.delete()):
                test_vol.wait_till_ready()
                test_vol.attach(test_instance, '/dev/sda2')
                test_vol.wait_for(
                    [VolumeState.IN_USE],
                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
                test_vol.detach()
                test_vol.wait_for(
                    [VolumeState.AVAILABLE],
                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
예제 #9
0
    def test_crud_security_group_service(self):
        name = 'CBTestSecurityGroupA-{0}'.format(uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        sg = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, security_group=sg)):
            net, _ = helpers.create_test_network(self.provider, name)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)

            self.assertEqual(name, sg.description)

            # test list method
            sgl = self.provider.security.security_groups.list()
            found_sgl = [i for i in sgl if i.name == name]
            self.assertTrue(
                len(found_sgl) == 1,
                "List security groups does not return the expected group %s" %
                name)

            # check iteration
            found_sgl = [i for i in self.provider.security.security_groups
                         if i.name == name]
            self.assertTrue(
                len(found_sgl) == 1,
                "Iter security groups does not return the expected group %s" %
                name)

            # check find
            find_sg = self.provider.security.security_groups.find(name=sg.name)
            self.assertTrue(
                len(find_sg) == 1,
                "List security groups returned {0} when expected was: {1}."
                .format(find_sg, sg.name))

            # check get
            get_sg = self.provider.security.security_groups.get(sg.id)
            self.assertTrue(
                get_sg == sg,
                "Get SecurityGroup did not return the expected key {0}."
                .format(name))

            self.assertTrue(
                sg.id in repr(sg),
                "repr(obj) should contain the object id so that the object"
                " can be reconstructed, but does not. eval(repr(obj)) == obj")
        sgl = self.provider.security.security_groups.list()
        found_sg = [g for g in sgl if g.name == name]
        self.assertTrue(
            len(found_sg) == 0,
            "Security group {0} should have been deleted but still exists."
            .format(name))
        no_sg = self.provider.security.security_groups.find(name='bogus_sg')
        self.assertTrue(
            len(no_sg) == 0,
            "Found a bogus security group?!?".format(no_sg))
예제 #10
0
    def test_security_group(self):
        """Test for proper creation of a security group."""
        name = 'CBTestSecurityGroupB-{0}'.format(uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        sg = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, security_group=sg)):
            net, _ = helpers.create_test_network(self.provider, name)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)

            rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
                               cidr_ip='0.0.0.0/0')
            found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
                                     to_port=1111, cidr_ip='0.0.0.0/0')
            self.assertTrue(
                rule == found_rule,
                "Expected rule {0} not found in security group: {0}".format(
                    rule, sg.rules))

            object_keys = (
                sg.rules[0].ip_protocol,
                sg.rules[0].from_port,
                sg.rules[0].to_port)
            self.assertTrue(
                all(str(key) in repr(sg.rules[0]) for key in object_keys),
                "repr(obj) should contain ip_protocol, form_port, and to_port"
                " so that the object can be reconstructed, but does not:"
                " {0}; {1}".format(sg.rules[0], object_keys))
            self.assertTrue(
                sg == sg,
                "The same security groups should be equal?")
            self.assertFalse(
                sg != sg,
                "The same security groups should still be equal?")
#             json_repr = json.dumps(
#                 {"description": name, "name": name, "id": sg.id,
#                  "rules":
#                     [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
#                       "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
#                       "id": sg.rules[0].id}]},
#                 sort_keys=True)
#             self.assertTrue(
#                 sg.to_json() == json_repr,
#                 "JSON SG representation {0} does not match expected {1}"
#                 .format(sg.to_json(), json_repr))

        sgl = self.provider.security.security_groups.list()
        found_sg = [g for g in sgl if g.name == name]
        self.assertTrue(
            len(found_sg) == 0,
            "Security group {0} should have been deleted but still exists."
            .format(name))
 def test_volume_properties(self):
     """
     Test volume properties
     """
     instance_name = "CBVolProps-{0}-{1}".format(
         self.provider.name,
         uuid.uuid4())
     vol_desc = 'newvoldesc1'
     net, _ = helpers.create_test_network(self.provider, instance_name)
     test_instance = helpers.get_test_instance(self.provider, instance_name,
                                               network=net)
     with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
             test_instance, net)):
         name = "CBUnitTestVolProps-{0}".format(uuid.uuid4())
         test_vol = self.provider.block_store.volumes.create(
             name, 1, test_instance.zone_id, description=vol_desc)
         with helpers.cleanup_action(lambda: test_vol.delete()):
             test_vol.wait_till_ready()
             self.assertTrue(
                 isinstance(test_vol.size, six.integer_types) and
                 test_vol.size >= 0,
                 "Volume.size must be a positive number, but got %s"
                 % test_vol.size)
             self.assertTrue(
                 test_vol.description is None or
                 isinstance(test_vol.description, six.string_types),
                 "Volume.description must be None or a string. Got: %s"
                 % test_vol.description)
             self.assertIsNone(test_vol.source)
             self.assertIsNone(test_vol.source)
             self.assertIsNotNone(test_vol.create_time)
             self.assertIsNotNone(test_vol.zone_id)
             self.assertIsNone(test_vol.attachments)
             test_vol.attach(test_instance, '/dev/sda2')
             test_vol.wait_for(
                 [VolumeState.IN_USE],
                 terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
             self.assertIsNotNone(test_vol.attachments)
             self.assertIsInstance(test_vol.attachments, AttachmentInfo)
             self.assertEqual(test_vol.attachments.volume, test_vol)
             self.assertEqual(test_vol.attachments.instance_id,
                              test_instance.id)
             self.assertEqual(test_vol.attachments.device,
                              "/dev/sda2")
             test_vol.detach()
             test_vol.name = 'newvolname1'
             # Force a refresh before checking attachment status
             test_vol.refresh()
             self.assertEqual(test_vol.name, 'newvolname1')
             self.assertEqual(test_vol.description, vol_desc)
             self.assertIsNone(test_vol.attachments)
             test_vol.wait_for(
                 [VolumeState.AVAILABLE],
                 terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
    def test_vm_firewall_properties(self):
        name = 'cb_propfw-{0}'.format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        fw = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, vm_firewall=fw)):
            net, _ = helpers.create_test_network(self.provider, name)
            fw = self.provider.security.vm_firewalls.create(name=name,
                                                            description=name,
                                                            network_id=net.id)

            self.assertEqual(name, fw.description)
    def test_vm_firewall_group_rule(self):
        name = 'cb_fwrule-{0}'.format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        fw = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, vm_firewall=fw)):
            net, _ = helpers.create_test_network(self.provider, name)
            fw = self.provider.security.vm_firewalls.create(name=name,
                                                            description=name,
                                                            network_id=net.id)
            rules = list(fw.rules)
            self.assertTrue(
                # TODO: This should be made consistent across all providers.
                # Currently, OpenStack creates two rules, one for IPV6 and
                # another for IPV4
                len(rules) >= 1,
                "Expected a single VM firewall rule allowing"
                " all outbound traffic. Got {0}.".format(rules))
            self.assertEqual(
                rules[0].direction, TrafficDirection.OUTBOUND,
                "Expected rule to be outbound. Got {0}.".format(rules))
            rule = fw.rules.create(direction=TrafficDirection.INBOUND,
                                   src_dest_fw=fw,
                                   protocol='tcp',
                                   from_port=1,
                                   to_port=65535)
            self.assertTrue(
                rule.src_dest_fw.name == name,
                "Expected VM firewall rule name {0}. Got {1}.".format(
                    name, rule.src_dest_fw.name))
            for r in fw.rules:
                r.delete()
            fw = self.provider.security.vm_firewalls.get(fw.id)  # update
            self.assertTrue(
                len(list(fw.rules)) == 0,
                "Deleting VMFirewallRule should delete it: {0}".format(
                    fw.rules))
        fwl = self.provider.security.vm_firewalls.list()
        found_fw = [f for f in fwl if f.name == name]
        self.assertTrue(
            len(found_fw) == 0,
            "VM firewall {0} should have been deleted but still exists.".
            format(name))
예제 #14
0
    def test_create_and_list_image(self):
        """
        Create a new image and check whether that image can be listed.
        This covers waiting till the image is ready, checking that the image
        name is the expected one and whether list_images is functional.
        """
        instance_name = "cb_crudimage-{0}".format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None

        def create_img(name):
            return test_instance.create_image(name)

        def cleanup_img(img):
            img.delete()
            img.wait_for([MachineImageState.UNKNOWN, MachineImageState.ERROR])

        def extra_tests(img):
            # TODO: Fix moto so that the BDM is populated correctly
            if not isinstance(self.provider, TestMockHelperMixin):
                # check image size
                img.refresh()
                self.assertGreater(
                    img.min_disk, 0, "Minimum disk"
                    " size required by image is invalid")

        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance, net)):
            net, subnet = helpers.create_test_network(self.provider,
                                                      instance_name)
            test_instance = helpers.get_test_instance(self.provider,
                                                      instance_name,
                                                      subnet=subnet)

            sit.check_crud(self,
                           self.provider.compute.images,
                           MachineImage,
                           "cb_listimg",
                           create_img,
                           cleanup_img,
                           extra_test_func=extra_tests)
    def test_crud_vm_firewall(self):
        name = 'cb_crudfw-{0}'.format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None

        def create_fw(name):
            return self.provider.security.vm_firewalls.create(
                name=name, description=name, network_id=net.id)

        def cleanup_fw(fw):
            fw.delete()

        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(network=net)):
            net, _ = helpers.create_test_network(self.provider, name)

            sit.check_crud(self, self.provider.security.vm_firewalls,
                           VMFirewall, "cb_crudfw", create_fw, cleanup_fw)
예제 #16
0
    def test_security_group_group_rule(self):
        """Test for proper creation of a security group rule."""
        name = 'CBTestSecurityGroupD-{0}'.format(uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        net = None
        sg = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                network=net, security_group=sg)):
            net, _ = helpers.create_test_network(self.provider, name)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)
            self.assertTrue(
                len(sg.rules) == 0,
                "Expected no security group group rule. Got {0}."
                .format(sg.rules))
            rule = sg.add_rule(src_group=sg, ip_protocol='tcp', from_port=1,
                               to_port=65535)
            self.assertTrue(
                rule.group.name == name,
                "Expected security group rule name {0}. Got {1}."
                .format(name, rule.group.name))
            for r in sg.rules:
                r.delete()
            sg = self.provider.security.security_groups.get(sg.id)  # update
            self.assertTrue(
                len(sg.rules) == 0,
                "Deleting SecurityGroupRule should delete it: {0}".format(
                    sg.rules))
        sgl = self.provider.security.security_groups.list()
        found_sg = [g for g in sgl if g.name == name]
        self.assertTrue(
            len(found_sg) == 0,
            "Security group {0} should have been deleted but still exists."
            .format(name))
예제 #17
0
    def test_create_and_list_image(self):
        """
        Create a new image and check whether that image can be listed.
        This covers waiting till the image is ready, checking that the image
        name is the expected one and whether list_images is functional.
        """
        instance_name = "CBImageTest-{0}-{1}".format(self.provider.name,
                                                     uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance, net)):
            net, subnet = helpers.create_test_network(self.provider,
                                                      instance_name)
            test_instance = helpers.get_test_instance(self.provider,
                                                      instance_name,
                                                      subnet=subnet)

            name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
            test_image = test_instance.create_image(name)

            def cleanup_img(img):
                img.delete()
                img.wait_for(
                    [MachineImageState.UNKNOWN, MachineImageState.ERROR])

            with helpers.cleanup_action(lambda: cleanup_img(test_image)):
                test_image.wait_till_ready()

                self.assertTrue(
                    test_instance.id in repr(test_instance),
                    "repr(obj) should contain the object id so that the object"
                    " can be reconstructed, but does not.")

                self.assertTrue(
                    test_image.description is None
                    or isinstance(test_image.description, six.string_types),
                    "Image description must be None or a string")

                # This check won't work when >50 images are available
                # images = self.provider.compute.images.list()
                # list_images = [image for image in images
                #                if image.name == name]
                # self.assertTrue(
                #     len(list_images) == 1,
                #     "List images does not return the expected image %s" %
                #     name)

                # check iteration
                iter_images = [
                    image for image in self.provider.compute.images
                    if image.name == name
                ]
                self.assertTrue(
                    name in [ii.name for ii in iter_images],
                    "Iter images (%s) does not contain the expected image %s" %
                    (iter_images, name))

                # find image
                found_images = self.provider.compute.images.find(name=name)
                self.assertTrue(
                    name in [fi.name for fi in found_images],
                    "Find images error: expected image %s but found: %s" %
                    (name, found_images))

                # check non-existent find
                ne_images = self.provider.compute.images.find(
                    name="non_existent")
                self.assertTrue(
                    len(ne_images) == 0,
                    "Find() for a non-existent image returned %s" % ne_images)

                get_img = self.provider.compute.images.get(test_image.id)
                self.assertTrue(
                    found_images[0] == get_img == test_image,
                    "Objects returned by list: {0} and get: {1} are not as "
                    " expected: {2}".format(found_images[0].id, get_img.id,
                                            test_image.id))
                self.assertTrue(
                    found_images[0].name == get_img.name == test_image.name,
                    "Names returned by find: {0} and get: {1} are"
                    " not as expected: {2}".format(found_images[0].name,
                                                   get_img.name,
                                                   test_image.name))
                # TODO: Fix moto so that the BDM is populated correctly
                if not isinstance(self.provider, TestMockHelperMixin):
                    # check image size
                    self.assertGreater(
                        get_img.min_disk, 0, "Minimum disk size"
                        " required by image is invalid")
            # TODO: Images take a long time to deregister on EC2. Needs
            # investigation
            images = self.provider.compute.images.list()
            found_images = [image for image in images if image.name == name]
            self.assertTrue(
                len(found_images) == 0,
                "Image %s should have been deleted but still exists." % name)
    def test_crud_instance(self):
        name = "CBInstCrud-{0}-{1}".format(
            self.provider.name,
            uuid.uuid4())
        net, _ = helpers.create_test_network(self.provider, name)
        inst = helpers.get_test_instance(self.provider, name, network=net)

        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                inst, net)):
            all_instances = self.provider.compute.instances.list()

            list_instances = [i for i in all_instances if i.name == name]
            self.assertTrue(
                len(list_instances) == 1,
                "List instances does not return the expected instance %s" %
                name)

            # check iteration
            iter_instances = [i for i in self.provider.compute.instances
                              if i.name == name]
            self.assertTrue(
                len(iter_instances) == 1,
                "Iter instances does not return the expected instance %s" %
                name)

            # check find
            find_instances = self.provider.compute.instances.find(name=name)
            self.assertTrue(
                len(find_instances) == 1,
                "Find instances does not return the expected instance %s" %
                name)

            # check non-existent find
            find_instances = self.provider.compute.instances.find(
                name="non_existent")
            self.assertTrue(
                len(find_instances) == 0,
                "Find() for a non-existent image returned %s" % find_instances)

            get_inst = self.provider.compute.instances.get(
                inst.id)
            self.assertTrue(
                list_instances[0] ==
                get_inst == inst,
                "Objects returned by list: {0} and get: {1} are not as "
                " expected: {2}" .format(list_instances[0].id,
                                         get_inst.id,
                                         inst.id))
            self.assertTrue(
                list_instances[0].name ==
                get_inst.name == inst.name,
                "Names returned by list: {0} and get: {1} are not as "
                " expected: {2}" .format(list_instances[0].name,
                                         get_inst.name,
                                         inst.name))
        deleted_inst = self.provider.compute.instances.get(
            inst.id)
        self.assertTrue(
            deleted_inst is None or deleted_inst.state in (
                InstanceState.TERMINATED,
                InstanceState.UNKNOWN),
            "Instance %s should have been deleted but still exists." %
            name)
예제 #19
0
    def test_instance_properties(self):
        name = "cb_inst_props-{0}".format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None
        sg = None
        kp = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, net, sg, kp)):
            net, subnet = helpers.create_test_network(self.provider, name)
            kp = self.provider.security.key_pairs.create(name=name)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)
            test_instance = helpers.get_test_instance(self.provider,
                                                      name,
                                                      key_pair=kp,
                                                      security_groups=[sg],
                                                      subnet=subnet)
            self.assertEqual(
                test_instance.name, name,
                "Instance name {0} is not equal to the expected name"
                " {1}".format(test_instance.name, name))
            image_id = helpers.get_provider_test_data(self.provider, "image")
            self.assertEqual(
                test_instance.image_id, image_id,
                "Image id {0} is not equal to the expected id"
                " {1}".format(test_instance.image_id, image_id))
            self.assertIsInstance(test_instance.zone_id, six.string_types)
            self.assertEqual(
                test_instance.image_id,
                helpers.get_provider_test_data(self.provider, "image"))
            self.assertIsInstance(test_instance.public_ips, list)
            self.assertIsInstance(test_instance.private_ips, list)
            self.assertEqual(test_instance.key_pair_name, kp.name)
            self.assertIsInstance(test_instance.security_groups, list)
            self.assertEqual(test_instance.security_groups[0], sg)
            self.assertIsInstance(test_instance.security_group_ids, list)
            self.assertEqual(test_instance.security_group_ids[0], sg.id)
            # Must have either a public or a private ip
            ip_private = test_instance.private_ips[0] \
                if test_instance.private_ips else None
            ip_address = test_instance.public_ips[0] \
                if test_instance.public_ips and test_instance.public_ips[0] \
                else ip_private
            self.assertIsNotNone(
                ip_address,
                "Instance must have either a public IP or a private IP")
            self.assertTrue(self._is_valid_ip(ip_address),
                            "Instance must have a valid IP address")
            self.assertIsInstance(test_instance.instance_type_id,
                                  six.string_types)
            itype = self.provider.compute.instance_types.get(
                test_instance.instance_type_id)
            self.assertEqual(
                itype, test_instance.instance_type,
                "Instance type {0} does not match expected type {1}".format(
                    itype.name, test_instance.instance_type))
            self.assertIsInstance(itype, InstanceType)
            expected_type = helpers.get_provider_test_data(
                self.provider, 'instance_type')
            self.assertEqual(
                itype.name, expected_type,
                "Instance type {0} does not match expected type {1}".format(
                    itype.name, expected_type))
            if isinstance(self.provider, TestMockHelperMixin):
                raise self.skipTest(
                    "Skipping rest of test because Moto is not returning the"
                    " instance's placement zone correctly")
            find_zone = [
                zone for zone in self.provider.compute.regions.current.zones
                if zone.id == test_instance.zone_id
            ]
            self.assertEqual(
                len(find_zone), 1, "Instance's placement zone could not be "
                " found in zones list")
예제 #20
0
    def test_instance_methods(self):
        name = "cb_instmethods-{0}".format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_inst = None
        net = None
        sg = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_inst, net, sg)):
            net, subnet = helpers.create_test_network(self.provider, name)
            test_inst = helpers.get_test_instance(self.provider,
                                                  name,
                                                  subnet=subnet)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)

            # Check adding a security group to a running instance
            test_inst.add_security_group(sg)
            test_inst.refresh()
            self.assertTrue(
                sg in test_inst.security_groups, "Expected security group '%s'"
                " to be among instance security_groups: [%s]" %
                (sg, test_inst.security_groups))

            # Check removing a security group from a running instance
            test_inst.remove_security_group(sg)
            test_inst.refresh()
            self.assertTrue(
                sg not in test_inst.security_groups, "Expected security group"
                " '%s' to be removed from instance security_groups: [%s]" %
                (sg, test_inst.security_groups))

            # check floating ips
            router = self.provider.networking.routers.create(name, net)
            gateway = None

            def cleanup_router(router, gateway):
                with helpers.cleanup_action(lambda: router.delete()):
                    with helpers.cleanup_action(lambda: gateway.delete()):
                        router.detach_subnet(subnet)
                        router.detach_gateway(gateway)

            with helpers.cleanup_action(
                    lambda: cleanup_router(router, gateway)):
                router.attach_subnet(subnet)
                gateway = (self.provider.networking.gateways.
                           get_or_create_inet_gateway(name))
                router.attach_gateway(gateway)
                # check whether adding an elastic ip works
                fip = (self.provider.networking.networks.create_floating_ip())
                with helpers.cleanup_action(lambda: fip.delete()):
                    test_inst.add_floating_ip(fip.public_ip)
                    test_inst.refresh()
                    # On Devstack, the floating IP is listed under private_ips.
                    self.assertIn(fip.public_ip,
                                  test_inst.public_ips + test_inst.private_ips)
                    if isinstance(self.provider, TestMockHelperMixin):
                        # TODO: Moto bug does not refresh removed public ip
                        return
                    # check whether removing an elastic ip works
                    test_inst.remove_floating_ip(fip.public_ip)
                    test_inst.refresh()
                    self.assertNotIn(
                        fip.public_ip,
                        test_inst.public_ips + test_inst.private_ips)
예제 #21
0
    def test_block_device_mapping_attachments(self):
        name = "cb_blkattch-{0}".format(helpers.get_uuid())

        if self.provider.PROVIDER_ID == ProviderList.OPENSTACK:
            raise self.skipTest("Not running BDM tests because OpenStack is"
                                " not stable enough yet")

        test_vol = self.provider.block_store.volumes.create(
            name, 1, helpers.get_provider_test_data(self.provider,
                                                    "placement"))
        with helpers.cleanup_action(lambda: test_vol.delete()):
            test_vol.wait_till_ready()
            test_snap = test_vol.create_snapshot(name=name, description=name)

            def cleanup_snap(snap):
                snap.delete()
                snap.wait_for([SnapshotState.UNKNOWN],
                              terminal_states=[SnapshotState.ERROR])

            with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
                test_snap.wait_till_ready()

                lc = self.provider.compute.instances.create_launch_config()

                # Add a new blank volume
                lc.add_volume_device(size=1, delete_on_terminate=True)

                # Attach an existing volume
                lc.add_volume_device(size=1,
                                     source=test_vol,
                                     delete_on_terminate=True)

                # Add a new volume based on a snapshot
                lc.add_volume_device(size=1,
                                     source=test_snap,
                                     delete_on_terminate=True)

                # Override root volume size
                image_id = helpers.get_provider_test_data(
                    self.provider, "image")
                img = self.provider.compute.images.get(image_id)
                # The size should be greater then the ami size
                # and therefore, img.min_disk is used.
                lc.add_volume_device(
                    is_root=True,
                    source=img,
                    size=img.min_disk if img and img.min_disk else 2,
                    delete_on_terminate=True)

                # Add all available ephemeral devices
                instance_type_name = helpers.get_provider_test_data(
                    self.provider, "instance_type")
                inst_type = self.provider.compute.instance_types.find(
                    name=instance_type_name)[0]
                for _ in range(inst_type.num_ephemeral_disks):
                    lc.add_ephemeral_device()

                net, subnet = helpers.create_test_network(self.provider, name)

                with helpers.cleanup_action(
                        lambda: helpers.delete_test_network(net)):

                    inst = helpers.create_test_instance(self.provider,
                                                        name,
                                                        subnet=subnet,
                                                        launch_config=lc)

                    with helpers.cleanup_action(
                            lambda: helpers.delete_test_instance(inst)):
                        try:
                            inst.wait_till_ready()
                        except WaitStateException as e:
                            self.fail("The block device mapped launch did not "
                                      " complete successfully: %s" % e)
    def test_instance_properties(self):
        name = "cb_inst_props-{0}".format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None
        fw = None
        kp = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, net, fw, kp)):
            net, subnet = helpers.create_test_network(self.provider, name)
            kp = self.provider.security.key_pairs.create(name=name)
            fw = self.provider.security.vm_firewalls.create(name=name,
                                                            description=name,
                                                            network_id=net.id)
            test_instance = helpers.get_test_instance(self.provider,
                                                      name,
                                                      key_pair=kp,
                                                      vm_firewalls=[fw],
                                                      subnet=subnet)
            self.assertEqual(
                test_instance.name, name,
                "Instance name {0} is not equal to the expected name"
                " {1}".format(test_instance.name, name))
            image_id = helpers.get_provider_test_data(self.provider, "image")
            self.assertEqual(
                test_instance.image_id, image_id,
                "Image id {0} is not equal to the expected id"
                " {1}".format(test_instance.image_id, image_id))
            self.assertIsInstance(test_instance.zone_id, six.string_types)
            self.assertEqual(
                test_instance.image_id,
                helpers.get_provider_test_data(self.provider, "image"))
            self.assertIsInstance(test_instance.public_ips, list)
            if test_instance.public_ips:
                self.assertTrue(
                    test_instance.public_ips[0], "public ip should contain a"
                    " valid value if a list of public_ips exist")
            self.assertIsInstance(test_instance.private_ips, list)
            self.assertTrue(test_instance.private_ips[0], "private ip should"
                            " contain a valid value")
            self.assertEqual(test_instance.key_pair_name, kp.name)
            self.assertIsInstance(test_instance.vm_firewalls, list)
            self.assertEqual(test_instance.vm_firewalls[0], fw)
            self.assertIsInstance(test_instance.vm_firewall_ids, list)
            self.assertEqual(test_instance.vm_firewall_ids[0], fw.id)
            # Must have either a public or a private ip
            ip_private = test_instance.private_ips[0] \
                if test_instance.private_ips else None
            ip_address = test_instance.public_ips[0] \
                if test_instance.public_ips and test_instance.public_ips[0] \
                else ip_private
            # Convert to unicode for py27 compatibility with ipaddress()
            ip_address = u"{}".format(ip_address)
            self.assertIsNotNone(
                ip_address,
                "Instance must have either a public IP or a private IP")
            self.assertTrue(
                self._is_valid_ip(ip_address),
                "Instance must have a valid IP address. Got: %s" % ip_address)
            self.assertIsInstance(test_instance.vm_type_id, six.string_types)
            vm_type = self.provider.compute.vm_types.get(
                test_instance.vm_type_id)
            self.assertEqual(
                vm_type, test_instance.vm_type,
                "VM type {0} does not match expected type {1}".format(
                    vm_type.name, test_instance.vm_type))
            self.assertIsInstance(vm_type, VMType)
            expected_type = helpers.get_provider_test_data(
                self.provider, 'vm_type')
            self.assertEqual(
                vm_type.name, expected_type,
                "VM type {0} does not match expected type {1}".format(
                    vm_type.name, expected_type))
            find_zone = [
                zone for zone in self.provider.compute.regions.current.zones
                if zone.id == test_instance.zone_id
            ]
            self.assertEqual(
                len(find_zone), 1, "Instance's placement zone could not be "
                " found in zones list")
    def test_create_and_list_image(self):
        """
        Create a new image and check whether that image can be listed.
        This covers waiting till the image is ready, checking that the image
        name is the expected one and whether list_images is functional.
        """
        instance_name = "CBImageTest-{0}-{1}".format(
            self.provider.name,
            uuid.uuid4())
        net, _ = helpers.create_test_network(self.provider, instance_name)
        test_instance = helpers.get_test_instance(self.provider, instance_name,
                                                  network=net)
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, net)):
            name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
            test_image = test_instance.create_image(name)

            def cleanup_img(img):
                img.delete()
                img.wait_for(
                    [MachineImageState.UNKNOWN, MachineImageState.ERROR])

            with helpers.cleanup_action(lambda: cleanup_img(test_image)):
                test_image.wait_till_ready()

                self.assertTrue(
                    test_instance.id in repr(test_instance),
                    "repr(obj) should contain the object id so that the object"
                    " can be reconstructed, but does not.")

                self.assertTrue(
                    test_image.description is None or isinstance(
                        test_image.description, six.string_types),
                    "Image description must be None or a string")

                images = self.provider.compute.images.list()
                list_images = [image for image in images
                               if image.name == name]
                self.assertTrue(
                    len(list_images) == 1,
                    "List images does not return the expected image %s" %
                    name)

                # check iteration
                iter_images = [image for image in self.provider.compute.images
                               if image.name == name]
                self.assertTrue(
                    len(iter_images) == 1,
                    "Iter images does not return the expected image %s" %
                    name)

                # find image
                found_images = self.provider.compute.images.find(name=name)
                self.assertTrue(
                    len(found_images) == 1,
                    "Find images error: expected image %s but found: %s" %
                    (name, found_images))

                # check non-existent find
                ne_images = self.provider.compute.images.find(
                    name="non_existent")
                self.assertTrue(
                    len(ne_images) == 0,
                    "Find() for a non-existent image returned %s" %
                    ne_images)

                get_img = self.provider.compute.images.get(
                    test_image.id)
                self.assertTrue(
                    found_images[0] == iter_images[0] == get_img == test_image,
                    "Objects returned by list: {0} and get: {1} are not as "
                    " expected: {2}" .format(found_images[0].id,
                                             get_img.id,
                                             test_image.id))
                self.assertTrue(
                    list_images[0].name == found_images[0].name ==
                    get_img.name == test_image.name,
                    "Names returned by list: {0}, find: {1} and get: {2} are"
                    " not as expected: {3}" .format(list_images[0].name,
                                                    found_images[0].name,
                                                    get_img.name,
                                                    test_image.name))
            # TODO: Images take a long time to deregister on EC2. Needs
            # investigation
            images = self.provider.compute.images.list()
            found_images = [image for image in images
                            if image.name == name]
            self.assertTrue(
                len(found_images) == 0,
                "Image %s should have been deleted but still exists." %
                name)
    def test_instance_methods(self):
        name = "cb_instmethods-{0}".format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_inst = None
        net = None
        fw = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_inst, net, fw)):
            net, subnet = helpers.create_test_network(self.provider, name)
            test_inst = helpers.get_test_instance(self.provider,
                                                  name,
                                                  subnet=subnet)
            fw = self.provider.security.vm_firewalls.create(name=name,
                                                            description=name,
                                                            network_id=net.id)

            # Check adding a VM firewall to a running instance
            test_inst.add_vm_firewall(fw)
            test_inst.refresh()
            self.assertTrue(
                fw in test_inst.vm_firewalls, "Expected VM firewall '%s'"
                " to be among instance vm_firewalls: [%s]" %
                (fw, test_inst.vm_firewalls))

            # Check removing a VM firewall from a running instance
            test_inst.remove_vm_firewall(fw)
            test_inst.refresh()
            self.assertTrue(
                fw not in test_inst.vm_firewalls, "Expected VM firewall"
                " '%s' to be removed from instance vm_firewalls: [%s]" %
                (fw, test_inst.vm_firewalls))

            # check floating ips
            router = self.provider.networking.routers.create(name, net)
            gateway = None

            def cleanup_router(router, gateway):
                with helpers.cleanup_action(lambda: router.delete()):
                    with helpers.cleanup_action(lambda: gateway.delete()):
                        router.detach_subnet(subnet)
                        router.detach_gateway(gateway)

            with helpers.cleanup_action(
                    lambda: cleanup_router(router, gateway)):
                router.attach_subnet(subnet)
                gateway = net.gateways.get_or_create_inet_gateway(name)
                router.attach_gateway(gateway)
                # check whether adding an elastic ip works
                fip = gateway.floating_ips.create()
                self.assertFalse(
                    fip.in_use,
                    "Newly created floating IP address should not be in use.")

                with helpers.cleanup_action(lambda: fip.delete()):
                    with helpers.cleanup_action(
                            lambda: test_inst.remove_floating_ip(fip)):
                        test_inst.add_floating_ip(fip)
                        test_inst.refresh()
                        # On Devstack, FloatingIP is listed under private_ips.
                        self.assertIn(
                            fip.public_ip,
                            test_inst.public_ips + test_inst.private_ips)
                        fip.refresh()
                        self.assertTrue(
                            fip.in_use,
                            "Attached floating IP address should be in use.")
                    test_inst.refresh()
                    self.assertNotIn(
                        fip.public_ip,
                        test_inst.public_ips + test_inst.private_ips)
예제 #25
0
    def test_instance_methods(self):
        name = "CBInstProps-{0}-{1}".format(self.provider.name, uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_inst = None
        net = None
        sg = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_inst, net, sg)):
            net, subnet = helpers.create_test_network(self.provider, name)
            test_inst = helpers.get_test_instance(self.provider,
                                                  name,
                                                  subnet=subnet)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)

            # Check adding a security group to a running instance
            test_inst.add_security_group(sg)
            test_inst.refresh()
            self.assertTrue(
                sg in test_inst.security_groups, "Expected security group '%s'"
                " to be among instance security_groups: [%s]" %
                (sg, test_inst.security_groups))

            # Check removing a security group from a running instance
            test_inst.remove_security_group(sg)
            test_inst.refresh()
            self.assertTrue(
                sg not in test_inst.security_groups, "Expected security group"
                " '%s' to be removed from instance security_groups: [%s]" %
                (sg, test_inst.security_groups))

            # check floating ips
            router = self.provider.network.create_router(name=name)

            with helpers.cleanup_action(lambda: router.delete()):

                # TODO: Cloud specific code, needs fixing
                if self.provider.PROVIDER_ID == 'openstack':
                    for n in self.provider.network.list():
                        if n.external:
                            external_net = n
                            break
                else:
                    external_net = net
                router.attach_network(external_net.id)
                router.add_route(subnet.id)

                def cleanup_router():
                    router.remove_route(subnet.id)
                    router.detach_network()

                with helpers.cleanup_action(lambda: cleanup_router()):
                    # check whether adding an elastic ip works
                    fip = self.provider.network.create_floating_ip()
                    with helpers.cleanup_action(lambda: fip.delete()):
                        test_inst.add_floating_ip(fip.public_ip)
                        test_inst.refresh()
                        self.assertIn(fip.public_ip, test_inst.public_ips)

                        if isinstance(self.provider, TestMockHelperMixin):
                            # TODO: Moto bug does not refresh removed public ip
                            return

                        # check whether removing an elastic ip works
                        test_inst.remove_floating_ip(fip.public_ip)
                        test_inst.refresh()
                        self.assertNotIn(fip.public_ip, test_inst.public_ips)
예제 #26
0
    def test_block_device_mapping_attachments(self):
        name = "CBInstBlkAttch-{0}-{1}".format(self.provider.name,
                                               uuid.uuid4())

        # Comment out BDM tests because OpenStack is not stable enough yet
        if True:
            if True:

                # test_vol = self.provider.block_store.volumes.create(
                #    name,
                #    1,
                #    helpers.get_provider_test_data(self.provider,
                #                                   "placement"))
                # with helpers.cleanup_action(lambda: test_vol.delete()):
                #    test_vol.wait_till_ready()
                #    test_snap = test_vol.create_snapshot(name=name,
                #                                         description=name)
                #
                #    def cleanup_snap(snap):
                #        snap.delete()
                #        snap.wait_for(
                #            [SnapshotState.UNKNOWN],
                #            terminal_states=[SnapshotState.ERROR])
                #
                #    with helpers.cleanup_action(lambda:
                #                                cleanup_snap(test_snap)):
                #         test_snap.wait_till_ready()

                lc = self.provider.compute.instances.create_launch_config()

                #                 # Add a new blank volume
                #                 lc.add_volume_device(size=1, delete_on_terminate=True)
                #
                #                 # Attach an existing volume
                #                 lc.add_volume_device(size=1, source=test_vol,
                #                                      delete_on_terminate=True)
                #
                #                 # Add a new volume based on a snapshot
                #                 lc.add_volume_device(size=1, source=test_snap,
                #                                      delete_on_terminate=True)

                # Override root volume size
                image_id = helpers.get_provider_test_data(
                    self.provider, "image")
                img = self.provider.compute.images.get(image_id)
                # The size should be greater then the ami size
                # and therefore, img.min_disk is used.
                lc.add_volume_device(
                    is_root=True,
                    source=img,
                    size=img.min_disk if img and img.min_disk else 2,
                    delete_on_terminate=True)

                # Add all available ephemeral devices
                instance_type_name = helpers.get_provider_test_data(
                    self.provider, "instance_type")
                inst_type = self.provider.compute.instance_types.find(
                    name=instance_type_name)[0]
                for _ in range(inst_type.num_ephemeral_disks):
                    lc.add_ephemeral_device()

                net, subnet = helpers.create_test_network(self.provider, name)

                with helpers.cleanup_action(
                        lambda: helpers.delete_test_network(net)):

                    inst = helpers.create_test_instance(self.provider,
                                                        name,
                                                        subnet=subnet,
                                                        launch_config=lc)

                    with helpers.cleanup_action(
                            lambda: helpers.delete_test_instance(inst)):
                        try:
                            inst.wait_till_ready()
                        except WaitStateException as e:
                            self.fail("The block device mapped launch did not "
                                      " complete successfully: %s" % e)
    def test_instance_properties(self):
        name = "CBInstProps-{0}-{1}".format(
            self.provider.name,
            uuid.uuid4())
        net, _ = helpers.create_test_network(self.provider, name)
        kp = self.provider.security.key_pairs.create(name=name)
        sg = self.provider.security.security_groups.create(
            name=name, description=name, network_id=net.id)
        test_instance = helpers.get_test_instance(self.provider,
                                                  name, key_pair=kp,
                                                  security_groups=[sg],
                                                  network=net)

        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, net, sg, kp)):
            self.assertTrue(
                test_instance.id in repr(test_instance),
                "repr(obj) should contain the object id so that the object"
                " can be reconstructed, but does not. eval(repr(obj)) == obj")
            self.assertEqual(
                test_instance.name, name,
                "Instance name {0} is not equal to the expected name"
                " {1}".format(test_instance.name, name))
            image_id = helpers.get_provider_test_data(self.provider, "image")
            self.assertEqual(test_instance.image_id, image_id,
                             "Image id {0} is not equal to the expected id"
                             " {1}".format(test_instance.image_id, image_id))
            self.assertIsInstance(test_instance.zone_id,
                                  six.string_types)
            # FIXME: Moto is not returning the instance's placement zone
#             find_zone = [zone for zone in
#                          self.provider.compute.regions.current.zones
#                          if zone.id == test_instance.zone_id]
#             self.assertEqual(len(find_zone), 1,
#                              "Instance's placement zone could not be "
#                              " found in zones list")
            self.assertEqual(
                test_instance.image_id,
                helpers.get_provider_test_data(self.provider, "image"))
            self.assertIsInstance(test_instance.public_ips, list)
            self.assertIsInstance(test_instance.private_ips, list)
            self.assertEqual(
                test_instance.key_pair_name,
                kp.name)
            self.assertIsInstance(test_instance.security_groups, list)
            self.assertEqual(
                test_instance.security_groups[0],
                sg)
            self.assertIsInstance(test_instance.security_group_ids, list)
            self.assertEqual(
                test_instance.security_group_ids[0],
                sg.id)
            # Must have either a public or a private ip
            ip_private = test_instance.private_ips[0] \
                if test_instance.private_ips else None
            ip_address = test_instance.public_ips[0] \
                if test_instance.public_ips and test_instance.public_ips[0] \
                else ip_private
            self.assertIsNotNone(
                ip_address,
                "Instance must have either a public IP or a private IP")
            self.assertTrue(
                self._is_valid_ip(ip_address),
                "Instance must have a valid IP address")
            self.assertIsInstance(test_instance.instance_type_id,
                                  six.string_types)
            itype = self.provider.compute.instance_types.get(
                test_instance.instance_type_id)
            self.assertEqual(
                itype, test_instance.instance_type,
                "Instance type {0} does not match expected type {1}".format(
                    itype.name, test_instance.instance_type))
            self.assertIsInstance(itype, InstanceType)
            expected_type = helpers.get_provider_test_data(self.provider,
                                                           'instance_type')
            self.assertEqual(
                itype.name, expected_type,
                "Instance type {0} does not match expected type {1}".format(
                    itype.name, expected_type))
예제 #28
0
    def test_create_and_list_image(self):
        """
        Create a new image and check whether that image can be listed.
        This covers waiting till the image is ready, checking that the image
        name is the expected one and whether list_images is functional.
        """
        instance_name = "CBImageTest-{0}-{1}".format(self.provider.name,
                                                     uuid.uuid4())
        net, _ = helpers.create_test_network(self.provider, instance_name)
        test_instance = helpers.get_test_instance(self.provider,
                                                  instance_name,
                                                  network=net)
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance, net)):
            name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
            test_image = test_instance.create_image(name)

            def cleanup_img(img):
                img.delete()
                img.wait_for(
                    [MachineImageState.UNKNOWN, MachineImageState.ERROR])

            with helpers.cleanup_action(lambda: cleanup_img(test_image)):
                test_image.wait_till_ready()

                self.assertTrue(
                    test_instance.id in repr(test_instance),
                    "repr(obj) should contain the object id so that the object"
                    " can be reconstructed, but does not.")

                self.assertTrue(
                    test_image.description is None
                    or isinstance(test_image.description, six.string_types),
                    "Image description must be None or a string")

                images = self.provider.compute.images.list()
                list_images = [image for image in images if image.name == name]
                self.assertTrue(
                    len(list_images) == 1,
                    "List images does not return the expected image %s" % name)

                # check iteration
                iter_images = [
                    image for image in self.provider.compute.images
                    if image.name == name
                ]
                self.assertTrue(
                    len(iter_images) == 1,
                    "Iter images does not return the expected image %s" % name)

                # find image
                found_images = self.provider.compute.images.find(name=name)
                self.assertTrue(
                    len(found_images) == 1,
                    "Find images error: expected image %s but found: %s" %
                    (name, found_images))

                # check non-existent find
                ne_images = self.provider.compute.images.find(
                    name="non_existent")
                self.assertTrue(
                    len(ne_images) == 0,
                    "Find() for a non-existent image returned %s" % ne_images)

                get_img = self.provider.compute.images.get(test_image.id)
                self.assertTrue(
                    found_images[0] == iter_images[0] == get_img == test_image,
                    "Objects returned by list: {0} and get: {1} are not as "
                    " expected: {2}".format(found_images[0].id, get_img.id,
                                            test_image.id))
                self.assertTrue(
                    list_images[0].name == found_images[0].name == get_img.name
                    == test_image.name,
                    "Names returned by list: {0}, find: {1} and get: {2} are"
                    " not as expected: {3}".format(list_images[0].name,
                                                   found_images[0].name,
                                                   get_img.name,
                                                   test_image.name))
            # TODO: Images take a long time to deregister on EC2. Needs
            # investigation
            images = self.provider.compute.images.list()
            found_images = [image for image in images if image.name == name]
            self.assertTrue(
                len(found_images) == 0,
                "Image %s should have been deleted but still exists." % name)
    def test_block_device_mapping_attachments(self):
        name = "CBInstBlkAttch-{0}-{1}".format(
            self.provider.name,
            uuid.uuid4())

#         test_vol = self.provider.block_store.volumes.create(
#             name,
#             1,
#             helpers.get_provider_test_data(self.provider, "placement"))
#         with helpers.cleanup_action(lambda: test_vol.delete()):
#             test_vol.wait_till_ready()
#             test_snap = test_vol.create_snapshot(name=name,
#                                                  description=name)
#
#             def cleanup_snap(snap):
#                 snap.delete()
#                 snap.wait_for(
#                     [SnapshotState.UNKNOWN],
#                     terminal_states=[SnapshotState.ERROR])
#
#             with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
#                 test_snap.wait_till_ready()

        lc = self.provider.compute.instances.create_launch_config()

        # Add a new blank volume
#         lc.add_volume_device(size=1, delete_on_terminate=True)

        # Attach an existing volume
#                 lc.add_volume_device(size=1, source=test_vol,
#                                      delete_on_terminate=True)

        # Add a new volume based on a snapshot
#                 lc.add_volume_device(size=1, source=test_snap,
#                                      delete_on_terminate=True)

        # Override root volume size
        image_id = helpers.get_provider_test_data(
            self.provider,
            "image")
        img = self.provider.compute.images.get(image_id)
        lc.add_volume_device(
            is_root=True,
            source=img,
            # TODO: This should be greater than the ami size or tests
            # will fail on actual infrastructure. Needs an image.size
            # method
            size=2,
            delete_on_terminate=True)

        # Add all available ephemeral devices
        instance_type_name = helpers.get_provider_test_data(
            self.provider,
            "instance_type")
        inst_type = self.provider.compute.instance_types.find(
            name=instance_type_name)[0]
        for _ in range(inst_type.num_ephemeral_disks):
            lc.add_ephemeral_device()

        net, _ = helpers.create_test_network(self.provider, name)
        lc.add_network_interface(net.id)

        inst = helpers.create_test_instance(
            self.provider,
            name,
            zone=helpers.get_provider_test_data(
                self.provider,
                'placement'),
            launch_config=lc)

        def cleanup(instance, net):
            instance.terminate()
            instance.wait_for(
                [InstanceState.TERMINATED, InstanceState.UNKNOWN],
                terminal_states=[InstanceState.ERROR])
            helpers.delete_test_network(net)

        with helpers.cleanup_action(lambda: cleanup(inst, net)):
            try:
                inst.wait_till_ready()
            except WaitStateException as e:
                self.fail("The block device mapped launch did not "
                          " complete successfully: %s" % e)
예제 #30
0
    def test_instance_properties(self):
        name = "CBInstProps-{0}-{1}".format(self.provider.name, uuid.uuid4())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None
        sg = None
        kp = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, net, sg, kp)):
            net, subnet = helpers.create_test_network(self.provider, name)
            kp = self.provider.security.key_pairs.create(name=name)
            sg = self.provider.security.security_groups.create(
                name=name, description=name, network_id=net.id)
            test_instance = helpers.get_test_instance(self.provider,
                                                      name,
                                                      key_pair=kp,
                                                      security_groups=[sg],
                                                      subnet=subnet)

            self.assertTrue(
                test_instance.id in repr(test_instance),
                "repr(obj) should contain the object id so that the object"
                " can be reconstructed, but does not. eval(repr(obj)) == obj")
            self.assertEqual(
                test_instance.name, name,
                "Instance name {0} is not equal to the expected name"
                " {1}".format(test_instance.name, name))
            image_id = helpers.get_provider_test_data(self.provider, "image")
            self.assertEqual(
                test_instance.image_id, image_id,
                "Image id {0} is not equal to the expected id"
                " {1}".format(test_instance.image_id, image_id))
            self.assertIsInstance(test_instance.zone_id, six.string_types)
            # FIXME: Moto is not returning the instance's placement zone
            #             find_zone = [zone for zone in
            #                          self.provider.compute.regions.current.zones
            #                          if zone.id == test_instance.zone_id]
            #             self.assertEqual(len(find_zone), 1,
            #                              "Instance's placement zone could not be "
            #                              " found in zones list")
            self.assertEqual(
                test_instance.image_id,
                helpers.get_provider_test_data(self.provider, "image"))
            self.assertIsInstance(test_instance.public_ips, list)
            self.assertIsInstance(test_instance.private_ips, list)
            self.assertEqual(test_instance.key_pair_name, kp.name)
            self.assertIsInstance(test_instance.security_groups, list)
            self.assertEqual(test_instance.security_groups[0], sg)
            self.assertIsInstance(test_instance.security_group_ids, list)
            self.assertEqual(test_instance.security_group_ids[0], sg.id)
            # Must have either a public or a private ip
            ip_private = test_instance.private_ips[0] \
                if test_instance.private_ips else None
            ip_address = test_instance.public_ips[0] \
                if test_instance.public_ips and test_instance.public_ips[0] \
                else ip_private
            self.assertIsNotNone(
                ip_address,
                "Instance must have either a public IP or a private IP")
            self.assertTrue(self._is_valid_ip(ip_address),
                            "Instance must have a valid IP address")
            self.assertIsInstance(test_instance.instance_type_id,
                                  six.string_types)
            itype = self.provider.compute.instance_types.get(
                test_instance.instance_type_id)
            self.assertEqual(
                itype, test_instance.instance_type,
                "Instance type {0} does not match expected type {1}".format(
                    itype.name, test_instance.instance_type))
            self.assertIsInstance(itype, InstanceType)
            expected_type = helpers.get_provider_test_data(
                self.provider, 'instance_type')
            self.assertEqual(
                itype.name, expected_type,
                "Instance type {0} does not match expected type {1}".format(
                    itype.name, expected_type))
예제 #31
0
    def test_block_device_mapping_attachments(self):
        name = "CBInstBlkAttch-{0}-{1}".format(self.provider.name,
                                               uuid.uuid4())

        #         test_vol = self.provider.block_store.volumes.create(
        #             name,
        #             1,
        #             helpers.get_provider_test_data(self.provider, "placement"))
        #         with helpers.cleanup_action(lambda: test_vol.delete()):
        #             test_vol.wait_till_ready()
        #             test_snap = test_vol.create_snapshot(name=name,
        #                                                  description=name)
        #
        #             def cleanup_snap(snap):
        #                 snap.delete()
        #                 snap.wait_for(
        #                     [SnapshotState.UNKNOWN],
        #                     terminal_states=[SnapshotState.ERROR])
        #
        #             with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
        #                 test_snap.wait_till_ready()

        lc = self.provider.compute.instances.create_launch_config()

        # Add a new blank volume
        #         lc.add_volume_device(size=1, delete_on_terminate=True)

        # Attach an existing volume
        #                 lc.add_volume_device(size=1, source=test_vol,
        #                                      delete_on_terminate=True)

        # Add a new volume based on a snapshot
        #                 lc.add_volume_device(size=1, source=test_snap,
        #                                      delete_on_terminate=True)

        # Override root volume size
        image_id = helpers.get_provider_test_data(self.provider, "image")
        img = self.provider.compute.images.get(image_id)
        lc.add_volume_device(
            is_root=True,
            source=img,
            # TODO: This should be greater than the ami size or tests
            # will fail on actual infrastructure. Needs an image.size
            # method
            size=2,
            delete_on_terminate=True)

        # Add all available ephemeral devices
        instance_type_name = helpers.get_provider_test_data(
            self.provider, "instance_type")
        inst_type = self.provider.compute.instance_types.find(
            name=instance_type_name)[0]
        for _ in range(inst_type.num_ephemeral_disks):
            lc.add_ephemeral_device()

        net, _ = helpers.create_test_network(self.provider, name)

        inst = helpers.create_test_instance(
            self.provider,
            name,
            network=net,
            # We don't have a way to match the test net placement and this zone
            # zone=helpers.get_provider_test_data(self.provider, 'placement'),
            launch_config=lc)

        def cleanup(instance, net):
            instance.terminate()
            instance.wait_for(
                [InstanceState.TERMINATED, InstanceState.UNKNOWN],
                terminal_states=[InstanceState.ERROR])
            helpers.delete_test_network(net)

        with helpers.cleanup_action(lambda: cleanup(inst, net)):
            try:
                inst.wait_till_ready()
            except WaitStateException as e:
                self.fail("The block device mapped launch did not "
                          " complete successfully: %s" % e)