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_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)
Esempio n. 3
0
    def test_attach_detach_volume(self):
        """
        Create a new volume, and attempt to attach it to an instance
        """
        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])
Esempio n. 4
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 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_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)
Esempio n. 5
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)
Esempio n. 6
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_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)
Esempio n. 8
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 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_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.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_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)
Esempio n. 10
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)
    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')
Esempio n. 12
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")
Esempio n. 13
0
    def test_block_device_mapping_attachments(self):
        label = "cb-blkattch-{0}".format(helpers.get_uuid())

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

        test_vol = self.provider.storage.volumes.create(
           label, 1)
        with cb_helpers.cleanup_action(lambda: test_vol.delete()):
            test_vol.wait_till_ready()
            test_snap = 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])

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

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

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

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

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

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

                # Add all available ephemeral devices
                vm_type_name = helpers.get_provider_test_data(
                    self.provider,
                    "vm_type")
                vm_type = self.provider.compute.vm_types.find(
                    name=vm_type_name)[0]
                # Some providers, e.g. GCP, has a limit on total number of
                # attached disks; it does not matter how many of them are
                # ephemeral or persistent. So, wee keep in mind that we have
                # attached 4 disks already, and add ephemeral disks accordingly
                # to not exceed the limit.
                for _ in range(vm_type.num_ephemeral_disks - 4):
                    lc.add_ephemeral_device()

                subnet = helpers.get_or_create_default_subnet(
                    self.provider)

                inst = None
                with cb_helpers.cleanup_action(
                        lambda: helpers.delete_instance(inst)):
                    inst = helpers.create_test_instance(
                        self.provider,
                        label,
                        subnet=subnet,
                        launch_config=lc)
                    try:
                        inst.wait_till_ready()
                    except WaitStateException as e:
                        self.fail("The block device mapped launch did not "
                                  " complete successfully: %s" % e)
Esempio n. 14
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)