Example #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])
Example #2
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)
    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 create_instance_from_image(img):
     img_instance = None
     with cb_helpers.cleanup_action(
             lambda: helpers.cleanup_test_resources(img_instance)):
         img_instance = self.provider.compute.instances.create(
             img_inst_label,
             img,
             helpers.get_provider_test_data(self.provider, 'vm_type'),
             subnet=subnet)
         img_instance.wait_till_ready()
         self.assertIsInstance(img_instance, Instance)
         self.assertEqual(
             img_instance.label, img_inst_label,
             "Instance label {0} is not equal to the expected label"
             " {1}".format(img_instance.label, img_inst_label))
         image_id = img.id
         self.assertEqual(
             img_instance.image_id, image_id,
             "Image id {0} is not equal to the expected id"
             " {1}".format(img_instance.image_id, image_id))
         self.assertIsInstance(img_instance.public_ips, list)
         if img_instance.public_ips:
             self.assertTrue(
                 img_instance.public_ips[0],
                 "public ip should contain a"
                 " valid value if a list of public_ips exist")
         self.assertIsInstance(img_instance.private_ips, list)
         self.assertTrue(img_instance.private_ips[0],
                         "private ip should"
                         " contain a valid value")
Example #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)
    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))
    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])
Example #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])
    def test_vm_firewall_group_rule(self):
        label = 'cb-fwrule-{0}'.format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        fw = None
        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                vm_firewall=fw)):
            subnet = helpers.get_or_create_default_subnet(self.provider)
            net = subnet.network
            fw = self.provider.security.vm_firewalls.create(
                label=label, description=label, network=net.id)
            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.label == fw.label,
                "Expected VM firewall rule label {0}. Got {1}."
                .format(fw.label, rule.src_dest_fw.label))
            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.label == label]
        self.assertTrue(
            len(found_fw) == 0,
            "VM firewall {0} should have been deleted but still exists."
            .format(label))
    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_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))
Example #12
0
    def test_volume_properties(self):
        """
        Test volume properties
        """
        label = "cb-volprops-{0}".format(helpers.get_uuid())
        vol_desc = 'newvoldesc1'
        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance)):
            subnet = helpers.get_or_create_default_subnet(self.provider)
            test_instance = helpers.get_test_instance(self.provider,
                                                      label,
                                                      subnet=subnet)

            test_vol = self.provider.storage.volumes.create(
                label, 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)
                if not self.provider.PROVIDER_ID == 'azure':
                    self.assertEqual(test_vol.attachments.device, "/dev/sda2")
                test_vol.detach()
                test_vol.label = 'newvolname1'
                test_vol.wait_for(
                    [VolumeState.AVAILABLE],
                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
                self.assertEqual(test_vol.label, '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_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):
        label = 'cb-propfw-{0}'.format(helpers.get_uuid())

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

            self.assertEqual(label, fw.description)
    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))
    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)
    def test_vm_firewall_rule_properties(self):
        label = 'cb-propfwrule-{0}'.format(helpers.get_uuid())

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

            rule = fw.rules.create(
                direction=TrafficDirection.INBOUND, protocol='tcp',
                from_port=1111, to_port=1111, cidr='0.0.0.0/0')
            self.assertEqual(rule.direction, TrafficDirection.INBOUND)
            self.assertEqual(rule.protocol, 'tcp')
            self.assertEqual(rule.from_port, 1111)
            self.assertEqual(rule.to_port, 1111)
            self.assertEqual(rule.cidr, '0.0.0.0/0')
Example #21
0
    def test_attach_detach_volume(self):
        label = "cb-attachvol-{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
        with helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance)):
            subnet = helpers.get_or_create_default_subnet(self.provider)
            test_instance = helpers.get_test_instance(self.provider,
                                                      label,
                                                      subnet=subnet)

            test_vol = self.provider.storage.volumes.create(
                label, 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_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))
Example #23
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)
    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))
Example #26
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)
    def test_instance_methods(self):
        label = "cb-instmethods-{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_inst = None
        fw = None
        with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                instance=test_inst, vm_firewall=fw, network=net)):
            net = self.provider.networking.networks.create(
                label=label, cidr_block=BaseNetwork.CB_DEFAULT_IPV4RANGE)
            cidr = '10.0.1.0/24'
            subnet = net.subnets.create(label=label, cidr_block=cidr)
            test_inst = helpers.get_test_instance(self.provider, label,
                                                  subnet=subnet)
            fw = self.provider.security.vm_firewalls.create(
                label=label, description=label, network=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(label, net)
            gateway = net.gateways.get_or_create()

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

            with cb_helpers.cleanup_action(lambda: cleanup_router(router,
                                                                  gateway)):
                router.attach_subnet(subnet)
                router.attach_gateway(gateway)
                fip = None

                with cb_helpers.cleanup_action(
                        lambda: helpers.cleanup_fip(fip)):
                    # check whether adding an elastic ip works
                    fip = gateway.floating_ips.create()
                    self.assertFalse(
                        fip.in_use,
                        "Newly created floating IP %s should not be in use." %
                        fip.public_ip)

                    with cb_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 %s address should be in use."
                            % fip.public_ip)
                    test_inst.refresh()
                    test_inst.reboot()
                    test_inst.wait_till_ready()
                    self.assertNotIn(
                        fip.public_ip,
                        test_inst.public_ips + test_inst.private_ips)
    def test_create_and_list_image(self):
        instance_label = "cb-crudimage-{0}".format(helpers.get_uuid())
        img_inst_label = "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
        subnet = None

        def create_img(label):
            return test_instance.create_image(label=label)

        def cleanup_img(img):
            if img:
                img.delete()
                img.wait_for(
                    [MachineImageState.UNKNOWN, MachineImageState.ERROR])
                img.refresh()
                self.assertTrue(
                    img.state == MachineImageState.UNKNOWN,
                    "MachineImage.state must be unknown when refreshing after "
                    "a delete but got %s" % img.state)

        def extra_tests(img):
            # check image size
            img.refresh()
            self.assertGreater(
                img.min_disk, 0, "Minimum disk"
                " size required by image is invalid")
            create_instance_from_image(img)

        def create_instance_from_image(img):
            img_instance = None
            with cb_helpers.cleanup_action(
                    lambda: helpers.cleanup_test_resources(img_instance)):
                img_instance = self.provider.compute.instances.create(
                    img_inst_label,
                    img,
                    helpers.get_provider_test_data(self.provider, 'vm_type'),
                    subnet=subnet)
                img_instance.wait_till_ready()
                self.assertIsInstance(img_instance, Instance)
                self.assertEqual(
                    img_instance.label, img_inst_label,
                    "Instance label {0} is not equal to the expected label"
                    " {1}".format(img_instance.label, img_inst_label))
                image_id = img.id
                self.assertEqual(
                    img_instance.image_id, image_id,
                    "Image id {0} is not equal to the expected id"
                    " {1}".format(img_instance.image_id, image_id))
                self.assertIsInstance(img_instance.public_ips, list)
                if img_instance.public_ips:
                    self.assertTrue(
                        img_instance.public_ips[0],
                        "public ip should contain a"
                        " valid value if a list of public_ips exist")
                self.assertIsInstance(img_instance.private_ips, list)
                self.assertTrue(img_instance.private_ips[0],
                                "private ip should"
                                " contain a valid value")

        with cb_helpers.cleanup_action(
                lambda: helpers.cleanup_test_resources(test_instance)):
            subnet = helpers.get_or_create_default_subnet(self.provider)
            test_instance = helpers.get_test_instance(self.provider,
                                                      instance_label,
                                                      subnet=subnet)
            sit.check_crud(self,
                           self.provider.compute.images,
                           MachineImage,
                           "cb-listimg",
                           create_img,
                           cleanup_img,
                           extra_test_func=extra_tests)
Example #29
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")
Example #30
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)
    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)
    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_properties(self):
        label = "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
        fw = None
        kp = None
        with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, fw, kp)):
            subnet = helpers.get_or_create_default_subnet(self.provider)
            net = subnet.network
            kp = self.provider.security.key_pairs.create(name=label)
            fw = self.provider.security.vm_firewalls.create(
                label=label, description=label, network=net.id)
            test_instance = helpers.get_test_instance(self.provider,
                                                      label, key_pair=kp,
                                                      vm_firewalls=[fw],
                                                      subnet=subnet)
            self.assertEqual(
                test_instance.label, label,
                "Instance label {0} is not equal to the expected label"
                " {1}".format(test_instance.label, label))
            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_id,
                kp.id)
            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")
Example #34
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))