示例#1
0
    def test_crud_snapshot(self):
        """
        Create a new volume, create a snapshot of the volume, and check
        whether list_snapshots properly detects the new snapshot.
        Delete everything afterwards.
        """
        name = "cb_crudsnap-{0}".format(helpers.get_uuid())
        test_vol = self.provider.storage.volumes.create(
            name, 1, helpers.get_provider_test_data(self.provider,
                                                    "placement"))
        with helpers.cleanup_action(lambda: test_vol.delete()):
            test_vol.wait_till_ready()

            def create_snap(name):
                return test_vol.create_snapshot(name=name, description=name)

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

            sit.check_crud(self, self.provider.storage.snapshots, Snapshot,
                           "cb_snap", create_snap, cleanup_snap)

            # Test creation of a snap via SnapshotService
            def create_snap2(name):
                return self.provider.storage.snapshots.create(name=name,
                                                              volume=test_vol,
                                                              description=name)

            if (self.provider.PROVIDER_ID == ProviderList.AWS
                    and not isinstance(self.provider, TestMockHelperMixin)):
                time.sleep(15)  # Or get SnapshotCreationPerVolumeRateExceeded
            sit.check_crud(self, self.provider.storage.snapshots, Snapshot,
                           "cb_snaptwo", create_snap2, cleanup_snap)
    def test_crud_subnet(self):
        # Late binding will make sure that create_subnet gets the
        # correct value
        sn = helpers.get_or_create_default_subnet(self.provider)
        net = sn.network

        def create_subnet(label):
            return self.provider.networking.subnets.create(
                label=label,
                network=net,
                cidr_block="10.0.10.0/24",
                zone=helpers.get_provider_test_data(self.provider,
                                                    'placement'))

        def cleanup_subnet(subnet):
            if subnet:
                subnet.delete()
                subnet.refresh()
                self.assertTrue(
                    subnet.state == SubnetState.UNKNOWN,
                    "Subnet.state must be unknown when refreshing after "
                    "a delete but got %s" % subnet.state)

        sit.check_crud(self, self.provider.networking.subnets, Subnet,
                       "cb-crudsubnet", create_subnet, cleanup_subnet)
示例#3
0
    def test_crud_subnet(self):
        # Late binding will make sure that create_subnet gets the
        # correct value
        net = self.provider.networking.networks.create(
                  label="cb-crudsubnet",
                  cidr_block=BaseNetwork.CB_DEFAULT_IPV4RANGE)

        def create_subnet(label):
            return self.provider.networking.subnets.create(
                label=label, network=net, cidr_block="10.0.10.0/24")

        def cleanup_subnet(subnet):
            if subnet:
                net = subnet.network
                subnet.delete()
                subnet.wait_for([SubnetState.UNKNOWN],
                                terminal_states=[SubnetState.ERROR])
                self.assertTrue(
                    subnet.state == SubnetState.UNKNOWN,
                    "Subnet.state must be unknown after "
                    "a delete but got %s"
                    % subnet.state)
                net.delete()
                net.wait_for([NetworkState.UNKNOWN],
                             terminal_states=[NetworkState.ERROR])
                self.assertTrue(
                    net.state == NetworkState.UNKNOWN,
                    "Network.state must be unknown after "
                    "a delete but got %s"
                    % net.state)

        sit.check_crud(self, self.provider.networking.subnets, Subnet,
                       "cb-crudsubnet", create_subnet, cleanup_subnet)
示例#4
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)
示例#5
0
    def test_crud_bucket(self):
        """
        Create a new bucket, check whether the expected values are set,
        and delete it.
        """
        def create_bucket(name):
            return self.provider.object_store.create(name)

        def cleanup_bucket(bucket):
            bucket.delete()

        with self.assertRaises(InvalidNameException):
            # underscores are not allowed in bucket names
            create_bucket("cb_bucket")

        with self.assertRaises(InvalidNameException):
            # names of length less than 3 should raise an exception
            create_bucket("cb")

        with self.assertRaises(InvalidNameException):
            # names of length greater than 63 should raise an exception
            create_bucket("a" * 64)

        with self.assertRaises(InvalidNameException):
            # bucket name cannot be an IP address
            create_bucket("197.10.100.42")

        sit.check_crud(self,
                       self.provider.object_store,
                       Bucket,
                       "cb-crudbucket",
                       create_bucket,
                       cleanup_bucket,
                       skip_name_check=True)
示例#6
0
    def test_crud_vm_firewall_rules(self):
        label = 'cb-crudfw-rules-{0}'.format(helpers.get_uuid())

        subnet = helpers.get_or_create_default_subnet(self.provider)
        net = subnet.network

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

            def create_fw_rule(label):
                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):
                if rule:
                    rule.delete()

            sit.check_crud(self,
                           fw.rules,
                           VMFirewallRule,
                           "cb-crudfwrule",
                           create_fw_rule,
                           cleanup_fw_rule,
                           skip_name_check=True)
    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)
示例#8
0
    def test_crud_bucket_object(self):
        test_bucket = None

        def create_bucket_obj(name):
            obj = test_bucket.create_object(name)
            # TODO: This is wrong. We shouldn't have to have a separate
            # call to upload some content before being able to delete
            # the content. Maybe the create_object method should accept
            # the file content as a parameter.
            obj.upload("dummy content")
            return obj

        def cleanup_bucket_obj(bucket_obj):
            bucket_obj.delete()

        with helpers.cleanup_action(lambda: test_bucket.delete()):
            name = "cb-crudbucketobj-{0}".format(uuid.uuid4())
            test_bucket = self.provider.object_store.create(name)

            sit.check_crud(self,
                           test_bucket,
                           BucketObject,
                           "cb_bucketobj",
                           create_bucket_obj,
                           cleanup_bucket_obj,
                           skip_name_check=True)
    def test_crud_network(self):
        def create_net(name):
            return self.provider.networking.networks.create(
                name=name, cidr_block='10.0.0.0/16')

        def cleanup_net(net):
            self.provider.networking.networks.delete(network_id=net.id)

        sit.check_crud(self, self.provider.networking.networks, Network,
                       "cb_crudnetwork", create_net, cleanup_net)
    def test_crud_floating_ip(self):
        def create_fip(name):
            return self.provider.networking.floating_ips.create()

        def cleanup_fip(fip):
            self.provider.networking.floating_ips.delete(fip.id)

        sit.check_crud(self,
                       self.provider.networking.floating_ips,
                       FloatingIP,
                       "cb_crudfip",
                       create_fip,
                       cleanup_fip,
                       skip_name_check=True)
    def test_crud_network(self):
        def create_net(label):
            return self.provider.networking.networks.create(
                label=label, cidr_block=BaseNetwork.CB_DEFAULT_IPV4RANGE)

        def cleanup_net(net):
            if net:
                net.delete()
                net.refresh()
                self.assertTrue(
                    net.state == NetworkState.UNKNOWN,
                    "Network.state must be unknown when refreshing after "
                    "a delete but got %s" % net.state)

        sit.check_crud(self, self.provider.networking.networks, Network,
                       "cb-crudnetwork", create_net, cleanup_net)
示例#12
0
    def test_crud_floating_ip(self):
        gw = helpers.get_test_gateway(
            self.provider)

        def create_fip(label):
            fip = gw.floating_ips.create()
            return fip

        def cleanup_fip(fip):
            if fip:
                gw.floating_ips.delete(fip.id)

        with cb_helpers.cleanup_action(
                lambda: helpers.cleanup_gateway(gw)):
            sit.check_crud(self, gw.floating_ips, FloatingIP,
                           "cb-crudfip", create_fip, cleanup_fip,
                           skip_name_check=True)
    def test_crud_key_pair_service(self):

        def create_kp(name):
            return self.provider.security.key_pairs.create(name=name)

        def cleanup_kp(kp):
            if kp:
                self.provider.security.key_pairs.delete(key_pair_id=kp.id)

        def extra_tests(kp):
            # Recreating existing keypair should raise an exception
            with self.assertRaises(DuplicateResourceException):
                self.provider.security.key_pairs.create(name=kp.name)

        sit.check_crud(self, self.provider.security.key_pairs, KeyPair,
                       "cb-crudkp", create_kp, cleanup_kp,
                       extra_test_func=extra_tests)
示例#14
0
    def test_crud_volume(self):
        """
        Create a new volume, check whether the expected values are set,
        and delete it
        """
        def create_vol(name):
            return self.provider.storage.volumes.create(
                name, 1,
                helpers.get_provider_test_data(self.provider, "placement"))

        def cleanup_vol(vol):
            vol.delete()
            vol.wait_for([VolumeState.DELETED, VolumeState.UNKNOWN],
                         terminal_states=[VolumeState.ERROR])

        sit.check_crud(self, self.provider.storage.volumes, Volume,
                       "cb_createvol", create_vol, cleanup_vol)
示例#15
0
    def test_create_and_list_image(self):
        """
        Create a new image and check whether that image can be listed.
        This covers waiting till the image is ready, checking that the image
        name is the expected one and whether list_images is functional.
        """
        instance_name = "cb_crudimage-{0}".format(helpers.get_uuid())

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

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

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

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

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

            sit.check_crud(self,
                           self.provider.compute.images,
                           MachineImage,
                           "cb_listimg",
                           create_img,
                           cleanup_img,
                           extra_test_func=extra_tests)
    def test_crud_subnet(self):
        # Late binding will make sure that create_subnet gets the
        # correct value
        net = None

        def create_subnet(name):
            return self.provider.networking.subnets.create(
                network=net, cidr_block="10.0.0.0/24", name=name)

        def cleanup_subnet(subnet):
            self.provider.networking.subnets.delete(subnet=subnet)

        net_name = 'cb_crudsubnet-{0}'.format(helpers.get_uuid())
        net = self.provider.networking.networks.create(
            name=net_name, cidr_block='10.0.0.0/16')
        with helpers.cleanup_action(lambda: self.provider.networking.networks.
                                    delete(network_id=net.id)):
            sit.check_crud(self, self.provider.networking.subnets, Subnet,
                           "cb_crudsubnet", create_subnet, cleanup_subnet)
示例#17
0
    def test_crud_volume(self):
        def create_vol(label):
            return self.provider.storage.volumes.create(
                label, 1,
                helpers.get_provider_test_data(self.provider, "placement"))

        def cleanup_vol(vol):
            if vol:
                vol.delete()
                vol.wait_for([VolumeState.DELETED, VolumeState.UNKNOWN],
                             terminal_states=[VolumeState.ERROR])
                vol.refresh()
                self.assertTrue(
                    vol.state == VolumeState.UNKNOWN,
                    "Volume.state must be unknown when refreshing after a "
                    "delete but got %s" % vol.state)

        sit.check_crud(self, self.provider.storage.volumes, Volume,
                       "cb-createvol", create_vol, cleanup_vol)
    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_crud_floating_ip(self):
        net, gw = helpers.get_test_gateway(
            self.provider, 'cb_crudfipgw-{0}'.format(helpers.get_uuid()))

        def create_fip(name):
            fip = gw.floating_ips.create()
            return fip

        def cleanup_fip(fip):
            gw.floating_ips.delete(fip.id)

        with helpers.cleanup_action(
                lambda: helpers.delete_test_gateway(net, gw)):
            sit.check_crud(self,
                           gw.floating_ips,
                           FloatingIP,
                           "cb_crudfip",
                           create_fip,
                           cleanup_fip,
                           skip_name_check=True)
    def test_crud_vm_firewall(self):

        subnet = helpers.get_or_create_default_subnet(self.provider)
        net = subnet.network

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

        def cleanup_fw(fw):
            if fw:
                fw.delete()

        def network_id_test(fw):
            # Checking that the network ID is returned correctly
            self.assertEqual(fw.network_id, net.id)

        sit.check_crud(self, self.provider.security.vm_firewalls,
                       VMFirewall, "cb-crudfw", create_fw, cleanup_fw,
                       extra_test_func=network_id_test)
示例#21
0
    def test_crud_bucket(self):
        def create_bucket(name):
            return self.provider.storage.buckets.create(name)

        def cleanup_bucket(bucket):
            if bucket:
                bucket.delete()

        def extra_tests(bucket):
            # Recreating existing bucket should raise an exception
            with self.assertRaises(DuplicateResourceException):
                self.provider.storage.buckets.create(name=bucket.name)

        sit.check_crud(self,
                       self.provider.storage.buckets,
                       Bucket,
                       "cb-crudbucket",
                       create_bucket,
                       cleanup_bucket,
                       extra_test_func=extra_tests)
示例#22
0
    def test_crud_instance(self):
        label = "cb-instcrud-{0}".format(helpers.get_uuid())
        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        subnet = None

        def create_inst(label):
            # Also test whether sending in an empty_dict for user_data
            # results in an automatic conversion to string.
            return helpers.get_test_instance(self.provider,
                                             label,
                                             subnet=subnet,
                                             user_data={})

        def cleanup_inst(inst):
            if inst:
                inst.delete()
                inst.wait_for([InstanceState.DELETED, InstanceState.UNKNOWN])
                inst.refresh()
                self.assertTrue(
                    inst.state == InstanceState.UNKNOWN,
                    "Instance.state must be unknown when refreshing after a "
                    "delete but got %s" % inst.state)

        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.DELETED, InstanceState.UNKNOWN),
                "Instance %s should have been deleted but still exists." %
                label)

        subnet = helpers.get_or_create_default_subnet(self.provider)

        sit.check_crud(self,
                       self.provider.compute.instances,
                       Instance,
                       "cb-instcrud",
                       create_inst,
                       cleanup_inst,
                       custom_check_delete=check_deleted)
示例#23
0
    def test_crud_snapshot(self):
        # Create a new volume, create a snapshot of the volume, and check
        # whether list_snapshots properly detects the new snapshot.
        # Delete everything afterwards.
        label = "cb-crudsnap-{0}".format(helpers.get_uuid())
        test_vol = self.provider.storage.volumes.create(
            label, 1)
        with cb_helpers.cleanup_action(lambda: test_vol.delete()):
            test_vol.wait_till_ready()

            def create_snap(label):
                return test_vol.create_snapshot(label=label,
                                                description=label)

            def cleanup_snap(snap):
                if snap:
                    snap.delete()
                    snap.wait_for([SnapshotState.UNKNOWN],
                                  terminal_states=[SnapshotState.ERROR])
                    snap.refresh()
                    self.assertTrue(
                        snap.state == SnapshotState.UNKNOWN,
                        "Snapshot.state must be unknown when refreshing after "
                        "a delete but got %s"
                        % snap.state)

            sit.check_crud(self, self.provider.storage.snapshots, Snapshot,
                           "cb-snap", create_snap, cleanup_snap)

            # Test creation of a snap via SnapshotService
            def create_snap2(label):
                return self.provider.storage.snapshots.create(
                    label=label, volume=test_vol, description=label)

            if (self.provider.PROVIDER_ID == ProviderList.AWS and
                    not isinstance(self.provider, TestMockHelperMixin)):
                time.sleep(15)  # Or get SnapshotCreationPerVolumeRateExceeded
            sit.check_crud(self, self.provider.storage.snapshots, Snapshot,
                           "cb-snaptwo", create_snap2, cleanup_snap)
示例#24
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)