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 test_attach_detach_volume(self): """ Create a new volume, and attempt to attach it to an instance """ instance_name = "CBVolOps-{0}-{1}".format( self.provider.name, uuid.uuid4()) net, _ = helpers.create_test_network(self.provider, instance_name) test_instance = helpers.get_test_instance(self.provider, instance_name, network=net) with helpers.cleanup_action(lambda: helpers.cleanup_test_resources( test_instance, net)): name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4()) test_vol = self.provider.block_store.volumes.create( name, 1, test_instance.zone_id) with helpers.cleanup_action(lambda: test_vol.delete()): test_vol.wait_till_ready() test_vol.attach(test_instance, '/dev/sda2') test_vol.wait_for( [VolumeState.IN_USE], terminal_states=[VolumeState.ERROR, VolumeState.DELETED]) test_vol.detach() test_vol.wait_for( [VolumeState.AVAILABLE], terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
def test_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])
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_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_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_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_instance_properties(self): name = "CBInstProps-{0}-{1}".format( self.provider.name, uuid.uuid4()) kp = self.provider.security.key_pairs.create(name=name) sg = self.provider.security.security_groups.create( name=name, description=name) test_instance = helpers.get_test_instance(self.provider, name, keypair=kp, security_groups=[sg]) def cleanup(inst, kp, sg): inst.terminate() inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN], terminal_states=[InstanceState.ERROR]) kp.delete() sg.delete() with helpers.cleanup_action(lambda: cleanup(test_instance, kp, sg)): 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.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) # 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 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, InstanceType)
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_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_instance_properties(self): name = "CBInstProps-{0}-{1}".format( self.provider.name, uuid.uuid4()) kp = self.provider.security.key_pairs.create(name=name) sg = self.provider.security.security_groups.create( name=name, description=name) test_instance = helpers.get_test_instance(self.provider, name, key_pair=kp, security_groups=[sg]) def cleanup(inst, kp, sg): inst.terminate() inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN], terminal_states=[InstanceState.ERROR]) kp.delete() sg.delete() with helpers.cleanup_action(lambda: cleanup(test_instance, kp, sg)): 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 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))
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")
def test_create_and_list_image(self): """ Create a new image and check whether that image can be listed. This covers waiting till the image is ready, checking that the image name is the expected one and whether list_images is functional. """ instance_name = "CBImageTest-{0}-{1}".format(self.provider.name, uuid.uuid4()) net, _ = helpers.create_test_network(self.provider, instance_name) test_instance = helpers.get_test_instance(self.provider, instance_name, network=net) with helpers.cleanup_action( lambda: helpers.cleanup_test_resources(test_instance, net)): name = "CBUnitTestListImg-{0}".format(uuid.uuid4()) test_image = test_instance.create_image(name) def cleanup_img(img): img.delete() img.wait_for( [MachineImageState.UNKNOWN, MachineImageState.ERROR]) with helpers.cleanup_action(lambda: cleanup_img(test_image)): test_image.wait_till_ready() self.assertTrue( test_instance.id in repr(test_instance), "repr(obj) should contain the object id so that the object" " can be reconstructed, but does not.") self.assertTrue( test_image.description is None or isinstance(test_image.description, six.string_types), "Image description must be None or a string") images = self.provider.compute.images.list() list_images = [image for image in images if image.name == name] self.assertTrue( len(list_images) == 1, "List images does not return the expected image %s" % name) # check iteration iter_images = [ image for image in self.provider.compute.images if image.name == name ] self.assertTrue( len(iter_images) == 1, "Iter images does not return the expected image %s" % name) # find image found_images = self.provider.compute.images.find(name=name) self.assertTrue( len(found_images) == 1, "Find images error: expected image %s but found: %s" % (name, found_images)) # check non-existent find ne_images = self.provider.compute.images.find( name="non_existent") self.assertTrue( len(ne_images) == 0, "Find() for a non-existent image returned %s" % ne_images) get_img = self.provider.compute.images.get(test_image.id) self.assertTrue( found_images[0] == iter_images[0] == get_img == test_image, "Objects returned by list: {0} and get: {1} are not as " " expected: {2}".format(found_images[0].id, get_img.id, test_image.id)) self.assertTrue( list_images[0].name == found_images[0].name == get_img.name == test_image.name, "Names returned by list: {0}, find: {1} and get: {2} are" " not as expected: {3}".format(list_images[0].name, found_images[0].name, get_img.name, test_image.name)) # TODO: Images take a long time to deregister on EC2. Needs # investigation images = self.provider.compute.images.list() found_images = [image for image in images if image.name == name] self.assertTrue( len(found_images) == 0, "Image %s should have been deleted but still exists." % name)
def test_instance_methods(self): name = "cb_instmethods-{0}".format(helpers.get_uuid()) # Declare these variables and late binding will allow # the cleanup method access to the most current values test_inst = None net = None fw = None with helpers.cleanup_action( lambda: helpers.cleanup_test_resources(test_inst, net, fw)): net, subnet = helpers.create_test_network(self.provider, name) test_inst = helpers.get_test_instance(self.provider, name, subnet=subnet) fw = self.provider.security.vm_firewalls.create(name=name, description=name, network_id=net.id) # Check adding a VM firewall to a running instance test_inst.add_vm_firewall(fw) test_inst.refresh() self.assertTrue( fw in test_inst.vm_firewalls, "Expected VM firewall '%s'" " to be among instance vm_firewalls: [%s]" % (fw, test_inst.vm_firewalls)) # Check removing a VM firewall from a running instance test_inst.remove_vm_firewall(fw) test_inst.refresh() self.assertTrue( fw not in test_inst.vm_firewalls, "Expected VM firewall" " '%s' to be removed from instance vm_firewalls: [%s]" % (fw, test_inst.vm_firewalls)) # check floating ips router = self.provider.networking.routers.create(name, net) gateway = None def cleanup_router(router, gateway): with helpers.cleanup_action(lambda: router.delete()): with helpers.cleanup_action(lambda: gateway.delete()): router.detach_subnet(subnet) router.detach_gateway(gateway) with helpers.cleanup_action( lambda: cleanup_router(router, gateway)): router.attach_subnet(subnet) gateway = net.gateways.get_or_create_inet_gateway(name) router.attach_gateway(gateway) # check whether adding an elastic ip works fip = gateway.floating_ips.create() self.assertFalse( fip.in_use, "Newly created floating IP address should not be in use.") with helpers.cleanup_action(lambda: fip.delete()): with helpers.cleanup_action( lambda: test_inst.remove_floating_ip(fip)): test_inst.add_floating_ip(fip) test_inst.refresh() # On Devstack, FloatingIP is listed under private_ips. self.assertIn( fip.public_ip, test_inst.public_ips + test_inst.private_ips) fip.refresh() self.assertTrue( fip.in_use, "Attached floating IP address should be in use.") test_inst.refresh() self.assertNotIn( fip.public_ip, test_inst.public_ips + test_inst.private_ips)
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_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_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")
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)
def create_inst(name): return helpers.get_test_instance(self.provider, name, subnet=subnet)
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))
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_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_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()) test_instance = helpers.get_test_instance(self.provider, instance_name) with helpers.cleanup_action(lambda: test_instance.terminate()): 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_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)