Exemplo n.º 1
0
    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 cb_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))
Exemplo n.º 2
0
 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")
Exemplo n.º 3
0
    def test_vm_firewall_rule_add_twice(self):
        label = 'cb-fwruletwice-{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 cb_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')
            # 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)
Exemplo n.º 4
0
    def test_volume_properties(self):
        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 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,
                                                      label,
                                                      subnet=subnet)

            test_vol = self.provider.storage.volumes.create(
                label, 1, description=vol_desc)
            with cb_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 (self.provider.PROVIDER_ID != 'azure'
                        and self.provider.PROVIDER_ID != 'gcp'
                        and self.provider.PROVIDER_ID != 'openstack'):
                    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])
Exemplo n.º 5
0
    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 cb_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)
Exemplo n.º 6
0
    def test_instance_start_stop_methods(self):
        label = "cb-instmethods-{0}".format(helpers.get_uuid())

        if self.provider.PROVIDER_ID != ProviderList.AWS:
            raise self.skipTest(f"Instance start/stop methods not implemented"
                                f"for provider: {self.provider.PROVIDER_ID}")

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        subnet = None
        test_inst = None
        with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                instance=test_inst)):
            subnet = helpers.get_or_create_default_subnet(self.provider)
            test_inst = helpers.get_test_instance(self.provider, label,
                                                  subnet=subnet)

            # check whether stopping aws instance works
            resp = test_inst.stop()
            test_inst.wait_for([InstanceState.STOPPED])
            test_inst.refresh()
            self.assertTrue(
                test_inst.state == InstanceState.STOPPED,
                "Instance state must be stopped when refreshing after a "
                "'stop' operation but got %s"
                % test_inst.state)

            self.assertTrue(resp, "Response from method was suppose to be"
                            + " True but got False")

            # check whether starting aws instance works
            resp = test_inst.start()
            test_inst.wait_for([InstanceState.RUNNING])
            test_inst.refresh()
            self.assertTrue(
                test_inst.state == InstanceState.RUNNING,
                "Instance state must be running when refreshing after a "
                "'start' operation but got %s"
                % test_inst.state)

            self.assertTrue(resp, "Response from method was suppose to be"
                            + " True but got False")
Exemplo n.º 7
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 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,
                                                      label,
                                                      subnet=subnet)

            test_vol = self.provider.storage.volumes.create(label, 1)
            with cb_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])
Exemplo n.º 8
0
    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")
Exemplo n.º 9
0
    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)

                    with cb_helpers.cleanup_action(
                            lambda: test_inst.remove_floating_ip(fip.id)):
                        test_inst.add_floating_ip(fip.id)
                        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)
Exemplo n.º 10
0
    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)