From 004c7352d0a4fb467a319ae9743eb6ca5ee9ce7f Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 5 May 2023 11:22:56 +0100 Subject: [PATCH] Blackify openstack.cloud Black used with the '-l 79 -S' flags. A future change will ignore this commit in git-blame history by adding a 'git-blame-ignore-revs' file. Change-Id: Ib58bb45ce8c29e5347ffc36d40d6f5d52b140c6b Signed-off-by: Stephen Finucane --- openstack/cloud/_accelerator.py | 4 +- openstack/cloud/_baremetal.py | 190 +- openstack/cloud/_block_storage.py | 84 +- openstack/cloud/_clustering.py | 4 +- openstack/cloud/_coe.py | 70 +- openstack/cloud/_compute.py | 486 +-- openstack/cloud/_dns.py | 51 +- openstack/cloud/_floating_ip.py | 520 ++- openstack/cloud/_identity.py | 278 +- openstack/cloud/_image.py | 79 +- openstack/cloud/_network.py | 748 +++-- openstack/cloud/_network_common.py | 138 +- openstack/cloud/_object_store.py | 105 +- openstack/cloud/_orchestration.py | 120 +- openstack/cloud/_security_group.py | 191 +- openstack/cloud/_utils.py | 89 +- openstack/cloud/cmd/inventory.py | 43 +- openstack/cloud/exc.py | 8 +- openstack/cloud/inventory.py | 32 +- openstack/cloud/meta.py | 111 +- openstack/cloud/openstackcloud.py | 147 +- .../tests/functional/cloud/test_aggregate.py | 18 +- .../cloud/test_cluster_templates.py | 29 +- .../tests/functional/cloud/test_clustering.py | 810 ++--- .../tests/functional/cloud/test_compute.py | 182 +- .../tests/functional/cloud/test_devstack.py | 14 +- .../tests/functional/cloud/test_domain.py | 34 +- .../tests/functional/cloud/test_endpoints.py | 84 +- .../tests/functional/cloud/test_flavor.py | 26 +- .../functional/cloud/test_floating_ip.py | 97 +- .../functional/cloud/test_floating_ip_pool.py | 3 +- .../tests/functional/cloud/test_groups.py | 18 +- .../tests/functional/cloud/test_identity.py | 250 +- .../tests/functional/cloud/test_image.py | 39 +- .../tests/functional/cloud/test_inventory.py | 9 +- .../tests/functional/cloud/test_keypairs.py | 4 +- .../tests/functional/cloud/test_limits.py | 1 - .../functional/cloud/test_magnum_services.py | 1 - .../tests/functional/cloud/test_network.py | 11 +- .../tests/functional/cloud/test_object.py | 157 +- openstack/tests/functional/cloud/test_port.py | 22 +- .../tests/functional/cloud/test_project.py | 45 +- .../functional/cloud/test_project_cleanup.py | 69 +- .../cloud/test_qos_bandwidth_limit_rule.py | 38 +- .../cloud/test_qos_dscp_marking_rule.py | 18 +- .../cloud/test_qos_minimum_bandwidth_rule.py | 18 +- .../tests/functional/cloud/test_qos_policy.py | 20 +- .../tests/functional/cloud/test_quotas.py | 51 +- .../functional/cloud/test_range_search.py | 18 +- .../tests/functional/cloud/test_recordset.py | 48 +- .../tests/functional/cloud/test_router.py | 144 +- .../functional/cloud/test_security_groups.py | 15 +- .../functional/cloud/test_server_group.py | 9 +- .../tests/functional/cloud/test_services.py | 58 +- .../tests/functional/cloud/test_stack.py | 30 +- .../tests/functional/cloud/test_users.py | 36 +- .../tests/functional/cloud/test_volume.py | 28 +- .../functional/cloud/test_volume_backup.py | 36 +- .../functional/cloud/test_volume_type.py | 71 +- openstack/tests/functional/cloud/test_zone.py | 11 +- openstack/tests/unit/cloud/test__utils.py | 128 +- .../tests/unit/cloud/test_accelerator.py | 457 +-- openstack/tests/unit/cloud/test_aggregate.py | 307 +- .../unit/cloud/test_availability_zones.py | 77 +- .../tests/unit/cloud/test_baremetal_node.py | 2947 ++++++++++------- .../tests/unit/cloud/test_baremetal_ports.py | 156 +- openstack/tests/unit/cloud/test_caching.py | 716 ++-- .../unit/cloud/test_cluster_templates.py | 238 +- openstack/tests/unit/cloud/test_clustering.py | 15 +- .../tests/unit/cloud/test_coe_clusters.py | 4 +- .../cloud/test_coe_clusters_certificate.py | 80 +- .../tests/unit/cloud/test_create_server.py | 2016 ++++++----- .../unit/cloud/test_create_volume_snapshot.py | 158 +- .../tests/unit/cloud/test_delete_server.py | 577 ++-- .../unit/cloud/test_delete_volume_snapshot.py | 132 +- .../tests/unit/cloud/test_domain_params.py | 24 +- openstack/tests/unit/cloud/test_domains.py | 312 +- openstack/tests/unit/cloud/test_endpoints.py | 393 ++- openstack/tests/unit/cloud/test_flavors.py | 422 ++- .../unit/cloud/test_floating_ip_common.py | 179 +- .../unit/cloud/test_floating_ip_neutron.py | 1953 ++++++----- .../tests/unit/cloud/test_floating_ip_nova.py | 416 ++- .../tests/unit/cloud/test_floating_ip_pool.py | 96 +- openstack/tests/unit/cloud/test_fwaas.py | 1971 +++++++---- openstack/tests/unit/cloud/test_groups.py | 157 +- .../tests/unit/cloud/test_identity_roles.py | 308 +- .../tests/unit/cloud/test_identity_users.py | 84 +- openstack/tests/unit/cloud/test_image.py | 2648 +++++++++------ .../tests/unit/cloud/test_image_snapshot.py | 133 +- openstack/tests/unit/cloud/test_inventory.py | 23 +- openstack/tests/unit/cloud/test_keypair.py | 199 +- openstack/tests/unit/cloud/test_limits.py | 148 +- .../tests/unit/cloud/test_magnum_services.py | 19 +- openstack/tests/unit/cloud/test_meta.py | 1304 +++++--- openstack/tests/unit/cloud/test_network.py | 580 ++-- openstack/tests/unit/cloud/test_object.py | 2134 +++++++----- .../tests/unit/cloud/test_openstackcloud.py | 66 +- openstack/tests/unit/cloud/test_operator.py | 123 +- .../tests/unit/cloud/test_operator_noauth.py | 199 +- openstack/tests/unit/cloud/test_port.py | 528 +-- openstack/tests/unit/cloud/test_project.py | 272 +- .../cloud/test_qos_bandwidth_limit_rule.py | 727 ++-- .../unit/cloud/test_qos_dscp_marking_rule.py | 528 ++- .../cloud/test_qos_minimum_bandwidth_rule.py | 532 ++- openstack/tests/unit/cloud/test_qos_policy.py | 550 +-- .../tests/unit/cloud/test_qos_rule_type.py | 233 +- openstack/tests/unit/cloud/test_quotas.py | 439 ++- .../tests/unit/cloud/test_rebuild_server.py | 358 +- openstack/tests/unit/cloud/test_recordset.py | 708 ++-- .../tests/unit/cloud/test_role_assignment.py | 2410 +++++++++----- openstack/tests/unit/cloud/test_router.py | 659 ++-- .../tests/unit/cloud/test_security_groups.py | 1243 ++++--- .../tests/unit/cloud/test_server_console.py | 90 +- .../unit/cloud/test_server_delete_metadata.py | 89 +- .../tests/unit/cloud/test_server_group.py | 74 +- .../unit/cloud/test_server_set_metadata.py | 86 +- openstack/tests/unit/cloud/test_services.py | 362 +- openstack/tests/unit/cloud/test_shade.py | 634 ++-- .../unit/cloud/test_shared_file_system.py | 22 +- openstack/tests/unit/cloud/test_stack.py | 1164 ++++--- openstack/tests/unit/cloud/test_subnet.py | 937 ++++-- .../tests/unit/cloud/test_update_server.py | 135 +- openstack/tests/unit/cloud/test_usage.py | 86 +- openstack/tests/unit/cloud/test_users.py | 231 +- openstack/tests/unit/cloud/test_volume.py | 824 +++-- .../tests/unit/cloud/test_volume_access.py | 368 +- .../tests/unit/cloud/test_volume_backups.py | 313 +- openstack/tests/unit/cloud/test_zone.py | 284 +- 128 files changed, 26600 insertions(+), 16255 deletions(-) diff --git a/openstack/cloud/_accelerator.py b/openstack/cloud/_accelerator.py index b4705815e..5cd7b124f 100644 --- a/openstack/cloud/_accelerator.py +++ b/openstack/cloud/_accelerator.py @@ -70,7 +70,7 @@ class AcceleratorCloudMixin: """ device_profile = self.accelerator.get_device_profile( name_or_id, - filters + filters, ) if device_profile is None: self.log.debug( @@ -104,7 +104,7 @@ class AcceleratorCloudMixin: """ accelerator_request = self.accelerator.get_accelerator_request( name_or_id, - filters + filters, ) if accelerator_request is None: self.log.debug( diff --git a/openstack/cloud/_baremetal.py b/openstack/cloud/_baremetal.py index 35895c35f..e856496b9 100644 --- a/openstack/cloud/_baremetal.py +++ b/openstack/cloud/_baremetal.py @@ -39,7 +39,8 @@ def _normalize_port_list(nics): except KeyError: raise TypeError( "Either 'address' or 'mac' must be provided " - "for port %s" % row) + "for port %s" % row + ) ports.append(dict(row, address=address)) return ports @@ -136,32 +137,34 @@ class BaremetalCloudMixin: raise exc.OpenStackCloudException( "Refusing to inspect available machine %(node)s " "which is associated with an instance " - "(instance_uuid %(inst)s)" % - {'node': node.id, 'inst': node.instance_id}) + "(instance_uuid %(inst)s)" + % {'node': node.id, 'inst': node.instance_id} + ) return_to_available = True # NOTE(TheJulia): Changing available machine to managedable state # and due to state transitions we need to until that transition has # completed. - node = self.baremetal.set_node_provision_state(node, 'manage', - wait=True, - timeout=timeout) + node = self.baremetal.set_node_provision_state( + node, 'manage', wait=True, timeout=timeout + ) if node.provision_state not in ('manageable', 'inspect failed'): raise exc.OpenStackCloudException( "Machine %(node)s must be in 'manageable', 'inspect failed' " "or 'available' provision state to start inspection, the " - "current state is %(state)s" % - {'node': node.id, 'state': node.provision_state}) + "current state is %(state)s" + % {'node': node.id, 'state': node.provision_state} + ) - node = self.baremetal.set_node_provision_state(node, 'inspect', - wait=True, - timeout=timeout) + node = self.baremetal.set_node_provision_state( + node, 'inspect', wait=True, timeout=timeout + ) if return_to_available: - node = self.baremetal.set_node_provision_state(node, 'provide', - wait=True, - timeout=timeout) + node = self.baremetal.set_node_provision_state( + node, 'provide', wait=True, timeout=timeout + ) return node @@ -170,19 +173,27 @@ class BaremetalCloudMixin: try: yield except Exception as exc: - self.log.debug("cleaning up node %s because of an error: %s", - node.id, exc) + self.log.debug( + "cleaning up node %s because of an error: %s", node.id, exc + ) tb = sys.exc_info()[2] try: self.baremetal.delete_node(node) except Exception: - self.log.debug("could not remove node %s", node.id, - exc_info=True) + self.log.debug( + "could not remove node %s", node.id, exc_info=True + ) raise exc.with_traceback(tb) - def register_machine(self, nics, wait=False, timeout=3600, - lock_timeout=600, provision_state='available', - **kwargs): + def register_machine( + self, + nics, + wait=False, + timeout=3600, + lock_timeout=600, + provision_state='available', + **kwargs + ): """Register Baremetal with Ironic Allows for the registration of Baremetal nodes with Ironic @@ -233,9 +244,10 @@ class BaremetalCloudMixin: :returns: Current state of the node. """ if provision_state not in ('enroll', 'manageable', 'available'): - raise ValueError('Initial provision state must be enroll, ' - 'manageable or available, got %s' - % provision_state) + raise ValueError( + 'Initial provision state must be enroll, ' + 'manageable or available, got %s' % provision_state + ) # Available is tricky: it cannot be directly requested on newer API # versions, we need to go through cleaning. But we cannot go through @@ -246,19 +258,24 @@ class BaremetalCloudMixin: with self._delete_node_on_error(machine): # Making a node at least manageable - if (machine.provision_state == 'enroll' - and provision_state != 'enroll'): + if ( + machine.provision_state == 'enroll' + and provision_state != 'enroll' + ): machine = self.baremetal.set_node_provision_state( - machine, 'manage', wait=True, timeout=timeout) + machine, 'manage', wait=True, timeout=timeout + ) machine = self.baremetal.wait_for_node_reservation( - machine, timeout=lock_timeout) + machine, timeout=lock_timeout + ) # Create NICs before trying to run cleaning created_nics = [] try: for port in _normalize_port_list(nics): - nic = self.baremetal.create_port(node_id=machine.id, - **port) + nic = self.baremetal.create_port( + node_id=machine.id, **port + ) created_nics.append(nic.id) except Exception: @@ -269,10 +286,13 @@ class BaremetalCloudMixin: pass raise - if (machine.provision_state != 'available' - and provision_state == 'available'): + if ( + machine.provision_state != 'available' + and provision_state == 'available' + ): machine = self.baremetal.set_node_provision_state( - machine, 'provide', wait=wait, timeout=timeout) + machine, 'provide', wait=wait, timeout=timeout + ) return machine @@ -295,15 +315,18 @@ class BaremetalCloudMixin: :raises: OpenStackCloudException on operation failure. """ if wait is not None: - warnings.warn("wait argument is deprecated and has no effect", - DeprecationWarning) + warnings.warn( + "wait argument is deprecated and has no effect", + DeprecationWarning, + ) machine = self.get_machine(uuid) invalid_states = ['active', 'cleaning', 'clean wait', 'clean failed'] if machine['provision_state'] in invalid_states: raise exc.OpenStackCloudException( "Error unregistering node '%s' due to current provision " - "state '%s'" % (uuid, machine['provision_state'])) + "state '%s'" % (uuid, machine['provision_state']) + ) # NOTE(TheJulia) There is a high possibility of a lock being present # if the machine was just moved through the state machine. This was @@ -314,7 +337,8 @@ class BaremetalCloudMixin: except exc.OpenStackCloudException as e: raise exc.OpenStackCloudException( "Error unregistering node '%s': Exception occured while" - " waiting to be able to proceed: %s" % (machine['uuid'], e)) + " waiting to be able to proceed: %s" % (machine['uuid'], e) + ) for nic in _normalize_port_list(nics): try: @@ -381,32 +405,28 @@ class BaremetalCloudMixin: machine = self.get_machine(name_or_id) if not machine: raise exc.OpenStackCloudException( - "Machine update failed to find Machine: %s. " % name_or_id) + "Machine update failed to find Machine: %s. " % name_or_id + ) new_config = dict(machine._to_munch(), **attrs) try: patch = jsonpatch.JsonPatch.from_diff( - machine._to_munch(), - new_config) + machine._to_munch(), new_config + ) except Exception as e: raise exc.OpenStackCloudException( "Machine update failed - Error generating JSON patch object " "for submission to the API. Machine: %s Error: %s" - % (name_or_id, e)) + % (name_or_id, e) + ) if not patch: - return dict( - node=machine, - changes=None - ) + return dict(node=machine, changes=None) change_list = [change['path'] for change in patch] node = self.baremetal.update_node(machine, **attrs) - return dict( - node=node, - changes=change_list - ) + return dict(node=node, changes=change_list) def attach_port_to_machine(self, name_or_id, port_name_or_id): """Attach a virtual port to the bare metal machine. @@ -459,16 +479,16 @@ class BaremetalCloudMixin: self.baremetal.validate_node(name_or_id, required=ifaces) def validate_node(self, uuid): - warnings.warn('validate_node is deprecated, please use ' - 'validate_machine instead', DeprecationWarning) + warnings.warn( + 'validate_node is deprecated, please use ' + 'validate_machine instead', + DeprecationWarning, + ) self.baremetal.validate_node(uuid) - def node_set_provision_state(self, - name_or_id, - state, - configdrive=None, - wait=False, - timeout=3600): + def node_set_provision_state( + self, name_or_id, state, configdrive=None, wait=False, timeout=3600 + ): """Set Node Provision State Enables a user to provision a Machine and optionally define a @@ -495,15 +515,17 @@ class BaremetalCloudMixin: :rtype: :class:`~openstack.baremetal.v1.node.Node`. """ node = self.baremetal.set_node_provision_state( - name_or_id, target=state, config_drive=configdrive, - wait=wait, timeout=timeout) + name_or_id, + target=state, + config_drive=configdrive, + wait=wait, + timeout=timeout, + ) return node def set_machine_maintenance_state( - self, - name_or_id, - state=True, - reason=None): + self, name_or_id, state=True, reason=None + ): """Set Baremetal Machine Maintenance State Sets Baremetal maintenance state and maintenance reason. @@ -587,28 +609,33 @@ class BaremetalCloudMixin: """ self.baremetal.set_node_power_state(name_or_id, 'rebooting') - def activate_node(self, uuid, configdrive=None, - wait=False, timeout=1200): + def activate_node(self, uuid, configdrive=None, wait=False, timeout=1200): self.node_set_provision_state( - uuid, 'active', configdrive, wait=wait, timeout=timeout) + uuid, 'active', configdrive, wait=wait, timeout=timeout + ) - def deactivate_node(self, uuid, wait=False, - timeout=1200): + def deactivate_node(self, uuid, wait=False, timeout=1200): self.node_set_provision_state( - uuid, 'deleted', wait=wait, timeout=timeout) + uuid, 'deleted', wait=wait, timeout=timeout + ) def set_node_instance_info(self, uuid, patch): - warnings.warn("The set_node_instance_info call is deprecated, " - "use patch_machine or update_machine instead", - DeprecationWarning) + warnings.warn( + "The set_node_instance_info call is deprecated, " + "use patch_machine or update_machine instead", + DeprecationWarning, + ) return self.patch_machine(uuid, patch) def purge_node_instance_info(self, uuid): - warnings.warn("The purge_node_instance_info call is deprecated, " - "use patch_machine or update_machine instead", - DeprecationWarning) - return self.patch_machine(uuid, - dict(path='/instance_info', op='remove')) + warnings.warn( + "The purge_node_instance_info call is deprecated, " + "use patch_machine or update_machine instead", + DeprecationWarning, + ) + return self.patch_machine( + uuid, dict(path='/instance_info', op='remove') + ) def wait_for_baremetal_node_lock(self, node, timeout=30): """Wait for a baremetal node to have no lock. @@ -618,7 +645,10 @@ class BaremetalCloudMixin: :raises: OpenStackCloudException upon client failure. :returns: None """ - warnings.warn("The wait_for_baremetal_node_lock call is deprecated " - "in favor of wait_for_node_reservation on the baremetal " - "proxy", DeprecationWarning) + warnings.warn( + "The wait_for_baremetal_node_lock call is deprecated " + "in favor of wait_for_node_reservation on the baremetal " + "proxy", + DeprecationWarning, + ) self.baremetal.wait_for_node_reservation(node, timeout) diff --git a/openstack/cloud/_block_storage.py b/openstack/cloud/_block_storage.py index 1ab4d3fac..c63ab716b 100644 --- a/openstack/cloud/_block_storage.py +++ b/openstack/cloud/_block_storage.py @@ -127,8 +127,7 @@ class BlockStorageCloudMixin: :returns: A volume ``Type`` object if found, else None. """ - return _utils._get_entity( - self, 'volume_type', name_or_id, filters) + return _utils._get_entity(self, 'volume_type', name_or_id, filters) def create_volume( self, @@ -162,7 +161,9 @@ class BlockStorageCloudMixin: raise exc.OpenStackCloudException( "Image {image} was requested as the basis for a new" " volume, but was not found on the cloud".format( - image=image)) + image=image + ) + ) kwargs['imageRef'] = image_obj['id'] kwargs = self._get_volume_kwargs(kwargs) kwargs['size'] = size @@ -193,10 +194,10 @@ class BlockStorageCloudMixin: volume = self.get_volume(name_or_id) if not volume: raise exc.OpenStackCloudException( - "Volume %s not found." % name_or_id) + "Volume %s not found." % name_or_id + ) - volume = self.block_storage.update_volume( - volume, **kwargs) + volume = self.block_storage.update_volume(volume, **kwargs) self.list_volumes.invalidate(self) @@ -219,7 +220,9 @@ class BlockStorageCloudMixin: if not volume: raise exc.OpenStackCloudException( "Volume {name_or_id} does not exist".format( - name_or_id=name_or_id)) + name_or_id=name_or_id + ) + ) self.block_storage.set_volume_bootable_status(volume, bootable) @@ -249,7 +252,8 @@ class BlockStorageCloudMixin: self.log.debug( "Volume %(name_or_id)s does not exist", {'name_or_id': name_or_id}, - exc_info=True) + exc_info=True, + ) return False try: self.block_storage.delete_volume(volume, force=force) @@ -297,10 +301,12 @@ class BlockStorageCloudMixin: project_id = proj.id params['tenant_id'] = project_id error_msg = "{msg} for the project: {project} ".format( - msg=error_msg, project=name_or_id) + msg=error_msg, project=name_or_id + ) data = proxy._json_response( - self.block_storage.get('/limits', params=params)) + self.block_storage.get('/limits', params=params) + ) limits = self._get_and_munchify('limits', data) return limits @@ -413,22 +419,23 @@ class BlockStorageCloudMixin: # If we got volume as dict we need to re-fetch it to be able to # use wait_for_status. volume = self.block_storage.get_volume(volume['id']) - self.block_storage.wait_for_status( - volume, 'in-use', wait=timeout) + self.block_storage.wait_for_status(volume, 'in-use', wait=timeout) return attachment def _get_volume_kwargs(self, kwargs): name = kwargs.pop('name', kwargs.pop('display_name', None)) - description = kwargs.pop('description', - kwargs.pop('display_description', None)) + description = kwargs.pop( + 'description', kwargs.pop('display_description', None) + ) if name: kwargs['name'] = name if description: kwargs['description'] = description return kwargs - @_utils.valid_kwargs('name', 'display_name', - 'description', 'display_description') + @_utils.valid_kwargs( + 'name', 'display_name', 'description', 'display_description' + ) def create_volume_snapshot( self, volume_id, @@ -459,7 +466,8 @@ class BlockStorageCloudMixin: snapshot = self.block_storage.create_snapshot(**payload) if wait: snapshot = self.block_storage.wait_for_status( - snapshot, wait=timeout) + snapshot, wait=timeout + ) return snapshot @@ -499,8 +507,7 @@ class BlockStorageCloudMixin: :returns: A volume ``Snapshot`` object if found, else None. """ - return _utils._get_entity(self, 'volume_snapshot', name_or_id, - filters) + return _utils._get_entity(self, 'volume_snapshot', name_or_id, filters) def create_volume_backup( self, @@ -572,8 +579,7 @@ class BlockStorageCloudMixin: :returns: A volume ``Backup`` object if found, else None. """ - return _utils._get_entity(self, 'volume_backup', name_or_id, - filters) + return _utils._get_entity(self, 'volume_backup', name_or_id, filters) def list_volume_snapshots(self, detailed=True, filters=None): """List all volume snapshots. @@ -615,8 +621,9 @@ class BlockStorageCloudMixin: return list(self.block_storage.backups(details=detailed, **filters)) - def delete_volume_backup(self, name_or_id=None, force=False, wait=False, - timeout=None): + def delete_volume_backup( + self, name_or_id=None, force=False, wait=False, timeout=None + ): """Delete a volume backup. :param name_or_id: Name or unique ID of the volume backup. @@ -635,7 +642,8 @@ class BlockStorageCloudMixin: return False self.block_storage.delete_backup( - volume_backup, ignore_missing=False, force=force) + volume_backup, ignore_missing=False, force=force + ) if wait: self.block_storage.wait_for_delete(volume_backup, wait=timeout) @@ -663,7 +671,8 @@ class BlockStorageCloudMixin: return False self.block_storage.delete_snapshot( - volumesnapshot, ignore_missing=False) + volumesnapshot, ignore_missing=False + ) if wait: self.block_storage.wait_for_delete(volumesnapshot, wait=timeout) @@ -695,8 +704,7 @@ class BlockStorageCloudMixin: :returns: A list of volume ``Volume`` objects, if any are found. """ volumes = self.list_volumes() - return _utils._filter_list( - volumes, name_or_id, filters) + return _utils._filter_list(volumes, name_or_id, filters) def search_volume_snapshots(self, name_or_id=None, filters=None): """Search for one or more volume snapshots. @@ -723,8 +731,7 @@ class BlockStorageCloudMixin: :returns: A list of volume ``Snapshot`` objects, if any are found. """ volumesnapshots = self.list_volume_snapshots() - return _utils._filter_list( - volumesnapshots, name_or_id, filters) + return _utils._filter_list(volumesnapshots, name_or_id, filters) def search_volume_backups(self, name_or_id=None, filters=None): """Search for one or more volume backups. @@ -751,8 +758,7 @@ class BlockStorageCloudMixin: :returns: A list of volume ``Backup`` objects, if any are found. """ volume_backups = self.list_volume_backups() - return _utils._filter_list( - volume_backups, name_or_id, filters) + return _utils._filter_list(volume_backups, name_or_id, filters) # TODO(stephenfin): Remove 'get_extra' in a future major version def search_volume_types( @@ -797,7 +803,8 @@ class BlockStorageCloudMixin: volume_type = self.get_volume_type(name_or_id) if not volume_type: raise exc.OpenStackCloudException( - "VolumeType not found: %s" % name_or_id) + "VolumeType not found: %s" % name_or_id + ) return self.block_storage.get_type_access(volume_type) @@ -814,7 +821,8 @@ class BlockStorageCloudMixin: volume_type = self.get_volume_type(name_or_id) if not volume_type: raise exc.OpenStackCloudException( - "VolumeType not found: %s" % name_or_id) + "VolumeType not found: %s" % name_or_id + ) self.block_storage.add_type_access(volume_type, project_id) @@ -829,7 +837,8 @@ class BlockStorageCloudMixin: volume_type = self.get_volume_type(name_or_id) if not volume_type: raise exc.OpenStackCloudException( - "VolumeType not found: %s" % name_or_id) + "VolumeType not found: %s" % name_or_id + ) self.block_storage.remove_type_access(volume_type, project_id) def set_volume_quotas(self, name_or_id, **kwargs): @@ -842,12 +851,11 @@ class BlockStorageCloudMixin: quota does not exist. """ - proj = self.identity.find_project( - name_or_id, ignore_missing=False) + proj = self.identity.find_project(name_or_id, ignore_missing=False) self.block_storage.update_quota_set( - _qs.QuotaSet(project_id=proj.id), - **kwargs) + _qs.QuotaSet(project_id=proj.id), **kwargs + ) def get_volume_quotas(self, name_or_id): """Get volume quotas for a project diff --git a/openstack/cloud/_clustering.py b/openstack/cloud/_clustering.py index c04367693..73851f5e4 100644 --- a/openstack/cloud/_clustering.py +++ b/openstack/cloud/_clustering.py @@ -23,10 +23,12 @@ class ClusteringCloudMixin: def _clustering_client(self): if 'clustering' not in self._raw_clients: clustering_client = self._get_versioned_client( - 'clustering', min_version=1, max_version='1.latest') + 'clustering', min_version=1, max_version='1.latest' + ) self._raw_clients['clustering'] = clustering_client return self._raw_clients['clustering'] + # NOTE(gtema): work on getting rid of direct API calls showed that this # implementation never worked properly and tests in reality verifying wrong # things. Unless someone is really interested in this piece of code this will diff --git a/openstack/cloud/_coe.py b/openstack/cloud/_coe.py index 6533f3f36..cbbe63187 100644 --- a/openstack/cloud/_coe.py +++ b/openstack/cloud/_coe.py @@ -19,7 +19,6 @@ from openstack.cloud import exc class CoeCloudMixin: - @_utils.cache_on_arguments() def list_coe_clusters(self): """List COE (Container Orchestration Engine) cluster. @@ -72,7 +71,10 @@ class CoeCloudMixin: return _utils._get_entity(self, 'coe_cluster', name_or_id, filters) def create_coe_cluster( - self, name, cluster_template_id, **kwargs, + self, + name, + cluster_template_id, + **kwargs, ): """Create a COE cluster based on given cluster template. @@ -133,11 +135,11 @@ class CoeCloudMixin: cluster = self.get_coe_cluster(name_or_id) if not cluster: raise exc.OpenStackCloudException( - "COE cluster %s not found." % name_or_id) + "COE cluster %s not found." % name_or_id + ) cluster = self.container_infrastructure_management.update_cluster( - cluster, - **kwargs + cluster, **kwargs ) return cluster @@ -149,8 +151,11 @@ class CoeCloudMixin: :returns: Details about the CA certificate for the given cluster. """ - return self.container_infrastructure_management\ - .get_cluster_certificate(cluster_id) + return ( + self.container_infrastructure_management.get_cluster_certificate( + cluster_id + ) + ) def sign_coe_cluster_certificate(self, cluster_id, csr): """Sign client key and generate the CA certificate for a cluster @@ -164,10 +169,9 @@ class CoeCloudMixin: :raises: OpenStackCloudException on operation error. """ - return self.container_infrastructure_management\ - .create_cluster_certificate( - cluster_uuid=cluster_id, - csr=csr) + return self.container_infrastructure_management.create_cluster_certificate( # noqa: E501 + cluster_uuid=cluster_id, csr=csr + ) @_utils.cache_on_arguments() def list_cluster_templates(self, detail=False): @@ -182,10 +186,12 @@ class CoeCloudMixin: the OpenStack API call. """ return list( - self.container_infrastructure_management.cluster_templates()) + self.container_infrastructure_management.cluster_templates() + ) def search_cluster_templates( - self, name_or_id=None, filters=None, detail=False): + self, name_or_id=None, filters=None, detail=False + ): """Search cluster templates. :param name_or_id: cluster template name or ID. @@ -199,8 +205,7 @@ class CoeCloudMixin: the OpenStack API call. """ cluster_templates = self.list_cluster_templates(detail=detail) - return _utils._filter_list( - cluster_templates, name_or_id, filters) + return _utils._filter_list(cluster_templates, name_or_id, filters) def get_cluster_template(self, name_or_id, filters=None, detail=False): """Get a cluster template by name or ID. @@ -225,11 +230,16 @@ class CoeCloudMixin: cluster template is found. """ return _utils._get_entity( - self, 'cluster_template', name_or_id, - filters=filters, detail=detail) + self, + 'cluster_template', + name_or_id, + filters=filters, + detail=detail, + ) def create_cluster_template( - self, name, image_id=None, keypair_id=None, coe=None, **kwargs): + self, name, image_id=None, keypair_id=None, coe=None, **kwargs + ): """Create a cluster template. :param string name: Name of the cluster template. @@ -243,14 +253,15 @@ class CoeCloudMixin: :raises: ``OpenStackCloudException`` if something goes wrong during the OpenStack API call """ - cluster_template = self.container_infrastructure_management \ - .create_cluster_template( + cluster_template = ( + self.container_infrastructure_management.create_cluster_template( name=name, image_id=image_id, keypair_id=keypair_id, coe=coe, **kwargs, ) + ) return cluster_template @@ -270,11 +281,13 @@ class CoeCloudMixin: self.log.debug( "Cluster template %(name_or_id)s does not exist", {'name_or_id': name_or_id}, - exc_info=True) + exc_info=True, + ) return False self.container_infrastructure_management.delete_cluster_template( - cluster_template) + cluster_template + ) return True def update_cluster_template(self, name_or_id, **kwargs): @@ -289,14 +302,15 @@ class CoeCloudMixin: cluster_template = self.get_cluster_template(name_or_id) if not cluster_template: raise exc.OpenStackCloudException( - "Cluster template %s not found." % name_or_id) - - cluster_template = self.container_infrastructure_management \ - .update_cluster_template( - cluster_template, - **kwargs + "Cluster template %s not found." % name_or_id ) + cluster_template = ( + self.container_infrastructure_management.update_cluster_template( + cluster_template, **kwargs + ) + ) + return cluster_template def list_magnum_services(self): diff --git a/openstack/cloud/_compute.py b/openstack/cloud/_compute.py index 89e329816..b2ab77850 100644 --- a/openstack/cloud/_compute.py +++ b/openstack/cloud/_compute.py @@ -114,12 +114,15 @@ class ComputeCloudMixin: """ flavors = self.list_flavors(get_extra=get_extra) for flavor in sorted(flavors, key=operator.itemgetter('ram')): - if (flavor['ram'] >= ram - and (not include or include in flavor['name'])): + if flavor['ram'] >= ram and ( + not include or include in flavor['name'] + ): return flavor raise exc.OpenStackCloudException( "Could not find a flavor with {ram} and '{include}'".format( - ram=ram, include=include)) + ram=ram, include=include + ) + ) @_utils.cache_on_arguments() def _nova_extensions(self): @@ -155,8 +158,12 @@ class ComputeCloudMixin: return _utils._filter_list(flavors, name_or_id, filters) def search_servers( - self, name_or_id=None, filters=None, detailed=False, - all_projects=False, bare=False, + self, + name_or_id=None, + filters=None, + detailed=False, + all_projects=False, + bare=False, ): """Search servers. @@ -169,7 +176,8 @@ class ComputeCloudMixin: criteria. """ servers = self.list_servers( - detailed=detailed, all_projects=all_projects, bare=bare) + detailed=detailed, all_projects=all_projects, bare=bare + ) return _utils._filter_list(servers, name_or_id, filters) def search_server_groups(self, name_or_id=None, filters=None): @@ -213,8 +221,8 @@ class ComputeCloudMixin: return ret except exceptions.SDKException: self.log.debug( - "Availability zone list could not be fetched", - exc_info=True) + "Availability zone list could not be fetched", exc_info=True + ) return [] @_utils.cache_on_arguments() @@ -226,8 +234,9 @@ class ComputeCloudMixin: clouds.yaml by setting openstack.cloud.get_extra_specs to False. :returns: A list of compute ``Flavor`` objects. """ - return list(self.compute.flavors( - details=True, get_extra_specs=get_extra)) + return list( + self.compute.flavors(details=True, get_extra_specs=get_extra) + ) def list_server_security_groups(self, server): """List all security groups associated with the given server. @@ -268,8 +277,9 @@ class ComputeCloudMixin: sg = self.get_security_group(sg) if sg is None: - self.log.debug('Security group %s not found for adding', - sg) + self.log.debug( + 'Security group %s not found for adding', sg + ) return None, None @@ -288,7 +298,8 @@ class ComputeCloudMixin: :raises: ``OpenStackCloudException``, on operation error. """ server, security_groups = self._get_server_security_groups( - server, security_groups) + server, security_groups + ) if not (server and security_groups): return False @@ -310,7 +321,8 @@ class ComputeCloudMixin: :raises: ``OpenStackCloudException``, on operation error. """ server, security_groups = self._get_server_security_groups( - server, security_groups) + server, security_groups + ) if not (server and security_groups): return False @@ -327,7 +339,10 @@ class ComputeCloudMixin: # error? Nova returns ok if you try to add a group twice. self.log.debug( "The security group %s was not present on server %s so " - "no action was performed", sg.name, server.name) + "no action was performed", + sg.name, + server.name, + ) ret = False return ret @@ -377,7 +392,8 @@ class ComputeCloudMixin: self._servers = self._list_servers( detailed=detailed, all_projects=all_projects, - bare=bare) + bare=bare, + ) self._servers_time = time.time() finally: self._servers_lock.release() @@ -386,14 +402,15 @@ class ComputeCloudMixin: # list from the cloud, we still return a filtered list. return _utils._filter_list(self._servers, None, filters) - def _list_servers(self, detailed=False, all_projects=False, bare=False, - filters=None): + def _list_servers( + self, detailed=False, all_projects=False, bare=False, filters=None + ): filters = filters or {} return [ self._expand_server(server, detailed, bare) for server in self.compute.servers( - all_projects=all_projects, - **filters) + all_projects=all_projects, **filters + ) ] def list_server_groups(self): @@ -472,12 +489,15 @@ class ComputeCloudMixin: if not filters: filters = {} flavor = self.compute.find_flavor( - name_or_id, get_extra_specs=get_extra, - ignore_missing=True, **filters) + name_or_id, + get_extra_specs=get_extra, + ignore_missing=True, + **filters, + ) return flavor def get_flavor_by_id(self, id, get_extra=False): - """ Get a flavor by ID + """Get a flavor by ID :param id: ID of the flavor. :param get_extra: Whether or not the list_flavors call should get the @@ -505,7 +525,8 @@ class ComputeCloudMixin: if not server: raise exc.OpenStackCloudException( - "Console log requested for invalid server") + "Console log requested for invalid server" + ) try: return self._get_server_console_output(server['id'], length) @@ -514,8 +535,7 @@ class ComputeCloudMixin: def _get_server_console_output(self, server_id, length=None): output = self.compute.get_server_console_output( - server=server_id, - length=length + server=server_id, length=length ) if 'output' in output: return output['output'] @@ -555,9 +575,12 @@ class ComputeCloudMixin: the current auth scoped project. :returns: A compute ``Server`` object if found, else None. """ - searchfunc = functools.partial(self.search_servers, - detailed=detailed, bare=True, - all_projects=all_projects) + searchfunc = functools.partial( + self.search_servers, + detailed=detailed, + bare=True, + all_projects=all_projects, + ) server = _utils._get_entity(self, searchfunc, name_or_id, filters) return self._expand_server(server, detailed, bare) @@ -600,8 +623,7 @@ class ComputeCloudMixin: :returns: A compute ``ServerGroup`` object if found, else None. """ - return _utils._get_entity(self, 'server_group', name_or_id, - filters) + return _utils._get_entity(self, 'server_group', name_or_id, filters) def create_keypair(self, name, public_key=None): """Create a new keypair. @@ -664,10 +686,12 @@ class ComputeCloudMixin: if not server_obj: raise exc.OpenStackCloudException( "Server {server} could not be found and therefore" - " could not be snapshotted.".format(server=server)) + " could not be snapshotted.".format(server=server) + ) server = server_obj image = self.compute.create_server_image( - server, name=name, metadata=metadata, wait=wait, timeout=timeout) + server, name=name, metadata=metadata, wait=wait, timeout=timeout + ) return image def get_server_id(self, name_or_id): @@ -709,12 +733,25 @@ class ComputeCloudMixin: return dict(server_vars=server_vars, groups=groups) @_utils.valid_kwargs( - 'meta', 'files', 'userdata', 'description', - 'reservation_id', 'return_raw', 'min_count', - 'max_count', 'security_groups', 'key_name', - 'availability_zone', 'block_device_mapping', - 'block_device_mapping_v2', 'nics', 'scheduler_hints', - 'config_drive', 'admin_pass', 'disk_config') + 'meta', + 'files', + 'userdata', + 'description', + 'reservation_id', + 'return_raw', + 'min_count', + 'max_count', + 'security_groups', + 'key_name', + 'availability_zone', + 'block_device_mapping', + 'block_device_mapping_v2', + 'nics', + 'scheduler_hints', + 'config_drive', + 'admin_pass', + 'disk_config', + ) def create_server( self, name, @@ -818,10 +855,12 @@ class ComputeCloudMixin: # after image in the argument list. Doh. if not flavor: raise TypeError( - "create_server() missing 1 required argument: 'flavor'") + "create_server() missing 1 required argument: 'flavor'" + ) if not image and not boot_volume: raise TypeError( - "create_server() requires either 'image' or 'boot_volume'") + "create_server() requires either 'image' or 'boot_volume'" + ) # TODO(mordred) Add support for description starting in 2.19 security_groups = kwargs.get('security_groups', []) @@ -836,11 +875,12 @@ class ComputeCloudMixin: if user_data: kwargs['user_data'] = self._encode_server_userdata(user_data) for (desired, given) in ( - ('OS-DCF:diskConfig', 'disk_config'), - ('config_drive', 'config_drive'), - ('key_name', 'key_name'), - ('metadata', 'meta'), - ('adminPass', 'admin_pass')): + ('OS-DCF:diskConfig', 'disk_config'), + ('config_drive', 'config_drive'), + ('key_name', 'key_name'), + ('metadata', 'meta'), + ('adminPass', 'admin_pass'), + ): value = kwargs.pop(given, None) if value: kwargs[desired] = value @@ -850,7 +890,8 @@ class ComputeCloudMixin: if not group_obj: raise exc.OpenStackCloudException( "Server Group {group} was requested but was not found" - " on the cloud".format(group=group)) + " on the cloud".format(group=group) + ) if 'scheduler_hints' not in kwargs: kwargs['scheduler_hints'] = {} kwargs['scheduler_hints']['group'] = group_obj['id'] @@ -865,7 +906,8 @@ class ComputeCloudMixin: else: raise exc.OpenStackCloudException( 'nics parameter to create_server takes a list of dicts.' - ' Got: {nics}'.format(nics=kwargs['nics'])) + ' Got: {nics}'.format(nics=kwargs['nics']) + ) if network and ('nics' not in kwargs or not kwargs['nics']): nics = [] @@ -881,7 +923,10 @@ class ComputeCloudMixin: 'Network {network} is not a valid network in' ' {cloud}:{region}'.format( network=network, - cloud=self.name, region=self._compute_region)) + cloud=self.name, + region=self._compute_region, + ) + ) nics.append({'net-id': network_obj['id']}) kwargs['nics'] = nics @@ -904,14 +949,17 @@ class ComputeCloudMixin: if not nic_net: raise exc.OpenStackCloudException( "Requested network {net} could not be found.".format( - net=net_name)) + net=net_name + ) + ) net['uuid'] = nic_net['id'] for ip_key in ('v4-fixed-ip', 'v6-fixed-ip', 'fixed_ip'): fixed_ip = nic.pop(ip_key, None) if fixed_ip and net.get('fixed_ip'): raise exc.OpenStackCloudException( "Only one of v4-fixed-ip, v6-fixed-ip or fixed_ip" - " may be given") + " may be given" + ) if fixed_ip: net['fixed_ip'] = fixed_ip for key in ('port', 'port-id'): @@ -920,13 +968,13 @@ class ComputeCloudMixin: # A tag supported only in server microversion 2.32-2.36 or >= 2.42 # Bumping the version to 2.42 to support the 'tag' implementation if 'tag' in nic: - utils.require_microversion( - self.compute, '2.42') + utils.require_microversion(self.compute, '2.42') net['tag'] = nic.pop('tag') if nic: raise exc.OpenStackCloudException( "Additional unsupported keys given for server network" - " creation: {keys}".format(keys=nic.keys())) + " creation: {keys}".format(keys=nic.keys()) + ) networks.append(net) if networks: kwargs['networks'] = networks @@ -954,10 +1002,14 @@ class ComputeCloudMixin: boot_volume = root_volume kwargs = self._get_boot_from_volume_kwargs( - image=image, boot_from_volume=boot_from_volume, - boot_volume=boot_volume, volume_size=str(volume_size), + image=image, + boot_from_volume=boot_from_volume, + boot_volume=boot_volume, + volume_size=str(volume_size), terminate_volume=terminate_volume, - volumes=volumes, kwargs=kwargs) + volumes=volumes, + kwargs=kwargs, + ) kwargs['name'] = name @@ -977,14 +1029,18 @@ class ComputeCloudMixin: server = self.compute.get_server(server.id) if server.status == 'ERROR': raise exc.OpenStackCloudCreateException( - resource='server', resource_id=server.id) + resource='server', resource_id=server.id + ) server = meta.add_server_interfaces(self, server) else: server = self.wait_for_server( server, - auto_ip=auto_ip, ips=ips, ip_pool=ip_pool, - reuse=reuse_ips, timeout=timeout, + auto_ip=auto_ip, + ips=ips, + ip_pool=ip_pool, + reuse=reuse_ips, + timeout=timeout, nat_destination=nat_destination, ) @@ -992,8 +1048,15 @@ class ComputeCloudMixin: return server def _get_boot_from_volume_kwargs( - self, image, boot_from_volume, boot_volume, volume_size, - terminate_volume, volumes, kwargs): + self, + image, + boot_from_volume, + boot_volume, + volume_size, + terminate_volume, + volumes, + kwargs, + ): """Return block device mappings :param image: Image dict, name or id to boot with. @@ -1015,7 +1078,10 @@ class ComputeCloudMixin: 'Volume {boot_volume} is not a valid volume' ' in {cloud}:{region}'.format( boot_volume=boot_volume, - cloud=self.name, region=self._compute_region)) + cloud=self.name, + region=self._compute_region, + ) + ) block_mapping = { 'boot_index': '0', 'delete_on_termination': terminate_volume, @@ -1036,7 +1102,10 @@ class ComputeCloudMixin: 'Image {image} is not a valid image in' ' {cloud}:{region}'.format( image=image, - cloud=self.name, region=self._compute_region)) + cloud=self.name, + region=self._compute_region, + ) + ) block_mapping = { 'boot_index': '0', @@ -1066,7 +1135,10 @@ class ComputeCloudMixin: 'Volume {volume} is not a valid volume' ' in {cloud}:{region}'.format( volume=volume, - cloud=self.name, region=self._compute_region)) + cloud=self.name, + region=self._compute_region, + ) + ) block_mapping = { 'boot_index': '-1', 'delete_on_termination': False, @@ -1080,8 +1152,15 @@ class ComputeCloudMixin: return kwargs def wait_for_server( - self, server, auto_ip=True, ips=None, ip_pool=None, - reuse=True, timeout=180, nat_destination=None): + self, + server, + auto_ip=True, + ips=None, + ip_pool=None, + reuse=True, + timeout=180, + nat_destination=None, + ): """ Wait for a server to reach ACTIVE status. """ @@ -1094,11 +1173,12 @@ class ComputeCloudMixin: # There is no point in iterating faster than the list_servers cache for count in utils.iterate_timeout( - timeout, - timeout_message, - # if _SERVER_AGE is 0 we still want to wait a bit - # to be friendly with the server. - wait=self._SERVER_AGE or 2): + timeout, + timeout_message, + # if _SERVER_AGE is 0 we still want to wait a bit + # to be friendly with the server. + wait=self._SERVER_AGE or 2, + ): try: # Use the get_server call so that the list_servers # cache can be leveraged @@ -1116,10 +1196,15 @@ class ComputeCloudMixin: raise exc.OpenStackCloudTimeout(timeout_message) server = self.get_active_server( - server=server, reuse=reuse, - auto_ip=auto_ip, ips=ips, ip_pool=ip_pool, - wait=True, timeout=remaining_timeout, - nat_destination=nat_destination) + server=server, + reuse=reuse, + auto_ip=auto_ip, + ips=ips, + ip_pool=ip_pool, + wait=True, + timeout=remaining_timeout, + nat_destination=nat_destination, + ) if server is not None and server['status'] == 'ACTIVE': return server @@ -1136,43 +1221,58 @@ class ComputeCloudMixin: nat_destination=None, ): if server['status'] == 'ERROR': - if ('fault' in server and server['fault'] is not None - and 'message' in server['fault']): + if ( + 'fault' in server + and server['fault'] is not None + and 'message' in server['fault'] + ): raise exc.OpenStackCloudException( "Error in creating the server." " Compute service reports fault: {reason}".format( - reason=server['fault']['message']), - extra_data=dict(server=server)) + reason=server['fault']['message'] + ), + extra_data=dict(server=server), + ) raise exc.OpenStackCloudException( "Error in creating the server" " (no further information available)", - extra_data=dict(server=server)) + extra_data=dict(server=server), + ) if server['status'] == 'ACTIVE': if 'addresses' in server and server['addresses']: return self.add_ips_to_server( - server, auto_ip, ips, ip_pool, reuse=reuse, + server, + auto_ip, + ips, + ip_pool, + reuse=reuse, nat_destination=nat_destination, - wait=wait, timeout=timeout) + wait=wait, + timeout=timeout, + ) self.log.debug( 'Server %(server)s reached ACTIVE state without' ' being allocated an IP address.' - ' Deleting server.', {'server': server['id']}) + ' Deleting server.', + {'server': server['id']}, + ) try: - self._delete_server( - server=server, wait=wait, timeout=timeout) + self._delete_server(server=server, wait=wait, timeout=timeout) except Exception as e: raise exc.OpenStackCloudException( 'Server reached ACTIVE state without being' ' allocated an IP address AND then could not' ' be deleted: {0}'.format(e), - extra_data=dict(server=server)) + extra_data=dict(server=server), + ) raise exc.OpenStackCloudException( 'Server reached ACTIVE state without being' ' allocated an IP address.', - extra_data=dict(server=server)) + extra_data=dict(server=server), + ) return None def rebuild_server( @@ -1202,17 +1302,12 @@ class ComputeCloudMixin: if admin_pass: kwargs['admin_password'] = admin_pass - server = self.compute.rebuild_server( - server_id, - **kwargs - ) + server = self.compute.rebuild_server(server_id, **kwargs) if not wait: - return self._expand_server( - server, bare=bare, detailed=detailed) + return self._expand_server(server, bare=bare, detailed=detailed) admin_pass = server.get('adminPass') or admin_pass - server = self.compute.wait_for_server( - server, wait=timeout) + server = self.compute.wait_for_server(server, wait=timeout) if server['status'] == 'ACTIVE': server.adminPass = admin_pass @@ -1231,7 +1326,8 @@ class ComputeCloudMixin: server = self.get_server(name_or_id, bare=True) if not server: raise exc.OpenStackCloudException( - 'Invalid Server {server}'.format(server=name_or_id)) + 'Invalid Server {server}'.format(server=name_or_id) + ) self.compute.set_server_metadata(server=server.id, **metadata) @@ -1248,10 +1344,12 @@ class ComputeCloudMixin: server = self.get_server(name_or_id, bare=True) if not server: raise exc.OpenStackCloudException( - 'Invalid Server {server}'.format(server=name_or_id)) + 'Invalid Server {server}'.format(server=name_or_id) + ) - self.compute.delete_server_metadata(server=server.id, - keys=metadata_keys) + self.compute.delete_server_metadata( + server=server.id, keys=metadata_keys + ) def delete_server( self, @@ -1275,8 +1373,7 @@ class ComputeCloudMixin: :raises: OpenStackCloudException on operation error. """ # If delete_ips is True, we need the server to not be bare. - server = self.compute.find_server( - name_or_id, ignore_missing=True) + server = self.compute.find_server(name_or_id, ignore_missing=True) if not server: return False @@ -1284,18 +1381,24 @@ class ComputeCloudMixin: # private method in order to avoid an unnecessary API call to get # a server we already have. return self._delete_server( - server, wait=wait, timeout=timeout, delete_ips=delete_ips, - delete_ip_retry=delete_ip_retry) + server, + wait=wait, + timeout=timeout, + delete_ips=delete_ips, + delete_ip_retry=delete_ip_retry, + ) def _delete_server_floating_ips(self, server, delete_ip_retry): # Does the server have floating ips in its # addresses dict? If not, skip this. server_floats = meta.find_nova_interfaces( - server['addresses'], ext_tag='floating') + server['addresses'], ext_tag='floating' + ) for fip in server_floats: try: - ip = self.get_floating_ip(id=None, filters={ - 'floating_ip_address': fip['addr']}) + ip = self.get_floating_ip( + id=None, filters={'floating_ip_address': fip['addr']} + ) except exc.OpenStackCloudURINotFound: # We're deleting. If it doesn't exist - awesome # NOTE(mordred) If the cloud is a nova FIP cloud but @@ -1304,19 +1407,24 @@ class ComputeCloudMixin: continue if not ip: continue - deleted = self.delete_floating_ip( - ip['id'], retry=delete_ip_retry) + deleted = self.delete_floating_ip(ip['id'], retry=delete_ip_retry) if not deleted: raise exc.OpenStackCloudException( "Tried to delete floating ip {floating_ip}" " associated with server {id} but there was" " an error deleting it. Not deleting server.".format( - floating_ip=ip['floating_ip_address'], - id=server['id'])) + floating_ip=ip['floating_ip_address'], id=server['id'] + ) + ) def _delete_server( - self, server, wait=False, timeout=180, delete_ips=False, - delete_ip_retry=1): + self, + server, + wait=False, + timeout=180, + delete_ips=False, + delete_ip_retry=1, + ): if not server: return False @@ -1324,8 +1432,7 @@ class ComputeCloudMixin: self._delete_server_floating_ips(server, delete_ip_retry) try: - self.compute.delete_server( - server) + self.compute.delete_server(server) except exceptions.ResourceNotFound: return False except Exception: @@ -1339,9 +1446,11 @@ class ComputeCloudMixin: # need to invalidate the cache. Avoid the extra API call if # caching is not enabled. reset_volume_cache = False - if (self.cache_enabled - and self.has_service('volume') - and self.get_volumes(server)): + if ( + self.cache_enabled + and self.has_service('volume') + and self.get_volumes(server) + ): reset_volume_cache = True if not isinstance(server, _server.Server): @@ -1349,8 +1458,7 @@ class ComputeCloudMixin: # If this is the case - convert it into real server to be able to # use wait_for_delete server = _server.Server(id=server['id']) - self.compute.wait_for_delete( - server, wait=timeout) + self.compute.wait_for_delete(server, wait=timeout) if reset_volume_cache: self.list_volumes.invalidate(self) @@ -1360,8 +1468,7 @@ class ComputeCloudMixin: self._servers_time = self._servers_time - self._SERVER_AGE return True - @_utils.valid_kwargs( - 'name', 'description') + @_utils.valid_kwargs('name', 'description') def update_server(self, name_or_id, detailed=False, bare=False, **kwargs): """Update a server. @@ -1377,13 +1484,9 @@ class ComputeCloudMixin: :returns: The updated compute ``Server`` object. :raises: OpenStackCloudException on operation error. """ - server = self.compute.find_server( - name_or_id, - ignore_missing=False - ) + server = self.compute.find_server(name_or_id, ignore_missing=False) - server = self.compute.update_server( - server, **kwargs) + server = self.compute.update_server(server, **kwargs) return self._expand_server(server, bare=bare, detailed=detailed) @@ -1395,16 +1498,12 @@ class ComputeCloudMixin: :returns: The created compute ``ServerGroup`` object. :raises: OpenStackCloudException on operation error. """ - sg_attrs = { - 'name': name - } + sg_attrs = {'name': name} if policies: sg_attrs['policies'] = policies if policy: sg_attrs['policy'] = policy - return self.compute.create_server_group( - **sg_attrs - ) + return self.compute.create_server_group(**sg_attrs) def delete_server_group(self, name_or_id): """Delete a server group. @@ -1415,8 +1514,9 @@ class ComputeCloudMixin: """ server_group = self.get_server_group(name_or_id) if not server_group: - self.log.debug("Server group %s not found for deleting", - name_or_id) + self.log.debug( + "Server group %s not found for deleting", name_or_id + ) return False self.compute.delete_server_group(server_group, ignore_missing=False) @@ -1477,14 +1577,14 @@ class ComputeCloudMixin: try: flavor = self.compute.find_flavor(name_or_id) if not flavor: - self.log.debug( - "Flavor %s not found for deleting", name_or_id) + self.log.debug("Flavor %s not found for deleting", name_or_id) return False self.compute.delete_flavor(flavor) return True except exceptions.SDKException: raise exceptions.OpenStackCloudException( - "Unable to delete flavor {name}".format(name=name_or_id)) + "Unable to delete flavor {name}".format(name=name_or_id) + ) def set_flavor_specs(self, flavor_id, extra_specs): """Add extra specs to a flavor @@ -1545,9 +1645,7 @@ class ComputeCloudMixin: :returns: A list of compute ``Hypervisor`` objects. """ - return list(self.compute.hypervisors( - details=True, - **filters)) + return list(self.compute.hypervisors(details=True, **filters)) def search_aggregates(self, name_or_id=None, filters=None): """Seach host aggregates. @@ -1587,8 +1685,7 @@ class ComputeCloudMixin: :returns: An aggregate dict or None if no matching aggregate is found. """ - return self.compute.find_aggregate( - name_or_id, ignore_missing=True) + return self.compute.find_aggregate(name_or_id, ignore_missing=True) def create_aggregate(self, name, availability_zone=None): """Create a new host aggregate. @@ -1599,8 +1696,7 @@ class ComputeCloudMixin: :raises: OpenStackCloudException on operation error. """ return self.compute.create_aggregate( - name=name, - availability_zone=availability_zone + name=name, availability_zone=availability_zone ) @_utils.valid_kwargs('name', 'availability_zone') @@ -1623,14 +1719,12 @@ class ComputeCloudMixin: :returns: True if delete succeeded, False otherwise. :raises: OpenStackCloudException on operation error. """ - if ( - isinstance(name_or_id, (str, bytes)) - and not name_or_id.isdigit() - ): + if isinstance(name_or_id, (str, bytes)) and not name_or_id.isdigit(): aggregate = self.get_aggregate(name_or_id) if not aggregate: self.log.debug( - "Aggregate %s not found for deleting", name_or_id) + "Aggregate %s not found for deleting", name_or_id + ) return False name_or_id = aggregate.id try: @@ -1654,7 +1748,8 @@ class ComputeCloudMixin: aggregate = self.get_aggregate(name_or_id) if not aggregate: raise exc.OpenStackCloudException( - "Host aggregate %s not found." % name_or_id) + "Host aggregate %s not found." % name_or_id + ) return self.compute.set_aggregate_metadata(aggregate, metadata) @@ -1669,7 +1764,8 @@ class ComputeCloudMixin: aggregate = self.get_aggregate(name_or_id) if not aggregate: raise exc.OpenStackCloudException( - "Host aggregate %s not found." % name_or_id) + "Host aggregate %s not found." % name_or_id + ) return self.compute.add_host_to_aggregate(aggregate, host_name) @@ -1684,12 +1780,13 @@ class ComputeCloudMixin: aggregate = self.get_aggregate(name_or_id) if not aggregate: raise exc.OpenStackCloudException( - "Host aggregate %s not found." % name_or_id) + "Host aggregate %s not found." % name_or_id + ) return self.compute.remove_host_from_aggregate(aggregate, host_name) def set_compute_quotas(self, name_or_id, **kwargs): - """ Set a quota in a project + """Set a quota in a project :param name_or_id: project name or id :param kwargs: key/value pairs of quota name and quota value @@ -1697,39 +1794,35 @@ class ComputeCloudMixin: :raises: OpenStackCloudException if the resource to set the quota does not exist. """ - proj = self.identity.find_project( - name_or_id, ignore_missing=False) + proj = self.identity.find_project(name_or_id, ignore_missing=False) kwargs['force'] = True self.compute.update_quota_set( - _qs.QuotaSet(project_id=proj.id), - **kwargs + _qs.QuotaSet(project_id=proj.id), **kwargs ) def get_compute_quotas(self, name_or_id): - """ Get quota for a project + """Get quota for a project :param name_or_id: project name or id :returns: A compute ``QuotaSet`` object if found, else None. :raises: OpenStackCloudException if it's not a valid project """ - proj = self.identity.find_project( - name_or_id, ignore_missing=False) + proj = self.identity.find_project(name_or_id, ignore_missing=False) return self.compute.get_quota_set(proj) def delete_compute_quotas(self, name_or_id): - """ Delete quota for a project + """Delete quota for a project :param name_or_id: project name or id :raises: OpenStackCloudException if it's not a valid project or the nova client call failed :returns: None """ - proj = self.identity.find_project( - name_or_id, ignore_missing=False) + proj = self.identity.find_project(name_or_id, ignore_missing=False) self.compute.revert_quota_set(proj) def get_compute_usage(self, name_or_id, start=None, end=None): - """ Get usage for a specific project + """Get usage for a specific project :param name_or_id: project name or id :param start: :class:`datetime.datetime` or string. Start date in UTC @@ -1741,6 +1834,7 @@ class ComputeCloudMixin: :returns: A :class:`~openstack.compute.v2.usage.Usage` object """ + def parse_date(date): try: return iso8601.parse_date(date) @@ -1751,8 +1845,8 @@ class ComputeCloudMixin: raise exc.OpenStackCloudException( "Date given, {date}, is invalid. Please pass in a date" " string in ISO 8601 format -" - " YYYY-MM-DDTHH:MM:SS".format( - date=date)) + " YYYY-MM-DDTHH:MM:SS".format(date=date) + ) if isinstance(start, str): start = parse_date(start) @@ -1762,7 +1856,8 @@ class ComputeCloudMixin: proj = self.get_project(name_or_id) if not proj: raise exc.OpenStackCloudException( - "project does not exist: {name}".format(name=proj.id)) + "project does not exist: {name}".format(name=proj.id) + ) return self.compute.get_usage(proj, start, end) @@ -1830,22 +1925,28 @@ class ComputeCloudMixin: project_id = server.pop('project_id', project_id) az = _pop_or_get( - server, 'OS-EXT-AZ:availability_zone', None, self.strict_mode) + server, 'OS-EXT-AZ:availability_zone', None, self.strict_mode + ) # the server resource has this already, but it's missing az info # from the resource. # TODO(mordred) create_server is still normalizing servers that aren't # from the resource layer. ret['location'] = server.pop( - 'location', self._get_current_location( - project_id=project_id, zone=az)) + 'location', + self._get_current_location(project_id=project_id, zone=az), + ) # Ensure volumes is always in the server dict, even if empty ret['volumes'] = _pop_or_get( - server, 'os-extended-volumes:volumes_attached', - [], self.strict_mode) + server, + 'os-extended-volumes:volumes_attached', + [], + self.strict_mode, + ) config_drive = server.pop( - 'has_config_drive', server.pop('config_drive', False)) + 'has_config_drive', server.pop('config_drive', False) + ) ret['has_config_drive'] = _to_bool(config_drive) host_id = server.pop('hostId', server.pop('host_id', None)) @@ -1855,24 +1956,25 @@ class ComputeCloudMixin: # Leave these in so that the general properties handling works ret['disk_config'] = _pop_or_get( - server, 'OS-DCF:diskConfig', None, self.strict_mode) + server, 'OS-DCF:diskConfig', None, self.strict_mode + ) for key in ( - 'OS-EXT-STS:power_state', - 'OS-EXT-STS:task_state', - 'OS-EXT-STS:vm_state', - 'OS-SRV-USG:launched_at', - 'OS-SRV-USG:terminated_at', - 'OS-EXT-SRV-ATTR:hypervisor_hostname', - 'OS-EXT-SRV-ATTR:instance_name', - 'OS-EXT-SRV-ATTR:user_data', - 'OS-EXT-SRV-ATTR:host', - 'OS-EXT-SRV-ATTR:hostname', - 'OS-EXT-SRV-ATTR:kernel_id', - 'OS-EXT-SRV-ATTR:launch_index', - 'OS-EXT-SRV-ATTR:ramdisk_id', - 'OS-EXT-SRV-ATTR:reservation_id', - 'OS-EXT-SRV-ATTR:root_device_name', - 'OS-SCH-HNT:scheduler_hints', + 'OS-EXT-STS:power_state', + 'OS-EXT-STS:task_state', + 'OS-EXT-STS:vm_state', + 'OS-SRV-USG:launched_at', + 'OS-SRV-USG:terminated_at', + 'OS-EXT-SRV-ATTR:hypervisor_hostname', + 'OS-EXT-SRV-ATTR:instance_name', + 'OS-EXT-SRV-ATTR:user_data', + 'OS-EXT-SRV-ATTR:host', + 'OS-EXT-SRV-ATTR:hostname', + 'OS-EXT-SRV-ATTR:kernel_id', + 'OS-EXT-SRV-ATTR:launch_index', + 'OS-EXT-SRV-ATTR:ramdisk_id', + 'OS-EXT-SRV-ATTR:reservation_id', + 'OS-EXT-SRV-ATTR:root_device_name', + 'OS-SCH-HNT:scheduler_hints', ): short_key = key.split(':')[1] ret[short_key] = _pop_or_get(server, key, None, self.strict_mode) diff --git a/openstack/cloud/_dns.py b/openstack/cloud/_dns.py index 8efd07d6c..4001aa6bd 100644 --- a/openstack/cloud/_dns.py +++ b/openstack/cloud/_dns.py @@ -33,8 +33,7 @@ class DnsCloudMixin: """ if not filters: filters = {} - return list(self.dns.zones(allow_unknown_params=True, - **filters)) + return list(self.dns.zones(allow_unknown_params=True, **filters)) def get_zone(self, name_or_id, filters=None): """Get a zone by name or ID. @@ -49,7 +48,8 @@ class DnsCloudMixin: if not filters: filters = {} zone = self.dns.find_zone( - name_or_id=name_or_id, ignore_missing=True, **filters) + name_or_id=name_or_id, ignore_missing=True, **filters + ) if not zone: return None return zone @@ -58,8 +58,15 @@ class DnsCloudMixin: zones = self.list_zones(filters) return _utils._filter_list(zones, name_or_id, filters) - def create_zone(self, name, zone_type=None, email=None, description=None, - ttl=None, masters=None): + def create_zone( + self, + name, + zone_type=None, + email=None, + description=None, + ttl=None, + masters=None, + ): """Create a new zone. :param name: Name of the zone being created. @@ -82,8 +89,9 @@ class DnsCloudMixin: zone_type = zone_type.upper() if zone_type not in ('PRIMARY', 'SECONDARY'): raise exc.OpenStackCloudException( - "Invalid type %s, valid choices are PRIMARY or SECONDARY" % - zone_type) + "Invalid type %s, valid choices are PRIMARY or SECONDARY" + % zone_type + ) zone = { "name": name, @@ -125,7 +133,8 @@ class DnsCloudMixin: zone = self.get_zone(name_or_id) if not zone: raise exc.OpenStackCloudException( - "Zone %s not found." % name_or_id) + "Zone %s not found." % name_or_id + ) return self.dns.update_zone(zone['id'], **kwargs) @@ -162,8 +171,7 @@ class DnsCloudMixin: else: zone_obj = self.get_zone(zone) if zone_obj is None: - raise exc.OpenStackCloudException( - "Zone %s not found." % zone) + raise exc.OpenStackCloudException("Zone %s not found." % zone) return list(self.dns.recordsets(zone_obj)) def get_recordset(self, zone, name_or_id): @@ -182,11 +190,11 @@ class DnsCloudMixin: else: zone_obj = self.get_zone(zone) if not zone_obj: - raise exc.OpenStackCloudException( - "Zone %s not found." % zone) + raise exc.OpenStackCloudException("Zone %s not found." % zone) try: return self.dns.find_recordset( - zone=zone_obj, name_or_id=name_or_id, ignore_missing=False) + zone=zone_obj, name_or_id=name_or_id, ignore_missing=False + ) except Exception: return None @@ -194,8 +202,9 @@ class DnsCloudMixin: recordsets = self.list_recordsets(zone=zone) return _utils._filter_list(recordsets, name_or_id, filters) - def create_recordset(self, zone, name, recordset_type, records, - description=None, ttl=None): + def create_recordset( + self, zone, name, recordset_type, records, description=None, ttl=None + ): """Create a recordset. :param zone: Name, ID or :class:`openstack.dns.v2.zone.Zone` instance @@ -216,17 +225,12 @@ class DnsCloudMixin: else: zone_obj = self.get_zone(zone) if not zone_obj: - raise exc.OpenStackCloudException( - "Zone %s not found." % zone) + raise exc.OpenStackCloudException("Zone %s not found." % zone) # We capitalize the type in case the user sends in lowercase recordset_type = recordset_type.upper() - body = { - 'name': name, - 'type': recordset_type, - 'records': records - } + body = {'name': name, 'type': recordset_type, 'records': records} if description: body['description'] = description @@ -255,7 +259,8 @@ class DnsCloudMixin: rs = self.get_recordset(zone, name_or_id) if not rs: raise exc.OpenStackCloudException( - "Recordset %s not found." % name_or_id) + "Recordset %s not found." % name_or_id + ) rs = self.dns.update_recordset(recordset=rs, **kwargs) diff --git a/openstack/cloud/_floating_ip.py b/openstack/cloud/_floating_ip.py index ee6ac0c3d..951173140 100644 --- a/openstack/cloud/_floating_ip.py +++ b/openstack/cloud/_floating_ip.py @@ -14,7 +14,6 @@ # We can't just use list, because sphinx gets confused by # openstack.resource.Resource.list and openstack.resource2.Resource.list import ipaddress -# import jsonpatch import threading import time import types # noqa @@ -30,7 +29,8 @@ from openstack import utils _CONFIG_DOC_URL = ( "https://docs.openstack.org/openstacksdk/latest/" - "user/config/configuration.html") + "user/config/configuration.html" +) class FloatingIPCloudMixin: @@ -39,8 +39,7 @@ class FloatingIPCloudMixin: def __init__(self): self.private = self.config.config.get('private', False) - self._floating_ip_source = self.config.config.get( - 'floating_ip_source') + self._floating_ip_source = self.config.config.get('floating_ip_source') if self._floating_ip_source: if self._floating_ip_source.lower() == 'none': self._floating_ip_source = None @@ -68,7 +67,8 @@ class FloatingIPCloudMixin: # understand, obviously. warnings.warn( "search_floating_ips is deprecated. " - "Use search_resource instead.") + "Use search_resource instead." + ) if self._use_neutron_floating() and isinstance(filters, dict): return list(self.network.ips(**filters)) else: @@ -83,8 +83,7 @@ class FloatingIPCloudMixin: def _nova_list_floating_ips(self): try: - data = proxy._json_response( - self.compute.get('/os-floating-ips')) + data = proxy._json_response(self.compute.get('/os-floating-ips')) except exc.OpenStackCloudURINotFound: return [] return self._get_and_munchify('floating_ips', data) @@ -137,10 +136,11 @@ class FloatingIPCloudMixin: " using clouds.yaml to configure settings for your" " cloud(s), and you want to configure this setting," " you will need a clouds.yaml file. For more" - " information, please see %(doc_url)s", { + " information, please see %(doc_url)s", + { 'cloud': self.name, 'doc_url': _CONFIG_DOC_URL, - } + }, ) # We can't fallback to nova because we push-down filters. # We got a 404 which means neutron doesn't exist. If the @@ -148,7 +148,9 @@ class FloatingIPCloudMixin: return [] self.log.debug( "Something went wrong talking to neutron API: " - "'%(msg)s'. Trying with Nova.", {'msg': str(e)}) + "'%(msg)s'. Trying with Nova.", + {'msg': str(e)}, + ) # Fall-through, trying with Nova else: if filters: @@ -174,11 +176,13 @@ class FloatingIPCloudMixin: """ if not self._has_nova_extension('os-floating-ip-pools'): raise exc.OpenStackCloudUnavailableExtension( - 'Floating IP pools extension is not available on target cloud') + 'Floating IP pools extension is not available on target cloud' + ) data = proxy._json_response( self.compute.get('os-floating-ip-pools'), - error_message="Error fetching floating IP pool list") + error_message="Error fetching floating IP pool list", + ) pools = self._get_and_munchify('floating_ip_pools', data) return [{'name': p['name']} for p in pools] @@ -217,7 +221,7 @@ class FloatingIPCloudMixin: return _utils._filter_list(self._floating_ips, None, filters) def get_floating_ip_by_id(self, id): - """ Get a floating ip by ID + """Get a floating ip by ID :param id: ID of the floating ip. :returns: A floating ip @@ -231,12 +235,15 @@ class FloatingIPCloudMixin: else: data = proxy._json_response( self.compute.get('/os-floating-ips/{id}'.format(id=id)), - error_message=error_message) + error_message=error_message, + ) return self._normalize_floating_ip( - self._get_and_munchify('floating_ip', data)) + self._get_and_munchify('floating_ip', data) + ) def _neutron_available_floating_ips( - self, network=None, project_id=None, server=None): + self, network=None, project_id=None, server=None + ): """Get a floating IP from a network. Return a list of available floating IPs or allocate a new one and @@ -271,8 +278,7 @@ class FloatingIPCloudMixin: if floating_network_id is None: raise exc.OpenStackCloudResourceNotFound( - "unable to find external network {net}".format( - net=network) + "unable to find external network {net}".format(net=network) ) else: floating_network_id = self._get_floating_network_id() @@ -285,14 +291,16 @@ class FloatingIPCloudMixin: floating_ips = self._list_floating_ips() available_ips = _utils._filter_list( - floating_ips, name_or_id=None, filters=filters) + floating_ips, name_or_id=None, filters=filters + ) if available_ips: return available_ips # No available IP found or we didn't try # allocate a new Floating IP f_ip = self._neutron_create_floating_ip( - network_id=floating_network_id, server=server) + network_id=floating_network_id, server=server + ) return [f_ip] @@ -311,23 +319,22 @@ class FloatingIPCloudMixin: """ with _utils.shade_exceptions( - "Unable to create floating IP in pool {pool}".format( - pool=pool)): + "Unable to create floating IP in pool {pool}".format(pool=pool) + ): if pool is None: pools = self.list_floating_ip_pools() if not pools: raise exc.OpenStackCloudResourceNotFound( - "unable to find a floating ip pool") + "unable to find a floating ip pool" + ) pool = pools[0]['name'] - filters = { - 'instance_id': None, - 'pool': pool - } + filters = {'instance_id': None, 'pool': pool} floating_ips = self._nova_list_floating_ips() available_ips = _utils._filter_list( - floating_ips, name_or_id=None, filters=filters) + floating_ips, name_or_id=None, filters=filters + ) if available_ips: return available_ips @@ -341,7 +348,8 @@ class FloatingIPCloudMixin: """Find the network providing floating ips by looking at routers.""" if self._floating_network_by_router_lock.acquire( - not self._floating_network_by_router_run): + not self._floating_network_by_router_run + ): if self._floating_network_by_router_run: self._floating_network_by_router_lock.release() return self._floating_network_by_router @@ -349,7 +357,8 @@ class FloatingIPCloudMixin: for router in self.list_routers(): if router['admin_state_up']: network_id = router.get( - 'external_gateway_info', {}).get('network_id') + 'external_gateway_info', {} + ).get('network_id') if network_id: self._floating_network_by_router = network_id finally: @@ -371,12 +380,15 @@ class FloatingIPCloudMixin: if self._use_neutron_floating(): try: f_ips = self._neutron_available_floating_ips( - network=network, server=server) + network=network, server=server + ) return f_ips[0] except exc.OpenStackCloudURINotFound as e: self.log.debug( "Something went wrong talking to neutron API: " - "'%(msg)s'. Trying with Nova.", {'msg': str(e)}) + "'%(msg)s'. Trying with Nova.", + {'msg': str(e)}, + ) # Fall-through, trying with Nova f_ips = self._normalize_floating_ips( @@ -395,12 +407,20 @@ class FloatingIPCloudMixin: floating_network_id = floating_network else: raise exc.OpenStackCloudResourceNotFound( - "unable to find an external network") + "unable to find an external network" + ) return floating_network_id - def create_floating_ip(self, network=None, server=None, - fixed_address=None, nat_destination=None, - port=None, wait=False, timeout=60): + def create_floating_ip( + self, + network=None, + server=None, + fixed_address=None, + nat_destination=None, + port=None, + wait=False, + timeout=60, + ): """Allocate a new floating IP from a network or a pool. :param network: Name or ID of the network @@ -430,15 +450,20 @@ class FloatingIPCloudMixin: if self._use_neutron_floating(): try: return self._neutron_create_floating_ip( - network_name_or_id=network, server=server, + network_name_or_id=network, + server=server, fixed_address=fixed_address, nat_destination=nat_destination, port=port, - wait=wait, timeout=timeout) + wait=wait, + timeout=timeout, + ) except exc.OpenStackCloudURINotFound as e: self.log.debug( "Something went wrong talking to neutron API: " - "'%(msg)s'. Trying with Nova.", {'msg': str(e)}) + "'%(msg)s'. Trying with Nova.", + {'msg': str(e)}, + ) # Fall-through, trying with Nova if port: @@ -447,10 +472,12 @@ class FloatingIPCloudMixin: " arbitrary floating-ip/port mappings. Please nudge" " your cloud provider to upgrade the networking stack" " to neutron, or alternately provide the server," - " fixed_address and nat_destination arguments as appropriate") + " fixed_address and nat_destination arguments as appropriate" + ) # Else, we are using Nova network f_ips = self._normalize_floating_ips( - [self._nova_create_floating_ip(pool=network)]) + [self._nova_create_floating_ip(pool=network)] + ) return f_ips[0] def _submit_create_fip(self, kwargs): @@ -458,10 +485,16 @@ class FloatingIPCloudMixin: return self.network.create_ip(**kwargs) def _neutron_create_floating_ip( - self, network_name_or_id=None, server=None, - fixed_address=None, nat_destination=None, - port=None, - wait=False, timeout=60, network_id=None): + self, + network_name_or_id=None, + server=None, + fixed_address=None, + nat_destination=None, + port=None, + wait=False, + timeout=60, + network_id=None, + ): if not network_id: if network_name_or_id: @@ -470,7 +503,8 @@ class FloatingIPCloudMixin: except exceptions.ResourceNotFound: raise exc.OpenStackCloudResourceNotFound( "unable to find network for floating ips with ID " - "{0}".format(network_name_or_id)) + "{0}".format(network_name_or_id) + ) network_id = network['id'] else: network_id = self._get_floating_network_id() @@ -480,8 +514,10 @@ class FloatingIPCloudMixin: if not port: if server: (port_obj, fixed_ip_address) = self._nat_destination_port( - server, fixed_address=fixed_address, - nat_destination=nat_destination) + server, + fixed_address=fixed_address, + nat_destination=nat_destination, + ) if port_obj: port = port_obj['id'] if fixed_ip_address: @@ -499,57 +535,68 @@ class FloatingIPCloudMixin: if wait: try: for count in utils.iterate_timeout( - timeout, - "Timeout waiting for the floating IP" - " to be ACTIVE", - wait=self._FLOAT_AGE): + timeout, + "Timeout waiting for the floating IP" " to be ACTIVE", + wait=self._FLOAT_AGE, + ): fip = self.get_floating_ip(fip_id) if fip and fip['status'] == 'ACTIVE': break except exc.OpenStackCloudTimeout: self.log.error( "Timed out on floating ip %(fip)s becoming active." - " Deleting", {'fip': fip_id}) + " Deleting", + {'fip': fip_id}, + ) try: self.delete_floating_ip(fip_id) except Exception as e: self.log.error( "FIP LEAK: Attempted to delete floating ip " "%(fip)s but received %(exc)s exception: " - "%(err)s", {'fip': fip_id, 'exc': e.__class__, - 'err': str(e)}) + "%(err)s", + {'fip': fip_id, 'exc': e.__class__, 'err': str(e)}, + ) raise if fip['port_id'] != port: if server: raise exc.OpenStackCloudException( "Attempted to create FIP on port {port} for server" " {server} but FIP has port {port_id}".format( - port=port, port_id=fip['port_id'], - server=server['id'])) + port=port, + port_id=fip['port_id'], + server=server['id'], + ) + ) else: raise exc.OpenStackCloudException( "Attempted to create FIP on port {port}" - " but something went wrong".format(port=port)) + " but something went wrong".format(port=port) + ) return fip def _nova_create_floating_ip(self, pool=None): with _utils.shade_exceptions( - "Unable to create floating IP in pool {pool}".format( - pool=pool)): + "Unable to create floating IP in pool {pool}".format(pool=pool) + ): if pool is None: pools = self.list_floating_ip_pools() if not pools: raise exc.OpenStackCloudResourceNotFound( - "unable to find a floating ip pool") + "unable to find a floating ip pool" + ) pool = pools[0]['name'] - data = proxy._json_response(self.compute.post( - '/os-floating-ips', json=dict(pool=pool))) + data = proxy._json_response( + self.compute.post('/os-floating-ips', json=dict(pool=pool)) + ) pool_ip = self._get_and_munchify('floating_ip', data) # TODO(mordred) Remove this - it's just for compat data = proxy._json_response( - self.compute.get('/os-floating-ips/{id}'.format( - id=pool_ip['id']))) + self.compute.get( + '/os-floating-ips/{id}'.format(id=pool_ip['id']) + ) + ) return self._get_and_munchify('floating_ip', data) def delete_floating_ip(self, floating_ip_id, retry=1): @@ -589,8 +636,11 @@ class FloatingIPCloudMixin: " {retry} times. Although the cloud did not indicate any errors" " the floating ip is still in existence. Aborting further" " operations.".format( - id=floating_ip_id, ip=f_ip['floating_ip_address'], - retry=retry + 1)) + id=floating_ip_id, + ip=f_ip['floating_ip_address'], + retry=retry + 1, + ) + ) def _delete_floating_ip(self, floating_ip_id): if self._use_neutron_floating(): @@ -599,14 +649,14 @@ class FloatingIPCloudMixin: except exc.OpenStackCloudURINotFound as e: self.log.debug( "Something went wrong talking to neutron API: " - "'%(msg)s'. Trying with Nova.", {'msg': str(e)}) + "'%(msg)s'. Trying with Nova.", + {'msg': str(e)}, + ) return self._nova_delete_floating_ip(floating_ip_id) def _neutron_delete_floating_ip(self, floating_ip_id): try: - self.network.delete_ip( - floating_ip_id, ignore_missing=False - ) + self.network.delete_ip(floating_ip_id, ignore_missing=False) except exceptions.ResourceNotFound: return False return True @@ -615,9 +665,12 @@ class FloatingIPCloudMixin: try: proxy._json_response( self.compute.delete( - '/os-floating-ips/{id}'.format(id=floating_ip_id)), + '/os-floating-ips/{id}'.format(id=floating_ip_id) + ), error_message='Unable to delete floating IP {fip_id}'.format( - fip_id=floating_ip_id)) + fip_id=floating_ip_id + ), + ) except exc.OpenStackCloudURINotFound: return False return True @@ -648,14 +701,23 @@ class FloatingIPCloudMixin: if self._use_neutron_floating(): for ip in self.list_floating_ips(): if not bool(ip.port_id): - processed.append(self.delete_floating_ip( - floating_ip_id=ip['id'], retry=retry)) + processed.append( + self.delete_floating_ip( + floating_ip_id=ip['id'], retry=retry + ) + ) return len(processed) if all(processed) else False def _attach_ip_to_server( - self, server, floating_ip, - fixed_address=None, wait=False, - timeout=60, skip_attach=False, nat_destination=None): + self, + server, + floating_ip, + fixed_address=None, + wait=False, + timeout=60, + skip_attach=False, + nat_destination=None, + ): """Attach a floating IP to a server. :param server: Server dict @@ -685,8 +747,9 @@ class FloatingIPCloudMixin: # the server data and try again. There are some clouds, which # explicitely forbids FIP assign call if it is already assigned. server = self.get_server_by_id(server['id']) - ext_ip = meta.get_server_ip(server, ext_tag='floating', - public=True) + ext_ip = meta.get_server_ip( + server, ext_tag='floating', public=True + ) if ext_ip == floating_ip['floating_ip_address']: return server @@ -694,74 +757,84 @@ class FloatingIPCloudMixin: if not skip_attach: try: self._neutron_attach_ip_to_server( - server=server, floating_ip=floating_ip, + server=server, + floating_ip=floating_ip, fixed_address=fixed_address, - nat_destination=nat_destination) + nat_destination=nat_destination, + ) except exc.OpenStackCloudURINotFound as e: self.log.debug( "Something went wrong talking to neutron API: " - "'%(msg)s'. Trying with Nova.", {'msg': str(e)}) + "'%(msg)s'. Trying with Nova.", + {'msg': str(e)}, + ) # Fall-through, trying with Nova else: # Nova network self._nova_attach_ip_to_server( - server_id=server['id'], floating_ip_id=floating_ip['id'], - fixed_address=fixed_address) + server_id=server['id'], + floating_ip_id=floating_ip['id'], + fixed_address=fixed_address, + ) if wait: # Wait for the address to be assigned to the server server_id = server['id'] for _ in utils.iterate_timeout( - timeout, - "Timeout waiting for the floating IP to be attached.", - wait=self._SERVER_AGE): + timeout, + "Timeout waiting for the floating IP to be attached.", + wait=self._SERVER_AGE, + ): server = self.get_server_by_id(server_id) ext_ip = meta.get_server_ip( - server, ext_tag='floating', public=True) + server, ext_tag='floating', public=True + ) if ext_ip == floating_ip['floating_ip_address']: return server return server def _neutron_attach_ip_to_server( - self, server, floating_ip, fixed_address=None, - nat_destination=None): + self, server, floating_ip, fixed_address=None, nat_destination=None + ): # Find an available port (port, fixed_address) = self._nat_destination_port( - server, fixed_address=fixed_address, - nat_destination=nat_destination) + server, + fixed_address=fixed_address, + nat_destination=nat_destination, + ) if not port: raise exc.OpenStackCloudException( - "unable to find a port for server {0}".format( - server['id'])) + "unable to find a port for server {0}".format(server['id']) + ) floating_ip_args = {'port_id': port['id']} if fixed_address is not None: floating_ip_args['fixed_ip_address'] = fixed_address - return self.network.update_ip( - floating_ip, - **floating_ip_args) + return self.network.update_ip(floating_ip, **floating_ip_args) - def _nova_attach_ip_to_server(self, server_id, floating_ip_id, - fixed_address=None): - f_ip = self.get_floating_ip( - id=floating_ip_id) + def _nova_attach_ip_to_server( + self, server_id, floating_ip_id, fixed_address=None + ): + f_ip = self.get_floating_ip(id=floating_ip_id) if f_ip is None: raise exc.OpenStackCloudException( - "unable to find floating IP {0}".format(floating_ip_id)) + "unable to find floating IP {0}".format(floating_ip_id) + ) error_message = "Error attaching IP {ip} to instance {id}".format( - ip=floating_ip_id, id=server_id) - body = { - 'address': f_ip['floating_ip_address'] - } + ip=floating_ip_id, id=server_id + ) + body = {'address': f_ip['floating_ip_address']} if fixed_address: body['fixed_address'] = fixed_address return proxy._json_response( self.compute.post( '/servers/{server_id}/action'.format(server_id=server_id), - json=dict(addFloatingIp=body)), - error_message=error_message) + json=dict(addFloatingIp=body), + ), + error_message=error_message, + ) def detach_ip_from_server(self, server_id, floating_ip_id): """Detach a floating IP from a server. @@ -777,31 +850,36 @@ class FloatingIPCloudMixin: if self._use_neutron_floating(): try: return self._neutron_detach_ip_from_server( - server_id=server_id, floating_ip_id=floating_ip_id) + server_id=server_id, floating_ip_id=floating_ip_id + ) except exc.OpenStackCloudURINotFound as e: self.log.debug( "Something went wrong talking to neutron API: " - "'%(msg)s'. Trying with Nova.", {'msg': str(e)}) + "'%(msg)s'. Trying with Nova.", + {'msg': str(e)}, + ) # Fall-through, trying with Nova # Nova network self._nova_detach_ip_from_server( - server_id=server_id, floating_ip_id=floating_ip_id) + server_id=server_id, floating_ip_id=floating_ip_id + ) def _neutron_detach_ip_from_server(self, server_id, floating_ip_id): f_ip = self.get_floating_ip(id=floating_ip_id) if f_ip is None or not bool(f_ip.port_id): return False try: - self.network.update_ip( - floating_ip_id, - port_id=None - ) + self.network.update_ip(floating_ip_id, port_id=None) except exceptions.SDKException: raise exceptions.SDKException( - ("Error detaching IP {ip} from " - "server {server_id}".format( - ip=floating_ip_id, server_id=server_id))) + ( + "Error detaching IP {ip} from " + "server {server_id}".format( + ip=floating_ip_id, server_id=server_id + ) + ) + ) return True @@ -810,21 +888,33 @@ class FloatingIPCloudMixin: f_ip = self.get_floating_ip(id=floating_ip_id) if f_ip is None: raise exc.OpenStackCloudException( - "unable to find floating IP {0}".format(floating_ip_id)) + "unable to find floating IP {0}".format(floating_ip_id) + ) error_message = "Error detaching IP {ip} from instance {id}".format( - ip=floating_ip_id, id=server_id) + ip=floating_ip_id, id=server_id + ) return proxy._json_response( self.compute.post( '/servers/{server_id}/action'.format(server_id=server_id), - json=dict(removeFloatingIp=dict( - address=f_ip['floating_ip_address']))), - error_message=error_message) + json=dict( + removeFloatingIp=dict(address=f_ip['floating_ip_address']) + ), + ), + error_message=error_message, + ) return True def _add_ip_from_pool( - self, server, network, fixed_address=None, reuse=True, - wait=False, timeout=60, nat_destination=None): + self, + server, + network, + fixed_address=None, + reuse=True, + wait=False, + timeout=60, + nat_destination=None, + ): """Add a floating IP to a server from a given pool This method reuses available IPs, when possible, or allocate new IPs @@ -851,9 +941,12 @@ class FloatingIPCloudMixin: start_time = time.time() f_ip = self.create_floating_ip( server=server, - network=network, nat_destination=nat_destination, + network=network, + nat_destination=nat_destination, fixed_address=fixed_address, - wait=wait, timeout=timeout) + wait=wait, + timeout=timeout, + ) timeout = timeout - (time.time() - start_time) # Wait for cache invalidation time so that we don't try # to attach the FIP a second time below @@ -866,12 +959,23 @@ class FloatingIPCloudMixin: # the attach function below to get back the server dict refreshed # with the FIP information. return self._attach_ip_to_server( - server=server, floating_ip=f_ip, fixed_address=fixed_address, - wait=wait, timeout=timeout, nat_destination=nat_destination) + server=server, + floating_ip=f_ip, + fixed_address=fixed_address, + wait=wait, + timeout=timeout, + nat_destination=nat_destination, + ) def add_ip_list( - self, server, ips, wait=False, timeout=60, - fixed_address=None, nat_destination=None): + self, + server, + ips, + wait=False, + timeout=60, + fixed_address=None, + nat_destination=None, + ): """Attach a list of IPs to a server. :param server: a server object @@ -896,10 +1000,16 @@ class FloatingIPCloudMixin: for ip in ips: f_ip = self.get_floating_ip( - id=None, filters={'floating_ip_address': ip}) + id=None, filters={'floating_ip_address': ip} + ) server = self._attach_ip_to_server( - server=server, floating_ip=f_ip, wait=wait, timeout=timeout, - fixed_address=fixed_address, nat_destination=nat_destination) + server=server, + floating_ip=f_ip, + wait=wait, + timeout=timeout, + fixed_address=fixed_address, + nat_destination=nat_destination, + ) return server def add_auto_ip(self, server, wait=False, timeout=60, reuse=True): @@ -925,7 +1035,8 @@ class FloatingIPCloudMixin: """ server = self._add_auto_ip( - server, wait=wait, timeout=timeout, reuse=reuse) + server, wait=wait, timeout=timeout, reuse=reuse + ) return server['interface_ip'] or None def _add_auto_ip(self, server, wait=False, timeout=60, reuse=True): @@ -936,7 +1047,8 @@ class FloatingIPCloudMixin: else: start_time = time.time() f_ip = self.create_floating_ip( - server=server, wait=wait, timeout=timeout) + server=server, wait=wait, timeout=timeout + ) timeout = timeout - (time.time() - start_time) if server: # This gets passed in for both nova and neutron @@ -951,8 +1063,12 @@ class FloatingIPCloudMixin: # the attach function below to get back the server dict refreshed # with the FIP information. return self._attach_ip_to_server( - server=server, floating_ip=f_ip, wait=wait, timeout=timeout, - skip_attach=skip_attach) + server=server, + floating_ip=f_ip, + wait=wait, + timeout=timeout, + skip_attach=skip_attach, + ) except exc.OpenStackCloudTimeout: if self._use_neutron_floating() and created: # We are here because we created an IP on the port @@ -962,36 +1078,60 @@ class FloatingIPCloudMixin: "Timeout waiting for floating IP to become" " active. Floating IP %(ip)s:%(id)s was created for" " server %(server)s but is being deleted due to" - " activation failure.", { + " activation failure.", + { 'ip': f_ip['floating_ip_address'], 'id': f_ip['id'], - 'server': server['id']}) + 'server': server['id'], + }, + ) try: self.delete_floating_ip(f_ip['id']) except Exception as e: self.log.error( "FIP LEAK: Attempted to delete floating ip " "%(fip)s but received %(exc)s exception: %(err)s", - {'fip': f_ip['id'], 'exc': e.__class__, 'err': str(e)}) + {'fip': f_ip['id'], 'exc': e.__class__, 'err': str(e)}, + ) raise e raise def add_ips_to_server( - self, server, auto_ip=True, ips=None, ip_pool=None, - wait=False, timeout=60, reuse=True, fixed_address=None, - nat_destination=None): + self, + server, + auto_ip=True, + ips=None, + ip_pool=None, + wait=False, + timeout=60, + reuse=True, + fixed_address=None, + nat_destination=None, + ): if ip_pool: server = self._add_ip_from_pool( - server, ip_pool, reuse=reuse, wait=wait, timeout=timeout, - fixed_address=fixed_address, nat_destination=nat_destination) + server, + ip_pool, + reuse=reuse, + wait=wait, + timeout=timeout, + fixed_address=fixed_address, + nat_destination=nat_destination, + ) elif ips: server = self.add_ip_list( - server, ips, wait=wait, timeout=timeout, - fixed_address=fixed_address, nat_destination=nat_destination) + server, + ips, + wait=wait, + timeout=timeout, + fixed_address=fixed_address, + nat_destination=nat_destination, + ) elif auto_ip: if self._needs_floating_ip(server, nat_destination): server = self._add_auto_ip( - server, wait=wait, timeout=timeout, reuse=reuse) + server, wait=wait, timeout=timeout, reuse=reuse + ) return server def _needs_floating_ip(self, server, nat_destination): @@ -1026,18 +1166,30 @@ class FloatingIPCloudMixin: # meta.add_server_interfaces() was not called server = self.compute.get_server(server) - if server['public_v4'] \ - or any([any([address['OS-EXT-IPS:type'] == 'floating' - for address in addresses]) - for addresses - in (server['addresses'] or {}).values()]): + if server['public_v4'] or any( + [ + any( + [ + address['OS-EXT-IPS:type'] == 'floating' + for address in addresses + ] + ) + for addresses in (server['addresses'] or {}).values() + ] + ): return False - if not server['private_v4'] \ - and not any([any([address['OS-EXT-IPS:type'] == 'fixed' - for address in addresses]) - for addresses - in (server['addresses'] or {}).values()]): + if not server['private_v4'] and not any( + [ + any( + [ + address['OS-EXT-IPS:type'] == 'fixed' + for address in addresses + ] + ) + for addresses in (server['addresses'] or {}).values() + ] + ): return False if self.private: @@ -1053,7 +1205,8 @@ class FloatingIPCloudMixin: return False (port_obj, fixed_ip_address) = self._nat_destination_port( - server, nat_destination=nat_destination) + server, nat_destination=nat_destination + ) if not port_obj or not fixed_ip_address: return False @@ -1061,7 +1214,8 @@ class FloatingIPCloudMixin: return True def _nat_destination_port( - self, server, fixed_address=None, nat_destination=None): + self, server, fixed_address=None, nat_destination=None + ): """Returns server port that is on a nat_destination network Find a port attached to the server which is on a network which @@ -1082,9 +1236,10 @@ class FloatingIPCloudMixin: else: timeout = None for count in utils.iterate_timeout( - timeout, - "Timeout waiting for port to show up in list", - wait=self._PORT_AGE): + timeout, + "Timeout waiting for port to show up in list", + wait=self._PORT_AGE, + ): try: port_filter = {'device_id': server['id']} ports = self.search_ports(filters=port_filter) @@ -1103,7 +1258,9 @@ class FloatingIPCloudMixin: 'NAT Destination {nat_destination} was configured' ' but not found on the cloud. Please check your' ' config and your cloud and try again.'.format( - nat_destination=nat_destination)) + nat_destination=nat_destination + ) + ) else: nat_network = self.get_nat_destination() @@ -1118,7 +1275,8 @@ class FloatingIPCloudMixin: ' nat_destination property of the networks list in' ' your clouds.yaml file. If you do not have a' ' clouds.yaml file, please make one - your setup' - ' is complicated.'.format(server=server['id'])) + ' is complicated.'.format(server=server['id']) + ) maybe_ports = [] for maybe_port in ports: @@ -1129,7 +1287,9 @@ class FloatingIPCloudMixin: 'No port on server {server} was found matching' ' your NAT destination network {dest}. Please ' ' check your config'.format( - server=server['id'], dest=nat_network['name'])) + server=server['id'], dest=nat_network['name'] + ) + ) ports = maybe_ports # Select the most recent available IPv4 address @@ -1139,9 +1299,8 @@ class FloatingIPCloudMixin: # if there are more than one, will be the arbitrary port we # select. for port in sorted( - ports, - key=lambda p: p.get('created_at', 0), - reverse=True): + ports, key=lambda p: p.get('created_at', 0), reverse=True + ): for address in port.get('fixed_ips', list()): try: ip = ipaddress.ip_address(address['ip_address']) @@ -1152,7 +1311,8 @@ class FloatingIPCloudMixin: return port, fixed_address raise exc.OpenStackCloudException( "unable to find a free fixed IPv4 address for server " - "{0}".format(server['id'])) + "{0}".format(server['id']) + ) # unfortunately a port can have more than one fixed IP: # we can't use the search_ports filtering for fixed_address as # they are contained in a list. e.g. @@ -1178,8 +1338,10 @@ class FloatingIPCloudMixin: return self._floating_ip_source in ('nova', 'neutron') def _use_neutron_floating(self): - return (self.has_service('network') - and self._floating_ip_source == 'neutron') + return ( + self.has_service('network') + and self._floating_ip_source == 'neutron' + ) def _normalize_floating_ips(self, ips): """Normalize the structure of floating IPs @@ -1210,16 +1372,13 @@ class FloatingIPCloudMixin: ] """ - return [ - self._normalize_floating_ip(ip) for ip in ips - ] + return [self._normalize_floating_ip(ip) for ip in ips] def _normalize_floating_ip(self, ip): # Copy incoming floating ip because of shared dicts in unittests # Only import munch when we really need it - location = self._get_current_location( - project_id=ip.get('owner')) + location = self._get_current_location(project_id=ip.get('owner')) # This copy is to keep things from getting epically weird in tests ip = ip.copy() @@ -1228,7 +1387,8 @@ class FloatingIPCloudMixin: fixed_ip_address = ip.pop('fixed_ip_address', ip.pop('fixed_ip', None)) floating_ip_address = ip.pop('floating_ip_address', ip.pop('ip', None)) network_id = ip.pop( - 'floating_network_id', ip.pop('network', ip.pop('pool', None))) + 'floating_network_id', ip.pop('network', ip.pop('pool', None)) + ) project_id = ip.pop('tenant_id', '') project_id = ip.pop('project_id', project_id) diff --git a/openstack/cloud/_identity.py b/openstack/cloud/_identity.py index 5c8952797..a6cc6e29d 100644 --- a/openstack/cloud/_identity.py +++ b/openstack/cloud/_identity.py @@ -28,7 +28,8 @@ class IdentityCloudMixin: def _identity_client(self): if 'identity' not in self._raw_clients: self._raw_clients['identity'] = self._get_versioned_client( - 'identity', min_version=2, max_version='3.latest') + 'identity', min_version=2, max_version='3.latest' + ) return self._raw_clients['identity'] @_utils.cache_on_arguments() @@ -129,8 +130,9 @@ class IdentityCloudMixin: :raises: ``OpenStackCloudException`` if something goes wrong during the OpenStack API call. """ - return _utils._get_entity(self, 'project', name_or_id, filters, - domain_id=domain_id) + return _utils._get_entity( + self, 'project', name_or_id, filters, domain_id=domain_id + ) def update_project( self, @@ -178,7 +180,7 @@ class IdentityCloudMixin: name=name, description=description, domain_id=domain_id, - is_enabled=enabled + is_enabled=enabled, ) if kwargs: attrs.update(kwargs) @@ -195,19 +197,19 @@ class IdentityCloudMixin: """ try: project = self.identity.find_project( - name_or_id=name_or_id, - ignore_missing=True, - domain_id=domain_id + name_or_id=name_or_id, ignore_missing=True, domain_id=domain_id ) if not project: - self.log.debug( - "Project %s not found for deleting", name_or_id) + self.log.debug("Project %s not found for deleting", name_or_id) return False self.identity.delete_project(project) return True except exceptions.SDKException: - self.log.exception("Error in deleting project {project}".format( - project=name_or_id)) + self.log.exception( + "Error in deleting project {project}".format( + project=name_or_id + ) + ) return False @_utils.valid_kwargs('domain_id', 'name') @@ -299,8 +301,15 @@ class IdentityCloudMixin: """ return self.identity.get_user(user_id) - @_utils.valid_kwargs('name', 'email', 'enabled', 'domain_id', 'password', - 'description', 'default_project') + @_utils.valid_kwargs( + 'name', + 'email', + 'enabled', + 'domain_id', + 'password', + 'description', + 'default_project', + ) def update_user(self, name_or_id, **kwargs): self.list_users.invalidate(self) user_kwargs = {} @@ -351,7 +360,8 @@ class IdentityCloudMixin: user = self.get_user(name_or_id, **kwargs) if not user: self.log.debug( - "User {0} not found for deleting".format(name_or_id)) + "User {0} not found for deleting".format(name_or_id) + ) return False self.identity.delete_user(user) @@ -359,21 +369,23 @@ class IdentityCloudMixin: return True except exceptions.SDKException: - self.log.exception("Error in deleting user {user}".format( - user=name_or_id - )) + self.log.exception( + "Error in deleting user {user}".format(user=name_or_id) + ) return False def _get_user_and_group(self, user_name_or_id, group_name_or_id): user = self.get_user(user_name_or_id) if not user: raise exc.OpenStackCloudException( - 'User {user} not found'.format(user=user_name_or_id)) + 'User {user} not found'.format(user=user_name_or_id) + ) group = self.get_group(group_name_or_id) if not group: raise exc.OpenStackCloudException( - 'Group {user} not found'.format(user=group_name_or_id)) + 'Group {user} not found'.format(user=group_name_or_id) + ) return (user, group) @@ -438,8 +450,9 @@ class IdentityCloudMixin: return self.identity.create_service(**kwargs) - @_utils.valid_kwargs('name', 'enabled', 'type', 'service_type', - 'description') + @_utils.valid_kwargs( + 'name', 'enabled', 'type', 'service_type', 'description' + ) def update_service(self, name_or_id, **kwargs): # NOTE(SamYaple): Keystone v3 only accepts 'type' but shade accepts @@ -519,7 +532,8 @@ class IdentityCloudMixin: return True except exceptions.SDKException: self.log.exception( - 'Failed to delete service {id}'.format(id=service['id'])) + 'Failed to delete service {id}'.format(id=service['id']) + ) return False @_utils.valid_kwargs('public_url', 'internal_url', 'admin_url') @@ -560,31 +574,42 @@ class IdentityCloudMixin: if service is None: raise exc.OpenStackCloudException( "service {service} not found".format( - service=service_name_or_id)) + service=service_name_or_id + ) + ) endpoints_args = [] if url: # v3 in use, v3-like arguments, one endpoint created endpoints_args.append( - {'url': url, 'interface': interface, - 'service_id': service['id'], 'enabled': enabled, - 'region_id': region}) + { + 'url': url, + 'interface': interface, + 'service_id': service['id'], + 'enabled': enabled, + 'region_id': region, + } + ) else: # v3 in use, v2.0-like arguments, one endpoint created for each # interface url provided - endpoint_args = {'region_id': region, 'enabled': enabled, - 'service_id': service['id']} + endpoint_args = { + 'region_id': region, + 'enabled': enabled, + 'service_id': service['id'], + } if public_url: - endpoint_args.update({'url': public_url, - 'interface': 'public'}) + endpoint_args.update( + {'url': public_url, 'interface': 'public'} + ) endpoints_args.append(endpoint_args.copy()) if internal_url: - endpoint_args.update({'url': internal_url, - 'interface': 'internal'}) + endpoint_args.update( + {'url': internal_url, 'interface': 'internal'} + ) endpoints_args.append(endpoint_args.copy()) if admin_url: - endpoint_args.update({'url': admin_url, - 'interface': 'admin'}) + endpoint_args.update({'url': admin_url, 'interface': 'admin'}) endpoints_args.append(endpoint_args.copy()) endpoints = [] @@ -592,8 +617,9 @@ class IdentityCloudMixin: endpoints.append(self.identity.create_endpoint(**args)) return endpoints - @_utils.valid_kwargs('enabled', 'service_name_or_id', 'url', 'interface', - 'region') + @_utils.valid_kwargs( + 'enabled', 'service_name_or_id', 'url', 'interface', 'region' + ) def update_endpoint(self, endpoint_id, **kwargs): service_name_or_id = kwargs.pop('service_name_or_id', None) if service_name_or_id is not None: @@ -670,8 +696,7 @@ class IdentityCloudMixin: self.identity.delete_endpoint(id) return True except exceptions.SDKException: - self.log.exception( - "Failed to delete endpoint {id}".format(id=id)) + self.log.exception("Failed to delete endpoint {id}".format(id=id)) return False def create_domain(self, name, description=None, enabled=True): @@ -746,7 +771,8 @@ class IdentityCloudMixin: dom = self.get_domain(name_or_id=name_or_id) if dom is None: self.log.debug( - "Domain %s not found for deleting", name_or_id) + "Domain %s not found for deleting", name_or_id + ) return False domain_id = dom['id'] @@ -963,8 +989,7 @@ class IdentityCloudMixin: try: group = self.identity.find_group(name_or_id) if group is None: - self.log.debug( - "Group %s not found for deleting", name_or_id) + self.log.debug("Group %s not found for deleting", name_or_id) return False self.identity.delete_group(group) @@ -974,7 +999,8 @@ class IdentityCloudMixin: except exceptions.SDKException: self.log.exception( - "Unable to delete group {name}".format(name=name_or_id)) + "Unable to delete group {name}".format(name=name_or_id) + ) return False def list_roles(self, **kwargs): @@ -1051,8 +1077,9 @@ class IdentityCloudMixin: filters['scope.' + k + '.id'] = filters[k] del filters[k] if 'os_inherit_extension_inherited_to' in filters: - filters['scope.OS-INHERIT:inherited_to'] = ( - filters['os_inherit_extension_inherited_to']) + filters['scope.OS-INHERIT:inherited_to'] = filters[ + 'os_inherit_extension_inherited_to' + ] del filters['os_inherit_extension_inherited_to'] return list(self.identity.role_assignments(**filters)) @@ -1138,8 +1165,7 @@ class IdentityCloudMixin: """ role = self.get_role(name_or_id, **kwargs) if role is None: - self.log.debug( - "Role %s not found for updating", name_or_id) + self.log.debug("Role %s not found for updating", name_or_id) return False return self.identity.update_role(role, name=name, **kwargs) @@ -1156,8 +1182,7 @@ class IdentityCloudMixin: """ role = self.get_role(name_or_id, **kwargs) if role is None: - self.log.debug( - "Role %s not found for deleting", name_or_id) + self.log.debug("Role %s not found for deleting", name_or_id) return False try: @@ -1165,17 +1190,25 @@ class IdentityCloudMixin: return True except exceptions.SDKExceptions: self.log.exception( - "Unable to delete role {name}".format( - name=name_or_id)) + "Unable to delete role {name}".format(name=name_or_id) + ) raise - def _get_grant_revoke_params(self, role, user=None, group=None, - project=None, domain=None, system=None): + def _get_grant_revoke_params( + self, + role, + user=None, + group=None, + project=None, + domain=None, + system=None, + ): data = {} search_args = {} if domain: data['domain'] = self.identity.find_domain( - domain, ignore_missing=False) + domain, ignore_missing=False + ) # We have domain. We should use it for further searching user, # group, role, project search_args['domain_id'] = data['domain'].id @@ -1183,33 +1216,47 @@ class IdentityCloudMixin: data['role'] = self.identity.find_role(name_or_id=role) if not data['role']: raise exc.OpenStackCloudException( - 'Role {0} not found.'.format(role)) + 'Role {0} not found.'.format(role) + ) if user: # use cloud.get_user to save us from bad searching by name data['user'] = self.get_user(user, filters=search_args) if group: data['group'] = self.identity.find_group( - group, ignore_missing=False, **search_args) + group, ignore_missing=False, **search_args + ) if data.get('user') and data.get('group'): raise exc.OpenStackCloudException( - 'Specify either a group or a user, not both') + 'Specify either a group or a user, not both' + ) if data.get('user') is None and data.get('group') is None: raise exc.OpenStackCloudException( - 'Must specify either a user or a group') + 'Must specify either a user or a group' + ) if project is None and domain is None and system is None: raise exc.OpenStackCloudException( - 'Must specify either a domain, project or system') + 'Must specify either a domain, project or system' + ) if project: data['project'] = self.identity.find_project( - project, ignore_missing=False, **search_args) + project, ignore_missing=False, **search_args + ) return data - def grant_role(self, name_or_id, user=None, group=None, - project=None, domain=None, system=None, wait=False, - timeout=60): + def grant_role( + self, + name_or_id, + user=None, + group=None, + project=None, + domain=None, + system=None, + wait=False, + timeout=60, + ): """Grant a role to a user. :param string name_or_id: Name or unique ID of the role. @@ -1236,8 +1283,13 @@ class IdentityCloudMixin: :raise OpenStackCloudException: if the role cannot be granted """ data = self._get_grant_revoke_params( - name_or_id, user=user, group=group, - project=project, domain=domain, system=system) + name_or_id, + user=user, + group=group, + project=project, + domain=domain, + system=system, + ) user = data.get('user') group = data.get('group') @@ -1249,63 +1301,73 @@ class IdentityCloudMixin: # Proceed with project - precedence over domain and system if user: has_role = self.identity.validate_user_has_project_role( - project, user, role) + project, user, role + ) if has_role: self.log.debug('Assignment already exists') return False - self.identity.assign_project_role_to_user( - project, user, role) + self.identity.assign_project_role_to_user(project, user, role) else: has_role = self.identity.validate_group_has_project_role( - project, group, role) + project, group, role + ) if has_role: self.log.debug('Assignment already exists') return False self.identity.assign_project_role_to_group( - project, group, role) + project, group, role + ) elif domain: # Proceed with domain - precedence over system if user: has_role = self.identity.validate_user_has_domain_role( - domain, user, role) + domain, user, role + ) if has_role: self.log.debug('Assignment already exists') return False - self.identity.assign_domain_role_to_user( - domain, user, role) + self.identity.assign_domain_role_to_user(domain, user, role) else: has_role = self.identity.validate_group_has_domain_role( - domain, group, role) + domain, group, role + ) if has_role: self.log.debug('Assignment already exists') return False - self.identity.assign_domain_role_to_group( - domain, group, role) + self.identity.assign_domain_role_to_group(domain, group, role) else: # Proceed with system # System name must be 'all' due to checks performed in # _get_grant_revoke_params if user: has_role = self.identity.validate_user_has_system_role( - user, role, system) + user, role, system + ) if has_role: self.log.debug('Assignment already exists') return False - self.identity.assign_system_role_to_user( - user, role, system) + self.identity.assign_system_role_to_user(user, role, system) else: has_role = self.identity.validate_group_has_system_role( - group, role, system) + group, role, system + ) if has_role: self.log.debug('Assignment already exists') return False - self.identity.assign_system_role_to_group( - group, role, system) + self.identity.assign_system_role_to_group(group, role, system) return True - def revoke_role(self, name_or_id, user=None, group=None, - project=None, domain=None, system=None, - wait=False, timeout=60): + def revoke_role( + self, + name_or_id, + user=None, + group=None, + project=None, + domain=None, + system=None, + wait=False, + timeout=60, + ): """Revoke a role from a user. :param string name_or_id: Name or unique ID of the role. @@ -1329,8 +1391,13 @@ class IdentityCloudMixin: :raise OpenStackCloudException: if the role cannot be removed """ data = self._get_grant_revoke_params( - name_or_id, user=user, group=group, - project=project, domain=domain, system=system) + name_or_id, + user=user, + group=group, + project=project, + domain=domain, + system=system, + ) user = data.get('user') group = data.get('group') @@ -1342,58 +1409,70 @@ class IdentityCloudMixin: # Proceed with project - precedence over domain and system if user: has_role = self.identity.validate_user_has_project_role( - project, user, role) + project, user, role + ) if not has_role: self.log.debug('Assignment does not exists') return False self.identity.unassign_project_role_from_user( - project, user, role) + project, user, role + ) else: has_role = self.identity.validate_group_has_project_role( - project, group, role) + project, group, role + ) if not has_role: self.log.debug('Assignment does not exists') return False self.identity.unassign_project_role_from_group( - project, group, role) + project, group, role + ) elif domain: # Proceed with domain - precedence over system if user: has_role = self.identity.validate_user_has_domain_role( - domain, user, role) + domain, user, role + ) if not has_role: self.log.debug('Assignment does not exists') return False self.identity.unassign_domain_role_from_user( - domain, user, role) + domain, user, role + ) else: has_role = self.identity.validate_group_has_domain_role( - domain, group, role) + domain, group, role + ) if not has_role: self.log.debug('Assignment does not exists') return False self.identity.unassign_domain_role_from_group( - domain, group, role) + domain, group, role + ) else: # Proceed with system # System name must be 'all' due to checks performed in # _get_grant_revoke_params if user: has_role = self.identity.validate_user_has_system_role( - user, role, system) + user, role, system + ) if not has_role: self.log.debug('Assignment does not exist') return False self.identity.unassign_system_role_from_user( - user, role, system) + user, role, system + ) else: has_role = self.identity.validate_group_has_system_role( - group, role, system) + group, role, system + ) if not has_role: self.log.debug('Assignment does not exist') return False self.identity.unassign_system_role_from_group( - group, role, system) + group, role, system + ) return True def _get_identity_params(self, domain_id=None, project=None): @@ -1406,7 +1485,8 @@ class IdentityCloudMixin: if not domain_id: raise exc.OpenStackCloudException( "User or project creation requires an explicit" - " domain_id argument.") + " domain_id argument." + ) else: ret.update({'domain_id': domain_id}) diff --git a/openstack/cloud/_image.py b/openstack/cloud/_image.py index 68174c995..1bd240fcb 100644 --- a/openstack/cloud/_image.py +++ b/openstack/cloud/_image.py @@ -46,7 +46,8 @@ class ImageCloudMixin: def _image_client(self): if 'image' not in self._raw_clients: self._raw_clients['image'] = self._get_versioned_client( - 'image', min_version=1, max_version='2.latest') + 'image', min_version=1, max_version='2.latest' + ) return self._raw_clients['image'] def search_images(self, name_or_id=None, filters=None): @@ -108,7 +109,7 @@ class ImageCloudMixin: return _utils._get_entity(self, 'image', name_or_id, filters) def get_image_by_id(self, id): - """ Get a image by ID + """Get a image by ID :param id: ID of the image. :returns: An image :class:`openstack.image.v2.image.Image` object. @@ -145,20 +146,23 @@ class ImageCloudMixin: if output_path is None and output_file is None: raise exc.OpenStackCloudException( 'No output specified, an output path or file object' - ' is necessary to write the image data to') + ' is necessary to write the image data to' + ) elif output_path is not None and output_file is not None: raise exc.OpenStackCloudException( 'Both an output path and file object were provided,' - ' however only one can be used at once') + ' however only one can be used at once' + ) image = self.image.find_image(name_or_id) if not image: raise exc.OpenStackCloudResourceNotFound( - "No images with name or ID %s were found" % name_or_id, None) + "No images with name or ID %s were found" % name_or_id, None + ) return self.image.download_image( - image, output=output_file or output_path, - chunk_size=chunk_size) + image, output=output_file or output_path, chunk_size=chunk_size + ) def get_image_exclude(self, name_or_id, exclude): for image in self.search_images(name_or_id): @@ -184,7 +188,8 @@ class ImageCloudMixin: def wait_for_image(self, image, timeout=3600): image_id = image['id'] for count in utils.iterate_timeout( - timeout, "Timeout waiting for image to snapshot"): + timeout, "Timeout waiting for image to snapshot" + ): self.list_images.invalidate(self) image = self.get_image(image_id) if not image: @@ -193,7 +198,8 @@ class ImageCloudMixin: return image elif image['status'] == 'error': raise exc.OpenStackCloudException( - 'Image {image} hit error state'.format(image=image_id)) + 'Image {image} hit error state'.format(image=image_id) + ) def delete_image( self, @@ -222,17 +228,19 @@ class ImageCloudMixin: # Task API means an image was uploaded to swift # TODO(gtema) does it make sense to move this into proxy? if self.image_api_use_tasks and ( - self.image._IMAGE_OBJECT_KEY in image.properties - or self.image._SHADE_IMAGE_OBJECT_KEY in image.properties): + self.image._IMAGE_OBJECT_KEY in image.properties + or self.image._SHADE_IMAGE_OBJECT_KEY in image.properties + ): (container, objname) = image.properties.get( - self.image._IMAGE_OBJECT_KEY, image.properties.get( - self.image._SHADE_IMAGE_OBJECT_KEY)).split('/', 1) + self.image._IMAGE_OBJECT_KEY, + image.properties.get(self.image._SHADE_IMAGE_OBJECT_KEY), + ).split('/', 1) self.delete_object(container=container, name=objname) if wait: for count in utils.iterate_timeout( - timeout, - "Timeout waiting for the image to be deleted."): + timeout, "Timeout waiting for the image to be deleted." + ): self._get_cache(None).invalidate() if self.get_image(image.id) is None: break @@ -307,38 +315,53 @@ class ImageCloudMixin: """ if volume: image = self.block_storage.create_image( - name=name, volume=volume, + name=name, + volume=volume, allow_duplicates=allow_duplicates, - container_format=container_format, disk_format=disk_format, - wait=wait, timeout=timeout) + container_format=container_format, + disk_format=disk_format, + wait=wait, + timeout=timeout, + ) else: image = self.image.create_image( - name, filename=filename, + name, + filename=filename, container=container, - md5=md5, sha256=sha256, - disk_format=disk_format, container_format=container_format, + md5=md5, + sha256=sha256, + disk_format=disk_format, + container_format=container_format, disable_vendor_agent=disable_vendor_agent, - wait=wait, timeout=timeout, tags=tags, - allow_duplicates=allow_duplicates, meta=meta, **kwargs) + wait=wait, + timeout=timeout, + tags=tags, + allow_duplicates=allow_duplicates, + meta=meta, + **kwargs, + ) self._get_cache(None).invalidate() if not wait: return image try: for count in utils.iterate_timeout( - timeout, - "Timeout waiting for the image to finish."): + timeout, "Timeout waiting for the image to finish." + ): image_obj = self.get_image(image.id) if image_obj and image_obj.status not in ('queued', 'saving'): return image_obj except exc.OpenStackCloudTimeout: self.log.debug( - "Timeout waiting for image to become ready. Deleting.") + "Timeout waiting for image to become ready. Deleting." + ) self.delete_image(image.id, wait=True) raise def update_image_properties( - self, image=None, name_or_id=None, meta=None, **properties): + self, image=None, name_or_id=None, meta=None, **properties + ): image = image or name_or_id return self.image.update_image_properties( - image=image, meta=meta, **properties) + image=image, meta=meta, **properties + ) diff --git a/openstack/cloud/_network.py b/openstack/cloud/_network.py index a4f5b81c2..ae7d96f94 100644 --- a/openstack/cloud/_network.py +++ b/openstack/cloud/_network.py @@ -1,4 +1,3 @@ - # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -231,14 +230,14 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) if not filters: filters = {} return self.network.find_qos_policy( - name_or_id=name_or_id, - ignore_missing=True, - **filters) + name_or_id=name_or_id, ignore_missing=True, **filters + ) # TODO(stephenfin): Deprecate this in favour of the 'list' function def search_qos_policies(self, name_or_id=None, filters=None): @@ -254,7 +253,8 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) query = {} if name_or_id: @@ -271,7 +271,8 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) # Translate None from search interface to empty {} for kwargs below if not filters: @@ -302,12 +303,14 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) if not self._has_neutron_extension('qos-rule-type-details'): raise exc.OpenStackCloudUnavailableExtension( 'qos-rule-type-details extension is not available ' - 'on target cloud') + 'on target cloud' + ) return self.network.get_qos_rule_type(rule_type) @@ -319,7 +322,8 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) # Translate None from search interface to empty {} for kwargs below if not filters: filters = {} @@ -350,9 +354,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_network( - name_or_id=name_or_id, - ignore_missing=True, - **filters) + name_or_id=name_or_id, ignore_missing=True, **filters + ) def get_network_by_id(self, id): """Get a network by ID @@ -387,9 +390,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_router( - name_or_id=name_or_id, - ignore_missing=True, - **filters) + name_or_id=name_or_id, ignore_missing=True, **filters + ) # TODO(stephenfin): Deprecate 'filters'; users should use 'list' for this def get_subnet(self, name_or_id, filters=None): @@ -412,9 +414,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_subnet( - name_or_id=name_or_id, - ignore_missing=True, - **filters) + name_or_id=name_or_id, ignore_missing=True, **filters + ) def get_subnet_by_id(self, id): """Get a subnet by ID @@ -449,9 +450,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_port( - name_or_id=name_or_id, - ignore_missing=True, - **filters) + name_or_id=name_or_id, ignore_missing=True, **filters + ) def get_port_by_id(self, id): """Get a port by ID @@ -510,20 +510,26 @@ class NetworkCloudMixin: if availability_zone_hints is not None: if not isinstance(availability_zone_hints, list): raise exc.OpenStackCloudException( - "Parameter 'availability_zone_hints' must be a list") + "Parameter 'availability_zone_hints' must be a list" + ) if not self._has_neutron_extension('network_availability_zone'): raise exc.OpenStackCloudUnavailableExtension( 'network_availability_zone extension is not available on ' - 'target cloud') + 'target cloud' + ) network['availability_zone_hints'] = availability_zone_hints if provider: if not isinstance(provider, dict): raise exc.OpenStackCloudException( - "Parameter 'provider' must be a dict") + "Parameter 'provider' must be a dict" + ) # Only pass what we know - for attr in ('physical_network', 'network_type', - 'segmentation_id'): + for attr in ( + 'physical_network', + 'network_type', + 'segmentation_id', + ): if attr in provider: arg = "provider:" + attr network[arg] = provider[attr] @@ -537,16 +543,19 @@ class NetworkCloudMixin: if port_security_enabled is not None: if not isinstance(port_security_enabled, bool): raise exc.OpenStackCloudException( - "Parameter 'port_security_enabled' must be a bool") + "Parameter 'port_security_enabled' must be a bool" + ) network['port_security_enabled'] = port_security_enabled if mtu_size: if not isinstance(mtu_size, int): raise exc.OpenStackCloudException( - "Parameter 'mtu_size' must be an integer.") + "Parameter 'mtu_size' must be an integer." + ) if not mtu_size >= 68: raise exc.OpenStackCloudException( - "Parameter 'mtu_size' must be greater than 67.") + "Parameter 'mtu_size' must be greater than 67." + ) network['mtu'] = mtu_size @@ -559,9 +568,16 @@ class NetworkCloudMixin: self._reset_network_caches() return network - @_utils.valid_kwargs("name", "shared", "admin_state_up", "external", - "provider", "mtu_size", "port_security_enabled", - "dns_domain") + @_utils.valid_kwargs( + "name", + "shared", + "admin_state_up", + "external", + "provider", + "mtu_size", + "port_security_enabled", + "dns_domain", + ) def update_network(self, name_or_id, **kwargs): """Update a network. @@ -586,9 +602,9 @@ class NetworkCloudMixin: if provider: if not isinstance(provider, dict): raise exc.OpenStackCloudException( - "Parameter 'provider' must be a dict") - for key in ('physical_network', 'network_type', - 'segmentation_id'): + "Parameter 'provider' must be a dict" + ) + for key in ('physical_network', 'network_type', 'segmentation_id'): if key in provider: kwargs['provider:' + key] = provider.pop(key) @@ -598,21 +614,25 @@ class NetworkCloudMixin: if 'port_security_enabled' in kwargs: if not isinstance(kwargs['port_security_enabled'], bool): raise exc.OpenStackCloudException( - "Parameter 'port_security_enabled' must be a bool") + "Parameter 'port_security_enabled' must be a bool" + ) if 'mtu_size' in kwargs: if not isinstance(kwargs['mtu_size'], int): raise exc.OpenStackCloudException( - "Parameter 'mtu_size' must be an integer.") + "Parameter 'mtu_size' must be an integer." + ) if kwargs['mtu_size'] < 68: raise exc.OpenStackCloudException( - "Parameter 'mtu_size' must be greater than 67.") + "Parameter 'mtu_size' must be greater than 67." + ) kwargs['mtu'] = kwargs.pop('mtu_size') network = self.get_network(name_or_id) if not network: raise exc.OpenStackCloudException( - "Network %s not found." % name_or_id) + "Network %s not found." % name_or_id + ) network = self.network.update_network(network, **kwargs) @@ -666,8 +686,7 @@ class NetworkCloudMixin: :raises: OpenStackCloudException if it's not a valid project :returns: A network ``Quota`` object if found, else None. """ - proj = self.identity.find_project( - name_or_id, ignore_missing=False) + proj = self.identity.find_project(name_or_id, ignore_missing=False) return self.network.get_quota(proj.id, details) def get_network_extensions(self): @@ -692,10 +711,21 @@ class NetworkCloudMixin: self.network.delete_quota(proj.id) @_utils.valid_kwargs( - 'action', 'description', 'destination_firewall_group_id', - 'destination_ip_address', 'destination_port', 'enabled', 'ip_version', - 'name', 'project_id', 'protocol', 'shared', 'source_firewall_group_id', - 'source_ip_address', 'source_port') + 'action', + 'description', + 'destination_firewall_group_id', + 'destination_ip_address', + 'destination_port', + 'enabled', + 'ip_version', + 'name', + 'project_id', + 'protocol', + 'shared', + 'source_firewall_group_id', + 'source_ip_address', + 'source_port', + ) def create_firewall_rule(self, **kwargs): """ Creates firewall rule. @@ -753,12 +783,15 @@ class NetworkCloudMixin: filters = {} try: firewall_rule = self.network.find_firewall_rule( - name_or_id, ignore_missing=False, **filters) - self.network.delete_firewall_rule(firewall_rule, - ignore_missing=False) + name_or_id, ignore_missing=False, **filters + ) + self.network.delete_firewall_rule( + firewall_rule, ignore_missing=False + ) except exceptions.ResourceNotFound: - self.log.debug('Firewall rule %s not found for deleting', - name_or_id) + self.log.debug( + 'Firewall rule %s not found for deleting', name_or_id + ) return False return True @@ -789,9 +822,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_firewall_rule( - name_or_id, - ignore_missing=True, - **filters) + name_or_id, ignore_missing=True, **filters + ) def list_firewall_rules(self, filters=None): """ @@ -820,10 +852,21 @@ class NetworkCloudMixin: return list(self.network.firewall_rules(**filters)) @_utils.valid_kwargs( - 'action', 'description', 'destination_firewall_group_id', - 'destination_ip_address', 'destination_port', 'enabled', 'ip_version', - 'name', 'project_id', 'protocol', 'shared', 'source_firewall_group_id', - 'source_ip_address', 'source_port') + 'action', + 'description', + 'destination_firewall_group_id', + 'destination_ip_address', + 'destination_port', + 'enabled', + 'ip_version', + 'name', + 'project_id', + 'protocol', + 'shared', + 'source_firewall_group_id', + 'source_ip_address', + 'source_port', + ) def update_firewall_rule(self, name_or_id, filters=None, **kwargs): """ Updates firewall rule. @@ -853,7 +896,8 @@ class NetworkCloudMixin: if not filters: filters = {} firewall_rule = self.network.find_firewall_rule( - name_or_id, ignore_missing=False, **filters) + name_or_id, ignore_missing=False, **filters + ) return self.network.update_firewall_rule(firewall_rule, **kwargs) @@ -875,12 +919,21 @@ class NetworkCloudMixin: filters = {} ids_list = [] for name_or_id in name_or_id_list: - ids_list.append(self.network.find_firewall_rule( - name_or_id, ignore_missing=False, **filters)['id']) + ids_list.append( + self.network.find_firewall_rule( + name_or_id, ignore_missing=False, **filters + )['id'] + ) return ids_list - @_utils.valid_kwargs('audited', 'description', 'firewall_rules', 'name', - 'project_id', 'shared') + @_utils.valid_kwargs( + 'audited', + 'description', + 'firewall_rules', + 'name', + 'project_id', + 'shared', + ) def create_firewall_policy(self, **kwargs): """ Create firewall policy. @@ -900,7 +953,8 @@ class NetworkCloudMixin: """ if 'firewall_rules' in kwargs: kwargs['firewall_rules'] = self._get_firewall_rule_ids( - kwargs['firewall_rules']) + kwargs['firewall_rules'] + ) return self.network.create_firewall_policy(**kwargs) @@ -933,12 +987,15 @@ class NetworkCloudMixin: filters = {} try: firewall_policy = self.network.find_firewall_policy( - name_or_id, ignore_missing=False, **filters) - self.network.delete_firewall_policy(firewall_policy, - ignore_missing=False) + name_or_id, ignore_missing=False, **filters + ) + self.network.delete_firewall_policy( + firewall_policy, ignore_missing=False + ) except exceptions.ResourceNotFound: - self.log.debug('Firewall policy %s not found for deleting', - name_or_id) + self.log.debug( + 'Firewall policy %s not found for deleting', name_or_id + ) return False return True @@ -969,9 +1026,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_firewall_policy( - name_or_id, - ignore_missing=True, - **filters) + name_or_id, ignore_missing=True, **filters + ) def list_firewall_policies(self, filters=None): """ @@ -999,8 +1055,14 @@ class NetworkCloudMixin: filters = {} return list(self.network.firewall_policies(**filters)) - @_utils.valid_kwargs('audited', 'description', 'firewall_rules', 'name', - 'project_id', 'shared') + @_utils.valid_kwargs( + 'audited', + 'description', + 'firewall_rules', + 'name', + 'project_id', + 'shared', + ) def update_firewall_policy(self, name_or_id, filters=None, **kwargs): """ Updates firewall policy. @@ -1031,17 +1093,24 @@ class NetworkCloudMixin: if not filters: filters = {} firewall_policy = self.network.find_firewall_policy( - name_or_id, ignore_missing=False, **filters) + name_or_id, ignore_missing=False, **filters + ) if 'firewall_rules' in kwargs: kwargs['firewall_rules'] = self._get_firewall_rule_ids( - kwargs['firewall_rules']) + kwargs['firewall_rules'] + ) return self.network.update_firewall_policy(firewall_policy, **kwargs) - def insert_rule_into_policy(self, name_or_id, rule_name_or_id, - insert_after=None, insert_before=None, - filters=None): + def insert_rule_into_policy( + self, + name_or_id, + rule_name_or_id, + insert_after=None, + insert_before=None, + filters=None, + ): """Add firewall rule to a policy. Adds firewall rule to the firewall_rules list of a firewall policy. @@ -1064,33 +1133,40 @@ class NetworkCloudMixin: if not filters: filters = {} firewall_policy = self.network.find_firewall_policy( - name_or_id, ignore_missing=False, **filters) + name_or_id, ignore_missing=False, **filters + ) firewall_rule = self.network.find_firewall_rule( - rule_name_or_id, ignore_missing=False) + rule_name_or_id, ignore_missing=False + ) # short-circuit if rule already in firewall_rules list # the API can't do any re-ordering of existing rules if firewall_rule['id'] in firewall_policy['firewall_rules']: self.log.debug( 'Firewall rule %s already associated with firewall policy %s', - rule_name_or_id, name_or_id) + rule_name_or_id, + name_or_id, + ) return firewall_policy pos_params = {} if insert_after is not None: pos_params['insert_after'] = self.network.find_firewall_rule( - insert_after, ignore_missing=False)['id'] + insert_after, ignore_missing=False + )['id'] if insert_before is not None: pos_params['insert_before'] = self.network.find_firewall_rule( - insert_before, ignore_missing=False)['id'] + insert_before, ignore_missing=False + )['id'] - return self.network.insert_rule_into_policy(firewall_policy['id'], - firewall_rule['id'], - **pos_params) + return self.network.insert_rule_into_policy( + firewall_policy['id'], firewall_rule['id'], **pos_params + ) - def remove_rule_from_policy(self, name_or_id, rule_name_or_id, - filters=None): + def remove_rule_from_policy( + self, name_or_id, rule_name_or_id, filters=None + ): """ Remove firewall rule from firewall policy's firewall_rules list. Short-circuits and returns firewall policy early if firewall rule @@ -1107,14 +1183,16 @@ class NetworkCloudMixin: if not filters: filters = {} firewall_policy = self.network.find_firewall_policy( - name_or_id, ignore_missing=False, **filters) + name_or_id, ignore_missing=False, **filters + ) firewall_rule = self.network.find_firewall_rule(rule_name_or_id) if not firewall_rule: # short-circuit: if firewall rule is not found, # return current firewall policy - self.log.debug('Firewall rule %s not found for removing', - rule_name_or_id) + self.log.debug( + 'Firewall rule %s not found for removing', rule_name_or_id + ) return firewall_policy if firewall_rule['id'] not in firewall_policy['firewall_rules']: @@ -1122,15 +1200,25 @@ class NetworkCloudMixin: # log it to debug and return current firewall policy self.log.debug( 'Firewall rule %s not associated with firewall policy %s', - rule_name_or_id, name_or_id) + rule_name_or_id, + name_or_id, + ) return firewall_policy - return self.network.remove_rule_from_policy(firewall_policy['id'], - firewall_rule['id']) + return self.network.remove_rule_from_policy( + firewall_policy['id'], firewall_rule['id'] + ) @_utils.valid_kwargs( - 'admin_state_up', 'description', 'egress_firewall_policy', - 'ingress_firewall_policy', 'name', 'ports', 'project_id', 'shared') + 'admin_state_up', + 'description', + 'egress_firewall_policy', + 'ingress_firewall_policy', + 'name', + 'ports', + 'project_id', + 'shared', + ) def create_firewall_group(self, **kwargs): """ Creates firewall group. The keys egress_firewall_policy and @@ -1174,12 +1262,15 @@ class NetworkCloudMixin: filters = {} try: firewall_group = self.network.find_firewall_group( - name_or_id, ignore_missing=False, **filters) - self.network.delete_firewall_group(firewall_group, - ignore_missing=False) + name_or_id, ignore_missing=False, **filters + ) + self.network.delete_firewall_group( + firewall_group, ignore_missing=False + ) except exceptions.ResourceNotFound: - self.log.debug('Firewall group %s not found for deleting', - name_or_id) + self.log.debug( + 'Firewall group %s not found for deleting', name_or_id + ) return False return True @@ -1196,9 +1287,8 @@ class NetworkCloudMixin: if not filters: filters = {} return self.network.find_firewall_group( - name_or_id, - ignore_missing=True, - **filters) + name_or_id, ignore_missing=True, **filters + ) def list_firewall_groups(self, filters=None): """ @@ -1211,8 +1301,15 @@ class NetworkCloudMixin: return list(self.network.firewall_groups(**filters)) @_utils.valid_kwargs( - 'admin_state_up', 'description', 'egress_firewall_policy', - 'ingress_firewall_policy', 'name', 'ports', 'project_id', 'shared') + 'admin_state_up', + 'description', + 'egress_firewall_policy', + 'ingress_firewall_policy', + 'name', + 'ports', + 'project_id', + 'shared', + ) def update_firewall_group(self, name_or_id, filters=None, **kwargs): """ Updates firewall group. @@ -1234,7 +1331,8 @@ class NetworkCloudMixin: if not filters: filters = {} firewall_group = self.network.find_firewall_group( - name_or_id, ignore_missing=False, **filters) + name_or_id, ignore_missing=False, **filters + ) self._lookup_ingress_egress_firewall_policy_ids(kwargs) if 'ports' in kwargs: @@ -1260,12 +1358,14 @@ class NetworkCloudMixin: val = None else: val = self.network.find_firewall_policy( - firewall_group[key], ignore_missing=False)['id'] + firewall_group[key], ignore_missing=False + )['id'] firewall_group[key + '_id'] = val del firewall_group[key] - @_utils.valid_kwargs("name", "description", "shared", "default", - "project_id") + @_utils.valid_kwargs( + "name", "description", "shared", "default", "project_id" + ) def create_qos_policy(self, **kwargs): """Create a QoS policy. @@ -1280,20 +1380,24 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) default = kwargs.pop("default", None) if default is not None: if self._has_neutron_extension('qos-default'): kwargs['is_default'] = default else: - self.log.debug("'qos-default' extension is not available on " - "target cloud") + self.log.debug( + "'qos-default' extension is not available on " + "target cloud" + ) return self.network.create_qos_policy(**kwargs) - @_utils.valid_kwargs("name", "description", "shared", "default", - "project_id") + @_utils.valid_kwargs( + "name", "description", "shared", "default", "project_id" + ) def update_qos_policy(self, name_or_id, **kwargs): """Update an existing QoS policy. @@ -1308,15 +1412,18 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) default = kwargs.pop("default", None) if default is not None: if self._has_neutron_extension('qos-default'): kwargs['is_default'] = default else: - self.log.debug("'qos-default' extension is not available on " - "target cloud") + self.log.debug( + "'qos-default' extension is not available on " + "target cloud" + ) if not kwargs: self.log.debug("No QoS policy data to update") @@ -1325,7 +1432,8 @@ class NetworkCloudMixin: curr_policy = self.network.find_qos_policy(name_or_id) if not curr_policy: raise exc.OpenStackCloudException( - "QoS policy %s not found." % name_or_id) + "QoS policy %s not found." % name_or_id + ) return self.network.update_qos_policy(curr_policy, **kwargs) @@ -1340,7 +1448,8 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(name_or_id) if not policy: self.log.debug("QoS policy %s not found for deleting", name_or_id) @@ -1384,20 +1493,26 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) # Translate None from search interface to empty {} for kwargs below if not filters: filters = {} - return list(self.network.qos_bandwidth_limit_rules( - qos_policy=policy, **filters)) + return list( + self.network.qos_bandwidth_limit_rules( + qos_policy=policy, **filters + ) + ) def get_qos_bandwidth_limit_rule(self, policy_name_or_id, rule_id): """Get a QoS bandwidth limit rule by name or ID. @@ -1410,16 +1525,18 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) - return self.network.get_qos_bandwidth_limit_rule( - rule_id, policy) + return self.network.get_qos_bandwidth_limit_rule(rule_id, policy) @_utils.valid_kwargs("max_burst_kbps", "direction") def create_qos_bandwidth_limit_rule( @@ -1442,28 +1559,33 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) if kwargs.get("direction") is not None: if not self._has_neutron_extension('qos-bw-limit-direction'): kwargs.pop("direction") self.log.debug( "'qos-bw-limit-direction' extension is not available on " - "target cloud") + "target cloud" + ) kwargs['max_kbps'] = max_kbps return self.network.create_qos_bandwidth_limit_rule(policy, **kwargs) @_utils.valid_kwargs("max_kbps", "max_burst_kbps", "direction") - def update_qos_bandwidth_limit_rule(self, policy_name_or_id, rule_id, - **kwargs): + def update_qos_bandwidth_limit_rule( + self, policy_name_or_id, rule_id, **kwargs + ): """Update a QoS bandwidth limit rule. :param string policy_name_or_id: Name or ID of the QoS policy to which @@ -1479,37 +1601,43 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy( - policy_name_or_id, - ignore_missing=True) + policy_name_or_id, ignore_missing=True + ) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) if kwargs.get("direction") is not None: if not self._has_neutron_extension('qos-bw-limit-direction'): kwargs.pop("direction") self.log.debug( "'qos-bw-limit-direction' extension is not available on " - "target cloud") + "target cloud" + ) if not kwargs: self.log.debug("No QoS bandwidth limit rule data to update") return curr_rule = self.network.get_qos_bandwidth_limit_rule( - qos_rule=rule_id, qos_policy=policy) + qos_rule=rule_id, qos_policy=policy + ) if not curr_rule: raise exc.OpenStackCloudException( "QoS bandwidth_limit_rule {rule_id} not found in policy " - "{policy_id}".format(rule_id=rule_id, - policy_id=policy['id'])) + "{policy_id}".format(rule_id=rule_id, policy_id=policy['id']) + ) return self.network.update_qos_bandwidth_limit_rule( - qos_rule=curr_rule, qos_policy=policy, **kwargs) + qos_rule=curr_rule, qos_policy=policy, **kwargs + ) def delete_qos_bandwidth_limit_rule(self, policy_name_or_id, rule_id): """Delete a QoS bandwidth limit rule. @@ -1522,22 +1650,28 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) try: self.network.delete_qos_bandwidth_limit_rule( - rule_id, policy, ignore_missing=False) + rule_id, policy, ignore_missing=False + ) except exceptions.ResourceNotFound: self.log.debug( "QoS bandwidth limit rule {rule_id} not found in policy " - "{policy_id}. Ignoring.".format(rule_id=rule_id, - policy_id=policy['id'])) + "{policy_id}. Ignoring.".format( + rule_id=rule_id, policy_id=policy['id'] + ) + ) return False return True @@ -1576,14 +1710,18 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy( - policy_name_or_id, ignore_missing=True) + policy_name_or_id, ignore_missing=True + ) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) # Translate None from search interface to empty {} for kwargs below if not filters: @@ -1601,13 +1739,16 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) return self.network.get_qos_dscp_marking_rule(rule_id, policy) @@ -1626,20 +1767,25 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) return self.network.create_qos_dscp_marking_rule( - policy, dscp_mark=dscp_mark) + policy, dscp_mark=dscp_mark + ) @_utils.valid_kwargs("dscp_mark") - def update_qos_dscp_marking_rule(self, policy_name_or_id, rule_id, - **kwargs): + def update_qos_dscp_marking_rule( + self, policy_name_or_id, rule_id, **kwargs + ): """Update a QoS DSCP marking rule. :param string policy_name_or_id: Name or ID of the QoS policy to which @@ -1651,28 +1797,31 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) if not kwargs: self.log.debug("No QoS DSCP marking rule data to update") return - curr_rule = self.network.get_qos_dscp_marking_rule( - rule_id, policy) + curr_rule = self.network.get_qos_dscp_marking_rule(rule_id, policy) if not curr_rule: raise exc.OpenStackCloudException( "QoS dscp_marking_rule {rule_id} not found in policy " - "{policy_id}".format(rule_id=rule_id, - policy_id=policy['id'])) + "{policy_id}".format(rule_id=rule_id, policy_id=policy['id']) + ) return self.network.update_qos_dscp_marking_rule( - curr_rule, policy, **kwargs) + curr_rule, policy, **kwargs + ) def delete_qos_dscp_marking_rule(self, policy_name_or_id, rule_id): """Delete a QoS DSCP marking rule. @@ -1685,22 +1834,28 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) try: self.network.delete_qos_dscp_marking_rule( - rule_id, policy, ignore_missing=False) + rule_id, policy, ignore_missing=False + ) except exceptions.ResourceNotFound: self.log.debug( "QoS DSCP marking rule {rule_id} not found in policy " - "{policy_id}. Ignoring.".format(rule_id=rule_id, - policy_id=policy['id'])) + "{policy_id}. Ignoring.".format( + rule_id=rule_id, policy_id=policy['id'] + ) + ) return False return True @@ -1725,11 +1880,13 @@ class NetworkCloudMixin: OpenStack API call. """ rules = self.list_qos_minimum_bandwidth_rules( - policy_name_or_id, filters) + policy_name_or_id, filters + ) return _utils._filter_list(rules, rule_id, filters) - def list_qos_minimum_bandwidth_rules(self, policy_name_or_id, - filters=None): + def list_qos_minimum_bandwidth_rules( + self, policy_name_or_id, filters=None + ): """List all available QoS minimum bandwidth rules. :param string policy_name_or_id: Name or ID of the QoS policy from @@ -1741,21 +1898,24 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) # Translate None from search interface to empty {} for kwargs below if not filters: filters = {} return list( - self.network.qos_minimum_bandwidth_rules( - policy, **filters)) + self.network.qos_minimum_bandwidth_rules(policy, **filters) + ) def get_qos_minimum_bandwidth_rule(self, policy_name_or_id, rule_id): """Get a QoS minimum bandwidth rule by name or ID. @@ -1768,13 +1928,16 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) return self.network.get_qos_minimum_bandwidth_rule(rule_id, policy) @@ -1797,13 +1960,16 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) kwargs['min_kbps'] = min_kbps @@ -1826,28 +1992,33 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) if not kwargs: self.log.debug("No QoS minimum bandwidth rule data to update") return curr_rule = self.network.get_qos_minimum_bandwidth_rule( - rule_id, policy) + rule_id, policy + ) if not curr_rule: raise exc.OpenStackCloudException( "QoS minimum_bandwidth_rule {rule_id} not found in policy " - "{policy_id}".format(rule_id=rule_id, - policy_id=policy['id'])) + "{policy_id}".format(rule_id=rule_id, policy_id=policy['id']) + ) return self.network.update_qos_minimum_bandwidth_rule( - curr_rule, policy, **kwargs) + curr_rule, policy, **kwargs + ) def delete_qos_minimum_bandwidth_rule(self, policy_name_or_id, rule_id): """Delete a QoS minimum bandwidth rule. @@ -1860,22 +2031,28 @@ class NetworkCloudMixin: """ if not self._has_neutron_extension('qos'): raise exc.OpenStackCloudUnavailableExtension( - 'QoS extension is not available on target cloud') + 'QoS extension is not available on target cloud' + ) policy = self.network.find_qos_policy(policy_name_or_id) if not policy: raise exc.OpenStackCloudResourceNotFound( "QoS policy {name_or_id} not Found.".format( - name_or_id=policy_name_or_id)) + name_or_id=policy_name_or_id + ) + ) try: self.network.delete_qos_minimum_bandwidth_rule( - rule_id, policy, ignore_missing=False) + rule_id, policy, ignore_missing=False + ) except exceptions.ResourceNotFound: self.log.debug( "QoS minimum bandwidth rule {rule_id} not found in policy " - "{policy_id}. Ignoring.".format(rule_id=rule_id, - policy_id=policy['id'])) + "{policy_id}. Ignoring.".format( + rule_id=rule_id, policy_id=policy['id'] + ) + ) return False return True @@ -1893,9 +2070,7 @@ class NetworkCloudMixin: :raises: OpenStackCloudException on operation error. """ return self.network.add_interface_to_router( - router=router, - subnet_id=subnet_id, - port_id=port_id + router=router, subnet_id=subnet_id, port_id=port_id ) def remove_router_interface(self, router, subnet_id=None, port_id=None): @@ -1917,12 +2092,11 @@ class NetworkCloudMixin: """ if not subnet_id and not port_id: raise ValueError( - "At least one of subnet_id or port_id must be supplied.") + "At least one of subnet_id or port_id must be supplied." + ) self.network.remove_interface_from_router( - router=router, - subnet_id=subnet_id, - port_id=port_id + router=router, subnet_id=subnet_id, port_id=port_id ) def list_router_interfaces(self, router, interface_type=None): @@ -1938,17 +2112,31 @@ class NetworkCloudMixin: ports = list(self.network.ports(device_id=router['id'])) router_interfaces = ( - [port for port in ports - if (port['device_owner'] in - ['network:router_interface', - 'network:router_interface_distributed', - 'network:ha_router_replicated_interface']) - ] if not interface_type or interface_type == 'internal' else []) + [ + port + for port in ports + if ( + port['device_owner'] + in [ + 'network:router_interface', + 'network:router_interface_distributed', + 'network:ha_router_replicated_interface', + ] + ) + ] + if not interface_type or interface_type == 'internal' + else [] + ) router_gateways = ( - [port for port in ports - if port['device_owner'] == 'network:router_gateway' - ] if not interface_type or interface_type == 'external' else []) + [ + port + for port in ports + if port['device_owner'] == 'network:router_gateway' + ] + if not interface_type or interface_type == 'external' + else [] + ) return router_interfaces + router_gateways @@ -1985,9 +2173,7 @@ class NetworkCloudMixin: :returns: The created network ``Router`` object. :raises: OpenStackCloudException on operation error. """ - router = { - 'admin_state_up': admin_state_up - } + router = {'admin_state_up': admin_state_up} if project_id is not None: router['project_id'] = project_id if name: @@ -2000,18 +2186,27 @@ class NetworkCloudMixin: if availability_zone_hints is not None: if not isinstance(availability_zone_hints, list): raise exc.OpenStackCloudException( - "Parameter 'availability_zone_hints' must be a list") + "Parameter 'availability_zone_hints' must be a list" + ) if not self._has_neutron_extension('router_availability_zone'): raise exc.OpenStackCloudUnavailableExtension( 'router_availability_zone extension is not available on ' - 'target cloud') + 'target cloud' + ) router['availability_zone_hints'] = availability_zone_hints return self.network.create_router(**router) - def update_router(self, name_or_id, name=None, admin_state_up=None, - ext_gateway_net_id=None, enable_snat=None, - ext_fixed_ips=None, routes=None): + def update_router( + self, + name_or_id, + name=None, + admin_state_up=None, + ext_gateway_net_id=None, + enable_snat=None, + ext_fixed_ips=None, + routes=None, + ): """Update an existing logical router. :param string name_or_id: The name or UUID of the router to update. @@ -2063,7 +2258,8 @@ class NetworkCloudMixin: router['routes'] = routes else: self.log.warning( - 'extra routes extension is not available on target cloud') + 'extra routes extension is not available on target cloud' + ) if not router: self.log.debug("No router data to update") @@ -2072,7 +2268,8 @@ class NetworkCloudMixin: curr_router = self.get_router(name_or_id) if not curr_router: raise exc.OpenStackCloudException( - "Router %s not found." % name_or_id) + "Router %s not found." % name_or_id + ) return self.network.update_router(curr_router, **router) @@ -2186,20 +2383,24 @@ class NetworkCloudMixin: network = self.get_network(network_name_or_id, filters) if not network: raise exc.OpenStackCloudException( - "Network %s not found." % network_name_or_id) + "Network %s not found." % network_name_or_id + ) if disable_gateway_ip and gateway_ip: raise exc.OpenStackCloudException( - 'arg:disable_gateway_ip is not allowed with arg:gateway_ip') + 'arg:disable_gateway_ip is not allowed with arg:gateway_ip' + ) if not cidr and not use_default_subnetpool: raise exc.OpenStackCloudException( - 'arg:cidr is required when a subnetpool is not used') + 'arg:cidr is required when a subnetpool is not used' + ) if cidr and use_default_subnetpool: raise exc.OpenStackCloudException( 'arg:cidr must be set to None when use_default_subnetpool == ' - 'True') + 'True' + ) # Be friendly on ip_version and allow strings if isinstance(ip_version, str): @@ -2207,15 +2408,19 @@ class NetworkCloudMixin: ip_version = int(ip_version) except ValueError: raise exc.OpenStackCloudException( - 'ip_version must be an integer') + 'ip_version must be an integer' + ) # The body of the neutron message for the subnet we wish to create. # This includes attributes that are required or have defaults. - subnet = dict({ - 'network_id': network['id'], - 'ip_version': ip_version, - 'enable_dhcp': enable_dhcp, - }, **kwargs) + subnet = dict( + { + 'network_id': network['id'], + 'ip_version': ip_version, + 'enable_dhcp': enable_dhcp, + }, + **kwargs, + ) # Add optional attributes to the message. if cidr: @@ -2267,10 +2472,17 @@ class NetworkCloudMixin: return True - def update_subnet(self, name_or_id, subnet_name=None, enable_dhcp=None, - gateway_ip=None, disable_gateway_ip=None, - allocation_pools=None, dns_nameservers=None, - host_routes=None): + def update_subnet( + self, + name_or_id, + subnet_name=None, + enable_dhcp=None, + gateway_ip=None, + disable_gateway_ip=None, + allocation_pools=None, + dns_nameservers=None, + host_routes=None, + ): """Update an existing subnet. :param string name_or_id: Name or ID of the subnet to update. @@ -2337,24 +2549,42 @@ class NetworkCloudMixin: if disable_gateway_ip and gateway_ip: raise exc.OpenStackCloudException( - 'arg:disable_gateway_ip is not allowed with arg:gateway_ip') + 'arg:disable_gateway_ip is not allowed with arg:gateway_ip' + ) curr_subnet = self.get_subnet(name_or_id) if not curr_subnet: raise exc.OpenStackCloudException( - "Subnet %s not found." % name_or_id) + "Subnet %s not found." % name_or_id + ) return self.network.update_subnet(curr_subnet, **subnet) - @_utils.valid_kwargs('name', 'admin_state_up', 'mac_address', 'fixed_ips', - 'subnet_id', 'ip_address', 'security_groups', - 'allowed_address_pairs', 'extra_dhcp_opts', - 'device_owner', 'device_id', 'binding:vnic_type', - 'binding:profile', 'port_security_enabled', - 'qos_policy_id', 'binding:host_id', 'project_id', - 'description', 'dns_domain', 'dns_name', - 'numa_affinity_policy', 'propagate_uplink_status', - 'mac_learning_enabled') + @_utils.valid_kwargs( + 'name', + 'admin_state_up', + 'mac_address', + 'fixed_ips', + 'subnet_id', + 'ip_address', + 'security_groups', + 'allowed_address_pairs', + 'extra_dhcp_opts', + 'device_owner', + 'device_id', + 'binding:vnic_type', + 'binding:profile', + 'port_security_enabled', + 'qos_policy_id', + 'binding:host_id', + 'project_id', + 'description', + 'dns_domain', + 'dns_name', + 'numa_affinity_policy', + 'propagate_uplink_status', + 'mac_learning_enabled', + ) def create_port(self, network_id, **kwargs): """Create a port @@ -2427,12 +2657,21 @@ class NetworkCloudMixin: return self.network.create_port(**kwargs) - @_utils.valid_kwargs('name', 'admin_state_up', 'fixed_ips', - 'security_groups', 'allowed_address_pairs', - 'extra_dhcp_opts', 'device_owner', 'device_id', - 'binding:vnic_type', 'binding:profile', - 'port_security_enabled', 'qos_policy_id', - 'binding:host_id') + @_utils.valid_kwargs( + 'name', + 'admin_state_up', + 'fixed_ips', + 'security_groups', + 'allowed_address_pairs', + 'extra_dhcp_opts', + 'device_owner', + 'device_id', + 'binding:vnic_type', + 'binding:profile', + 'port_security_enabled', + 'qos_policy_id', + 'binding:host_id', + ) def update_port(self, name_or_id, **kwargs): """Update a port @@ -2491,7 +2730,8 @@ class NetworkCloudMixin: port = self.get_port(name_or_id=name_or_id) if port is None: raise exc.OpenStackCloudException( - "failed to find port '{port}'".format(port=name_or_id)) + "failed to find port '{port}'".format(port=name_or_id) + ) return self.network.update_port(port, **kwargs) @@ -2531,12 +2771,14 @@ class NetworkCloudMixin: port = self.get_port(name_or_id, filters) if not port: raise exceptions.ResourceNotFound( - 'Port {id} not found'.format(id=name_or_id)) + 'Port {id} not found'.format(id=name_or_id) + ) ids_list.append(port['id']) return ids_list - def _build_external_gateway_info(self, ext_gateway_net_id, enable_snat, - ext_fixed_ips): + def _build_external_gateway_info( + self, ext_gateway_net_id, enable_snat, ext_fixed_ips + ): info = {} if ext_gateway_net_id: info['network_id'] = ext_gateway_net_id diff --git a/openstack/cloud/_network_common.py b/openstack/cloud/_network_common.py index ed6e4b969..aa774345e 100644 --- a/openstack/cloud/_network_common.py +++ b/openstack/cloud/_network_common.py @@ -20,8 +20,8 @@ from openstack.cloud import exc class NetworkCommonCloudMixin: - """Shared networking functions used by FloatingIP, Network, Compute classes - """ + """Shared networking functions used by FloatingIP, Network, Compute + classes.""" def __init__(self): self._external_ipv4_names = self.config.get_external_ipv4_networks() @@ -33,9 +33,11 @@ class NetworkCommonCloudMixin: self._default_network = self.config.get_default_network() self._use_external_network = self.config.config.get( - 'use_external_network', True) + 'use_external_network', True + ) self._use_internal_network = self.config.config.get( - 'use_internal_network', True) + 'use_internal_network', True + ) self._networks_lock = threading.Lock() self._reset_network_caches() @@ -90,46 +92,63 @@ class NetworkCommonCloudMixin: for network in all_networks: # External IPv4 networks - if (network['name'] in self._external_ipv4_names - or network['id'] in self._external_ipv4_names): + if ( + network['name'] in self._external_ipv4_names + or network['id'] in self._external_ipv4_names + ): external_ipv4_networks.append(network) - elif ((network.is_router_external - or network.provider_physical_network) - and network['name'] not in self._internal_ipv4_names - and network['id'] not in self._internal_ipv4_names): + elif ( + ( + network.is_router_external + or network.provider_physical_network + ) + and network['name'] not in self._internal_ipv4_names + and network['id'] not in self._internal_ipv4_names + ): external_ipv4_networks.append(network) # Internal networks - if (network['name'] in self._internal_ipv4_names - or network['id'] in self._internal_ipv4_names): + if ( + network['name'] in self._internal_ipv4_names + or network['id'] in self._internal_ipv4_names + ): internal_ipv4_networks.append(network) - elif (not network.is_router_external - and not network.provider_physical_network - and network['name'] not in self._external_ipv4_names - and network['id'] not in self._external_ipv4_names): + elif ( + not network.is_router_external + and not network.provider_physical_network + and network['name'] not in self._external_ipv4_names + and network['id'] not in self._external_ipv4_names + ): internal_ipv4_networks.append(network) # External networks - if (network['name'] in self._external_ipv6_names - or network['id'] in self._external_ipv6_names): + if ( + network['name'] in self._external_ipv6_names + or network['id'] in self._external_ipv6_names + ): external_ipv6_networks.append(network) - elif (network.is_router_external - and network['name'] not in self._internal_ipv6_names - and network['id'] not in self._internal_ipv6_names): + elif ( + network.is_router_external + and network['name'] not in self._internal_ipv6_names + and network['id'] not in self._internal_ipv6_names + ): external_ipv6_networks.append(network) # Internal networks - if (network['name'] in self._internal_ipv6_names - or network['id'] in self._internal_ipv6_names): + if ( + network['name'] in self._internal_ipv6_names + or network['id'] in self._internal_ipv6_names + ): internal_ipv6_networks.append(network) - elif (not network.is_router_external - and network['name'] not in self._external_ipv6_names - and network['id'] not in self._external_ipv6_names): + elif ( + not network.is_router_external + and network['name'] not in self._external_ipv6_names + and network['id'] not in self._external_ipv6_names + ): internal_ipv6_networks.append(network) # External Floating IPv4 networks - if self._nat_source in ( - network['name'], network['id']): + if self._nat_source in (network['name'], network['id']): if nat_source: raise exc.OpenStackCloudException( 'Multiple networks were found matching' @@ -137,8 +156,8 @@ class NetworkCommonCloudMixin: ' to be the NAT source. Please check your' ' cloud resources. It is probably a good idea' ' to configure this network by ID rather than' - ' by name.'.format( - nat_net=self._nat_source)) + ' by name.'.format(nat_net=self._nat_source) + ) external_ipv4_floating_networks.append(network) nat_source = network elif self._nat_source is None: @@ -147,8 +166,7 @@ class NetworkCommonCloudMixin: nat_source = nat_source or network # NAT Destination - if self._nat_destination in ( - network['name'], network['id']): + if self._nat_destination in (network['name'], network['id']): if nat_destination: raise exc.OpenStackCloudException( 'Multiple networks were found matching' @@ -156,8 +174,8 @@ class NetworkCommonCloudMixin: ' to be the NAT destination. Please check your' ' cloud resources. It is probably a good idea' ' to configure this network by ID rather than' - ' by name.'.format( - nat_net=self._nat_destination)) + ' by name.'.format(nat_net=self._nat_destination) + ) nat_destination = network elif self._nat_destination is None: # TODO(mordred) need a config value for floating @@ -174,14 +192,16 @@ class NetworkCommonCloudMixin: for subnet in all_subnets: # TODO(mordred) trap for detecting more than # one network with a gateway_ip without a config - if ('gateway_ip' in subnet and subnet['gateway_ip'] - and network['id'] == subnet['network_id']): + if ( + 'gateway_ip' in subnet + and subnet['gateway_ip'] + and network['id'] == subnet['network_id'] + ): nat_destination = network break # Default network - if self._default_network in ( - network['name'], network['id']): + if self._default_network in (network['name'], network['id']): if default_network: raise exc.OpenStackCloudException( 'Multiple networks were found matching' @@ -190,8 +210,8 @@ class NetworkCommonCloudMixin: ' network. Please check your cloud resources.' ' It is probably a good idea' ' to configure this network by ID rather than' - ' by name.'.format( - default_net=self._default_network)) + ' by name.'.format(default_net=self._default_network) + ) default_network = network # Validate config vs. reality @@ -200,49 +220,57 @@ class NetworkCommonCloudMixin: raise exc.OpenStackCloudException( "Networks: {network} was provided for external IPv4" " access and those networks could not be found".format( - network=net_name)) + network=net_name + ) + ) for net_name in self._internal_ipv4_names: if net_name not in [net['name'] for net in internal_ipv4_networks]: raise exc.OpenStackCloudException( "Networks: {network} was provided for internal IPv4" " access and those networks could not be found".format( - network=net_name)) + network=net_name + ) + ) for net_name in self._external_ipv6_names: if net_name not in [net['name'] for net in external_ipv6_networks]: raise exc.OpenStackCloudException( "Networks: {network} was provided for external IPv6" " access and those networks could not be found".format( - network=net_name)) + network=net_name + ) + ) for net_name in self._internal_ipv6_names: if net_name not in [net['name'] for net in internal_ipv6_networks]: raise exc.OpenStackCloudException( "Networks: {network} was provided for internal IPv6" " access and those networks could not be found".format( - network=net_name)) + network=net_name + ) + ) if self._nat_destination and not nat_destination: raise exc.OpenStackCloudException( 'Network {network} was configured to be the' ' destination for inbound NAT but it could not be' - ' found'.format( - network=self._nat_destination)) + ' found'.format(network=self._nat_destination) + ) if self._nat_source and not nat_source: raise exc.OpenStackCloudException( 'Network {network} was configured to be the' ' source for inbound NAT but it could not be' - ' found'.format( - network=self._nat_source)) + ' found'.format(network=self._nat_source) + ) if self._default_network and not default_network: raise exc.OpenStackCloudException( 'Network {network} was configured to be the' ' default network interface but it could not be' - ' found'.format( - network=self._default_network)) + ' found'.format(network=self._default_network) + ) self._external_ipv4_networks = external_ipv4_networks self._external_ipv4_floating_networks = external_ipv4_floating_networks @@ -304,9 +332,8 @@ class NetworkCommonCloudMixin: :returns: A list of network ``Network`` objects if any are found """ self._find_interesting_networks() - return ( - list(self._external_ipv4_networks) - + list(self._external_ipv6_networks) + return list(self._external_ipv4_networks) + list( + self._external_ipv6_networks ) def get_internal_networks(self): @@ -318,9 +345,8 @@ class NetworkCommonCloudMixin: :returns: A list of network ``Network`` objects if any are found """ self._find_interesting_networks() - return ( - list(self._internal_ipv4_networks) - + list(self._internal_ipv6_networks) + return list(self._internal_ipv4_networks) + list( + self._internal_ipv6_networks ) def get_external_ipv4_networks(self): diff --git a/openstack/cloud/_object_store.py b/openstack/cloud/_object_store.py index d4a392af1..945d4e00f 100644 --- a/openstack/cloud/_object_store.py +++ b/openstack/cloud/_object_store.py @@ -105,9 +105,7 @@ class ObjectStoreCloudMixin: container = self.get_container(name) if container: return container - attrs = dict( - name=name - ) + attrs = dict(name=name) if public: attrs['read_ACL'] = OBJECT_CONTAINER_ACLS['public'] container = self.object_store.create_container(**attrs) @@ -129,7 +127,9 @@ class ObjectStoreCloudMixin: 'Attempt to delete container {container} failed. The' ' container is not empty. Please delete the objects' ' inside it before deleting the container'.format( - container=name)) + container=name + ) + ) def update_container(self, name, headers): """Update the metadata in a container. @@ -138,7 +138,8 @@ class ObjectStoreCloudMixin: :param dict headers: Key/Value headers to set on the container. """ self.object_store.set_container_metadata( - name, refresh=False, **headers) + name, refresh=False, **headers + ) def set_container_access(self, name, access, refresh=False): """Set the access control list on a container. @@ -152,11 +153,10 @@ class ObjectStoreCloudMixin: if access not in OBJECT_CONTAINER_ACLS: raise exc.OpenStackCloudException( "Invalid container access specified: %s. Must be one of %s" - % (access, list(OBJECT_CONTAINER_ACLS.keys()))) + % (access, list(OBJECT_CONTAINER_ACLS.keys())) + ) return self.object_store.set_container_metadata( - name, - read_ACL=OBJECT_CONTAINER_ACLS[access], - refresh=refresh + name, read_ACL=OBJECT_CONTAINER_ACLS[access], refresh=refresh ) def get_container_access(self, name): @@ -179,7 +179,8 @@ class ObjectStoreCloudMixin: if str(acl) == str(value): return key raise exc.OpenStackCloudException( - "Could not determine container access for ACL: %s." % acl) + "Could not determine container access for ACL: %s." % acl + ) @_utils.cache_on_arguments() def get_object_capabilities(self): @@ -201,7 +202,8 @@ class ObjectStoreCloudMixin: return self.object_store.get_object_segment_size(segment_size) def is_object_stale( - self, container, name, filename, file_md5=None, file_sha256=None): + self, container, name, filename, file_md5=None, file_sha256=None + ): """Check to see if an object matches the hashes of a file. :param container: Name of the container. @@ -213,8 +215,11 @@ class ObjectStoreCloudMixin: Defaults to None which means calculate locally. """ return self.object_store.is_object_stale( - container, name, filename, - file_md5=file_md5, file_sha256=file_sha256 + container, + name, + filename, + file_md5=file_md5, + file_sha256=file_sha256, ) def create_directory_marker_object(self, container, name, **headers): @@ -241,11 +246,8 @@ class ObjectStoreCloudMixin: headers['content-type'] = 'application/directory' return self.create_object( - container, - name, - data='', - generate_checksums=False, - **headers) + container, name, data='', generate_checksums=False, **headers + ) def create_object( self, @@ -295,12 +297,16 @@ class ObjectStoreCloudMixin: :raises: ``OpenStackCloudException`` on operation error. """ return self.object_store.create_object( - container, name, - filename=filename, data=data, - md5=md5, sha256=sha256, use_slo=use_slo, + container, + name, + filename=filename, + data=data, + md5=md5, + sha256=sha256, + use_slo=use_slo, generate_checksums=generate_checksums, metadata=metadata, - **headers + **headers, ) def update_object(self, container, name, metadata=None, **headers): @@ -317,8 +323,7 @@ class ObjectStoreCloudMixin: """ meta = metadata.copy() or {} meta.update(**headers) - self.object_store.set_object_metadata( - name, container, **meta) + self.object_store.set_object_metadata(name, container, **meta) def list_objects(self, container, full_listing=True, prefix=None): """List objects. @@ -330,10 +335,9 @@ class ObjectStoreCloudMixin: :returns: A list of object store ``Object`` objects. :raises: OpenStackCloudException on operation error. """ - return list(self.object_store.objects( - container=container, - prefix=prefix - )) + return list( + self.object_store.objects(container=container, prefix=prefix) + ) def search_objects(self, container, name=None, filters=None): """Search objects. @@ -364,7 +368,9 @@ class ObjectStoreCloudMixin: """ try: self.object_store.delete_object( - name, ignore_missing=False, container=container, + name, + ignore_missing=False, + container=container, ) return True except exceptions.SDKException: @@ -400,9 +406,7 @@ class ObjectStoreCloudMixin: :param name: :returns: The object metadata. """ - return self.object_store.get_object_metadata( - name, container - ).metadata + return self.object_store.get_object_metadata(name, container).metadata def get_object_raw(self, container, obj, query_string=None, stream=False): """Get a raw response object for an object. @@ -422,12 +426,12 @@ class ObjectStoreCloudMixin: endpoint = urllib.parse.quote(container) if obj: endpoint = '{endpoint}/{object}'.format( - endpoint=endpoint, - object=urllib.parse.quote(obj) + endpoint=endpoint, object=urllib.parse.quote(obj) ) if query_string: endpoint = '{endpoint}?{query_string}'.format( - endpoint=endpoint, query_string=query_string) + endpoint=endpoint, query_string=query_string + ) return endpoint def stream_object( @@ -451,13 +455,21 @@ class ObjectStoreCloudMixin: """ try: for ret in self.object_store.stream_object( - obj, container, chunk_size=resp_chunk_size): + obj, container, chunk_size=resp_chunk_size + ): yield ret except exceptions.ResourceNotFound: return - def get_object(self, container, obj, query_string=None, - resp_chunk_size=1024, outfile=None, stream=False): + def get_object( + self, + container, + obj, + query_string=None, + resp_chunk_size=1024, + outfile=None, + stream=False, + ): """Get the headers and body of an object :param string container: Name of the container. @@ -477,13 +489,13 @@ class ObjectStoreCloudMixin: """ try: obj = self.object_store.get_object( - obj, container=container, + obj, + container=container, resp_chunk_size=resp_chunk_size, outfile=outfile, - remember_content=(outfile is None) + remember_content=(outfile is None), ) - headers = { - k.lower(): v for k, v in obj._last_headers.items()} + headers = {k.lower(): v for k, v in obj._last_headers.items()} return (headers, obj.data) except exceptions.ResourceNotFound: @@ -500,10 +512,13 @@ class ObjectStoreCloudMixin: result = completed.result() exceptions.raise_from_response(result) results.append(result) - except (keystoneauth1.exceptions.RetriableConnectionFailure, - exceptions.HttpException) as e: + except ( + keystoneauth1.exceptions.RetriableConnectionFailure, + exceptions.HttpException, + ) as e: error_text = "Exception processing async task: {}".format( - str(e)) + str(e) + ) if raise_on_error: self.log.exception(error_text) raise diff --git a/openstack/cloud/_orchestration.py b/openstack/cloud/_orchestration.py index 0aaa27cdd..451f1ab77 100644 --- a/openstack/cloud/_orchestration.py +++ b/openstack/cloud/_orchestration.py @@ -41,20 +41,33 @@ class OrchestrationCloudMixin: return self._raw_clients['orchestration'] def get_template_contents( - self, template_file=None, template_url=None, - template_object=None, files=None): + self, + template_file=None, + template_url=None, + template_object=None, + files=None, + ): return self.orchestration.get_template_contents( - template_file=template_file, template_url=template_url, - template_object=template_object, files=files) + template_file=template_file, + template_url=template_url, + template_object=template_object, + files=files, + ) def create_stack( - self, name, tags=None, - template_file=None, template_url=None, - template_object=None, files=None, - rollback=True, - wait=False, timeout=3600, - environment_files=None, - **parameters): + self, + name, + tags=None, + template_file=None, + template_url=None, + template_object=None, + files=None, + rollback=True, + wait=False, + timeout=3600, + environment_files=None, + **parameters + ): """Create a stack. :param string name: Name of the stack. @@ -83,27 +96,36 @@ class OrchestrationCloudMixin: tags=tags, is_rollback_disabled=not rollback, timeout_mins=timeout // 60, - parameters=parameters + parameters=parameters, + ) + params.update( + self.orchestration.read_env_and_templates( + template_file=template_file, + template_url=template_url, + template_object=template_object, + files=files, + environment_files=environment_files, + ) ) - params.update(self.orchestration.read_env_and_templates( - template_file=template_file, template_url=template_url, - template_object=template_object, files=files, - environment_files=environment_files - )) self.orchestration.create_stack(name=name, **params) if wait: - event_utils.poll_for_events(self, stack_name=name, - action='CREATE') + event_utils.poll_for_events(self, stack_name=name, action='CREATE') return self.get_stack(name) def update_stack( - self, name_or_id, - template_file=None, template_url=None, - template_object=None, files=None, - rollback=True, tags=None, - wait=False, timeout=3600, - environment_files=None, - **parameters): + self, + name_or_id, + template_file=None, + template_url=None, + template_object=None, + files=None, + rollback=True, + tags=None, + wait=False, + timeout=3600, + environment_files=None, + **parameters + ): """Update a stack. :param string name_or_id: Name or ID of the stack to update. @@ -131,27 +153,31 @@ class OrchestrationCloudMixin: tags=tags, is_rollback_disabled=not rollback, timeout_mins=timeout // 60, - parameters=parameters + parameters=parameters, + ) + params.update( + self.orchestration.read_env_and_templates( + template_file=template_file, + template_url=template_url, + template_object=template_object, + files=files, + environment_files=environment_files, + ) ) - params.update(self.orchestration.read_env_and_templates( - template_file=template_file, template_url=template_url, - template_object=template_object, files=files, - environment_files=environment_files - )) if wait: # find the last event to use as the marker events = event_utils.get_events( - self, name_or_id, event_args={'sort_dir': 'desc', 'limit': 1}) + self, name_or_id, event_args={'sort_dir': 'desc', 'limit': 1} + ) marker = events[0].id if events else None # Not to cause update of ID field pass stack as dict self.orchestration.update_stack(stack={'id': name_or_id}, **params) if wait: - event_utils.poll_for_events(self, - name_or_id, - action='UPDATE', - marker=marker) + event_utils.poll_for_events( + self, name_or_id, action='UPDATE', marker=marker + ) return self.get_stack(name_or_id) def delete_stack(self, name_or_id, wait=False): @@ -173,24 +199,26 @@ class OrchestrationCloudMixin: if wait: # find the last event to use as the marker events = event_utils.get_events( - self, name_or_id, event_args={'sort_dir': 'desc', 'limit': 1}) + self, name_or_id, event_args={'sort_dir': 'desc', 'limit': 1} + ) marker = events[0].id if events else None self.orchestration.delete_stack(stack) if wait: try: - event_utils.poll_for_events(self, - stack_name=name_or_id, - action='DELETE', - marker=marker) + event_utils.poll_for_events( + self, stack_name=name_or_id, action='DELETE', marker=marker + ) except exc.OpenStackCloudHTTPError: pass stack = self.get_stack(name_or_id, resolve_outputs=False) if stack and stack['stack_status'] == 'DELETE_FAILED': raise exc.OpenStackCloudException( "Failed to delete stack {id}: {reason}".format( - id=name_or_id, reason=stack['stack_status_reason'])) + id=name_or_id, reason=stack['stack_status_reason'] + ) + ) return True @@ -246,12 +274,12 @@ class OrchestrationCloudMixin: stack = self.orchestration.find_stack( name_or_id, ignore_missing=False, - resolve_outputs=resolve_outputs) + resolve_outputs=resolve_outputs, + ) if stack.status == 'DELETE_COMPLETE': return [] except exc.OpenStackCloudURINotFound: return [] return _utils._filter_list([stack], name_or_id, filters) - return _utils._get_entity( - self, _search_one_stack, name_or_id, filters) + return _utils._get_entity(self, _search_one_stack, name_or_id, filters) diff --git a/openstack/cloud/_security_group.py b/openstack/cloud/_security_group.py index b0bf33185..08d56e90c 100644 --- a/openstack/cloud/_security_group.py +++ b/openstack/cloud/_security_group.py @@ -59,15 +59,16 @@ class SecurityGroupCloudMixin: if self._use_neutron_secgroups(): # pass filters dict to the list to filter as much as possible on # the server side - return list( - self.network.security_groups(**filters)) + return list(self.network.security_groups(**filters)) # Handle nova security groups else: - data = proxy._json_response(self.compute.get( - '/os-security-groups', params=filters)) + data = proxy._json_response( + self.compute.get('/os-security-groups', params=filters) + ) return self._normalize_secgroups( - self._get_and_munchify('security_groups', data)) + self._get_and_munchify('security_groups', data) + ) def get_security_group(self, name_or_id, filters=None): """Get a security group by name or ID. @@ -93,11 +94,10 @@ class SecurityGroupCloudMixin: or None if no matching security group is found. """ - return _utils._get_entity( - self, 'security_group', name_or_id, filters) + return _utils._get_entity(self, 'security_group', name_or_id, filters) def get_security_group_by_id(self, id): - """ Get a security group by ID + """Get a security group by ID :param id: ID of the security group. :returns: A security group @@ -107,20 +107,23 @@ class SecurityGroupCloudMixin: raise exc.OpenStackCloudUnavailableFeature( "Unavailable feature: security groups" ) - error_message = ("Error getting security group with" - " ID {id}".format(id=id)) + error_message = "Error getting security group with" " ID {id}".format( + id=id + ) if self._use_neutron_secgroups(): return self.network.get_security_group(id) else: data = proxy._json_response( - self.compute.get( - '/os-security-groups/{id}'.format(id=id)), - error_message=error_message) + self.compute.get('/os-security-groups/{id}'.format(id=id)), + error_message=error_message, + ) return self._normalize_secgroup( - self._get_and_munchify('security_group', data)) + self._get_and_munchify('security_group', data) + ) - def create_security_group(self, name, description, - project_id=None, stateful=None): + def create_security_group( + self, name, description, project_id=None, stateful=None + ): """Create a new security group :param string name: A name for the security group. @@ -145,22 +148,23 @@ class SecurityGroupCloudMixin: ) data = [] - security_group_json = { - 'name': name, 'description': description - } + security_group_json = {'name': name, 'description': description} if stateful is not None: security_group_json['stateful'] = stateful if project_id is not None: security_group_json['tenant_id'] = project_id if self._use_neutron_secgroups(): - return self.network.create_security_group( - **security_group_json) + return self.network.create_security_group(**security_group_json) else: - data = proxy._json_response(self.compute.post( - '/os-security-groups', - json={'security_group': security_group_json})) + data = proxy._json_response( + self.compute.post( + '/os-security-groups', + json={'security_group': security_group_json}, + ) + ) return self._normalize_secgroup( - self._get_and_munchify('security_group', data)) + self._get_and_munchify('security_group', data) + ) def delete_security_group(self, name_or_id): """Delete a security group @@ -183,18 +187,23 @@ class SecurityGroupCloudMixin: # the delete. secgroup = self.get_security_group(name_or_id) if secgroup is None: - self.log.debug('Security group %s not found for deleting', - name_or_id) + self.log.debug( + 'Security group %s not found for deleting', name_or_id + ) return False if self._use_neutron_secgroups(): self.network.delete_security_group( - secgroup['id'], ignore_missing=False) + secgroup['id'], ignore_missing=False + ) return True else: - proxy._json_response(self.compute.delete( - '/os-security-groups/{id}'.format(id=secgroup['id']))) + proxy._json_response( + self.compute.delete( + '/os-security-groups/{id}'.format(id=secgroup['id']) + ) + ) return True @_utils.valid_kwargs('name', 'description', 'stateful') @@ -220,35 +229,38 @@ class SecurityGroupCloudMixin: if group is None: raise exc.OpenStackCloudException( - "Security group %s not found." % name_or_id) + "Security group %s not found." % name_or_id + ) if self._use_neutron_secgroups(): - return self.network.update_security_group( - group['id'], - **kwargs - ) + return self.network.update_security_group(group['id'], **kwargs) else: for key in ('name', 'description'): kwargs.setdefault(key, group[key]) data = proxy._json_response( self.compute.put( '/os-security-groups/{id}'.format(id=group['id']), - json={'security_group': kwargs})) + json={'security_group': kwargs}, + ) + ) return self._normalize_secgroup( - self._get_and_munchify('security_group', data)) + self._get_and_munchify('security_group', data) + ) - def create_security_group_rule(self, - secgroup_name_or_id, - port_range_min=None, - port_range_max=None, - protocol=None, - remote_ip_prefix=None, - remote_group_id=None, - remote_address_group_id=None, - direction='ingress', - ethertype='IPv4', - project_id=None, - description=None): + def create_security_group_rule( + self, + secgroup_name_or_id, + port_range_min=None, + port_range_max=None, + protocol=None, + remote_ip_prefix=None, + remote_group_id=None, + remote_address_group_id=None, + direction='ingress', + ethertype='IPv4', + project_id=None, + description=None, + ): """Create a new security group rule :param string secgroup_name_or_id: @@ -308,31 +320,32 @@ class SecurityGroupCloudMixin: secgroup = self.get_security_group(secgroup_name_or_id) if not secgroup: raise exc.OpenStackCloudException( - "Security group %s not found." % secgroup_name_or_id) + "Security group %s not found." % secgroup_name_or_id + ) if self._use_neutron_secgroups(): # NOTE: Nova accepts -1 port numbers, but Neutron accepts None # as the equivalent value. rule_def = { 'security_group_id': secgroup['id'], - 'port_range_min': - None if port_range_min == -1 else port_range_min, - 'port_range_max': - None if port_range_max == -1 else port_range_max, + 'port_range_min': None + if port_range_min == -1 + else port_range_min, + 'port_range_max': None + if port_range_max == -1 + else port_range_max, 'protocol': protocol, 'remote_ip_prefix': remote_ip_prefix, 'remote_group_id': remote_group_id, 'remote_address_group_id': remote_address_group_id, 'direction': direction, - 'ethertype': ethertype + 'ethertype': ethertype, } if project_id is not None: rule_def['tenant_id'] = project_id if description is not None: rule_def["description"] = description - return self.network.create_security_group_rule( - **rule_def - ) + return self.network.create_security_group_rule(**rule_def) else: # NOTE: Neutron accepts None for protocol. Nova does not. if protocol is None: @@ -343,7 +356,8 @@ class SecurityGroupCloudMixin: 'Rule creation failed: Nova does not support egress rules' ) raise exc.OpenStackCloudException( - 'No support for egress rules') + 'No support for egress rules' + ) # NOTE: Neutron accepts None for ports, but Nova requires -1 # as the equivalent value for ICMP. @@ -363,24 +377,28 @@ class SecurityGroupCloudMixin: port_range_min = 1 port_range_max = 65535 - security_group_rule_dict = dict(security_group_rule=dict( - parent_group_id=secgroup['id'], - ip_protocol=protocol, - from_port=port_range_min, - to_port=port_range_max, - cidr=remote_ip_prefix, - group_id=remote_group_id - )) + security_group_rule_dict = dict( + security_group_rule=dict( + parent_group_id=secgroup['id'], + ip_protocol=protocol, + from_port=port_range_min, + to_port=port_range_max, + cidr=remote_ip_prefix, + group_id=remote_group_id, + ) + ) if project_id is not None: - security_group_rule_dict[ - 'security_group_rule']['tenant_id'] = project_id + security_group_rule_dict['security_group_rule'][ + 'tenant_id' + ] = project_id data = proxy._json_response( self.compute.post( - '/os-security-group-rules', - json=security_group_rule_dict - )) + '/os-security-group-rules', json=security_group_rule_dict + ) + ) return self._normalize_secgroup_rule( - self._get_and_munchify('security_group_rule', data)) + self._get_and_munchify('security_group_rule', data) + ) def delete_security_group_rule(self, rule_id): """Delete a security group rule @@ -401,8 +419,7 @@ class SecurityGroupCloudMixin: if self._use_neutron_secgroups(): self.network.delete_security_group_rule( - rule_id, - ignore_missing=False + rule_id, ignore_missing=False ) return True @@ -410,7 +427,9 @@ class SecurityGroupCloudMixin: try: exceptions.raise_from_response( self.compute.delete( - '/os-security-group-rules/{id}'.format(id=rule_id))) + '/os-security-group-rules/{id}'.format(id=rule_id) + ) + ) except exc.OpenStackCloudResourceNotFound: return False @@ -423,8 +442,9 @@ class SecurityGroupCloudMixin: return self.secgroup_source.lower() in ('nova', 'neutron') def _use_neutron_secgroups(self): - return (self.has_service('network') - and self.secgroup_source == 'neutron') + return ( + self.has_service('network') and self.secgroup_source == 'neutron' + ) def _normalize_secgroups(self, groups): """Normalize the structure of security groups @@ -454,7 +474,8 @@ class SecurityGroupCloudMixin: self._remove_novaclient_artifacts(group) rules = self._normalize_secgroup_rules( - group.pop('security_group_rules', group.pop('rules', []))) + group.pop('security_group_rules', group.pop('rules', [])) + ) project_id = group.pop('tenant_id', '') project_id = group.pop('project_id', project_id) @@ -506,14 +527,14 @@ class SecurityGroupCloudMixin: ret['direction'] = rule.pop('direction', 'ingress') ret['ethertype'] = rule.pop('ethertype', 'IPv4') port_range_min = rule.get( - 'port_range_min', rule.pop('from_port', None)) + 'port_range_min', rule.pop('from_port', None) + ) if port_range_min == -1: port_range_min = None if port_range_min is not None: port_range_min = int(port_range_min) ret['port_range_min'] = port_range_min - port_range_max = rule.pop( - 'port_range_max', rule.pop('to_port', None)) + port_range_max = rule.pop('port_range_max', rule.pop('to_port', None)) if port_range_max == -1: port_range_max = None if port_range_min is not None: @@ -521,9 +542,11 @@ class SecurityGroupCloudMixin: ret['port_range_max'] = port_range_max ret['protocol'] = rule.pop('protocol', rule.pop('ip_protocol', None)) ret['remote_ip_prefix'] = rule.pop( - 'remote_ip_prefix', rule.pop('ip_range', {}).get('cidr', None)) + 'remote_ip_prefix', rule.pop('ip_range', {}).get('cidr', None) + ) ret['security_group_id'] = rule.pop( - 'security_group_id', rule.pop('parent_group_id', None)) + 'security_group_id', rule.pop('parent_group_id', None) + ) ret['remote_group_id'] = rule.pop('remote_group_id', None) project_id = rule.pop('tenant_id', '') project_id = rule.pop('project_id', project_id) diff --git a/openstack/cloud/_utils.py b/openstack/cloud/_utils.py index 50bbb6d3e..77f0600f7 100644 --- a/openstack/cloud/_utils.py +++ b/openstack/cloud/_utils.py @@ -102,8 +102,9 @@ def _filter_list(data, name_or_id, filters): e_id = _make_unicode(e.get('id', None)) e_name = _make_unicode(e.get('name', None)) - if ((e_id and e_id == name_or_id) - or (e_name and e_name == name_or_id)): + if (e_id and e_id == name_or_id) or ( + e_name and e_name == name_or_id + ): identifier_matches.append(e) else: # Only try fnmatch if we don't match exactly @@ -112,8 +113,9 @@ def _filter_list(data, name_or_id, filters): # so that we log the bad pattern bad_pattern = True continue - if ((e_id and fn_reg.match(e_id)) - or (e_name and fn_reg.match(e_name))): + if (e_id and fn_reg.match(e_id)) or ( + e_name and fn_reg.match(e_name) + ): identifier_matches.append(e) if not identifier_matches and bad_pattern: log.debug("Bad pattern passed to fnmatch", exc_info=True) @@ -172,8 +174,9 @@ def _get_entity(cloud, resource, name_or_id, filters, **kwargs): # an additional call, it's simple enough to test to see if we got an # object and just short-circuit return it. - if (hasattr(name_or_id, 'id') - or (isinstance(name_or_id, dict) and 'id' in name_or_id)): + if hasattr(name_or_id, 'id') or ( + isinstance(name_or_id, dict) and 'id' in name_or_id + ): return name_or_id # If a uuid is passed short-circuit it calling the @@ -183,14 +186,18 @@ def _get_entity(cloud, resource, name_or_id, filters, **kwargs): if get_resource: return get_resource(name_or_id) - search = resource if callable(resource) else getattr( - cloud, 'search_%ss' % resource, None) + search = ( + resource + if callable(resource) + else getattr(cloud, 'search_%ss' % resource, None) + ) if search: entities = search(name_or_id, filters, **kwargs) if entities: if len(entities) > 1: raise exc.OpenStackCloudException( - "Multiple matches found for %s" % name_or_id) + "Multiple matches found for %s" % name_or_id + ) return entities[0] return None @@ -230,8 +237,10 @@ def valid_kwargs(*valid_args): if k not in argspec.args[1:] and k not in valid_args: raise TypeError( "{f}() got an unexpected keyword argument " - "'{arg}'".format(f=inspect.stack()[1][3], arg=k)) + "'{arg}'".format(f=inspect.stack()[1][3], arg=k) + ) return func(*args, **kwargs) + return func_wrapper @@ -244,6 +253,7 @@ def _func_wrap(f): @functools.wraps(f) def inner(*args, **kwargs): return f(*args, **kwargs) + return inner @@ -253,20 +263,23 @@ def cache_on_arguments(*cache_on_args, **cache_on_kwargs): def _inner_cache_on_arguments(func): def _cache_decorator(obj, *args, **kwargs): the_method = obj._get_cache(_cache_name).cache_on_arguments( - *cache_on_args, **cache_on_kwargs)( - _func_wrap(func.__get__(obj, type(obj)))) + *cache_on_args, **cache_on_kwargs + )(_func_wrap(func.__get__(obj, type(obj)))) return the_method(*args, **kwargs) def invalidate(obj, *args, **kwargs): - return obj._get_cache( - _cache_name).cache_on_arguments()(func).invalidate( - *args, **kwargs) + return ( + obj._get_cache(_cache_name) + .cache_on_arguments()(func) + .invalidate(*args, **kwargs) + ) _cache_decorator.invalidate = invalidate _cache_decorator.func = func _decorated_methods.append(func.__name__) return _cache_decorator + return _inner_cache_on_arguments @@ -320,7 +333,8 @@ def safe_dict_min(key, data): raise exc.OpenStackCloudException( "Search for minimum value failed. " "Value for {key} is not an integer: {value}".format( - key=key, value=d[key]) + key=key, value=d[key] + ) ) if (min_value is None) or (val < min_value): min_value = val @@ -352,16 +366,17 @@ def safe_dict_max(key, data): raise exc.OpenStackCloudException( "Search for maximum value failed. " "Value for {key} is not an integer: {value}".format( - key=key, value=d[key]) + key=key, value=d[key] + ) ) if (max_value is None) or (val > max_value): max_value = val return max_value -def _call_client_and_retry(client, url, retry_on=None, - call_retries=3, retry_wait=2, - **kwargs): +def _call_client_and_retry( + client, url, retry_on=None, call_retries=3, retry_wait=2, **kwargs +): """Method to provide retry operations. Some APIs utilize HTTP errors on certain operations to indicate that @@ -391,18 +406,17 @@ def _call_client_and_retry(client, url, retry_on=None, retry_on = [retry_on] count = 0 - while (count < call_retries): + while count < call_retries: count += 1 try: ret_val = client(url, **kwargs) except exc.OpenStackCloudHTTPError as e: - if (retry_on is not None - and e.response.status_code in retry_on): - log.debug('Received retryable error %(err)s, waiting ' - '%(wait)s seconds to retry', { - 'err': e.response.status_code, - 'wait': retry_wait - }) + if retry_on is not None and e.response.status_code in retry_on: + log.debug( + 'Received retryable error %(err)s, waiting ' + '%(wait)s seconds to retry', + {'err': e.response.status_code, 'wait': retry_wait}, + ) time.sleep(retry_wait) continue else: @@ -484,7 +498,8 @@ def range_filter(data, key, range_exp): # If parsing the range fails, it must be a bad value. if val_range is None: raise exc.OpenStackCloudException( - "Invalid range value: {value}".format(value=range_exp)) + "Invalid range value: {value}".format(value=range_exp) + ) op = val_range[0] if op: @@ -523,9 +538,7 @@ def generate_patches_from_kwargs(operation, **kwargs): """ patches = [] for k, v in kwargs.items(): - patch = {'op': operation, - 'value': v, - 'path': '/%s' % k} + patch = {'op': operation, 'value': v, 'path': '/%s' % k} patches.append(patch) return sorted(patches) @@ -568,11 +581,13 @@ class FileSegment: def _format_uuid_string(string): - return (string.replace('urn:', '') - .replace('uuid:', '') - .strip('{}') - .replace('-', '') - .lower()) + return ( + string.replace('urn:', '') + .replace('uuid:', '') + .strip('{}') + .replace('-', '') + .lower() + ) def _is_uuid_like(val): diff --git a/openstack/cloud/cmd/inventory.py b/openstack/cloud/cmd/inventory.py index 548ddf15f..ce6cbeb97 100644 --- a/openstack/cloud/cmd/inventory.py +++ b/openstack/cloud/cmd/inventory.py @@ -32,20 +32,35 @@ def output_format_dict(data, use_yaml): def parse_args(): parser = argparse.ArgumentParser(description='OpenStack Inventory Module') - parser.add_argument('--refresh', action='store_true', - help='Refresh cached information') + parser.add_argument( + '--refresh', action='store_true', help='Refresh cached information' + ) group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('--list', action='store_true', - help='List active servers') + group.add_argument( + '--list', action='store_true', help='List active servers' + ) group.add_argument('--host', help='List details about the specific host') - parser.add_argument('--private', action='store_true', default=False, - help='Use private IPs for interface_ip') - parser.add_argument('--cloud', default=None, - help='Return data for one cloud only') - parser.add_argument('--yaml', action='store_true', default=False, - help='Output data in nicely readable yaml') - parser.add_argument('--debug', action='store_true', default=False, - help='Enable debug output') + parser.add_argument( + '--private', + action='store_true', + default=False, + help='Use private IPs for interface_ip', + ) + parser.add_argument( + '--cloud', default=None, help='Return data for one cloud only' + ) + parser.add_argument( + '--yaml', + action='store_true', + default=False, + help='Output data in nicely readable yaml', + ) + parser.add_argument( + '--debug', + action='store_true', + default=False, + help='Enable debug output', + ) return parser.parse_args() @@ -54,8 +69,8 @@ def main(): try: openstack.enable_logging(debug=args.debug) inventory = openstack.cloud.inventory.OpenStackInventory( - refresh=args.refresh, private=args.private, - cloud=args.cloud) + refresh=args.refresh, private=args.private, cloud=args.cloud + ) if args.list: output = inventory.list_hosts() elif args.host: diff --git a/openstack/cloud/exc.py b/openstack/cloud/exc.py index d82d73e6c..3d75d4878 100644 --- a/openstack/cloud/exc.py +++ b/openstack/cloud/exc.py @@ -19,12 +19,14 @@ OpenStackCloudTimeout = exceptions.ResourceTimeout class OpenStackCloudCreateException(OpenStackCloudException): - def __init__(self, resource, resource_id, extra_data=None, **kwargs): super(OpenStackCloudCreateException, self).__init__( message="Error creating {resource}: {resource_id}".format( - resource=resource, resource_id=resource_id), - extra_data=extra_data, **kwargs) + resource=resource, resource_id=resource_id + ), + extra_data=extra_data, + **kwargs + ) self.resource_id = resource_id diff --git a/openstack/cloud/inventory.py b/openstack/cloud/inventory.py index f8e9040ba..42b9402dc 100644 --- a/openstack/cloud/inventory.py +++ b/openstack/cloud/inventory.py @@ -28,15 +28,23 @@ class OpenStackInventory: extra_config = None def __init__( - self, config_files=None, refresh=False, private=False, - config_key=None, config_defaults=None, cloud=None, - use_direct_get=False): + self, + config_files=None, + refresh=False, + private=False, + config_key=None, + config_defaults=None, + cloud=None, + use_direct_get=False, + ): if config_files is None: config_files = [] config = loader.OpenStackConfig( - config_files=loader.CONFIG_FILES + config_files) + config_files=loader.CONFIG_FILES + config_files + ) self.extra_config = config.get_extra_config( - config_key, config_defaults) + config_key, config_defaults + ) if cloud is None: self.clouds = [ @@ -44,9 +52,7 @@ class OpenStackInventory: for cloud_region in config.get_all() ] else: - self.clouds = [ - connection.Connection(config=config.get_one(cloud)) - ] + self.clouds = [connection.Connection(config=config.get_one(cloud))] if private: for cloud in self.clouds: @@ -57,15 +63,17 @@ class OpenStackInventory: for cloud in self.clouds: cloud._cache.invalidate() - def list_hosts(self, expand=True, fail_on_cloud_config=True, - all_projects=False): + def list_hosts( + self, expand=True, fail_on_cloud_config=True, all_projects=False + ): hostvars = [] for cloud in self.clouds: try: # Cycle on servers - for server in cloud.list_servers(detailed=expand, - all_projects=all_projects): + for server in cloud.list_servers( + detailed=expand, all_projects=all_projects + ): hostvars.append(server) except exceptions.OpenStackCloudException: # Don't fail on one particular cloud as others may work diff --git a/openstack/cloud/meta.py b/openstack/cloud/meta.py index 5622aeaf0..c1e6f692f 100644 --- a/openstack/cloud/meta.py +++ b/openstack/cloud/meta.py @@ -23,8 +23,9 @@ from openstack import utils NON_CALLABLES = (str, bool, dict, int, float, list, type(None)) -def find_nova_interfaces(addresses, ext_tag=None, key_name=None, version=4, - mac_addr=None): +def find_nova_interfaces( + addresses, ext_tag=None, key_name=None, version=4, mac_addr=None +): ret = [] for (k, v) in iter(addresses.items()): if key_name is not None and k != key_name: @@ -64,10 +65,12 @@ def find_nova_interfaces(addresses, ext_tag=None, key_name=None, version=4, return ret -def find_nova_addresses(addresses, ext_tag=None, key_name=None, version=4, - mac_addr=None): - interfaces = find_nova_interfaces(addresses, ext_tag, key_name, version, - mac_addr) +def find_nova_addresses( + addresses, ext_tag=None, key_name=None, version=4, mac_addr=None +): + interfaces = find_nova_interfaces( + addresses, ext_tag, key_name, version, mac_addr + ) floating_addrs = [] fixed_addrs = [] for i in interfaces: @@ -91,8 +94,7 @@ def get_server_ip(server, public=False, cloud_public=True, **kwargs): private ip we expect shade to be able to reach """ addrs = find_nova_addresses(server['addresses'], **kwargs) - return find_best_address( - addrs, public=public, cloud_public=cloud_public) + return find_best_address(addrs, public=public, cloud_public=cloud_public) def get_server_private_ip(server, cloud=None): @@ -126,30 +128,34 @@ def get_server_private_ip(server, cloud=None): int_nets = cloud.get_internal_ipv4_networks() for int_net in int_nets: int_ip = get_server_ip( - server, key_name=int_net['name'], + server, + key_name=int_net['name'], ext_tag='fixed', cloud_public=not cloud.private, - mac_addr=fip_mac) + mac_addr=fip_mac, + ) if int_ip is not None: return int_ip # Try a second time without the fixed tag. This is for old nova-network # results that do not have the fixed/floating tag. for int_net in int_nets: int_ip = get_server_ip( - server, key_name=int_net['name'], + server, + key_name=int_net['name'], cloud_public=not cloud.private, - mac_addr=fip_mac) + mac_addr=fip_mac, + ) if int_ip is not None: return int_ip ip = get_server_ip( - server, ext_tag='fixed', key_name='private', mac_addr=fip_mac) + server, ext_tag='fixed', key_name='private', mac_addr=fip_mac + ) if ip: return ip # Last resort, and Rackspace - return get_server_ip( - server, key_name='private') + return get_server_ip(server, key_name='private') def get_server_external_ipv4(cloud, server): @@ -183,8 +189,11 @@ def get_server_external_ipv4(cloud, server): ext_nets = cloud.get_external_ipv4_networks() for ext_net in ext_nets: ext_ip = get_server_ip( - server, key_name=ext_net['name'], public=True, - cloud_public=not cloud.private) + server, + key_name=ext_net['name'], + public=True, + cloud_public=not cloud.private, + ) if ext_ip is not None: return ext_ip @@ -192,8 +201,8 @@ def get_server_external_ipv4(cloud, server): # Much as I might find floating IPs annoying, if it has one, that's # almost certainly the one that wants to be used ext_ip = get_server_ip( - server, ext_tag='floating', public=True, - cloud_public=not cloud.private) + server, ext_tag='floating', public=True, cloud_public=not cloud.private + ) if ext_ip is not None: return ext_ip @@ -203,8 +212,8 @@ def get_server_external_ipv4(cloud, server): # Try to get an address from a network named 'public' ext_ip = get_server_ip( - server, key_name='public', public=True, - cloud_public=not cloud.private) + server, key_name='public', public=True, cloud_public=not cloud.private + ) if ext_ip is not None: return ext_ip @@ -238,15 +247,21 @@ def find_best_address(addresses, public=False, cloud_public=True): for address in addresses: try: for count in utils.iterate_timeout( - 5, "Timeout waiting for %s" % address, wait=0.1): + 5, "Timeout waiting for %s" % address, wait=0.1 + ): # Return the first one that is reachable try: for res in socket.getaddrinfo( - address, 22, socket.AF_UNSPEC, - socket.SOCK_STREAM, 0): + address, + 22, + socket.AF_UNSPEC, + socket.SOCK_STREAM, + 0, + ): family, socktype, proto, _, sa = res connect_socket = socket.socket( - family, socktype, proto) + family, socktype, proto + ) connect_socket.settimeout(1) connect_socket.connect(sa) return address @@ -265,12 +280,13 @@ def find_best_address(addresses, public=False, cloud_public=True): "The cloud returned multiple addresses %s:, and we could not " "connect to port 22 on either. That might be what you wanted, " "but we have no clue what's going on, so we picked the first one " - "%s" % (addresses, addresses[0])) + "%s" % (addresses, addresses[0]) + ) return addresses[0] def get_server_external_ipv6(server): - """ Get an IPv6 address reachable from outside the cloud. + """Get an IPv6 address reachable from outside the cloud. This function assumes that if a server has an IPv6 address, that address is reachable from outside the cloud. @@ -286,7 +302,7 @@ def get_server_external_ipv6(server): def get_server_default_ip(cloud, server): - """ Get the configured 'default' address + """Get the configured 'default' address It is possible in clouds.yaml to configure for a cloud a network that is the 'default_interface'. This is the network that should be used @@ -299,22 +315,26 @@ def get_server_default_ip(cloud, server): """ ext_net = cloud.get_default_network() if ext_net: - if (cloud._local_ipv6 and not cloud.force_ipv4): + if cloud._local_ipv6 and not cloud.force_ipv4: # try 6 first, fall back to four versions = [6, 4] else: versions = [4] for version in versions: ext_ip = get_server_ip( - server, key_name=ext_net['name'], version=version, public=True, - cloud_public=not cloud.private) + server, + key_name=ext_net['name'], + version=version, + public=True, + cloud_public=not cloud.private, + ) if ext_ip is not None: return ext_ip return None def _get_interface_ip(cloud, server): - """ Get the interface IP for the server + """Get the interface IP for the server Interface IP is the IP that should be used for communicating with the server. It is: @@ -329,7 +349,7 @@ def _get_interface_ip(cloud, server): if cloud.private and server['private_v4']: return server['private_v4'] - if (server['public_v6'] and cloud._local_ipv6 and not cloud.force_ipv4): + if server['public_v6'] and cloud._local_ipv6 and not cloud.force_ipv4: return server['public_v6'] else: return server['public_v4'] @@ -404,15 +424,19 @@ def _get_supplemental_addresses(cloud, server): try: # Don't bother doing this before the server is active, it's a waste # of an API call while polling for a server to come up - if (cloud.has_service('network') - and cloud._has_floating_ips() - and server['status'] == 'ACTIVE'): + if ( + cloud.has_service('network') + and cloud._has_floating_ips() + and server['status'] == 'ACTIVE' + ): for port in cloud.search_ports( - filters=dict(device_id=server['id'])): + filters=dict(device_id=server['id']) + ): # This SHOULD return one and only one FIP - but doing it as a # search/list lets the logic work regardless for fip in cloud.search_floating_ips( - filters=dict(port_id=port['id'])): + filters=dict(port_id=port['id']) + ): fixed_net = fixed_ip_mapping.get(fip['fixed_ip_address']) if fixed_net is None: log = _log.setup_logging('openstack') @@ -422,10 +446,12 @@ def _get_supplemental_addresses(cloud, server): " with the floating ip in the neutron listing" " does not exist in the nova listing. Something" " is exceptionally broken.", - dict(fip=fip['id'], server=server['id'])) + dict(fip=fip['id'], server=server['id']), + ) else: server['addresses'][fixed_net].append( - _make_address_dict(fip, port)) + _make_address_dict(fip, port) + ) except exc.OpenStackCloudException: # If something goes wrong with a cloud call, that's cool - this is # an attempt to provide additional data and should not block forward @@ -485,8 +511,7 @@ def get_hostvars_from_server(cloud, server, mounts=None): expand_server_vars if caching is not set up. If caching is set up, the extra cost should be minimal. """ - server_vars = obj_to_munch( - add_server_interfaces(cloud, server)) + server_vars = obj_to_munch(add_server_interfaces(cloud, server)) flavor_id = server['flavor'].get('id') if flavor_id: @@ -539,7 +564,7 @@ def get_hostvars_from_server(cloud, server, mounts=None): def obj_to_munch(obj): - """ Turn an object with attributes into a dict suitable for serializing. + """Turn an object with attributes into a dict suitable for serializing. Some of the things that are returned in OpenStack are objects with attributes. That's awesome - except when you want to expose them as JSON diff --git a/openstack/cloud/openstackcloud.py b/openstack/cloud/openstackcloud.py index 9b96e58db..76f73b500 100644 --- a/openstack/cloud/openstackcloud.py +++ b/openstack/cloud/openstackcloud.py @@ -12,6 +12,7 @@ import copy import functools import queue + # import types so that we can reference ListType in sphinx param declarations. # We can't just use list, because sphinx gets confused by # openstack.resource.Resource.list and openstack.resource2.Resource.list @@ -60,6 +61,7 @@ class _OpenStackCloudMixin: :param bool strict: Only return documented attributes for each resource as per the Data Model contract. (Default False) """ + _OBJECT_MD5_KEY = 'x-sdk-md5' _OBJECT_SHA256_KEY = 'x-sdk-sha256' _OBJECT_AUTOCREATE_KEY = 'x-sdk-autocreated' @@ -90,7 +92,8 @@ class _OpenStackCloudMixin: # cert verification if not self.verify: self.log.debug( - "Turning off Insecure SSL warnings since verify=False") + "Turning off Insecure SSL warnings since verify=False" + ) category = requestsexceptions.InsecureRequestWarning if category: # InsecureRequestWarning references a Warning class or is None @@ -131,19 +134,20 @@ class _OpenStackCloudMixin: meth_obj = getattr(self, method, None) if not meth_obj: continue - if (hasattr(meth_obj, 'invalidate') - and hasattr(meth_obj, 'func')): + if hasattr(meth_obj, 'invalidate') and hasattr( + meth_obj, 'func' + ): new_func = functools.partial(meth_obj.func, self) new_func.invalidate = _fake_invalidate setattr(self, method, new_func) # Uncoditionally create cache even with a "null" backend self._cache = self._make_cache( - cache_class, cache_expiration_time, cache_arguments) + cache_class, cache_expiration_time, cache_arguments + ) expirations = self.config.get_cache_expirations() for expire_key in expirations.keys(): - self._cache_expirations[expire_key] = \ - expirations[expire_key] + self._cache_expirations[expire_key] = expirations[expire_key] # TODO(gtema): delete in next change self._SERVER_AGE = 0 @@ -159,7 +163,8 @@ class _OpenStackCloudMixin: self._raw_clients = {} self._local_ipv6 = ( - _utils.localhost_supports_ipv6() if not self.force_ipv4 else False) + _utils.localhost_supports_ipv6() if not self.force_ipv4 else False + ) def connect_as(self, **kwargs): """Make a new OpenStackCloud object with new auth context. @@ -191,7 +196,8 @@ class _OpenStackCloudMixin: config = openstack.config.OpenStackConfig( app_name=self.config._app_name, app_version=self.config._app_version, - load_yaml_config=False) + load_yaml_config=False, + ) params = copy.deepcopy(self.config.config) # Remove profile from current cloud so that overridding works params.pop('profile', None) @@ -298,7 +304,8 @@ class _OpenStackCloudMixin: app_name=self.config._app_name, app_version=self.config._app_version, discovery_cache=self.session._discovery_cache, - **params) + **params + ) # Override the cloud name so that logging/location work right cloud_region._name = self.name @@ -313,9 +320,8 @@ class _OpenStackCloudMixin: return dogpile.cache.make_region( function_key_generator=self._make_cache_key ).configure( - cache_class, - expiration_time=expiration_time, - arguments=arguments) + cache_class, expiration_time=expiration_time, arguments=arguments + ) def _make_cache_key(self, namespace, fn): fname = fn.__name__ @@ -329,10 +335,11 @@ class _OpenStackCloudMixin: arg_key = '' kw_keys = sorted(kwargs.keys()) kwargs_key = ','.join( - ['%s:%s' % (k, kwargs[k]) for k in kw_keys if k != 'cache']) - ans = "_".join( - [str(name_key), fname, arg_key, kwargs_key]) + ['%s:%s' % (k, kwargs[k]) for k in kw_keys if k != 'cache'] + ) + ans = "_".join([str(name_key), fname, arg_key, kwargs_key]) return ans + return generate_key def _get_cache(self, resource_name): @@ -349,7 +356,8 @@ class _OpenStackCloudMixin: return version def _get_versioned_client( - self, service_type, min_version=None, max_version=None): + self, service_type, min_version=None, max_version=None + ): config_version = self.config.get_api_version(service_type) config_major = self._get_major_version_id(config_version) max_major = self._get_major_version_id(max_version) @@ -372,7 +380,9 @@ class _OpenStackCloudMixin: " but shade understands a minimum of {min_version}".format( config_version=config_version, service_type=service_type, - min_version=min_version)) + min_version=min_version, + ) + ) elif max_major and config_major > max_major: raise exc.OpenStackCloudException( "Version {config_version} requested for {service_type}" @@ -380,10 +390,13 @@ class _OpenStackCloudMixin: " {max_version}".format( config_version=config_version, service_type=service_type, - max_version=max_version)) + max_version=max_version, + ) + ) request_min_version = config_version request_max_version = '{version}.latest'.format( - version=config_major) + version=config_major + ) adapter = proxy._ShadeAdapter( session=self.session, service_type=self.config.get_service_type(service_type), @@ -397,7 +410,8 @@ class _OpenStackCloudMixin: prometheus_histogram=self.config.get_prometheus_histogram(), influxdb_client=self.config.get_influxdb_client(), min_version=request_min_version, - max_version=request_max_version) + max_version=request_max_version, + ) if adapter.get_endpoint(): return adapter @@ -409,12 +423,14 @@ class _OpenStackCloudMixin: endpoint_override=self.config.get_endpoint(service_type), region_name=self.config.get_region_name(service_type), min_version=min_version, - max_version=max_version) + max_version=max_version, + ) # data.api_version can be None if no version was detected, such # as with neutron api_version = adapter.get_api_major_version( - endpoint_override=self.config.get_endpoint(service_type)) + endpoint_override=self.config.get_endpoint(service_type) + ) api_major = self._get_major_version_id(api_version) # If we detect a different version that was configured, warn the user. @@ -430,7 +446,9 @@ class _OpenStackCloudMixin: ' your config.'.format( service_type=service_type, config_version=config_version, - api_version='.'.join([str(f) for f in api_version]))) + api_version='.'.join([str(f) for f in api_version]), + ) + ) self.log.debug(warning_msg) warnings.warn(warning_msg) return adapter @@ -438,19 +456,22 @@ class _OpenStackCloudMixin: # TODO(shade) This should be replaced with using openstack Connection # object. def _get_raw_client( - self, service_type, api_version=None, endpoint_override=None): + self, service_type, api_version=None, endpoint_override=None + ): return proxy._ShadeAdapter( session=self.session, service_type=self.config.get_service_type(service_type), service_name=self.config.get_service_name(service_type), interface=self.config.get_interface(service_type), - endpoint_override=self.config.get_endpoint( - service_type) or endpoint_override, - region_name=self.config.get_region_name(service_type)) + endpoint_override=self.config.get_endpoint(service_type) + or endpoint_override, + region_name=self.config.get_region_name(service_type), + ) def _is_client_version(self, client, version): client_name = '_{client}_client'.format( - client=client.replace('-', '_')) + client=client.replace('-', '_') + ) client = getattr(self, client_name) return client._version_matches(version) @@ -458,7 +479,8 @@ class _OpenStackCloudMixin: def _application_catalog_client(self): if 'application-catalog' not in self._raw_clients: self._raw_clients['application-catalog'] = self._get_raw_client( - 'application-catalog') + 'application-catalog' + ) return self._raw_clients['application-catalog'] @property @@ -478,6 +500,7 @@ class _OpenStackCloudMixin: """Wrapper around pprint that groks munch objects""" # import late since this is a utility function import pprint + new_resource = _utils._dictify_resource(resource) pprint.pprint(new_resource) @@ -485,6 +508,7 @@ class _OpenStackCloudMixin: """Wrapper around pformat that groks munch objects""" # import late since this is a utility function import pprint + new_resource = _utils._dictify_resource(resource) return pprint.pformat(new_resource) @@ -521,7 +545,8 @@ class _OpenStackCloudMixin: return self.config.get_endpoint_from_catalog( service_type=service_type, interface=interface, - region_name=region_name) + region_name=region_name, + ) @property def auth_token(self): @@ -600,10 +625,9 @@ class _OpenStackCloudMixin: region_name=None, zone=None, project=utils.Munch( - id=None, - name=None, - domain_id=None, - domain_name=None)) + id=None, name=None, domain_id=None, domain_name=None + ), + ) def _get_project_id_param_dict(self, name_or_id): if name_or_id: @@ -628,7 +652,8 @@ class _OpenStackCloudMixin: if not domain_id: raise exc.OpenStackCloudException( "User or project creation requires an explicit" - " domain_id argument.") + " domain_id argument." + ) else: return {'domain_id': domain_id} else: @@ -714,7 +739,8 @@ class _OpenStackCloudMixin: return self.config.get_session_endpoint(service_key, **kwargs) except keystoneauth1.exceptions.catalog.EndpointNotFound as e: self.log.debug( - "Endpoint not found in %s cloud: %s", self.name, str(e)) + "Endpoint not found in %s cloud: %s", self.name, str(e) + ) endpoint = None except exc.OpenStackCloudException: raise @@ -725,17 +751,22 @@ class _OpenStackCloudMixin: service=service_key, cloud=self.name, region=self.config.get_region_name(service_key), - error=str(e))) + error=str(e), + ) + ) return endpoint def has_service(self, service_key, version=None): if not self.config.has_service(service_key): # TODO(mordred) add a stamp here so that we only report this once - if not (service_key in self._disable_warnings - and self._disable_warnings[service_key]): + if not ( + service_key in self._disable_warnings + and self._disable_warnings[service_key] + ): self.log.debug( - "Disabling %(service_key)s entry in catalog" - " per config", {'service_key': service_key}) + "Disabling %(service_key)s entry in catalog" " per config", + {'service_key': service_key}, + ) self._disable_warnings[service_key] = True return False try: @@ -786,26 +817,23 @@ class _OpenStackCloudMixin: (service_name, resource_name) = resource_type.split('.') if not hasattr(self, service_name): raise exceptions.SDKException( - "service %s is not existing/enabled" % - service_name + "service %s is not existing/enabled" % service_name ) service_proxy = getattr(self, service_name) try: resource_type = service_proxy._resource_registry[resource_name] except KeyError: raise exceptions.SDKException( - "Resource %s is not known in service %s" % - (resource_name, service_name) + "Resource %s is not known in service %s" + % (resource_name, service_name) ) if name_or_id: # name_or_id is definitely not None try: resource_by_id = service_proxy._get( - resource_type, - name_or_id, - *get_args, - **get_kwargs) + resource_type, name_or_id, *get_args, **get_kwargs + ) return [resource_by_id] except exceptions.ResourceNotFound: pass @@ -817,11 +845,9 @@ class _OpenStackCloudMixin: filters["name"] = name_or_id list_kwargs.update(filters) - return list(service_proxy._list( - resource_type, - *list_args, - **list_kwargs - )) + return list( + service_proxy._list(resource_type, *list_args, **list_kwargs) + ) def project_cleanup( self, @@ -829,7 +855,7 @@ class _OpenStackCloudMixin: wait_timeout=120, status_queue=None, filters=None, - resource_evaluation_fn=None + resource_evaluation_fn=None, ): """Cleanup the project resources. @@ -866,7 +892,7 @@ class _OpenStackCloudMixin: dependencies.update(deps) except ( exceptions.NotSupported, - exceptions.ServiceDisabledException + exceptions.ServiceDisabledException, ): # Cloud may include endpoint in catalog but not # implement the service or disable it @@ -895,7 +921,7 @@ class _OpenStackCloudMixin: client_status_queue=status_queue, identified_resources=cleanup_resources, filters=filters, - resource_evaluation_fn=resource_evaluation_fn + resource_evaluation_fn=resource_evaluation_fn, ) except exceptions.ServiceDisabledException: # same reason as above @@ -908,9 +934,10 @@ class _OpenStackCloudMixin: dep_graph.node_done(service) for count in utils.iterate_timeout( - timeout=wait_timeout, - message="Timeout waiting for cleanup to finish", - wait=1): + timeout=wait_timeout, + message="Timeout waiting for cleanup to finish", + wait=1, + ): if dep_graph.is_complete(): return diff --git a/openstack/tests/functional/cloud/test_aggregate.py b/openstack/tests/functional/cloud/test_aggregate.py index e96b64f3d..abd920e62 100644 --- a/openstack/tests/functional/cloud/test_aggregate.py +++ b/openstack/tests/functional/cloud/test_aggregate.py @@ -21,7 +21,6 @@ from openstack.tests.functional import base class TestAggregate(base.BaseFunctionalTest): - def test_aggregates(self): if not self.operator_cloud: self.skipTest("Operator cloud is required for this test") @@ -30,31 +29,28 @@ class TestAggregate(base.BaseFunctionalTest): self.addCleanup(self.cleanup, aggregate_name) aggregate = self.operator_cloud.create_aggregate(aggregate_name) - aggregate_ids = [v['id'] - for v in self.operator_cloud.list_aggregates()] + aggregate_ids = [ + v['id'] for v in self.operator_cloud.list_aggregates() + ] self.assertIn(aggregate['id'], aggregate_ids) aggregate = self.operator_cloud.update_aggregate( - aggregate_name, - availability_zone=availability_zone + aggregate_name, availability_zone=availability_zone ) self.assertEqual(availability_zone, aggregate['availability_zone']) aggregate = self.operator_cloud.set_aggregate_metadata( - aggregate_name, - {'key': 'value'} + aggregate_name, {'key': 'value'} ) self.assertIn('key', aggregate['metadata']) aggregate = self.operator_cloud.set_aggregate_metadata( - aggregate_name, - {'key': None} + aggregate_name, {'key': None} ) self.assertNotIn('key', aggregate['metadata']) # Validate that we can delete by name - self.assertTrue( - self.operator_cloud.delete_aggregate(aggregate_name)) + self.assertTrue(self.operator_cloud.delete_aggregate(aggregate_name)) def cleanup(self, aggregate_name): aggregate = self.operator_cloud.get_aggregate(aggregate_name) diff --git a/openstack/tests/functional/cloud/test_cluster_templates.py b/openstack/tests/functional/cloud/test_cluster_templates.py index afc304bba..897950a22 100644 --- a/openstack/tests/functional/cloud/test_cluster_templates.py +++ b/openstack/tests/functional/cloud/test_cluster_templates.py @@ -26,7 +26,6 @@ from openstack.tests.functional import base class TestClusterTemplate(base.BaseFunctionalTest): - def setUp(self): super(TestClusterTemplate, self).setUp() if not self.user_cloud.has_service( @@ -52,8 +51,16 @@ class TestClusterTemplate(base.BaseFunctionalTest): # generate a keypair to add to nova subprocess.call( - ['ssh-keygen', '-t', 'rsa', '-N', '', '-f', - '%s/id_rsa_sdk' % self.ssh_directory]) + [ + 'ssh-keygen', + '-t', + 'rsa', + '-N', + '', + '-f', + '%s/id_rsa_sdk' % self.ssh_directory, + ] + ) # add keypair to nova with open('%s/id_rsa_sdk.pub' % self.ssh_directory) as f: @@ -62,8 +69,8 @@ class TestClusterTemplate(base.BaseFunctionalTest): # Test we can create a cluster_template and we get it returned self.ct = self.user_cloud.create_cluster_template( - name=name, image_id=image_id, - keypair_id=keypair_id, coe=coe) + name=name, image_id=image_id, keypair_id=keypair_id, coe=coe + ) self.assertEqual(self.ct['name'], name) self.assertEqual(self.ct['image_id'], image_id) self.assertEqual(self.ct['keypair_id'], keypair_id) @@ -80,7 +87,8 @@ class TestClusterTemplate(base.BaseFunctionalTest): # Test we get the same cluster_template with the # get_cluster_template method cluster_template_get = self.user_cloud.get_cluster_template( - self.ct['uuid']) + self.ct['uuid'] + ) self.assertEqual(cluster_template_get['uuid'], self.ct['uuid']) # Test the get method also works by name @@ -90,14 +98,15 @@ class TestClusterTemplate(base.BaseFunctionalTest): # Test we can update a field on the cluster_template and only that # field is updated cluster_template_update = self.user_cloud.update_cluster_template( - self.ct, tls_disabled=True) - self.assertEqual( - cluster_template_update['uuid'], self.ct['uuid']) + self.ct, tls_disabled=True + ) + self.assertEqual(cluster_template_update['uuid'], self.ct['uuid']) self.assertTrue(cluster_template_update['tls_disabled']) # Test we can delete and get True returned cluster_template_delete = self.user_cloud.delete_cluster_template( - self.ct['uuid']) + self.ct['uuid'] + ) self.assertTrue(cluster_template_delete) def cleanup(self, name): diff --git a/openstack/tests/functional/cloud/test_clustering.py b/openstack/tests/functional/cloud/test_clustering.py index 80fd8a7a7..96bb3302a 100644 --- a/openstack/tests/functional/cloud/test_clustering.py +++ b/openstack/tests/functional/cloud/test_clustering.py @@ -24,8 +24,9 @@ from testtools import content from openstack.tests.functional import base -def wait_for_status(client, client_args, field, value, check_interval=1, - timeout=60): +def wait_for_status( + client, client_args, field, value, check_interval=1, timeout=60 +): """Wait for an OpenStack resource to enter a specified state :param client: An uncalled client resource to be called with resource_args @@ -55,15 +56,15 @@ def wait_for_status(client, client_args, field, value, check_interval=1, def wait_for_create(client, client_args, check_interval=1, timeout=60): """Wait for an OpenStack resource to be created - :param client: An uncalled client resource to be called with resource_args - :param client_args: Arguments to be passed to client - :param name: Name of the resource (for logging) - :param check_interval: Interval between checks - :param timeout: Time in seconds to wait for status to update. - :returns: True if openstack.exceptions.NotFoundException is caught - :raises: TimeoutException + :param client: An uncalled client resource to be called with resource_args + :param client_args: Arguments to be passed to client + :param name: Name of the resource (for logging) + :param check_interval: Interval between checks + :param timeout: Time in seconds to wait for status to update. + :returns: True if openstack.exceptions.NotFoundException is caught + :raises: TimeoutException - """ + """ resource = client(**client_args) start = time.time() @@ -106,7 +107,6 @@ def wait_for_delete(client, client_args, check_interval=1, timeout=60): class TestClustering(base.BaseFunctionalTest): - def setUp(self): super(TestClustering, self).setUp() self.skipTest('clustering service not supported by cloud') @@ -117,24 +117,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -147,24 +142,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -175,15 +165,18 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) self.assertEqual(cluster['cluster']['name'], cluster_name) self.assertEqual(cluster['cluster']['profile_id'], profile['id']) - self.assertEqual(cluster['cluster']['desired_capacity'], - desired_capacity) + self.assertEqual( + cluster['cluster']['desired_capacity'], desired_capacity + ) def test_get_cluster_by_id(self): profile_name = "test_profile" @@ -191,24 +184,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) cluster_name = 'example_cluster' @@ -218,14 +206,17 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) # Test that we get the same cluster with the get_cluster method cluster_get = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) + cluster['cluster']['id'] + ) self.assertEqual(cluster_get['id'], cluster['cluster']['id']) def test_update_cluster(self): @@ -234,24 +225,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -262,30 +248,40 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) # Test that we can update a field on the cluster and only that field # is updated - self.user_cloud.update_cluster(cluster['cluster']['id'], - new_name='new_cluster_name') + self.user_cloud.update_cluster( + cluster['cluster']['id'], new_name='new_cluster_name' + ) wait = wait_for_status( self.user_cloud.get_cluster_by_id, - {'name_or_id': cluster['cluster']['id']}, 'status', 'ACTIVE') + {'name_or_id': cluster['cluster']['id']}, + 'status', + 'ACTIVE', + ) self.assertTrue(wait) cluster_update = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) + cluster['cluster']['id'] + ) self.assertEqual(cluster_update['id'], cluster['cluster']['id']) self.assertEqual(cluster_update['name'], 'new_cluster_name') - self.assertEqual(cluster_update['profile_id'], - cluster['cluster']['profile_id']) - self.assertEqual(cluster_update['desired_capacity'], - cluster['cluster']['desired_capacity']) + self.assertEqual( + cluster_update['profile_id'], cluster['cluster']['profile_id'] + ) + self.assertEqual( + cluster_update['desired_capacity'], + cluster['cluster']['desired_capacity'], + ) def test_create_cluster_policy(self): policy_name = 'example_policy' @@ -294,20 +290,21 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) self.addCleanup(self.cleanup_policy, policy['id']) @@ -320,24 +317,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -348,8 +340,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -359,33 +353,36 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) - self.addCleanup(self.cleanup_policy, policy['id'], - cluster['cluster']['id']) + self.addCleanup( + self.cleanup_policy, policy['id'], cluster['cluster']['id'] + ) # Test that we can attach policy to cluster and get True returned attach_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) - attach_policy = self.user_cloud.get_cluster_policy_by_id( - policy['id']) + cluster['cluster']['id'] + ) + attach_policy = self.user_cloud.get_cluster_policy_by_id(policy['id']) policy_attach = self.user_cloud.attach_policy_to_cluster( - attach_cluster, attach_policy, is_enabled=True) + attach_cluster, attach_policy, is_enabled=True + ) self.assertTrue(policy_attach) def test_detach_policy_from_cluster(self): @@ -394,24 +391,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -422,8 +414,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -433,39 +427,45 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) - self.addCleanup(self.cleanup_policy, policy['id'], - cluster['cluster']['id']) + self.addCleanup( + self.cleanup_policy, policy['id'], cluster['cluster']['id'] + ) attach_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) - attach_policy = self.user_cloud.get_cluster_policy_by_id( - policy['id']) + cluster['cluster']['id'] + ) + attach_policy = self.user_cloud.get_cluster_policy_by_id(policy['id']) self.user_cloud.attach_policy_to_cluster( - attach_cluster, attach_policy, is_enabled=True) + attach_cluster, attach_policy, is_enabled=True + ) wait = wait_for_status( self.user_cloud.get_cluster_by_id, - {'name_or_id': cluster['cluster']['id']}, 'policies', - ['{policy}'.format(policy=policy['id'])]) + {'name_or_id': cluster['cluster']['id']}, + 'policies', + ['{policy}'.format(policy=policy['id'])], + ) policy_detach = self.user_cloud.detach_policy_from_cluster( - attach_cluster, attach_policy) + attach_cluster, attach_policy + ) self.assertTrue(policy_detach) self.assertTrue(wait) @@ -476,24 +476,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -504,8 +499,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -515,50 +512,58 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) - self.addCleanup(self.cleanup_policy, policy['id'], - cluster['cluster']['id']) + self.addCleanup( + self.cleanup_policy, policy['id'], cluster['cluster']['id'] + ) # Test that we can attach policy to cluster and get True returned attach_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) - attach_policy = self.user_cloud.get_cluster_policy_by_id( - policy['id']) + cluster['cluster']['id'] + ) + attach_policy = self.user_cloud.get_cluster_policy_by_id(policy['id']) policy_attach = self.user_cloud.attach_policy_to_cluster( - attach_cluster, attach_policy, is_enabled=True) + attach_cluster, attach_policy, is_enabled=True + ) self.assertTrue(policy_attach) wait = wait_for_status( self.user_cloud.get_cluster_by_id, - {'name_or_id': cluster['cluster']['id']}, 'policies', - ["{policy}".format(policy=policy['id'])]) + {'name_or_id': cluster['cluster']['id']}, + 'policies', + ["{policy}".format(policy=policy['id'])], + ) # Test that we get the same policy with the get_policy_on_cluster # method cluster_policy_get = self.user_cloud.get_policy_on_cluster( - cluster['cluster']["id"], policy['id']) + cluster['cluster']["id"], policy['id'] + ) - self.assertEqual(cluster_policy_get['cluster_id'], - cluster['cluster']["id"]) - self.assertEqual(cluster_policy_get['cluster_name'], - cluster['cluster']["name"]) + self.assertEqual( + cluster_policy_get['cluster_id'], cluster['cluster']["id"] + ) + self.assertEqual( + cluster_policy_get['cluster_name'], cluster['cluster']["name"] + ) self.assertEqual(cluster_policy_get['policy_id'], policy['id']), self.assertEqual(cluster_policy_get['policy_name'], policy['name']) self.assertTrue(wait) @@ -569,24 +574,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -597,8 +597,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -608,48 +610,53 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) - self.addCleanup(self.cleanup_policy, policy['id'], - cluster['cluster']['id']) + self.addCleanup( + self.cleanup_policy, policy['id'], cluster['cluster']['id'] + ) attach_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) - attach_policy = self.user_cloud.get_cluster_policy_by_id( - policy['id']) + cluster['cluster']['id'] + ) + attach_policy = self.user_cloud.get_cluster_policy_by_id(policy['id']) self.user_cloud.attach_policy_to_cluster( - attach_cluster, attach_policy, is_enabled=True) + attach_cluster, attach_policy, is_enabled=True + ) wait = wait_for_status( self.user_cloud.get_cluster_by_id, - {'name_or_id': cluster['cluster']['id']}, 'policies', - ["{policy}".format(policy=policy['id'])]) + {'name_or_id': cluster['cluster']['id']}, + 'policies', + ["{policy}".format(policy=policy['id'])], + ) cluster_policy = self.user_cloud.get_policy_on_cluster( - name_or_id=cluster['cluster']['id'], - policy_name_or_id=policy['id']) + name_or_id=cluster['cluster']['id'], policy_name_or_id=policy['id'] + ) policy_list = {"cluster_policies": [cluster_policy]} # Test that we can list the policies on a cluster cluster_policies = self.user_cloud.list_policies_on_cluster( - cluster['cluster']["id"]) - self.assertEqual( - cluster_policies, policy_list) + cluster['cluster']["id"] + ) + self.assertEqual(cluster_policies, policy_list) self.assertTrue(wait) def test_create_cluster_receiver(self): @@ -658,24 +665,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -686,8 +688,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -699,9 +703,11 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a receiver and we get it returned receiver = self.user_cloud.create_cluster_receiver( - name=receiver_name, receiver_type=receiver_type, + name=receiver_name, + receiver_type=receiver_type, cluster_name_or_id=cluster['cluster']['id'], - action='CLUSTER_SCALE_OUT') + action='CLUSTER_SCALE_OUT', + ) self.addCleanup(self.cleanup_receiver, receiver['id']) @@ -715,24 +721,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -743,8 +744,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -756,14 +759,17 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a receiver and we get it returned receiver = self.user_cloud.create_cluster_receiver( - name=receiver_name, receiver_type=receiver_type, + name=receiver_name, + receiver_type=receiver_type, cluster_name_or_id=cluster['cluster']['id'], - action='CLUSTER_SCALE_OUT') + action='CLUSTER_SCALE_OUT', + ) self.addCleanup(self.cleanup_receiver, receiver['id']) get_receiver = self.user_cloud.get_cluster_receiver_by_id( - receiver['id']) + receiver['id'] + ) receiver_list = {"receivers": [get_receiver]} # Test that we can list receivers @@ -777,24 +783,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -805,8 +806,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -816,31 +819,33 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) self.addCleanup(self.cleanup_policy, policy['id']) # Test that we can attach policy to cluster and get True returned attach_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) - attach_policy = self.user_cloud.get_cluster_policy_by_id( - policy['id']) + cluster['cluster']['id'] + ) + attach_policy = self.user_cloud.get_cluster_policy_by_id(policy['id']) self.user_cloud.attach_policy_to_cluster( - attach_cluster, attach_policy, is_enabled=True) + attach_cluster, attach_policy, is_enabled=True + ) receiver_name = "example_receiver" receiver_type = "webhook" @@ -850,13 +855,16 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a receiver and we get it returned self.user_cloud.create_cluster_receiver( - name=receiver_name, receiver_type=receiver_type, + name=receiver_name, + receiver_type=receiver_type, cluster_name_or_id=cluster['cluster']['id'], - action='CLUSTER_SCALE_OUT') + action='CLUSTER_SCALE_OUT', + ) # Test that we can delete cluster and get True returned cluster_delete = self.user_cloud.delete_cluster( - cluster['cluster']['id']) + cluster['cluster']['id'] + ) self.assertTrue(cluster_delete) def test_list_clusters(self): @@ -865,24 +873,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -893,17 +896,23 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) wait = wait_for_status( self.user_cloud.get_cluster_by_id, - {'name_or_id': cluster['cluster']['id']}, 'status', 'ACTIVE') + {'name_or_id': cluster['cluster']['id']}, + 'status', + 'ACTIVE', + ) get_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) + cluster['cluster']['id'] + ) # Test that we can list clusters clusters = self.user_cloud.list_clusters() @@ -916,24 +925,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -944,8 +948,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -955,56 +961,68 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) - self.addCleanup(self.cleanup_policy, policy['id'], - cluster['cluster']['id']) + self.addCleanup( + self.cleanup_policy, policy['id'], cluster['cluster']['id'] + ) # Test that we can attach policy to cluster and get True returned attach_cluster = self.user_cloud.get_cluster_by_id( - cluster['cluster']['id']) - attach_policy = self.user_cloud.get_cluster_policy_by_id( - policy['id']) + cluster['cluster']['id'] + ) + attach_policy = self.user_cloud.get_cluster_policy_by_id(policy['id']) self.user_cloud.attach_policy_to_cluster( - attach_cluster, attach_policy, is_enabled=True) + attach_cluster, attach_policy, is_enabled=True + ) wait_attach = wait_for_status( self.user_cloud.get_cluster_by_id, - {'name_or_id': cluster['cluster']['id']}, 'policies', - ["{policy}".format(policy=policy['id'])]) + {'name_or_id': cluster['cluster']['id']}, + 'policies', + ["{policy}".format(policy=policy['id'])], + ) get_old_policy = self.user_cloud.get_policy_on_cluster( - cluster['cluster']["id"], policy['id']) + cluster['cluster']["id"], policy['id'] + ) # Test that we can update the policy on cluster policy_update = self.user_cloud.update_policy_on_cluster( - attach_cluster, attach_policy, is_enabled=False) + attach_cluster, attach_policy, is_enabled=False + ) get_old_policy.update({'enabled': False}) wait_update = wait_for_status( self.user_cloud.get_policy_on_cluster, - {'name_or_id': cluster['cluster']['id'], - 'policy_name_or_id': policy['id']}, 'enabled', - False) + { + 'name_or_id': cluster['cluster']['id'], + 'policy_name_or_id': policy['id'], + }, + 'enabled', + False, + ) get_new_policy = self.user_cloud.get_policy_on_cluster( - cluster['cluster']["id"], policy['id']) + cluster['cluster']["id"], policy['id'] + ) get_old_policy['last_op'] = None get_new_policy['last_op'] = None @@ -1020,31 +1038,28 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) # Test that we can list profiles - wait = wait_for_create(self.user_cloud.get_cluster_profile_by_id, - {'name_or_id': profile['id']}) + wait = wait_for_create( + self.user_cloud.get_cluster_profile_by_id, + {'name_or_id': profile['id']}, + ) get_profile = self.user_cloud.get_cluster_profile_by_id(profile['id']) @@ -1058,24 +1073,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -1096,24 +1106,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -1121,7 +1126,8 @@ class TestClustering(base.BaseFunctionalTest): # is updated profile_update = self.user_cloud.update_cluster_profile( - profile['id'], new_name='new_profile_name') + profile['id'], new_name='new_profile_name' + ) self.assertEqual(profile_update['profile']['id'], profile['id']) self.assertEqual(profile_update['profile']['spec'], profile['spec']) self.assertEqual(profile_update['profile']['name'], 'new_profile_name') @@ -1132,24 +1138,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -1164,20 +1165,21 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) self.addCleanup(self.cleanup_policy, policy['id']) @@ -1202,20 +1204,21 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) self.addCleanup(self.cleanup_policy, policy['id']) @@ -1238,20 +1241,21 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) self.addCleanup(self.cleanup_policy, policy['id']) @@ -1259,7 +1263,8 @@ class TestClustering(base.BaseFunctionalTest): # is updated policy_update = self.user_cloud.update_cluster_policy( - policy['id'], new_name='new_policy_name') + policy['id'], new_name='new_policy_name' + ) self.assertEqual(policy_update['policy']['id'], policy['id']) self.assertEqual(policy_update['policy']['spec'], policy['spec']) self.assertEqual(policy_update['policy']['name'], 'new_policy_name') @@ -1271,26 +1276,26 @@ class TestClustering(base.BaseFunctionalTest): "adjustment": { "min_step": 1, "number": 1, - "type": "CHANGE_IN_CAPACITY" + "type": "CHANGE_IN_CAPACITY", }, - "event": "CLUSTER_SCALE_IN" + "event": "CLUSTER_SCALE_IN", }, "type": "senlin.policy.scaling", - "version": "1.0" + "version": "1.0", } self.addDetail('policy', content.text_content(policy_name)) # Test that we can create a policy and we get it returned - policy = self.user_cloud.create_cluster_policy(name=policy_name, - spec=spec) + policy = self.user_cloud.create_cluster_policy( + name=policy_name, spec=spec + ) self.addCleanup(self.cleanup_policy, policy['id']) # Test that we can delete a policy and get True returned - policy_delete = self.user_cloud.delete_cluster_policy( - policy['id']) + policy_delete = self.user_cloud.delete_cluster_policy(policy['id']) self.assertTrue(policy_delete) def test_get_cluster_receiver_by_id(self): @@ -1299,24 +1304,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -1327,8 +1327,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -1340,16 +1342,19 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a receiver and we get it returned receiver = self.user_cloud.create_cluster_receiver( - name=receiver_name, receiver_type=receiver_type, + name=receiver_name, + receiver_type=receiver_type, cluster_name_or_id=cluster['cluster']['id'], - action='CLUSTER_SCALE_OUT') + action='CLUSTER_SCALE_OUT', + ) self.addCleanup(self.cleanup_receiver, receiver['id']) # Test that we get the same receiver with the get_receiver method receiver_get = self.user_cloud.get_cluster_receiver_by_id( - receiver['id']) + receiver['id'] + ) self.assertEqual(receiver_get['id'], receiver["id"]) def test_update_cluster_receiver(self): @@ -1358,24 +1363,19 @@ class TestClustering(base.BaseFunctionalTest): "properties": { "flavor": self.flavor.name, "image": self.image.name, - "networks": [ - { - "network": "private" - } - ], - "security_groups": [ - "default" - ] + "networks": [{"network": "private"}], + "security_groups": ["default"], }, "type": "os.nova.server", - "version": 1.0 + "version": 1.0, } self.addDetail('profile', content.text_content(profile_name)) # Test that we can create a profile and we get it returned - profile = self.user_cloud.create_cluster_profile(name=profile_name, - spec=spec) + profile = self.user_cloud.create_cluster_profile( + name=profile_name, spec=spec + ) self.addCleanup(self.cleanup_profile, profile['id']) @@ -1386,8 +1386,10 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a cluster and we get it returned cluster = self.user_cloud.create_cluster( - name=cluster_name, profile=profile, - desired_capacity=desired_capacity) + name=cluster_name, + profile=profile, + desired_capacity=desired_capacity, + ) self.addCleanup(self.cleanup_cluster, cluster['cluster']['id']) @@ -1399,9 +1401,11 @@ class TestClustering(base.BaseFunctionalTest): # Test that we can create a receiver and we get it returned receiver = self.user_cloud.create_cluster_receiver( - name=receiver_name, receiver_type=receiver_type, + name=receiver_name, + receiver_type=receiver_type, cluster_name_or_id=cluster['cluster']['id'], - action='CLUSTER_SCALE_OUT') + action='CLUSTER_SCALE_OUT', + ) self.addCleanup(self.cleanup_receiver, receiver['id']) @@ -1409,13 +1413,16 @@ class TestClustering(base.BaseFunctionalTest): # is updated receiver_update = self.user_cloud.update_cluster_receiver( - receiver['id'], new_name='new_receiver_name') + receiver['id'], new_name='new_receiver_name' + ) self.assertEqual(receiver_update['receiver']['id'], receiver['id']) self.assertEqual(receiver_update['receiver']['type'], receiver['type']) - self.assertEqual(receiver_update['receiver']['cluster_id'], - receiver['cluster_id']) - self.assertEqual(receiver_update['receiver']['name'], - 'new_receiver_name') + self.assertEqual( + receiver_update['receiver']['cluster_id'], receiver['cluster_id'] + ) + self.assertEqual( + receiver_update['receiver']['name'], 'new_receiver_name' + ) def cleanup_profile(self, name): time.sleep(5) @@ -1431,8 +1438,9 @@ class TestClustering(base.BaseFunctionalTest): if cluster_name is not None: cluster = self.user_cloud.get_cluster_by_id(cluster_name) policy = self.user_cloud.get_cluster_policy_by_id(name) - policy_status = \ - self.user_cloud.get_cluster_by_id(cluster['id'])['policies'] + policy_status = self.user_cloud.get_cluster_by_id(cluster['id'])[ + 'policies' + ] if policy_status != []: self.user_cloud.detach_policy_from_cluster(cluster, policy) self.user_cloud.delete_cluster_policy(name) diff --git a/openstack/tests/functional/cloud/test_compute.py b/openstack/tests/functional/cloud/test_compute.py index c97ef5fe6..becb67cce 100644 --- a/openstack/tests/functional/cloud/test_compute.py +++ b/openstack/tests/functional/cloud/test_compute.py @@ -59,7 +59,8 @@ class TestCompute(base.BaseFunctionalTest): self.user_cloud.delete_server(server.name) for volume in volumes: self.operator_cloud.delete_volume( - volume.id, wait=False, force=True) + volume.id, wait=False, force=True + ) def test_create_and_delete_server(self): self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) @@ -67,13 +68,15 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) + wait=True, + ) self.assertEqual(self.server_name, server['name']) self.assertEqual(self.image.id, server['image']['id']) self.assertEqual(self.flavor.name, server['flavor']['original_name']) self.assertIsNotNone(server['adminPass']) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) srv = self.user_cloud.get_server(self.server_name) self.assertTrue(srv is None or srv.status.lower() == 'deleted') @@ -84,14 +87,17 @@ class TestCompute(base.BaseFunctionalTest): image=self.image, flavor=self.flavor, auto_ip=True, - wait=True) + wait=True, + ) self.assertEqual(self.server_name, server['name']) self.assertEqual(self.image.id, server['image']['id']) self.assertEqual(self.flavor.name, server['flavor']['original_name']) self.assertIsNotNone(server['adminPass']) self.assertTrue( self.user_cloud.delete_server( - self.server_name, wait=True, delete_ips=True)) + self.server_name, wait=True, delete_ips=True + ) + ) srv = self.user_cloud.get_server(self.server_name) self.assertTrue(srv is None or srv.status.lower() == 'deleted') @@ -100,8 +106,8 @@ class TestCompute(base.BaseFunctionalTest): server_name = self.getUniqueString() self.addCleanup(self._cleanup_servers_and_volumes, server_name) server = self.user_cloud.create_server( - name=server_name, image=self.image, flavor=self.flavor, - wait=True) + name=server_name, image=self.image, flavor=self.flavor, wait=True + ) volume = self.user_cloud.create_volume(1) vol_attachment = self.user_cloud.attach_volume(server, volume) for key in ('device', 'serverId', 'volumeId'): @@ -116,14 +122,16 @@ class TestCompute(base.BaseFunctionalTest): image=self.image, flavor=self.flavor, config_drive=True, - wait=True) + wait=True, + ) self.assertEqual(self.server_name, server['name']) self.assertEqual(self.image.id, server['image']['id']) self.assertEqual(self.flavor.name, server['flavor']['original_name']) self.assertTrue(server['has_config_drive']) self.assertIsNotNone(server['adminPass']) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) srv = self.user_cloud.get_server(self.server_name) self.assertTrue(srv is None or srv.status.lower() == 'deleted') @@ -137,15 +145,16 @@ class TestCompute(base.BaseFunctionalTest): image=self.image, flavor=self.flavor, config_drive=None, - wait=True) + wait=True, + ) self.assertEqual(self.server_name, server['name']) self.assertEqual(self.image.id, server['image']['id']) self.assertEqual(self.flavor.name, server['flavor']['original_name']) self.assertFalse(server['has_config_drive']) self.assertIsNotNone(server['adminPass']) self.assertTrue( - self.user_cloud.delete_server( - self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) srv = self.user_cloud.get_server(self.server_name) self.assertTrue(srv is None or srv.status.lower() == 'deleted') @@ -157,7 +166,8 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) + wait=True, + ) # We're going to get servers from other tests, but that's ok, as long # as we get the server we created with the demo user. found_server = False @@ -171,7 +181,8 @@ class TestCompute(base.BaseFunctionalTest): self.assertRaises( exc.OpenStackCloudException, self.user_cloud.list_servers, - all_projects=True) + all_projects=True, + ) def test_create_server_image_flavor_dict(self): self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) @@ -179,13 +190,15 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image={'id': self.image.id}, flavor={'id': self.flavor.id}, - wait=True) + wait=True, + ) self.assertEqual(self.server_name, server['name']) self.assertEqual(self.image.id, server['image']['id']) self.assertEqual(self.flavor.name, server['flavor']['original_name']) self.assertIsNotNone(server['adminPass']) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) srv = self.user_cloud.get_server(self.server_name) self.assertTrue(srv is None or srv.status.lower() == 'deleted') @@ -195,7 +208,8 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) + wait=True, + ) # _get_server_console_output does not trap HTTP exceptions, so this # returning a string tests that the call is correct. Testing that # the cloud returns actual data in the output is out of scope. @@ -208,19 +222,22 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) + wait=True, + ) log = self.user_cloud.get_server_console(server=self.server_name) self.assertIsInstance(log, str) def test_list_availability_zone_names(self): self.assertEqual( - ['nova'], self.user_cloud.list_availability_zone_names()) + ['nova'], self.user_cloud.list_availability_zone_names() + ) def test_get_server_console_bad_server(self): self.assertRaises( exc.OpenStackCloudException, self.user_cloud.get_server_console, - server=self.server_name) + server=self.server_name, + ) def test_create_and_delete_server_with_admin_pass(self): self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) @@ -229,27 +246,33 @@ class TestCompute(base.BaseFunctionalTest): image=self.image, flavor=self.flavor, admin_pass='sheiqu9loegahSh', - wait=True) + wait=True, + ) self.assertEqual(self.server_name, server['name']) self.assertEqual(self.image.id, server['image']['id']) self.assertEqual(self.flavor.name, server['flavor']['original_name']) self.assertEqual(server['adminPass'], 'sheiqu9loegahSh') self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) srv = self.user_cloud.get_server(self.server_name) self.assertTrue(srv is None or srv.status.lower() == 'deleted') def test_get_image_id(self): self.assertEqual( - self.image.id, self.user_cloud.get_image_id(self.image.id)) + self.image.id, self.user_cloud.get_image_id(self.image.id) + ) self.assertEqual( - self.image.id, self.user_cloud.get_image_id(self.image.name)) + self.image.id, self.user_cloud.get_image_id(self.image.name) + ) def test_get_image_name(self): self.assertEqual( - self.image.name, self.user_cloud.get_image_name(self.image.id)) + self.image.name, self.user_cloud.get_image_name(self.image.id) + ) self.assertEqual( - self.image.name, self.user_cloud.get_image_name(self.image.name)) + self.image.name, self.user_cloud.get_image_name(self.image.name) + ) def _assert_volume_attach(self, server, volume_id=None, image=''): self.assertEqual(self.server_name, server['name']) @@ -277,7 +300,8 @@ class TestCompute(base.BaseFunctionalTest): flavor=self.flavor, boot_from_volume=True, volume_size=1, - wait=True) + wait=True, + ) volume_id = self._assert_volume_attach(server) volume = self.user_cloud.get_volume(volume_id) self.assertIsNotNone(volume) @@ -296,13 +320,18 @@ class TestCompute(base.BaseFunctionalTest): # deleting a server that had had a volume attached. Yay for eventual # consistency! for count in utils.iterate_timeout( - 60, - 'Timeout waiting for volume {volume_id} to detach'.format( - volume_id=volume_id)): + 60, + 'Timeout waiting for volume {volume_id} to detach'.format( + volume_id=volume_id + ), + ): volume = self.user_cloud.get_volume(volume_id) if volume.status in ( - 'available', 'error', - 'error_restoring', 'error_extending'): + 'available', + 'error', + 'error_restoring', + 'error_extending', + ): return def test_create_terminate_volume_image(self): @@ -317,10 +346,12 @@ class TestCompute(base.BaseFunctionalTest): boot_from_volume=True, terminate_volume=True, volume_size=1, - wait=True) + wait=True, + ) volume_id = self._assert_volume_attach(server) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) volume = self.user_cloud.get_volume(volume_id) # We can either get None (if the volume delete was quick), or a volume # that is in the process of being deleted. @@ -335,7 +366,8 @@ class TestCompute(base.BaseFunctionalTest): self.skipTest('volume service not supported by cloud') self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) volume = self.user_cloud.create_volume( - size=1, name=self.server_name, image=self.image, wait=True) + size=1, name=self.server_name, image=self.image, wait=True + ) self.addCleanup(self.user_cloud.delete_volume, volume.id) server = self.user_cloud.create_server( name=self.server_name, @@ -343,10 +375,12 @@ class TestCompute(base.BaseFunctionalTest): flavor=self.flavor, boot_volume=volume, volume_size=1, - wait=True) + wait=True, + ) volume_id = self._assert_volume_attach(server, volume_id=volume['id']) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) volume = self.user_cloud.get_volume(volume_id) self.assertIsNotNone(volume) self.assertEqual(volume['name'], volume['display_name']) @@ -364,7 +398,8 @@ class TestCompute(base.BaseFunctionalTest): self.skipTest('volume service not supported by cloud') self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) volume = self.user_cloud.create_volume( - size=1, name=self.server_name, image=self.image, wait=True) + size=1, name=self.server_name, image=self.image, wait=True + ) self.addCleanup(self.user_cloud.delete_volume, volume['id']) server = self.user_cloud.create_server( name=self.server_name, @@ -372,11 +407,14 @@ class TestCompute(base.BaseFunctionalTest): image=self.image, boot_from_volume=False, volumes=[volume], - wait=True) + wait=True, + ) volume_id = self._assert_volume_attach( - server, volume_id=volume['id'], image={'id': self.image['id']}) + server, volume_id=volume['id'], image={'id': self.image['id']} + ) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) volume = self.user_cloud.get_volume(volume_id) self.assertIsNotNone(volume) self.assertEqual(volume['name'], volume['display_name']) @@ -393,7 +431,8 @@ class TestCompute(base.BaseFunctionalTest): self.skipTest('volume service not supported by cloud') self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) volume = self.user_cloud.create_volume( - size=1, name=self.server_name, image=self.image, wait=True) + size=1, name=self.server_name, image=self.image, wait=True + ) server = self.user_cloud.create_server( name=self.server_name, image=None, @@ -401,10 +440,12 @@ class TestCompute(base.BaseFunctionalTest): boot_volume=volume, terminate_volume=True, volume_size=1, - wait=True) + wait=True, + ) volume_id = self._assert_volume_attach(server, volume_id=volume['id']) self.assertTrue( - self.user_cloud.delete_server(self.server_name, wait=True)) + self.user_cloud.delete_server(self.server_name, wait=True) + ) volume = self.user_cloud.get_volume(volume_id) # We can either get None (if the volume delete was quick), or a volume # that is in the process of being deleted. @@ -420,9 +461,11 @@ class TestCompute(base.BaseFunctionalTest): image=self.image, flavor=self.flavor, admin_pass='sheiqu9loegahSh', - wait=True) - image = self.user_cloud.create_image_snapshot('test-snapshot', server, - wait=True) + wait=True, + ) + image = self.user_cloud.create_image_snapshot( + 'test-snapshot', server, wait=True + ) self.addCleanup(self.user_cloud.delete_image, image['id']) self.assertEqual('active', image['status']) @@ -432,24 +475,32 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) - self.user_cloud.set_server_metadata(self.server_name, - {'key1': 'value1', - 'key2': 'value2'}) + wait=True, + ) + self.user_cloud.set_server_metadata( + self.server_name, {'key1': 'value1', 'key2': 'value2'} + ) updated_server = self.user_cloud.get_server(self.server_name) - self.assertEqual(set(updated_server.metadata.items()), - set({'key1': 'value1', 'key2': 'value2'}.items())) + self.assertEqual( + set(updated_server.metadata.items()), + set({'key1': 'value1', 'key2': 'value2'}.items()), + ) - self.user_cloud.set_server_metadata(self.server_name, - {'key2': 'value3'}) + self.user_cloud.set_server_metadata( + self.server_name, {'key2': 'value3'} + ) updated_server = self.user_cloud.get_server(self.server_name) - self.assertEqual(set(updated_server.metadata.items()), - set({'key1': 'value1', 'key2': 'value3'}.items())) + self.assertEqual( + set(updated_server.metadata.items()), + set({'key1': 'value1', 'key2': 'value3'}.items()), + ) self.user_cloud.delete_server_metadata(self.server_name, ['key2']) updated_server = self.user_cloud.get_server(self.server_name) - self.assertEqual(set(updated_server.metadata.items()), - set({'key1': 'value1'}.items())) + self.assertEqual( + set(updated_server.metadata.items()), + set({'key1': 'value1'}.items()), + ) self.user_cloud.delete_server_metadata(self.server_name, ['key1']) updated_server = self.user_cloud.get_server(self.server_name) @@ -458,7 +509,9 @@ class TestCompute(base.BaseFunctionalTest): self.assertRaises( exc.OpenStackCloudURINotFound, self.user_cloud.delete_server_metadata, - self.server_name, ['key1']) + self.server_name, + ['key1'], + ) def test_update_server(self): self.addCleanup(self._cleanup_servers_and_volumes, self.server_name) @@ -466,10 +519,10 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) + wait=True, + ) server_updated = self.user_cloud.update_server( - self.server_name, - name='new_name' + self.server_name, name='new_name' ) self.assertEqual('new_name', server_updated['name']) @@ -484,7 +537,8 @@ class TestCompute(base.BaseFunctionalTest): name=self.server_name, image=self.image, flavor=self.flavor, - wait=True) + wait=True, + ) start = datetime.datetime.now() - datetime.timedelta(seconds=5) usage = self.operator_cloud.get_compute_usage('demo', start) self.add_info_on_exception('usage', usage) diff --git a/openstack/tests/functional/cloud/test_devstack.py b/openstack/tests/functional/cloud/test_devstack.py index 52cced22a..a6754009a 100644 --- a/openstack/tests/functional/cloud/test_devstack.py +++ b/openstack/tests/functional/cloud/test_devstack.py @@ -30,16 +30,18 @@ class TestDevstack(base.BaseFunctionalTest): scenarios = [ ('designate', dict(env='DESIGNATE', service='dns')), ('heat', dict(env='HEAT', service='orchestration')), - ('magnum', dict( - env='MAGNUM', - service='container-infrastructure-management' - )), + ( + 'magnum', + dict(env='MAGNUM', service='container-infrastructure-management'), + ), ('neutron', dict(env='NEUTRON', service='network')), ('octavia', dict(env='OCTAVIA', service='load-balancer')), ('swift', dict(env='SWIFT', service='object-store')), ] def test_has_service(self): - if os.environ.get( - 'OPENSTACKSDK_HAS_{env}'.format(env=self.env), '0') == '1': + if ( + os.environ.get('OPENSTACKSDK_HAS_{env}'.format(env=self.env), '0') + == '1' + ): self.assertTrue(self.user_cloud.has_service(self.service)) diff --git a/openstack/tests/functional/cloud/test_domain.py b/openstack/tests/functional/cloud/test_domain.py index 447864356..f5f2ea210 100644 --- a/openstack/tests/functional/cloud/test_domain.py +++ b/openstack/tests/functional/cloud/test_domain.py @@ -22,7 +22,6 @@ from openstack.tests.functional import base class TestDomain(base.BaseFunctionalTest): - def setUp(self): super(TestDomain, self).setUp() if not self.operator_cloud: @@ -47,14 +46,16 @@ class TestDomain(base.BaseFunctionalTest): # Raise an error: we must make users aware that something went # wrong raise openstack.cloud.OpenStackCloudException( - '\n'.join(exception_list)) + '\n'.join(exception_list) + ) def test_search_domains(self): domain_name = self.domain_prefix + '_search' # Shouldn't find any domain with this name yet results = self.operator_cloud.search_domains( - filters=dict(name=domain_name)) + filters=dict(name=domain_name) + ) self.assertEqual(0, len(results)) # Now create a new domain @@ -63,7 +64,8 @@ class TestDomain(base.BaseFunctionalTest): # Now we should find only the new domain results = self.operator_cloud.search_domains( - filters=dict(name=domain_name)) + filters=dict(name=domain_name) + ) self.assertEqual(1, len(results)) self.assertEqual(domain_name, results[0]['name']) @@ -74,13 +76,17 @@ class TestDomain(base.BaseFunctionalTest): def test_update_domain(self): domain = self.operator_cloud.create_domain( - self.domain_prefix, 'description') + self.domain_prefix, 'description' + ) self.assertEqual(self.domain_prefix, domain['name']) self.assertEqual('description', domain['description']) self.assertTrue(domain['enabled']) updated = self.operator_cloud.update_domain( - domain['id'], name='updated name', - description='updated description', enabled=False) + domain['id'], + name='updated name', + description='updated description', + enabled=False, + ) self.assertEqual('updated name', updated['name']) self.assertEqual('updated description', updated['description']) self.assertFalse(updated['enabled']) @@ -91,14 +97,16 @@ class TestDomain(base.BaseFunctionalTest): name_or_id='updated name', name='updated name 2', description='updated description 2', - enabled=True) + enabled=True, + ) self.assertEqual('updated name 2', updated['name']) self.assertEqual('updated description 2', updated['description']) self.assertTrue(updated['enabled']) def test_delete_domain(self): - domain = self.operator_cloud.create_domain(self.domain_prefix, - 'description') + domain = self.operator_cloud.create_domain( + self.domain_prefix, 'description' + ) self.assertEqual(self.domain_prefix, domain['name']) self.assertEqual('description', domain['description']) self.assertTrue(domain['enabled']) @@ -107,7 +115,8 @@ class TestDomain(base.BaseFunctionalTest): # Now we delete domain by name with name_or_id domain = self.operator_cloud.create_domain( - self.domain_prefix, 'description') + self.domain_prefix, 'description' + ) self.assertEqual(self.domain_prefix, domain['name']) self.assertEqual('description', domain['description']) self.assertTrue(domain['enabled']) @@ -117,7 +126,8 @@ class TestDomain(base.BaseFunctionalTest): # Finally, we assert we get False from delete_domain if domain does # not exist domain = self.operator_cloud.create_domain( - self.domain_prefix, 'description') + self.domain_prefix, 'description' + ) self.assertEqual(self.domain_prefix, domain['name']) self.assertEqual('description', domain['description']) self.assertTrue(domain['enabled']) diff --git a/openstack/tests/functional/cloud/test_endpoints.py b/openstack/tests/functional/cloud/test_endpoints.py index 2381f9775..d27dd9dda 100644 --- a/openstack/tests/functional/cloud/test_endpoints.py +++ b/openstack/tests/functional/cloud/test_endpoints.py @@ -29,8 +29,14 @@ from openstack.tests.functional import base class TestEndpoints(base.KeystoneBaseFunctionalTest): - endpoint_attributes = ['id', 'region', 'publicurl', 'internalurl', - 'service_id', 'adminurl'] + endpoint_attributes = [ + 'id', + 'region', + 'publicurl', + 'internalurl', + 'service_id', + 'adminurl', + ] def setUp(self): super(TestEndpoints, self).setUp() @@ -39,7 +45,8 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): # Generate a random name for services and regions in this test self.new_item_name = 'test_' + ''.join( - random.choice(string.ascii_lowercase) for _ in range(5)) + random.choice(string.ascii_lowercase) for _ in range(5) + ) self.addCleanup(self._cleanup_services) self.addCleanup(self._cleanup_endpoints) @@ -47,8 +54,9 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): def _cleanup_endpoints(self): exception_list = list() for e in self.operator_cloud.list_endpoints(): - if e.get('region') is not None and \ - e['region'].startswith(self.new_item_name): + if e.get('region') is not None and e['region'].startswith( + self.new_item_name + ): try: self.operator_cloud.delete_endpoint(id=e['id']) except Exception as e: @@ -63,8 +71,9 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): def _cleanup_services(self): exception_list = list() for s in self.operator_cloud.list_services(): - if s['name'] is not None and \ - s['name'].startswith(self.new_item_name): + if s['name'] is not None and s['name'].startswith( + self.new_item_name + ): try: self.operator_cloud.delete_service(name_or_id=s['id']) except Exception as e: @@ -82,15 +91,18 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): region = list(self.operator_cloud.identity.regions())[0].id service = self.operator_cloud.create_service( - name=service_name, type='test_type', - description='this is a test description') + name=service_name, + type='test_type', + description='this is a test description', + ) endpoints = self.operator_cloud.create_endpoint( service_name_or_id=service['id'], public_url='http://public.test/', internal_url='http://internal.test/', admin_url='http://admin.url/', - region=region) + region=region, + ) self.assertNotEqual([], endpoints) self.assertIsNotNone(endpoints[0].get('id')) @@ -99,7 +111,8 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): endpoints = self.operator_cloud.create_endpoint( service_name_or_id=service['id'], public_url='http://public.test/', - region=region) + region=region, + ) self.assertNotEqual([], endpoints) self.assertIsNotNone(endpoints[0].get('id')) @@ -108,32 +121,38 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): ver = self.operator_cloud.config.get_api_version('identity') if ver.startswith('2'): # NOTE(SamYaple): Update endpoint only works with v3 api - self.assertRaises(OpenStackCloudUnavailableFeature, - self.operator_cloud.update_endpoint, - 'endpoint_id1') + self.assertRaises( + OpenStackCloudUnavailableFeature, + self.operator_cloud.update_endpoint, + 'endpoint_id1', + ) else: # service operations require existing region. Do not test updating # region for now region = list(self.operator_cloud.identity.regions())[0].id service = self.operator_cloud.create_service( - name='service1', type='test_type') + name='service1', type='test_type' + ) endpoint = self.operator_cloud.create_endpoint( service_name_or_id=service['id'], url='http://admin.url/', interface='admin', region=region, - enabled=False)[0] + enabled=False, + )[0] new_service = self.operator_cloud.create_service( - name='service2', type='test_type') + name='service2', type='test_type' + ) new_endpoint = self.operator_cloud.update_endpoint( endpoint.id, service_name_or_id=new_service.id, url='http://public.url/', interface='public', region=region, - enabled=True) + enabled=True, + ) self.assertEqual(new_endpoint.url, 'http://public.url/') self.assertEqual(new_endpoint.interface, 'public') @@ -147,14 +166,17 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): region = list(self.operator_cloud.identity.regions())[0].id service = self.operator_cloud.create_service( - name=service_name, type='test_type', - description='this is a test description') + name=service_name, + type='test_type', + description='this is a test description', + ) endpoints = self.operator_cloud.create_endpoint( service_name_or_id=service['id'], public_url='http://public.test/', internal_url='http://internal.test/', - region=region) + region=region, + ) observed_endpoints = self.operator_cloud.list_endpoints() found = False @@ -170,10 +192,10 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): elif e['interface'] == 'public': self.assertEqual('http://public.test/', e['url']) else: - self.assertEqual('http://public.test/', - e['publicurl']) - self.assertEqual('http://internal.test/', - e['internalurl']) + self.assertEqual('http://public.test/', e['publicurl']) + self.assertEqual( + 'http://internal.test/', e['internalurl'] + ) self.assertEqual(region, e['region_id']) self.assertTrue(found, msg='new endpoint not found in endpoints list!') @@ -184,14 +206,17 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): region = list(self.operator_cloud.identity.regions())[0].id service = self.operator_cloud.create_service( - name=service_name, type='test_type', - description='this is a test description') + name=service_name, + type='test_type', + description='this is a test description', + ) endpoints = self.operator_cloud.create_endpoint( service_name_or_id=service['id'], public_url='http://public.test/', internal_url='http://internal.test/', - region=region) + region=region, + ) self.assertNotEqual([], endpoints) for endpoint in endpoints: @@ -204,5 +229,4 @@ class TestEndpoints(base.KeystoneBaseFunctionalTest): if e['id'] == endpoint['id']: found = True break - self.assertEqual( - False, found, message='new endpoint was not deleted!') + self.assertEqual(False, found, message='new endpoint was not deleted!') diff --git a/openstack/tests/functional/cloud/test_flavor.py b/openstack/tests/functional/cloud/test_flavor.py index f1a76b7ff..5d3e94241 100644 --- a/openstack/tests/functional/cloud/test_flavor.py +++ b/openstack/tests/functional/cloud/test_flavor.py @@ -24,7 +24,6 @@ from openstack.tests.functional import base class TestFlavor(base.BaseFunctionalTest): - def setUp(self): super(TestFlavor, self).setUp() @@ -56,8 +55,14 @@ class TestFlavor(base.BaseFunctionalTest): flavor_name = self.new_item_name + '_create' flavor_kwargs = dict( - name=flavor_name, ram=1024, vcpus=2, disk=10, ephemeral=5, - swap=100, rxtx_factor=1.5, is_public=True + name=flavor_name, + ram=1024, + vcpus=2, + disk=10, + ephemeral=5, + swap=100, + rxtx_factor=1.5, + is_public=True, ) flavor = self.operator_cloud.create_flavor(**flavor_kwargs) @@ -144,8 +149,9 @@ class TestFlavor(base.BaseFunctionalTest): self.assertEqual(project['id'], acls[0]['tenant_id']) # Now revoke the access and make sure we can't find it - self.operator_cloud.remove_flavor_access(new_flavor['id'], - project['id']) + self.operator_cloud.remove_flavor_access( + new_flavor['id'], project['id'] + ) flavors = self.user_cloud.search_flavors(priv_flavor_name) self.assertEqual(0, len(flavors)) @@ -157,9 +163,7 @@ class TestFlavor(base.BaseFunctionalTest): self.skipTest("Operator cloud is required for this test") flavor_name = self.new_item_name + '_spec_test' - kwargs = dict( - name=flavor_name, ram=1024, vcpus=2, disk=10 - ) + kwargs = dict(name=flavor_name, ram=1024, vcpus=2, disk=10) new_flavor = self.operator_cloud.create_flavor(**kwargs) # Expect no extra_specs @@ -169,7 +173,8 @@ class TestFlavor(base.BaseFunctionalTest): extra_specs = {'foo': 'aaa', 'bar': 'bbb'} self.operator_cloud.set_flavor_specs(new_flavor['id'], extra_specs) mod_flavor = self.operator_cloud.get_flavor( - new_flavor['id'], get_extra=True) + new_flavor['id'], get_extra=True + ) # Verify extra_specs were set self.assertIn('extra_specs', mod_flavor) @@ -178,7 +183,8 @@ class TestFlavor(base.BaseFunctionalTest): # Unset the 'foo' value self.operator_cloud.unset_flavor_specs(mod_flavor['id'], ['foo']) mod_flavor = self.operator_cloud.get_flavor_by_id( - new_flavor['id'], get_extra=True) + new_flavor['id'], get_extra=True + ) # Verify 'foo' is unset and 'bar' is still set self.assertEqual({'bar': 'bbb'}, mod_flavor['extra_specs']) diff --git a/openstack/tests/functional/cloud/test_floating_ip.py b/openstack/tests/functional/cloud/test_floating_ip.py index 23610c4a3..34e631059 100644 --- a/openstack/tests/functional/cloud/test_floating_ip.py +++ b/openstack/tests/functional/cloud/test_floating_ip.py @@ -54,12 +54,14 @@ class TestFloatingIP(base.BaseFunctionalTest): try: if r['name'].startswith(self.new_item_name): self.user_cloud.update_router( - r, ext_gateway_net_id=None) + r, ext_gateway_net_id=None + ) for s in self.user_cloud.list_subnets(): if s['name'].startswith(self.new_item_name): try: self.user_cloud.remove_router_interface( - r, subnet_id=s['id']) + r, subnet_id=s['id'] + ) except Exception: pass self.user_cloud.delete_router(r.id) @@ -93,7 +95,9 @@ class TestFloatingIP(base.BaseFunctionalTest): self.addDetail( 'exceptions', content.text_content( - '\n'.join([str(ex) for ex in exception_list]))) + '\n'.join([str(ex) for ex in exception_list]) + ), + ) exc = exception_list[0] raise exc @@ -121,8 +125,10 @@ class TestFloatingIP(base.BaseFunctionalTest): fixed_ip = meta.get_server_private_ip(server) for ip in self.user_cloud.list_floating_ips(): - if (ip.get('fixed_ip', None) == fixed_ip - or ip.get('fixed_ip_address', None) == fixed_ip): + if ( + ip.get('fixed_ip', None) == fixed_ip + or ip.get('fixed_ip_address', None) == fixed_ip + ): try: self.user_cloud.delete_floating_ip(ip.id) except Exception as e: @@ -138,42 +144,49 @@ class TestFloatingIP(base.BaseFunctionalTest): if self.user_cloud.has_service('network'): # Create a network self.test_net = self.user_cloud.create_network( - name=self.new_item_name + '_net') + name=self.new_item_name + '_net' + ) # Create a subnet on it self.test_subnet = self.user_cloud.create_subnet( subnet_name=self.new_item_name + '_subnet', network_name_or_id=self.test_net['id'], cidr='10.24.4.0/24', - enable_dhcp=True + enable_dhcp=True, ) # Create a router self.test_router = self.user_cloud.create_router( - name=self.new_item_name + '_router') + name=self.new_item_name + '_router' + ) # Attach the router to an external network ext_nets = self.user_cloud.search_networks( - filters={'router:external': True}) + filters={'router:external': True} + ) self.user_cloud.update_router( name_or_id=self.test_router['id'], - ext_gateway_net_id=ext_nets[0]['id']) + ext_gateway_net_id=ext_nets[0]['id'], + ) # Attach the router to the internal subnet self.user_cloud.add_router_interface( - self.test_router, subnet_id=self.test_subnet['id']) + self.test_router, subnet_id=self.test_subnet['id'] + ) # Select the network for creating new servers self.nic = {'net-id': self.test_net['id']} self.addDetail( 'networks-neutron', - content.text_content(pprint.pformat( - self.user_cloud.list_networks()))) + content.text_content( + pprint.pformat(self.user_cloud.list_networks()) + ), + ) else: # Find network names for nova-net data = proxy._json_response( - self.user_cloud._conn.compute.get('/os-tenant-networks')) + self.user_cloud._conn.compute.get('/os-tenant-networks') + ) nets = meta.get_and_munchify('networks', data) self.addDetail( - 'networks-nova', - content.text_content(pprint.pformat( - nets))) + 'networks-nova', content.text_content(pprint.pformat(nets)) + ) self.nic = {'net-id': nets[0].id} def test_private_ip(self): @@ -181,27 +194,36 @@ class TestFloatingIP(base.BaseFunctionalTest): new_server = self.user_cloud.get_openstack_vars( self.user_cloud.create_server( - wait=True, name=self.new_item_name + '_server', + wait=True, + name=self.new_item_name + '_server', image=self.image, - flavor=self.flavor, nics=[self.nic])) + flavor=self.flavor, + nics=[self.nic], + ) + ) self.addDetail( - 'server', content.text_content(pprint.pformat(new_server))) + 'server', content.text_content(pprint.pformat(new_server)) + ) self.assertNotEqual(new_server['private_v4'], '') def test_add_auto_ip(self): self._setup_networks() new_server = self.user_cloud.create_server( - wait=True, name=self.new_item_name + '_server', + wait=True, + name=self.new_item_name + '_server', image=self.image, - flavor=self.flavor, nics=[self.nic]) + flavor=self.flavor, + nics=[self.nic], + ) # ToDo: remove the following iteration when create_server waits for # the IP to be attached ip = None for _ in utils.iterate_timeout( - self.timeout, "Timeout waiting for IP address to be attached"): + self.timeout, "Timeout waiting for IP address to be attached" + ): ip = meta.get_server_external_ipv4(self.user_cloud, new_server) if ip is not None: break @@ -213,15 +235,19 @@ class TestFloatingIP(base.BaseFunctionalTest): self._setup_networks() new_server = self.user_cloud.create_server( - wait=True, name=self.new_item_name + '_server', + wait=True, + name=self.new_item_name + '_server', image=self.image, - flavor=self.flavor, nics=[self.nic]) + flavor=self.flavor, + nics=[self.nic], + ) # ToDo: remove the following iteration when create_server waits for # the IP to be attached ip = None for _ in utils.iterate_timeout( - self.timeout, "Timeout waiting for IP address to be attached"): + self.timeout, "Timeout waiting for IP address to be attached" + ): ip = meta.get_server_external_ipv4(self.user_cloud, new_server) if ip is not None: break @@ -230,15 +256,18 @@ class TestFloatingIP(base.BaseFunctionalTest): self.addCleanup(self._cleanup_ips, new_server) f_ip = self.user_cloud.get_floating_ip( - id=None, filters={'floating_ip_address': ip}) + id=None, filters={'floating_ip_address': ip} + ) self.user_cloud.detach_ip_from_server( - server_id=new_server.id, floating_ip_id=f_ip['id']) + server_id=new_server.id, floating_ip_id=f_ip['id'] + ) def test_list_floating_ips(self): if self.operator_cloud: fip_admin = self.operator_cloud.create_floating_ip() self.addCleanup( - self.operator_cloud.delete_floating_ip, fip_admin.id) + self.operator_cloud.delete_floating_ip, fip_admin.id + ) fip_user = self.user_cloud.create_floating_ip() self.addCleanup(self.user_cloud.delete_floating_ip, fip_user.id) @@ -260,7 +289,8 @@ class TestFloatingIP(base.BaseFunctionalTest): # Ask Neutron for only a subset of all the FIPs. if self.operator_cloud: filtered_fip_id_list = [ - fip.id for fip in self.operator_cloud.list_floating_ips( + fip.id + for fip in self.operator_cloud.list_floating_ips( {'tenant_id': self.user_cloud.current_project_id} ) ] @@ -275,9 +305,10 @@ class TestFloatingIP(base.BaseFunctionalTest): if self.operator_cloud: self.assertNotIn(fip_user.id, fip_op_id_list) self.assertRaisesRegex( - ValueError, "Nova-network don't support server-side.*", + ValueError, + "Nova-network don't support server-side.*", self.operator_cloud.list_floating_ips, - filters={'foo': 'bar'} + filters={'foo': 'bar'}, ) def test_search_floating_ips(self): @@ -286,7 +317,7 @@ class TestFloatingIP(base.BaseFunctionalTest): self.assertIn( fip_user['id'], - [fip.id for fip in self.user_cloud.search_floating_ips()] + [fip.id for fip in self.user_cloud.search_floating_ips()], ) def test_get_floating_ip_by_id(self): diff --git a/openstack/tests/functional/cloud/test_floating_ip_pool.py b/openstack/tests/functional/cloud/test_floating_ip_pool.py index 2eba99af1..30e84fc3a 100644 --- a/openstack/tests/functional/cloud/test_floating_ip_pool.py +++ b/openstack/tests/functional/cloud/test_floating_ip_pool.py @@ -38,8 +38,7 @@ class TestFloatingIPPool(base.BaseFunctionalTest): if not self.user_cloud._has_nova_extension('os-floating-ip-pools'): # Skipping this test is floating-ip-pool extension is not # available on the testing cloud - self.skip( - 'Floating IP pools extension is not available') + self.skip('Floating IP pools extension is not available') def test_list_floating_ip_pools(self): pools = self.user_cloud.list_floating_ip_pools() diff --git a/openstack/tests/functional/cloud/test_groups.py b/openstack/tests/functional/cloud/test_groups.py index 3ef2a5626..9415b337f 100644 --- a/openstack/tests/functional/cloud/test_groups.py +++ b/openstack/tests/functional/cloud/test_groups.py @@ -22,7 +22,6 @@ from openstack.tests.functional import base class TestGroup(base.BaseFunctionalTest): - def setUp(self): super(TestGroup, self).setUp() if not self.operator_cloud: @@ -48,7 +47,8 @@ class TestGroup(base.BaseFunctionalTest): # Raise an error: we must make users aware that something went # wrong raise openstack.cloud.OpenStackCloudException( - '\n'.join(exception_list)) + '\n'.join(exception_list) + ) def test_create_group(self): group_name = self.group_prefix + '_create' @@ -68,7 +68,8 @@ class TestGroup(base.BaseFunctionalTest): self.assertTrue(self.operator_cloud.delete_group(group_name)) results = self.operator_cloud.search_groups( - filters=dict(name=group_name)) + filters=dict(name=group_name) + ) self.assertEqual(0, len(results)) def test_delete_group_not_exists(self): @@ -79,7 +80,8 @@ class TestGroup(base.BaseFunctionalTest): # Shouldn't find any group with this name yet results = self.operator_cloud.search_groups( - filters=dict(name=group_name)) + filters=dict(name=group_name) + ) self.assertEqual(0, len(results)) # Now create a new group @@ -88,7 +90,8 @@ class TestGroup(base.BaseFunctionalTest): # Now we should find only the new group results = self.operator_cloud.search_groups( - filters=dict(name=group_name)) + filters=dict(name=group_name) + ) self.assertEqual(1, len(results)) self.assertEqual(group_name, results[0]['name']) @@ -103,8 +106,7 @@ class TestGroup(base.BaseFunctionalTest): updated_group_name = group_name + '_xyz' updated_group_desc = group_desc + ' updated' updated_group = self.operator_cloud.update_group( - group_name, - name=updated_group_name, - description=updated_group_desc) + group_name, name=updated_group_name, description=updated_group_desc + ) self.assertEqual(updated_group_name, updated_group['name']) self.assertEqual(updated_group_desc, updated_group['description']) diff --git a/openstack/tests/functional/cloud/test_identity.py b/openstack/tests/functional/cloud/test_identity.py index 118cf33f9..5fc004628 100644 --- a/openstack/tests/functional/cloud/test_identity.py +++ b/openstack/tests/functional/cloud/test_identity.py @@ -30,7 +30,8 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): if not self.operator_cloud: self.skipTest("Operator cloud is required for this test") self.role_prefix = 'test_role' + ''.join( - random.choice(string.ascii_lowercase) for _ in range(5)) + random.choice(string.ascii_lowercase) for _ in range(5) + ) self.user_prefix = self.getUniqueString('user') self.group_prefix = self.getUniqueString('group') @@ -133,7 +134,8 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): user = self.operator_cloud.get_user('demo') project = self.operator_cloud.get_project('demo') assignments = self.operator_cloud.list_role_assignments( - filters={'user': user['id'], 'project': project['id']}) + filters={'user': user['id'], 'project': project['id']} + ) self.assertIsInstance(assignments, list) self.assertGreater(len(assignments), 0) @@ -142,25 +144,35 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): user_email = 'nobody@nowhere.com' role_name = self.role_prefix + '_grant_user_project' role = self.operator_cloud.create_role(role_name) - user = self._create_user(name=user_name, - email=user_email, - default_project='demo') - self.assertTrue(self.operator_cloud.grant_role( - role_name, user=user['id'], project='demo', wait=True)) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'user': user['id'], - 'project': self.operator_cloud.get_project('demo')['id'] - }) + user = self._create_user( + name=user_name, email=user_email, default_project='demo' + ) + self.assertTrue( + self.operator_cloud.grant_role( + role_name, user=user['id'], project='demo', wait=True + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'user': user['id'], + 'project': self.operator_cloud.get_project('demo')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(1, len(assignments)) - self.assertTrue(self.operator_cloud.revoke_role( - role_name, user=user['id'], project='demo', wait=True)) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'user': user['id'], - 'project': self.operator_cloud.get_project('demo')['id'] - }) + self.assertTrue( + self.operator_cloud.revoke_role( + role_name, user=user['id'], project='demo', wait=True + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'user': user['id'], + 'project': self.operator_cloud.get_project('demo')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(0, len(assignments)) @@ -171,25 +183,34 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): role = self.operator_cloud.create_role(role_name) group_name = self.group_prefix + '_group_project' group = self.operator_cloud.create_group( - name=group_name, - description='test group', - domain='default') - self.assertTrue(self.operator_cloud.grant_role( - role_name, group=group['id'], project='demo')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'group': group['id'], - 'project': self.operator_cloud.get_project('demo')['id'] - }) + name=group_name, description='test group', domain='default' + ) + self.assertTrue( + self.operator_cloud.grant_role( + role_name, group=group['id'], project='demo' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'group': group['id'], + 'project': self.operator_cloud.get_project('demo')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(1, len(assignments)) - self.assertTrue(self.operator_cloud.revoke_role( - role_name, group=group['id'], project='demo')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'group': group['id'], - 'project': self.operator_cloud.get_project('demo')['id'] - }) + self.assertTrue( + self.operator_cloud.revoke_role( + role_name, group=group['id'], project='demo' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'group': group['id'], + 'project': self.operator_cloud.get_project('demo')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(0, len(assignments)) @@ -200,25 +221,35 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): role = self.operator_cloud.create_role(role_name) user_name = self.user_prefix + '_user_domain' user_email = 'nobody@nowhere.com' - user = self._create_user(name=user_name, - email=user_email, - default_project='demo') - self.assertTrue(self.operator_cloud.grant_role( - role_name, user=user['id'], domain='default')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'user': user['id'], - 'domain': self.operator_cloud.get_domain('default')['id'] - }) + user = self._create_user( + name=user_name, email=user_email, default_project='demo' + ) + self.assertTrue( + self.operator_cloud.grant_role( + role_name, user=user['id'], domain='default' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'user': user['id'], + 'domain': self.operator_cloud.get_domain('default')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(1, len(assignments)) - self.assertTrue(self.operator_cloud.revoke_role( - role_name, user=user['id'], domain='default')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'user': user['id'], - 'domain': self.operator_cloud.get_domain('default')['id'] - }) + self.assertTrue( + self.operator_cloud.revoke_role( + role_name, user=user['id'], domain='default' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'user': user['id'], + 'domain': self.operator_cloud.get_domain('default')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(0, len(assignments)) @@ -229,25 +260,34 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): role = self.operator_cloud.create_role(role_name) group_name = self.group_prefix + '_group_domain' group = self.operator_cloud.create_group( - name=group_name, - description='test group', - domain='default') - self.assertTrue(self.operator_cloud.grant_role( - role_name, group=group['id'], domain='default')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'group': group['id'], - 'domain': self.operator_cloud.get_domain('default')['id'] - }) + name=group_name, description='test group', domain='default' + ) + self.assertTrue( + self.operator_cloud.grant_role( + role_name, group=group['id'], domain='default' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'group': group['id'], + 'domain': self.operator_cloud.get_domain('default')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(1, len(assignments)) - self.assertTrue(self.operator_cloud.revoke_role( - role_name, group=group['id'], domain='default')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'group': group['id'], - 'domain': self.operator_cloud.get_domain('default')['id'] - }) + self.assertTrue( + self.operator_cloud.revoke_role( + role_name, group=group['id'], domain='default' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + { + 'role': role['id'], + 'group': group['id'], + 'domain': self.operator_cloud.get_domain('default')['id'], + } + ) self.assertIsInstance(assignments, list) self.assertEqual(0, len(assignments)) @@ -256,25 +296,27 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): role = self.operator_cloud.create_role(role_name) user_name = self.user_prefix + '_user_system' user_email = 'nobody@nowhere.com' - user = self._create_user(name=user_name, - email=user_email, - default_project='demo') - self.assertTrue(self.operator_cloud.grant_role( - role_name, user=user['id'], system='all')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'user': user['id'], - 'system': 'all' - }) + user = self._create_user( + name=user_name, email=user_email, default_project='demo' + ) + self.assertTrue( + self.operator_cloud.grant_role( + role_name, user=user['id'], system='all' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + {'role': role['id'], 'user': user['id'], 'system': 'all'} + ) self.assertIsInstance(assignments, list) self.assertEqual(1, len(assignments)) - self.assertTrue(self.operator_cloud.revoke_role( - role_name, user=user['id'], system='all')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'user': user['id'], - 'system': 'all' - }) + self.assertTrue( + self.operator_cloud.revoke_role( + role_name, user=user['id'], system='all' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + {'role': role['id'], 'user': user['id'], 'system': 'all'} + ) self.assertIsInstance(assignments, list) self.assertEqual(0, len(assignments)) @@ -285,23 +327,25 @@ class TestIdentity(base.KeystoneBaseFunctionalTest): role = self.operator_cloud.create_role(role_name) group_name = self.group_prefix + '_group_system' group = self.operator_cloud.create_group( - name=group_name, - description='test group') - self.assertTrue(self.operator_cloud.grant_role( - role_name, group=group['id'], system='all')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'group': group['id'], - 'system': 'all' - }) + name=group_name, description='test group' + ) + self.assertTrue( + self.operator_cloud.grant_role( + role_name, group=group['id'], system='all' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + {'role': role['id'], 'group': group['id'], 'system': 'all'} + ) self.assertIsInstance(assignments, list) self.assertEqual(1, len(assignments)) - self.assertTrue(self.operator_cloud.revoke_role( - role_name, group=group['id'], system='all')) - assignments = self.operator_cloud.list_role_assignments({ - 'role': role['id'], - 'group': group['id'], - 'system': 'all' - }) + self.assertTrue( + self.operator_cloud.revoke_role( + role_name, group=group['id'], system='all' + ) + ) + assignments = self.operator_cloud.list_role_assignments( + {'role': role['id'], 'group': group['id'], 'system': 'all'} + ) self.assertIsInstance(assignments, list) self.assertEqual(0, len(assignments)) diff --git a/openstack/tests/functional/cloud/test_image.py b/openstack/tests/functional/cloud/test_image.py index b6e43fbb0..489f2c1a2 100644 --- a/openstack/tests/functional/cloud/test_image.py +++ b/openstack/tests/functional/cloud/test_image.py @@ -25,7 +25,6 @@ from openstack.tests.functional import base class TestImage(base.BaseFunctionalTest): - def test_create_image(self): test_image = tempfile.NamedTemporaryFile(delete=False) test_image.write(b'\0' * 1024 * 1024) @@ -40,7 +39,8 @@ class TestImage(base.BaseFunctionalTest): min_disk=10, min_ram=1024, tags=['custom'], - wait=True) + wait=True, + ) finally: self.user_cloud.delete_image(image_name, wait=True) @@ -57,13 +57,16 @@ class TestImage(base.BaseFunctionalTest): container_format='bare', min_disk=10, min_ram=1024, - wait=True) + wait=True, + ) self.addCleanup(self.user_cloud.delete_image, image_name, wait=True) output = os.path.join(tempfile.gettempdir(), self.getUniqueString()) self.user_cloud.download_image(image_name, output) self.addCleanup(os.remove, output) - self.assertTrue(filecmp.cmp(test_image.name, output), - "Downloaded contents don't match created image") + self.assertTrue( + filecmp.cmp(test_image.name, output), + "Downloaded contents don't match created image", + ) def test_create_image_skip_duplicate(self): test_image = tempfile.NamedTemporaryFile(delete=False) @@ -79,7 +82,8 @@ class TestImage(base.BaseFunctionalTest): min_disk=10, min_ram=1024, validate_checksum=True, - wait=True) + wait=True, + ) second_image = self.user_cloud.create_image( name=image_name, filename=test_image.name, @@ -88,7 +92,8 @@ class TestImage(base.BaseFunctionalTest): min_disk=10, min_ram=1024, validate_checksum=True, - wait=True) + wait=True, + ) self.assertEqual(first_image.id, second_image.id) finally: self.user_cloud.delete_image(image_name, wait=True) @@ -108,7 +113,8 @@ class TestImage(base.BaseFunctionalTest): container_format='bare', min_disk=10, min_ram=1024, - wait=True) + wait=True, + ) second_image = self.user_cloud.create_image( name=image_name, filename=test_image.name, @@ -117,7 +123,8 @@ class TestImage(base.BaseFunctionalTest): min_disk=10, min_ram=1024, allow_duplicates=True, - wait=True) + wait=True, + ) self.assertNotEqual(first_image.id, second_image.id) finally: if first_image: @@ -138,11 +145,11 @@ class TestImage(base.BaseFunctionalTest): container_format='bare', min_disk=10, min_ram=1024, - wait=True) + wait=True, + ) self.user_cloud.update_image_properties( - image=image, - name=image_name, - foo='bar') + image=image, name=image_name, foo='bar' + ) image = self.user_cloud.get_image(image_name) self.assertIn('foo', image.properties) self.assertEqual(image.properties['foo'], 'bar') @@ -158,7 +165,8 @@ class TestImage(base.BaseFunctionalTest): min_disk=10, min_ram=1024, allow_duplicates=True, - wait=False) + wait=False, + ) self.assertEqual(image_name, image.name) self.user_cloud.delete_image(image.id, wait=True) @@ -175,7 +183,8 @@ class TestImage(base.BaseFunctionalTest): container_format='bare', min_disk=10, min_ram=1024, - wait=True) + wait=True, + ) image = self.user_cloud.get_image_by_id(image.id) self.assertEqual(image_name, image.name) self.assertEqual('raw', image.disk_format) diff --git a/openstack/tests/functional/cloud/test_inventory.py b/openstack/tests/functional/cloud/test_inventory.py index 79de47a83..c5235fc0b 100644 --- a/openstack/tests/functional/cloud/test_inventory.py +++ b/openstack/tests/functional/cloud/test_inventory.py @@ -35,8 +35,13 @@ class TestInventory(base.BaseFunctionalTest): self.server_name = self.getUniqueString('inventory') self.addCleanup(self._cleanup_server) server = self.operator_cloud.create_server( - name=self.server_name, image=self.image, flavor=self.flavor, - wait=True, auto_ip=True, network='public') + name=self.server_name, + image=self.image, + flavor=self.flavor, + wait=True, + auto_ip=True, + network='public', + ) self.server_id = server['id'] def _cleanup_server(self): diff --git a/openstack/tests/functional/cloud/test_keypairs.py b/openstack/tests/functional/cloud/test_keypairs.py index 80eee3844..64b58d186 100644 --- a/openstack/tests/functional/cloud/test_keypairs.py +++ b/openstack/tests/functional/cloud/test_keypairs.py @@ -21,7 +21,6 @@ from openstack.tests.functional import base class TestKeypairs(base.BaseFunctionalTest): - def test_create_and_delete(self): '''Test creating and deleting keypairs functionality''' name = self.getUniqueString('keypair') @@ -46,7 +45,8 @@ class TestKeypairs(base.BaseFunctionalTest): name = self.getUniqueString('keypair') self.addCleanup(self.user_cloud.delete_keypair, name) keypair = self.user_cloud.create_keypair( - name=name, public_key=fakes.FAKE_PUBLIC_KEY) + name=name, public_key=fakes.FAKE_PUBLIC_KEY + ) self.assertEqual(keypair['name'], name) self.assertIsNotNone(keypair['public_key']) self.assertIsNone(keypair['private_key']) diff --git a/openstack/tests/functional/cloud/test_limits.py b/openstack/tests/functional/cloud/test_limits.py index 160adaca6..5e9f7bc83 100644 --- a/openstack/tests/functional/cloud/test_limits.py +++ b/openstack/tests/functional/cloud/test_limits.py @@ -21,7 +21,6 @@ from openstack.tests.functional import base class TestUsage(base.BaseFunctionalTest): - def test_get_our_compute_limits(self): '''Test quotas functionality''' limits = self.user_cloud.get_compute_limits() diff --git a/openstack/tests/functional/cloud/test_magnum_services.py b/openstack/tests/functional/cloud/test_magnum_services.py index b971f1383..1690b5d23 100644 --- a/openstack/tests/functional/cloud/test_magnum_services.py +++ b/openstack/tests/functional/cloud/test_magnum_services.py @@ -21,7 +21,6 @@ from openstack.tests.functional import base class TestMagnumServices(base.BaseFunctionalTest): - def setUp(self): super(TestMagnumServices, self).setUp() if not self.user_cloud.has_service( diff --git a/openstack/tests/functional/cloud/test_network.py b/openstack/tests/functional/cloud/test_network.py index d4cfe4267..60c3a0b74 100644 --- a/openstack/tests/functional/cloud/test_network.py +++ b/openstack/tests/functional/cloud/test_network.py @@ -84,7 +84,8 @@ class TestNetwork(base.BaseFunctionalTest): def test_create_network_provider_flat(self): existing_public = self.operator_cloud.search_networks( - filters={'provider:network_type': 'flat'}) + filters={'provider:network_type': 'flat'} + ) if existing_public: self.skipTest('Physical network already allocated') net1 = self.operator_cloud.create_network( @@ -93,7 +94,7 @@ class TestNetwork(base.BaseFunctionalTest): provider={ 'physical_network': 'public', 'network_type': 'flat', - } + }, ) self.assertIn('id', net1) self.assertEqual(self.network_name, net1['name']) @@ -117,10 +118,12 @@ class TestNetwork(base.BaseFunctionalTest): net1 = self.operator_cloud.create_network(name=self.network_name) self.assertIsNotNone(net1) net2 = self.operator_cloud.create_network( - name=self.network_name + 'other') + name=self.network_name + 'other' + ) self.assertIsNotNone(net2) match = self.operator_cloud.list_networks( - filters=dict(name=self.network_name)) + filters=dict(name=self.network_name) + ) self.assertEqual(1, len(match)) self.assertEqual(net1['name'], match[0]['name']) diff --git a/openstack/tests/functional/cloud/test_object.py b/openstack/tests/functional/cloud/test_object.py index cc707f53d..0b3e274d7 100644 --- a/openstack/tests/functional/cloud/test_object.py +++ b/openstack/tests/functional/cloud/test_object.py @@ -28,7 +28,6 @@ from openstack.tests.functional import base class TestObject(base.BaseFunctionalTest): - def setUp(self): super(TestObject, self).setUp() if not self.user_cloud.has_service('object-store'): @@ -41,69 +40,84 @@ class TestObject(base.BaseFunctionalTest): self.addCleanup(self.user_cloud.delete_container, container_name) self.user_cloud.create_container(container_name) container = self.user_cloud.get_container(container_name) + self.assertEqual(container_name, container.name) self.assertEqual( - container_name, container.name) - self.assertEqual( - [], - self.user_cloud.list_containers(prefix='somethin')) + [], self.user_cloud.list_containers(prefix='somethin') + ) sizes = ( (64 * 1024, 1), # 64K, one segment - (64 * 1024, 5) # 64MB, 5 segments + (64 * 1024, 5), # 64MB, 5 segments ) for size, nseg in sizes: segment_size = int(round(size / nseg)) with tempfile.NamedTemporaryFile() as fake_file: - fake_content = ''.join(random.SystemRandom().choice( - string.ascii_uppercase + string.digits) - for _ in range(size)).encode('latin-1') + fake_content = ''.join( + random.SystemRandom().choice( + string.ascii_uppercase + string.digits + ) + for _ in range(size) + ).encode('latin-1') fake_file.write(fake_content) fake_file.flush() name = 'test-%d' % size self.addCleanup( - self.user_cloud.delete_object, container_name, name) + self.user_cloud.delete_object, container_name, name + ) self.user_cloud.create_object( - container_name, name, + container_name, + name, fake_file.name, segment_size=segment_size, - metadata={'foo': 'bar'}) - self.assertFalse(self.user_cloud.is_object_stale( - container_name, name, - fake_file.name - )) + metadata={'foo': 'bar'}, + ) + self.assertFalse( + self.user_cloud.is_object_stale( + container_name, name, fake_file.name + ) + ) self.assertEqual( - 'bar', self.user_cloud.get_object_metadata( - container_name, name)['foo'] + 'bar', + self.user_cloud.get_object_metadata(container_name, name)[ + 'foo' + ], + ) + self.user_cloud.update_object( + container=container_name, + name=name, + metadata={'testk': 'testv'}, ) - self.user_cloud.update_object(container=container_name, name=name, - metadata={'testk': 'testv'}) self.assertEqual( - 'testv', self.user_cloud.get_object_metadata( - container_name, name)['testk'] + 'testv', + self.user_cloud.get_object_metadata(container_name, name)[ + 'testk' + ], ) try: self.assertIsNotNone( - self.user_cloud.get_object(container_name, name)) + self.user_cloud.get_object(container_name, name) + ) except exc.OpenStackCloudException as e: self.addDetail( 'failed_response', - content.text_content(str(e.response.headers))) + content.text_content(str(e.response.headers)), + ) self.addDetail( - 'failed_response', - content.text_content(e.response.text)) + 'failed_response', content.text_content(e.response.text) + ) self.assertEqual( - name, - self.user_cloud.list_objects(container_name)[0]['name']) + name, self.user_cloud.list_objects(container_name)[0]['name'] + ) self.assertEqual( - [], - self.user_cloud.list_objects(container_name, - prefix='abc')) + [], self.user_cloud.list_objects(container_name, prefix='abc') + ) self.assertTrue( - self.user_cloud.delete_object(container_name, name)) + self.user_cloud.delete_object(container_name, name) + ) self.assertEqual([], self.user_cloud.list_objects(container_name)) self.assertEqual( - container_name, - self.user_cloud.get_container(container_name).name) + container_name, self.user_cloud.get_container(container_name).name + ) self.user_cloud.delete_container(container_name) def test_download_object_to_file(self): @@ -112,64 +126,83 @@ class TestObject(base.BaseFunctionalTest): self.addDetail('container', content.text_content(container_name)) self.addCleanup(self.user_cloud.delete_container, container_name) self.user_cloud.create_container(container_name) - self.assertEqual(container_name, - self.user_cloud.list_containers()[0]['name']) + self.assertEqual( + container_name, self.user_cloud.list_containers()[0]['name'] + ) sizes = ( (64 * 1024, 1), # 64K, one segment - (64 * 1024, 5) # 64MB, 5 segments + (64 * 1024, 5), # 64MB, 5 segments ) for size, nseg in sizes: fake_content = '' segment_size = int(round(size / nseg)) with tempfile.NamedTemporaryFile() as fake_file: - fake_content = ''.join(random.SystemRandom().choice( - string.ascii_uppercase + string.digits) - for _ in range(size)).encode('latin-1') + fake_content = ''.join( + random.SystemRandom().choice( + string.ascii_uppercase + string.digits + ) + for _ in range(size) + ).encode('latin-1') fake_file.write(fake_content) fake_file.flush() name = 'test-%d' % size self.addCleanup( - self.user_cloud.delete_object, container_name, name) + self.user_cloud.delete_object, container_name, name + ) self.user_cloud.create_object( - container_name, name, + container_name, + name, fake_file.name, segment_size=segment_size, - metadata={'foo': 'bar'}) - self.assertFalse(self.user_cloud.is_object_stale( - container_name, name, - fake_file.name - )) + metadata={'foo': 'bar'}, + ) + self.assertFalse( + self.user_cloud.is_object_stale( + container_name, name, fake_file.name + ) + ) self.assertEqual( - 'bar', self.user_cloud.get_object_metadata( - container_name, name)['foo'] + 'bar', + self.user_cloud.get_object_metadata(container_name, name)[ + 'foo' + ], + ) + self.user_cloud.update_object( + container=container_name, + name=name, + metadata={'testk': 'testv'}, ) - self.user_cloud.update_object(container=container_name, name=name, - metadata={'testk': 'testv'}) self.assertEqual( - 'testv', self.user_cloud.get_object_metadata( - container_name, name)['testk'] + 'testv', + self.user_cloud.get_object_metadata(container_name, name)[ + 'testk' + ], ) try: with tempfile.NamedTemporaryFile() as fake_file: self.user_cloud.get_object( - container_name, name, outfile=fake_file.name) + container_name, name, outfile=fake_file.name + ) downloaded_content = open(fake_file.name, 'rb').read() self.assertEqual(fake_content, downloaded_content) except exc.OpenStackCloudException as e: self.addDetail( 'failed_response', - content.text_content(str(e.response.headers))) + content.text_content(str(e.response.headers)), + ) self.addDetail( - 'failed_response', - content.text_content(e.response.text)) + 'failed_response', content.text_content(e.response.text) + ) raise self.assertEqual( - name, - self.user_cloud.list_objects(container_name)[0]['name']) + name, self.user_cloud.list_objects(container_name)[0]['name'] + ) self.assertTrue( - self.user_cloud.delete_object(container_name, name)) + self.user_cloud.delete_object(container_name, name) + ) self.assertEqual([], self.user_cloud.list_objects(container_name)) - self.assertEqual(container_name, - self.user_cloud.list_containers()[0]['name']) + self.assertEqual( + container_name, self.user_cloud.list_containers()[0]['name'] + ) self.user_cloud.delete_container(container_name) diff --git a/openstack/tests/functional/cloud/test_port.py b/openstack/tests/functional/cloud/test_port.py index a798a20f9..c36923334 100644 --- a/openstack/tests/functional/cloud/test_port.py +++ b/openstack/tests/functional/cloud/test_port.py @@ -27,7 +27,6 @@ from openstack.tests.functional import base class TestPort(base.BaseFunctionalTest): - def setUp(self): super(TestPort, self).setUp() # Skip Neutron tests if neutron is not present @@ -40,7 +39,8 @@ class TestPort(base.BaseFunctionalTest): # Generate a unique port name to allow concurrent tests self.new_port_name = 'test_' + ''.join( - random.choice(string.ascii_lowercase) for _ in range(5)) + random.choice(string.ascii_lowercase) for _ in range(5) + ) self.addCleanup(self._cleanup_ports) @@ -65,7 +65,8 @@ class TestPort(base.BaseFunctionalTest): port_name = self.new_port_name + '_create' port = self.user_cloud.create_port( - network_id=self.net.id, name=port_name) + network_id=self.net.id, name=port_name + ) self.assertIsInstance(port, dict) self.assertIn('id', port) self.assertEqual(port.get('name'), port_name) @@ -74,7 +75,8 @@ class TestPort(base.BaseFunctionalTest): port_name = self.new_port_name + '_get' port = self.user_cloud.create_port( - network_id=self.net.id, name=port_name) + network_id=self.net.id, name=port_name + ) self.assertIsInstance(port, dict) self.assertIn('id', port) self.assertEqual(port.get('name'), port_name) @@ -89,7 +91,8 @@ class TestPort(base.BaseFunctionalTest): port_name = self.new_port_name + '_get_by_id' port = self.user_cloud.create_port( - network_id=self.net.id, name=port_name) + network_id=self.net.id, name=port_name + ) self.assertIsInstance(port, dict) self.assertIn('id', port) self.assertEqual(port.get('name'), port_name) @@ -104,11 +107,11 @@ class TestPort(base.BaseFunctionalTest): port_name = self.new_port_name + '_update' new_port_name = port_name + '_new' - self.user_cloud.create_port( - network_id=self.net.id, name=port_name) + self.user_cloud.create_port(network_id=self.net.id, name=port_name) port = self.user_cloud.update_port( - name_or_id=port_name, name=new_port_name) + name_or_id=port_name, name=new_port_name + ) self.assertIsInstance(port, dict) self.assertEqual(port.get('name'), new_port_name) @@ -129,7 +132,8 @@ class TestPort(base.BaseFunctionalTest): port_name = self.new_port_name + '_delete' port = self.user_cloud.create_port( - network_id=self.net.id, name=port_name) + network_id=self.net.id, name=port_name + ) self.assertIsInstance(port, dict) self.assertIn('id', port) self.assertEqual(port.get('name'), port_name) diff --git a/openstack/tests/functional/cloud/test_project.py b/openstack/tests/functional/cloud/test_project.py index 26390f7db..d4a4647c6 100644 --- a/openstack/tests/functional/cloud/test_project.py +++ b/openstack/tests/functional/cloud/test_project.py @@ -25,7 +25,6 @@ from openstack.tests.functional import base class TestProject(base.KeystoneBaseFunctionalTest): - def setUp(self): super(TestProject, self).setUp() if not self.operator_cloud: @@ -54,8 +53,9 @@ class TestProject(base.KeystoneBaseFunctionalTest): 'description': 'test_create_project', } if self.identity_version == '3': - params['domain_id'] = \ - self.operator_cloud.get_domain('default')['id'] + params['domain_id'] = self.operator_cloud.get_domain('default')[ + 'id' + ] project = self.operator_cloud.create_project(**params) @@ -66,15 +66,23 @@ class TestProject(base.KeystoneBaseFunctionalTest): user_id = self.operator_cloud.current_user_id # Grant the current user access to the project - self.assertTrue(self.operator_cloud.grant_role( - 'member', user=user_id, project=project['id'], wait=True)) + self.assertTrue( + self.operator_cloud.grant_role( + 'member', user=user_id, project=project['id'], wait=True + ) + ) self.addCleanup( self.operator_cloud.revoke_role, - 'member', user=user_id, project=project['id'], wait=True) + 'member', + user=user_id, + project=project['id'], + wait=True, + ) new_cloud = self.operator_cloud.connect_as_project(project) self.add_info_on_exception( - 'new_cloud_config', pprint.pformat(new_cloud.config.config)) + 'new_cloud_config', pprint.pformat(new_cloud.config.config) + ) location = new_cloud.current_location self.assertEqual(project_name, location['project']['name']) @@ -84,15 +92,17 @@ class TestProject(base.KeystoneBaseFunctionalTest): params = { 'name': project_name, 'description': 'test_update_project', - 'enabled': True + 'enabled': True, } if self.identity_version == '3': - params['domain_id'] = \ - self.operator_cloud.get_domain('default')['id'] + params['domain_id'] = self.operator_cloud.get_domain('default')[ + 'id' + ] project = self.operator_cloud.create_project(**params) updated_project = self.operator_cloud.update_project( - project_name, enabled=False, description='new') + project_name, enabled=False, description='new' + ) self.assertIsNotNone(updated_project) self.assertEqual(project['id'], updated_project['id']) self.assertEqual(project['name'], updated_project['name']) @@ -102,12 +112,14 @@ class TestProject(base.KeystoneBaseFunctionalTest): # Revert the description and verify the project is still disabled updated_project = self.operator_cloud.update_project( - project_name, description=params['description']) + project_name, description=params['description'] + ) self.assertIsNotNone(updated_project) self.assertEqual(project['id'], updated_project['id']) self.assertEqual(project['name'], updated_project['name']) - self.assertEqual(project['description'], - updated_project['description']) + self.assertEqual( + project['description'], updated_project['description'] + ) self.assertTrue(project['enabled']) self.assertFalse(updated_project['enabled']) @@ -115,8 +127,9 @@ class TestProject(base.KeystoneBaseFunctionalTest): project_name = self.new_project_name + '_delete' params = {'name': project_name} if self.identity_version == '3': - params['domain_id'] = \ - self.operator_cloud.get_domain('default')['id'] + params['domain_id'] = self.operator_cloud.get_domain('default')[ + 'id' + ] project = self.operator_cloud.create_project(**params) self.assertIsNotNone(project) self.assertTrue(self.operator_cloud.delete_project(project['id'])) diff --git a/openstack/tests/functional/cloud/test_project_cleanup.py b/openstack/tests/functional/cloud/test_project_cleanup.py index 9016358cc..8d661d0ce 100644 --- a/openstack/tests/functional/cloud/test_project_cleanup.py +++ b/openstack/tests/functional/cloud/test_project_cleanup.py @@ -48,8 +48,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): name=self.getUniqueString('router') ) conn.network.add_interface_to_router( - self.router.id, - subnet_id=self.subnet.id) + self.router.id, subnet_id=self.subnet.id + ) def test_cleanup(self): self._create_network_resources() @@ -60,7 +60,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): dry_run=True, wait_timeout=120, status_queue=status_queue, - filters={'created_at': '2000-01-01'}) + filters={'created_at': '2000-01-01'}, + ) self.assertTrue(status_queue.empty()) @@ -71,7 +72,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): wait_timeout=120, status_queue=status_queue, filters={'created_at': '2200-01-01'}, - resource_evaluation_fn=lambda x, y, z: False) + resource_evaluation_fn=lambda x, y, z: False, + ) self.assertTrue(status_queue.empty()) @@ -80,7 +82,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): dry_run=True, wait_timeout=120, status_queue=status_queue, - filters={'created_at': '2200-01-01'}) + filters={'created_at': '2200-01-01'}, + ) objects = [] while not status_queue.empty(): @@ -92,9 +95,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): # Fourth round - dry run with no filters, ensure everything identified self.conn.project_cleanup( - dry_run=True, - wait_timeout=120, - status_queue=status_queue) + dry_run=True, wait_timeout=120, status_queue=status_queue + ) objects = [] while not status_queue.empty(): @@ -109,9 +111,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): # Last round - do a real cleanup self.conn.project_cleanup( - dry_run=False, - wait_timeout=600, - status_queue=status_queue) + dry_run=False, wait_timeout=600, status_queue=status_queue + ) objects = [] while not status_queue.empty(): @@ -136,10 +137,12 @@ class TestProjectCleanup(base.BaseFunctionalTest): b1 = self.conn.block_storage.create_backup(volume_id=vol.id) self.conn.block_storage.wait_for_status(b1) b2 = self.conn.block_storage.create_backup( - volume_id=vol.id, is_incremental=True, snapshot_id=s1.id) + volume_id=vol.id, is_incremental=True, snapshot_id=s1.id + ) self.conn.block_storage.wait_for_status(b2) b3 = self.conn.block_storage.create_backup( - volume_id=vol.id, is_incremental=True, snapshot_id=s1.id) + volume_id=vol.id, is_incremental=True, snapshot_id=s1.id + ) self.conn.block_storage.wait_for_status(b3) # First round - check no resources are old enough @@ -147,7 +150,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): dry_run=True, wait_timeout=120, status_queue=status_queue, - filters={'created_at': '2000-01-01'}) + filters={'created_at': '2000-01-01'}, + ) self.assertTrue(status_queue.empty()) @@ -158,7 +162,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): wait_timeout=120, status_queue=status_queue, filters={'created_at': '2200-01-01'}, - resource_evaluation_fn=lambda x, y, z: False) + resource_evaluation_fn=lambda x, y, z: False, + ) self.assertTrue(status_queue.empty()) @@ -167,7 +172,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): dry_run=True, wait_timeout=120, status_queue=status_queue, - filters={'created_at': '2200-01-01'}) + filters={'created_at': '2200-01-01'}, + ) objects = [] while not status_queue.empty(): @@ -179,9 +185,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): # Fourth round - dry run with no filters, ensure everything identified self.conn.project_cleanup( - dry_run=True, - wait_timeout=120, - status_queue=status_queue) + dry_run=True, wait_timeout=120, status_queue=status_queue + ) objects = [] while not status_queue.empty(): @@ -196,9 +201,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): # Last round - do a real cleanup self.conn.project_cleanup( - dry_run=False, - wait_timeout=600, - status_queue=status_queue) + dry_run=False, wait_timeout=600, status_queue=status_queue + ) # Ensure no backups remain self.assertEqual(0, len(list(self.conn.block_storage.backups()))) # Ensure no snapshots remain @@ -212,14 +216,16 @@ class TestProjectCleanup(base.BaseFunctionalTest): self.conn.object_store.create_container('test_cleanup') for i in range(1, 10): self.conn.object_store.create_object( - "test_cleanup", f"test{i}", data="test{i}") + "test_cleanup", f"test{i}", data="test{i}" + ) # First round - check no resources are old enough self.conn.project_cleanup( dry_run=True, wait_timeout=120, status_queue=status_queue, - filters={'updated_at': '2000-01-01'}) + filters={'updated_at': '2000-01-01'}, + ) self.assertTrue(status_queue.empty()) @@ -228,7 +234,8 @@ class TestProjectCleanup(base.BaseFunctionalTest): dry_run=True, wait_timeout=120, status_queue=status_queue, - filters={'updated_at': '2200-01-01'}) + filters={'updated_at': '2200-01-01'}, + ) objects = [] while not status_queue.empty(): objects.append(status_queue.get()) @@ -238,19 +245,15 @@ class TestProjectCleanup(base.BaseFunctionalTest): self.assertIn('test1', obj_names) # Ensure object still exists - obj = self.conn.object_store.get_object( - "test1", "test_cleanup") + obj = self.conn.object_store.get_object("test1", "test_cleanup") self.assertIsNotNone(obj) # Last round - do a real cleanup self.conn.project_cleanup( - dry_run=False, - wait_timeout=600, - status_queue=status_queue) + dry_run=False, wait_timeout=600, status_queue=status_queue + ) objects.clear() while not status_queue.empty(): objects.append(status_queue.get()) - self.assertIsNone( - self.conn.get_container('test_container') - ) + self.assertIsNone(self.conn.get_container('test_container')) diff --git a/openstack/tests/functional/cloud/test_qos_bandwidth_limit_rule.py b/openstack/tests/functional/cloud/test_qos_bandwidth_limit_rule.py index e91098df2..3f618698c 100644 --- a/openstack/tests/functional/cloud/test_qos_bandwidth_limit_rule.py +++ b/openstack/tests/functional/cloud/test_qos_bandwidth_limit_rule.py @@ -50,59 +50,61 @@ class TestQosBandwidthLimitRule(base.BaseFunctionalTest): # Create bw limit rule rule = self.operator_cloud.create_qos_bandwidth_limit_rule( - self.policy['id'], - max_kbps=max_kbps, - max_burst_kbps=max_burst_kbps) + self.policy['id'], max_kbps=max_kbps, max_burst_kbps=max_burst_kbps + ) self.assertIn('id', rule) self.assertEqual(max_kbps, rule['max_kbps']) self.assertEqual(max_burst_kbps, rule['max_burst_kbps']) # Now try to update rule updated_rule = self.operator_cloud.update_qos_bandwidth_limit_rule( - self.policy['id'], - rule['id'], - max_kbps=updated_max_kbps) + self.policy['id'], rule['id'], max_kbps=updated_max_kbps + ) self.assertIn('id', updated_rule) self.assertEqual(updated_max_kbps, updated_rule['max_kbps']) self.assertEqual(max_burst_kbps, updated_rule['max_burst_kbps']) # List rules from policy policy_rules = self.operator_cloud.list_qos_bandwidth_limit_rules( - self.policy['id']) + self.policy['id'] + ) self.assertEqual([updated_rule], policy_rules) # Delete rule self.operator_cloud.delete_qos_bandwidth_limit_rule( - self.policy['id'], updated_rule['id']) + self.policy['id'], updated_rule['id'] + ) # Check if there is no rules in policy policy_rules = self.operator_cloud.list_qos_bandwidth_limit_rules( - self.policy['id']) + self.policy['id'] + ) self.assertEqual([], policy_rules) def test_create_qos_bandwidth_limit_rule_direction(self): if not self.operator_cloud._has_neutron_extension( - 'qos-bw-limit-direction'): - self.skipTest("'qos-bw-limit-direction' network extension " - "not supported by cloud") + 'qos-bw-limit-direction' + ): + self.skipTest( + "'qos-bw-limit-direction' network extension " + "not supported by cloud" + ) max_kbps = 1500 direction = "ingress" updated_direction = "egress" # Create bw limit rule rule = self.operator_cloud.create_qos_bandwidth_limit_rule( - self.policy['id'], - max_kbps=max_kbps, - direction=direction) + self.policy['id'], max_kbps=max_kbps, direction=direction + ) self.assertIn('id', rule) self.assertEqual(max_kbps, rule['max_kbps']) self.assertEqual(direction, rule['direction']) # Now try to update direction in rule updated_rule = self.operator_cloud.update_qos_bandwidth_limit_rule( - self.policy['id'], - rule['id'], - direction=updated_direction) + self.policy['id'], rule['id'], direction=updated_direction + ) self.assertIn('id', updated_rule) self.assertEqual(max_kbps, updated_rule['max_kbps']) self.assertEqual(updated_direction, updated_rule['direction']) diff --git a/openstack/tests/functional/cloud/test_qos_dscp_marking_rule.py b/openstack/tests/functional/cloud/test_qos_dscp_marking_rule.py index aff8813b3..b5f100010 100644 --- a/openstack/tests/functional/cloud/test_qos_dscp_marking_rule.py +++ b/openstack/tests/functional/cloud/test_qos_dscp_marking_rule.py @@ -49,29 +49,31 @@ class TestQosDscpMarkingRule(base.BaseFunctionalTest): # Create DSCP marking rule rule = self.operator_cloud.create_qos_dscp_marking_rule( - self.policy['id'], - dscp_mark=dscp_mark) + self.policy['id'], dscp_mark=dscp_mark + ) self.assertIn('id', rule) self.assertEqual(dscp_mark, rule['dscp_mark']) # Now try to update rule updated_rule = self.operator_cloud.update_qos_dscp_marking_rule( - self.policy['id'], - rule['id'], - dscp_mark=updated_dscp_mark) + self.policy['id'], rule['id'], dscp_mark=updated_dscp_mark + ) self.assertIn('id', updated_rule) self.assertEqual(updated_dscp_mark, updated_rule['dscp_mark']) # List rules from policy policy_rules = self.operator_cloud.list_qos_dscp_marking_rules( - self.policy['id']) + self.policy['id'] + ) self.assertEqual([updated_rule], policy_rules) # Delete rule self.operator_cloud.delete_qos_dscp_marking_rule( - self.policy['id'], updated_rule['id']) + self.policy['id'], updated_rule['id'] + ) # Check if there is no rules in policy policy_rules = self.operator_cloud.list_qos_dscp_marking_rules( - self.policy['id']) + self.policy['id'] + ) self.assertEqual([], policy_rules) diff --git a/openstack/tests/functional/cloud/test_qos_minimum_bandwidth_rule.py b/openstack/tests/functional/cloud/test_qos_minimum_bandwidth_rule.py index 8cc16a893..43d3c5b9c 100644 --- a/openstack/tests/functional/cloud/test_qos_minimum_bandwidth_rule.py +++ b/openstack/tests/functional/cloud/test_qos_minimum_bandwidth_rule.py @@ -49,29 +49,31 @@ class TestQosMinimumBandwidthRule(base.BaseFunctionalTest): # Create min bw rule rule = self.operator_cloud.create_qos_minimum_bandwidth_rule( - self.policy['id'], - min_kbps=min_kbps) + self.policy['id'], min_kbps=min_kbps + ) self.assertIn('id', rule) self.assertEqual(min_kbps, rule['min_kbps']) # Now try to update rule updated_rule = self.operator_cloud.update_qos_minimum_bandwidth_rule( - self.policy['id'], - rule['id'], - min_kbps=updated_min_kbps) + self.policy['id'], rule['id'], min_kbps=updated_min_kbps + ) self.assertIn('id', updated_rule) self.assertEqual(updated_min_kbps, updated_rule['min_kbps']) # List rules from policy policy_rules = self.operator_cloud.list_qos_minimum_bandwidth_rules( - self.policy['id']) + self.policy['id'] + ) self.assertEqual([updated_rule], policy_rules) # Delete rule self.operator_cloud.delete_qos_minimum_bandwidth_rule( - self.policy['id'], updated_rule['id']) + self.policy['id'], updated_rule['id'] + ) # Check if there is no rules in policy policy_rules = self.operator_cloud.list_qos_minimum_bandwidth_rules( - self.policy['id']) + self.policy['id'] + ) self.assertEqual([], policy_rules) diff --git a/openstack/tests/functional/cloud/test_qos_policy.py b/openstack/tests/functional/cloud/test_qos_policy.py index 6bd3150ce..b55b17d2f 100644 --- a/openstack/tests/functional/cloud/test_qos_policy.py +++ b/openstack/tests/functional/cloud/test_qos_policy.py @@ -56,7 +56,8 @@ class TestQosPolicy(base.BaseFunctionalTest): def test_create_qos_policy_shared(self): policy = self.operator_cloud.create_qos_policy( - name=self.policy_name, shared=True) + name=self.policy_name, shared=True + ) self.assertIn('id', policy) self.assertEqual(self.policy_name, policy['name']) self.assertTrue(policy['is_shared']) @@ -64,10 +65,12 @@ class TestQosPolicy(base.BaseFunctionalTest): def test_create_qos_policy_default(self): if not self.operator_cloud._has_neutron_extension('qos-default'): - self.skipTest("'qos-default' network extension not supported " - "by cloud") + self.skipTest( + "'qos-default' network extension not supported " "by cloud" + ) policy = self.operator_cloud.create_qos_policy( - name=self.policy_name, default=True) + name=self.policy_name, default=True + ) self.assertIn('id', policy) self.assertEqual(self.policy_name, policy['name']) self.assertFalse(policy['is_shared']) @@ -80,7 +83,8 @@ class TestQosPolicy(base.BaseFunctionalTest): self.assertFalse(policy['is_default']) updated_policy = self.operator_cloud.update_qos_policy( - policy['id'], shared=True, default=True) + policy['id'], shared=True, default=True + ) self.assertEqual(self.policy_name, updated_policy['name']) self.assertTrue(updated_policy['is_shared']) self.assertTrue(updated_policy['is_default']) @@ -89,9 +93,11 @@ class TestQosPolicy(base.BaseFunctionalTest): policy1 = self.operator_cloud.create_qos_policy(name=self.policy_name) self.assertIsNotNone(policy1) policy2 = self.operator_cloud.create_qos_policy( - name=self.policy_name + 'other') + name=self.policy_name + 'other' + ) self.assertIsNotNone(policy2) match = self.operator_cloud.list_qos_policies( - filters=dict(name=self.policy_name)) + filters=dict(name=self.policy_name) + ) self.assertEqual(1, len(match)) self.assertEqual(policy1['name'], match[0]['name']) diff --git a/openstack/tests/functional/cloud/test_quotas.py b/openstack/tests/functional/cloud/test_quotas.py index f23c6ae59..15525f5d2 100644 --- a/openstack/tests/functional/cloud/test_quotas.py +++ b/openstack/tests/functional/cloud/test_quotas.py @@ -21,11 +21,9 @@ from openstack.tests.functional import base class TestComputeQuotas(base.BaseFunctionalTest): - def test_get_quotas(self): '''Test quotas functionality''' - self.user_cloud.get_compute_quotas( - self.user_cloud.current_project_id) + self.user_cloud.get_compute_quotas(self.user_cloud.current_project_id) def test_set_quotas(self): '''Test quotas functionality''' @@ -36,15 +34,15 @@ class TestComputeQuotas(base.BaseFunctionalTest): cores = quotas['cores'] self.operator_cloud.set_compute_quotas('demo', cores=cores + 1) self.assertEqual( - cores + 1, - self.operator_cloud.get_compute_quotas('demo')['cores']) + cores + 1, self.operator_cloud.get_compute_quotas('demo')['cores'] + ) self.operator_cloud.delete_compute_quotas('demo') self.assertEqual( - cores, self.operator_cloud.get_compute_quotas('demo')['cores']) + cores, self.operator_cloud.get_compute_quotas('demo')['cores'] + ) class TestVolumeQuotas(base.BaseFunctionalTest): - def setUp(self): super(TestVolumeQuotas, self).setUp() if not self.user_cloud.has_service('volume'): @@ -52,9 +50,7 @@ class TestVolumeQuotas(base.BaseFunctionalTest): def test_get_quotas(self): '''Test get quotas functionality''' - self.user_cloud.get_volume_quotas( - self.user_cloud.current_project_id - ) + self.user_cloud.get_volume_quotas(self.user_cloud.current_project_id) def test_set_quotas(self): '''Test set quotas functionality''' @@ -66,19 +62,18 @@ class TestVolumeQuotas(base.BaseFunctionalTest): self.operator_cloud.set_volume_quotas('demo', volumes=volumes + 1) self.assertEqual( volumes + 1, - self.operator_cloud.get_volume_quotas('demo')['volumes']) + self.operator_cloud.get_volume_quotas('demo')['volumes'], + ) self.operator_cloud.delete_volume_quotas('demo') self.assertEqual( - volumes, - self.operator_cloud.get_volume_quotas('demo')['volumes']) + volumes, self.operator_cloud.get_volume_quotas('demo')['volumes'] + ) class TestNetworkQuotas(base.BaseFunctionalTest): - def test_get_quotas(self): '''Test get quotas functionality''' - self.user_cloud.get_network_quotas( - self.user_cloud.current_project_id) + self.user_cloud.get_network_quotas(self.user_cloud.current_project_id) def test_quotas(self): '''Test quotas functionality''' @@ -92,11 +87,12 @@ class TestNetworkQuotas(base.BaseFunctionalTest): self.operator_cloud.set_network_quotas('demo', networks=network + 1) self.assertEqual( network + 1, - self.operator_cloud.get_network_quotas('demo')['networks']) + self.operator_cloud.get_network_quotas('demo')['networks'], + ) self.operator_cloud.delete_network_quotas('demo') self.assertEqual( - network, - self.operator_cloud.get_network_quotas('demo')['networks']) + network, self.operator_cloud.get_network_quotas('demo')['networks'] + ) def test_get_quotas_details(self): if not self.operator_cloud: @@ -105,14 +101,21 @@ class TestNetworkQuotas(base.BaseFunctionalTest): self.skipTest('network service not supported by cloud') quotas = [ - 'floating_ips', 'networks', 'ports', - 'rbac_policies', 'routers', 'subnets', - 'subnet_pools', 'security_group_rules', - 'security_groups'] + 'floating_ips', + 'networks', + 'ports', + 'rbac_policies', + 'routers', + 'subnets', + 'subnet_pools', + 'security_group_rules', + 'security_groups', + ] expected_keys = ['limit', 'used', 'reserved'] '''Test getting details about quota usage''' quota_details = self.operator_cloud.get_network_quotas( - 'demo', details=True) + 'demo', details=True + ) for quota in quotas: quota_val = quota_details[quota] if quota_val: diff --git a/openstack/tests/functional/cloud/test_range_search.py b/openstack/tests/functional/cloud/test_range_search.py index ab4870d7f..c0a186c0a 100644 --- a/openstack/tests/functional/cloud/test_range_search.py +++ b/openstack/tests/functional/cloud/test_range_search.py @@ -17,7 +17,6 @@ from openstack.tests.functional import base class TestRangeSearch(base.BaseFunctionalTest): - def _filter_m1_flavors(self, results): """The m1 flavors are the original devstack flavors""" new_results = [] @@ -30,7 +29,10 @@ class TestRangeSearch(base.BaseFunctionalTest): flavors = self.user_cloud.list_flavors(get_extra=False) self.assertRaises( exc.OpenStackCloudException, - self.user_cloud.range_search, flavors, {"ram": "<1a0"}) + self.user_cloud.range_search, + flavors, + {"ram": "<1a0"}, + ) def test_range_search_exact(self): flavors = self.user_cloud.list_flavors(get_extra=False) @@ -103,7 +105,8 @@ class TestRangeSearch(base.BaseFunctionalTest): def test_range_search_multi_1(self): flavors = self.user_cloud.list_flavors(get_extra=False) result = self.user_cloud.range_search( - flavors, {"ram": "MIN", "vcpus": "MIN"}) + flavors, {"ram": "MIN", "vcpus": "MIN"} + ) self.assertIsInstance(result, list) self.assertEqual(1, len(result)) # older devstack does not have cirros256 @@ -112,7 +115,8 @@ class TestRangeSearch(base.BaseFunctionalTest): def test_range_search_multi_2(self): flavors = self.user_cloud.list_flavors(get_extra=False) result = self.user_cloud.range_search( - flavors, {"ram": "<1024", "vcpus": "MIN"}) + flavors, {"ram": "<1024", "vcpus": "MIN"} + ) self.assertIsInstance(result, list) result = self._filter_m1_flavors(result) self.assertEqual(1, len(result)) @@ -122,7 +126,8 @@ class TestRangeSearch(base.BaseFunctionalTest): def test_range_search_multi_3(self): flavors = self.user_cloud.list_flavors(get_extra=False) result = self.user_cloud.range_search( - flavors, {"ram": ">=4096", "vcpus": "<6"}) + flavors, {"ram": ">=4096", "vcpus": "<6"} + ) self.assertIsInstance(result, list) result = self._filter_m1_flavors(result) self.assertEqual(2, len(result)) @@ -133,7 +138,8 @@ class TestRangeSearch(base.BaseFunctionalTest): def test_range_search_multi_4(self): flavors = self.user_cloud.list_flavors(get_extra=False) result = self.user_cloud.range_search( - flavors, {"ram": ">=4096", "vcpus": "MAX"}) + flavors, {"ram": ">=4096", "vcpus": "MAX"} + ) self.assertIsInstance(result, list) self.assertEqual(1, len(result)) # This is the only result that should have max vcpu diff --git a/openstack/tests/functional/cloud/test_recordset.py b/openstack/tests/functional/cloud/test_recordset.py index 3b8550b46..709547a47 100644 --- a/openstack/tests/functional/cloud/test_recordset.py +++ b/openstack/tests/functional/cloud/test_recordset.py @@ -25,7 +25,6 @@ from openstack.tests.functional import base class TestRecordset(base.BaseFunctionalTest): - def setUp(self): super(TestRecordset, self).setUp() if not self.user_cloud.has_service('dns'): @@ -50,11 +49,9 @@ class TestRecordset(base.BaseFunctionalTest): zone_obj = self.user_cloud.create_zone(name=zone, email=email) # Test we can create a recordset and we get it returned - created_recordset = self.user_cloud.create_recordset(zone_obj['id'], - name, - type_, - records, - description, ttl) + created_recordset = self.user_cloud.create_recordset( + zone_obj['id'], name, type_, records, description, ttl + ) self.addCleanup(self.cleanup, zone, created_recordset['id']) self.assertEqual(created_recordset['zone_id'], zone_obj['id']) @@ -65,20 +62,22 @@ class TestRecordset(base.BaseFunctionalTest): self.assertEqual(created_recordset['ttl'], ttl) # Test that we can list recordsets - recordsets = self.user_cloud.list_recordsets(zone_obj['id'],) + recordsets = self.user_cloud.list_recordsets( + zone_obj['id'], + ) self.assertIsNotNone(recordsets) # Test we get the same recordset with the get_recordset method - get_recordset = self.user_cloud.get_recordset(zone_obj['id'], - created_recordset['id']) + get_recordset = self.user_cloud.get_recordset( + zone_obj['id'], created_recordset['id'] + ) self.assertEqual(get_recordset['id'], created_recordset['id']) # Test we can update a field on the recordset and only that field # is updated updated_recordset = self.user_cloud.update_recordset( - zone_obj['id'], - created_recordset['id'], - ttl=7200) + zone_obj['id'], created_recordset['id'], ttl=7200 + ) self.assertEqual(updated_recordset['id'], created_recordset['id']) self.assertEqual(updated_recordset['name'], name) self.assertEqual(updated_recordset['type'], type_.upper()) @@ -88,7 +87,8 @@ class TestRecordset(base.BaseFunctionalTest): # Test we can delete and get True returned deleted_recordset = self.user_cloud.delete_recordset( - zone, created_recordset['id']) + zone, created_recordset['id'] + ) self.assertTrue(deleted_recordset) def test_recordsets_with_zone_name(self): @@ -110,9 +110,9 @@ class TestRecordset(base.BaseFunctionalTest): zone_obj = self.user_cloud.create_zone(name=zone, email=email) # Test we can create a recordset and we get it returned - created_recordset = self.user_cloud.create_recordset(zone, name, type_, - records, - description, ttl) + created_recordset = self.user_cloud.create_recordset( + zone, name, type_, records, description, ttl + ) self.addCleanup(self.cleanup, zone, created_recordset['id']) self.assertEqual(created_recordset['zone_id'], zone_obj['id']) @@ -127,16 +127,16 @@ class TestRecordset(base.BaseFunctionalTest): self.assertIsNotNone(recordsets) # Test we get the same recordset with the get_recordset method - get_recordset = self.user_cloud.get_recordset(zone, - created_recordset['id']) + get_recordset = self.user_cloud.get_recordset( + zone, created_recordset['id'] + ) self.assertEqual(get_recordset['id'], created_recordset['id']) # Test we can update a field on the recordset and only that field # is updated updated_recordset = self.user_cloud.update_recordset( - zone_obj['id'], - created_recordset['id'], - ttl=7200) + zone_obj['id'], created_recordset['id'], ttl=7200 + ) self.assertEqual(updated_recordset['id'], created_recordset['id']) self.assertEqual(updated_recordset['name'], name) self.assertEqual(updated_recordset['type'], type_.upper()) @@ -146,10 +146,10 @@ class TestRecordset(base.BaseFunctionalTest): # Test we can delete and get True returned deleted_recordset = self.user_cloud.delete_recordset( - zone, created_recordset['id']) + zone, created_recordset['id'] + ) self.assertTrue(deleted_recordset) def cleanup(self, zone_name, recordset_id): - self.user_cloud.delete_recordset( - zone_name, recordset_id) + self.user_cloud.delete_recordset(zone_name, recordset_id) self.user_cloud.delete_zone(zone_name) diff --git a/openstack/tests/functional/cloud/test_router.py b/openstack/tests/functional/cloud/test_router.py index ef1dbc2d9..75dcf8d9c 100644 --- a/openstack/tests/functional/cloud/test_router.py +++ b/openstack/tests/functional/cloud/test_router.py @@ -24,8 +24,13 @@ from openstack.tests.functional import base EXPECTED_TOPLEVEL_FIELDS = ( - 'id', 'name', 'is_admin_state_up', 'external_gateway_info', - 'project_id', 'routes', 'status' + 'id', + 'name', + 'is_admin_state_up', + 'external_gateway_info', + 'project_id', + 'routes', + 'status', ) EXPECTED_GW_INFO_FIELDS = ('network_id', 'enable_snat', 'external_fixed_ips') @@ -90,7 +95,8 @@ class TestRouter(base.BaseFunctionalTest): def test_create_router_basic(self): net1_name = self.network_prefix + '_net1' net1 = self.operator_cloud.create_network( - name=net1_name, external=True) + name=net1_name, external=True + ) router_name = self.router_prefix + '_create_basic' router = self.operator_cloud.create_router( @@ -117,14 +123,15 @@ class TestRouter(base.BaseFunctionalTest): proj_id = project['id'] net1_name = self.network_prefix + '_net1' net1 = self.operator_cloud.create_network( - name=net1_name, external=True, project_id=proj_id) + name=net1_name, external=True, project_id=proj_id + ) router_name = self.router_prefix + '_create_project' router = self.operator_cloud.create_router( name=router_name, admin_state_up=True, ext_gateway_net_id=net1['id'], - project_id=proj_id + project_id=proj_id, ) for field in EXPECTED_TOPLEVEL_FIELDS: @@ -140,9 +147,9 @@ class TestRouter(base.BaseFunctionalTest): self.assertEqual(net1['id'], ext_gw_info['network_id']) self.assertTrue(ext_gw_info['enable_snat']) - def _create_and_verify_advanced_router(self, - external_cidr, - external_gateway_ip=None): + def _create_and_verify_advanced_router( + self, external_cidr, external_gateway_ip=None + ): # external_cidr must be passed in as unicode (u'') # NOTE(Shrews): The arguments are needed because these tests # will run in parallel and we want to make sure that each test @@ -150,10 +157,13 @@ class TestRouter(base.BaseFunctionalTest): net1_name = self.network_prefix + '_net1' sub1_name = self.subnet_prefix + '_sub1' net1 = self.operator_cloud.create_network( - name=net1_name, external=True) + name=net1_name, external=True + ) sub1 = self.operator_cloud.create_subnet( - net1['id'], external_cidr, subnet_name=sub1_name, - gateway_ip=external_gateway_ip + net1['id'], + external_cidr, + subnet_name=sub1_name, + gateway_ip=external_gateway_ip, ) ip_net = ipaddress.IPv4Network(external_cidr) @@ -165,9 +175,7 @@ class TestRouter(base.BaseFunctionalTest): admin_state_up=False, ext_gateway_net_id=net1['id'], enable_snat=False, - ext_fixed_ips=[ - {'subnet_id': sub1['id'], 'ip_address': last_ip} - ] + ext_fixed_ips=[{'subnet_id': sub1['id'], 'ip_address': last_ip}], ) for field in EXPECTED_TOPLEVEL_FIELDS: @@ -183,12 +191,10 @@ class TestRouter(base.BaseFunctionalTest): self.assertEqual(1, len(ext_gw_info['external_fixed_ips'])) self.assertEqual( - sub1['id'], - ext_gw_info['external_fixed_ips'][0]['subnet_id'] + sub1['id'], ext_gw_info['external_fixed_ips'][0]['subnet_id'] ) self.assertEqual( - last_ip, - ext_gw_info['external_fixed_ips'][0]['ip_address'] + last_ip, ext_gw_info['external_fixed_ips'][0]['ip_address'] ) return router @@ -198,20 +204,25 @@ class TestRouter(base.BaseFunctionalTest): def test_add_remove_router_interface(self): router = self._create_and_verify_advanced_router( - external_cidr=u'10.3.3.0/24') + external_cidr=u'10.3.3.0/24' + ) net_name = self.network_prefix + '_intnet1' sub_name = self.subnet_prefix + '_intsub1' net = self.operator_cloud.create_network(name=net_name) sub = self.operator_cloud.create_subnet( - net['id'], '10.4.4.0/24', subnet_name=sub_name, - gateway_ip='10.4.4.1' + net['id'], + '10.4.4.0/24', + subnet_name=sub_name, + gateway_ip='10.4.4.1', ) iface = self.operator_cloud.add_router_interface( - router, subnet_id=sub['id']) + router, subnet_id=sub['id'] + ) self.assertIsNone( self.operator_cloud.remove_router_interface( - router, subnet_id=sub['id']) + router, subnet_id=sub['id'] + ) ) # Test return values *after* the interface is detached so the @@ -224,25 +235,32 @@ class TestRouter(base.BaseFunctionalTest): def test_list_router_interfaces(self): router = self._create_and_verify_advanced_router( - external_cidr=u'10.5.5.0/24') + external_cidr=u'10.5.5.0/24' + ) net_name = self.network_prefix + '_intnet1' sub_name = self.subnet_prefix + '_intsub1' net = self.operator_cloud.create_network(name=net_name) sub = self.operator_cloud.create_subnet( - net['id'], '10.6.6.0/24', subnet_name=sub_name, - gateway_ip='10.6.6.1' + net['id'], + '10.6.6.0/24', + subnet_name=sub_name, + gateway_ip='10.6.6.1', ) iface = self.operator_cloud.add_router_interface( - router, subnet_id=sub['id']) + router, subnet_id=sub['id'] + ) all_ifaces = self.operator_cloud.list_router_interfaces(router) int_ifaces = self.operator_cloud.list_router_interfaces( - router, interface_type='internal') + router, interface_type='internal' + ) ext_ifaces = self.operator_cloud.list_router_interfaces( - router, interface_type='external') + router, interface_type='external' + ) self.assertIsNone( self.operator_cloud.remove_router_interface( - router, subnet_id=sub['id']) + router, subnet_id=sub['id'] + ) ) # Test return values *after* the interface is detached so the @@ -253,17 +271,21 @@ class TestRouter(base.BaseFunctionalTest): self.assertEqual(1, len(ext_ifaces)) ext_fixed_ips = router['external_gateway_info']['external_fixed_ips'] - self.assertEqual(ext_fixed_ips[0]['subnet_id'], - ext_ifaces[0]['fixed_ips'][0]['subnet_id']) + self.assertEqual( + ext_fixed_ips[0]['subnet_id'], + ext_ifaces[0]['fixed_ips'][0]['subnet_id'], + ) self.assertEqual(sub['id'], int_ifaces[0]['fixed_ips'][0]['subnet_id']) def test_update_router_name(self): router = self._create_and_verify_advanced_router( - external_cidr=u'10.7.7.0/24') + external_cidr=u'10.7.7.0/24' + ) new_name = self.router_prefix + '_update_name' updated = self.operator_cloud.update_router( - router['id'], name=new_name) + router['id'], name=new_name + ) self.assertIsNotNone(updated) for field in EXPECTED_TOPLEVEL_FIELDS: @@ -275,20 +297,20 @@ class TestRouter(base.BaseFunctionalTest): # Validate nothing else changed self.assertEqual(router['status'], updated['status']) self.assertEqual(router['admin_state_up'], updated['admin_state_up']) - self.assertEqual(router['external_gateway_info'], - updated['external_gateway_info']) + self.assertEqual( + router['external_gateway_info'], updated['external_gateway_info'] + ) def test_update_router_routes(self): router = self._create_and_verify_advanced_router( - external_cidr=u'10.7.7.0/24') + external_cidr=u'10.7.7.0/24' + ) - routes = [{ - "destination": "10.7.7.0/24", - "nexthop": "10.7.7.99" - }] + routes = [{"destination": "10.7.7.0/24", "nexthop": "10.7.7.99"}] updated = self.operator_cloud.update_router( - router['id'], routes=routes) + router['id'], routes=routes + ) self.assertIsNotNone(updated) for field in EXPECTED_TOPLEVEL_FIELDS: @@ -300,15 +322,18 @@ class TestRouter(base.BaseFunctionalTest): # Validate nothing else changed self.assertEqual(router['status'], updated['status']) self.assertEqual(router['admin_state_up'], updated['admin_state_up']) - self.assertEqual(router['external_gateway_info'], - updated['external_gateway_info']) + self.assertEqual( + router['external_gateway_info'], updated['external_gateway_info'] + ) def test_update_router_admin_state(self): router = self._create_and_verify_advanced_router( - external_cidr=u'10.8.8.0/24') + external_cidr=u'10.8.8.0/24' + ) updated = self.operator_cloud.update_router( - router['id'], admin_state_up=True) + router['id'], admin_state_up=True + ) self.assertIsNotNone(updated) for field in EXPECTED_TOPLEVEL_FIELDS: @@ -316,25 +341,30 @@ class TestRouter(base.BaseFunctionalTest): # admin_state_up is the only change we expect self.assertTrue(updated['admin_state_up']) - self.assertNotEqual(router['admin_state_up'], - updated['admin_state_up']) + self.assertNotEqual( + router['admin_state_up'], updated['admin_state_up'] + ) # Validate nothing else changed self.assertEqual(router['status'], updated['status']) self.assertEqual(router['name'], updated['name']) - self.assertEqual(router['external_gateway_info'], - updated['external_gateway_info']) + self.assertEqual( + router['external_gateway_info'], updated['external_gateway_info'] + ) def test_update_router_ext_gw_info(self): router = self._create_and_verify_advanced_router( - external_cidr=u'10.9.9.0/24') + external_cidr=u'10.9.9.0/24' + ) # create a new subnet existing_net_id = router['external_gateway_info']['network_id'] sub_name = self.subnet_prefix + '_update' sub = self.operator_cloud.create_subnet( - existing_net_id, '10.10.10.0/24', subnet_name=sub_name, - gateway_ip='10.10.10.1' + existing_net_id, + '10.10.10.0/24', + subnet_name=sub_name, + gateway_ip='10.10.10.1', ) updated = self.operator_cloud.update_router( @@ -342,7 +372,7 @@ class TestRouter(base.BaseFunctionalTest): ext_gateway_net_id=existing_net_id, ext_fixed_ips=[ {'subnet_id': sub['id'], 'ip_address': '10.10.10.77'} - ] + ], ) self.assertIsNotNone(updated) @@ -353,12 +383,10 @@ class TestRouter(base.BaseFunctionalTest): ext_gw_info = updated['external_gateway_info'] self.assertEqual(1, len(ext_gw_info['external_fixed_ips'])) self.assertEqual( - sub['id'], - ext_gw_info['external_fixed_ips'][0]['subnet_id'] + sub['id'], ext_gw_info['external_fixed_ips'][0]['subnet_id'] ) self.assertEqual( - '10.10.10.77', - ext_gw_info['external_fixed_ips'][0]['ip_address'] + '10.10.10.77', ext_gw_info['external_fixed_ips'][0]['ip_address'] ) # Validate nothing else changed diff --git a/openstack/tests/functional/cloud/test_security_groups.py b/openstack/tests/functional/cloud/test_security_groups.py index 54ceda6fc..3ad785c9a 100644 --- a/openstack/tests/functional/cloud/test_security_groups.py +++ b/openstack/tests/functional/cloud/test_security_groups.py @@ -23,7 +23,8 @@ from openstack.tests.functional import base class TestSecurityGroups(base.BaseFunctionalTest): def test_create_list_security_groups(self): sg1 = self.user_cloud.create_security_group( - name="sg1", description="sg1") + name="sg1", description="sg1" + ) self.addCleanup(self.user_cloud.delete_security_group, sg1['id']) if self.user_cloud.has_service('network'): # Neutron defaults to all_tenants=1 when admin @@ -39,10 +40,12 @@ class TestSecurityGroups(base.BaseFunctionalTest): self.skipTest("Operator cloud is required for this test") sg1 = self.user_cloud.create_security_group( - name="sg1", description="sg1") + name="sg1", description="sg1" + ) self.addCleanup(self.user_cloud.delete_security_group, sg1['id']) sg2 = self.operator_cloud.create_security_group( - name="sg2", description="sg2") + name="sg2", description="sg2" + ) self.addCleanup(self.operator_cloud.delete_security_group, sg2['id']) if self.user_cloud.has_service('network'): @@ -53,7 +56,8 @@ class TestSecurityGroups(base.BaseFunctionalTest): # Filter by tenant_id (filtering by project_id won't work with # Keystone V2) sg_list = self.operator_cloud.list_security_groups( - filters={'tenant_id': self.user_cloud.current_project_id}) + filters={'tenant_id': self.user_cloud.current_project_id} + ) self.assertIn(sg1['id'], [sg['id'] for sg in sg_list]) self.assertNotIn(sg2['id'], [sg['id'] for sg in sg_list]) @@ -64,7 +68,8 @@ class TestSecurityGroups(base.BaseFunctionalTest): self.assertNotIn(sg1['id'], [sg['id'] for sg in sg_list]) sg_list = self.operator_cloud.list_security_groups( - filters={'all_tenants': 1}) + filters={'all_tenants': 1} + ) self.assertIn(sg1['id'], [sg['id'] for sg in sg_list]) def test_get_security_group_by_id(self): diff --git a/openstack/tests/functional/cloud/test_server_group.py b/openstack/tests/functional/cloud/test_server_group.py index a1f645039..77d31d38d 100644 --- a/openstack/tests/functional/cloud/test_server_group.py +++ b/openstack/tests/functional/cloud/test_server_group.py @@ -21,15 +21,16 @@ from openstack.tests.functional import base class TestServerGroup(base.BaseFunctionalTest): - def test_server_group(self): server_group_name = self.getUniqueString() self.addCleanup(self.cleanup, server_group_name) server_group = self.user_cloud.create_server_group( - server_group_name, ['affinity']) + server_group_name, ['affinity'] + ) - server_group_ids = [v['id'] - for v in self.user_cloud.list_server_groups()] + server_group_ids = [ + v['id'] for v in self.user_cloud.list_server_groups() + ] self.assertIn(server_group['id'], server_group_ids) self.user_cloud.delete_server_group(server_group_name) diff --git a/openstack/tests/functional/cloud/test_services.py b/openstack/tests/functional/cloud/test_services.py index 70c85d58d..30ba4f5a4 100644 --- a/openstack/tests/functional/cloud/test_services.py +++ b/openstack/tests/functional/cloud/test_services.py @@ -38,15 +38,17 @@ class TestServices(base.KeystoneBaseFunctionalTest): # Generate a random name for services in this test self.new_service_name = 'test_' + ''.join( - random.choice(string.ascii_lowercase) for _ in range(5)) + random.choice(string.ascii_lowercase) for _ in range(5) + ) self.addCleanup(self._cleanup_services) def _cleanup_services(self): exception_list = list() for s in self.operator_cloud.list_services(): - if s['name'] is not None and \ - s['name'].startswith(self.new_service_name): + if s['name'] is not None and s['name'].startswith( + self.new_service_name + ): try: self.operator_cloud.delete_service(name_or_id=s['id']) except Exception as e: @@ -60,45 +62,57 @@ class TestServices(base.KeystoneBaseFunctionalTest): def test_create_service(self): service = self.operator_cloud.create_service( - name=self.new_service_name + '_create', type='test_type', - description='this is a test description') + name=self.new_service_name + '_create', + type='test_type', + description='this is a test description', + ) self.assertIsNotNone(service.get('id')) def test_update_service(self): ver = self.operator_cloud.config.get_api_version('identity') if ver.startswith('2'): # NOTE(SamYaple): Update service only works with v3 api - self.assertRaises(OpenStackCloudUnavailableFeature, - self.operator_cloud.update_service, - 'service_id', name='new name') + self.assertRaises( + OpenStackCloudUnavailableFeature, + self.operator_cloud.update_service, + 'service_id', + name='new name', + ) else: service = self.operator_cloud.create_service( - name=self.new_service_name + '_create', type='test_type', - description='this is a test description', enabled=True) + name=self.new_service_name + '_create', + type='test_type', + description='this is a test description', + enabled=True, + ) new_service = self.operator_cloud.update_service( service.id, name=self.new_service_name + '_update', description='this is an updated description', - enabled=False + enabled=False, + ) + self.assertEqual( + new_service.name, self.new_service_name + '_update' + ) + self.assertEqual( + new_service.description, 'this is an updated description' ) - self.assertEqual(new_service.name, - self.new_service_name + '_update') - self.assertEqual(new_service.description, - 'this is an updated description') self.assertFalse(new_service.is_enabled) self.assertEqual(service.id, new_service.id) def test_list_services(self): service = self.operator_cloud.create_service( - name=self.new_service_name + '_list', type='test_type') + name=self.new_service_name + '_list', type='test_type' + ) observed_services = self.operator_cloud.list_services() self.assertIsInstance(observed_services, list) found = False for s in observed_services: # Test all attributes are returned if s['id'] == service['id']: - self.assertEqual(self.new_service_name + '_list', - s.get('name')) + self.assertEqual( + self.new_service_name + '_list', s.get('name') + ) self.assertEqual('test_type', s.get('type')) found = True self.assertTrue(found, msg='new service not found in service list!') @@ -106,8 +120,8 @@ class TestServices(base.KeystoneBaseFunctionalTest): def test_delete_service_by_name(self): # Test delete by name service = self.operator_cloud.create_service( - name=self.new_service_name + '_delete_by_name', - type='test_type') + name=self.new_service_name + '_delete_by_name', type='test_type' + ) self.operator_cloud.delete_service(name_or_id=service['name']) observed_services = self.operator_cloud.list_services() found = False @@ -120,8 +134,8 @@ class TestServices(base.KeystoneBaseFunctionalTest): def test_delete_service_by_id(self): # Test delete by id service = self.operator_cloud.create_service( - name=self.new_service_name + '_delete_by_id', - type='test_type') + name=self.new_service_name + '_delete_by_id', type='test_type' + ) self.operator_cloud.delete_service(name_or_id=service['id']) observed_services = self.operator_cloud.list_services() found = False diff --git a/openstack/tests/functional/cloud/test_stack.py b/openstack/tests/functional/cloud/test_stack.py index 0df37c462..ee2ae9775 100644 --- a/openstack/tests/functional/cloud/test_stack.py +++ b/openstack/tests/functional/cloud/test_stack.py @@ -73,7 +73,6 @@ validate_template = '''heat_template_version: asdf-no-such-version ''' class TestStack(base.BaseFunctionalTest): - def setUp(self): super(TestStack, self).setUp() if not self.user_cloud.has_service('orchestration'): @@ -88,10 +87,12 @@ class TestStack(base.BaseFunctionalTest): test_template.write(validate_template.encode('utf-8')) test_template.close() stack_name = self.getUniqueString('validate_template') - self.assertRaises(exc.OpenStackCloudException, - self.user_cloud.create_stack, - name=stack_name, - template_file=test_template.name) + self.assertRaises( + exc.OpenStackCloudException, + self.user_cloud.create_stack, + name=stack_name, + template_file=test_template.name, + ) def test_stack_simple(self): test_template = tempfile.NamedTemporaryFile(delete=False) @@ -100,9 +101,8 @@ class TestStack(base.BaseFunctionalTest): self.stack_name = self.getUniqueString('simple_stack') self.addCleanup(self._cleanup_stack) stack = self.user_cloud.create_stack( - name=self.stack_name, - template_file=test_template.name, - wait=True) + name=self.stack_name, template_file=test_template.name, wait=True + ) # assert expected values in stack self.assertEqual('CREATE_COMPLETE', stack['stack_status']) @@ -121,9 +121,8 @@ class TestStack(base.BaseFunctionalTest): # update with no changes stack = self.user_cloud.update_stack( - self.stack_name, - template_file=test_template.name, - wait=True) + self.stack_name, template_file=test_template.name, wait=True + ) # assert no change in updated stack self.assertEqual('UPDATE_COMPLETE', stack['stack_status']) @@ -135,7 +134,8 @@ class TestStack(base.BaseFunctionalTest): self.stack_name, template_file=test_template.name, wait=True, - length=12) + length=12, + ) # assert changed output in updated stack stack = self.user_cloud.get_stack(self.stack_name) @@ -147,7 +147,8 @@ class TestStack(base.BaseFunctionalTest): def test_stack_nested(self): test_template = tempfile.NamedTemporaryFile( - suffix='.yaml', delete=False) + suffix='.yaml', delete=False + ) test_template.write(root_template.encode('utf-8')) test_template.close() @@ -166,7 +167,8 @@ class TestStack(base.BaseFunctionalTest): name=self.stack_name, template_file=test_template.name, environment_files=[env.name], - wait=True) + wait=True, + ) # assert expected values in stack self.assertEqual('CREATE_COMPLETE', stack['stack_status']) diff --git a/openstack/tests/functional/cloud/test_users.py b/openstack/tests/functional/cloud/test_users.py index 3dc94a220..32e58b231 100644 --- a/openstack/tests/functional/cloud/test_users.py +++ b/openstack/tests/functional/cloud/test_users.py @@ -105,7 +105,8 @@ class TestUsers(base.KeystoneBaseFunctionalTest): email='somebody@nowhere.com', enabled=False, password='secret', - description='') + description='', + ) self.assertIsNotNone(new_user) self.assertEqual(user['id'], new_user['id']) self.assertEqual(user_name + '2', new_user['name']) @@ -115,30 +116,37 @@ class TestUsers(base.KeystoneBaseFunctionalTest): def test_update_user_password(self): user_name = self.user_prefix + '_password' user_email = 'nobody@nowhere.com' - user = self._create_user(name=user_name, - email=user_email, - password='old_secret') + user = self._create_user( + name=user_name, email=user_email, password='old_secret' + ) self.assertIsNotNone(user) self.assertTrue(user['enabled']) # This should work for both v2 and v3 new_user = self.operator_cloud.update_user( - user['id'], password='new_secret') + user['id'], password='new_secret' + ) self.assertIsNotNone(new_user) self.assertEqual(user['id'], new_user['id']) self.assertEqual(user_name, new_user['name']) self.assertEqual(user_email, new_user['email']) self.assertTrue(new_user['enabled']) - self.assertTrue(self.operator_cloud.grant_role( - 'member', user=user['id'], project='demo', wait=True)) + self.assertTrue( + self.operator_cloud.grant_role( + 'member', user=user['id'], project='demo', wait=True + ) + ) self.addCleanup( self.operator_cloud.revoke_role, - 'member', user=user['id'], project='demo', wait=True) + 'member', + user=user['id'], + project='demo', + wait=True, + ) new_cloud = self.operator_cloud.connect_as( - user_id=user['id'], - password='new_secret', - project_name='demo') + user_id=user['id'], password='new_secret', project_name='demo' + ) self.assertIsNotNone(new_cloud) location = new_cloud.current_location @@ -166,9 +174,11 @@ class TestUsers(base.KeystoneBaseFunctionalTest): # Add the user to the group self.operator_cloud.add_user_to_group(user_name, group_name) self.assertTrue( - self.operator_cloud.is_user_in_group(user_name, group_name)) + self.operator_cloud.is_user_in_group(user_name, group_name) + ) # Remove them from the group self.operator_cloud.remove_user_from_group(user_name, group_name) self.assertFalse( - self.operator_cloud.is_user_in_group(user_name, group_name)) + self.operator_cloud.is_user_in_group(user_name, group_name) + ) diff --git a/openstack/tests/functional/cloud/test_volume.py b/openstack/tests/functional/cloud/test_volume.py index 7b355f928..722f59967 100644 --- a/openstack/tests/functional/cloud/test_volume.py +++ b/openstack/tests/functional/cloud/test_volume.py @@ -43,10 +43,10 @@ class TestVolume(base.BaseFunctionalTest): self.addDetail('volume', content.text_content(volume_name)) self.addCleanup(self.cleanup, volume_name, snapshot_name=snapshot_name) volume = self.user_cloud.create_volume( - display_name=volume_name, size=1) + display_name=volume_name, size=1 + ) snapshot = self.user_cloud.create_volume_snapshot( - volume['id'], - display_name=snapshot_name + volume['id'], display_name=snapshot_name ) ret_volume = self.user_cloud.get_volume_by_id(volume['id']) @@ -60,7 +60,8 @@ class TestVolume(base.BaseFunctionalTest): self.assertIn(snapshot['id'], snapshot_ids) ret_snapshot = self.user_cloud.get_volume_snapshot_by_id( - snapshot['id']) + snapshot['id'] + ) self.assertEqual(snapshot['id'], ret_snapshot['id']) self.user_cloud.delete_volume_snapshot(snapshot_name, wait=True) @@ -73,9 +74,11 @@ class TestVolume(base.BaseFunctionalTest): self.addDetail('volume', content.text_content(volume_name)) self.addCleanup(self.cleanup, volume_name, image_name=image_name) volume = self.user_cloud.create_volume( - display_name=volume_name, size=1) + display_name=volume_name, size=1 + ) image = self.user_cloud.create_image( - image_name, volume=volume, wait=True) + image_name, volume=volume, wait=True + ) volume_ids = [v['id'] for v in self.user_cloud.list_volumes()] self.assertIn(volume['id'], volume_ids) @@ -93,7 +96,8 @@ class TestVolume(base.BaseFunctionalTest): snapshot = self.user_cloud.get_volume_snapshot(snapshot_name) if snapshot: self.user_cloud.delete_volume_snapshot( - snapshot_name, wait=True) + snapshot_name, wait=True + ) if image_name: image = self.user_cloud.get_image(image_name) if image: @@ -108,7 +112,8 @@ class TestVolume(base.BaseFunctionalTest): self.user_cloud.delete_volume(v, wait=False) try: for count in utils.iterate_timeout( - 180, "Timeout waiting for volume cleanup"): + 180, "Timeout waiting for volume cleanup" + ): found = False for existing in self.user_cloud.list_volumes(): for v in volume: @@ -127,7 +132,8 @@ class TestVolume(base.BaseFunctionalTest): for v in volume: if v['id'] == existing['id']: self.operator_cloud.delete_volume( - v, wait=False, force=True) + v, wait=False, force=True + ) def test_list_volumes_pagination(self): '''Test pagination for list volumes functionality''' @@ -146,9 +152,7 @@ class TestVolume(base.BaseFunctionalTest): for i in self.user_cloud.list_volumes(): if i['name'] and i['name'].startswith(self.id()): result.append(i['id']) - self.assertEqual( - sorted([i['id'] for i in volumes]), - sorted(result)) + self.assertEqual(sorted([i['id'] for i in volumes]), sorted(result)) def test_update_volume(self): name, desc = self.getUniqueString('name'), self.getUniqueString('desc') diff --git a/openstack/tests/functional/cloud/test_volume_backup.py b/openstack/tests/functional/cloud/test_volume_backup.py index c864f88b8..ce3c89e1c 100644 --- a/openstack/tests/functional/cloud/test_volume_backup.py +++ b/openstack/tests/functional/cloud/test_volume_backup.py @@ -27,14 +27,18 @@ class TestVolume(base.BaseFunctionalTest): def test_create_get_delete_volume_backup(self): volume = self.user_cloud.create_volume( - display_name=self.getUniqueString(), size=1) + display_name=self.getUniqueString(), size=1 + ) self.addCleanup(self.user_cloud.delete_volume, volume['id']) backup_name_1 = self.getUniqueString() backup_desc_1 = self.getUniqueString() backup = self.user_cloud.create_volume_backup( - volume_id=volume['id'], name=backup_name_1, - description=backup_desc_1, wait=True) + volume_id=volume['id'], + name=backup_name_1, + description=backup_desc_1, + wait=True, + ) self.assertEqual(backup_name_1, backup['name']) backup = self.user_cloud.get_volume_backup(backup['id']) @@ -48,11 +52,13 @@ class TestVolume(base.BaseFunctionalTest): volume = self.user_cloud.create_volume(size=1) snapshot = self.user_cloud.create_volume_snapshot(volume['id']) self.addCleanup(self.user_cloud.delete_volume, volume['id']) - self.addCleanup(self.user_cloud.delete_volume_snapshot, snapshot['id'], - wait=True) + self.addCleanup( + self.user_cloud.delete_volume_snapshot, snapshot['id'], wait=True + ) backup = self.user_cloud.create_volume_backup( - volume_id=volume['id'], snapshot_id=snapshot['id'], wait=True) + volume_id=volume['id'], snapshot_id=snapshot['id'], wait=True + ) backup = self.user_cloud.get_volume_backup(backup['id']) self.assertEqual(backup['snapshot_id'], snapshot['id']) @@ -65,9 +71,11 @@ class TestVolume(base.BaseFunctionalTest): self.addCleanup(self.user_cloud.delete_volume, volume['id']) full_backup = self.user_cloud.create_volume_backup( - volume_id=volume['id'], wait=True) + volume_id=volume['id'], wait=True + ) incr_backup = self.user_cloud.create_volume_backup( - volume_id=volume['id'], incremental=True, wait=True) + volume_id=volume['id'], incremental=True, wait=True + ) full_backup = self.user_cloud.get_volume_backup(full_backup['id']) incr_backup = self.user_cloud.get_volume_backup(incr_backup['id']) @@ -81,7 +89,8 @@ class TestVolume(base.BaseFunctionalTest): def test_list_volume_backups(self): vol1 = self.user_cloud.create_volume( - display_name=self.getUniqueString(), size=1) + display_name=self.getUniqueString(), size=1 + ) self.addCleanup(self.user_cloud.delete_volume, vol1['id']) # We create 2 volumes to create 2 backups. We could have created 2 @@ -89,12 +98,14 @@ class TestVolume(base.BaseFunctionalTest): # to be race-condition prone. And I didn't want to use an ugly sleep() # here. vol2 = self.user_cloud.create_volume( - display_name=self.getUniqueString(), size=1) + display_name=self.getUniqueString(), size=1 + ) self.addCleanup(self.user_cloud.delete_volume, vol2['id']) backup_name_1 = self.getUniqueString() backup = self.user_cloud.create_volume_backup( - volume_id=vol1['id'], name=backup_name_1) + volume_id=vol1['id'], name=backup_name_1 + ) self.addCleanup(self.user_cloud.delete_volume_backup, backup['id']) backup = self.user_cloud.create_volume_backup(volume_id=vol2['id']) @@ -104,6 +115,7 @@ class TestVolume(base.BaseFunctionalTest): self.assertEqual(2, len(backups)) backups = self.user_cloud.list_volume_backups( - search_opts={"name": backup_name_1}) + search_opts={"name": backup_name_1} + ) self.assertEqual(1, len(backups)) self.assertEqual(backup_name_1, backups[0]['name']) diff --git a/openstack/tests/functional/cloud/test_volume_type.py b/openstack/tests/functional/cloud/test_volume_type.py index d71ee014c..ab8b0e5a6 100644 --- a/openstack/tests/functional/cloud/test_volume_type.py +++ b/openstack/tests/functional/cloud/test_volume_type.py @@ -25,7 +25,6 @@ from openstack.tests.functional import base class TestVolumeType(base.BaseFunctionalTest): - def _assert_project(self, volume_name_or_id, project_id, allowed=True): acls = self.operator_cloud.get_volume_type_access(volume_name_or_id) allowed_projects = [x.get('project_id') for x in acls] @@ -40,83 +39,87 @@ class TestVolumeType(base.BaseFunctionalTest): volume_type = { "name": 'test-volume-type', "description": None, - "os-volume-type-access:is_public": False} + "os-volume-type-access:is_public": False, + } self.operator_cloud.block_storage.post( - '/types', json={'volume_type': volume_type}) + '/types', json={'volume_type': volume_type} + ) def tearDown(self): ret = self.operator_cloud.get_volume_type('test-volume-type') if ret.get('id'): self.operator_cloud.block_storage.delete( - '/types/{volume_type_id}'.format(volume_type_id=ret.id)) + '/types/{volume_type_id}'.format(volume_type_id=ret.id) + ) super(TestVolumeType, self).tearDown() def test_list_volume_types(self): volume_types = self.operator_cloud.list_volume_types() self.assertTrue(volume_types) - self.assertTrue(any( - x for x in volume_types if x.name == 'test-volume-type')) + self.assertTrue( + any(x for x in volume_types if x.name == 'test-volume-type') + ) def test_add_remove_volume_type_access(self): volume_type = self.operator_cloud.get_volume_type('test-volume-type') self.assertEqual('test-volume-type', volume_type.name) self.operator_cloud.add_volume_type_access( - 'test-volume-type', - self.operator_cloud.current_project_id) + 'test-volume-type', self.operator_cloud.current_project_id + ) self._assert_project( - 'test-volume-type', self.operator_cloud.current_project_id, - allowed=True) + 'test-volume-type', + self.operator_cloud.current_project_id, + allowed=True, + ) self.operator_cloud.remove_volume_type_access( - 'test-volume-type', - self.operator_cloud.current_project_id) + 'test-volume-type', self.operator_cloud.current_project_id + ) self._assert_project( - 'test-volume-type', self.operator_cloud.current_project_id, - allowed=False) + 'test-volume-type', + self.operator_cloud.current_project_id, + allowed=False, + ) def test_add_volume_type_access_missing_project(self): # Project id is not valitaded and it may not exist. self.operator_cloud.add_volume_type_access( - 'test-volume-type', - '00000000000000000000000000000000') + 'test-volume-type', '00000000000000000000000000000000' + ) self.operator_cloud.remove_volume_type_access( - 'test-volume-type', - '00000000000000000000000000000000') + 'test-volume-type', '00000000000000000000000000000000' + ) def test_add_volume_type_access_missing_volume(self): with testtools.ExpectedException( - exc.OpenStackCloudException, - "VolumeType not found.*" + exc.OpenStackCloudException, "VolumeType not found.*" ): self.operator_cloud.add_volume_type_access( - 'MISSING_VOLUME_TYPE', - self.operator_cloud.current_project_id) + 'MISSING_VOLUME_TYPE', self.operator_cloud.current_project_id + ) def test_remove_volume_type_access_missing_volume(self): with testtools.ExpectedException( - exc.OpenStackCloudException, - "VolumeType not found.*" + exc.OpenStackCloudException, "VolumeType not found.*" ): self.operator_cloud.remove_volume_type_access( - 'MISSING_VOLUME_TYPE', - self.operator_cloud.current_project_id) + 'MISSING_VOLUME_TYPE', self.operator_cloud.current_project_id + ) def test_add_volume_type_access_bad_project(self): with testtools.ExpectedException( - exc.OpenStackCloudBadRequest, - "Unable to authorize.*" + exc.OpenStackCloudBadRequest, "Unable to authorize.*" ): self.operator_cloud.add_volume_type_access( - 'test-volume-type', - 'BAD_PROJECT_ID') + 'test-volume-type', 'BAD_PROJECT_ID' + ) def test_remove_volume_type_access_missing_project(self): with testtools.ExpectedException( - exc.OpenStackCloudURINotFound, - "Unable to revoke.*" + exc.OpenStackCloudURINotFound, "Unable to revoke.*" ): self.operator_cloud.remove_volume_type_access( - 'test-volume-type', - '00000000000000000000000000000000') + 'test-volume-type', '00000000000000000000000000000000' + ) diff --git a/openstack/tests/functional/cloud/test_zone.py b/openstack/tests/functional/cloud/test_zone.py index 31ea5de15..c9625fe0f 100644 --- a/openstack/tests/functional/cloud/test_zone.py +++ b/openstack/tests/functional/cloud/test_zone.py @@ -23,7 +23,6 @@ from openstack.tests.functional import base class TestZone(base.BaseFunctionalTest): - def setUp(self): super(TestZone, self).setUp() if not self.user_cloud.has_service('dns'): @@ -43,9 +42,13 @@ class TestZone(base.BaseFunctionalTest): # Test we can create a zone and we get it returned zone = self.user_cloud.create_zone( - name=name, zone_type=zone_type, email=email, - description=description, ttl=ttl, - masters=masters) + name=name, + zone_type=zone_type, + email=email, + description=description, + ttl=ttl, + masters=masters, + ) self.assertEqual(zone['name'], name) self.assertEqual(zone['type'], zone_type.upper()) self.assertEqual(zone['email'], email) diff --git a/openstack/tests/unit/cloud/test__utils.py b/openstack/tests/unit/cloud/test__utils.py index 20cabae4f..de5a37cde 100644 --- a/openstack/tests/unit/cloud/test__utils.py +++ b/openstack/tests/unit/cloud/test__utils.py @@ -32,7 +32,6 @@ RANGE_DATA = [ class TestUtils(base.TestCase): - def test__filter_list_name_or_id(self): el1 = dict(id=100, name='donald') el2 = dict(id=200, name='pluto') @@ -85,18 +84,28 @@ class TestUtils(base.TestCase): self.assertEqual([], ret) def test__filter_list_unicode(self): - el1 = dict(id=100, name=u'中文', last='duck', - other=dict(category='duck', financial=dict(status='poor'))) - el2 = dict(id=200, name=u'中文', last='trump', - other=dict(category='human', financial=dict(status='rich'))) - el3 = dict(id=300, name='donald', last='ronald mac', - other=dict(category='clown', financial=dict(status='rich'))) + el1 = dict( + id=100, + name=u'中文', + last='duck', + other=dict(category='duck', financial=dict(status='poor')), + ) + el2 = dict( + id=200, + name=u'中文', + last='trump', + other=dict(category='human', financial=dict(status='rich')), + ) + el3 = dict( + id=300, + name='donald', + last='ronald mac', + other=dict(category='clown', financial=dict(status='rich')), + ) data = [el1, el2, el3] ret = _utils._filter_list( - data, u'中文', - {'other': { - 'financial': {'status': 'rich'} - }}) + data, u'中文', {'other': {'financial': {'status': 'rich'}}} + ) self.assertEqual([el2], ret) def test__filter_list_filter(self): @@ -114,30 +123,47 @@ class TestUtils(base.TestCase): self.assertEqual([el1], ret) def test__filter_list_dict1(self): - el1 = dict(id=100, name='donald', last='duck', - other=dict(category='duck')) - el2 = dict(id=200, name='donald', last='trump', - other=dict(category='human')) - el3 = dict(id=300, name='donald', last='ronald mac', - other=dict(category='clown')) + el1 = dict( + id=100, name='donald', last='duck', other=dict(category='duck') + ) + el2 = dict( + id=200, name='donald', last='trump', other=dict(category='human') + ) + el3 = dict( + id=300, + name='donald', + last='ronald mac', + other=dict(category='clown'), + ) data = [el1, el2, el3] ret = _utils._filter_list( - data, 'donald', {'other': {'category': 'clown'}}) + data, 'donald', {'other': {'category': 'clown'}} + ) self.assertEqual([el3], ret) def test__filter_list_dict2(self): - el1 = dict(id=100, name='donald', last='duck', - other=dict(category='duck', financial=dict(status='poor'))) - el2 = dict(id=200, name='donald', last='trump', - other=dict(category='human', financial=dict(status='rich'))) - el3 = dict(id=300, name='donald', last='ronald mac', - other=dict(category='clown', financial=dict(status='rich'))) + el1 = dict( + id=100, + name='donald', + last='duck', + other=dict(category='duck', financial=dict(status='poor')), + ) + el2 = dict( + id=200, + name='donald', + last='trump', + other=dict(category='human', financial=dict(status='rich')), + ) + el3 = dict( + id=300, + name='donald', + last='ronald mac', + other=dict(category='clown', financial=dict(status='rich')), + ) data = [el1, el2, el3] ret = _utils._filter_list( - data, 'donald', - {'other': { - 'financial': {'status': 'rich'} - }}) + data, 'donald', {'other': {'financial': {'status': 'rich'}}} + ) self.assertEqual([el2, el3], ret) def test_safe_dict_min_ints(self): @@ -176,7 +202,7 @@ class TestUtils(base.TestCase): with testtools.ExpectedException( exc.OpenStackCloudException, "Search for minimum value failed. " - "Value for f1 is not an integer: aaa" + "Value for f1 is not an integer: aaa", ): _utils.safe_dict_min('f1', data) @@ -216,7 +242,7 @@ class TestUtils(base.TestCase): with testtools.ExpectedException( exc.OpenStackCloudException, "Search for maximum value failed. " - "Value for f1 is not an integer: aaa" + "Value for f1 is not an integer: aaa", ): _utils.safe_dict_max('f1', data) @@ -282,15 +308,13 @@ class TestUtils(base.TestCase): def test_range_filter_invalid_int(self): with testtools.ExpectedException( - exc.OpenStackCloudException, - "Invalid range value: <1A0" + exc.OpenStackCloudException, "Invalid range value: <1A0" ): _utils.range_filter(RANGE_DATA, "key1", "<1A0") def test_range_filter_invalid_op(self): with testtools.ExpectedException( - exc.OpenStackCloudException, - "Invalid range value: <>100" + exc.OpenStackCloudException, "Invalid range value: <>100" ): _utils.range_filter(RANGE_DATA, "key1", "<>100") @@ -330,8 +354,16 @@ class TestUtils(base.TestCase): def test_get_entity_pass_uuid(self): uuid = uuid4().hex self.cloud.use_direct_get = True - resources = ['flavor', 'image', 'volume', 'network', - 'subnet', 'port', 'floating_ip', 'security_group'] + resources = [ + 'flavor', + 'image', + 'volume', + 'network', + 'subnet', + 'port', + 'floating_ip', + 'security_group', + ] for r in resources: f = 'get_%s_by_id' % r with mock.patch.object(self.cloud, f) as get: @@ -340,8 +372,16 @@ class TestUtils(base.TestCase): def test_get_entity_pass_search_methods(self): self.cloud.use_direct_get = True - resources = ['flavor', 'image', 'volume', 'network', - 'subnet', 'port', 'floating_ip', 'security_group'] + resources = [ + 'flavor', + 'image', + 'volume', + 'network', + 'subnet', + 'port', + 'floating_ip', + 'security_group', + ] filters = {} name = 'name_no_uuid' for r in resources: @@ -351,8 +391,16 @@ class TestUtils(base.TestCase): search.assert_called_once_with(name, filters) def test_get_entity_get_and_search(self): - resources = ['flavor', 'image', 'volume', 'network', - 'subnet', 'port', 'floating_ip', 'security_group'] + resources = [ + 'flavor', + 'image', + 'volume', + 'network', + 'subnet', + 'port', + 'floating_ip', + 'security_group', + ] for r in resources: self.assertTrue(hasattr(self.cloud, 'get_%s_by_id' % r)) self.assertTrue(hasattr(self.cloud, 'search_%ss' % r)) diff --git a/openstack/tests/unit/cloud/test_accelerator.py b/openstack/tests/unit/cloud/test_accelerator.py index 39dcf761e..e42757073 100644 --- a/openstack/tests/unit/cloud/test_accelerator.py +++ b/openstack/tests/unit/cloud/test_accelerator.py @@ -23,7 +23,7 @@ DEP_DICT = { 'parent_id': None, 'root_id': 1, 'num_accelerators': 4, - 'device_id': 0 + 'device_id': 0, } DEV_UUID = uuid.uuid4().hex @@ -40,14 +40,16 @@ DEV_DICT = { DEV_PROF_UUID = uuid.uuid4().hex DEV_PROF_GROUPS = [ - {"resources:ACCELERATOR_FPGA": "1", - "trait:CUSTOM_FPGA_INTEL_PAC_ARRIA10": "required", - "trait:CUSTOM_FUNCTION_ID_3AFB": "required", - }, - {"resources:CUSTOM_ACCELERATOR_FOO": "2", - "resources:CUSTOM_MEMORY": "200", - "trait:CUSTOM_TRAIT_ALWAYS": "required", - } + { + "resources:ACCELERATOR_FPGA": "1", + "trait:CUSTOM_FPGA_INTEL_PAC_ARRIA10": "required", + "trait:CUSTOM_FUNCTION_ID_3AFB": "required", + }, + { + "resources:CUSTOM_ACCELERATOR_FOO": "2", + "resources:CUSTOM_MEMORY": "200", + "trait:CUSTOM_TRAIT_ALWAYS": "required", + }, ] DEV_PROF_DICT = { "id": 1, @@ -61,10 +63,9 @@ NEW_DEV_PROF_DICT = copy.copy(DEV_PROF_DICT) ARQ_UUID = uuid.uuid4().hex ARQ_DEV_RP_UUID = uuid.uuid4().hex ARQ_INSTANCE_UUID = uuid.uuid4().hex -ARQ_ATTACH_INFO_STR = '{"bus": "5e", '\ - '"device": "00", '\ - '"domain": "0000", '\ - '"function": "1"}' +ARQ_ATTACH_INFO_STR = ( + '{"bus": "5e", ' '"device": "00", ' '"domain": "0000", ' '"function": "1"}' +) ARQ_DICT = { 'uuid': ARQ_UUID, 'hostname': 'test_hostname', @@ -85,36 +86,41 @@ class TestAccelerator(base.TestCase): self.use_cyborg() def test_list_deployables(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'deployables']), - json={'deployables': [DEP_DICT]} - ), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', 'public', append=['v2', 'deployables'] + ), + json={'deployables': [DEP_DICT]}, + ), + ] + ) dep_list = self.cloud.list_deployables() self.assertEqual(len(dep_list), 1) self.assertEqual(dep_list[0].id, DEP_DICT['uuid']) self.assertEqual(dep_list[0].name, DEP_DICT['name']) self.assertEqual(dep_list[0].parent_id, DEP_DICT['parent_id']) self.assertEqual(dep_list[0].root_id, DEP_DICT['root_id']) - self.assertEqual(dep_list[0].num_accelerators, - DEP_DICT['num_accelerators']) + self.assertEqual( + dep_list[0].num_accelerators, DEP_DICT['num_accelerators'] + ) self.assertEqual(dep_list[0].device_id, DEP_DICT['device_id']) self.assert_calls() def test_list_devices(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'devices']), - json={'devices': [DEV_DICT]} - ), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', 'public', append=['v2', 'devices'] + ), + json={'devices': [DEV_DICT]}, + ), + ] + ) dev_list = self.cloud.list_devices() self.assertEqual(len(dev_list), 1) self.assertEqual(dev_list[0].id, DEV_DICT['id']) @@ -123,22 +129,28 @@ class TestAccelerator(base.TestCase): self.assertEqual(dev_list[0].type, DEV_DICT['type']) self.assertEqual(dev_list[0].vendor, DEV_DICT['vendor']) self.assertEqual(dev_list[0].model, DEV_DICT['model']) - self.assertEqual(dev_list[0].std_board_info, - DEV_DICT['std_board_info']) - self.assertEqual(dev_list[0].vendor_board_info, - DEV_DICT['vendor_board_info']) + self.assertEqual( + dev_list[0].std_board_info, DEV_DICT['std_board_info'] + ) + self.assertEqual( + dev_list[0].vendor_board_info, DEV_DICT['vendor_board_info'] + ) self.assert_calls() def test_list_device_profiles(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'device_profiles']), - json={'device_profiles': [DEV_PROF_DICT]} - ), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=['v2', 'device_profiles'], + ), + json={'device_profiles': [DEV_PROF_DICT]}, + ), + ] + ) dev_prof_list = self.cloud.list_device_profiles() self.assertEqual(len(dev_prof_list), 1) self.assertEqual(dev_prof_list[0].id, DEV_PROF_DICT['id']) @@ -148,183 +160,248 @@ class TestAccelerator(base.TestCase): self.assert_calls() def test_create_device_profile(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'device_profiles']), - json=NEW_DEV_PROF_DICT) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=['v2', 'device_profiles'], + ), + json=NEW_DEV_PROF_DICT, + ) + ] + ) attrs = { 'name': NEW_DEV_PROF_DICT['name'], - 'groups': NEW_DEV_PROF_DICT['groups'] + 'groups': NEW_DEV_PROF_DICT['groups'], } - self.assertTrue( - self.cloud.create_device_profile( - attrs - ) - ) + self.assertTrue(self.cloud.create_device_profile(attrs)) self.assert_calls() def test_delete_device_profile(self, filters=None): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'device_profiles', DEV_PROF_DICT['name']]), - json={"device_profiles": [DEV_PROF_DICT]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'device_profiles', DEV_PROF_DICT['name']]), - json=DEV_PROF_DICT) - - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'device_profiles', + DEV_PROF_DICT['name'], + ], + ), + json={"device_profiles": [DEV_PROF_DICT]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'device_profiles', + DEV_PROF_DICT['name'], + ], + ), + json=DEV_PROF_DICT, + ), + ] + ) self.assertTrue( - self.cloud.delete_device_profile( - DEV_PROF_DICT['name'], - filters - ) + self.cloud.delete_device_profile(DEV_PROF_DICT['name'], filters) ) self.assert_calls() def test_list_accelerator_requests(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests']), - json={'arqs': [ARQ_DICT]} - ), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=['v2', 'accelerator_requests'], + ), + json={'arqs': [ARQ_DICT]}, + ), + ] + ) arq_list = self.cloud.list_accelerator_requests() self.assertEqual(len(arq_list), 1) self.assertEqual(arq_list[0].uuid, ARQ_DICT['uuid']) - self.assertEqual(arq_list[0].device_profile_name, - ARQ_DICT['device_profile_name']) - self.assertEqual(arq_list[0].device_profile_group_id, - ARQ_DICT['device_profile_group_id']) - self.assertEqual(arq_list[0].device_rp_uuid, - ARQ_DICT['device_rp_uuid']) - self.assertEqual(arq_list[0].instance_uuid, - ARQ_DICT['instance_uuid']) - self.assertEqual(arq_list[0].attach_handle_type, - ARQ_DICT['attach_handle_type']) - self.assertEqual(arq_list[0].attach_handle_info, - ARQ_DICT['attach_handle_info']) - self.assert_calls() - - def test_create_accelerator_request(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests']), - json=NEW_ARQ_DICT - ), - ]) - - attrs = { - 'device_profile_name': NEW_ARQ_DICT['device_profile_name'], - 'device_profile_group_id': NEW_ARQ_DICT['device_profile_group_id'] - } - - self.assertTrue( - self.cloud.create_accelerator_request( - attrs - ) + self.assertEqual( + arq_list[0].device_profile_name, ARQ_DICT['device_profile_name'] + ) + self.assertEqual( + arq_list[0].device_profile_group_id, + ARQ_DICT['device_profile_group_id'], + ) + self.assertEqual( + arq_list[0].device_rp_uuid, ARQ_DICT['device_rp_uuid'] + ) + self.assertEqual(arq_list[0].instance_uuid, ARQ_DICT['instance_uuid']) + self.assertEqual( + arq_list[0].attach_handle_type, ARQ_DICT['attach_handle_type'] + ) + self.assertEqual( + arq_list[0].attach_handle_info, ARQ_DICT['attach_handle_info'] ) self.assert_calls() - def test_delete_accelerator_request(self, filters=None): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests', ARQ_DICT['uuid']]), - json={"accelerator_requests": [ARQ_DICT]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests', ARQ_DICT['uuid']]), - json=ARQ_DICT) + def test_create_accelerator_request(self): + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=['v2', 'accelerator_requests'], + ), + json=NEW_ARQ_DICT, + ), + ] + ) - ]) + attrs = { + 'device_profile_name': NEW_ARQ_DICT['device_profile_name'], + 'device_profile_group_id': NEW_ARQ_DICT['device_profile_group_id'], + } + + self.assertTrue(self.cloud.create_accelerator_request(attrs)) + self.assert_calls() + + def test_delete_accelerator_request(self, filters=None): + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'accelerator_requests', + ARQ_DICT['uuid'], + ], + ), + json={"accelerator_requests": [ARQ_DICT]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'accelerator_requests', + ARQ_DICT['uuid'], + ], + ), + json=ARQ_DICT, + ), + ] + ) self.assertTrue( - self.cloud.delete_accelerator_request( - ARQ_DICT['uuid'], - filters - ) + self.cloud.delete_accelerator_request(ARQ_DICT['uuid'], filters) ) self.assert_calls() def test_bind_accelerator_request(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests', ARQ_DICT['uuid']]), - json={"accelerator_requests": [ARQ_DICT]}), - dict(method='PATCH', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests', ARQ_DICT['uuid']]), - json=ARQ_DICT) - ]) - properties = [{'path': '/hostname', - 'value': ARQ_DICT['hostname'], - 'op': 'add'}, - {'path': '/instance_uuid', - 'value': ARQ_DICT['instance_uuid'], - 'op': 'add'}, - {'path': '/device_rp_uuid', - 'value': ARQ_DICT['device_rp_uuid'], - 'op': 'add'}] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'accelerator_requests', + ARQ_DICT['uuid'], + ], + ), + json={"accelerator_requests": [ARQ_DICT]}, + ), + dict( + method='PATCH', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'accelerator_requests', + ARQ_DICT['uuid'], + ], + ), + json=ARQ_DICT, + ), + ] + ) + properties = [ + {'path': '/hostname', 'value': ARQ_DICT['hostname'], 'op': 'add'}, + { + 'path': '/instance_uuid', + 'value': ARQ_DICT['instance_uuid'], + 'op': 'add', + }, + { + 'path': '/device_rp_uuid', + 'value': ARQ_DICT['device_rp_uuid'], + 'op': 'add', + }, + ] self.assertTrue( - self.cloud.bind_accelerator_request( - ARQ_DICT['uuid'], properties - ) + self.cloud.bind_accelerator_request(ARQ_DICT['uuid'], properties) ) self.assert_calls() def test_unbind_accelerator_request(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests', ARQ_DICT['uuid']]), - json={"accelerator_requests": [ARQ_DICT]}), - dict(method='PATCH', - uri=self.get_mock_url( - 'accelerator', - 'public', - append=['v2', 'accelerator_requests', ARQ_DICT['uuid']]), - json=ARQ_DICT) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'accelerator_requests', + ARQ_DICT['uuid'], + ], + ), + json={"accelerator_requests": [ARQ_DICT]}, + ), + dict( + method='PATCH', + uri=self.get_mock_url( + 'accelerator', + 'public', + append=[ + 'v2', + 'accelerator_requests', + ARQ_DICT['uuid'], + ], + ), + json=ARQ_DICT, + ), + ] + ) - properties = [{'path': '/hostname', - 'op': 'remove'}, - {'path': '/instance_uuid', - 'op': 'remove'}, - {'path': '/device_rp_uuid', - 'op': 'remove'}] + properties = [ + {'path': '/hostname', 'op': 'remove'}, + {'path': '/instance_uuid', 'op': 'remove'}, + {'path': '/device_rp_uuid', 'op': 'remove'}, + ] self.assertTrue( - self.cloud.unbind_accelerator_request( - ARQ_DICT['uuid'], properties - ) + self.cloud.unbind_accelerator_request(ARQ_DICT['uuid'], properties) ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_aggregate.py b/openstack/tests/unit/cloud/test_aggregate.py index deec61c12..d5c265513 100644 --- a/openstack/tests/unit/cloud/test_aggregate.py +++ b/openstack/tests/unit/cloud/test_aggregate.py @@ -15,7 +15,6 @@ from openstack.tests.unit import base class TestAggregate(base.TestCase): - def setUp(self): super(TestAggregate, self).setUp() self.aggregate_name = self.getUniqueString('aggregate') @@ -27,17 +26,25 @@ class TestAggregate(base.TestCase): del create_aggregate['metadata'] del create_aggregate['hosts'] - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates']), - json={'aggregate': create_aggregate}, - validate=dict(json={ - 'aggregate': { - 'name': self.aggregate_name, - 'availability_zone': None, - }})), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates'] + ), + json={'aggregate': create_aggregate}, + validate=dict( + json={ + 'aggregate': { + 'name': self.aggregate_name, + 'availability_zone': None, + } + } + ), + ), + ] + ) self.cloud.create_aggregate(name=self.aggregate_name) self.assert_calls() @@ -45,100 +52,144 @@ class TestAggregate(base.TestCase): def test_create_aggregate_with_az(self): availability_zone = 'az1' az_aggregate = fakes.make_fake_aggregate( - 1, self.aggregate_name, availability_zone=availability_zone) + 1, self.aggregate_name, availability_zone=availability_zone + ) create_aggregate = az_aggregate.copy() del create_aggregate['metadata'] del create_aggregate['hosts'] - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates']), - json={'aggregate': create_aggregate}, - validate=dict(json={ - 'aggregate': { - 'name': self.aggregate_name, - 'availability_zone': availability_zone, - }})), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates'] + ), + json={'aggregate': create_aggregate}, + validate=dict( + json={ + 'aggregate': { + 'name': self.aggregate_name, + 'availability_zone': availability_zone, + } + } + ), + ), + ] + ) self.cloud.create_aggregate( - name=self.aggregate_name, availability_zone=availability_zone) + name=self.aggregate_name, availability_zone=availability_zone + ) self.assert_calls() def test_delete_aggregate(self): - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1'])), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + ), + ] + ) self.assertTrue(self.cloud.delete_aggregate('1')) self.assert_calls() def test_delete_aggregate_by_name(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', - self.aggregate_name] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-aggregates', self.aggregate_name], + ), + status_code=404, ), - status_code=404, - ), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates']), - json={'aggregates': [self.fake_aggregate]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1'])), - ]) + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates'] + ), + json={'aggregates': [self.fake_aggregate]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + ), + ] + ) self.assertTrue(self.cloud.delete_aggregate(self.aggregate_name)) self.assert_calls() def test_update_aggregate_set_az(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json=self.fake_aggregate), - dict(method='PUT', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json={'aggregate': self.fake_aggregate}, - validate=dict( - json={ - 'aggregate': { - 'availability_zone': 'az', - }})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json=self.fake_aggregate, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json={'aggregate': self.fake_aggregate}, + validate=dict( + json={ + 'aggregate': { + 'availability_zone': 'az', + } + } + ), + ), + ] + ) self.cloud.update_aggregate(1, availability_zone='az') self.assert_calls() def test_update_aggregate_unset_az(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json=self.fake_aggregate), - dict(method='PUT', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json={'aggregate': self.fake_aggregate}, - validate=dict( - json={ - 'aggregate': { - 'availability_zone': None, - }})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json=self.fake_aggregate, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json={'aggregate': self.fake_aggregate}, + validate=dict( + json={ + 'aggregate': { + 'availability_zone': None, + } + } + ), + ), + ] + ) self.cloud.update_aggregate(1, availability_zone=None) @@ -146,57 +197,83 @@ class TestAggregate(base.TestCase): def test_set_aggregate_metadata(self): metadata = {'key': 'value'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json=self.fake_aggregate), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-aggregates', '1', 'action']), - json={'aggregate': self.fake_aggregate}, - validate=dict( - json={'set_metadata': {'metadata': metadata}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json=self.fake_aggregate, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-aggregates', '1', 'action'], + ), + json={'aggregate': self.fake_aggregate}, + validate=dict( + json={'set_metadata': {'metadata': metadata}} + ), + ), + ] + ) self.cloud.set_aggregate_metadata('1', metadata) self.assert_calls() def test_add_host_to_aggregate(self): hostname = 'host1' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json=self.fake_aggregate), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-aggregates', '1', 'action']), - json={'aggregate': self.fake_aggregate}, - validate=dict( - json={'add_host': {'host': hostname}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json=self.fake_aggregate, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-aggregates', '1', 'action'], + ), + json={'aggregate': self.fake_aggregate}, + validate=dict(json={'add_host': {'host': hostname}}), + ), + ] + ) self.cloud.add_host_to_aggregate('1', hostname) self.assert_calls() def test_remove_host_from_aggregate(self): hostname = 'host1' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-aggregates', '1']), - json=self.fake_aggregate), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-aggregates', '1', 'action']), - json={'aggregate': self.fake_aggregate}, - validate=dict( - json={'remove_host': {'host': hostname}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-aggregates', '1'] + ), + json=self.fake_aggregate, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-aggregates', '1', 'action'], + ), + json={'aggregate': self.fake_aggregate}, + validate=dict(json={'remove_host': {'host': hostname}}), + ), + ] + ) self.cloud.remove_host_from_aggregate('1', hostname) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_availability_zones.py b/openstack/tests/unit/cloud/test_availability_zones.py index 05a25bd60..68d67a64b 100644 --- a/openstack/tests/unit/cloud/test_availability_zones.py +++ b/openstack/tests/unit/cloud/test_availability_zones.py @@ -17,62 +17,63 @@ from openstack.tests.unit import base _fake_zone_list = { "availabilityZoneInfo": [ - { - "hosts": None, - "zoneName": "az1", - "zoneState": { - "available": True - } - }, - { - "hosts": None, - "zoneName": "nova", - "zoneState": { - "available": False - } - } + {"hosts": None, "zoneName": "az1", "zoneState": {"available": True}}, + {"hosts": None, "zoneName": "nova", "zoneState": {"available": False}}, ] } class TestAvailabilityZoneNames(base.TestCase): - def test_list_availability_zone_names(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-availability-zone'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=_fake_zone_list), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-availability-zone'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=_fake_zone_list, + ), + ] + ) - self.assertEqual( - ['az1'], self.cloud.list_availability_zone_names()) + self.assertEqual(['az1'], self.cloud.list_availability_zone_names()) self.assert_calls() def test_unauthorized_availability_zone_names(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-availability-zone'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - status_code=403), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-availability-zone'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + status_code=403, + ), + ] + ) - self.assertEqual( - [], self.cloud.list_availability_zone_names()) + self.assertEqual([], self.cloud.list_availability_zone_names()) self.assert_calls() def test_list_all_availability_zone_names(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-availability-zone'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=_fake_zone_list), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-availability-zone'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=_fake_zone_list, + ), + ] + ) self.assertEqual( ['az1', 'nova'], - self.cloud.list_availability_zone_names(unavailable=True)) + self.cloud.list_availability_zone_names(unavailable=True), + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_baremetal_node.py b/openstack/tests/unit/cloud/test_baremetal_node.py index 888258bef..71d9a9027 100644 --- a/openstack/tests/unit/cloud/test_baremetal_node.py +++ b/openstack/tests/unit/cloud/test_baremetal_node.py @@ -29,26 +29,31 @@ from openstack.tests.unit import base class TestBaremetalNode(base.IronicTestCase): - def setUp(self): super(TestBaremetalNode, self).setUp() self.fake_baremetal_node = fakes.make_fake_machine( - self.name, self.uuid) + self.name, self.uuid + ) # TODO(TheJulia): Some tests below have fake ports, # since they are required in some processes. Lets refactor # them at some point to use self.fake_baremetal_port. self.fake_baremetal_port = fakes.make_fake_port( - '00:01:02:03:04:05', - node_id=self.uuid) + '00:01:02:03:04:05', node_id=self.uuid + ) def test_list_machines(self): fake_baremetal_two = fakes.make_fake_machine('two', str(uuid.uuid4())) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(resource='nodes'), - json={'nodes': [self.fake_baremetal_node, - fake_baremetal_two]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='nodes'), + json={ + 'nodes': [self.fake_baremetal_node, fake_baremetal_two] + }, + ), + ] + ) machines = self.cloud.list_machines() self.assertEqual(2, len(machines)) @@ -57,40 +62,54 @@ class TestBaremetalNode(base.IronicTestCase): self.assert_calls() def test_get_machine(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) machine = self.cloud.get_machine(self.fake_baremetal_node['uuid']) - self.assertEqual(machine['uuid'], - self.fake_baremetal_node['uuid']) + self.assertEqual(machine['uuid'], self.fake_baremetal_node['uuid']) self.assert_calls() def test_get_machine_by_mac(self): mac_address = '00:01:02:03:04:05' node_uuid = self.fake_baremetal_node['uuid'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='ports', - append=['detail'], - qs_elements=['address=%s' % mac_address]), - json={'ports': [{'address': mac_address, - 'node_uuid': node_uuid}]}), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + append=['detail'], + qs_elements=['address=%s' % mac_address], + ), + json={ + 'ports': [ + {'address': mac_address, 'node_uuid': node_uuid} + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) machine = self.cloud.get_machine_by_mac(mac_address) - self.assertEqual(machine['uuid'], - self.fake_baremetal_node['uuid']) + self.assertEqual(machine['uuid'], self.fake_baremetal_node['uuid']) self.assert_calls() def test_validate_machine(self): @@ -112,15 +131,20 @@ class TestBaremetalNode(base.IronicTestCase): }, 'foo': { 'result': False, - }} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'validate']), - json=validate_return), - ]) + }, + } + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid'], 'validate'], + ), + json=validate_return, + ), + ] + ) self.cloud.validate_machine(self.fake_baremetal_node['uuid']) self.assert_calls() @@ -136,17 +160,23 @@ class TestBaremetalNode(base.IronicTestCase): }, 'foo': { 'result': False, - }} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'validate']), - json=validate_return), - ]) - self.cloud.validate_machine(self.fake_baremetal_node['uuid'], - for_deploy=False) + }, + } + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid'], 'validate'], + ), + json=validate_return, + ), + ] + ) + self.cloud.validate_machine( + self.fake_baremetal_node['uuid'], for_deploy=False + ) self.assert_calls() @@ -160,15 +190,20 @@ class TestBaremetalNode(base.IronicTestCase): }, 'foo': { 'result': False, - }} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'validate']), - json=validate_return), - ]) + }, + } + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid'], 'validate'], + ), + json=validate_return, + ), + ] + ) self.cloud.validate_node(self.fake_baremetal_node['uuid']) self.assert_calls() @@ -183,114 +218,139 @@ class TestBaremetalNode(base.IronicTestCase): 'result': True, 'reason': None, }, - 'foo': { - 'result': True - }} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'validate']), - json=validate_return), - ]) + 'foo': {'result': True}, + } + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid'], 'validate'], + ), + json=validate_return, + ), + ] + ) self.assertRaises( exceptions.ValidationException, self.cloud.validate_machine, - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'], + ) self.assert_calls() def test_patch_machine(self): - test_patch = [{ - 'op': 'remove', - 'path': '/instance_info'}] + test_patch = [{'op': 'remove', 'path': '/instance_info'}] self.fake_baremetal_node['instance_info'] = {} - self.register_uris([ - dict(method='PATCH', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node, - validate=dict(json=test_patch)), - ]) + self.register_uris( + [ + dict( + method='PATCH', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + validate=dict(json=test_patch), + ), + ] + ) result = self.cloud.patch_machine( - self.fake_baremetal_node['uuid'], test_patch) + self.fake_baremetal_node['uuid'], test_patch + ) self.assertEqual(self.fake_baremetal_node['uuid'], result['uuid']) self.assert_calls() def test_set_node_instance_info(self): - test_patch = [{ - 'op': 'add', - 'path': '/foo', - 'value': 'bar'}] - self.register_uris([ - dict(method='PATCH', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node, - validate=dict(json=test_patch)), - ]) + test_patch = [{'op': 'add', 'path': '/foo', 'value': 'bar'}] + self.register_uris( + [ + dict( + method='PATCH', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + validate=dict(json=test_patch), + ), + ] + ) self.cloud.set_node_instance_info( - self.fake_baremetal_node['uuid'], test_patch) + self.fake_baremetal_node['uuid'], test_patch + ) self.assert_calls() def test_purge_node_instance_info(self): - test_patch = [{ - 'op': 'remove', - 'path': '/instance_info'}] + test_patch = [{'op': 'remove', 'path': '/instance_info'}] self.fake_baremetal_node['instance_info'] = {} - self.register_uris([ - dict(method='PATCH', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node, - validate=dict(json=test_patch)), - ]) - self.cloud.purge_node_instance_info( - self.fake_baremetal_node['uuid']) + self.register_uris( + [ + dict( + method='PATCH', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + validate=dict(json=test_patch), + ), + ] + ) + self.cloud.purge_node_instance_info(self.fake_baremetal_node['uuid']) self.assert_calls() def test_inspect_machine_fail_active(self): self.fake_baremetal_node['provision_state'] = 'active' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.inspect_machine, self.fake_baremetal_node['uuid'], wait=True, - timeout=1) + timeout=1, + ) self.assert_calls() def test_inspect_machine_fail_associated(self): self.fake_baremetal_node['provision_state'] = 'available' self.fake_baremetal_node['instance_uuid'] = '1234' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.assertRaisesRegex( exc.OpenStackCloudException, 'associated with an instance', self.cloud.inspect_machine, self.fake_baremetal_node['uuid'], wait=True, - timeout=1) + timeout=1, + ) self.assert_calls() @@ -301,33 +361,46 @@ class TestBaremetalNode(base.IronicTestCase): inspecting_node['provision_state'] = 'inspecting' finished_node = self.fake_baremetal_node.copy() finished_node['provision_state'] = 'manageable' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'inspect'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspecting_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=finished_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'inspect'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspecting_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=finished_node, + ), + ] + ) self.cloud.inspect_machine(self.fake_baremetal_node['uuid']) @@ -337,33 +410,46 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['provision_state'] = 'manageable' inspecting_node = self.fake_baremetal_node.copy() inspecting_node['provision_state'] = 'inspecting' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'inspect'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspecting_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'inspect'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspecting_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.cloud.inspect_machine(self.fake_baremetal_node['uuid']) self.assert_calls() @@ -374,53 +460,78 @@ class TestBaremetalNode(base.IronicTestCase): manageable_node = self.fake_baremetal_node.copy() manageable_node['provision_state'] = 'manageable' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'inspect'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'provide'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'inspect'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'provide'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + ] + ) self.cloud.inspect_machine(self.fake_baremetal_node['uuid']) self.assert_calls() @@ -433,67 +544,97 @@ class TestBaremetalNode(base.IronicTestCase): inspecting_node = self.fake_baremetal_node.copy() inspecting_node['provision_state'] = 'inspecting' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'inspect'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspecting_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'provide'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'inspect'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspecting_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'provide'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + ] + ) self.cloud.inspect_machine( - self.fake_baremetal_node['uuid'], wait=True, timeout=1) + self.fake_baremetal_node['uuid'], wait=True, timeout=1 + ) self.assert_calls() @@ -501,41 +642,57 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['provision_state'] = 'manageable' inspecting_node = self.fake_baremetal_node.copy() inspecting_node['provision_state'] = 'inspecting' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'inspect'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspecting_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspecting_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'inspect'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspecting_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspecting_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.cloud.inspect_machine( - self.fake_baremetal_node['uuid'], wait=True, timeout=1) + self.fake_baremetal_node['uuid'], wait=True, timeout=1 + ) self.assert_calls() @@ -546,113 +703,164 @@ class TestBaremetalNode(base.IronicTestCase): inspect_fail_node = self.fake_baremetal_node.copy() inspect_fail_node['provision_state'] = 'inspect failed' inspect_fail_node['last_error'] = 'Earth Imploded' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'inspect'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspecting_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=inspect_fail_node), - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.inspect_machine, - self.fake_baremetal_node['uuid'], - wait=True, timeout=1) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'inspect'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspecting_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=inspect_fail_node, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.inspect_machine, + self.fake_baremetal_node['uuid'], + wait=True, + timeout=1, + ) self.assert_calls() def test_set_machine_maintenace_state(self): - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'maintenance']), - validate=dict(json={'reason': 'no reason'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'maintenance', + ], + ), + validate=dict(json={'reason': 'no reason'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.cloud.set_machine_maintenance_state( - self.fake_baremetal_node['uuid'], True, reason='no reason') + self.fake_baremetal_node['uuid'], True, reason='no reason' + ) self.assert_calls() def test_set_machine_maintenace_state_false(self): - self.register_uris([ - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'maintenance'])), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'maintenance', + ], + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.cloud.set_machine_maintenance_state( - self.fake_baremetal_node['uuid'], False) + self.fake_baremetal_node['uuid'], False + ) self.assert_calls def test_remove_machine_from_maintenance(self): - self.register_uris([ - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'maintenance'])), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'maintenance', + ], + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.cloud.remove_machine_from_maintenance( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assert_calls() def test_set_machine_power_on(self): - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - validate=dict(json={'target': 'power on'})), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + validate=dict(json={'target': 'power on'}), + ), + ] + ) return_value = self.cloud.set_machine_power_on( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assertIsNone(return_value) self.assert_calls() @@ -660,84 +868,127 @@ class TestBaremetalNode(base.IronicTestCase): def test_set_machine_power_on_with_retires(self): # NOTE(TheJulia): This logic ends up testing power on/off and reboot # as they all utilize the same helper method. - self.register_uris([ - dict( - method='PUT', - status_code=503, - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - validate=dict(json={'target': 'power on'})), - dict( - method='PUT', - status_code=409, - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - validate=dict(json={'target': 'power on'})), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - validate=dict(json={'target': 'power on'})), - ]) + self.register_uris( + [ + dict( + method='PUT', + status_code=503, + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + validate=dict(json={'target': 'power on'}), + ), + dict( + method='PUT', + status_code=409, + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + validate=dict(json={'target': 'power on'}), + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + validate=dict(json={'target': 'power on'}), + ), + ] + ) return_value = self.cloud.set_machine_power_on( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assertIsNone(return_value) self.assert_calls() def test_set_machine_power_off(self): - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - validate=dict(json={'target': 'power off'})), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + validate=dict(json={'target': 'power off'}), + ), + ] + ) return_value = self.cloud.set_machine_power_off( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assertIsNone(return_value) self.assert_calls() def test_set_machine_power_reboot(self): - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - validate=dict(json={'target': 'rebooting'})), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + validate=dict(json={'target': 'rebooting'}), + ), + ] + ) return_value = self.cloud.set_machine_power_reboot( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assertIsNone(return_value) self.assert_calls() def test_set_machine_power_reboot_failure(self): - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'power']), - status_code=400, - json={'error': 'invalid'}, - validate=dict(json={'target': 'rebooting'})), - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.set_machine_power_reboot, - self.fake_baremetal_node['uuid']) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'power', + ], + ), + status_code=400, + json={'error': 'invalid'}, + validate=dict(json={'target': 'rebooting'}), + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.set_machine_power_reboot, + self.fake_baremetal_node['uuid'], + ) self.assert_calls() @@ -746,25 +997,40 @@ class TestBaremetalNode(base.IronicTestCase): deploy_node['provision_state'] = 'deploying' active_node = self.fake_baremetal_node.copy() active_node['provision_state'] = 'active' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active', - 'configdrive': 'http://host/file'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict( + json={ + 'target': 'active', + 'configdrive': 'http://host/file', + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) result = self.cloud.node_set_provision_state( self.fake_baremetal_node['uuid'], 'active', - configdrive='http://host/file') + configdrive='http://host/file', + ) self.assertEqual(self.fake_baremetal_node['uuid'], result['uuid']) self.assert_calls() @@ -774,43 +1040,76 @@ class TestBaremetalNode(base.IronicTestCase): deploy_node['provision_state'] = 'deploying' active_node = self.fake_baremetal_node.copy() active_node['provision_state'] = 'active' - self.register_uris([ - dict( - method='PUT', - status_code=409, - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active', - 'configdrive': 'http://host/file'})), - dict( - method='PUT', - status_code=503, - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active', - 'configdrive': 'http://host/file'})), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active', - 'configdrive': 'http://host/file'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + status_code=409, + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict( + json={ + 'target': 'active', + 'configdrive': 'http://host/file', + } + ), + ), + dict( + method='PUT', + status_code=503, + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict( + json={ + 'target': 'active', + 'configdrive': 'http://host/file', + } + ), + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict( + json={ + 'target': 'active', + 'configdrive': 'http://host/file', + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.cloud.node_set_provision_state( self.fake_baremetal_node['uuid'], 'active', - configdrive='http://host/file') + configdrive='http://host/file', + ) self.assert_calls() @@ -820,34 +1119,49 @@ class TestBaremetalNode(base.IronicTestCase): active_node = self.fake_baremetal_node.copy() active_node['provision_state'] = 'active' self.fake_baremetal_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=deploy_node), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=active_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'active'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=deploy_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=active_node, + ), + ] + ) return_value = self.cloud.node_set_provision_state( - self.fake_baremetal_node['uuid'], - 'active', - wait=True) + self.fake_baremetal_node['uuid'], 'active', wait=True + ) self.assertSubdict(active_node, return_value) self.assert_calls() @@ -855,20 +1169,30 @@ class TestBaremetalNode(base.IronicTestCase): def test_node_set_provision_state_wait_timeout_fails(self): # Intentionally time out. self.fake_baremetal_node['provision_state'] = 'deploy wait' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'active'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, @@ -876,51 +1200,71 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['uuid'], 'active', wait=True, - timeout=0.001) + timeout=0.001, + ) self.assert_calls() def test_node_set_provision_state_wait_success(self): self.fake_baremetal_node['provision_state'] = 'active' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'active'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) return_value = self.cloud.node_set_provision_state( - self.fake_baremetal_node['uuid'], - 'active', - wait=True) + self.fake_baremetal_node['uuid'], 'active', wait=True + ) self.assertSubdict(self.fake_baremetal_node, return_value) self.assert_calls() def test_node_set_provision_state_wait_failure_cases(self): self.fake_baremetal_node['provision_state'] = 'foo failed' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'active'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, @@ -928,7 +1272,8 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['uuid'], 'active', wait=True, - timeout=300) + timeout=300, + ) self.assert_calls() @@ -936,29 +1281,41 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['provision_state'] = 'manageable' available_node = self.fake_baremetal_node.copy() available_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'provide'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'provide'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + ] + ) return_value = self.cloud.node_set_provision_state( - self.fake_baremetal_node['uuid'], - 'provide', - wait=True) + self.fake_baremetal_node['uuid'], 'provide', wait=True + ) self.assertSubdict(available_node, return_value) self.assert_calls() @@ -967,22 +1324,31 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['reservation'] = 'conductor0' unlocked_node = self.fake_baremetal_node.copy() unlocked_node['reservation'] = None - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=unlocked_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=unlocked_node, + ), + ] + ) self.assertIsNone( self.cloud.wait_for_baremetal_node_lock( - self.fake_baremetal_node, - timeout=1)) + self.fake_baremetal_node, timeout=1 + ) + ) self.assert_calls() @@ -990,73 +1356,105 @@ class TestBaremetalNode(base.IronicTestCase): self.fake_baremetal_node['reservation'] = None self.assertIsNone( self.cloud.wait_for_baremetal_node_lock( - self.fake_baremetal_node, - timeout=1)) + self.fake_baremetal_node, timeout=1 + ) + ) # NOTE(dtantsur): service discovery apparently requires 3 calls self.assertEqual(3, len(self.adapter.request_history)) def test_wait_for_baremetal_node_lock_timeout(self): self.fake_baremetal_node['reservation'] = 'conductor0' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.wait_for_baremetal_node_lock, self.fake_baremetal_node, - timeout=0.001) + timeout=0.001, + ) self.assert_calls() def test_activate_node(self): self.fake_baremetal_node['provision_state'] = 'active' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'active', - 'configdrive': 'http://host/file'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict( + json={ + 'target': 'active', + 'configdrive': 'http://host/file', + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) return_value = self.cloud.activate_node( self.fake_baremetal_node['uuid'], configdrive='http://host/file', - wait=True) + wait=True, + ) self.assertIsNone(return_value) self.assert_calls() def test_deactivate_node(self): self.fake_baremetal_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'deleted'})), - dict(method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'deleted'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) return_value = self.cloud.deactivate_node( - self.fake_baremetal_node['uuid'], - wait=True) + self.fake_baremetal_node['uuid'], wait=True + ) self.assertIsNone(return_value) self.assert_calls() @@ -1073,25 +1471,29 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'available' if 'provision_state' in node_to_post: node_to_post.pop('provision_state') - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - json=self.fake_baremetal_node, - validate=dict(json=node_to_post)), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid}), - json=self.fake_baremetal_port), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + json=self.fake_baremetal_node, + validate=dict(json=node_to_post), + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + validate=dict( + json={'address': mac_address, 'node_uuid': node_uuid} + ), + json=self.fake_baremetal_port, + ), + ] + ) return_value = self.cloud.register_machine(nics, **node_to_post) self.assertEqual(self.uuid, return_value.id) @@ -1112,54 +1514,75 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'enroll' manageable_node = self.fake_baremetal_node.copy() manageable_node['provision_state'] = 'manageable' available_node = self.fake_baremetal_node.copy() available_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - validate=dict(json=node_to_post), - json=self.fake_baremetal_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid, - 'pxe_enabled': False}), - json=self.fake_baremetal_port), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'provide'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + validate=dict(json=node_to_post), + json=self.fake_baremetal_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + validate=dict( + json={ + 'address': mac_address, + 'node_uuid': node_uuid, + 'pxe_enabled': False, + } + ), + json=self.fake_baremetal_port, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'provide'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + ] + ) return_value = self.cloud.register_machine(nics, **node_to_post) self.assertSubdict(available_node, return_value) @@ -1175,67 +1598,90 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'enroll' manageable_node = self.fake_baremetal_node.copy() manageable_node['provision_state'] = 'manageable' available_node = self.fake_baremetal_node.copy() available_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - validate=dict(json=node_to_post), - json=self.fake_baremetal_node), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid}), - json=self.fake_baremetal_port), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'provide'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=available_node), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + validate=dict(json=node_to_post), + json=self.fake_baremetal_node, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + validate=dict( + json={'address': mac_address, 'node_uuid': node_uuid} + ), + json=self.fake_baremetal_port, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'provide'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=available_node, + ), + ] + ) return_value = self.cloud.register_machine( - nics, wait=True, **node_to_post) + nics, wait=True, **node_to_post + ) self.assertSubdict(available_node, return_value) self.assert_calls() @@ -1250,44 +1696,57 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'enroll' failed_node = self.fake_baremetal_node.copy() failed_node['reservation'] = 'conductor0' failed_node['provision_state'] = 'enroll' failed_node['last_error'] = 'kaboom!' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - json=self.fake_baremetal_node, - validate=dict(json=node_to_post)), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=failed_node), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + json=self.fake_baremetal_node, + validate=dict(json=node_to_post), + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=failed_node, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.register_machine, nics, - **node_to_post) + **node_to_post + ) self.assert_calls() def test_register_machine_enroll_timeout(self): @@ -1300,37 +1759,49 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'enroll' busy_node = self.fake_baremetal_node.copy() busy_node['reservation'] = 'conductor0' busy_node['provision_state'] = 'verifying' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - json=self.fake_baremetal_node, - validate=dict(json=node_to_post)), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=busy_node), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + json=self.fake_baremetal_node, + validate=dict(json=node_to_post), + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=busy_node, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) # NOTE(TheJulia): This test shortcircuits the timeout loop # such that it executes only once. The very last returned # state to the API is essentially a busy state that we @@ -1341,7 +1812,8 @@ class TestBaremetalNode(base.IronicTestCase): nics, timeout=0.001, lock_timeout=0.001, - **node_to_post) + **node_to_post + ) self.assert_calls() def test_register_machine_enroll_timeout_wait(self): @@ -1354,63 +1826,84 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'enroll' manageable_node = self.fake_baremetal_node.copy() manageable_node['provision_state'] = 'manageable' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - json=self.fake_baremetal_node, - validate=dict(json=node_to_post)), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'manage'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=manageable_node), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid}), - json=self.fake_baremetal_port), - dict( - method='PUT', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], - 'states', 'provision']), - validate=dict(json={'target': 'provide'})), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + json=self.fake_baremetal_node, + validate=dict(json=node_to_post), + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'manage'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=manageable_node, + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + validate=dict( + json={'address': mac_address, 'node_uuid': node_uuid} + ), + json=self.fake_baremetal_port, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'states', + 'provision', + ], + ), + validate=dict(json={'target': 'provide'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.register_machine, nics, wait=True, timeout=0.001, - **node_to_post) + **node_to_post + ) self.assert_calls() def test_register_machine_port_create_failed(self): @@ -1423,33 +1916,42 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - json=self.fake_baremetal_node, - validate=dict(json=node_to_post)), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - status_code=400, - json={'error': 'no ports for you'}, - validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid})), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) - self.assertRaisesRegex(exc.OpenStackCloudException, - 'no ports for you', - self.cloud.register_machine, - nics, **node_to_post) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + json=self.fake_baremetal_node, + validate=dict(json=node_to_post), + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + status_code=400, + json={'error': 'no ports for you'}, + validate=dict( + json={'address': mac_address, 'node_uuid': node_uuid} + ), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) + self.assertRaisesRegex( + exc.OpenStackCloudException, + 'no ports for you', + self.cloud.register_machine, + nics, + **node_to_post + ) self.assert_calls() @@ -1465,45 +1967,57 @@ class TestBaremetalNode(base.IronicTestCase): 'driver_info': None, 'name': self.fake_baremetal_node['name'], 'properties': None, - 'uuid': node_uuid} + 'uuid': node_uuid, + } self.fake_baremetal_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes'), - json=self.fake_baremetal_node, - validate=dict(json=node_to_post)), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid}), - json=self.fake_baremetal_port), - dict( - method='POST', - uri=self.get_mock_url( - resource='ports'), - status_code=400, - json={'error': 'no ports for you'}, - validate=dict(json={'address': mac_address2, - 'node_uuid': node_uuid})), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='ports', - append=[self.fake_baremetal_port['uuid']])), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) - self.assertRaisesRegex(exc.OpenStackCloudException, - 'no ports for you', - self.cloud.register_machine, - nics, **node_to_post) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='nodes'), + json=self.fake_baremetal_node, + validate=dict(json=node_to_post), + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + validate=dict( + json={'address': mac_address, 'node_uuid': node_uuid} + ), + json=self.fake_baremetal_port, + ), + dict( + method='POST', + uri=self.get_mock_url(resource='ports'), + status_code=400, + json={'error': 'no ports for you'}, + validate=dict( + json={'address': mac_address2, 'node_uuid': node_uuid} + ), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='ports', + append=[self.fake_baremetal_port['uuid']], + ), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) + self.assertRaisesRegex( + exc.OpenStackCloudException, + 'no ports for you', + self.cloud.register_machine, + nics, + **node_to_post + ) self.assert_calls() @@ -1514,35 +2028,50 @@ class TestBaremetalNode(base.IronicTestCase): # NOTE(TheJulia): The two values below should be the same. port_node_uuid = self.fake_baremetal_port['node_uuid'] self.fake_baremetal_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='ports', - qs_elements=['address=%s' % mac_address]), - json={'ports': [{'address': mac_address, - 'node_uuid': port_node_uuid, - 'uuid': port_uuid}]}), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='ports', - append=[self.fake_baremetal_port['uuid']])), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + qs_elements=['address=%s' % mac_address], + ), + json={ + 'ports': [ + { + 'address': mac_address, + 'node_uuid': port_node_uuid, + 'uuid': port_uuid, + } + ] + }, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='ports', + append=[self.fake_baremetal_port['uuid']], + ), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) - self.cloud.unregister_machine( - nics, self.fake_baremetal_node['uuid']) + self.cloud.unregister_machine(nics, self.fake_baremetal_node['uuid']) self.assert_calls() @@ -1551,26 +2080,33 @@ class TestBaremetalNode(base.IronicTestCase): nics = [{'mac': mac_address}] self.fake_baremetal_node['provision_state'] = 'available' self.fake_baremetal_node['reservation'] = 'conductor99' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.unregister_machine, nics, self.fake_baremetal_node['uuid'], - timeout=0.001) + timeout=0.001, + ) self.assert_calls() def test_unregister_machine_retries(self): @@ -1580,53 +2116,74 @@ class TestBaremetalNode(base.IronicTestCase): # NOTE(TheJulia): The two values below should be the same. port_node_uuid = self.fake_baremetal_port['node_uuid'] self.fake_baremetal_node['provision_state'] = 'available' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='ports', - qs_elements=['address=%s' % mac_address]), - json={'ports': [{'address': mac_address, - 'node_uuid': port_node_uuid, - 'uuid': port_uuid}]}), - dict( - method='DELETE', - status_code=503, - uri=self.get_mock_url( - resource='ports', - append=[self.fake_baremetal_port['uuid']])), - dict( - method='DELETE', - status_code=409, - uri=self.get_mock_url( - resource='ports', - append=[self.fake_baremetal_port['uuid']])), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='ports', - append=[self.fake_baremetal_port['uuid']])), - dict( - method='DELETE', - status_code=409, - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']])), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + qs_elements=['address=%s' % mac_address], + ), + json={ + 'ports': [ + { + 'address': mac_address, + 'node_uuid': port_node_uuid, + 'uuid': port_uuid, + } + ] + }, + ), + dict( + method='DELETE', + status_code=503, + uri=self.get_mock_url( + resource='ports', + append=[self.fake_baremetal_port['uuid']], + ), + ), + dict( + method='DELETE', + status_code=409, + uri=self.get_mock_url( + resource='ports', + append=[self.fake_baremetal_port['uuid']], + ), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='ports', + append=[self.fake_baremetal_port['uuid']], + ), + ), + dict( + method='DELETE', + status_code=409, + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + ), + ] + ) - self.cloud.unregister_machine( - nics, self.fake_baremetal_node['uuid']) + self.cloud.unregister_machine(nics, self.fake_baremetal_node['uuid']) self.assert_calls() @@ -1644,8 +2201,11 @@ class TestBaremetalNode(base.IronicTestCase): method='GET', uri=self.get_mock_url( resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node)) + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ) + ) self.register_uris(url_list) @@ -1654,21 +2214,28 @@ class TestBaremetalNode(base.IronicTestCase): exc.OpenStackCloudException, self.cloud.unregister_machine, nics, - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'], + ) self.assert_calls() def test_update_machine_patch_no_action(self): - self.register_uris([dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + ] + ) # NOTE(TheJulia): This is just testing mechanics. update_dict = self.cloud.update_machine( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assertIsNone(update_dict['changes']) self.assertSubdict(self.fake_baremetal_node, update_dict['node']) @@ -1676,90 +2243,121 @@ class TestBaremetalNode(base.IronicTestCase): def test_attach_port_to_machine(self): vif_id = '953ccbee-e854-450f-95fe-fe5e40d611ec' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - service_type='network', - resource='ports', - base_url_append='v2.0', - append=[vif_id]), - json={'id': vif_id}), - dict( - method='POST', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], 'vifs'])), - ]) - self.cloud.attach_port_to_machine(self.fake_baremetal_node['uuid'], - vif_id) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + service_type='network', + resource='ports', + base_url_append='v2.0', + append=[vif_id], + ), + json={'id': vif_id}, + ), + dict( + method='POST', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid'], 'vifs'], + ), + ), + ] + ) + self.cloud.attach_port_to_machine( + self.fake_baremetal_node['uuid'], vif_id + ) self.assert_calls() def test_detach_port_from_machine(self): vif_id = '953ccbee-e854-450f-95fe-fe5e40d611ec' - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - service_type='network', - resource='ports', - base_url_append='v2.0', - append=[vif_id]), - json={'id': vif_id}), - dict( - method='DELETE', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], 'vifs', - vif_id])), - ]) - self.cloud.detach_port_from_machine(self.fake_baremetal_node['uuid'], - vif_id) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + service_type='network', + resource='ports', + base_url_append='v2.0', + append=[vif_id], + ), + json={'id': vif_id}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='nodes', + append=[ + self.fake_baremetal_node['uuid'], + 'vifs', + vif_id, + ], + ), + ), + ] + ) + self.cloud.detach_port_from_machine( + self.fake_baremetal_node['uuid'], vif_id + ) self.assert_calls() def test_list_ports_attached_to_machine(self): vif_id = '953ccbee-e854-450f-95fe-fe5e40d611ec' fake_port = {'id': vif_id, 'name': 'test'} - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), - dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid'], 'vifs']), - json={'vifs': [{'id': vif_id}]}), - dict( - method='GET', - uri=self.get_mock_url( - service_type='network', - resource='ports', - base_url_append='v2.0', - append=[vif_id]), - json=fake_port), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid']], + ), + json=self.fake_baremetal_node, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', + append=[self.fake_baremetal_node['uuid'], 'vifs'], + ), + json={'vifs': [{'id': vif_id}]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + service_type='network', + resource='ports', + base_url_append='v2.0', + append=[vif_id], + ), + json=fake_port, + ), + ] + ) res = self.cloud.list_ports_attached_to_machine( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assert_calls() self.assertEqual( [_port.Port(**fake_port).to_dict(computed=False)], - [i.to_dict(computed=False) for i in res]) + [i.to_dict(computed=False) for i in res], + ) class TestUpdateMachinePatch(base.IronicTestCase): @@ -1772,7 +2370,8 @@ class TestUpdateMachinePatch(base.IronicTestCase): def setUp(self): super(TestUpdateMachinePatch, self).setUp() self.fake_baremetal_node = fakes.make_fake_machine( - self.name, self.uuid) + self.name, self.uuid + ) def test_update_machine_patch(self): # The model has evolved over time, create the field if @@ -1782,32 +2381,41 @@ class TestUpdateMachinePatch(base.IronicTestCase): value_to_send = self.fake_baremetal_node[self.field_name] if self.changed: value_to_send = self.new_value - uris = [dict( - method='GET', - uri=self.get_mock_url( - resource='nodes', - append=[self.fake_baremetal_node['uuid']]), - json=self.fake_baremetal_node), + uris = [ + dict( + method='GET', + uri=self.get_mock_url( + resource='nodes', append=[self.fake_baremetal_node['uuid']] + ), + json=self.fake_baremetal_node, + ), ] if self.changed: - test_patch = [{ - 'op': 'replace', - 'path': '/' + self.field_name, - 'value': value_to_send}] + test_patch = [ + { + 'op': 'replace', + 'path': '/' + self.field_name, + 'value': value_to_send, + } + ] uris.append( dict( method='PATCH', uri=self.get_mock_url( resource='nodes', - append=[self.fake_baremetal_node['uuid']]), + append=[self.fake_baremetal_node['uuid']], + ), json=self.fake_baremetal_node, - validate=dict(json=test_patch))) + validate=dict(json=test_patch), + ) + ) self.register_uris(uris) call_args = {self.field_name: value_to_send} update_dict = self.cloud.update_machine( - self.fake_baremetal_node['uuid'], **call_args) + self.fake_baremetal_node['uuid'], **call_args + ) if self.changed: self.assertEqual(['/' + self.field_name], update_dict['changes']) @@ -1819,27 +2427,50 @@ class TestUpdateMachinePatch(base.IronicTestCase): scenarios = [ ('chassis_uuid', dict(field_name='chassis_uuid', changed=False)), - ('chassis_uuid_changed', - dict(field_name='chassis_uuid', changed=True, - new_value='meow')), + ( + 'chassis_uuid_changed', + dict(field_name='chassis_uuid', changed=True, new_value='meow'), + ), ('driver', dict(field_name='driver', changed=False)), - ('driver_changed', dict(field_name='driver', changed=True, - new_value='meow')), + ( + 'driver_changed', + dict(field_name='driver', changed=True, new_value='meow'), + ), ('driver_info', dict(field_name='driver_info', changed=False)), - ('driver_info_changed', dict(field_name='driver_info', changed=True, - new_value={'cat': 'meow'})), + ( + 'driver_info_changed', + dict( + field_name='driver_info', + changed=True, + new_value={'cat': 'meow'}, + ), + ), ('instance_info', dict(field_name='instance_info', changed=False)), - ('instance_info_changed', - dict(field_name='instance_info', changed=True, - new_value={'cat': 'meow'})), + ( + 'instance_info_changed', + dict( + field_name='instance_info', + changed=True, + new_value={'cat': 'meow'}, + ), + ), ('instance_uuid', dict(field_name='instance_uuid', changed=False)), - ('instance_uuid_changed', - dict(field_name='instance_uuid', changed=True, - new_value='meow')), + ( + 'instance_uuid_changed', + dict(field_name='instance_uuid', changed=True, new_value='meow'), + ), ('name', dict(field_name='name', changed=False)), - ('name_changed', dict(field_name='name', changed=True, - new_value='meow')), + ( + 'name_changed', + dict(field_name='name', changed=True, new_value='meow'), + ), ('properties', dict(field_name='properties', changed=False)), - ('properties_changed', dict(field_name='properties', changed=True, - new_value={'cat': 'meow'})) + ( + 'properties_changed', + dict( + field_name='properties', + changed=True, + new_value={'cat': 'meow'}, + ), + ), ] diff --git a/openstack/tests/unit/cloud/test_baremetal_ports.py b/openstack/tests/unit/cloud/test_baremetal_ports.py index 4ff9b63ef..cd2a2152b 100644 --- a/openstack/tests/unit/cloud/test_baremetal_ports.py +++ b/openstack/tests/unit/cloud/test_baremetal_ports.py @@ -25,28 +25,36 @@ from openstack.tests.unit import base class TestBaremetalPort(base.IronicTestCase): - def setUp(self): super(TestBaremetalPort, self).setUp() self.fake_baremetal_node = fakes.make_fake_machine( - self.name, self.uuid) + self.name, self.uuid + ) # TODO(TheJulia): Some tests below have fake ports, # since they are required in some processes. Lets refactor # them at some point to use self.fake_baremetal_port. self.fake_baremetal_port = fakes.make_fake_port( - '00:01:02:03:04:05', - node_id=self.uuid) + '00:01:02:03:04:05', node_id=self.uuid + ) self.fake_baremetal_port2 = fakes.make_fake_port( - '0a:0b:0c:0d:0e:0f', - node_id=self.uuid) + '0a:0b:0c:0d:0e:0f', node_id=self.uuid + ) def test_list_nics(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(resource='ports', append=['detail']), - json={'ports': [self.fake_baremetal_port, - self.fake_baremetal_port2]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='ports', append=['detail']), + json={ + 'ports': [ + self.fake_baremetal_port, + self.fake_baremetal_port2, + ] + }, + ), + ] + ) return_value = self.cloud.list_nics() self.assertEqual(2, len(return_value)) @@ -54,59 +62,86 @@ class TestBaremetalPort(base.IronicTestCase): self.assert_calls() def test_list_nics_failure(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(resource='ports', append=['detail']), - status_code=400) - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.list_nics) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='ports', append=['detail']), + status_code=400, + ) + ] + ) + self.assertRaises(exc.OpenStackCloudException, self.cloud.list_nics) self.assert_calls() def test_list_nics_for_machine(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='ports', - append=['detail'], - qs_elements=['node_uuid=%s' % - self.fake_baremetal_node['uuid']]), - json={'ports': [self.fake_baremetal_port, - self.fake_baremetal_port2]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + append=['detail'], + qs_elements=[ + 'node_uuid=%s' % self.fake_baremetal_node['uuid'] + ], + ), + json={ + 'ports': [ + self.fake_baremetal_port, + self.fake_baremetal_port2, + ] + }, + ), + ] + ) return_value = self.cloud.list_nics_for_machine( - self.fake_baremetal_node['uuid']) + self.fake_baremetal_node['uuid'] + ) self.assertEqual(2, len(return_value)) self.assertSubdict(self.fake_baremetal_port, return_value[0]) self.assert_calls() def test_list_nics_for_machine_failure(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='ports', - append=['detail'], - qs_elements=['node_uuid=%s' % - self.fake_baremetal_node['uuid']]), - status_code=400) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + append=['detail'], + qs_elements=[ + 'node_uuid=%s' % self.fake_baremetal_node['uuid'] + ], + ), + status_code=400, + ) + ] + ) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.list_nics_for_machine, - self.fake_baremetal_node['uuid']) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.list_nics_for_machine, + self.fake_baremetal_node['uuid'], + ) self.assert_calls() def test_get_nic_by_mac(self): mac = self.fake_baremetal_port['address'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='ports', - append=['detail'], - qs_elements=['address=%s' % mac]), - json={'ports': [self.fake_baremetal_port]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + append=['detail'], + qs_elements=['address=%s' % mac], + ), + json={'ports': [self.fake_baremetal_port]}, + ), + ] + ) return_value = self.cloud.get_nic_by_mac(mac) @@ -115,14 +150,19 @@ class TestBaremetalPort(base.IronicTestCase): def test_get_nic_by_mac_failure(self): mac = self.fake_baremetal_port['address'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='ports', - append=['detail'], - qs_elements=['address=%s' % mac]), - json={'ports': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='ports', + append=['detail'], + qs_elements=['address=%s' % mac], + ), + json={'ports': []}, + ), + ] + ) self.assertIsNone(self.cloud.get_nic_by_mac(mac)) diff --git a/openstack/tests/unit/cloud/test_caching.py b/openstack/tests/unit/cloud/test_caching.py index 7b700737d..a1fd697f8 100644 --- a/openstack/tests/unit/cloud/test_caching.py +++ b/openstack/tests/unit/cloud/test_caching.py @@ -39,26 +39,23 @@ def _(msg): _TASK_PROPERTIES = { "id": { "description": _("An identifier for the task"), - "pattern": _('^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}' - '-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$'), - "type": "string" + "pattern": _( + '^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}' + '-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$' + ), + "type": "string", }, "type": { "description": _("The type of task represented by this content"), "enum": [ "import", ], - "type": "string" + "type": "string", }, "status": { "description": _("The current status of this task"), - "enum": [ - "pending", - "processing", - "success", - "failure" - ], - "type": "string" + "enum": ["pending", "processing", "success", "failure"], + "type": "string", }, "input": { "description": _("The parameters required by task, JSON blob"), @@ -70,50 +67,55 @@ _TASK_PROPERTIES = { }, "owner": { "description": _("An identifier for the owner of this task"), - "type": "string" + "type": "string", }, "message": { - "description": _("Human-readable informative message only included" - " when appropriate (usually on failure)"), + "description": _( + "Human-readable informative message only included" + " when appropriate (usually on failure)" + ), "type": "string", }, "expires_at": { - "description": _("Datetime when this resource would be" - " subject to removal"), - "type": ["null", "string"] + "description": _( + "Datetime when this resource would be" " subject to removal" + ), + "type": ["null", "string"], }, "created_at": { "description": _("Datetime when this resource was created"), - "type": "string" + "type": "string", }, "updated_at": { "description": _("Datetime when this resource was updated"), - "type": "string" + "type": "string", }, 'self': {'type': 'string'}, - 'schema': {'type': 'string'} + 'schema': {'type': 'string'}, } _TASK_SCHEMA = dict( - name='Task', properties=_TASK_PROPERTIES, + name='Task', + properties=_TASK_PROPERTIES, additionalProperties=False, ) class TestMemoryCache(base.TestCase): - def setUp(self): super(TestMemoryCache, self).setUp( - cloud_config_fixture='clouds_cache.yaml') + cloud_config_fixture='clouds_cache.yaml' + ) def _compare_images(self, exp, real): self.assertDictEqual( _image.Image(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def _compare_volumes(self, exp, real): self.assertDictEqual( _volume.Volume(**exp).to_dict(computed=False), - real.to_dict(computed=False) + real.to_dict(computed=False), ) def test_openstack_cloud(self): @@ -122,13 +124,13 @@ class TestMemoryCache(base.TestCase): def _compare_projects(self, exp, real): self.assertDictEqual( _project.Project(**exp).to_dict(computed=False), - real.to_dict(computed=False) + real.to_dict(computed=False), ) def _compare_users(self, exp, real): self.assertDictEqual( _user.User(**exp).to_dict(computed=False), - real.to_dict(computed=False) + real.to_dict(computed=False), ) def test_list_projects_v3(self): @@ -137,28 +139,42 @@ class TestMemoryCache(base.TestCase): project_list = [project_one, project_two] first_response = {'projects': [project_one.json_response['project']]} - second_response = {'projects': [p.json_response['project'] - for p in project_list]} + second_response = { + 'projects': [p.json_response['project'] for p in project_list] + } mock_uri = self.get_mock_url( - service_type='identity', resource='projects', - base_url_append='v3') + service_type='identity', resource='projects', base_url_append='v3' + ) - self.register_uris([ - dict(method='GET', uri=mock_uri, status_code=200, - json=first_response), - dict(method='GET', uri=mock_uri, status_code=200, - json=second_response)]) + self.register_uris( + [ + dict( + method='GET', + uri=mock_uri, + status_code=200, + json=first_response, + ), + dict( + method='GET', + uri=mock_uri, + status_code=200, + json=second_response, + ), + ] + ) - for a, b in zip(first_response['projects'], - self.cloud.list_projects()): + for a, b in zip( + first_response['projects'], self.cloud.list_projects() + ): self._compare_projects(a, b) # invalidate the list_projects cache self.cloud.list_projects.invalidate(self.cloud) - for a, b in zip(second_response['projects'], - self.cloud.list_projects()): + for a, b in zip( + second_response['projects'], self.cloud.list_projects() + ): self._compare_projects(a, b) self.assert_calls() @@ -166,13 +182,18 @@ class TestMemoryCache(base.TestCase): def test_list_servers_no_herd(self): self.cloud._SERVER_AGE = 2 fake_server = fakes.make_fake_server('1234', 'name') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [fake_server]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + ] + ) with concurrent.futures.ThreadPoolExecutor(16) as pool: for i in range(16): pool.submit(lambda: self.cloud.list_servers(bare=True)) @@ -183,125 +204,180 @@ class TestMemoryCache(base.TestCase): self.assert_calls() def test_list_volumes(self): - fake_volume = fakes.FakeVolume('volume1', 'available', - 'Volume 1 Display Name') + fake_volume = fakes.FakeVolume( + 'volume1', 'available', 'Volume 1 Display Name' + ) fake_volume_dict = meta.obj_to_munch(fake_volume) - fake_volume2 = fakes.FakeVolume('volume2', 'available', - 'Volume 2 Display Name') + fake_volume2 = fakes.FakeVolume( + 'volume2', 'available', 'Volume 2 Display Name' + ) fake_volume2_dict = meta.obj_to_munch(fake_volume2) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volume_dict]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volume_dict, fake_volume2_dict]})]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volume_dict]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volume_dict, fake_volume2_dict]}, + ), + ] + ) - for a, b in zip([fake_volume_dict], - self.cloud.list_volumes()): + for a, b in zip([fake_volume_dict], self.cloud.list_volumes()): self._compare_volumes(a, b) # this call should hit the cache - for a, b in zip([fake_volume_dict], - self.cloud.list_volumes()): + for a, b in zip([fake_volume_dict], self.cloud.list_volumes()): self._compare_volumes(a, b) self.cloud.list_volumes.invalidate(self.cloud) - for a, b in zip([fake_volume_dict, fake_volume2_dict], - self.cloud.list_volumes()): + for a, b in zip( + [fake_volume_dict, fake_volume2_dict], self.cloud.list_volumes() + ): self._compare_volumes(a, b) self.assert_calls() def test_list_volumes_creating_invalidates(self): - fake_volume = fakes.FakeVolume('volume1', 'creating', - 'Volume 1 Display Name') + fake_volume = fakes.FakeVolume( + 'volume1', 'creating', 'Volume 1 Display Name' + ) fake_volume_dict = meta.obj_to_munch(fake_volume) - fake_volume2 = fakes.FakeVolume('volume2', 'available', - 'Volume 2 Display Name') + fake_volume2 = fakes.FakeVolume( + 'volume2', 'available', 'Volume 2 Display Name' + ) fake_volume2_dict = meta.obj_to_munch(fake_volume2) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volume_dict]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volume_dict, fake_volume2_dict]})]) - for a, b in zip([fake_volume_dict], - self.cloud.list_volumes()): + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volume_dict]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volume_dict, fake_volume2_dict]}, + ), + ] + ) + for a, b in zip([fake_volume_dict], self.cloud.list_volumes()): self._compare_volumes(a, b) - for a, b in zip([fake_volume_dict, fake_volume2_dict], - self.cloud.list_volumes()): + for a, b in zip( + [fake_volume_dict, fake_volume2_dict], self.cloud.list_volumes() + ): self._compare_volumes(a, b) self.assert_calls() def test_create_volume_invalidates(self): fake_volb4 = meta.obj_to_munch( - fakes.FakeVolume('volume1', 'available', '')) + fakes.FakeVolume('volume1', 'available', '') + ) _id = '12345' fake_vol_creating = meta.obj_to_munch( - fakes.FakeVolume(_id, 'creating', '')) + fakes.FakeVolume(_id, 'creating', '') + ) fake_vol_avail = meta.obj_to_munch( - fakes.FakeVolume(_id, 'available', '')) + fakes.FakeVolume(_id, 'available', '') + ) def now_deleting(request, context): fake_vol_avail['status'] = 'deleting' - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volb4]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes']), - json={'volume': fake_vol_creating}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', _id]), - json={'volume': fake_vol_creating}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', _id]), - json={'volume': fake_vol_avail}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volb4, fake_vol_avail]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', _id]), - json={'volume': fake_vol_avail}), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', _id]), - json=now_deleting), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', _id]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [fake_volb4, fake_vol_avail]}), - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volb4]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes'] + ), + json={'volume': fake_vol_creating}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', _id] + ), + json={'volume': fake_vol_creating}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', _id] + ), + json={'volume': fake_vol_avail}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volb4, fake_vol_avail]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', _id] + ), + json={'volume': fake_vol_avail}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', _id] + ), + json=now_deleting, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', _id] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [fake_volb4, fake_vol_avail]}, + ), + ] + ) for a, b in zip([fake_volb4], self.cloud.list_volumes()): self._compare_volumes(a, b) - volume = dict(display_name='junk_vol', - size=1, - display_description='test junk volume') + volume = dict( + display_name='junk_vol', + size=1, + display_description='test junk volume', + ) self.cloud.create_volume(wait=True, timeout=2, **volume) # If cache was not invalidated, we would not see our own volume here # because the first volume was available and thus would already be # cached. - for a, b in zip([fake_volb4, fake_vol_avail], - self.cloud.list_volumes()): + for a, b in zip( + [fake_volb4, fake_vol_avail], self.cloud.list_volumes() + ): self._compare_volumes(a, b) self.cloud.delete_volume(_id) # And now delete and check same thing since list is cached as all @@ -312,14 +388,20 @@ class TestMemoryCache(base.TestCase): def test_list_users(self): user_data = self._get_user_data(email='test@example.com') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - service_type='identity', - resource='users', - base_url_append='v3'), - status_code=200, - json={'users': [user_data.json_response['user']]})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + service_type='identity', + resource='users', + base_url_append='v3', + ), + status_code=200, + json={'users': [user_data.json_response['user']]}, + ) + ] + ) users = self.cloud.list_users() self.assertEqual(1, len(users)) self.assertEqual(user_data.user_id, users[0]['id']) @@ -336,14 +418,14 @@ class TestMemoryCache(base.TestCase): new_req = {'user': {'email': new_resp['user']['email']}} mock_users_url = self.get_mock_url( - service_type='identity', - interface='admin', - resource='users') + service_type='identity', interface='admin', resource='users' + ) mock_user_resource_url = self.get_mock_url( service_type='identity', interface='admin', resource='users', - append=[user_data.user_id]) + append=[user_data.user_id], + ) empty_user_list_resp = {'users': []} users_list_resp = {'users': [user_data.json_response['user']]} @@ -354,35 +436,68 @@ class TestMemoryCache(base.TestCase): uris_to_mock = [ # Inital User List is Empty - dict(method='GET', uri=mock_users_url, status_code=200, - json=empty_user_list_resp), + dict( + method='GET', + uri=mock_users_url, + status_code=200, + json=empty_user_list_resp, + ), # POST to create the user # GET to get the user data after POST - dict(method='POST', uri=mock_users_url, status_code=200, - json=user_data.json_response, - validate=dict(json=user_data.json_request)), + dict( + method='POST', + uri=mock_users_url, + status_code=200, + json=user_data.json_response, + validate=dict(json=user_data.json_request), + ), # List Users Call - dict(method='GET', uri=mock_users_url, status_code=200, - json=users_list_resp), + dict( + method='GET', + uri=mock_users_url, + status_code=200, + json=users_list_resp, + ), # List users to get ID for update # Get user using user_id from list # Update user # Get updated user - dict(method='GET', uri=mock_users_url, status_code=200, - json=users_list_resp), - dict(method='PUT', uri=mock_user_resource_url, status_code=200, - json=new_resp, validate=dict(json=new_req)), + dict( + method='GET', + uri=mock_users_url, + status_code=200, + json=users_list_resp, + ), + dict( + method='PUT', + uri=mock_user_resource_url, + status_code=200, + json=new_resp, + validate=dict(json=new_req), + ), # List Users Call - dict(method='GET', uri=mock_users_url, status_code=200, - json=updated_users_list_resp), + dict( + method='GET', + uri=mock_users_url, + status_code=200, + json=updated_users_list_resp, + ), # List User to get ID for delete # delete user - dict(method='GET', uri=mock_users_url, status_code=200, - json=updated_users_list_resp), + dict( + method='GET', + uri=mock_users_url, + status_code=200, + json=updated_users_list_resp, + ), dict(method='DELETE', uri=mock_user_resource_url, status_code=204), # List Users Call (empty post delete) - dict(method='GET', uri=mock_users_url, status_code=200, - json=empty_user_list_resp) + dict( + method='GET', + uri=mock_users_url, + status_code=200, + json=empty_user_list_resp, + ), ] self.register_uris(uris_to_mock) @@ -391,8 +506,9 @@ class TestMemoryCache(base.TestCase): self.assertEqual([], self.cloud.list_users()) # now add one - created = self.cloud.create_user(name=user_data.name, - email=user_data.email) + created = self.cloud.create_user( + name=user_data.name, email=user_data.email + ) self.assertEqual(user_data.user_id, created['id']) self.assertEqual(user_data.name, created['name']) self.assertEqual(user_data.email, created['email']) @@ -403,8 +519,9 @@ class TestMemoryCache(base.TestCase): self.assertEqual(user_data.email, users[0]['email']) # Update and check to see if it is updated - updated = self.cloud.update_user(user_data.user_id, - email=new_resp['user']['email']) + updated = self.cloud.update_user( + user_data.user_id, email=new_resp['user']['email'] + ) self.assertEqual(user_data.user_id, updated.id) self.assertEqual(user_data.name, updated.name) self.assertEqual(new_resp['user']['email'], updated.email) @@ -420,17 +537,26 @@ class TestMemoryCache(base.TestCase): def test_list_flavors(self): mock_uri = '{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT) + endpoint=fakes.COMPUTE_ENDPOINT + ) uris_to_mock = [ - dict(method='GET', uri=mock_uri, - validate=dict( - headers={'OpenStack-API-Version': 'compute 2.53'}), - json={'flavors': []}), - dict(method='GET', uri=mock_uri, - validate=dict( - headers={'OpenStack-API-Version': 'compute 2.53'}), - json={'flavors': fakes.FAKE_FLAVOR_LIST}) + dict( + method='GET', + uri=mock_uri, + validate=dict( + headers={'OpenStack-API-Version': 'compute 2.53'} + ), + json={'flavors': []}, + ), + dict( + method='GET', + uri=mock_uri, + validate=dict( + headers={'OpenStack-API-Version': 'compute 2.53'} + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), ] self.use_compute_discovery() @@ -442,9 +568,7 @@ class TestMemoryCache(base.TestCase): self.cloud.list_flavors.invalidate(self.cloud) self.assertResourceListEqual( - self.cloud.list_flavors(), - fakes.FAKE_FLAVOR_LIST, - _flavor.Flavor + self.cloud.list_flavors(), fakes.FAKE_FLAVOR_LIST, _flavor.Flavor ) self.assert_calls() @@ -454,23 +578,32 @@ class TestMemoryCache(base.TestCase): self.use_glance() fake_image = fakes.make_fake_image(image_id='42') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('image', 'public', - append=['v2', 'images']), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url('image', 'public', - append=['v2', 'images']), - json={'images': [fake_image]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', 'public', append=['v2', 'images'] + ), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', 'public', append=['v2', 'images'] + ), + json={'images': [fake_image]}, + ), + ] + ) self.assertEqual([], self.cloud.list_images()) self.assertEqual([], self.cloud.list_images()) self.cloud.list_images.invalidate(self.cloud) - [self._compare_images(a, b) for a, b in zip( - [fake_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([fake_image], self.cloud.list_images()) + ] self.assert_calls() @@ -479,23 +612,30 @@ class TestMemoryCache(base.TestCase): deleted_image_id = self.getUniqueString() deleted_image = fakes.make_fake_image( - image_id=deleted_image_id, status='deleted') + image_id=deleted_image_id, status='deleted' + ) active_image_id = self.getUniqueString() active_image = fakes.make_fake_image(image_id=active_image_id) list_return = {'images': [active_image, deleted_image]} - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json=list_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=list_return, + ), + ] + ) - [self._compare_images(a, b) for a, b in zip( - [active_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([active_image], self.cloud.list_images()) + ] - [self._compare_images(a, b) for a, b in zip( - [active_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([active_image], self.cloud.list_images()) + ] # We should only have one call self.assert_calls() @@ -507,29 +647,38 @@ class TestMemoryCache(base.TestCase): fi = fakes.make_fake_image(image_id=self.getUniqueString()) fi2 = fakes.make_fake_image(image_id=self.getUniqueString()) - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json={'images': [fi]}), - dict(method='GET', - uri='https://image.example.com/v2/images', - json={'images': [fi, fi2]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json={'images': [fi]}, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json={'images': [fi, fi2]}, + ), + ] + ) - [self._compare_images(a, b) for a, b in zip( - [fi], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([fi], self.cloud.list_images()) + ] # Now test that the list was cached - [self._compare_images(a, b) for a, b in zip( - [fi], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([fi], self.cloud.list_images()) + ] # Invalidation too self.cloud.list_images.invalidate(self.cloud) - [self._compare_images(a, b) for a, b in zip( - [fi, fi2], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([fi, fi2], self.cloud.list_images()) + ] def test_list_ports_filtered(self): down_port = test_port.TestPort.mock_neutron_port_create_rep['port'] @@ -537,21 +686,31 @@ class TestMemoryCache(base.TestCase): active_port['status'] = 'ACTIVE' # We're testing to make sure a query string is passed when we're # caching (cache by url), and that the results are still filtered. - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['status=DOWN']), - json={'ports': [ - down_port, - active_port, - ]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['status=DOWN'], + ), + json={ + 'ports': [ + down_port, + active_port, + ] + }, + ), + ] + ) ports = self.cloud.list_ports(filters={'status': 'DOWN'}) for a, b in zip([down_port], ports): self.assertDictEqual( _port.Port(**a).to_dict(computed=False), - b.to_dict(computed=False)) + b.to_dict(computed=False), + ) self.assert_calls() @@ -565,41 +724,56 @@ class TestCacheIgnoresQueuedStatus(base.TestCase): def setUp(self): super(TestCacheIgnoresQueuedStatus, self).setUp( - cloud_config_fixture='clouds_cache.yaml') + cloud_config_fixture='clouds_cache.yaml' + ) self.use_glance() active_image_id = self.getUniqueString() self.active_image = fakes.make_fake_image( - image_id=active_image_id, status=self.status) + image_id=active_image_id, status=self.status + ) self.active_list_return = {'images': [self.active_image]} steady_image_id = self.getUniqueString() self.steady_image = fakes.make_fake_image(image_id=steady_image_id) self.steady_list_return = { - 'images': [self.active_image, self.steady_image]} + 'images': [self.active_image, self.steady_image] + } def _compare_images(self, exp, real): self.assertDictEqual( _image.Image(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_list_images_ignores_pending_status(self): - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json=self.active_list_return), - dict(method='GET', - uri='https://image.example.com/v2/images', - json=self.steady_list_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=self.active_list_return, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=self.steady_list_return, + ), + ] + ) - [self._compare_images(a, b) for a, b in zip( - [self.active_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([self.active_image], self.cloud.list_images()) + ] # Should expect steady_image to appear if active wasn't cached - [self._compare_images(a, b) for a, b in zip( - [self.active_image, self.steady_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip( + [self.active_image, self.steady_image], + self.cloud.list_images(), + ) + ] class TestCacheSteadyStatus(base.TestCase): @@ -611,45 +785,53 @@ class TestCacheSteadyStatus(base.TestCase): def setUp(self): super(TestCacheSteadyStatus, self).setUp( - cloud_config_fixture='clouds_cache.yaml') + cloud_config_fixture='clouds_cache.yaml' + ) self.use_glance() active_image_id = self.getUniqueString() self.active_image = fakes.make_fake_image( - image_id=active_image_id, status=self.status) + image_id=active_image_id, status=self.status + ) self.active_list_return = {'images': [self.active_image]} def _compare_images(self, exp, real): self.assertDictEqual( _image.Image(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_list_images_caches_steady_status(self): - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json=self.active_list_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=self.active_list_return, + ), + ] + ) - [self._compare_images(a, b) for a, b in zip( - [self.active_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([self.active_image], self.cloud.list_images()) + ] - [self._compare_images(a, b) for a, b in zip( - [self.active_image], - self.cloud.list_images())] + [ + self._compare_images(a, b) + for a, b in zip([self.active_image], self.cloud.list_images()) + ] # We should only have one call self.assert_calls() class TestBogusAuth(base.TestCase): - def setUp(self): super(TestBogusAuth, self).setUp( - cloud_config_fixture='clouds_cache.yaml') + cloud_config_fixture='clouds_cache.yaml' + ) def test_get_auth_bogus(self): with testtools.ExpectedException(exceptions.ConfigException): - openstack.connect( - cloud='_bogus_test_', config=self.config) + openstack.connect(cloud='_bogus_test_', config=self.config) diff --git a/openstack/tests/unit/cloud/test_cluster_templates.py b/openstack/tests/unit/cloud/test_cluster_templates.py index c6f5ad70f..bddf07fc8 100644 --- a/openstack/tests/unit/cloud/test_cluster_templates.py +++ b/openstack/tests/unit/cloud/test_cluster_templates.py @@ -49,7 +49,6 @@ cluster_template_obj = dict( class TestClusterTemplates(base.TestCase): - def _compare_clustertemplates(self, exp, real): self.assertDictEqual( cluster_template.ClusterTemplate(**exp).to_dict(computed=False), @@ -57,20 +56,30 @@ class TestClusterTemplates(base.TestCase): ) def get_mock_url( - self, - service_type='container-infrastructure-management', - base_url_append=None, append=None, resource=None): + self, + service_type='container-infrastructure-management', + base_url_append=None, + append=None, + resource=None, + ): return super(TestClusterTemplates, self).get_mock_url( - service_type=service_type, resource=resource, - append=append, base_url_append=base_url_append) + service_type=service_type, + resource=resource, + append=append, + base_url_append=base_url_append, + ) def test_list_cluster_templates_without_detail(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ) + ] + ) cluster_templates_list = self.cloud.list_cluster_templates() self._compare_clustertemplates( cluster_template_obj, @@ -79,11 +88,15 @@ class TestClusterTemplates(base.TestCase): self.assert_calls() def test_list_cluster_templates_with_detail(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ) + ] + ) cluster_templates_list = self.cloud.list_cluster_templates(detail=True) self._compare_clustertemplates( cluster_template_obj, @@ -92,14 +105,19 @@ class TestClusterTemplates(base.TestCase): self.assert_calls() def test_search_cluster_templates_by_name(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ) + ] + ) cluster_templates = self.cloud.search_cluster_templates( - name_or_id='fake-cluster-template') + name_or_id='fake-cluster-template' + ) self.assertEqual(1, len(cluster_templates)) self.assertEqual('fake-uuid', cluster_templates[0]['uuid']) @@ -107,24 +125,33 @@ class TestClusterTemplates(base.TestCase): def test_search_cluster_templates_not_found(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ) + ] + ) cluster_templates = self.cloud.search_cluster_templates( - name_or_id='non-existent') + name_or_id='non-existent' + ) self.assertEqual(0, len(cluster_templates)) self.assert_calls() def test_get_cluster_template(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ) + ] + ) r = self.cloud.get_cluster_template('fake-cluster-template') self.assertIsNotNone(r) @@ -135,41 +162,52 @@ class TestClusterTemplates(base.TestCase): self.assert_calls() def test_get_cluster_template_not_found(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[]), + ) + ] + ) r = self.cloud.get_cluster_template('doesNotExist') self.assertIsNone(r) self.assert_calls() def test_create_cluster_template(self): json_response = cluster_template_obj.copy() - kwargs = dict(name=cluster_template_obj['name'], - image_id=cluster_template_obj['image_id'], - keypair_id=cluster_template_obj['keypair_id'], - coe=cluster_template_obj['coe']) - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url(resource='clustertemplates'), - json=json_response, - validate=dict(json=kwargs))]) - response = self.cloud.create_cluster_template(**kwargs) - self._compare_clustertemplates( - json_response, - response + kwargs = dict( + name=cluster_template_obj['name'], + image_id=cluster_template_obj['image_id'], + keypair_id=cluster_template_obj['keypair_id'], + coe=cluster_template_obj['coe'], ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='clustertemplates'), + json=json_response, + validate=dict(json=kwargs), + ) + ] + ) + response = self.cloud.create_cluster_template(**kwargs) + self._compare_clustertemplates(json_response, response) self.assert_calls() def test_create_cluster_template_exception(self): - self.register_uris([ - dict( - method='POST', - uri=self.get_mock_url(resource='clustertemplates'), - status_code=403)]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='clustertemplates'), + status_code=403, + ) + ] + ) # TODO(mordred) requests here doens't give us a great story # for matching the old error message text. Investigate plumbing # an error message in to the adapter call so that we can give a @@ -177,54 +215,72 @@ class TestClusterTemplates(base.TestCase): # OpenStackCloudException - but for some reason testtools will not # match the more specific HTTPError, even though it's a subclass # of OpenStackCloudException. - with testtools.ExpectedException( - exceptions.ForbiddenException): + with testtools.ExpectedException(exceptions.ForbiddenException): self.cloud.create_cluster_template('fake-cluster-template') self.assert_calls() def test_delete_cluster_template(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj])), - dict( - method='DELETE', - uri=self.get_mock_url(resource='clustertemplates/fake-uuid')), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='clustertemplates/fake-uuid' + ), + ), + ] + ) self.cloud.delete_cluster_template('fake-uuid') self.assert_calls() def test_update_cluster_template(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj])), - dict( - method='PATCH', - uri=self.get_mock_url(resource='clustertemplates/fake-uuid'), - status_code=200, - validate=dict( - json=[{ - 'op': 'replace', - 'path': '/name', - 'value': 'new-cluster-template' - }] - )), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ), + dict( + method='PATCH', + uri=self.get_mock_url( + resource='clustertemplates/fake-uuid' + ), + status_code=200, + validate=dict( + json=[ + { + 'op': 'replace', + 'path': '/name', + 'value': 'new-cluster-template', + } + ] + ), + ), + ] + ) new_name = 'new-cluster-template' updated = self.cloud.update_cluster_template( - 'fake-uuid', name=new_name) + 'fake-uuid', name=new_name + ) self.assertEqual(new_name, updated.name) self.assert_calls() def test_coe_get_cluster_template(self): - self.register_uris([ - dict( - method='GET', - uri=self.get_mock_url(resource='clustertemplates'), - json=dict(clustertemplates=[cluster_template_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='clustertemplates'), + json=dict(clustertemplates=[cluster_template_obj]), + ) + ] + ) r = self.cloud.get_cluster_template('fake-cluster-template') self.assertIsNotNone(r) diff --git a/openstack/tests/unit/cloud/test_clustering.py b/openstack/tests/unit/cloud/test_clustering.py index 062e62861..7c0d1dc4a 100644 --- a/openstack/tests/unit/cloud/test_clustering.py +++ b/openstack/tests/unit/cloud/test_clustering.py @@ -24,14 +24,10 @@ CLUSTERING_DICT = { 'max_size': 1, 'min_size': 1, 'timeout': 100, - 'metadata': {} + 'metadata': {}, } -PROFILE_DICT = { - 'name': 'fake-profile-name', - 'spec': {}, - 'metadata': {} -} +PROFILE_DICT = {'name': 'fake-profile-name', 'spec': {}, 'metadata': {}} POLICY_DICT = { 'name': 'fake-profile-name', @@ -43,7 +39,7 @@ RECEIVER_DICT = { 'cluster_id': 'fake-cluster-id', 'name': 'fake-receiver-name', 'params': {}, - 'type': 'webhook' + 'type': 'webhook', } NEW_CLUSTERING_DICT = copy.copy(CLUSTERING_DICT) @@ -57,7 +53,6 @@ NEW_RECEIVER_DICT['id'] = '1' class TestClustering(base.TestCase): - def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) @@ -65,12 +60,14 @@ class TestClustering(base.TestCase): def _compare_clusters(self, exp, real): self.assertEqual( cluster.Cluster(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def setUp(self): super(TestClustering, self).setUp() self.use_senlin() + # def test_create_cluster(self): # self.register_uris([ # dict(method='GET', diff --git a/openstack/tests/unit/cloud/test_coe_clusters.py b/openstack/tests/unit/cloud/test_coe_clusters.py index e05ba774c..833b86584 100644 --- a/openstack/tests/unit/cloud/test_coe_clusters.py +++ b/openstack/tests/unit/cloud/test_coe_clusters.py @@ -212,7 +212,5 @@ class TestCOEClusters(base.TestCase): ), ] ) - self.cloud.update_coe_cluster( - coe_cluster_obj["uuid"], node_count=3 - ) + self.cloud.update_coe_cluster(coe_cluster_obj["uuid"], node_count=3) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_coe_clusters_certificate.py b/openstack/tests/unit/cloud/test_coe_clusters_certificate.py index 56c42a846..57aabe7f4 100644 --- a/openstack/tests/unit/cloud/test_coe_clusters_certificate.py +++ b/openstack/tests/unit/cloud/test_coe_clusters_certificate.py @@ -12,7 +12,7 @@ from openstack.container_infrastructure_management.v1 import ( - cluster_certificate + cluster_certificate, ) from openstack.tests.unit import base @@ -20,7 +20,7 @@ coe_cluster_ca_obj = dict( cluster_uuid="43e305ce-3a5f-412a-8a14-087834c34c8c", pem="-----BEGIN CERTIFICATE-----\nMIIDAO\n-----END CERTIFICATE-----\n", bay_uuid="43e305ce-3a5f-412a-8a14-087834c34c8c", - links=[] + links=[], ) coe_cluster_signed_cert_obj = dict( @@ -28,50 +28,72 @@ coe_cluster_signed_cert_obj = dict( pem='-----BEGIN CERTIFICATE-----\nMIIDAO\n-----END CERTIFICATE-----', bay_uuid='43e305ce-3a5f-412a-8a14-087834c34c8c', links=[], - csr=('-----BEGIN CERTIFICATE REQUEST-----\nMIICfz==' - '\n-----END CERTIFICATE REQUEST-----\n') + csr=( + '-----BEGIN CERTIFICATE REQUEST-----\nMIICfz==' + '\n-----END CERTIFICATE REQUEST-----\n' + ), ) class TestCOEClusters(base.TestCase): def _compare_cluster_certs(self, exp, real): self.assertDictEqual( - cluster_certificate.ClusterCertificate( - **exp).to_dict(computed=False), + cluster_certificate.ClusterCertificate(**exp).to_dict( + computed=False + ), real.to_dict(computed=False), ) def get_mock_url( - self, - service_type='container-infrastructure-management', - base_url_append=None, append=None, resource=None): + self, + service_type='container-infrastructure-management', + base_url_append=None, + append=None, + resource=None, + ): return super(TestCOEClusters, self).get_mock_url( - service_type=service_type, resource=resource, - append=append, base_url_append=base_url_append) + service_type=service_type, + resource=resource, + append=append, + base_url_append=base_url_append, + ) def test_get_coe_cluster_certificate(self): - self.register_uris([dict( - method='GET', - uri=self.get_mock_url( - resource='certificates', - append=[coe_cluster_ca_obj['cluster_uuid']]), - json=coe_cluster_ca_obj) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='certificates', + append=[coe_cluster_ca_obj['cluster_uuid']], + ), + json=coe_cluster_ca_obj, + ) + ] + ) ca_cert = self.cloud.get_coe_cluster_certificate( - coe_cluster_ca_obj['cluster_uuid']) - self._compare_cluster_certs( - coe_cluster_ca_obj, - ca_cert) + coe_cluster_ca_obj['cluster_uuid'] + ) + self._compare_cluster_certs(coe_cluster_ca_obj, ca_cert) self.assert_calls() def test_sign_coe_cluster_certificate(self): - self.register_uris([dict( - method='POST', - uri=self.get_mock_url(resource='certificates'), - json={"cluster_uuid": coe_cluster_signed_cert_obj['cluster_uuid'], - "csr": coe_cluster_signed_cert_obj['csr']} - )]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(resource='certificates'), + json={ + "cluster_uuid": coe_cluster_signed_cert_obj[ + 'cluster_uuid' + ], + "csr": coe_cluster_signed_cert_obj['csr'], + }, + ) + ] + ) self.cloud.sign_coe_cluster_certificate( coe_cluster_signed_cert_obj['cluster_uuid'], - coe_cluster_signed_cert_obj['csr']) + coe_cluster_signed_cert_obj['csr'], + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_create_server.py b/openstack/tests/unit/cloud/test_create_server.py index 5fb868479..4b3c1e543 100644 --- a/openstack/tests/unit/cloud/test_create_server.py +++ b/openstack/tests/unit/cloud/test_create_server.py @@ -42,32 +42,51 @@ class TestCreateServer(base.TestCase): raises an exception in create_server. """ build_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + status_code=404, + ), + ] + ) self.assertRaises( - exc.OpenStackCloudException, self.cloud.create_server, - 'server-name', {'id': 'image-id'}, {'id': 'flavor-id'}) + exc.OpenStackCloudException, + self.cloud.create_server, + 'server-name', + {'id': 'image-id'}, + {'id': 'flavor-id'}, + ) self.assert_calls() def test_create_server_with_server_error(self): @@ -77,32 +96,51 @@ class TestCreateServer(base.TestCase): """ build_server = fakes.make_fake_server('1234', '', 'BUILD') error_server = fakes.make_fake_server('1234', '', 'ERROR') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': error_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': error_server}, + ), + ] + ) self.assertRaises( - exc.OpenStackCloudException, self.cloud.create_server, - 'server-name', {'id': 'image-id'}, {'id': 'flavor-id'}) + exc.OpenStackCloudException, + self.cloud.create_server, + 'server-name', + {'id': 'image-id'}, + {'id': 'flavor-id'}, + ) self.assert_calls() def test_create_server_wait_server_error(self): @@ -112,38 +150,59 @@ class TestCreateServer(base.TestCase): """ build_server = fakes.make_fake_server('1234', '', 'BUILD') error_server = fakes.make_fake_server('1234', '', 'ERROR') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [build_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [error_server]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [build_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [error_server]}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.create_server, - 'server-name', dict(id='image-id'), - dict(id='flavor-id'), wait=True) + 'server-name', + dict(id='image-id'), + dict(id='flavor-id'), + wait=True, + ) self.assert_calls() @@ -153,35 +212,53 @@ class TestCreateServer(base.TestCase): exception in create_server. """ fake_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [fake_server]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudTimeout, self.cloud.create_server, 'server-name', - dict(id='image-id'), dict(id='flavor-id'), - wait=True, timeout=0.01) + dict(id='image-id'), + dict(id='flavor-id'), + wait=True, + timeout=0.01, + ) # We poll at the end, so we don't know real counts self.assert_calls(do_count=False) @@ -191,35 +268,51 @@ class TestCreateServer(base.TestCase): create call returns the server instance. """ fake_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.assertDictEqual( server.Server(**fake_server).to_dict(computed=False), self.cloud.create_server( name='server-name', image=dict(id='image-id'), - flavor=dict(id='flavor-id')).to_dict(computed=False) + flavor=dict(id='flavor-id'), + ).to_dict(computed=False), ) self.assert_calls() @@ -229,37 +322,54 @@ class TestCreateServer(base.TestCase): Test that config_drive gets passed in properly """ fake_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'config_drive': True, - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'config_drive': True, + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.assertDictEqual( server.Server(**fake_server).to_dict(computed=False), self.cloud.create_server( name='server-name', image=dict(id='image-id'), flavor=dict(id='flavor-id'), - config_drive=True).to_dict(computed=False)) + config_drive=True, + ).to_dict(computed=False), + ) self.assert_calls() @@ -268,36 +378,52 @@ class TestCreateServer(base.TestCase): Test that config_drive gets not passed in properly """ fake_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.assertEqual( server.Server(**fake_server).to_dict(computed=False), self.cloud.create_server( name='server-name', image=dict(id='image-id'), flavor=dict(id='flavor-id'), - config_drive=None).to_dict(computed=False) + config_drive=None, + ).to_dict(computed=False), ) self.assert_calls() @@ -309,37 +435,56 @@ class TestCreateServer(base.TestCase): admin_pass = self.getUniqueString('password') fake_server = fakes.make_fake_server('1234', '', 'BUILD') fake_create_server = fakes.make_fake_server( - '1234', '', 'BUILD', admin_pass=admin_pass) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_create_server}, - validate=dict( - json={'server': { - u'adminPass': admin_pass, - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + '1234', '', 'BUILD', admin_pass=admin_pass + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_create_server}, + validate=dict( + json={ + 'server': { + 'adminPass': admin_pass, + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.assertEqual( admin_pass, self.cloud.create_server( - name='server-name', image=dict(id='image-id'), + name='server-name', + image=dict(id='image-id'), flavor=dict(id='flavor-id'), - admin_pass=admin_pass)['admin_password']) + admin_pass=admin_pass, + )['admin_password'], + ) self.assert_calls() @@ -351,43 +496,58 @@ class TestCreateServer(base.TestCase): admin_pass = self.getUniqueString('password') fake_server = fakes.make_fake_server('1234', '', 'BUILD') fake_server_with_pass = fakes.make_fake_server( - '1234', '', 'BUILD', admin_pass=admin_pass) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server_with_pass}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'adminPass': admin_pass, - u'name': u'server-name', - 'networks': 'auto'}})), - ]) + '1234', '', 'BUILD', admin_pass=admin_pass + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server_with_pass}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'adminPass': admin_pass, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + ] + ) # The wait returns non-password server mock_wait.return_value = server.Server(**fake_server) new_server = self.cloud.create_server( - name='server-name', image=dict(id='image-id'), + name='server-name', + image=dict(id='image-id'), flavor=dict(id='flavor-id'), - admin_pass=admin_pass, wait=True) + admin_pass=admin_pass, + wait=True, + ) # Assert that we did wait self.assertTrue(mock_wait.called) # Even with the wait, we should still get back a passworded server self.assertEqual( - new_server['admin_password'], - fake_server_with_pass['adminPass'] + new_server['admin_password'], fake_server_with_pass['adminPass'] ) self.assert_calls() @@ -396,40 +556,59 @@ class TestCreateServer(base.TestCase): Test that a server passed user-data sends it base64 encoded. """ user_data = self.getUniqueString('user_data') - user_data_b64 = base64.b64encode( - user_data.encode('utf-8')).decode('utf-8') + user_data_b64 = base64.b64encode(user_data.encode('utf-8')).decode( + 'utf-8' + ) fake_server = fakes.make_fake_server('1234', '', 'BUILD') fake_server['user_data'] = user_data - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'user_data': user_data_b64, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'user_data': user_data_b64, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.cloud.create_server( - name='server-name', image=dict(id='image-id'), + name='server-name', + image=dict(id='image-id'), flavor=dict(id='flavor-id'), - userdata=user_data, wait=False) + userdata=user_data, + wait=False, + ) self.assert_calls() @@ -445,26 +624,45 @@ class TestCreateServer(base.TestCase): active_server = {'id': 'fake_server_id', 'status': 'ACTIVE'} mock_get_server.side_effect = iter([building_server, active_server]) - mock_get_active_server.side_effect = iter([ - building_server, active_server]) + mock_get_active_server.side_effect = iter( + [building_server, active_server] + ) server = self.cloud.wait_for_server(building_server) self.assertEqual(2, mock_get_server.call_count) - mock_get_server.assert_has_calls([ - mock.call(building_server['id']), - mock.call(active_server['id']), - ]) + mock_get_server.assert_has_calls( + [ + mock.call(building_server['id']), + mock.call(active_server['id']), + ] + ) self.assertEqual(2, mock_get_active_server.call_count) - mock_get_active_server.assert_has_calls([ - mock.call(server=building_server, reuse=True, auto_ip=True, - ips=None, ip_pool=None, wait=True, timeout=mock.ANY, - nat_destination=None), - mock.call(server=active_server, reuse=True, auto_ip=True, - ips=None, ip_pool=None, wait=True, timeout=mock.ANY, - nat_destination=None), - ]) + mock_get_active_server.assert_has_calls( + [ + mock.call( + server=building_server, + reuse=True, + auto_ip=True, + ips=None, + ip_pool=None, + wait=True, + timeout=mock.ANY, + nat_destination=None, + ), + mock.call( + server=active_server, + reuse=True, + auto_ip=True, + ips=None, + ip_pool=None, + wait=True, + timeout=mock.ANY, + nat_destination=None, + ), + ] + ) self.assertEqual('ACTIVE', server['status']) @@ -476,102 +674,153 @@ class TestCreateServer(base.TestCase): # TODO(mordred) Make this a full proper response fake_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + ] + ) self.cloud.create_server( - 'server-name', - dict(id='image-id'), dict(id='flavor-id'), wait=True), + 'server-name', dict(id='image-id'), dict(id='flavor-id'), + wait=True, + ), # This is a pretty dirty hack to ensure we in principle use object with # expected properties srv = server.Server.existing( connection=self.cloud, - min_count=1, max_count=1, + min_count=1, + max_count=1, networks='auto', imageRef='image-id', flavorRef='flavor-id', - **fake_server) + **fake_server + ) mock_wait.assert_called_once_with( srv, - auto_ip=True, ips=None, - ip_pool=None, reuse=True, timeout=180, + auto_ip=True, + ips=None, + ip_pool=None, + reuse=True, + timeout=180, nat_destination=None, ) self.assert_calls() @mock.patch.object(connection.Connection, 'add_ips_to_server') - def test_create_server_no_addresses( - self, mock_add_ips_to_server): + def test_create_server_no_addresses(self, mock_add_ips_to_server): """ Test that create_server with a wait throws an exception if the server doesn't have addresses. """ build_server = fakes.make_fake_server('1234', '', 'BUILD') fake_server = fakes.make_fake_server( - '1234', '', 'ACTIVE', addresses={}) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [build_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [fake_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['device_id=1234']), - json={'ports': []}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234'])), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - status_code=404), - ]) + '1234', '', 'ACTIVE', addresses={} + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [build_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['device_id=1234'], + ), + json={'ports': []}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + status_code=404, + ), + ] + ) mock_add_ips_to_server.return_value = fake_server self.cloud._SERVER_AGE = 0 self.assertRaises( - exc.OpenStackCloudException, self.cloud.create_server, - 'server-name', {'id': 'image-id'}, {'id': 'flavor-id'}, - wait=True) + exc.OpenStackCloudException, + self.cloud.create_server, + 'server-name', + {'id': 'image-id'}, + {'id': 'flavor-id'}, + wait=True, + ) self.assert_calls() @@ -581,51 +830,77 @@ class TestCreateServer(base.TestCase): attempt to get the network for the server. """ build_server = fakes.make_fake_server('1234', '', 'BUILD') - network = { - 'id': 'network-id', - 'name': 'network-name' - } - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', 'network-name']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=network-name']), - json={'networks': [network]}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'networks': [{u'uuid': u'network-id'}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': build_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [network]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - ]) + network = {'id': 'network-id', 'name': 'network-name'} + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', 'network-name'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=network-name'], + ), + json={'networks': [network]}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'networks': [{'uuid': 'network-id'}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': build_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [network]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + ] + ) self.cloud.create_server( 'server-name', - dict(id='image-id'), dict(id='flavor-id'), network='network-name') + dict(id='image-id'), + dict(id='flavor-id'), + network='network-name', + ) self.assert_calls() def test_create_server_network_with_empty_nics(self): @@ -633,52 +908,79 @@ class TestCreateServer(base.TestCase): Verify that if 'network' is supplied, along with an empty 'nics' list, it's treated the same as if 'nics' were not included. """ - network = { - 'id': 'network-id', - 'name': 'network-name' - } + network = {'id': 'network-id', 'name': 'network-name'} build_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', 'network-name']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=network-name']), - json={'networks': [network]}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'networks': [{u'uuid': u'network-id'}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': build_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [network]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', 'network-name'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=network-name'], + ), + json={'networks': [network]}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'networks': [{'uuid': 'network-id'}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': build_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [network]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + ] + ) self.cloud.create_server( - 'server-name', dict(id='image-id'), dict(id='flavor-id'), - network='network-name', nics=[]) + 'server-name', + dict(id='image-id'), + dict(id='flavor-id'), + network='network-name', + nics=[], + ) self.assert_calls() def test_create_server_network_fixed_ip(self): @@ -686,42 +988,60 @@ class TestCreateServer(base.TestCase): Verify that if 'fixed_ip' is supplied in nics, we pass it to networks appropriately. """ - network = { - 'id': 'network-id', - 'name': 'network-name' - } + network = {'id': 'network-id', 'name': 'network-name'} fixed_ip = '10.0.0.1' build_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'networks': [{'fixed_ip': fixed_ip}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': build_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [network]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'networks': [{'fixed_ip': fixed_ip}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': build_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [network]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + ] + ) self.cloud.create_server( - 'server-name', dict(id='image-id'), dict(id='flavor-id'), - nics=[{'fixed_ip': fixed_ip}]) + 'server-name', + dict(id='image-id'), + dict(id='flavor-id'), + nics=[{'fixed_ip': fixed_ip}], + ) self.assert_calls() def test_create_server_network_v4_fixed_ip(self): @@ -729,42 +1049,60 @@ class TestCreateServer(base.TestCase): Verify that if 'v4-fixed-ip' is supplied in nics, we pass it to networks appropriately. """ - network = { - 'id': 'network-id', - 'name': 'network-name' - } + network = {'id': 'network-id', 'name': 'network-name'} fixed_ip = '10.0.0.1' build_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'networks': [{'fixed_ip': fixed_ip}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': build_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [network]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'networks': [{'fixed_ip': fixed_ip}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': build_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [network]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + ] + ) self.cloud.create_server( - 'server-name', dict(id='image-id'), dict(id='flavor-id'), - nics=[{'fixed_ip': fixed_ip}]) + 'server-name', + dict(id='image-id'), + dict(id='flavor-id'), + nics=[{'fixed_ip': fixed_ip}], + ) self.assert_calls() def test_create_server_network_v6_fixed_ip(self): @@ -772,44 +1110,62 @@ class TestCreateServer(base.TestCase): Verify that if 'v6-fixed-ip' is supplied in nics, we pass it to networks appropriately. """ - network = { - 'id': 'network-id', - 'name': 'network-name' - } + network = {'id': 'network-id', 'name': 'network-name'} # Note - it doesn't actually have to be a v6 address - it's just # an alias. fixed_ip = 'fe80::28da:5fff:fe57:13ed' build_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'networks': [{'fixed_ip': fixed_ip}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': build_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [network]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'networks': [{'fixed_ip': fixed_ip}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': build_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [network]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + ] + ) self.cloud.create_server( - 'server-name', dict(id='image-id'), dict(id='flavor-id'), - nics=[{'fixed_ip': fixed_ip}]) + 'server-name', + dict(id='image-id'), + dict(id='flavor-id'), + nics=[{'fixed_ip': fixed_ip}], + ) self.assert_calls() def test_create_server_network_fixed_ip_conflicts(self): @@ -822,12 +1178,13 @@ class TestCreateServer(base.TestCase): self.use_nothing() fixed_ip = '10.0.0.1' self.assertRaises( - exc.OpenStackCloudException, self.cloud.create_server, - 'server-name', dict(id='image-id'), dict(id='flavor-id'), - nics=[{ - 'fixed_ip': fixed_ip, - 'v4-fixed-ip': fixed_ip - }]) + exc.OpenStackCloudException, + self.cloud.create_server, + 'server-name', + dict(id='image-id'), + dict(id='flavor-id'), + nics=[{'fixed_ip': fixed_ip, 'v4-fixed-ip': fixed_ip}], + ) self.assert_calls() def test_create_server_get_flavor_image(self): @@ -839,41 +1196,67 @@ class TestCreateServer(base.TestCase): build_server = fakes.make_fake_server('1234', '', 'BUILD') active_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json=fake_image_search_return), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['flavors', 'vanilla'], - qs_elements=[]), - json=fakes.FAKE_FLAVOR), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': fakes.FLAVOR_ID, - u'imageRef': image_id, - u'max_count': 1, - u'min_count': 1, - u'networks': [{u'uuid': u'some-network'}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': active_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=fake_image_search_return, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['flavors', 'vanilla'], + qs_elements=[], + ), + json=fakes.FAKE_FLAVOR, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': fakes.FLAVOR_ID, + 'imageRef': image_id, + 'max_count': 1, + 'min_count': 1, + 'networks': [{'uuid': 'some-network'}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': active_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.cloud.create_server( - 'server-name', image_id, 'vanilla', - nics=[{'net-id': 'some-network'}], wait=False) + 'server-name', + image_id, + 'vanilla', + nics=[{'net-id': 'some-network'}], + wait=False, + ) self.assert_calls() @@ -884,33 +1267,52 @@ class TestCreateServer(base.TestCase): image_id = uuid.uuid4().hex port_id = uuid.uuid4().hex - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': fakes.FLAVOR_ID, - u'imageRef': image_id, - u'max_count': 1, - u'min_count': 1, - u'networks': [{u'port': port_id}], - u'name': u'server-name'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': active_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': fakes.FLAVOR_ID, + 'imageRef': image_id, + 'max_count': 1, + 'min_count': 1, + 'networks': [{'port': port_id}], + 'name': 'server-name', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': active_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.cloud.create_server( - 'server-name', dict(id=image_id), dict(id=fakes.FLAVOR_ID), - nics=[{'port-id': port_id}], wait=False) + 'server-name', + dict(id=image_id), + dict(id=fakes.FLAVOR_ID), + nics=[{'port-id': port_id}], + wait=False, + ) self.assert_calls() @@ -918,49 +1320,68 @@ class TestCreateServer(base.TestCase): build_server = fakes.make_fake_server('1234', '', 'BUILD') active_server = fakes.make_fake_server('1234', '', 'BUILD') - vol = {'id': 'volume001', 'status': 'available', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'available', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': 'flavor-id', - u'imageRef': 'image-id', - u'max_count': 1, - u'min_count': 1, - u'block_device_mapping_v2': [ - { - u'boot_index': 0, - u'delete_on_termination': True, - u'destination_type': u'local', - u'source_type': u'image', - u'uuid': u'image-id' - }, - { - u'boot_index': u'-1', - u'delete_on_termination': False, - u'destination_type': u'volume', - u'source_type': u'volume', - u'uuid': u'volume001' - } - ], - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': active_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'block_device_mapping_v2': [ + { + 'boot_index': 0, + 'delete_on_termination': True, + 'destination_type': 'local', + 'source_type': 'image', + 'uuid': 'image-id', + }, + { + 'boot_index': '-1', + 'delete_on_termination': False, + 'destination_type': 'volume', + 'source_type': 'volume', + 'uuid': 'volume001', + }, + ], + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': active_server}, + ), + ] + ) self.cloud.create_server( name='server-name', @@ -968,7 +1389,8 @@ class TestCreateServer(base.TestCase): flavor=dict(id='flavor-id'), boot_from_volume=False, volumes=[volume], - wait=False) + wait=False, + ) self.assert_calls() @@ -976,36 +1398,54 @@ class TestCreateServer(base.TestCase): build_server = fakes.make_fake_server('1234', '', 'BUILD') active_server = fakes.make_fake_server('1234', '', 'BUILD') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': build_server}, - validate=dict( - json={'server': { - u'flavorRef': 'flavor-id', - u'imageRef': '', - u'max_count': 1, - u'min_count': 1, - u'block_device_mapping_v2': [{ - u'boot_index': u'0', - u'delete_on_termination': True, - u'destination_type': u'volume', - u'source_type': u'image', - u'uuid': u'image-id', - u'volume_size': u'1'}], - u'name': u'server-name', - 'networks': 'auto'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': active_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': build_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': '', + 'max_count': 1, + 'min_count': 1, + 'block_device_mapping_v2': [ + { + 'boot_index': '0', + 'delete_on_termination': True, + 'destination_type': 'volume', + 'source_type': 'image', + 'uuid': 'image-id', + 'volume_size': '1', + } + ], + 'name': 'server-name', + 'networks': 'auto', + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': active_server}, + ), + ] + ) self.cloud.create_server( name='server-name', @@ -1014,7 +1454,8 @@ class TestCreateServer(base.TestCase): boot_from_volume=True, terminate_volume=True, volume_size=1, - wait=False) + wait=False, + ) self.assert_calls() @@ -1028,36 +1469,53 @@ class TestCreateServer(base.TestCase): fake_server = fakes.make_fake_server('1234', '', 'BUILD') fake_server['scheduler_hints'] = scheduler_hints - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={ - 'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}, - u'OS-SCH-HNT:scheduler_hints': scheduler_hints, })), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + }, + 'OS-SCH-HNT:scheduler_hints': scheduler_hints, + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.cloud.create_server( - name='server-name', image=dict(id='image-id'), + name='server-name', + image=dict(id='image-id'), flavor=dict(id='flavor-id'), - scheduler_hints=scheduler_hints, wait=False) + scheduler_hints=scheduler_hints, + wait=False, + ) self.assert_calls() @@ -1070,7 +1528,8 @@ class TestCreateServer(base.TestCase): group_name = self.getUniqueString('server-group') policies = ['affinity'] fake_group = fakes.make_fake_server_group( - group_id, group_name, policies) + group_id, group_name, policies + ) # The scheduler hints we pass in scheduler_hints = { @@ -1086,42 +1545,61 @@ class TestCreateServer(base.TestCase): fake_server = fakes.make_fake_server('1234', '', 'BUILD') fake_server['scheduler_hints'] = scheduler_hints_merged - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-server-groups']), - json={'server_groups': [fake_group]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={ - 'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}, - u'OS-SCH-HNT:scheduler_hints': scheduler_hints_merged, - })), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-server-groups'] + ), + json={'server_groups': [fake_group]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + }, + 'OS-SCH-HNT:scheduler_hints': scheduler_hints_merged, # noqa: E501 + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.cloud.create_server( - name='server-name', image=dict(id='image-id'), + name='server-name', + image=dict(id='image-id'), flavor=dict(id='flavor-id'), - scheduler_hints=dict(scheduler_hints), group=group_name, - wait=False) + scheduler_hints=dict(scheduler_hints), + group=group_name, + wait=False, + ) self.assert_calls() @@ -1135,7 +1613,8 @@ class TestCreateServer(base.TestCase): group_name = self.getUniqueString('server-group') policies = ['affinity'] fake_group = fakes.make_fake_server_group( - group_id_param, group_name, policies) + group_id_param, group_name, policies + ) # The scheduler hints we pass in that are expected to be ignored in # POST call @@ -1151,41 +1630,60 @@ class TestCreateServer(base.TestCase): fake_server = fakes.make_fake_server('1234', '', 'BUILD') fake_server['scheduler_hints'] = group_scheduler_hints - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-server-groups']), - json={'server_groups': [fake_group]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['servers']), - json={'server': fake_server}, - validate=dict( - json={ - 'server': { - u'flavorRef': u'flavor-id', - u'imageRef': u'image-id', - u'max_count': 1, - u'min_count': 1, - u'name': u'server-name', - 'networks': 'auto'}, - u'OS-SCH-HNT:scheduler_hints': group_scheduler_hints, - })), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - json={'server': fake_server}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-server-groups'] + ), + json={'server_groups': [fake_group]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['servers'] + ), + json={'server': fake_server}, + validate=dict( + json={ + 'server': { + 'flavorRef': 'flavor-id', + 'imageRef': 'image-id', + 'max_count': 1, + 'min_count': 1, + 'name': 'server-name', + 'networks': 'auto', + }, + 'OS-SCH-HNT:scheduler_hints': group_scheduler_hints, # noqa: E501 + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + json={'server': fake_server}, + ), + ] + ) self.cloud.create_server( - name='server-name', image=dict(id='image-id'), + name='server-name', + image=dict(id='image-id'), flavor=dict(id='flavor-id'), - scheduler_hints=dict(scheduler_hints), group=group_name, - wait=False) + scheduler_hints=dict(scheduler_hints), + group=group_name, + wait=False, + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_create_volume_snapshot.py b/openstack/tests/unit/cloud/test_create_volume_snapshot.py index e77f18905..ed8421475 100644 --- a/openstack/tests/unit/cloud/test_create_volume_snapshot.py +++ b/openstack/tests/unit/cloud/test_create_volume_snapshot.py @@ -25,7 +25,6 @@ from openstack.tests.unit import base class TestCreateVolumeSnapshot(base.TestCase): - def setUp(self): super(TestCreateVolumeSnapshot, self).setUp() self.use_cinder() @@ -33,7 +32,8 @@ class TestCreateVolumeSnapshot(base.TestCase): def _compare_snapshots(self, exp, real): self.assertDictEqual( snapshot.Snapshot(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_create_volume_snapshot_wait(self): """ @@ -42,32 +42,46 @@ class TestCreateVolumeSnapshot(base.TestCase): """ snapshot_id = '5678' volume_id = '1234' - build_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'creating', - 'foo', 'derpysnapshot') + build_snapshot = fakes.FakeVolumeSnapshot( + snapshot_id, 'creating', 'foo', 'derpysnapshot' + ) build_snapshot_dict = meta.obj_to_munch(build_snapshot) - fake_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'available', - 'foo', 'derpysnapshot') + fake_snapshot = fakes.FakeVolumeSnapshot( + snapshot_id, 'available', 'foo', 'derpysnapshot' + ) fake_snapshot_dict = meta.obj_to_munch(fake_snapshot) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', append=['snapshots']), - json={'snapshot': build_snapshot_dict}, - validate=dict(json={ - 'snapshot': {'volume_id': '1234'}})), - dict(method='GET', - uri=self.get_mock_url('volumev3', 'public', - append=['snapshots', snapshot_id]), - json={'snapshot': build_snapshot_dict}), - dict(method='GET', - uri=self.get_mock_url('volumev3', 'public', - append=['snapshots', snapshot_id]), - json={'snapshot': fake_snapshot_dict})]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots'] + ), + json={'snapshot': build_snapshot_dict}, + validate=dict(json={'snapshot': {'volume_id': '1234'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', snapshot_id] + ), + json={'snapshot': build_snapshot_dict}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', snapshot_id] + ), + json={'snapshot': fake_snapshot_dict}, + ), + ] + ) self._compare_snapshots( fake_snapshot_dict, - self.cloud.create_volume_snapshot(volume_id=volume_id, wait=True)) + self.cloud.create_volume_snapshot(volume_id=volume_id, wait=True), + ) self.assert_calls() def test_create_volume_snapshot_with_timeout(self): @@ -77,26 +91,38 @@ class TestCreateVolumeSnapshot(base.TestCase): """ snapshot_id = '5678' volume_id = '1234' - build_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'creating', - 'foo', 'derpysnapshot') + build_snapshot = fakes.FakeVolumeSnapshot( + snapshot_id, 'creating', 'foo', 'derpysnapshot' + ) build_snapshot_dict = meta.obj_to_munch(build_snapshot) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', append=['snapshots']), - json={'snapshot': build_snapshot_dict}, - validate=dict(json={ - 'snapshot': {'volume_id': '1234'}})), - dict(method='GET', - uri=self.get_mock_url('volumev3', 'public', - append=['snapshots', snapshot_id]), - json={'snapshot': build_snapshot_dict})]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots'] + ), + json={'snapshot': build_snapshot_dict}, + validate=dict(json={'snapshot': {'volume_id': '1234'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', snapshot_id] + ), + json={'snapshot': build_snapshot_dict}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudTimeout, - self.cloud.create_volume_snapshot, volume_id=volume_id, - wait=True, timeout=0.01) + self.cloud.create_volume_snapshot, + volume_id=volume_id, + wait=True, + timeout=0.01, + ) self.assert_calls(do_count=False) def test_create_volume_snapshot_with_error(self): @@ -106,31 +132,47 @@ class TestCreateVolumeSnapshot(base.TestCase): """ snapshot_id = '5678' volume_id = '1234' - build_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'creating', - 'bar', 'derpysnapshot') + build_snapshot = fakes.FakeVolumeSnapshot( + snapshot_id, 'creating', 'bar', 'derpysnapshot' + ) build_snapshot_dict = meta.obj_to_munch(build_snapshot) - error_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'error', - 'blah', 'derpysnapshot') + error_snapshot = fakes.FakeVolumeSnapshot( + snapshot_id, 'error', 'blah', 'derpysnapshot' + ) error_snapshot_dict = meta.obj_to_munch(error_snapshot) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', append=['snapshots']), - json={'snapshot': build_snapshot_dict}, - validate=dict(json={ - 'snapshot': {'volume_id': '1234'}})), - dict(method='GET', - uri=self.get_mock_url('volumev3', 'public', - append=['snapshots', snapshot_id]), - json={'snapshot': build_snapshot_dict}), - dict(method='GET', - uri=self.get_mock_url('volumev3', 'public', - append=['snapshots', snapshot_id]), - json={'snapshot': error_snapshot_dict})]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots'] + ), + json={'snapshot': build_snapshot_dict}, + validate=dict(json={'snapshot': {'volume_id': '1234'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', snapshot_id] + ), + json={'snapshot': build_snapshot_dict}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', snapshot_id] + ), + json={'snapshot': error_snapshot_dict}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.create_volume_snapshot, volume_id=volume_id, - wait=True, timeout=5) + self.cloud.create_volume_snapshot, + volume_id=volume_id, + wait=True, + timeout=5, + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_delete_server.py b/openstack/tests/unit/cloud/test_delete_server.py index 562e7ffbe..9a508cfab 100644 --- a/openstack/tests/unit/cloud/test_delete_server.py +++ b/openstack/tests/unit/cloud/test_delete_server.py @@ -24,27 +24,39 @@ from openstack.tests.unit import base class TestDeleteServer(base.TestCase): - def test_delete_server(self): """ Test that server delete is called when wait=False """ server = fakes.make_fake_server('1234', 'daffy', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'daffy']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=daffy']), - json={'servers': [server]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234'])), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'daffy'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=daffy'], + ), + json={'servers': [server]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + ), + ] + ) self.assertTrue(self.cloud.delete_server('daffy', wait=False)) self.assert_calls() @@ -53,35 +65,55 @@ class TestDeleteServer(base.TestCase): """ Test that we return immediately when server is already gone """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'tweety']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=tweety']), - json={'servers': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'tweety'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=tweety'], + ), + json={'servers': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_server('tweety', wait=False)) self.assert_calls() def test_delete_server_already_gone_wait(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'speedy']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=speedy']), - json={'servers': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'speedy'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=speedy'], + ), + json={'servers': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_server('speedy', wait=True)) self.assert_calls() @@ -90,29 +122,48 @@ class TestDeleteServer(base.TestCase): Test that delete_server waits for the server to be gone """ server = fakes.make_fake_server('9999', 'wily', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'wily']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=wily']), - json={'servers': [server]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '9999'])), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '9999']), - json={'server': server}), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '9999']), - status_code=404), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'wily'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=wily'], + ), + json={'servers': [server]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '9999'] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '9999'] + ), + json={'server': server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '9999'] + ), + status_code=404, + ), + ] + ) self.assertTrue(self.cloud.delete_server('wily', wait=True)) self.assert_calls() @@ -122,27 +173,42 @@ class TestDeleteServer(base.TestCase): Test that delete_server raises non-404 exceptions """ server = fakes.make_fake_server('1212', 'speedy', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'speedy']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=speedy']), - json={'servers': [server]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1212']), - status_code=400), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'speedy'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=speedy'], + ), + json={'servers': [server]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1212'] + ), + status_code=400, + ), + ] + ) self.assertRaises( shade_exc.OpenStackCloudException, - self.cloud.delete_server, 'speedy', - wait=False) + self.cloud.delete_server, + 'speedy', + wait=False, + ) self.assert_calls() @@ -156,24 +222,38 @@ class TestDeleteServer(base.TestCase): if service_type == 'volume': return False return orig_has_service(service_type) + self.cloud.has_service = fake_has_service server = fakes.make_fake_server('1234', 'porky', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'porky']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=porky']), - json={'servers': [server]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234'])), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'porky'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=porky'], + ), + json={'servers': [server]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + ), + ] + ) self.assertTrue(self.cloud.delete_server('porky', wait=False)) self.assert_calls() @@ -185,50 +265,84 @@ class TestDeleteServer(base.TestCase): server = fakes.make_fake_server('1234', 'porky', 'ACTIVE') fip_id = uuid.uuid4().hex - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'porky']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=porky']), - json={'servers': [server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips'], - qs_elements=['floating_ip_address=172.24.5.5']), - complete_qs=True, - json={'floatingips': [{ - 'router_id': 'd23abc8d-2991-4a55-ba98-2aaea84cc72f', - 'tenant_id': '4969c491a3c74ee4af974e6d800c62de', - 'floating_network_id': '376da547-b977-4cfe-9cba7', - 'fixed_ip_address': '10.0.0.4', - 'floating_ip_address': '172.24.5.5', - 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', - 'id': fip_id, - 'status': 'ACTIVE'}]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips', fip_id])), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - complete_qs=True, - json={'floatingips': []}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234'])), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - status_code=404), - ]) - self.assertTrue(self.cloud.delete_server( - 'porky', wait=True, delete_ips=True)) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'porky'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=porky'], + ), + json={'servers': [server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips'], + qs_elements=['floating_ip_address=172.24.5.5'], + ), + complete_qs=True, + json={ + 'floatingips': [ + { + 'router_id': 'd23abc8d-2991-4a55-ba98-2aaea84cc72f', # noqa: E501 + 'tenant_id': '4969c491a3c74ee4af974e6d800c62de', # noqa: E501 + 'floating_network_id': '376da547-b977-4cfe-9cba7', # noqa: E501 + 'fixed_ip_address': '10.0.0.4', + 'floating_ip_address': '172.24.5.5', + 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', # noqa: E501 + 'id': fip_id, + 'status': 'ACTIVE', + } + ] + }, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips', fip_id], + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + complete_qs=True, + json={'floatingips': []}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + status_code=404, + ), + ] + ) + self.assertTrue( + self.cloud.delete_server('porky', wait=True, delete_ips=True) + ) self.assert_calls() @@ -238,33 +352,55 @@ class TestDeleteServer(base.TestCase): """ server = fakes.make_fake_server('1234', 'porky', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'porky']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=porky']), - json={'servers': [server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips'], - qs_elements=['floating_ip_address=172.24.5.5']), - complete_qs=True, - status_code=404), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234'])), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - status_code=404), - ]) - self.assertTrue(self.cloud.delete_server( - 'porky', wait=True, delete_ips=True)) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'porky'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=porky'], + ), + json={'servers': [server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips'], + qs_elements=['floating_ip_address=172.24.5.5'], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + status_code=404, + ), + ] + ) + self.assertTrue( + self.cloud.delete_server('porky', wait=True, delete_ips=True) + ) self.assert_calls() @@ -275,44 +411,73 @@ class TestDeleteServer(base.TestCase): self.cloud._floating_ip_source = 'nova' server = fakes.make_fake_server('1234', 'porky', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'porky']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=porky']), - json={'servers': [server]}), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-floating-ips']), - json={'floating_ips': [ - { - 'fixed_ip': None, - 'id': 1, - 'instance_id': None, - 'ip': '172.24.5.5', - 'pool': 'nova' - }]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['os-floating-ips', '1'])), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-floating-ips']), - json={'floating_ips': []}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234'])), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', '1234']), - status_code=404), - ]) - self.assertTrue(self.cloud.delete_server( - 'porky', wait=True, delete_ips=True)) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'porky'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=porky'], + ), + json={'servers': [server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-floating-ips'] + ), + json={ + 'floating_ips': [ + { + 'fixed_ip': None, + 'id': 1, + 'instance_id': None, + 'ip': '172.24.5.5', + 'pool': 'nova', + } + ] + }, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['os-floating-ips', '1'] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-floating-ips'] + ), + json={'floating_ips': []}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', '1234'] + ), + status_code=404, + ), + ] + ) + self.assertTrue( + self.cloud.delete_server('porky', wait=True, delete_ips=True) + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_delete_volume_snapshot.py b/openstack/tests/unit/cloud/test_delete_volume_snapshot.py index 1b607d21c..c70f938ee 100644 --- a/openstack/tests/unit/cloud/test_delete_volume_snapshot.py +++ b/openstack/tests/unit/cloud/test_delete_volume_snapshot.py @@ -24,7 +24,6 @@ from openstack.tests.unit import base class TestDeleteVolumeSnapshot(base.TestCase): - def setUp(self): super(TestDeleteVolumeSnapshot, self).setUp() self.use_cinder() @@ -34,23 +33,34 @@ class TestDeleteVolumeSnapshot(base.TestCase): Test that delete_volume_snapshot without a wait returns True instance when the volume snapshot deletes. """ - fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', - 'foo', 'derpysnapshot') + fake_snapshot = fakes.FakeVolumeSnapshot( + '1234', 'available', 'foo', 'derpysnapshot' + ) fake_snapshot_dict = meta.obj_to_munch(fake_snapshot) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', 'detail']), - json={'snapshots': [fake_snapshot_dict]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', fake_snapshot_dict['id']]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', 'detail'] + ), + json={'snapshots': [fake_snapshot_dict]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['snapshots', fake_snapshot_dict['id']], + ), + ), + ] + ) self.assertTrue( - self.cloud.delete_volume_snapshot(name_or_id='1234', wait=False)) + self.cloud.delete_volume_snapshot(name_or_id='1234', wait=False) + ) self.assert_calls() def test_delete_volume_snapshot_with_error(self): @@ -58,24 +68,36 @@ class TestDeleteVolumeSnapshot(base.TestCase): Test that a exception while deleting a volume snapshot will cause an OpenStackCloudException. """ - fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', - 'foo', 'derpysnapshot') + fake_snapshot = fakes.FakeVolumeSnapshot( + '1234', 'available', 'foo', 'derpysnapshot' + ) fake_snapshot_dict = meta.obj_to_munch(fake_snapshot) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', 'detail']), - json={'snapshots': [fake_snapshot_dict]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', fake_snapshot_dict['id']]), - status_code=404)]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', 'detail'] + ), + json={'snapshots': [fake_snapshot_dict]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['snapshots', fake_snapshot_dict['id']], + ), + status_code=404, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.delete_volume_snapshot, name_or_id='1234') + self.cloud.delete_volume_snapshot, + name_or_id='1234', + ) self.assert_calls() def test_delete_volume_snapshot_with_timeout(self): @@ -83,29 +105,43 @@ class TestDeleteVolumeSnapshot(base.TestCase): Test that a timeout while waiting for the volume snapshot to delete raises an exception in delete_volume_snapshot. """ - fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', - 'foo', 'derpysnapshot') + fake_snapshot = fakes.FakeVolumeSnapshot( + '1234', 'available', 'foo', 'derpysnapshot' + ) fake_snapshot_dict = meta.obj_to_munch(fake_snapshot) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', 'detail']), - json={'snapshots': [fake_snapshot_dict]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', fake_snapshot_dict['id']])), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['snapshots', '1234']), - json={'snapshot': fake_snapshot_dict}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', 'detail'] + ), + json={'snapshots': [fake_snapshot_dict]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['snapshots', fake_snapshot_dict['id']], + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['snapshots', '1234'] + ), + json={'snapshot': fake_snapshot_dict}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudTimeout, - self.cloud.delete_volume_snapshot, name_or_id='1234', - wait=True, timeout=0.01) + self.cloud.delete_volume_snapshot, + name_or_id='1234', + wait=True, + timeout=0.01, + ) self.assert_calls(do_count=False) diff --git a/openstack/tests/unit/cloud/test_domain_params.py b/openstack/tests/unit/cloud/test_domain_params.py index 4f5e1ab6d..fd52e89b7 100644 --- a/openstack/tests/unit/cloud/test_domain_params.py +++ b/openstack/tests/unit/cloud/test_domain_params.py @@ -15,17 +15,23 @@ from openstack.tests.unit import base class TestDomainParams(base.TestCase): - def test_identity_params_v3(self): project_data = self._get_project_data(v3=True) - self.register_uris([ - dict(method='GET', - uri='https://identity.example.com/v3/projects', - json=dict(projects=[project_data.json_response['project']])) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://identity.example.com/v3/projects', + json=dict( + projects=[project_data.json_response['project']] + ), + ) + ] + ) ret = self.cloud._get_identity_params( - domain_id='5678', project=project_data.project_name) + domain_id='5678', project=project_data.project_name + ) self.assertIn('default_project_id', ret) self.assertEqual(ret['default_project_id'], project_data.project_id) self.assertIn('domain_id', ret) @@ -39,6 +45,8 @@ class TestDomainParams(base.TestCase): self.assertRaises( exc.OpenStackCloudException, self.cloud._get_identity_params, - domain_id=None, project=project_data.project_name) + domain_id=None, + project=project_data.project_name, + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_domains.py b/openstack/tests/unit/cloud/test_domains.py index 29392e492..824377975 100644 --- a/openstack/tests/unit/cloud/test_domains.py +++ b/openstack/tests/unit/cloud/test_domains.py @@ -23,36 +23,54 @@ from openstack.tests.unit import base class TestDomains(base.TestCase): - - def get_mock_url(self, service_type='identity', - resource='domains', - append=None, base_url_append='v3', - qs_elements=None): + def get_mock_url( + self, + service_type='identity', + resource='domains', + append=None, + base_url_append='v3', + qs_elements=None, + ): return super(TestDomains, self).get_mock_url( - service_type=service_type, resource=resource, - append=append, base_url_append=base_url_append, - qs_elements=qs_elements) + service_type=service_type, + resource=resource, + append=append, + base_url_append=base_url_append, + qs_elements=qs_elements, + ) def test_list_domains(self): domain_data = self._get_domain_data() - self.register_uris([ - dict(method='GET', uri=self.get_mock_url(), status_code=200, - json={'domains': [domain_data.json_response['domain']]})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'domains': [domain_data.json_response['domain']]}, + ) + ] + ) domains = self.cloud.list_domains() self.assertThat(len(domains), matchers.Equals(1)) - self.assertThat(domains[0].name, - matchers.Equals(domain_data.domain_name)) - self.assertThat(domains[0].id, - matchers.Equals(domain_data.domain_id)) + self.assertThat( + domains[0].name, matchers.Equals(domain_data.domain_name) + ) + self.assertThat(domains[0].id, matchers.Equals(domain_data.domain_id)) self.assert_calls() def test_get_domain(self): domain_data = self._get_domain_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(append=[domain_data.domain_id]), - status_code=200, - json=domain_data.json_response)]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[domain_data.domain_id]), + status_code=200, + json=domain_data.json_response, + ) + ] + ) domain = self.cloud.get_domain(domain_id=domain_data.domain_id) self.assertThat(domain.id, matchers.Equals(domain_data.domain_id)) self.assertThat(domain.name, matchers.Equals(domain_data.domain_name)) @@ -61,57 +79,86 @@ class TestDomains(base.TestCase): def test_get_domain_with_name_or_id(self): domain_data = self._get_domain_data() response = {'domains': [domain_data.json_response['domain']]} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(append=[domain_data.domain_id]), - status_code=200, - json=domain_data.json_response), - dict(method='GET', - uri=self.get_mock_url(append=[domain_data.domain_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - qs_elements=['name=' + domain_data.domain_name] - ), - status_code=200, - json=response), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[domain_data.domain_id]), + status_code=200, + json=domain_data.json_response, + ), + dict( + method='GET', + uri=self.get_mock_url(append=[domain_data.domain_name]), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + qs_elements=['name=' + domain_data.domain_name] + ), + status_code=200, + json=response, + ), + ] + ) domain = self.cloud.get_domain(name_or_id=domain_data.domain_id) domain_by_name = self.cloud.get_domain( - name_or_id=domain_data.domain_name) - self.assertThat(domain.id, matchers.Equals(domain_data.domain_id)) - self.assertThat(domain.name, matchers.Equals(domain_data.domain_name)) - self.assertThat(domain_by_name.id, - matchers.Equals(domain_data.domain_id)) - self.assertThat(domain_by_name.name, - matchers.Equals(domain_data.domain_name)) - self.assert_calls() - - def test_create_domain(self): - domain_data = self._get_domain_data(description=uuid.uuid4().hex, - enabled=True) - self.register_uris([ - dict(method='POST', uri=self.get_mock_url(), status_code=200, - json=domain_data.json_response, - validate=dict(json=domain_data.json_request))]) - domain = self.cloud.create_domain( - domain_data.domain_name, domain_data.description) + name_or_id=domain_data.domain_name + ) self.assertThat(domain.id, matchers.Equals(domain_data.domain_id)) self.assertThat(domain.name, matchers.Equals(domain_data.domain_name)) self.assertThat( - domain.description, matchers.Equals(domain_data.description)) + domain_by_name.id, matchers.Equals(domain_data.domain_id) + ) + self.assertThat( + domain_by_name.name, matchers.Equals(domain_data.domain_name) + ) + self.assert_calls() + + def test_create_domain(self): + domain_data = self._get_domain_data( + description=uuid.uuid4().hex, enabled=True + ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=domain_data.json_response, + validate=dict(json=domain_data.json_request), + ) + ] + ) + domain = self.cloud.create_domain( + domain_data.domain_name, domain_data.description + ) + self.assertThat(domain.id, matchers.Equals(domain_data.domain_id)) + self.assertThat(domain.name, matchers.Equals(domain_data.domain_name)) + self.assertThat( + domain.description, matchers.Equals(domain_data.description) + ) self.assert_calls() def test_create_domain_exception(self): - domain_data = self._get_domain_data(domain_name='domain_name', - enabled=True) + domain_data = self._get_domain_data( + domain_name='domain_name', enabled=True + ) with testtools.ExpectedException( openstack.cloud.OpenStackCloudBadRequest ): - self.register_uris([ - dict(method='POST', uri=self.get_mock_url(), status_code=400, - json=domain_data.json_response, - validate=dict(json=domain_data.json_request))]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=400, + json=domain_data.json_response, + validate=dict(json=domain_data.json_request), + ) + ] + ) self.cloud.create_domain('domain_name') self.assert_calls() @@ -120,11 +167,20 @@ class TestDomains(base.TestCase): new_resp = domain_data.json_response.copy() new_resp['domain']['enabled'] = False domain_resource_uri = self.get_mock_url(append=[domain_data.domain_id]) - self.register_uris([ - dict(method='PATCH', uri=domain_resource_uri, status_code=200, - json=new_resp, - validate=dict(json={'domain': {'enabled': False}})), - dict(method='DELETE', uri=domain_resource_uri, status_code=204)]) + self.register_uris( + [ + dict( + method='PATCH', + uri=domain_resource_uri, + status_code=200, + json=new_resp, + validate=dict(json={'domain': {'enabled': False}}), + ), + dict( + method='DELETE', uri=domain_resource_uri, status_code=204 + ), + ] + ) self.cloud.delete_domain(domain_data.domain_id) self.assert_calls() @@ -134,15 +190,26 @@ class TestDomains(base.TestCase): new_resp['domain']['enabled'] = False domain_resource_uri = self.get_mock_url(append=[domain_data.domain_id]) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(append=[domain_data.domain_id]), - status_code=200, - json={'domain': domain_data.json_response['domain']}), - dict(method='PATCH', uri=domain_resource_uri, status_code=200, - json=new_resp, - validate=dict(json={'domain': {'enabled': False}})), - dict(method='DELETE', uri=domain_resource_uri, status_code=204)]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[domain_data.domain_id]), + status_code=200, + json={'domain': domain_data.json_response['domain']}, + ), + dict( + method='PATCH', + uri=domain_resource_uri, + status_code=200, + json=new_resp, + validate=dict(json={'domain': {'enabled': False}}), + ), + dict( + method='DELETE', uri=domain_resource_uri, status_code=204 + ), + ] + ) self.cloud.delete_domain(name_or_id=domain_data.domain_id) self.assert_calls() @@ -156,11 +223,20 @@ class TestDomains(base.TestCase): new_resp = domain_data.json_response.copy() new_resp['domain']['enabled'] = False domain_resource_uri = self.get_mock_url(append=[domain_data.domain_id]) - self.register_uris([ - dict(method='PATCH', uri=domain_resource_uri, status_code=200, - json=new_resp, - validate=dict(json={'domain': {'enabled': False}})), - dict(method='DELETE', uri=domain_resource_uri, status_code=404)]) + self.register_uris( + [ + dict( + method='PATCH', + uri=domain_resource_uri, + status_code=200, + json=new_resp, + validate=dict(json={'domain': {'enabled': False}}), + ), + dict( + method='DELETE', uri=domain_resource_uri, status_code=404 + ), + ] + ) with testtools.ExpectedException( openstack.exceptions.ResourceNotFound ): @@ -169,53 +245,81 @@ class TestDomains(base.TestCase): def test_update_domain(self): domain_data = self._get_domain_data( - description=self.getUniqueString('domainDesc')) + description=self.getUniqueString('domainDesc') + ) domain_resource_uri = self.get_mock_url(append=[domain_data.domain_id]) - self.register_uris([ - dict(method='PATCH', uri=domain_resource_uri, status_code=200, - json=domain_data.json_response, - validate=dict(json=domain_data.json_request))]) + self.register_uris( + [ + dict( + method='PATCH', + uri=domain_resource_uri, + status_code=200, + json=domain_data.json_response, + validate=dict(json=domain_data.json_request), + ) + ] + ) domain = self.cloud.update_domain( domain_data.domain_id, name=domain_data.domain_name, - description=domain_data.description) + description=domain_data.description, + ) self.assertThat(domain.id, matchers.Equals(domain_data.domain_id)) self.assertThat(domain.name, matchers.Equals(domain_data.domain_name)) self.assertThat( - domain.description, matchers.Equals(domain_data.description)) + domain.description, matchers.Equals(domain_data.description) + ) self.assert_calls() def test_update_domain_name_or_id(self): domain_data = self._get_domain_data( - description=self.getUniqueString('domainDesc')) + description=self.getUniqueString('domainDesc') + ) domain_resource_uri = self.get_mock_url(append=[domain_data.domain_id]) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(append=[domain_data.domain_id]), - status_code=200, - json={'domain': domain_data.json_response['domain']}), - dict(method='PATCH', uri=domain_resource_uri, status_code=200, - json=domain_data.json_response, - validate=dict(json=domain_data.json_request))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[domain_data.domain_id]), + status_code=200, + json={'domain': domain_data.json_response['domain']}, + ), + dict( + method='PATCH', + uri=domain_resource_uri, + status_code=200, + json=domain_data.json_response, + validate=dict(json=domain_data.json_request), + ), + ] + ) domain = self.cloud.update_domain( name_or_id=domain_data.domain_id, name=domain_data.domain_name, - description=domain_data.description) + description=domain_data.description, + ) self.assertThat(domain.id, matchers.Equals(domain_data.domain_id)) self.assertThat(domain.name, matchers.Equals(domain_data.domain_name)) self.assertThat( - domain.description, matchers.Equals(domain_data.description)) + domain.description, matchers.Equals(domain_data.description) + ) self.assert_calls() def test_update_domain_exception(self): domain_data = self._get_domain_data( - description=self.getUniqueString('domainDesc')) - self.register_uris([ - dict(method='PATCH', - uri=self.get_mock_url(append=[domain_data.domain_id]), - status_code=409, - json=domain_data.json_response, - validate=dict(json={'domain': {'enabled': False}}))]) + description=self.getUniqueString('domainDesc') + ) + self.register_uris( + [ + dict( + method='PATCH', + uri=self.get_mock_url(append=[domain_data.domain_id]), + status_code=409, + json=domain_data.json_response, + validate=dict(json={'domain': {'enabled': False}}), + ) + ] + ) with testtools.ExpectedException( openstack.exceptions.ConflictException ): diff --git a/openstack/tests/unit/cloud/test_endpoints.py b/openstack/tests/unit/cloud/test_endpoints.py index 476da65a9..6077464b5 100644 --- a/openstack/tests/unit/cloud/test_endpoints.py +++ b/openstack/tests/unit/cloud/test_endpoints.py @@ -27,11 +27,17 @@ from openstack.tests.unit import base class TestCloudEndpoints(base.TestCase): - - def get_mock_url(self, service_type='identity', interface='public', - resource='endpoints', append=None, base_url_append='v3'): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource='endpoints', + append=None, + base_url_append='v3', + ): return super(TestCloudEndpoints, self).get_mock_url( - service_type, interface, resource, append, base_url_append) + service_type, interface, resource, append, base_url_append + ) def _dummy_url(self): return 'https://%s.example.com/' % uuid.uuid4().hex @@ -39,148 +45,207 @@ class TestCloudEndpoints(base.TestCase): def test_create_endpoint_v3(self): service_data = self._get_service_data() public_endpoint_data = self._get_endpoint_v3_data( - service_id=service_data.service_id, interface='public', - url=self._dummy_url()) + service_id=service_data.service_id, + interface='public', + url=self._dummy_url(), + ) public_endpoint_data_disabled = self._get_endpoint_v3_data( - service_id=service_data.service_id, interface='public', - url=self._dummy_url(), enabled=False) + service_id=service_data.service_id, + interface='public', + url=self._dummy_url(), + enabled=False, + ) admin_endpoint_data = self._get_endpoint_v3_data( - service_id=service_data.service_id, interface='admin', - url=self._dummy_url(), region=public_endpoint_data.region_id) + service_id=service_data.service_id, + interface='admin', + url=self._dummy_url(), + region=public_endpoint_data.region_id, + ) internal_endpoint_data = self._get_endpoint_v3_data( - service_id=service_data.service_id, interface='internal', - url=self._dummy_url(), region=public_endpoint_data.region_id) + service_id=service_data.service_id, + interface='internal', + url=self._dummy_url(), + region=public_endpoint_data.region_id, + ) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(resource='services'), - status_code=200, - json={'services': [ - service_data.json_response_v3['service']]}), - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=public_endpoint_data_disabled.json_response, - validate=dict( - json=public_endpoint_data_disabled.json_request)), - dict(method='GET', - uri=self.get_mock_url(resource='services'), - status_code=200, - json={'services': [ - service_data.json_response_v3['service']]}), - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=public_endpoint_data.json_response, - validate=dict(json=public_endpoint_data.json_request)), - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=internal_endpoint_data.json_response, - validate=dict(json=internal_endpoint_data.json_request)), - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=admin_endpoint_data.json_response, - validate=dict(json=admin_endpoint_data.json_request)), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='services'), + status_code=200, + json={ + 'services': [service_data.json_response_v3['service']] + }, + ), + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=public_endpoint_data_disabled.json_response, + validate=dict( + json=public_endpoint_data_disabled.json_request + ), + ), + dict( + method='GET', + uri=self.get_mock_url(resource='services'), + status_code=200, + json={ + 'services': [service_data.json_response_v3['service']] + }, + ), + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=public_endpoint_data.json_response, + validate=dict(json=public_endpoint_data.json_request), + ), + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=internal_endpoint_data.json_response, + validate=dict(json=internal_endpoint_data.json_request), + ), + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=admin_endpoint_data.json_response, + validate=dict(json=admin_endpoint_data.json_request), + ), + ] + ) endpoints = self.cloud.create_endpoint( service_name_or_id=service_data.service_id, region=public_endpoint_data_disabled.region_id, url=public_endpoint_data_disabled.url, interface=public_endpoint_data_disabled.interface, - enabled=False) + enabled=False, + ) # Test endpoint values self.assertThat( endpoints[0].id, - matchers.Equals(public_endpoint_data_disabled.endpoint_id)) - self.assertThat(endpoints[0].url, - matchers.Equals(public_endpoint_data_disabled.url)) + matchers.Equals(public_endpoint_data_disabled.endpoint_id), + ) + self.assertThat( + endpoints[0].url, + matchers.Equals(public_endpoint_data_disabled.url), + ) self.assertThat( endpoints[0].interface, - matchers.Equals(public_endpoint_data_disabled.interface)) + matchers.Equals(public_endpoint_data_disabled.interface), + ) self.assertThat( endpoints[0].region_id, - matchers.Equals(public_endpoint_data_disabled.region_id)) + matchers.Equals(public_endpoint_data_disabled.region_id), + ) self.assertThat( endpoints[0].region_id, - matchers.Equals(public_endpoint_data_disabled.region_id)) - self.assertThat(endpoints[0].is_enabled, - matchers.Equals(public_endpoint_data_disabled.enabled)) + matchers.Equals(public_endpoint_data_disabled.region_id), + ) + self.assertThat( + endpoints[0].is_enabled, + matchers.Equals(public_endpoint_data_disabled.enabled), + ) endpoints_2on3 = self.cloud.create_endpoint( service_name_or_id=service_data.service_id, region=public_endpoint_data.region_id, public_url=public_endpoint_data.url, internal_url=internal_endpoint_data.url, - admin_url=admin_endpoint_data.url) + admin_url=admin_endpoint_data.url, + ) # Three endpoints should be returned, public, internal, and admin self.assertThat(len(endpoints_2on3), matchers.Equals(3)) # test keys and values are correct for each endpoint created for result, reference in zip( - endpoints_2on3, [public_endpoint_data, - internal_endpoint_data, - admin_endpoint_data] + endpoints_2on3, + [ + public_endpoint_data, + internal_endpoint_data, + admin_endpoint_data, + ], ): self.assertThat(result.id, matchers.Equals(reference.endpoint_id)) self.assertThat(result.url, matchers.Equals(reference.url)) - self.assertThat(result.interface, - matchers.Equals(reference.interface)) - self.assertThat(result.region_id, - matchers.Equals(reference.region_id)) - self.assertThat(result.is_enabled, - matchers.Equals(reference.enabled)) + self.assertThat( + result.interface, matchers.Equals(reference.interface) + ) + self.assertThat( + result.region_id, matchers.Equals(reference.region_id) + ) + self.assertThat( + result.is_enabled, matchers.Equals(reference.enabled) + ) self.assert_calls() def test_update_endpoint_v3(self): service_data = self._get_service_data() dummy_url = self._dummy_url() endpoint_data = self._get_endpoint_v3_data( - service_id=service_data.service_id, interface='admin', - enabled=False) + service_id=service_data.service_id, + interface='admin', + enabled=False, + ) reference_request = endpoint_data.json_request.copy() reference_request['endpoint']['url'] = dummy_url - self.register_uris([ - dict(method='PATCH', - uri=self.get_mock_url(append=[endpoint_data.endpoint_id]), - status_code=200, - json=endpoint_data.json_response, - validate=dict(json=reference_request)) - ]) + self.register_uris( + [ + dict( + method='PATCH', + uri=self.get_mock_url(append=[endpoint_data.endpoint_id]), + status_code=200, + json=endpoint_data.json_response, + validate=dict(json=reference_request), + ) + ] + ) endpoint = self.cloud.update_endpoint( endpoint_data.endpoint_id, service_name_or_id=service_data.service_id, region=endpoint_data.region_id, url=dummy_url, interface=endpoint_data.interface, - enabled=False + enabled=False, ) # test keys and values are correct - self.assertThat(endpoint.id, - matchers.Equals(endpoint_data.endpoint_id)) - self.assertThat(endpoint.service_id, - matchers.Equals(service_data.service_id)) - self.assertThat(endpoint.url, - matchers.Equals(endpoint_data.url)) - self.assertThat(endpoint.interface, - matchers.Equals(endpoint_data.interface)) + self.assertThat( + endpoint.id, matchers.Equals(endpoint_data.endpoint_id) + ) + self.assertThat( + endpoint.service_id, matchers.Equals(service_data.service_id) + ) + self.assertThat(endpoint.url, matchers.Equals(endpoint_data.url)) + self.assertThat( + endpoint.interface, matchers.Equals(endpoint_data.interface) + ) self.assert_calls() def test_list_endpoints(self): endpoints_data = [self._get_endpoint_v3_data() for e in range(1, 10)] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'endpoints': [e.json_response['endpoint'] - for e in endpoints_data]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'endpoints': [ + e.json_response['endpoint'] for e in endpoints_data + ] + }, + ) + ] + ) endpoints = self.cloud.list_endpoints() # test we are getting exactly len(self.mock_endpoints) elements @@ -188,58 +253,89 @@ class TestCloudEndpoints(base.TestCase): # test keys and values are correct for i, ep in enumerate(endpoints_data): - self.assertThat(endpoints[i].id, - matchers.Equals(ep.endpoint_id)) - self.assertThat(endpoints[i].service_id, - matchers.Equals(ep.service_id)) - self.assertThat(endpoints[i].url, - matchers.Equals(ep.url)) - self.assertThat(endpoints[i].interface, - matchers.Equals(ep.interface)) + self.assertThat(endpoints[i].id, matchers.Equals(ep.endpoint_id)) + self.assertThat( + endpoints[i].service_id, matchers.Equals(ep.service_id) + ) + self.assertThat(endpoints[i].url, matchers.Equals(ep.url)) + self.assertThat( + endpoints[i].interface, matchers.Equals(ep.interface) + ) self.assert_calls() def test_search_endpoints(self): - endpoints_data = [self._get_endpoint_v3_data(region='region1') - for e in range(0, 2)] - endpoints_data.extend([self._get_endpoint_v3_data() - for e in range(1, 8)]) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'endpoints': [e.json_response['endpoint'] - for e in endpoints_data]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'endpoints': [e.json_response['endpoint'] - for e in endpoints_data]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'endpoints': [e.json_response['endpoint'] - for e in endpoints_data]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'endpoints': [e.json_response['endpoint'] - for e in endpoints_data]}) - ]) + endpoints_data = [ + self._get_endpoint_v3_data(region='region1') for e in range(0, 2) + ] + endpoints_data.extend( + [self._get_endpoint_v3_data() for e in range(1, 8)] + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'endpoints': [ + e.json_response['endpoint'] for e in endpoints_data + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'endpoints': [ + e.json_response['endpoint'] for e in endpoints_data + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'endpoints': [ + e.json_response['endpoint'] for e in endpoints_data + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'endpoints': [ + e.json_response['endpoint'] for e in endpoints_data + ] + }, + ), + ] + ) # Search by id endpoints = self.cloud.search_endpoints( - id=endpoints_data[-1].endpoint_id) + id=endpoints_data[-1].endpoint_id + ) # # test we are getting exactly 1 element self.assertEqual(1, len(endpoints)) - self.assertThat(endpoints[0].id, - matchers.Equals(endpoints_data[-1].endpoint_id)) - self.assertThat(endpoints[0].service_id, - matchers.Equals(endpoints_data[-1].service_id)) - self.assertThat(endpoints[0].url, - matchers.Equals(endpoints_data[-1].url)) - self.assertThat(endpoints[0].interface, - matchers.Equals(endpoints_data[-1].interface)) + self.assertThat( + endpoints[0].id, matchers.Equals(endpoints_data[-1].endpoint_id) + ) + self.assertThat( + endpoints[0].service_id, + matchers.Equals(endpoints_data[-1].service_id), + ) + self.assertThat( + endpoints[0].url, matchers.Equals(endpoints_data[-1].url) + ) + self.assertThat( + endpoints[0].interface, + matchers.Equals(endpoints_data[-1].interface), + ) # Not found endpoints = self.cloud.search_endpoints(id='!invalid!') @@ -247,13 +343,15 @@ class TestCloudEndpoints(base.TestCase): # Multiple matches endpoints = self.cloud.search_endpoints( - filters={'region_id': 'region1'}) + filters={'region_id': 'region1'} + ) # # test we are getting exactly 2 elements self.assertEqual(2, len(endpoints)) # test we are getting the correct response for region/region_id compat endpoints = self.cloud.search_endpoints( - filters={'region_id': 'region1'}) + filters={'region_id': 'region1'} + ) # # test we are getting exactly 2 elements, this is v3 self.assertEqual(2, len(endpoints)) @@ -261,16 +359,23 @@ class TestCloudEndpoints(base.TestCase): def test_delete_endpoint(self): endpoint_data = self._get_endpoint_v3_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'endpoints': [ - endpoint_data.json_response['endpoint']]}), - dict(method='DELETE', - uri=self.get_mock_url(append=[endpoint_data.endpoint_id]), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'endpoints': [endpoint_data.json_response['endpoint']] + }, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[endpoint_data.endpoint_id]), + status_code=204, + ), + ] + ) # Delete by id self.cloud.delete_endpoint(id=endpoint_data.endpoint_id) diff --git a/openstack/tests/unit/cloud/test_flavors.py b/openstack/tests/unit/cloud/test_flavors.py index 9ca45f4ad..98e5c4efd 100644 --- a/openstack/tests/unit/cloud/test_flavors.py +++ b/openstack/tests/unit/cloud/test_flavors.py @@ -17,7 +17,6 @@ from openstack.tests.unit import base class TestFlavors(base.TestCase): - def setUp(self): super(TestFlavors, self).setUp() # self.use_compute_discovery() @@ -25,55 +24,85 @@ class TestFlavors(base.TestCase): def test_create_flavor(self): self.use_compute_discovery() - self.register_uris([ - dict(method='POST', - uri='{endpoint}/flavors'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavor': fakes.FAKE_FLAVOR}, - validate=dict( - json={ - 'flavor': { - "name": "vanilla", - "description": None, - "ram": 65536, - "vcpus": 24, - "swap": 0, - "os-flavor-access:is_public": True, - "rxtx_factor": 1.0, - "OS-FLV-EXT-DATA:ephemeral": 0, - "disk": 1600, - "id": None}}))]) + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/flavors'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavor': fakes.FAKE_FLAVOR}, + validate=dict( + json={ + 'flavor': { + "name": "vanilla", + "description": None, + "ram": 65536, + "vcpus": 24, + "swap": 0, + "os-flavor-access:is_public": True, + "rxtx_factor": 1.0, + "OS-FLV-EXT-DATA:ephemeral": 0, + "disk": 1600, + "id": None, + } + } + ), + ) + ] + ) self.cloud.create_flavor( - 'vanilla', ram=65536, disk=1600, vcpus=24, + 'vanilla', + ram=65536, + disk=1600, + vcpus=24, ) self.assert_calls() def test_delete_flavor(self): self.use_compute_discovery() - self.register_uris([ - dict(method='GET', - uri='{endpoint}/flavors/vanilla'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=fakes.FAKE_FLAVOR), - dict(method='DELETE', - uri='{endpoint}/flavors/{id}'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=fakes.FLAVOR_ID))]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/flavors/vanilla'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=fakes.FAKE_FLAVOR, + ), + dict( + method='DELETE', + uri='{endpoint}/flavors/{id}'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=fakes.FLAVOR_ID + ), + ), + ] + ) self.assertTrue(self.cloud.delete_flavor('vanilla')) self.assert_calls() def test_delete_flavor_not_found(self): self.use_compute_discovery() - self.register_uris([ - dict(method='GET', - uri='{endpoint}/flavors/invalid'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - status_code=404), - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': fakes.FAKE_FLAVOR_LIST})]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/flavors/invalid'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + status_code=404, + ), + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), + ] + ) self.assertFalse(self.cloud.delete_flavor('invalid')) @@ -81,30 +110,48 @@ class TestFlavors(base.TestCase): def test_delete_flavor_exception(self): self.use_compute_discovery() - self.register_uris([ - dict(method='GET', - uri='{endpoint}/flavors/vanilla'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=fakes.FAKE_FLAVOR), - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': fakes.FAKE_FLAVOR_LIST}), - dict(method='DELETE', - uri='{endpoint}/flavors/{id}'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=fakes.FLAVOR_ID), - status_code=503)]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/flavors/vanilla'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=fakes.FAKE_FLAVOR, + ), + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), + dict( + method='DELETE', + uri='{endpoint}/flavors/{id}'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=fakes.FLAVOR_ID + ), + status_code=503, + ), + ] + ) - self.assertRaises(openstack.cloud.OpenStackCloudException, - self.cloud.delete_flavor, 'vanilla') + self.assertRaises( + openstack.cloud.OpenStackCloudException, + self.cloud.delete_flavor, + 'vanilla', + ) def test_list_flavors(self): self.use_compute_discovery() uris_to_mock = [ - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': fakes.FAKE_FLAVOR_LIST}), + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), ] self.register_uris(uris_to_mock) @@ -126,17 +173,26 @@ class TestFlavors(base.TestCase): def test_list_flavors_with_extra(self): self.use_compute_discovery() uris_to_mock = [ - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': fakes.FAKE_FLAVOR_LIST}), + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), ] - uris_to_mock.extend([ - dict(method='GET', - uri='{endpoint}/flavors/{id}/os-extra_specs'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=flavor['id']), - json={'extra_specs': {}}) - for flavor in fakes.FAKE_FLAVOR_LIST]) + uris_to_mock.extend( + [ + dict( + method='GET', + uri='{endpoint}/flavors/{id}/os-extra_specs'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=flavor['id'] + ), + json={'extra_specs': {}}, + ) + for flavor in fakes.FAKE_FLAVOR_LIST + ] + ) self.register_uris(uris_to_mock) flavors = self.cloud.list_flavors(get_extra=True) @@ -157,17 +213,26 @@ class TestFlavors(base.TestCase): def test_get_flavor_by_ram(self): self.use_compute_discovery() uris_to_mock = [ - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': fakes.FAKE_FLAVOR_LIST}), + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), ] - uris_to_mock.extend([ - dict(method='GET', - uri='{endpoint}/flavors/{id}/os-extra_specs'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=flavor['id']), - json={'extra_specs': {}}) - for flavor in fakes.FAKE_FLAVOR_LIST]) + uris_to_mock.extend( + [ + dict( + method='GET', + uri='{endpoint}/flavors/{id}/os-extra_specs'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=flavor['id'] + ), + json={'extra_specs': {}}, + ) + for flavor in fakes.FAKE_FLAVOR_LIST + ] + ) self.register_uris(uris_to_mock) flavor = self.cloud.get_flavor_by_ram(ram=250) @@ -176,47 +241,69 @@ class TestFlavors(base.TestCase): def test_get_flavor_by_ram_and_include(self): self.use_compute_discovery() uris_to_mock = [ - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': fakes.FAKE_FLAVOR_LIST}), + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': fakes.FAKE_FLAVOR_LIST}, + ), ] - uris_to_mock.extend([ - dict(method='GET', - uri='{endpoint}/flavors/{id}/os-extra_specs'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=flavor['id']), - json={'extra_specs': {}}) - for flavor in fakes.FAKE_FLAVOR_LIST]) + uris_to_mock.extend( + [ + dict( + method='GET', + uri='{endpoint}/flavors/{id}/os-extra_specs'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=flavor['id'] + ), + json={'extra_specs': {}}, + ) + for flavor in fakes.FAKE_FLAVOR_LIST + ] + ) self.register_uris(uris_to_mock) flavor = self.cloud.get_flavor_by_ram(ram=150, include='strawberry') self.assertEqual(fakes.STRAWBERRY_FLAVOR_ID, flavor['id']) def test_get_flavor_by_ram_not_found(self): self.use_compute_discovery() - self.register_uris([ - dict(method='GET', - uri='{endpoint}/flavors/detail?is_public=None'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'flavors': []})]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/flavors/detail?is_public=None'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'flavors': []}, + ) + ] + ) self.assertRaises( openstack.cloud.OpenStackCloudException, self.cloud.get_flavor_by_ram, - ram=100) + ram=100, + ) def test_get_flavor_string_and_int(self): self.use_compute_discovery() flavor_resource_uri = '{endpoint}/flavors/1/os-extra_specs'.format( - endpoint=fakes.COMPUTE_ENDPOINT) + endpoint=fakes.COMPUTE_ENDPOINT + ) flavor = fakes.make_fake_flavor('1', 'vanilla') flavor_json = {'extra_specs': {}} - self.register_uris([ - dict(method='GET', - uri='{endpoint}/flavors/1'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=flavor), - dict(method='GET', uri=flavor_resource_uri, json=flavor_json), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/flavors/1'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=flavor, + ), + dict(method='GET', uri=flavor_resource_uri, json=flavor_json), + ] + ) flavor1 = self.cloud.get_flavor('1') self.assertEqual('1', flavor1['id']) @@ -226,11 +313,17 @@ class TestFlavors(base.TestCase): def test_set_flavor_specs(self): self.use_compute_discovery() extra_specs = dict(key1='value1') - self.register_uris([ - dict(method='POST', - uri='{endpoint}/flavors/{id}/os-extra_specs'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=1), - json=dict(extra_specs=extra_specs))]) + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/flavors/{id}/os-extra_specs'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=1 + ), + json=dict(extra_specs=extra_specs), + ) + ] + ) self.cloud.set_flavor_specs(1, extra_specs) self.assert_calls() @@ -238,62 +331,97 @@ class TestFlavors(base.TestCase): def test_unset_flavor_specs(self): self.use_compute_discovery() keys = ['key1', 'key2'] - self.register_uris([ - dict(method='DELETE', - uri='{endpoint}/flavors/{id}/os-extra_specs/{key}'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id=1, key=key)) - for key in keys]) + self.register_uris( + [ + dict( + method='DELETE', + uri='{endpoint}/flavors/{id}/os-extra_specs/{key}'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=1, key=key + ), + ) + for key in keys + ] + ) self.cloud.unset_flavor_specs(1, keys) self.assert_calls() def test_add_flavor_access(self): - self.register_uris([ - dict(method='POST', - uri='{endpoint}/flavors/{id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id='flavor_id'), - json={ - 'flavor_access': [{ - 'flavor_id': 'flavor_id', 'tenant_id': 'tenant_id'}]}, - validate=dict( - json={'addTenantAccess': {'tenant': 'tenant_id'}}))]) + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/flavors/{id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id='flavor_id' + ), + json={ + 'flavor_access': [ + { + 'flavor_id': 'flavor_id', + 'tenant_id': 'tenant_id', + } + ] + }, + validate=dict( + json={'addTenantAccess': {'tenant': 'tenant_id'}} + ), + ) + ] + ) self.cloud.add_flavor_access('flavor_id', 'tenant_id') self.assert_calls() def test_remove_flavor_access(self): - self.register_uris([ - dict(method='POST', - uri='{endpoint}/flavors/{id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, id='flavor_id'), - json={'flavor_access': []}, - validate=dict( - json={'removeTenantAccess': {'tenant': 'tenant_id'}}))]) + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/flavors/{id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id='flavor_id' + ), + json={'flavor_access': []}, + validate=dict( + json={'removeTenantAccess': {'tenant': 'tenant_id'}} + ), + ) + ] + ) self.cloud.remove_flavor_access('flavor_id', 'tenant_id') self.assert_calls() def test_list_flavor_access(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/flavors/vanilla/os-flavor-access'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={ - 'flavor_access': [ - {'flavor_id': 'vanilla', 'tenant_id': 'tenant_id'}]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/flavors/vanilla/os-flavor-access'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={ + 'flavor_access': [ + {'flavor_id': 'vanilla', 'tenant_id': 'tenant_id'} + ] + }, + ) + ] + ) self.cloud.list_flavor_access('vanilla') self.assert_calls() def test_get_flavor_by_id(self): self.use_compute_discovery() flavor_uri = '{endpoint}/flavors/1'.format( - endpoint=fakes.COMPUTE_ENDPOINT) + endpoint=fakes.COMPUTE_ENDPOINT + ) flavor_json = {'flavor': fakes.make_fake_flavor('1', 'vanilla')} - self.register_uris([ - dict(method='GET', uri=flavor_uri, json=flavor_json), - ]) + self.register_uris( + [ + dict(method='GET', uri=flavor_uri, json=flavor_json), + ] + ) flavor1 = self.cloud.get_flavor_by_id('1') self.assertEqual('1', flavor1['id']) @@ -305,16 +433,22 @@ class TestFlavors(base.TestCase): def test_get_flavor_with_extra_specs(self): self.use_compute_discovery() flavor_uri = '{endpoint}/flavors/1'.format( - endpoint=fakes.COMPUTE_ENDPOINT) + endpoint=fakes.COMPUTE_ENDPOINT + ) flavor_extra_uri = '{endpoint}/flavors/1/os-extra_specs'.format( - endpoint=fakes.COMPUTE_ENDPOINT) + endpoint=fakes.COMPUTE_ENDPOINT + ) flavor_json = {'flavor': fakes.make_fake_flavor('1', 'vanilla')} flavor_extra_json = {'extra_specs': {'name': 'test'}} - self.register_uris([ - dict(method='GET', uri=flavor_uri, json=flavor_json), - dict(method='GET', uri=flavor_extra_uri, json=flavor_extra_json), - ]) + self.register_uris( + [ + dict(method='GET', uri=flavor_uri, json=flavor_json), + dict( + method='GET', uri=flavor_extra_uri, json=flavor_extra_json + ), + ] + ) flavor1 = self.cloud.get_flavor_by_id('1', get_extra=True) self.assertEqual('1', flavor1['id']) diff --git a/openstack/tests/unit/cloud/test_floating_ip_common.py b/openstack/tests/unit/cloud/test_floating_ip_common.py index 6687e37da..420af74de 100644 --- a/openstack/tests/unit/cloud/test_floating_ip_common.py +++ b/openstack/tests/unit/cloud/test_floating_ip_common.py @@ -29,16 +29,20 @@ from openstack.tests.unit import base class TestFloatingIP(base.TestCase): - @patch.object(connection.Connection, 'get_floating_ip') @patch.object(connection.Connection, '_attach_ip_to_server') @patch.object(connection.Connection, 'available_floating_ip') def test_add_auto_ip( - self, mock_available_floating_ip, mock_attach_ip_to_server, - mock_get_floating_ip): + self, + mock_available_floating_ip, + mock_attach_ip_to_server, + mock_get_floating_ip, + ): server_dict = fakes.make_fake_server( - server_id='server-id', name='test-server', status="ACTIVE", - addresses={} + server_id='server-id', + name='test-server', + status="ACTIVE", + addresses={}, ) floating_ip_dict = { "id": "this-is-a-floating-ip-id", @@ -47,7 +51,7 @@ class TestFloatingIP(base.TestCase): "floating_ip_address": "203.0.113.29", "network": "this-is-a-net-or-pool-id", "attached": False, - "status": "ACTIVE" + "status": "ACTIVE", } mock_available_floating_ip.return_value = floating_ip_dict @@ -55,51 +59,63 @@ class TestFloatingIP(base.TestCase): self.cloud.add_auto_ip(server=server_dict) mock_attach_ip_to_server.assert_called_with( - timeout=60, wait=False, server=server_dict, - floating_ip=floating_ip_dict, skip_attach=False) + timeout=60, + wait=False, + server=server_dict, + floating_ip=floating_ip_dict, + skip_attach=False, + ) @patch.object(connection.Connection, '_add_ip_from_pool') def test_add_ips_to_server_pool(self, mock_add_ip_from_pool): server_dict = fakes.make_fake_server( - server_id='romeo', name='test-server', status="ACTIVE", - addresses={}) + server_id='romeo', + name='test-server', + status="ACTIVE", + addresses={}, + ) pool = 'nova' self.cloud.add_ips_to_server(server_dict, ip_pool=pool) mock_add_ip_from_pool.assert_called_with( - server_dict, pool, reuse=True, wait=False, timeout=60, - fixed_address=None, nat_destination=None) + server_dict, + pool, + reuse=True, + wait=False, + timeout=60, + fixed_address=None, + nat_destination=None, + ) @patch.object(connection.Connection, 'has_service') @patch.object(connection.Connection, 'get_floating_ip') @patch.object(connection.Connection, '_add_auto_ip') def test_add_ips_to_server_ipv6_only( - self, mock_add_auto_ip, - mock_get_floating_ip, - mock_has_service): + self, mock_add_auto_ip, mock_get_floating_ip, mock_has_service + ): self.cloud._floating_ip_source = None self.cloud.force_ipv4 = False self.cloud._local_ipv6 = True mock_has_service.return_value = False server = fakes.make_fake_server( - server_id='server-id', name='test-server', status="ACTIVE", + server_id='server-id', + name='test-server', + status="ACTIVE", addresses={ - 'private': [{ - 'addr': "10.223.160.141", - 'version': 4 - }], - 'public': [{ - u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42', - u'OS-EXT-IPS:type': u'fixed', - 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", - 'version': 6 - }] - } + 'private': [{'addr': "10.223.160.141", 'version': 4}], + 'public': [ + { + u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42', + u'OS-EXT-IPS:type': u'fixed', + 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", + 'version': 6, + } + ], + }, ) server_dict = meta.add_server_interfaces( - self.cloud, - _server.Server(**server) + self.cloud, _server.Server(**server) ) new_server = self.cloud.add_ips_to_server(server=server_dict) @@ -107,80 +123,79 @@ class TestFloatingIP(base.TestCase): mock_add_auto_ip.assert_not_called() self.assertEqual( new_server['interface_ip'], - '2001:4800:7819:103:be76:4eff:fe05:8525') + '2001:4800:7819:103:be76:4eff:fe05:8525', + ) self.assertEqual(new_server['private_v4'], '10.223.160.141') self.assertEqual(new_server['public_v4'], '') self.assertEqual( - new_server['public_v6'], '2001:4800:7819:103:be76:4eff:fe05:8525') + new_server['public_v6'], '2001:4800:7819:103:be76:4eff:fe05:8525' + ) @patch.object(connection.Connection, 'has_service') @patch.object(connection.Connection, 'get_floating_ip') @patch.object(connection.Connection, '_add_auto_ip') def test_add_ips_to_server_rackspace( - self, mock_add_auto_ip, - mock_get_floating_ip, - mock_has_service): + self, mock_add_auto_ip, mock_get_floating_ip, mock_has_service + ): self.cloud._floating_ip_source = None self.cloud.force_ipv4 = False self.cloud._local_ipv6 = True mock_has_service.return_value = False server = fakes.make_fake_server( - server_id='server-id', name='test-server', status="ACTIVE", + server_id='server-id', + name='test-server', + status="ACTIVE", addresses={ - 'private': [{ - 'addr': "10.223.160.141", - 'version': 4 - }], - 'public': [{ - 'addr': "104.130.246.91", - 'version': 4 - }, { - 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", - 'version': 6 - }] - } + 'private': [{'addr': "10.223.160.141", 'version': 4}], + 'public': [ + {'addr': "104.130.246.91", 'version': 4}, + { + 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", + 'version': 6, + }, + ], + }, ) server_dict = meta.add_server_interfaces( - self.cloud, - _server.Server(**server)) + self.cloud, _server.Server(**server) + ) new_server = self.cloud.add_ips_to_server(server=server_dict) mock_get_floating_ip.assert_not_called() mock_add_auto_ip.assert_not_called() self.assertEqual( new_server['interface_ip'], - '2001:4800:7819:103:be76:4eff:fe05:8525') + '2001:4800:7819:103:be76:4eff:fe05:8525', + ) @patch.object(connection.Connection, 'has_service') @patch.object(connection.Connection, 'get_floating_ip') @patch.object(connection.Connection, '_add_auto_ip') def test_add_ips_to_server_rackspace_local_ipv4( - self, mock_add_auto_ip, - mock_get_floating_ip, - mock_has_service): + self, mock_add_auto_ip, mock_get_floating_ip, mock_has_service + ): self.cloud._floating_ip_source = None self.cloud.force_ipv4 = False self.cloud._local_ipv6 = False mock_has_service.return_value = False server = fakes.make_fake_server( - server_id='server-id', name='test-server', status="ACTIVE", + server_id='server-id', + name='test-server', + status="ACTIVE", addresses={ - 'private': [{ - 'addr': "10.223.160.141", - 'version': 4 - }], - 'public': [{ - 'addr': "104.130.246.91", - 'version': 4 - }, { - 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", - 'version': 6 - }] - } + 'private': [{'addr': "10.223.160.141", 'version': 4}], + 'public': [ + {'addr': "104.130.246.91", 'version': 4}, + { + 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", + 'version': 6, + }, + ], + }, ) server_dict = meta.add_server_interfaces( - self.cloud, - _server.Server(**server)) + self.cloud, _server.Server(**server) + ) new_server = self.cloud.add_ips_to_server(server=server_dict) mock_get_floating_ip.assert_not_called() @@ -190,24 +205,35 @@ class TestFloatingIP(base.TestCase): @patch.object(connection.Connection, 'add_ip_list') def test_add_ips_to_server_ip_list(self, mock_add_ip_list): server_dict = fakes.make_fake_server( - server_id='server-id', name='test-server', status="ACTIVE", - addresses={}) + server_id='server-id', + name='test-server', + status="ACTIVE", + addresses={}, + ) ips = ['203.0.113.29', '172.24.4.229'] self.cloud.add_ips_to_server(server_dict, ips=ips) mock_add_ip_list.assert_called_with( - server_dict, ips, wait=False, timeout=60, + server_dict, + ips, + wait=False, + timeout=60, fixed_address=None, - nat_destination=None) + nat_destination=None, + ) @patch.object(connection.Connection, '_needs_floating_ip') @patch.object(connection.Connection, '_add_auto_ip') def test_add_ips_to_server_auto_ip( - self, mock_add_auto_ip, mock_needs_floating_ip): + self, mock_add_auto_ip, mock_needs_floating_ip + ): server_dict = fakes.make_fake_server( - server_id='server-id', name='test-server', status="ACTIVE", - addresses={}) + server_id='server-id', + name='test-server', + status="ACTIVE", + addresses={}, + ) # TODO(mordred) REMOVE THIS MOCK WHEN THE NEXT PATCH LANDS # SERIOUSLY THIS TIME. NEXT PATCH - WHICH SHOULD ADD MOCKS FOR @@ -218,4 +244,5 @@ class TestFloatingIP(base.TestCase): self.cloud.add_ips_to_server(server_dict) mock_add_auto_ip.assert_called_with( - server_dict, wait=False, timeout=60, reuse=True) + server_dict, wait=False, timeout=60, reuse=True + ) diff --git a/openstack/tests/unit/cloud/test_floating_ip_neutron.py b/openstack/tests/unit/cloud/test_floating_ip_neutron.py index d2fb8710b..9f85c9fab 100644 --- a/openstack/tests/unit/cloud/test_floating_ip_neutron.py +++ b/openstack/tests/unit/cloud/test_floating_ip_neutron.py @@ -39,7 +39,7 @@ class TestFloatingIP(base.TestCase): 'floating_ip_address': '172.24.4.229', 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda7', - 'status': 'ACTIVE' + 'status': 'ACTIVE', }, { 'router_id': None, @@ -49,8 +49,8 @@ class TestFloatingIP(base.TestCase): 'floating_ip_address': '203.0.113.30', 'port_id': None, 'id': '61cea855-49cb-4846-997d-801b70c71bdd', - 'status': 'DOWN' - } + 'status': 'DOWN', + }, ] } @@ -63,7 +63,7 @@ class TestFloatingIP(base.TestCase): 'port_id': None, 'router_id': None, 'status': 'ACTIVE', - 'tenant_id': '4969c491a3c74ee4af974e6d800c62df' + 'tenant_id': '4969c491a3c74ee4af974e6d800c62df', } } @@ -76,15 +76,13 @@ class TestFloatingIP(base.TestCase): 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', 'router_id': None, 'status': 'ACTIVE', - 'tenant_id': '4969c491a3c74ee4af974e6d800c62df' + 'tenant_id': '4969c491a3c74ee4af974e6d800c62df', } } mock_get_network_rep = { 'status': 'ACTIVE', - 'subnets': [ - '54d6f61d-db07-451c-9ab3-b9609b6b6f0b' - ], + 'subnets': ['54d6f61d-db07-451c-9ab3-b9609b6b6f0b'], 'name': 'my-network', 'provider:physical_network': None, 'admin_state_up': True, @@ -93,7 +91,7 @@ class TestFloatingIP(base.TestCase): 'router:external': True, 'shared': True, 'id': 'my-network-id', - 'provider:segmentation_id': None + 'provider:segmentation_id': None, } mock_search_ports_rep = [ @@ -109,7 +107,7 @@ class TestFloatingIP(base.TestCase): 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, - 'ovs_hybrid_plug': True + 'ovs_hybrid_plug': True, }, 'binding:vif_type': 'ovs', 'device_owner': 'compute:None', @@ -119,12 +117,12 @@ class TestFloatingIP(base.TestCase): 'fixed_ips': [ { 'subnet_id': '008ba151-0b8c-4a67-98b5-0d2b87666062', - 'ip_address': u'172.24.4.2' + 'ip_address': '172.24.4.2', } ], 'id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', 'security_groups': [], - 'device_id': 'server-id' + 'device_id': 'server-id', } ] @@ -136,20 +134,32 @@ class TestFloatingIP(base.TestCase): super(TestFloatingIP, self).setUp() self.fake_server = fakes.make_fake_server( - 'server-id', '', 'ACTIVE', - addresses={u'test_pnztt_net': [{ - u'OS-EXT-IPS:type': u'fixed', - u'addr': '192.0.2.129', - u'version': 4, - u'OS-EXT-IPS-MAC:mac_addr': - u'fa:16:3e:ae:7d:42'}]}) + 'server-id', + '', + 'ACTIVE', + addresses={ + 'test_pnztt_net': [ + { + 'OS-EXT-IPS:type': 'fixed', + 'addr': '192.0.2.129', + 'version': 4, + 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:ae:7d:42', + } + ] + }, + ) self.floating_ip = self.mock_floating_ip_list_rep['floatingips'][0] def test_list_floating_ips(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/floatingips', - json=self.mock_floating_ip_list_rep)]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/floatingips', + json=self.mock_floating_ip_list_rep, + ) + ] + ) floating_ips = self.cloud.list_floating_ips() @@ -161,24 +171,37 @@ class TestFloatingIP(base.TestCase): def test_list_floating_ips_with_filters(self): - self.register_uris([ - dict(method='GET', - uri=('https://network.example.com/v2.0/floatingips?' - 'description=42'), - json={'floatingips': []})]) + self.register_uris( + [ + dict( + method='GET', + uri=( + 'https://network.example.com/v2.0/floatingips?' + 'description=42' + ), + json={'floatingips': []}, + ) + ] + ) self.cloud.list_floating_ips(filters={'description': 42}) self.assert_calls() def test_search_floating_ips(self): - self.register_uris([ - dict(method='GET', - uri=('https://network.example.com/v2.0/floatingips'), - json=self.mock_floating_ip_list_rep)]) + self.register_uris( + [ + dict( + method='GET', + uri=('https://network.example.com/v2.0/floatingips'), + json=self.mock_floating_ip_list_rep, + ) + ] + ) floating_ips = self.cloud.search_floating_ips( - filters={'updated_at': 'never'}) + filters={'updated_at': 'never'} + ) self.assertIsInstance(floating_ips, list) self.assertAreInstances(floating_ips, dict) @@ -186,32 +209,43 @@ class TestFloatingIP(base.TestCase): self.assert_calls() def test_get_floating_ip(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/floatingips', - json=self.mock_floating_ip_list_rep)]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/floatingips', + json=self.mock_floating_ip_list_rep, + ) + ] + ) floating_ip = self.cloud.get_floating_ip( - id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') + id='2f245a7b-796b-4f26-9cf9-9e82d248fda7' + ) self.assertIsInstance(floating_ip, dict) self.assertEqual('172.24.4.229', floating_ip['floating_ip_address']) self.assertEqual( self.mock_floating_ip_list_rep['floatingips'][0]['tenant_id'], - floating_ip['project_id'] + floating_ip['project_id'], ) self.assertEqual( self.mock_floating_ip_list_rep['floatingips'][0]['tenant_id'], - floating_ip['tenant_id'] + floating_ip['tenant_id'], ) self.assertIn('location', floating_ip) self.assert_calls() def test_get_floating_ip_not_found(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/floatingips', - json=self.mock_floating_ip_list_rep)]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/floatingips', + json=self.mock_floating_ip_list_rep, + ) + ] + ) floating_ip = self.cloud.get_floating_ip(id='non-existent') @@ -220,11 +254,16 @@ class TestFloatingIP(base.TestCase): def test_get_floating_ip_by_id(self): fid = self.mock_floating_ip_new_rep['floatingip']['id'] - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/floatingips/' - '{id}'.format(id=fid), - json=self.mock_floating_ip_new_rep)]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/floatingips/' + '{id}'.format(id=fid), + json=self.mock_floating_ip_new_rep, + ) + ] + ) floating_ip = self.cloud.get_floating_ip_by_id(id=fid) @@ -232,83 +271,122 @@ class TestFloatingIP(base.TestCase): self.assertEqual('172.24.4.229', floating_ip['floating_ip_address']) self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['tenant_id'], - floating_ip['project_id'] + floating_ip['project_id'], ) self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['tenant_id'], - floating_ip['tenant_id'] + floating_ip['tenant_id'], ) self.assertIn('location', floating_ip) self.assert_calls() def test_create_floating_ip(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks/my-network', - status_code=404), - dict(method='GET', - uri='https://network.example.com/v2.0/networks' - '?name=my-network', - json={'networks': [self.mock_get_network_rep]}), - dict(method='POST', - uri='https://network.example.com/v2.0/floatingips', - json=self.mock_floating_ip_new_rep, - validate=dict( - json={'floatingip': { - 'floating_network_id': 'my-network-id'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks/my-network', + status_code=404, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/networks' + '?name=my-network', + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='POST', + uri='https://network.example.com/v2.0/floatingips', + json=self.mock_floating_ip_new_rep, + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': 'my-network-id' + } + } + ), + ), + ] + ) ip = self.cloud.create_floating_ip(network='my-network') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], - ip['floating_ip_address']) + ip['floating_ip_address'], + ) self.assert_calls() def test_create_floating_ip_port_bad_response(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks/my-network', - json=self.mock_get_network_rep), - dict(method='POST', - uri='https://network.example.com/v2.0/floatingips', - json=self.mock_floating_ip_new_rep, - validate=dict( - json={'floatingip': { - 'floating_network_id': 'my-network-id', - 'port_id': u'ce705c24-c1ef-408a-bda3-7bbd946164ab'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks/my-network', + json=self.mock_get_network_rep, + ), + dict( + method='POST', + uri='https://network.example.com/v2.0/floatingips', + json=self.mock_floating_ip_new_rep, + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': 'my-network-id', + 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ab', # noqa: E501 + } + } + ), + ), + ] + ) # Fails because we requested a port and the returned FIP has no port self.assertRaises( exc.OpenStackCloudException, self.cloud.create_floating_ip, - network='my-network', port='ce705c24-c1ef-408a-bda3-7bbd946164ab') + network='my-network', + port='ce705c24-c1ef-408a-bda3-7bbd946164ab', + ) self.assert_calls() def test_create_floating_ip_port(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks/my-network', - status_code=404), - dict(method='GET', - uri='https://network.example.com/v2.0/networks' - '?name=my-network', - json={'networks': [self.mock_get_network_rep]}), - dict(method='POST', - uri='https://network.example.com/v2.0/floatingips', - json=self.mock_floating_ip_port_rep, - validate=dict( - json={'floatingip': { - 'floating_network_id': 'my-network-id', - 'port_id': u'ce705c24-c1ef-408a-bda3-7bbd946164ac'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks/my-network', + status_code=404, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/networks' + '?name=my-network', + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='POST', + uri='https://network.example.com/v2.0/floatingips', + json=self.mock_floating_ip_port_rep, + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': 'my-network-id', + 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', # noqa: E501 + } + } + ), + ), + ] + ) ip = self.cloud.create_floating_ip( - network='my-network', port='ce705c24-c1ef-408a-bda3-7bbd946164ac') + network='my-network', port='ce705c24-c1ef-408a-bda3-7bbd946164ac' + ) self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], - ip['floating_ip_address']) + ip['floating_ip_address'], + ) self.assert_calls() def test_neutron_available_floating_ips(self): @@ -316,21 +394,37 @@ class TestFloatingIP(base.TestCase): Test without specifying a network name. """ fips_mock_uri = 'https://network.example.com/v2.0/floatingips' - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [self.mock_get_network_rep]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': []}), - dict(method='GET', uri=fips_mock_uri, json={'floatingips': []}), - dict(method='POST', uri=fips_mock_uri, - json=self.mock_floating_ip_new_rep, - validate=dict(json={ - 'floatingip': { - 'floating_network_id': self.mock_get_network_rep['id'] - }})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': []}, + ), + dict( + method='GET', uri=fips_mock_uri, json={'floatingips': []} + ), + dict( + method='POST', + uri=fips_mock_uri, + json=self.mock_floating_ip_new_rep, + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': self.mock_get_network_rep[ # noqa: E501 + 'id' + ] + } + } + ), + ), + ] + ) # Test if first network is selected if no network is given self.cloud._neutron_available_floating_ips() @@ -341,21 +435,37 @@ class TestFloatingIP(base.TestCase): Test with specifying a network name. """ fips_mock_uri = 'https://network.example.com/v2.0/floatingips' - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [self.mock_get_network_rep]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': []}), - dict(method='GET', uri=fips_mock_uri, json={'floatingips': []}), - dict(method='POST', uri=fips_mock_uri, - json=self.mock_floating_ip_new_rep, - validate=dict(json={ - 'floatingip': { - 'floating_network_id': self.mock_get_network_rep['id'] - }})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': []}, + ), + dict( + method='GET', uri=fips_mock_uri, json={'floatingips': []} + ), + dict( + method='POST', + uri=fips_mock_uri, + json=self.mock_floating_ip_new_rep, + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': self.mock_get_network_rep[ # noqa: E501 + 'id' + ] + } + } + ), + ), + ] + ) # Test if first network is selected if no network is given self.cloud._neutron_available_floating_ips( @@ -367,249 +477,342 @@ class TestFloatingIP(base.TestCase): """ Test with an invalid network name. """ - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [self.mock_get_network_rep]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': []}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud._neutron_available_floating_ips, - network='INVALID') + network='INVALID', + ) self.assert_calls() def test_auto_ip_pool_no_reuse(self): # payloads taken from citycloud - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks/ext-net', - status_code=404), - dict(method='GET', - uri='https://network.example.com/v2.0/networks?name=ext-net', - json={"networks": [{ - "status": "ACTIVE", - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "ext-net", - "admin_state_up": True, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa - "mtu": 0, - "is_default": False, - "router:external": True, - "ipv4_address_scope": None, - "shared": False, - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", - "description": None - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/ports' - '?device_id=f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7', - json={"ports": [{ - "status": "ACTIVE", - "created_at": "2017-02-06T20:59:45", - "description": "", - "allowed_address_pairs": [], - "admin_state_up": True, - "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "dns_name": None, - "extra_dhcp_opts": [], - "mac_address": "fa:16:3e:e8:7f:03", - "updated_at": "2017-02-06T20:59:49", - "name": "", - "device_owner": "compute:None", - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "binding:vnic_type": "normal", - "fixed_ips": [{ - "subnet_id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", - "ip_address": "10.4.0.16"}], - "id": "a767944e-057a-47d1-a669-824a21b8fb7b", - "security_groups": [ - "9fb5ba44-5c46-4357-8e60-8b55526cab54"], - "device_id": "f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7", - }]}), - - dict(method='POST', - uri='https://network.example.com/v2.0/floatingips', - json={"floatingip": { - "router_id": "9de9c787-8f89-4a53-8468-a5533d6d7fd1", - "status": "DOWN", - "description": "", - "dns_domain": "", - "floating_network_id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", # noqa - "fixed_ip_address": "10.4.0.16", - "floating_ip_address": "89.40.216.153", - "port_id": "a767944e-057a-47d1-a669-824a21b8fb7b", - "id": "e69179dc-a904-4c9a-a4c9-891e2ecb984c", - "dns_name": "", - "tenant_id": "65222a4d09ea4c68934fa1028c77f394" - }}, - validate=dict(json={"floatingip": { - "floating_network_id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", # noqa - "fixed_ip_address": "10.4.0.16", - "port_id": "a767944e-057a-47d1-a669-824a21b8fb7b", - }})), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri='{endpoint}/servers/detail'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={"servers": [{ - "status": "ACTIVE", - "updated": "2017-02-06T20:59:49Z", - "addresses": { - "private": [{ - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:e8:7f:03", - "version": 4, - "addr": "10.4.0.16", - "OS-EXT-IPS:type": "fixed" - }, { - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:e8:7f:03", - "version": 4, - "addr": "89.40.216.153", - "OS-EXT-IPS:type": "floating" - }]}, - "key_name": None, - "image": {"id": "95e4c449-8abf-486e-97d9-dc3f82417d2d"}, - "OS-EXT-STS:task_state": None, - "OS-EXT-STS:vm_state": "active", - "OS-SRV-USG:launched_at": "2017-02-06T20:59:48.000000", - "flavor": {"id": "2186bd79-a05e-4953-9dde-ddefb63c88d4"}, - "id": "f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7", - "security_groups": [{"name": "default"}], - "OS-SRV-USG:terminated_at": None, - "OS-EXT-AZ:availability_zone": "nova", - "user_id": "c17534835f8f42bf98fc367e0bf35e09", - "name": "testmt", - "created": "2017-02-06T20:59:44Z", - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "OS-DCF:diskConfig": "MANUAL", - "os-extended-volumes:volumes_attached": [], - "accessIPv4": "", - "accessIPv6": "", - "progress": 0, - "OS-EXT-STS:power_state": 1, - "config_drive": "", - "metadata": {} - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={"networks": [{ - "status": "ACTIVE", - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "ext-net", - "admin_state_up": True, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa - "mtu": 0, - "is_default": False, - "router:external": True, - "ipv4_address_scope": None, - "shared": False, - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", - "description": None - }, { - "status": "ACTIVE", - "subnets": ["f0ad1df5-53ee-473f-b86b-3604ea5591e9"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "private", - "admin_state_up": True, - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26", - "tags": [], - "updated_at": "2016-10-22T13:46:26", - "ipv6_address_scope": None, - "router:external": False, - "ipv4_address_scope": None, - "shared": False, - "mtu": 1450, - "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "description": "" - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={"subnets": [{ - "description": "", - "enable_dhcp": True, - "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26", - "dns_nameservers": [ - "89.36.90.101", - "89.36.90.102"], - "updated_at": "2016-10-22T13:46:26", - "gateway_ip": "10.4.0.1", - "ipv6_ra_mode": None, - "allocation_pools": [{ - "start": "10.4.0.2", - "end": "10.4.0.200"}], - "host_routes": [], - "ip_version": 4, - "ipv6_address_mode": None, - "cidr": "10.4.0.0/24", - "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", - "subnetpool_id": None, - "name": "private-subnet-ipv4", - }]})]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks/ext-net', + status_code=404, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/networks?name=ext-net', # noqa: E501 + json={ + "networks": [ + { + "status": "ACTIVE", + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "ext-net", + "admin_state_up": True, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", # noqa: E501 + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 0, + "is_default": False, + "router:external": True, + "ipv4_address_scope": None, + "shared": False, + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", + "description": None, + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/ports' + '?device_id=f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7', + json={ + "ports": [ + { + "status": "ACTIVE", + "created_at": "2017-02-06T20:59:45", + "description": "", + "allowed_address_pairs": [], + "admin_state_up": True, + "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", # noqa: E501 + "dns_name": None, + "extra_dhcp_opts": [], + "mac_address": "fa:16:3e:e8:7f:03", + "updated_at": "2017-02-06T20:59:49", + "name": "", + "device_owner": "compute:None", + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "binding:vnic_type": "normal", + "fixed_ips": [ + { + "subnet_id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", # noqa: E501 + "ip_address": "10.4.0.16", + } + ], + "id": "a767944e-057a-47d1-a669-824a21b8fb7b", + "security_groups": [ + "9fb5ba44-5c46-4357-8e60-8b55526cab54" + ], + "device_id": "f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7", # noqa: E501 + } + ] + }, + ), + dict( + method='POST', + uri='https://network.example.com/v2.0/floatingips', + json={ + "floatingip": { + "router_id": "9de9c787-8f89-4a53-8468-a5533d6d7fd1", # noqa: E501 + "status": "DOWN", + "description": "", + "dns_domain": "", + "floating_network_id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", # noqa: E501 + "fixed_ip_address": "10.4.0.16", + "floating_ip_address": "89.40.216.153", + "port_id": "a767944e-057a-47d1-a669-824a21b8fb7b", + "id": "e69179dc-a904-4c9a-a4c9-891e2ecb984c", + "dns_name": "", + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", + } + }, + validate=dict( + json={ + "floatingip": { + "floating_network_id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", # noqa: E501 + "fixed_ip_address": "10.4.0.16", + "port_id": "a767944e-057a-47d1-a669-824a21b8fb7b", # noqa: E501 + } + } + ), + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri='{endpoint}/servers/detail'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={ + "servers": [ + { + "status": "ACTIVE", + "updated": "2017-02-06T20:59:49Z", + "addresses": { + "private": [ + { + "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:e8:7f:03", # noqa: E501 + "version": 4, + "addr": "10.4.0.16", + "OS-EXT-IPS:type": "fixed", + }, + { + "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:e8:7f:03", # noqa: E501 + "version": 4, + "addr": "89.40.216.153", + "OS-EXT-IPS:type": "floating", + }, + ] + }, + "key_name": None, + "image": { + "id": "95e4c449-8abf-486e-97d9-dc3f82417d2d" # noqa: E501 + }, + "OS-EXT-STS:task_state": None, + "OS-EXT-STS:vm_state": "active", + "OS-SRV-USG:launched_at": "2017-02-06T20:59:48.000000", # noqa: E501 + "flavor": { + "id": "2186bd79-a05e-4953-9dde-ddefb63c88d4" # noqa: E501 + }, + "id": "f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7", + "security_groups": [{"name": "default"}], + "OS-SRV-USG:terminated_at": None, + "OS-EXT-AZ:availability_zone": "nova", + "user_id": "c17534835f8f42bf98fc367e0bf35e09", + "name": "testmt", + "created": "2017-02-06T20:59:44Z", + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "OS-DCF:diskConfig": "MANUAL", + "os-extended-volumes:volumes_attached": [], + "accessIPv4": "", + "accessIPv6": "", + "progress": 0, + "OS-EXT-STS:power_state": 1, + "config_drive": "", + "metadata": {}, + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + "networks": [ + { + "status": "ACTIVE", + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "ext-net", + "admin_state_up": True, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", # noqa: E501 + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 0, + "is_default": False, + "router:external": True, + "ipv4_address_scope": None, + "shared": False, + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", + "description": None, + }, + { + "status": "ACTIVE", + "subnets": [ + "f0ad1df5-53ee-473f-b86b-3604ea5591e9" + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "private", + "admin_state_up": True, + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "created_at": "2016-10-22T13:46:26", + "tags": [], + "updated_at": "2016-10-22T13:46:26", + "ipv6_address_scope": None, + "router:external": False, + "ipv4_address_scope": None, + "shared": False, + "mtu": 1450, + "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", + "description": "", + }, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={ + "subnets": [ + { + "description": "", + "enable_dhcp": True, + "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", # noqa: E501 + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "created_at": "2016-10-22T13:46:26", + "dns_nameservers": [ + "89.36.90.101", + "89.36.90.102", + ], + "updated_at": "2016-10-22T13:46:26", + "gateway_ip": "10.4.0.1", + "ipv6_ra_mode": None, + "allocation_pools": [ + {"start": "10.4.0.2", "end": "10.4.0.200"} + ], + "host_routes": [], + "ip_version": 4, + "ipv6_address_mode": None, + "cidr": "10.4.0.0/24", + "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", + "subnetpool_id": None, + "name": "private-subnet-ipv4", + } + ] + }, + ), + ] + ) self.cloud.add_ips_to_server( utils.Munch( id='f80e3ad0-e13e-41d4-8e9c-be79bccdb8f7', addresses={ - "private": [{ - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:e8:7f:03", - "version": 4, - "addr": "10.4.0.16", - "OS-EXT-IPS:type": "fixed" - }]}), - ip_pool='ext-net', reuse=False) + "private": [ + { + "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:e8:7f:03", + "version": 4, + "addr": "10.4.0.16", + "OS-EXT-IPS:type": "fixed", + } + ] + }, + ), + ip_pool='ext-net', + reuse=False, + ) self.assert_calls() def test_available_floating_ip_new(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [self.mock_get_network_rep]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': []}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - validate=dict( - json={'floatingip': { - 'floating_network_id': 'my-network-id'}}), - json=self.mock_floating_ip_new_rep) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': 'my-network-id' + } + } + ), + json=self.mock_floating_ip_new_rep, + ), + ] + ) ip = self.cloud.available_floating_ip(network='my-network') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], - ip['floating_ip_address']) + ip['floating_ip_address'], + ) self.assert_calls() def test_delete_floating_ip_existing(self): @@ -619,38 +822,62 @@ class TestFloatingIP(base.TestCase): 'floating_ip_address': '172.99.106.167', 'status': 'ACTIVE', } - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fake_fip]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fake_fip]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': []}), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fake_fip]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fake_fip]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': []}, + ), + ] + ) self.assertTrue( - self.cloud.delete_floating_ip(floating_ip_id=fip_id, retry=2)) + self.cloud.delete_floating_ip(floating_ip_id=fip_id, retry=2) + ) self.assert_calls() def test_delete_floating_ip_existing_down(self): @@ -665,29 +892,46 @@ class TestFloatingIP(base.TestCase): 'floating_ip_address': '172.99.106.167', 'status': 'DOWN', } - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fake_fip]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [down_fip]}), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fake_fip]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [down_fip]}, + ), + ] + ) self.assertTrue( - self.cloud.delete_floating_ip(floating_ip_id=fip_id, retry=2)) + self.cloud.delete_floating_ip(floating_ip_id=fip_id, retry=2) + ) self.assert_calls() def test_delete_floating_ip_existing_no_delete(self): @@ -697,50 +941,81 @@ class TestFloatingIP(base.TestCase): 'floating_ip_address': '172.99.106.167', 'status': 'ACTIVE', } - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fake_fip]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fake_fip]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format(fip_id)]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fake_fip]}), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fake_fip]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fake_fip]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip_id)], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fake_fip]}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.delete_floating_ip, - floating_ip_id=fip_id, retry=2) + floating_ip_id=fip_id, + retry=2, + ) self.assert_calls() def test_delete_floating_ip_not_found(self): - self.register_uris([ - dict(method='DELETE', - uri=('https://network.example.com/v2.0/floatingips/' - 'a-wild-id-appears'), - status_code=404)]) + self.register_uris( + [ + dict( + method='DELETE', + uri=( + 'https://network.example.com/v2.0/floatingips/' + 'a-wild-id-appears' + ), + status_code=404, + ) + ] + ) - ret = self.cloud.delete_floating_ip( - floating_ip_id='a-wild-id-appears') + ret = self.cloud.delete_floating_ip(floating_ip_id='a-wild-id-appears') self.assertFalse(ret) self.assert_calls() @@ -750,52 +1025,78 @@ class TestFloatingIP(base.TestCase): fip.update({'status': 'DOWN', 'port_id': None, 'router_id': None}) device_id = self.fake_server['id'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=["device_id={0}".format(device_id)]), - json={'ports': self.mock_search_ports_rep}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format( - fip['id'])]), - json={'floatingip': - self.mock_floating_ip_list_rep['floatingips'][0]}, - validate=dict( - json={'floatingip': { - 'port_id': self.mock_search_ports_rep[0]['id'], - 'fixed_ip_address': self.mock_search_ports_rep[0][ - 'fixed_ips'][0]['ip_address']}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=["device_id={0}".format(device_id)], + ), + json={'ports': self.mock_search_ports_rep}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip['id'])], + ), + json={ + 'floatingip': self.mock_floating_ip_list_rep[ + 'floatingips' + ][0] + }, + validate=dict( + json={ + 'floatingip': { + 'port_id': self.mock_search_ports_rep[0]['id'], + 'fixed_ip_address': self.mock_search_ports_rep[ + 0 + ]['fixed_ips'][0]['ip_address'], + } + } + ), + ), + ] + ) self.cloud._attach_ip_to_server( server=self.fake_server, - floating_ip=self.cloud._normalize_floating_ip(fip)) + floating_ip=self.cloud._normalize_floating_ip(fip), + ) self.assert_calls() def test_detach_ip_from_server(self): fip = self.mock_floating_ip_new_rep['floatingip'] attached_fip = copy.copy(fip) attached_fip['port_id'] = 'server-port-id' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [attached_fip]}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format( - fip['id'])]), - json={'floatingip': fip}, - validate=dict( - json={'floatingip': {'port_id': None}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [attached_fip]}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip['id'])], + ), + json={'floatingip': fip}, + validate=dict(json={'floatingip': {'port_id': None}}), + ), + ] + ) self.cloud.detach_ip_from_server( - server_id='server-id', - floating_ip_id=fip['id']) + server_id='server-id', floating_ip_id=fip['id'] + ) self.assert_calls() def test_add_ip_from_pool(self): @@ -803,106 +1104,165 @@ class TestFloatingIP(base.TestCase): fip = self.mock_floating_ip_new_rep['floatingip'] fixed_ip = self.mock_search_ports_rep[0]['fixed_ips'][0]['ip_address'] port_id = self.mock_search_ports_rep[0]['id'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [network]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [fip]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingip': fip}, - validate=dict( - json={'floatingip': { - 'floating_network_id': network['id']}})), - dict(method="GET", - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=[ - "device_id={0}".format(self.fake_server['id'])]), - json={'ports': self.mock_search_ports_rep}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format( - fip['id'])]), - json={'floatingip': fip}, - validate=dict( - json={'floatingip': { - 'fixed_ip_address': fixed_ip, - 'port_id': port_id}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [network]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [fip]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingip': fip}, + validate=dict( + json={ + 'floatingip': { + 'floating_network_id': network['id'] + } + } + ), + ), + dict( + method="GET", + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=[ + "device_id={0}".format(self.fake_server['id']) + ], + ), + json={'ports': self.mock_search_ports_rep}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'floatingips/{0}'.format(fip['id'])], + ), + json={'floatingip': fip}, + validate=dict( + json={ + 'floatingip': { + 'fixed_ip_address': fixed_ip, + 'port_id': port_id, + } + } + ), + ), + ] + ) server = self.cloud._add_ip_from_pool( server=self.fake_server, network=network['id'], - fixed_address=fixed_ip) + fixed_address=fixed_ip, + ) self.assertEqual(server, self.fake_server) self.assert_calls() def test_cleanup_floating_ips(self): - floating_ips = [{ - "id": "this-is-a-floating-ip-id", - "fixed_ip_address": None, - "internal_network": None, - "floating_ip_address": "203.0.113.29", - "network": "this-is-a-net-or-pool-id", - "port_id": None, - "status": "ACTIVE" - }, { - "id": "this-is-a-second-floating-ip-id", - "fixed_ip_address": None, - "internal_network": None, - "floating_ip_address": "203.0.113.30", - "network": "this-is-a-net-or-pool-id", - "port_id": None, - "status": "ACTIVE" - }, { - "id": "this-is-an-attached-floating-ip-id", - "fixed_ip_address": None, - "internal_network": None, - "floating_ip_address": "203.0.113.29", - "network": "this-is-a-net-or-pool-id", - "attached": True, - "port_id": "this-is-id-of-port-with-fip", - "status": "ACTIVE" - }] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': floating_ips}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format( - floating_ips[0]['id'])]), - json={}), - # First IP has been deleted now, return just the second - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': floating_ips[1:]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'floatingips/{0}'.format( - floating_ips[1]['id'])]), - json={}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingips': [floating_ips[2]]}), - ]) + floating_ips = [ + { + "id": "this-is-a-floating-ip-id", + "fixed_ip_address": None, + "internal_network": None, + "floating_ip_address": "203.0.113.29", + "network": "this-is-a-net-or-pool-id", + "port_id": None, + "status": "ACTIVE", + }, + { + "id": "this-is-a-second-floating-ip-id", + "fixed_ip_address": None, + "internal_network": None, + "floating_ip_address": "203.0.113.30", + "network": "this-is-a-net-or-pool-id", + "port_id": None, + "status": "ACTIVE", + }, + { + "id": "this-is-an-attached-floating-ip-id", + "fixed_ip_address": None, + "internal_network": None, + "floating_ip_address": "203.0.113.29", + "network": "this-is-a-net-or-pool-id", + "attached": True, + "port_id": "this-is-id-of-port-with-fip", + "status": "ACTIVE", + }, + ] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': floating_ips}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'floatingips/{0}'.format(floating_ips[0]['id']), + ], + ), + json={}, + ), + # First IP has been deleted now, return just the second + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': floating_ips[1:]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'floatingips/{0}'.format(floating_ips[1]['id']), + ], + ), + json={}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingips': [floating_ips[2]]}, + ), + ] + ) cleaned_up = self.cloud.delete_unattached_floating_ips() self.assertEqual(cleaned_up, 2) self.assert_calls() @@ -913,135 +1273,166 @@ class TestFloatingIP(base.TestCase): "device_id": "some-server", 'created_at': datetime.datetime.now().isoformat(), 'fixed_ips': [ - { - 'subnet_id': 'subnet-id', - 'ip_address': '172.24.4.2' - } + {'subnet_id': 'subnet-id', 'ip_address': '172.24.4.2'} ], } - floating_ip = { - "id": "floating-ip-id", - "port_id": None - } - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [self.mock_get_network_rep]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnets': []}), - dict(method="GET", - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['device_id=some-server']), - json={'ports': [server_port]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'floatingips']), - json={'floatingip': floating_ip}) - ]) + floating_ip = {"id": "floating-ip-id", "port_id": None} + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [self.mock_get_network_rep]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnets': []}, + ), + dict( + method="GET", + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['device_id=some-server'], + ), + json={'ports': [server_port]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips'] + ), + json={'floatingip': floating_ip}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud._neutron_create_floating_ip, - server=dict(id='some-server')) + server=dict(id='some-server'), + ) self.assert_calls() def test_find_nat_source_inferred(self): # payloads contrived but based on ones from citycloud - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={"networks": [{ - "status": "ACTIVE", - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "ext-net", - "admin_state_up": True, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa - "mtu": 0, - "is_default": False, - "router:external": True, - "ipv4_address_scope": None, - "shared": False, - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", - "description": None - }, { - "status": "ACTIVE", - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "my-network", - "admin_state_up": True, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa - "mtu": 0, - "is_default": False, - "router:external": True, - "ipv4_address_scope": None, - "shared": False, - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebg", - "description": None - }, { - "status": "ACTIVE", - "subnets": ["f0ad1df5-53ee-473f-b86b-3604ea5591e9"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "private", - "admin_state_up": True, - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26", - "tags": [], - "updated_at": "2016-10-22T13:46:26", - "ipv6_address_scope": None, - "router:external": False, - "ipv4_address_scope": None, - "shared": False, - "mtu": 1450, - "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "description": "" - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={"subnets": [{ - "description": "", - "enable_dhcp": True, - "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26", - "dns_nameservers": [ - "89.36.90.101", - "89.36.90.102"], - "updated_at": "2016-10-22T13:46:26", - "gateway_ip": "10.4.0.1", - "ipv6_ra_mode": None, - "allocation_pools": [{ - "start": "10.4.0.2", - "end": "10.4.0.200"}], - "host_routes": [], - "ip_version": 4, - "ipv6_address_mode": None, - "cidr": "10.4.0.0/24", - "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", - "subnetpool_id": None, - "name": "private-subnet-ipv4", - }]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + "networks": [ + { + "status": "ACTIVE", + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "ext-net", + "admin_state_up": True, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", # noqa: E501 + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 0, + "is_default": False, + "router:external": True, + "ipv4_address_scope": None, + "shared": False, + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", + "description": None, + }, + { + "status": "ACTIVE", + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "my-network", + "admin_state_up": True, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", # noqa: E501 + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 0, + "is_default": False, + "router:external": True, + "ipv4_address_scope": None, + "shared": False, + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebg", + "description": None, + }, + { + "status": "ACTIVE", + "subnets": [ + "f0ad1df5-53ee-473f-b86b-3604ea5591e9" + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "private", + "admin_state_up": True, + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "created_at": "2016-10-22T13:46:26", + "tags": [], + "updated_at": "2016-10-22T13:46:26", + "ipv6_address_scope": None, + "router:external": False, + "ipv4_address_scope": None, + "shared": False, + "mtu": 1450, + "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", + "description": "", + }, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={ + "subnets": [ + { + "description": "", + "enable_dhcp": True, + "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", # noqa: E501 + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "created_at": "2016-10-22T13:46:26", + "dns_nameservers": [ + "89.36.90.101", + "89.36.90.102", + ], + "updated_at": "2016-10-22T13:46:26", + "gateway_ip": "10.4.0.1", + "ipv6_ra_mode": None, + "allocation_pools": [ + {"start": "10.4.0.2", "end": "10.4.0.200"} + ], + "host_routes": [], + "ip_version": 4, + "ipv6_address_mode": None, + "cidr": "10.4.0.0/24", + "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", + "subnetpool_id": None, + "name": "private-subnet-ipv4", + } + ] + }, + ), + ] + ) - self.assertEqual( - 'ext-net', self.cloud.get_nat_source()['name']) + self.assertEqual('ext-net', self.cloud.get_nat_source()['name']) self.assert_calls() @@ -1049,96 +1440,116 @@ class TestFloatingIP(base.TestCase): self.cloud._nat_source = 'my-network' # payloads contrived but based on ones from citycloud - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={"networks": [{ - "status": "ACTIVE", - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "ext-net", - "admin_state_up": True, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa - "mtu": 0, - "is_default": False, - "router:external": True, - "ipv4_address_scope": None, - "shared": False, - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", - "description": None - }, { - "status": "ACTIVE", - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "my-network", - "admin_state_up": True, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa - "mtu": 0, - "is_default": False, - "router:external": True, - "ipv4_address_scope": None, - "shared": False, - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebg", - "description": None - }, { - "status": "ACTIVE", - "subnets": ["f0ad1df5-53ee-473f-b86b-3604ea5591e9"], - "availability_zone_hints": [], - "availability_zones": ["nova"], - "name": "private", - "admin_state_up": True, - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26", - "tags": [], - "updated_at": "2016-10-22T13:46:26", - "ipv6_address_scope": None, - "router:external": False, - "ipv4_address_scope": None, - "shared": False, - "mtu": 1450, - "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "description": "" - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={"subnets": [{ - "description": "", - "enable_dhcp": True, - "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26", - "dns_nameservers": [ - "89.36.90.101", - "89.36.90.102"], - "updated_at": "2016-10-22T13:46:26", - "gateway_ip": "10.4.0.1", - "ipv6_ra_mode": None, - "allocation_pools": [{ - "start": "10.4.0.2", - "end": "10.4.0.200"}], - "host_routes": [], - "ip_version": 4, - "ipv6_address_mode": None, - "cidr": "10.4.0.0/24", - "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", - "subnetpool_id": None, - "name": "private-subnet-ipv4", - }]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + "networks": [ + { + "status": "ACTIVE", + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "ext-net", + "admin_state_up": True, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", # noqa: E501 + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 0, + "is_default": False, + "router:external": True, + "ipv4_address_scope": None, + "shared": False, + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", + "description": None, + }, + { + "status": "ACTIVE", + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "my-network", + "admin_state_up": True, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", # noqa: E501 + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 0, + "is_default": False, + "router:external": True, + "ipv4_address_scope": None, + "shared": False, + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebg", + "description": None, + }, + { + "status": "ACTIVE", + "subnets": [ + "f0ad1df5-53ee-473f-b86b-3604ea5591e9" + ], + "availability_zone_hints": [], + "availability_zones": ["nova"], + "name": "private", + "admin_state_up": True, + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "created_at": "2016-10-22T13:46:26", + "tags": [], + "updated_at": "2016-10-22T13:46:26", + "ipv6_address_scope": None, + "router:external": False, + "ipv4_address_scope": None, + "shared": False, + "mtu": 1450, + "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", + "description": "", + }, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={ + "subnets": [ + { + "description": "", + "enable_dhcp": True, + "network_id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", # noqa: E501 + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", # noqa: E501 + "created_at": "2016-10-22T13:46:26", + "dns_nameservers": [ + "89.36.90.101", + "89.36.90.102", + ], + "updated_at": "2016-10-22T13:46:26", + "gateway_ip": "10.4.0.1", + "ipv6_ra_mode": None, + "allocation_pools": [ + {"start": "10.4.0.2", "end": "10.4.0.200"} + ], + "host_routes": [], + "ip_version": 4, + "ipv6_address_mode": None, + "cidr": "10.4.0.0/24", + "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", + "subnetpool_id": None, + "name": "private-subnet-ipv4", + } + ] + }, + ), + ] + ) - self.assertEqual( - 'my-network', self.cloud.get_nat_source()['name']) + self.assertEqual('my-network', self.cloud.get_nat_source()['name']) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_floating_ip_nova.py b/openstack/tests/unit/cloud/test_floating_ip_nova.py index 7d49e19d8..406d504f6 100644 --- a/openstack/tests/unit/cloud/test_floating_ip_nova.py +++ b/openstack/tests/unit/cloud/test_floating_ip_nova.py @@ -28,6 +28,7 @@ def get_fake_has_service(has_service): if s == 'network': return False return has_service(s) + return fake_has_service @@ -38,27 +39,28 @@ class TestFloatingIP(base.TestCase): 'id': 1, 'instance_id': None, 'ip': '203.0.113.1', - 'pool': 'nova' + 'pool': 'nova', }, { 'fixed_ip': None, 'id': 2, 'instance_id': None, 'ip': '203.0.113.2', - 'pool': 'nova' + 'pool': 'nova', }, { 'fixed_ip': '192.0.2.3', 'id': 29, 'instance_id': 'myself', 'ip': '198.51.100.29', - 'pool': 'black_hole' - } + 'pool': 'black_hole', + }, ] mock_floating_ip_pools = [ {'id': 'pool1_id', 'name': 'nova'}, - {'id': 'pool2_id', 'name': 'pool2'}] + {'id': 'pool2_id', 'name': 'pool2'}, + ] def assertAreInstances(self, elements, elem_type): for e in elements: @@ -68,23 +70,36 @@ class TestFloatingIP(base.TestCase): super(TestFloatingIP, self).setUp() self.fake_server = fakes.make_fake_server( - 'server-id', '', 'ACTIVE', - addresses={u'test_pnztt_net': [{ - u'OS-EXT-IPS:type': u'fixed', - u'addr': '192.0.2.129', - u'version': 4, - u'OS-EXT-IPS-MAC:mac_addr': - u'fa:16:3e:ae:7d:42'}]}) + 'server-id', + '', + 'ACTIVE', + addresses={ + u'test_pnztt_net': [ + { + u'OS-EXT-IPS:type': u'fixed', + u'addr': '192.0.2.129', + u'version': 4, + u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42', + } + ] + }, + ) self.cloud.has_service = get_fake_has_service(self.cloud.has_service) def test_list_floating_ips(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + ] + ) floating_ips = self.cloud.list_floating_ips() self.assertIsInstance(floating_ips, list) @@ -95,19 +110,28 @@ class TestFloatingIP(base.TestCase): def test_list_floating_ips_with_filters(self): self.assertRaisesRegex( - ValueError, "Nova-network don't support server-side", - self.cloud.list_floating_ips, filters={'Foo': 42} + ValueError, + "Nova-network don't support server-side", + self.cloud.list_floating_ips, + filters={'Foo': 42}, ) def test_search_floating_ips(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + ] + ) floating_ips = self.cloud.search_floating_ips( - filters={'attached': False}) + filters={'attached': False} + ) self.assertIsInstance(floating_ips, list) self.assertEqual(2, len(floating_ips)) @@ -116,11 +140,17 @@ class TestFloatingIP(base.TestCase): self.assert_calls() def test_get_floating_ip(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + ] + ) floating_ip = self.cloud.get_floating_ip(id='29') @@ -130,11 +160,17 @@ class TestFloatingIP(base.TestCase): self.assert_calls() def test_get_floating_ip_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + ] + ) floating_ip = self.cloud.get_floating_ip(id='666') @@ -143,12 +179,17 @@ class TestFloatingIP(base.TestCase): self.assert_calls() def test_get_floating_ip_by_id(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips', - '1']), - json={'floating_ip': self.mock_floating_ip_list_rep[0]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips', '1'] + ), + json={'floating_ip': self.mock_floating_ip_list_rep[0]}, + ), + ] + ) floating_ip = self.cloud.get_floating_ip_by_id(id='1') @@ -157,161 +198,240 @@ class TestFloatingIP(base.TestCase): self.assert_calls() def test_create_floating_ip(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ip': self.mock_floating_ip_list_rep[1]}, - validate=dict( - json={'pool': 'nova'})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', - append=['os-floating-ips', '2']), - json={'floating_ip': self.mock_floating_ip_list_rep[1]}), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ip': self.mock_floating_ip_list_rep[1]}, + validate=dict(json={'pool': 'nova'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips', '2'] + ), + json={'floating_ip': self.mock_floating_ip_list_rep[1]}, + ), + ] + ) self.cloud.create_floating_ip(network='nova') self.assert_calls() def test_available_floating_ip_existing(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep[:1]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep[:1]}, + ), + ] + ) ip = self.cloud.available_floating_ip(network='nova') - self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], - ip['floating_ip_address']) + self.assertEqual( + self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address'] + ) self.assert_calls() def test_available_floating_ip_new(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': []}), - dict(method='POST', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ip': self.mock_floating_ip_list_rep[0]}, - validate=dict( - json={'pool': 'nova'})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', - append=['os-floating-ips', '1']), - json={'floating_ip': self.mock_floating_ip_list_rep[0]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ip': self.mock_floating_ip_list_rep[0]}, + validate=dict(json={'pool': 'nova'}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips', '1'] + ), + json={'floating_ip': self.mock_floating_ip_list_rep[0]}, + ), + ] + ) ip = self.cloud.available_floating_ip(network='nova') - self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], - ip['floating_ip_address']) + self.assertEqual( + self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address'] + ) self.assert_calls() def test_delete_floating_ip_existing(self): - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', - append=['os-floating-ips', 'a-wild-id-appears'])), - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': []}), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + append=['os-floating-ips', 'a-wild-id-appears'], + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': []}, + ), + ] + ) - ret = self.cloud.delete_floating_ip( - floating_ip_id='a-wild-id-appears') + ret = self.cloud.delete_floating_ip(floating_ip_id='a-wild-id-appears') self.assertTrue(ret) self.assert_calls() def test_delete_floating_ip_not_found(self): - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', - append=['os-floating-ips', 'a-wild-id-appears']), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + append=['os-floating-ips', 'a-wild-id-appears'], + ), + status_code=404, + ), + ] + ) - ret = self.cloud.delete_floating_ip( - floating_ip_id='a-wild-id-appears') + ret = self.cloud.delete_floating_ip(floating_ip_id='a-wild-id-appears') self.assertFalse(ret) self.assert_calls() def test_attach_ip_to_server(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', - append=['servers', self.fake_server['id'], 'action']), - validate=dict( - json={ - "addFloatingIp": { - "address": "203.0.113.1", - "fixed_address": "192.0.2.129", - }})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + append=['servers', self.fake_server['id'], 'action'], + ), + validate=dict( + json={ + "addFloatingIp": { + "address": "203.0.113.1", + "fixed_address": "192.0.2.129", + } + } + ), + ), + ] + ) self.cloud._attach_ip_to_server( server=self.fake_server, floating_ip=self.cloud._normalize_floating_ip( - self.mock_floating_ip_list_rep[0]), - fixed_address='192.0.2.129') + self.mock_floating_ip_list_rep[0] + ), + fixed_address='192.0.2.129', + ) self.assert_calls() def test_detach_ip_from_server(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', - append=['servers', self.fake_server['id'], 'action']), - validate=dict( - json={ - "removeFloatingIp": { - "address": "203.0.113.1", - }})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + append=['servers', self.fake_server['id'], 'action'], + ), + validate=dict( + json={ + "removeFloatingIp": { + "address": "203.0.113.1", + } + } + ), + ), + ] + ) self.cloud.detach_ip_from_server( - server_id='server-id', floating_ip_id=1) + server_id='server-id', floating_ip_id=1 + ) self.assert_calls() def test_add_ip_from_pool(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - dict(method='GET', - uri=self.get_mock_url('compute', append=['os-floating-ips']), - json={'floating_ips': self.mock_floating_ip_list_rep}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', - append=['servers', self.fake_server['id'], 'action']), - validate=dict( - json={ - "addFloatingIp": { - "address": "203.0.113.1", - "fixed_address": "192.0.2.129", - }})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', append=['os-floating-ips'] + ), + json={'floating_ips': self.mock_floating_ip_list_rep}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + append=['servers', self.fake_server['id'], 'action'], + ), + validate=dict( + json={ + "addFloatingIp": { + "address": "203.0.113.1", + "fixed_address": "192.0.2.129", + } + } + ), + ), + ] + ) server = self.cloud._add_ip_from_pool( server=self.fake_server, network='nova', - fixed_address='192.0.2.129') + fixed_address='192.0.2.129', + ) self.assertEqual(server, self.fake_server) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_floating_ip_pool.py b/openstack/tests/unit/cloud/test_floating_ip_pool.py index 7e7132b05..6c4d87b0e 100644 --- a/openstack/tests/unit/cloud/test_floating_ip_pool.py +++ b/openstack/tests/unit/cloud/test_floating_ip_pool.py @@ -25,27 +25,39 @@ from openstack.tests.unit import base class TestFloatingIPPool(base.TestCase): - pools = [{'name': u'public'}] + pools = [{'name': 'public'}] def test_list_floating_ip_pools(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/extensions'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'extensions': [{ - u'alias': u'os-floating-ip-pools', - u'updated': u'2014-12-03T00:00:00Z', - u'name': u'FloatingIpPools', - u'links': [], - u'namespace': - u'http://docs.openstack.org/compute/ext/fake_xml', - u'description': u'Floating IPs support.'}]}), - dict(method='GET', - uri='{endpoint}/os-floating-ip-pools'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={"floating_ip_pools": [{"name": "public"}]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/extensions'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={ + 'extensions': [ + { + 'alias': 'os-floating-ip-pools', + 'updated': '2014-12-03T00:00:00Z', + 'name': 'FloatingIpPools', + 'links': [], + 'namespace': 'http://docs.openstack.org/compute/ext/fake_xml', # noqa: E501 + 'description': 'Floating IPs support.', + } + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/os-floating-ip-pools'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={"floating_ip_pools": [{"name": "public"}]}, + ), + ] + ) floating_ip_pools = self.cloud.list_floating_ip_pools() @@ -55,24 +67,38 @@ class TestFloatingIPPool(base.TestCase): def test_list_floating_ip_pools_exception(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/extensions'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'extensions': [{ - u'alias': u'os-floating-ip-pools', - u'updated': u'2014-12-03T00:00:00Z', - u'name': u'FloatingIpPools', - u'links': [], - u'namespace': - u'http://docs.openstack.org/compute/ext/fake_xml', - u'description': u'Floating IPs support.'}]}), - dict(method='GET', - uri='{endpoint}/os-floating-ip-pools'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - status_code=404)]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/extensions'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={ + 'extensions': [ + { + 'alias': 'os-floating-ip-pools', + 'updated': '2014-12-03T00:00:00Z', + 'name': 'FloatingIpPools', + 'links': [], + 'namespace': 'http://docs.openstack.org/compute/ext/fake_xml', # noqa: E501 + 'description': 'Floating IPs support.', + } + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/os-floating-ip-pools'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + status_code=404, + ), + ] + ) self.assertRaises( - OpenStackCloudException, self.cloud.list_floating_ip_pools) + OpenStackCloudException, self.cloud.list_floating_ip_pools + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_fwaas.py b/openstack/tests/unit/cloud/test_fwaas.py index 959b3d113..708e12d27 100644 --- a/openstack/tests/unit/cloud/test_fwaas.py +++ b/openstack/tests/unit/cloud/test_fwaas.py @@ -23,9 +23,12 @@ from openstack.tests.unit import base class FirewallTestCase(base.TestCase): def _make_mock_url(self, *args, **params): params_list = ['='.join([k, v]) for k, v in params.items()] - return self.get_mock_url('network', 'public', - append=['v2.0', 'fwaas'] + list(args), - qs_elements=params_list or None) + return self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'fwaas'] + list(args), + qs_elements=params_list or None, + ) class TestFirewallRule(FirewallTestCase): @@ -44,27 +47,31 @@ class TestFirewallRule(FirewallTestCase): 'protocol': 'tcp', 'shared': False, 'source_ip_address': None, - 'source_port': None + 'source_port': None, } mock_firewall_rule = None def setUp(self, cloud_config_fixture='clouds.yaml'): super(TestFirewallRule, self).setUp() self.mock_firewall_rule = FirewallRule( - connection=self.cloud, - **self._mock_firewall_rule_attrs).to_dict() + connection=self.cloud, **self._mock_firewall_rule_attrs + ).to_dict() def test_create_firewall_rule(self): # attributes that are passed to the tested function passed_attrs = self._mock_firewall_rule_attrs.copy() del passed_attrs['id'] - self.register_uris([ - # no validate due to added location key - dict(method='POST', - uri=self._make_mock_url('firewall_rules'), - json={'firewall_rule': self.mock_firewall_rule.copy()}) - ]) + self.register_uris( + [ + # no validate due to added location key + dict( + method='POST', + uri=self._make_mock_url('firewall_rules'), + json={'firewall_rule': self.mock_firewall_rule.copy()}, + ) + ] + ) r = self.cloud.create_firewall_rule(**passed_attrs) self.assertDictEqual(self.mock_firewall_rule, r.to_dict()) self.assert_calls() @@ -73,154 +80,241 @@ class TestFirewallRule(FirewallTestCase): bad_rule = self._mock_firewall_rule_attrs.copy() del bad_rule['id'] # id not allowed bad_rule['ip_version'] = 5 - self.register_uris([ - # no validate due to added location key - dict(method='POST', - uri=self._make_mock_url('firewall_rules'), - status_code=400, - json={}) - ]) - self.assertRaises(exceptions.BadRequestException, - self.cloud.create_firewall_rule, **bad_rule) + self.register_uris( + [ + # no validate due to added location key + dict( + method='POST', + uri=self._make_mock_url('firewall_rules'), + status_code=400, + json={}, + ) + ] + ) + self.assertRaises( + exceptions.BadRequestException, + self.cloud.create_firewall_rule, + **bad_rule + ) self.assert_calls() def test_delete_firewall_rule(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', - name=self.firewall_rule_name), - json={'firewall_rules': [self.mock_firewall_rule]}), - dict(method='DELETE', - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_id), - json={}, status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=self.firewall_rule_name + ), + json={'firewall_rules': [self.mock_firewall_rule]}, + ), + dict( + method='DELETE', + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_id + ), + json={}, + status_code=204, + ), + ] + ) self.assertTrue( - self.cloud.delete_firewall_rule(self.firewall_rule_name)) + self.cloud.delete_firewall_rule(self.firewall_rule_name) + ) self.assert_calls() def test_delete_firewall_rule_filters(self): filters = {'project_id': self.mock_firewall_rule['project_id']} - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_name, - **filters), - status_code=404), - dict(method='GET', - uri=self._make_mock_url( - 'firewall_rules', - name=self.firewall_rule_name, **filters), - json={'firewall_rules': [self.mock_firewall_rule]}, ), - dict(method='DELETE', - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_id), - json={}, status_code=204), - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name, **filters + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', + name=self.firewall_rule_name, + **filters + ), + json={'firewall_rules': [self.mock_firewall_rule]}, + ), + dict( + method='DELETE', + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_id + ), + json={}, + status_code=204, + ), + ] + ) self.assertTrue( - self.cloud.delete_firewall_rule(self.firewall_rule_name, filters)) + self.cloud.delete_firewall_rule(self.firewall_rule_name, filters) + ) self.assert_calls() def test_delete_firewall_rule_not_found(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules'), - json={'firewall_rules': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_rules'), + json={'firewall_rules': []}, + ), + ] + ) - with mock.patch.object(self.cloud.network, 'delete_firewall_rule'), \ - mock.patch.object(self.cloud.log, 'debug'): + with mock.patch.object( + self.cloud.network, 'delete_firewall_rule' + ), mock.patch.object(self.cloud.log, 'debug'): self.assertFalse( - self.cloud.delete_firewall_rule(self.firewall_rule_name)) + self.cloud.delete_firewall_rule(self.firewall_rule_name) + ) self.cloud.network.delete_firewall_rule.assert_not_called() self.cloud.log.debug.assert_called_once() def test_delete_firewall_multiple_matches(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', - name=self.firewall_rule_name), - json={'firewall_rules': [self.mock_firewall_rule, - self.mock_firewall_rule]}) - ]) - self.assertRaises(exceptions.DuplicateResource, - self.cloud.delete_firewall_rule, - self.firewall_rule_name) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=self.firewall_rule_name + ), + json={ + 'firewall_rules': [ + self.mock_firewall_rule, + self.mock_firewall_rule, + ] + }, + ), + ] + ) + self.assertRaises( + exceptions.DuplicateResource, + self.cloud.delete_firewall_rule, + self.firewall_rule_name, + ) self.assert_calls() def test_get_firewall_rule(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', - name=self.firewall_rule_name), - json={'firewall_rules': [self.mock_firewall_rule]}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=self.firewall_rule_name + ), + json={'firewall_rules': [self.mock_firewall_rule]}, + ), + ] + ) r = self.cloud.get_firewall_rule(self.firewall_rule_name) self.assertDictEqual(self.mock_firewall_rule, r) self.assert_calls() def test_get_firewall_rule_not_found(self): name = 'not_found' - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', name=name), - json={'firewall_rules': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', name), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_rules', name=name), + json={'firewall_rules': []}, + ), + ] + ) self.assertIsNone(self.cloud.get_firewall_rule(name)) self.assert_calls() def test_list_firewall_rules(self): - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_rules'), - json={'firewall_rules': [self.mock_firewall_rule]}) - ]) - self.assertDictEqual(self.mock_firewall_rule, - self.cloud.list_firewall_rules()[0]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url('firewall_rules'), + json={'firewall_rules': [self.mock_firewall_rule]}, + ) + ] + ) + self.assertDictEqual( + self.mock_firewall_rule, self.cloud.list_firewall_rules()[0] + ) self.assert_calls() def test_update_firewall_rule(self): params = {'description': 'UpdatedDescription'} updated = self.mock_firewall_rule.copy() updated.update(params) - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', - name=self.firewall_rule_name), - json={'firewall_rules': [self.mock_firewall_rule]}), - dict(method='PUT', - uri=self._make_mock_url('firewall_rules', - self.firewall_rule_id), - json={'firewall_rule': updated}, - validate=dict(json={'firewall_rule': params})) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=self.firewall_rule_name + ), + json={'firewall_rules': [self.mock_firewall_rule]}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_id + ), + json={'firewall_rule': updated}, + validate=dict(json={'firewall_rule': params}), + ), + ] + ) self.assertDictEqual( updated, - self.cloud.update_firewall_rule(self.firewall_rule_name, **params)) + self.cloud.update_firewall_rule(self.firewall_rule_name, **params), + ) self.assert_calls() def test_update_firewall_rule_filters(self): @@ -230,23 +324,30 @@ class TestFirewallRule(FirewallTestCase): updated.update(params) updated_dict = self._mock_firewall_rule_attrs.copy() updated_dict.update(params) - self.register_uris([ - dict( - method='GET', - uri=self._make_mock_url( - 'firewall_rules', self.firewall_rule_name, **filters), - json={'firewall_rule': self._mock_firewall_rule_attrs}), - dict( - method='PUT', - uri=self._make_mock_url( - 'firewall_rules', self.firewall_rule_id), - json={'firewall_rule': updated_dict}, - validate={ - 'json': {'firewall_rule': params}, - }) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_name, **filters + ), + json={'firewall_rule': self._mock_firewall_rule_attrs}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_rules', self.firewall_rule_id + ), + json={'firewall_rule': updated_dict}, + validate={ + 'json': {'firewall_rule': params}, + }, + ), + ] + ) updated_rule = self.cloud.update_firewall_rule( - self.firewall_rule_name, filters, **params) + self.firewall_rule_name, filters, **params + ) self.assertDictEqual(updated, updated_rule) self.assert_calls() @@ -261,15 +362,15 @@ class TestFirewallPolicy(FirewallTestCase): 'id': firewall_policy_id, 'name': firewall_policy_name, 'project_id': 'b64238cb-a25d-41af-9ee1-42deb4587d20', - 'shared': False + 'shared': False, } mock_firewall_policy = None def setUp(self, cloud_config_fixture='clouds.yaml'): super(TestFirewallPolicy, self).setUp() self.mock_firewall_policy = FirewallPolicy( - connection=self.cloud, - **self._mock_firewall_policy_attrs).to_dict() + connection=self.cloud, **self._mock_firewall_policy_attrs + ).to_dict() def test_create_firewall_policy(self): # attributes that are passed to the tested method @@ -285,23 +386,35 @@ class TestFirewallPolicy(FirewallTestCase): validate_attrs = deepcopy(created_attrs) del validate_attrs['id'] - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - TestFirewallRule.firewall_rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url( - 'firewall_rules', - name=TestFirewallRule.firewall_rule_name), - json={'firewall_rules': [ - TestFirewallRule._mock_firewall_rule_attrs]}), - dict(method='POST', - uri=self._make_mock_url('firewall_policies'), - json={'firewall_policy': created_attrs}, - validate=dict( - json={'firewall_policy': validate_attrs})) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', TestFirewallRule.firewall_rule_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', + name=TestFirewallRule.firewall_rule_name, + ), + json={ + 'firewall_rules': [ + TestFirewallRule._mock_firewall_rule_attrs + ] + }, + ), + dict( + method='POST', + uri=self._make_mock_url('firewall_policies'), + json={'firewall_policy': created_attrs}, + validate=dict(json={'firewall_policy': validate_attrs}), + ), + ] + ) res = self.cloud.create_firewall_policy(**passed_attrs) self.assertDictEqual(created_policy, res.to_dict()) self.assert_calls() @@ -309,200 +422,309 @@ class TestFirewallPolicy(FirewallTestCase): def test_create_firewall_policy_rule_not_found(self): posted_policy = deepcopy(self._mock_firewall_policy_attrs) del posted_policy['id'] - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - posted_policy['firewall_rules'][0]), - status_code=404), - dict(method='GET', - uri=self._make_mock_url( - 'firewall_rules', - name=posted_policy['firewall_rules'][0]), - json={'firewall_rules': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_rules', posted_policy['firewall_rules'][0] + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', + name=posted_policy['firewall_rules'][0], + ), + json={'firewall_rules': []}, + ), + ] + ) with mock.patch.object(self.cloud.network, 'create_firewall_policy'): - self.assertRaises(exceptions.ResourceNotFound, - self.cloud.create_firewall_policy, - **posted_policy) + self.assertRaises( + exceptions.ResourceNotFound, + self.cloud.create_firewall_policy, + **posted_policy + ) self.cloud.network.create_firewall_policy.assert_not_called() self.assert_calls() def test_delete_firewall_policy(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': [self.mock_firewall_policy]}), - dict(method='DELETE', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={}, status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': [self.mock_firewall_policy]}, + ), + dict( + method='DELETE', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={}, + status_code=204, + ), + ] + ) with mock.patch.object(self.cloud.log, 'debug'): self.assertTrue( - self.cloud.delete_firewall_policy(self.firewall_policy_name)) + self.cloud.delete_firewall_policy(self.firewall_policy_name) + ) self.assert_calls() self.cloud.log.debug.assert_not_called() def test_delete_firewall_policy_filters(self): filters = {'project_id': self.mock_firewall_policy['project_id']} - self.register_uris([ - dict(method='DELETE', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={}, status_code=204) - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={}, + status_code=204, + ) + ] + ) - with mock.patch.object(self.cloud.network, 'find_firewall_policy', - return_value=self.mock_firewall_policy), \ - mock.patch.object(self.cloud.log, 'debug'): + with mock.patch.object( + self.cloud.network, + 'find_firewall_policy', + return_value=self.mock_firewall_policy, + ), mock.patch.object(self.cloud.log, 'debug'): self.assertTrue( - self.cloud.delete_firewall_policy(self.firewall_policy_name, - filters)) + self.cloud.delete_firewall_policy( + self.firewall_policy_name, filters + ) + ) self.assert_calls() self.cloud.network.find_firewall_policy.assert_called_once_with( - self.firewall_policy_name, ignore_missing=False, **filters) + self.firewall_policy_name, ignore_missing=False, **filters + ) self.cloud.log.debug.assert_not_called() def test_delete_firewall_policy_not_found(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': []}, + ), + ] + ) with mock.patch.object(self.cloud.log, 'debug'): self.assertFalse( - self.cloud.delete_firewall_policy(self.firewall_policy_name)) + self.cloud.delete_firewall_policy(self.firewall_policy_name) + ) self.assert_calls() self.cloud.log.debug.assert_called_once() def test_get_firewall_policy(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': [self.mock_firewall_policy]}) - ]) - self.assertDictEqual(self.mock_firewall_policy, - self.cloud.get_firewall_policy( - self.firewall_policy_name)) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': [self.mock_firewall_policy]}, + ), + ] + ) + self.assertDictEqual( + self.mock_firewall_policy, + self.cloud.get_firewall_policy(self.firewall_policy_name), + ) self.assert_calls() def test_get_firewall_policy_not_found(self): name = 'not_found' - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', name=name), - json={'firewall_policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_policies', name), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_policies', name=name), + json={'firewall_policies': []}, + ), + ] + ) self.assertIsNone(self.cloud.get_firewall_policy(name)) self.assert_calls() def test_list_firewall_policies(self): - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_policies'), - json={'firewall_policies': [ - self.mock_firewall_policy.copy(), - self.mock_firewall_policy.copy()]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url('firewall_policies'), + json={ + 'firewall_policies': [ + self.mock_firewall_policy.copy(), + self.mock_firewall_policy.copy(), + ] + }, + ) + ] + ) policy = FirewallPolicy( - connection=self.cloud, - **self.mock_firewall_policy) - self.assertListEqual(self.cloud.list_firewall_policies(), - [policy, policy]) + connection=self.cloud, **self.mock_firewall_policy + ) + self.assertListEqual( + self.cloud.list_firewall_policies(), [policy, policy] + ) self.assert_calls() def test_list_firewall_policies_filters(self): filters = {'project_id': self.mock_firewall_policy['project_id']} - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_policies', **filters), - json={'firewall_policies': [ - self.mock_firewall_policy]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url('firewall_policies', **filters), + json={'firewall_policies': [self.mock_firewall_policy]}, + ) + ] + ) self.assertListEqual( - self.cloud.list_firewall_policies(filters), [ + self.cloud.list_firewall_policies(filters), + [ FirewallPolicy( - connection=self.cloud, - **self.mock_firewall_policy)]) + connection=self.cloud, **self.mock_firewall_policy + ) + ], + ) self.assert_calls() def test_update_firewall_policy(self): lookup_rule = FirewallRule( - connection=self.cloud, - **TestFirewallRule._mock_firewall_rule_attrs).to_dict() - params = {'firewall_rules': [lookup_rule['id']], - 'description': 'updated!'} + connection=self.cloud, **TestFirewallRule._mock_firewall_rule_attrs + ).to_dict() + params = { + 'firewall_rules': [lookup_rule['id']], + 'description': 'updated!', + } retrieved_policy = deepcopy(self.mock_firewall_policy) del retrieved_policy['firewall_rules'][0] updated_policy = deepcopy(self.mock_firewall_policy) updated_policy.update(params) - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': [retrieved_policy]}), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', lookup_rule['id']), - json={'firewall_rule': lookup_rule}), - dict(method='PUT', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={'firewall_policy': updated_policy}, - validate=dict(json={'firewall_policy': params})) - ]) - self.assertDictEqual(updated_policy, - self.cloud.update_firewall_policy( - self.firewall_policy_name, **params)) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': [retrieved_policy]}, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', lookup_rule['id'] + ), + json={'firewall_rule': lookup_rule}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={'firewall_policy': updated_policy}, + validate=dict(json={'firewall_policy': params}), + ), + ] + ) + self.assertDictEqual( + updated_policy, + self.cloud.update_firewall_policy( + self.firewall_policy_name, **params + ), + ) self.assert_calls() def test_update_firewall_policy_no_rules(self): params = {'description': 'updated!'} updated_policy = deepcopy(self.mock_firewall_policy) updated_policy.update(params) - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': [ - deepcopy(self.mock_firewall_policy)]}), - dict(method='PUT', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={'firewall_policy': updated_policy}, - validate=dict(json={'firewall_policy': params})), - ]) - self.assertDictEqual(updated_policy, - self.cloud.update_firewall_policy( - self.firewall_policy_name, **params)) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={ + 'firewall_policies': [ + deepcopy(self.mock_firewall_policy) + ] + }, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={'firewall_policy': updated_policy}, + validate=dict(json={'firewall_policy': params}), + ), + ] + ) + self.assertDictEqual( + updated_policy, + self.cloud.update_firewall_policy( + self.firewall_policy_name, **params + ), + ) self.assert_calls() def test_update_firewall_policy_filters(self): @@ -511,90 +733,135 @@ class TestFirewallPolicy(FirewallTestCase): updated_policy = deepcopy(self.mock_firewall_policy) updated_policy.update(params) - self.register_uris([ - dict(method='PUT', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={'firewall_policy': updated_policy}, - validate=dict(json={'firewall_policy': params})), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={'firewall_policy': updated_policy}, + validate=dict(json={'firewall_policy': params}), + ), + ] + ) - with mock.patch.object(self.cloud.network, 'find_firewall_policy', - return_value=deepcopy( - self.mock_firewall_policy)): + with mock.patch.object( + self.cloud.network, + 'find_firewall_policy', + return_value=deepcopy(self.mock_firewall_policy), + ): self.assertDictEqual( updated_policy, - self.cloud.update_firewall_policy(self.firewall_policy_name, - filters, **params)) + self.cloud.update_firewall_policy( + self.firewall_policy_name, filters, **params + ), + ) self.assert_calls() self.cloud.network.find_firewall_policy.assert_called_once_with( - self.firewall_policy_name, ignore_missing=False, **filters) + self.firewall_policy_name, ignore_missing=False, **filters + ) def test_insert_rule_into_policy(self): rule0 = FirewallRule( - connection=self.cloud, - **TestFirewallRule._mock_firewall_rule_attrs) + connection=self.cloud, **TestFirewallRule._mock_firewall_rule_attrs + ) - _rule1_attrs = deepcopy( - TestFirewallRule._mock_firewall_rule_attrs) - _rule1_attrs.update(id='8068fc06-0e72-43f2-a76f-a51a33b46e08', - name='after_rule') + _rule1_attrs = deepcopy(TestFirewallRule._mock_firewall_rule_attrs) + _rule1_attrs.update( + id='8068fc06-0e72-43f2-a76f-a51a33b46e08', name='after_rule' + ) rule1 = FirewallRule(**_rule1_attrs) _rule2_attrs = deepcopy(TestFirewallRule._mock_firewall_rule_attrs) - _rule2_attrs.update(id='c716382d-183b-475d-b500-dcc762f45ce3', - name='before_rule') + _rule2_attrs.update( + id='c716382d-183b-475d-b500-dcc762f45ce3', name='before_rule' + ) rule2 = FirewallRule(**_rule2_attrs) retrieved_policy = deepcopy(self.mock_firewall_policy) retrieved_policy['firewall_rules'] = [rule1['id'], rule2['id']] updated_policy = deepcopy(self.mock_firewall_policy) - updated_policy['firewall_rules'] = [rule0['id'], rule1['id'], - rule2['id']] - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', # get policy - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': [retrieved_policy]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', rule0['name']), - status_code=404), - dict(method='GET', # get rule to add - uri=self._make_mock_url('firewall_rules', name=rule0['name']), - json={'firewall_rules': [rule0]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', rule1['name']), - status_code=404), - dict(method='GET', # get after rule - uri=self._make_mock_url('firewall_rules', name=rule1['name']), - json={'firewall_rules': [rule1]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', rule2['name']), - status_code=404), - dict(method='GET', # get before rule - uri=self._make_mock_url('firewall_rules', name=rule2['name']), - json={'firewall_rules': [rule2]}), - - dict(method='PUT', # add rule - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id, - 'insert_rule'), - json=updated_policy, - validate=dict(json={'firewall_rule_id': rule0['id'], - 'insert_after': rule1['id'], - 'insert_before': rule2['id']})), - ]) + updated_policy['firewall_rules'] = [ + rule0['id'], + rule1['id'], + rule2['id'], + ] + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', # get policy + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': [retrieved_policy]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule0['name']), + status_code=404, + ), + dict( + method='GET', # get rule to add + uri=self._make_mock_url( + 'firewall_rules', name=rule0['name'] + ), + json={'firewall_rules': [rule0]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule1['name']), + status_code=404, + ), + dict( + method='GET', # get after rule + uri=self._make_mock_url( + 'firewall_rules', name=rule1['name'] + ), + json={'firewall_rules': [rule1]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule2['name']), + status_code=404, + ), + dict( + method='GET', # get before rule + uri=self._make_mock_url( + 'firewall_rules', name=rule2['name'] + ), + json={'firewall_rules': [rule2]}, + ), + dict( + method='PUT', # add rule + uri=self._make_mock_url( + 'firewall_policies', + self.firewall_policy_id, + 'insert_rule', + ), + json=updated_policy, + validate=dict( + json={ + 'firewall_rule_id': rule0['id'], + 'insert_after': rule1['id'], + 'insert_before': rule2['id'], + } + ), + ), + ] + ) r = self.cloud.insert_rule_into_policy( name_or_id=self.firewall_policy_name, rule_name_or_id=rule0['name'], insert_after=rule1['name'], - insert_before=rule2['name']) + insert_before=rule2['name'], + ) self.assertDictEqual(updated_policy, r.to_dict()) self.assert_calls() @@ -607,89 +874,140 @@ class TestFirewallPolicy(FirewallTestCase): retrieved_policy['firewall_rules'] = [] updated_policy = deepcopy(retrieved_policy) updated_policy['firewall_rules'].append(rule['id']) - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': [retrieved_policy]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', rule['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', name=rule['name']), - json={'firewall_rules': [rule]}), - - dict(method='PUT', - uri=self._make_mock_url('firewall_policies', - retrieved_policy['id'], - 'insert_rule'), - json=updated_policy, - validate=dict(json={'firewall_rule_id': rule['id'], - 'insert_after': None, - 'insert_before': None})) - ]) - r = self.cloud.insert_rule_into_policy(self.firewall_policy_name, - rule['name']) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': [retrieved_policy]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule['name']), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=rule['name'] + ), + json={'firewall_rules': [rule]}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_policies', + retrieved_policy['id'], + 'insert_rule', + ), + json=updated_policy, + validate=dict( + json={ + 'firewall_rule_id': rule['id'], + 'insert_after': None, + 'insert_before': None, + } + ), + ), + ] + ) + r = self.cloud.insert_rule_into_policy( + self.firewall_policy_name, rule['name'] + ) self.assertDictEqual(updated_policy, r.to_dict()) self.assert_calls() def test_insert_rule_into_policy_not_found(self): policy_name = 'bogus_policy' - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=policy_name), - json={'firewall_policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_policies', policy_name), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=policy_name + ), + json={'firewall_policies': []}, + ), + ] + ) with mock.patch.object(self.cloud.network, 'find_firewall_rule'): - self.assertRaises(exceptions.ResourceNotFound, - self.cloud.insert_rule_into_policy, - policy_name, 'bogus_rule') + self.assertRaises( + exceptions.ResourceNotFound, + self.cloud.insert_rule_into_policy, + policy_name, + 'bogus_rule', + ) self.assert_calls() self.cloud.network.find_firewall_rule.assert_not_called() def test_insert_rule_into_policy_rule_not_found(self): rule_name = 'unknown_rule' - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={'firewall_policy': self.mock_firewall_policy}), - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', rule_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', name=rule_name), - json={'firewall_rules': []}) - ]) - self.assertRaises(exceptions.ResourceNotFound, - self.cloud.insert_rule_into_policy, - self.firewall_policy_id, rule_name) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={'firewall_policy': self.mock_firewall_policy}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule_name), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_rules', name=rule_name), + json={'firewall_rules': []}, + ), + ] + ) + self.assertRaises( + exceptions.ResourceNotFound, + self.cloud.insert_rule_into_policy, + self.firewall_policy_id, + rule_name, + ) self.assert_calls() def test_insert_rule_into_policy_already_associated(self): rule = FirewallRule( - **TestFirewallRule._mock_firewall_rule_attrs).to_dict() + **TestFirewallRule._mock_firewall_rule_attrs + ).to_dict() policy = deepcopy(self.mock_firewall_policy) policy['firewall_rules'] = [rule['id']] - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={'firewall_policy': policy}), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', rule['id']), - json={'firewall_rule': rule}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={'firewall_policy': policy}, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_rules', rule['id']), + json={'firewall_rule': rule}, + ), + ] + ) with mock.patch.object(self.cloud.log, 'debug'): r = self.cloud.insert_rule_into_policy(policy['id'], rule['id']) @@ -706,50 +1024,75 @@ class TestFirewallPolicy(FirewallTestCase): updated_policy = deepcopy(self.mock_firewall_policy) del updated_policy['firewall_rules'][0] - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=policy_name), - json={'firewall_policies': [retrieved_policy]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', rule['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', name=rule['name']), - json={'firewall_rules': [rule]}), - - dict(method='PUT', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id, - 'remove_rule'), - json=updated_policy, - validate=dict(json={'firewall_rule_id': rule['id']})) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_policies', policy_name), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=policy_name + ), + json={'firewall_policies': [retrieved_policy]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule['name']), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=rule['name'] + ), + json={'firewall_rules': [rule]}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_policies', + self.firewall_policy_id, + 'remove_rule', + ), + json=updated_policy, + validate=dict(json={'firewall_rule_id': rule['id']}), + ), + ] + ) r = self.cloud.remove_rule_from_policy(policy_name, rule['name']) self.assertDictEqual(updated_policy, r.to_dict()) self.assert_calls() def test_remove_rule_from_policy_not_found(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.firewall_policy_name), - json={'firewall_policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', name=self.firewall_policy_name + ), + json={'firewall_policies': []}, + ), + ] + ) with mock.patch.object(self.cloud.network, 'find_firewall_rule'): - self.assertRaises(exceptions.ResourceNotFound, - self.cloud.remove_rule_from_policy, - self.firewall_policy_name, - TestFirewallRule.firewall_rule_name) + self.assertRaises( + exceptions.ResourceNotFound, + self.cloud.remove_rule_from_policy, + self.firewall_policy_name, + TestFirewallRule.firewall_rule_name, + ) self.assert_calls() self.cloud.network.find_firewall_rule.assert_not_called() @@ -757,41 +1100,60 @@ class TestFirewallPolicy(FirewallTestCase): retrieved_policy = deepcopy(self.mock_firewall_policy) rule = FirewallRule(**TestFirewallRule._mock_firewall_rule_attrs) retrieved_policy['firewall_rules'][0] = rule['id'] - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - self.firewall_policy_id), - json={'firewall_policy': retrieved_policy}), - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_rules', - rule['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', name=rule['name']), - json={'firewall_rules': []}) - ]) - r = self.cloud.remove_rule_from_policy(self.firewall_policy_id, - rule['name']) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', self.firewall_policy_id + ), + json={'firewall_policy': retrieved_policy}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_rules', rule['name']), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_rules', name=rule['name'] + ), + json={'firewall_rules': []}, + ), + ] + ) + r = self.cloud.remove_rule_from_policy( + self.firewall_policy_id, rule['name'] + ) self.assertDictEqual(retrieved_policy, r.to_dict()) self.assert_calls() def test_remove_rule_from_policy_not_associated(self): rule = FirewallRule( - **TestFirewallRule._mock_firewall_rule_attrs).to_dict() + **TestFirewallRule._mock_firewall_rule_attrs + ).to_dict() policy = deepcopy(self.mock_firewall_policy) del policy['firewall_rules'][0] - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_policies', policy['id']), - json={'firewall_policy': policy}), - dict(method='GET', - uri=self._make_mock_url('firewall_rules', rule['id']), - json={'firewall_rule': rule}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url('firewall_policies', policy['id']), + json={'firewall_policy': policy}, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_rules', rule['id']), + json={'firewall_rule': rule}, + ), + ] + ) - with mock.patch.object(self.cloud.network, 'remove_rule_from_policy'),\ - mock.patch.object(self.cloud.log, 'debug'): + with mock.patch.object( + self.cloud.network, 'remove_rule_from_policy' + ), mock.patch.object(self.cloud.log, 'debug'): r = self.cloud.remove_rule_from_policy(policy['id'], rule['id']) self.assertDictEqual(policy, r.to_dict()) self.assert_calls() @@ -804,15 +1166,15 @@ class TestFirewallGroup(FirewallTestCase): firewall_group_name = 'max_security_group' mock_port = { 'name': 'mock_port', - 'id': '7d90977c-45ec-467e-a16d-dcaed772a161' + 'id': '7d90977c-45ec-467e-a16d-dcaed772a161', } _mock_egress_policy_attrs = { 'id': '34335e5b-44af-4ffd-9dcf-518133f897c7', - 'name': 'safe_outgoing_data' + 'name': 'safe_outgoing_data', } _mock_ingress_policy_attrs = { 'id': 'cd28fb50-85d0-4f36-89af-50fac08ac174', - 'name': 'bad_incoming_data' + 'name': 'bad_incoming_data', } _mock_firewall_group_attrs = { 'admin_state_up': True, @@ -823,7 +1185,7 @@ class TestFirewallGroup(FirewallTestCase): 'name': firewall_group_name, 'ports': [mock_port['name']], 'project_id': 'da347b09-0b4f-4994-a3ef-05d13eaecb2c', - 'shared': False + 'shared': False, } _mock_returned_firewall_group_attrs = { 'admin_state_up': True, @@ -836,7 +1198,7 @@ class TestFirewallGroup(FirewallTestCase): 'name': firewall_group_name, 'ports': [mock_port['id']], 'project_id': 'da347b09-0b4f-4994-a3ef-05d13eaecb2c', - 'shared': False + 'shared': False, } mock_egress_policy = None mock_ingress_policy = None @@ -846,17 +1208,17 @@ class TestFirewallGroup(FirewallTestCase): def setUp(self, cloud_config_fixture='clouds.yaml'): super(TestFirewallGroup, self).setUp() self.mock_egress_policy = FirewallPolicy( - connection=self.cloud, - **self._mock_egress_policy_attrs).to_dict() + connection=self.cloud, **self._mock_egress_policy_attrs + ).to_dict() self.mock_ingress_policy = FirewallPolicy( - connection=self.cloud, - **self._mock_ingress_policy_attrs).to_dict() + connection=self.cloud, **self._mock_ingress_policy_attrs + ).to_dict() self.mock_firewall_group = FirewallGroup( - connection=self.cloud, - **self._mock_firewall_group_attrs).to_dict() + connection=self.cloud, **self._mock_firewall_group_attrs + ).to_dict() self.mock_returned_firewall_group = FirewallGroup( - connection=self.cloud, - **self._mock_returned_firewall_group_attrs).to_dict() + connection=self.cloud, **self._mock_returned_firewall_group_attrs + ).to_dict() def test_create_firewall_group(self): create_group_attrs = self._mock_firewall_group_attrs.copy() @@ -865,43 +1227,69 @@ class TestFirewallGroup(FirewallTestCase): del posted_group_attrs['egress_firewall_policy'] del posted_group_attrs['ingress_firewall_policy'] del posted_group_attrs['id'] - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.mock_egress_policy['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.mock_egress_policy['name']), - json={'firewall_policies': [self.mock_egress_policy]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.mock_ingress_policy['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url( - 'firewall_policies', - name=self.mock_ingress_policy['name']), - json={'firewall_policies': [self.mock_ingress_policy]}), - - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', self.mock_port['name']]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports'], - qs_elements=['name=%s' % self.mock_port['name']]), - json={'ports': [self.mock_port]}), - dict(method='POST', - uri=self._make_mock_url('firewall_groups'), - json={'firewall_group': deepcopy( - self.mock_returned_firewall_group)}, - validate=dict(json={'firewall_group': posted_group_attrs})) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.mock_egress_policy['name'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', + name=self.mock_egress_policy['name'], + ), + json={'firewall_policies': [self.mock_egress_policy]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.mock_ingress_policy['name'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', + name=self.mock_ingress_policy['name'], + ), + json={'firewall_policies': [self.mock_ingress_policy]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', self.mock_port['name']], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['name=%s' % self.mock_port['name']], + ), + json={'ports': [self.mock_port]}, + ), + dict( + method='POST', + uri=self._make_mock_url('firewall_groups'), + json={ + 'firewall_group': deepcopy( + self.mock_returned_firewall_group + ) + }, + validate=dict(json={'firewall_group': posted_group_attrs}), + ), + ] + ) r = self.cloud.create_firewall_group(**create_group_attrs) self.assertDictEqual(self.mock_returned_firewall_group, r.to_dict()) self.assert_calls() @@ -915,129 +1303,193 @@ class TestFirewallGroup(FirewallTestCase): del firewall_group['egress_firewall_policy'] del firewall_group['ingress_firewall_policy'] created_firewall = deepcopy(firewall_group) - created_firewall.update(egress_firewall_policy_id=None, - ingress_firewall_policy_id=None, - ports=[]) + created_firewall.update( + egress_firewall_policy_id=None, + ingress_firewall_policy_id=None, + ports=[], + ) del firewall_group['id'] - self.register_uris([ - dict(method='POST', - uri=self._make_mock_url('firewall_groups'), - json={'firewall_group': created_firewall}, - validate=dict(json={'firewall_group': firewall_group})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self._make_mock_url('firewall_groups'), + json={'firewall_group': created_firewall}, + validate=dict(json={'firewall_group': firewall_group}), + ) + ] + ) r = self.cloud.create_firewall_group(**firewall_group) self.assertDictEqual( - FirewallGroup( - connection=self.cloud, - **created_firewall).to_dict(), - r.to_dict()) + FirewallGroup(connection=self.cloud, **created_firewall).to_dict(), + r.to_dict(), + ) self.assert_calls() def test_delete_firewall_group(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_groups', - self.firewall_group_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - name=self.firewall_group_name), - json={'firewall_groups': [ - deepcopy(self.mock_returned_firewall_group)]}), - dict(method='DELETE', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', name=self.firewall_group_name + ), + json={ + 'firewall_groups': [ + deepcopy(self.mock_returned_firewall_group) + ] + }, + ), + dict( + method='DELETE', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + status_code=204, + ), + ] + ) self.assertTrue( - self.cloud.delete_firewall_group(self.firewall_group_name)) + self.cloud.delete_firewall_group(self.firewall_group_name) + ) self.assert_calls() def test_delete_firewall_group_filters(self): filters = {'project_id': self.mock_firewall_group['project_id']} - self.register_uris([ - dict(method='DELETE', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + status_code=204, + ) + ] + ) - with mock.patch.object(self.cloud.network, 'find_firewall_group', - return_value=deepcopy( - self.mock_firewall_group)): + with mock.patch.object( + self.cloud.network, + 'find_firewall_group', + return_value=deepcopy(self.mock_firewall_group), + ): self.assertTrue( - self.cloud.delete_firewall_group(self.firewall_group_name, - filters)) + self.cloud.delete_firewall_group( + self.firewall_group_name, filters + ) + ) self.assert_calls() self.cloud.network.find_firewall_group.assert_called_once_with( - self.firewall_group_name, ignore_missing=False, **filters) + self.firewall_group_name, ignore_missing=False, **filters + ) def test_delete_firewall_group_not_found(self): - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_groups', - self.firewall_group_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - name=self.firewall_group_name), - json={'firewall_groups': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', name=self.firewall_group_name + ), + json={'firewall_groups': []}, + ), + ] + ) with mock.patch.object(self.cloud.log, 'debug'): self.assertFalse( - self.cloud.delete_firewall_group(self.firewall_group_name)) + self.cloud.delete_firewall_group(self.firewall_group_name) + ) self.assert_calls() self.cloud.log.debug.assert_called_once() def test_get_firewall_group(self): returned_group = deepcopy(self.mock_returned_firewall_group) - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_groups', - self.firewall_group_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - name=self.firewall_group_name), - json={'firewall_groups': [returned_group]}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', name=self.firewall_group_name + ), + json={'firewall_groups': [returned_group]}, + ), + ] + ) self.assertDictEqual( returned_group, - self.cloud.get_firewall_group(self.firewall_group_name)) + self.cloud.get_firewall_group(self.firewall_group_name), + ) self.assert_calls() def test_get_firewall_group_not_found(self): name = 'not_found' - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_groups', name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_groups', name=name), - json={'firewall_groups': []}) - ]) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url('firewall_groups', name), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url('firewall_groups', name=name), + json={'firewall_groups': []}, + ), + ] + ) self.assertIsNone(self.cloud.get_firewall_group(name)) self.assert_calls() def test_get_firewall_group_by_id(self): returned_group = deepcopy(self.mock_returned_firewall_group) - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': returned_group})]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={'firewall_group': returned_group}, + ) + ] + ) r = self.cloud.get_firewall_group(self.firewall_group_id) self.assertDictEqual(returned_group, r.to_dict()) self.assert_calls() def test_list_firewall_groups(self): returned_attrs = deepcopy(self.mock_returned_firewall_group) - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_groups'), - json={'firewall_groups': [returned_attrs, returned_attrs]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url('firewall_groups'), + json={'firewall_groups': [returned_attrs, returned_attrs]}, + ) + ] + ) group = FirewallGroup(connection=self.cloud, **returned_attrs) self.assertListEqual([group, group], self.cloud.list_firewall_groups()) self.assert_calls() @@ -1047,7 +1499,7 @@ class TestFirewallGroup(FirewallTestCase): 'description': 'updated!', 'egress_firewall_policy': self.mock_egress_policy['name'], 'ingress_firewall_policy': self.mock_ingress_policy['name'], - 'ports': [self.mock_port['name']] + 'ports': [self.mock_port['name']], } updated_group = deepcopy(self.mock_returned_firewall_group) updated_group['description'] = params['description'] @@ -1057,65 +1509,110 @@ class TestFirewallGroup(FirewallTestCase): returned_group.update( ingress_firewall_policy_id=None, egress_firewall_policy_id=None, - ports=[]) - self.register_uris([ - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_groups', - self.firewall_group_name), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - name=self.firewall_group_name), - json={'firewall_groups': [returned_group]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.mock_egress_policy['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url('firewall_policies', - name=self.mock_egress_policy['name']), - json={'firewall_policies': [ - deepcopy(self.mock_egress_policy)]}), - - dict(method='GET', # short-circuit - uri=self._make_mock_url('firewall_policies', - self.mock_ingress_policy['name']), - status_code=404), - dict(method='GET', - uri=self._make_mock_url( - 'firewall_policies', - name=self.mock_ingress_policy['name']), - json={'firewall_policies': [ - deepcopy(self.mock_ingress_policy)]}), - - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', self.mock_port['name']]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports'], - qs_elements=['name=%s' % self.mock_port['name']]), - json={'ports': [self.mock_port]}), - dict(method='PUT', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': updated_group}, - validate=dict(json={'firewall_group': { - 'description': params['description'], - 'egress_firewall_policy_id': - self.mock_egress_policy['id'], - 'ingress_firewall_policy_id': - self.mock_ingress_policy['id'], - 'ports': [self.mock_port['id']] - }})) - ]) - self.assertDictEqual(updated_group, - self.cloud.update_firewall_group( - self.firewall_group_name, **params)) + ports=[], + ) + self.register_uris( + [ + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_name + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', name=self.firewall_group_name + ), + json={'firewall_groups': [returned_group]}, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.mock_egress_policy['name'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', + name=self.mock_egress_policy['name'], + ), + json={ + 'firewall_policies': [ + deepcopy(self.mock_egress_policy) + ] + }, + ), + dict( + method='GET', # short-circuit + uri=self._make_mock_url( + 'firewall_policies', self.mock_ingress_policy['name'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_policies', + name=self.mock_ingress_policy['name'], + ), + json={ + 'firewall_policies': [ + deepcopy(self.mock_ingress_policy) + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', self.mock_port['name']], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['name=%s' % self.mock_port['name']], + ), + json={'ports': [self.mock_port]}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={'firewall_group': updated_group}, + validate=dict( + json={ + 'firewall_group': { + 'description': params['description'], + 'egress_firewall_policy_id': self.mock_egress_policy[ # noqa: E501 + 'id' + ], + 'ingress_firewall_policy_id': self.mock_ingress_policy[ # noqa: E501 + 'id' + ], + 'ports': [self.mock_port['id']], + } + } + ), + ), + ] + ) + self.assertDictEqual( + updated_group, + self.cloud.update_firewall_group( + self.firewall_group_name, **params + ), + ) self.assert_calls() def test_update_firewall_group_compact(self): @@ -1123,65 +1620,99 @@ class TestFirewallGroup(FirewallTestCase): updated_group = deepcopy(self.mock_returned_firewall_group) updated_group.update(params) - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': deepcopy( - self.mock_returned_firewall_group)}), - dict(method='PUT', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': updated_group}, - validate=dict(json={'firewall_group': params})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={ + 'firewall_group': deepcopy( + self.mock_returned_firewall_group + ) + }, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={'firewall_group': updated_group}, + validate=dict(json={'firewall_group': params}), + ), + ] + ) self.assertDictEqual( updated_group, - self.cloud.update_firewall_group(self.firewall_group_id, **params)) + self.cloud.update_firewall_group(self.firewall_group_id, **params), + ) self.assert_calls() def test_update_firewall_group_filters(self): filters = {'project_id': self.mock_firewall_group['project_id']} params = {'description': 'updated again!'} updated_group = deepcopy(self.mock_returned_firewall_group) - self.register_uris([ - dict(method='PUT', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': updated_group}, - validate=dict(json={'firewall_group': params})) - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={'firewall_group': updated_group}, + validate=dict(json={'firewall_group': params}), + ) + ] + ) - with mock.patch.object(self.cloud.network, 'find_firewall_group', - return_value=deepcopy( - self.mock_firewall_group)): - r = self.cloud.update_firewall_group(self.firewall_group_name, - filters, **params) + with mock.patch.object( + self.cloud.network, + 'find_firewall_group', + return_value=deepcopy(self.mock_firewall_group), + ): + r = self.cloud.update_firewall_group( + self.firewall_group_name, filters, **params + ) self.assertDictEqual(updated_group, r.to_dict()) self.assert_calls() self.cloud.network.find_firewall_group.assert_called_once_with( - self.firewall_group_name, ignore_missing=False, **filters) + self.firewall_group_name, ignore_missing=False, **filters + ) def test_update_firewall_group_unset_policies(self): - transformed_params = {'ingress_firewall_policy_id': None, - 'egress_firewall_policy_id': None} + transformed_params = { + 'ingress_firewall_policy_id': None, + 'egress_firewall_policy_id': None, + } updated_group = deepcopy(self.mock_returned_firewall_group) updated_group.update(**transformed_params) returned_group = deepcopy(self.mock_returned_firewall_group) - self.register_uris([ - dict(method='GET', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': returned_group}), - dict(method='PUT', - uri=self._make_mock_url('firewall_groups', - self.firewall_group_id), - json={'firewall_group': updated_group}, - validate=dict(json={'firewall_group': transformed_params})) - ]) - self.assertDictEqual(updated_group, - self.cloud.update_firewall_group( - self.firewall_group_id, - ingress_firewall_policy=None, - egress_firewall_policy=None)) + self.register_uris( + [ + dict( + method='GET', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={'firewall_group': returned_group}, + ), + dict( + method='PUT', + uri=self._make_mock_url( + 'firewall_groups', self.firewall_group_id + ), + json={'firewall_group': updated_group}, + validate=dict(json={'firewall_group': transformed_params}), + ), + ] + ) + self.assertDictEqual( + updated_group, + self.cloud.update_firewall_group( + self.firewall_group_id, + ingress_firewall_policy=None, + egress_firewall_policy=None, + ), + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_groups.py b/openstack/tests/unit/cloud/test_groups.py index d83e51ce3..dab4bc3e6 100644 --- a/openstack/tests/unit/cloud/test_groups.py +++ b/openstack/tests/unit/cloud/test_groups.py @@ -17,86 +17,129 @@ from openstack.tests.unit import base class TestGroups(base.TestCase): def setUp(self, cloud_config_fixture='clouds.yaml'): super(TestGroups, self).setUp( - cloud_config_fixture=cloud_config_fixture) + cloud_config_fixture=cloud_config_fixture + ) self.addCleanup(self.assert_calls) - def get_mock_url(self, service_type='identity', interface='public', - resource='groups', append=None, base_url_append='v3'): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource='groups', + append=None, + base_url_append='v3', + ): return super(TestGroups, self).get_mock_url( - service_type='identity', interface=interface, resource=resource, - append=append, base_url_append=base_url_append) + service_type='identity', + interface=interface, + resource=resource, + append=append, + base_url_append=base_url_append, + ) def test_list_groups(self): group_data = self._get_group_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'groups': [group_data.json_response['group']]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'groups': [group_data.json_response['group']]}, + ) + ] + ) self.cloud.list_groups() def test_get_group(self): group_data = self._get_group_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'groups': [group_data.json_response['group']]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'groups': [group_data.json_response['group']]}, + ), + ] + ) self.cloud.get_group(group_data.group_id) def test_delete_group(self): group_data = self._get_group_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - append=[group_data.group_id]), - status_code=200, - json={'group': group_data.json_response['group']}), - dict(method='DELETE', - uri=self.get_mock_url(append=[group_data.group_id]), - status_code=204), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[group_data.group_id]), + status_code=200, + json={'group': group_data.json_response['group']}, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[group_data.group_id]), + status_code=204, + ), + ] + ) self.assertTrue(self.cloud.delete_group(group_data.group_id)) def test_create_group(self): domain_data = self._get_domain_data() group_data = self._get_group_data(domain_id=domain_data.domain_id) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(resource='domains', - append=[domain_data.domain_id]), - status_code=200, - json=domain_data.json_response), - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=group_data.json_response, - validate=dict(json=group_data.json_request)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='domains', append=[domain_data.domain_id] + ), + status_code=200, + json=domain_data.json_response, + ), + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=group_data.json_response, + validate=dict(json=group_data.json_request), + ), + ] + ) self.cloud.create_group( - name=group_data.group_name, description=group_data.description, - domain=group_data.domain_id) + name=group_data.group_name, + description=group_data.description, + domain=group_data.domain_id, + ) def test_update_group(self): group_data = self._get_group_data() # Domain ID is not sent group_data.json_request['group'].pop('domain_id') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - append=[group_data.group_id]), - status_code=200, - json={'group': group_data.json_response['group']}), - dict(method='PATCH', - uri=self.get_mock_url( - append=[group_data.group_id]), - status_code=200, - json=group_data.json_response, - validate=dict(json={ - 'group': {'name': 'new_name', 'description': - 'new_description'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[group_data.group_id]), + status_code=200, + json={'group': group_data.json_response['group']}, + ), + dict( + method='PATCH', + uri=self.get_mock_url(append=[group_data.group_id]), + status_code=200, + json=group_data.json_response, + validate=dict( + json={ + 'group': { + 'name': 'new_name', + 'description': 'new_description', + } + } + ), + ), + ] + ) self.cloud.update_group( - group_data.group_id, 'new_name', 'new_description') + group_data.group_id, 'new_name', 'new_description' + ) diff --git a/openstack/tests/unit/cloud/test_identity_roles.py b/openstack/tests/unit/cloud/test_identity_roles.py index 045552325..c89562834 100644 --- a/openstack/tests/unit/cloud/test_identity_roles.py +++ b/openstack/tests/unit/cloud/test_identity_roles.py @@ -23,46 +23,65 @@ RAW_ROLE_ASSIGNMENTS = [ "links": {"assignment": "http://example"}, "role": {"id": "123456"}, "scope": {"domain": {"id": "161718"}}, - "user": {"id": "313233"} + "user": {"id": "313233"}, }, { "links": {"assignment": "http://example"}, "group": {"id": "101112"}, "role": {"id": "123456"}, - "scope": {"project": {"id": "456789"}} - } + "scope": {"project": {"id": "456789"}}, + }, ] class TestIdentityRoles(base.TestCase): - - def get_mock_url(self, service_type='identity', interface='public', - resource='roles', append=None, base_url_append='v3', - qs_elements=None): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource='roles', + append=None, + base_url_append='v3', + qs_elements=None, + ): return super(TestIdentityRoles, self).get_mock_url( - service_type, interface, resource, append, base_url_append, - qs_elements) + service_type, + interface, + resource, + append, + base_url_append, + qs_elements, + ) def test_list_roles(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'roles': [role_data.json_response['role']]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ) + ] + ) self.cloud.list_roles() self.assert_calls() def test_list_role_by_name(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - qs_elements=['name={0}'.format(role_data.role_name)]), - status_code=200, - json={'roles': [role_data.json_response['role']]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + qs_elements=['name={0}'.format(role_data.role_name)] + ), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ) + ] + ) role = self.cloud.list_roles(name=role_data.role_name)[0] self.assertIsNotNone(role) @@ -72,12 +91,16 @@ class TestIdentityRoles(base.TestCase): def test_get_role_by_name(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'roles': [role_data.json_response['role']]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ) + ] + ) role = self.cloud.get_role(role_data.role_name) self.assertIsNotNone(role) @@ -87,12 +110,16 @@ class TestIdentityRoles(base.TestCase): def test_get_role_by_id(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'roles': [role_data.json_response['role']]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ) + ] + ) role = self.cloud.get_role(role_data.role_id) self.assertIsNotNone(role) @@ -102,13 +129,17 @@ class TestIdentityRoles(base.TestCase): def test_create_role(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=role_data.json_response, - validate=dict(json=role_data.json_request)) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=role_data.json_response, + validate=dict(json=role_data.json_request), + ) + ] + ) role = self.cloud.create_role(role_data.role_name) @@ -120,20 +151,25 @@ class TestIdentityRoles(base.TestCase): def test_update_role(self): role_data = self._get_role_data() req = {'role': {'name': 'new_name'}} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'roles': [role_data.json_response['role']]}), - dict(method='PATCH', - uri=self.get_mock_url(append=[role_data.role_id]), - status_code=200, - json=role_data.json_response, - validate=dict(json=req)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ), + dict( + method='PATCH', + uri=self.get_mock_url(append=[role_data.role_id]), + status_code=200, + json=role_data.json_response, + validate=dict(json=req), + ), + ] + ) - role = self.cloud.update_role( - role_data.role_id, 'new_name') + role = self.cloud.update_role(role_data.role_id, 'new_name') self.assertIsNotNone(role) self.assertThat(role.name, matchers.Equals(role_data.role_name)) @@ -142,30 +178,42 @@ class TestIdentityRoles(base.TestCase): def test_delete_role_by_id(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'roles': [role_data.json_response['role']]}), - dict(method='DELETE', - uri=self.get_mock_url(append=[role_data.role_id]), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[role_data.role_id]), + status_code=204, + ), + ] + ) role = self.cloud.delete_role(role_data.role_id) self.assertThat(role, matchers.Equals(True)) self.assert_calls() def test_delete_role_by_name(self): role_data = self._get_role_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'roles': [role_data.json_response['role']]}), - dict(method='DELETE', - uri=self.get_mock_url(append=[role_data.role_id]), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'roles': [role_data.json_response['role']]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[role_data.role_id]), + status_code=204, + ), + ] + ) role = self.cloud.delete_role(role_data.role_name) self.assertThat(role, matchers.Equals(True)) self.assert_calls() @@ -177,78 +225,102 @@ class TestIdentityRoles(base.TestCase): project_data = self._get_project_data(domain_id=domain_data.domain_id) role_data = self._get_role_data() response = [ - {'links': 'https://example.com', - 'role': {'id': role_data.role_id}, - 'scope': {'domain': {'id': domain_data.domain_id}}, - 'user': {'id': user_data.user_id}}, - {'links': 'https://example.com', - 'role': {'id': role_data.role_id}, - 'scope': {'project': {'id': project_data.project_id}}, - 'group': {'id': group_data.group_id}}, + { + 'links': 'https://example.com', + 'role': {'id': role_data.role_id}, + 'scope': {'domain': {'id': domain_data.domain_id}}, + 'user': {'id': user_data.user_id}, + }, + { + 'links': 'https://example.com', + 'role': {'id': role_data.role_id}, + 'scope': {'project': {'id': project_data.project_id}}, + 'group': {'id': group_data.group_id}, + }, ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='role_assignments'), - status_code=200, - json={'role_assignments': response}, - complete_qs=True) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='role_assignments'), + status_code=200, + json={'role_assignments': response}, + complete_qs=True, + ) + ] + ) ret = self.cloud.list_role_assignments() self.assertThat(len(ret), matchers.Equals(2)) self.assertThat(ret[0].user['id'], matchers.Equals(user_data.user_id)) self.assertThat(ret[0].role['id'], matchers.Equals(role_data.role_id)) self.assertThat( ret[0].scope['domain']['id'], - matchers.Equals(domain_data.domain_id)) + matchers.Equals(domain_data.domain_id), + ) self.assertThat( - ret[1].group['id'], - matchers.Equals(group_data.group_id)) + ret[1].group['id'], matchers.Equals(group_data.group_id) + ) self.assertThat(ret[1].role['id'], matchers.Equals(role_data.role_id)) self.assertThat( ret[1].scope['project']['id'], - matchers.Equals(project_data.project_id)) + matchers.Equals(project_data.project_id), + ) def test_list_role_assignments_filters(self): domain_data = self._get_domain_data() user_data = self._get_user_data(domain_id=domain_data.domain_id) role_data = self._get_role_data() response = [ - {'links': 'https://example.com', - 'role': {'id': role_data.role_id}, - 'scope': {'domain': {'id': domain_data.domain_id}}, - 'user': {'id': user_data.user_id}} + { + 'links': 'https://example.com', + 'role': {'id': role_data.role_id}, + 'scope': {'domain': {'id': domain_data.domain_id}}, + 'user': {'id': user_data.user_id}, + } ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='role_assignments', - qs_elements=['scope.domain.id=%s' % domain_data.domain_id, - 'user.id=%s' % user_data.user_id, - 'effective=True']), - status_code=200, - json={'role_assignments': response}, - complete_qs=True) - ]) - params = dict(user=user_data.user_id, domain=domain_data.domain_id, - effective=True) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='role_assignments', + qs_elements=[ + 'scope.domain.id=%s' % domain_data.domain_id, + 'user.id=%s' % user_data.user_id, + 'effective=True', + ], + ), + status_code=200, + json={'role_assignments': response}, + complete_qs=True, + ) + ] + ) + params = dict( + user=user_data.user_id, + domain=domain_data.domain_id, + effective=True, + ) ret = self.cloud.list_role_assignments(filters=params) self.assertThat(len(ret), matchers.Equals(1)) self.assertThat(ret[0].user['id'], matchers.Equals(user_data.user_id)) self.assertThat(ret[0].role['id'], matchers.Equals(role_data.role_id)) self.assertThat( ret[0].scope['domain']['id'], - matchers.Equals(domain_data.domain_id)) + matchers.Equals(domain_data.domain_id), + ) def test_list_role_assignments_exception(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(resource='role_assignments'), - status_code=403) - ]) - with testtools.ExpectedException( - exceptions.ForbiddenException - ): + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(resource='role_assignments'), + status_code=403, + ) + ] + ) + with testtools.ExpectedException(exceptions.ForbiddenException): self.cloud.list_role_assignments() self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_identity_users.py b/openstack/tests/unit/cloud/test_identity_users.py index 943029d67..93b85500a 100644 --- a/openstack/tests/unit/cloud/test_identity_users.py +++ b/openstack/tests/unit/cloud/test_identity_users.py @@ -16,29 +16,46 @@ from openstack.tests.unit import base class TestIdentityUsers(base.TestCase): - - def get_mock_url(self, service_type='identity', interface='public', - resource='users', append=None, base_url_append='v3', - qs_elements=None): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource='users', + append=None, + base_url_append='v3', + qs_elements=None, + ): return super(TestIdentityUsers, self).get_mock_url( - service_type, interface, resource, append, base_url_append, - qs_elements) + service_type, + interface, + resource, + append, + base_url_append, + qs_elements, + ) def test_create_user(self): domain_data = self._get_domain_data() - user_data = self._get_user_data("myusername", "mypassword", - domain_id=domain_data.domain_id) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=user_data.json_response, - validate=dict(json=user_data.json_request)) - ]) + user_data = self._get_user_data( + "myusername", "mypassword", domain_id=domain_data.domain_id + ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=user_data.json_response, + validate=dict(json=user_data.json_request), + ) + ] + ) - user = self.cloud.create_user(user_data.name, - password=user_data.password, - domain_id=domain_data.domain_id) + user = self.cloud.create_user( + user_data.name, + password=user_data.password, + domain_id=domain_data.domain_id, + ) self.assertIsNotNone(user) self.assertThat(user.name, matchers.Equals(user_data.name)) @@ -46,22 +63,29 @@ class TestIdentityUsers(base.TestCase): def test_create_user_without_password(self): domain_data = self._get_domain_data() - user_data = self._get_user_data("myusername", - domain_id=domain_data.domain_id) + user_data = self._get_user_data( + "myusername", domain_id=domain_data.domain_id + ) user_data._replace( password=None, - json_request=user_data.json_request["user"].pop("password")) + json_request=user_data.json_request["user"].pop("password"), + ) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=user_data.json_response, - validate=dict(json=user_data.json_request)) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=user_data.json_response, + validate=dict(json=user_data.json_request), + ) + ] + ) - user = self.cloud.create_user(user_data.name, - domain_id=domain_data.domain_id) + user = self.cloud.create_user( + user_data.name, domain_id=domain_data.domain_id + ) self.assertIsNotNone(user) self.assertThat(user.name, matchers.Equals(user_data.name)) diff --git a/openstack/tests/unit/cloud/test_image.py b/openstack/tests/unit/cloud/test_image.py index dfd9be7fb..8e8b4b733 100644 --- a/openstack/tests/unit/cloud/test_image.py +++ b/openstack/tests/unit/cloud/test_image.py @@ -30,20 +30,20 @@ IMPORT_METHODS = 'glance-direct,web-download' class BaseTestImage(base.TestCase): - def setUp(self): super(BaseTestImage, self).setUp() self.image_id = str(uuid.uuid4()) self.image_name = self.getUniqueString('image') - self.object_name = u'images/{name}'.format(name=self.image_name) + self.object_name = 'images/{name}'.format(name=self.image_name) self.imagefile = tempfile.NamedTemporaryFile(delete=False) data = b'\2\0' self.imagefile.write(data) self.imagefile.close() self.output = data self.fake_image_dict = fakes.make_fake_image( - image_id=self.image_id, image_name=self.image_name, - data=self.imagefile.name + image_id=self.image_id, + image_name=self.image_name, + data=self.imagefile.name, ) self.fake_search_return = {'images': [self.fake_image_dict]} self.container_name = self.getUniqueString('container') @@ -51,16 +51,17 @@ class BaseTestImage(base.TestCase): def _compare_images(self, exp, real): self.assertDictEqual( image.Image(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def _compare_images_v1(self, exp, real): self.assertDictEqual( image_v1.Image(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) class TestImage(BaseTestImage): - def setUp(self): super(TestImage, self).setUp() self.use_glance() @@ -71,9 +72,9 @@ class TestImage(BaseTestImage): # because glance has a bug where it doesn't return https properly. self.assertEqual( 'https://image.example.com/v1/', - self.cloud._image_client.get_endpoint()) - self.assertEqual( - '1', self.cloud_config.get_api_version('image')) + self.cloud._image_client.get_endpoint(), + ) + self.assertEqual('1', self.cloud_config.get_api_version('image')) def test_config_v2(self): self.cloud.config.config['image_api_version'] = '2' @@ -81,57 +82,89 @@ class TestImage(BaseTestImage): # because glance has a bug where it doesn't return https properly. self.assertEqual( 'https://image.example.com/v2/', - self.cloud._image_client.get_endpoint()) - self.assertEqual( - '2', self.cloud_config.get_api_version('image')) + self.cloud._image_client.get_endpoint(), + ) + self.assertEqual('2', self.cloud_config.get_api_version('image')) def test_download_image_no_output(self): - self.assertRaises(exc.OpenStackCloudException, - self.cloud.download_image, self.image_name) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.download_image, + self.image_name, + ) def test_download_image_two_outputs(self): fake_fd = io.BytesIO() - self.assertRaises(exc.OpenStackCloudException, - self.cloud.download_image, self.image_name, - output_path='fake_path', output_file=fake_fd) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.download_image, + self.image_name, + output_path='fake_path', + output_file=fake_fd, + ) def test_download_image_no_images_found(self): - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images/{name}'.format( - name=self.image_name), - status_code=404), - dict(method='GET', - uri='https://image.example.com/v2/images?name={name}'.format( - name=self.image_name), - json=dict(images=[])), - dict(method='GET', - uri='https://image.example.com/v2/images?os_hidden=True', - json=dict(images=[]))]) - self.assertRaises(exc.OpenStackCloudResourceNotFound, - self.cloud.download_image, self.image_name, - output_path='fake_path') + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images/{name}'.format( + name=self.image_name + ), + status_code=404, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images?name={name}'.format( # noqa: E501 + name=self.image_name + ), + json=dict(images=[]), + ), + dict( + method='GET', + uri='https://image.example.com/v2/images?os_hidden=True', + json=dict(images=[]), + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudResourceNotFound, + self.cloud.download_image, + self.image_name, + output_path='fake_path', + ) self.assert_calls() def _register_image_mocks(self): - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images/{name}'.format( - name=self.image_name), - status_code=404), - dict(method='GET', - uri='https://image.example.com/v2/images?name={name}'.format( - name=self.image_name), - json=self.fake_search_return), - dict(method='GET', - uri='https://image.example.com/v2/images/{id}/file'.format( - id=self.image_id), - content=self.output, - headers={ - 'Content-Type': 'application/octet-stream', - 'Content-MD5': self.fake_image_dict['checksum'] - }) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images/{name}'.format( + name=self.image_name + ), + status_code=404, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images?name={name}'.format( # noqa: E501 + name=self.image_name + ), + json=self.fake_search_return, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images/{id}/file'.format( + id=self.image_id + ), + content=self.output, + headers={ + 'Content-Type': 'application/octet-stream', + 'Content-MD5': self.fake_image_dict['checksum'], + }, + ), + ] + ) def test_download_image_with_fd(self): self._register_image_mocks() @@ -145,62 +178,82 @@ class TestImage(BaseTestImage): self._register_image_mocks() output_file = tempfile.NamedTemporaryFile() self.cloud.download_image( - self.image_name, output_path=output_file.name) + self.image_name, output_path=output_file.name + ) output_file.seek(0) self.assertEqual(output_file.read(), self.output) self.assert_calls() def test_get_image_name(self, cloud=None): cloud = cloud or self.cloud - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + ] + ) + self.assertEqual(self.image_name, cloud.get_image_name(self.image_id)) self.assertEqual( - self.image_name, cloud.get_image_name(self.image_id)) - self.assertEqual( - self.image_name, cloud.get_image_name(self.image_name)) + self.image_name, cloud.get_image_name(self.image_name) + ) self.assert_calls() def test_get_image_by_id(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_id], - base_url_append='v2'), - json=self.fake_image_dict) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id], + base_url_append='v2', + ), + json=self.fake_image_dict, + ) + ] + ) self._compare_images( - self.fake_image_dict, - self.cloud.get_image_by_id(self.image_id) + self.fake_image_dict, self.cloud.get_image_by_id(self.image_id) ) self.assert_calls() def test_get_image_id(self, cloud=None): cloud = cloud or self.cloud - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + ] + ) - self.assertEqual( - self.image_id, cloud.get_image_id(self.image_id)) - self.assertEqual( - self.image_id, cloud.get_image_id(self.image_name)) + self.assertEqual(self.image_id, cloud.get_image_id(self.image_id)) + self.assertEqual(self.image_id, cloud.get_image_id(self.image_name)) self.assert_calls() @@ -213,338 +266,512 @@ class TestImage(BaseTestImage): self.test_get_image_id(cloud=self.cloud) def test_empty_list_images(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={'images': []}, + ) + ] + ) self.assertEqual([], self.cloud.list_images()) self.assert_calls() def test_list_images(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return) - ]) - [self._compare_images(a, b) for a, b in zip( - [self.fake_image_dict], - self.cloud.list_images())] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ) + ] + ) + [ + self._compare_images(a, b) + for a, b in zip([self.fake_image_dict], self.cloud.list_images()) + ] self.assert_calls() def test_list_images_show_all(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2', - qs_elements=['member_status=all']), - json=self.fake_search_return) - ]) - [self._compare_images(a, b) for a, b in zip( - [self.fake_image_dict], - self.cloud.list_images(show_all=True))] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['member_status=all'], + ), + json=self.fake_search_return, + ) + ] + ) + [ + self._compare_images(a, b) + for a, b in zip( + [self.fake_image_dict], self.cloud.list_images(show_all=True) + ) + ] self.assert_calls() def test_list_images_show_all_deleted(self): deleted_image = self.fake_image_dict.copy() deleted_image['status'] = 'deleted' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2', - qs_elements=['member_status=all']), - json={'images': [self.fake_image_dict, deleted_image]}) - ]) - [self._compare_images(a, b) for a, b in zip( - [self.fake_image_dict], - self.cloud.list_images(show_all=True))] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['member_status=all'], + ), + json={'images': [self.fake_image_dict, deleted_image]}, + ) + ] + ) + [ + self._compare_images(a, b) + for a, b in zip( + [self.fake_image_dict], self.cloud.list_images(show_all=True) + ) + ] self.assert_calls() def test_list_images_no_filter_deleted(self): deleted_image = self.fake_image_dict.copy() deleted_image['status'] = 'deleted' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': [self.fake_image_dict, deleted_image]}) - ]) - [self._compare_images(a, b) for a, b in zip( - [self.fake_image_dict], - self.cloud.list_images(filter_deleted=False))] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={'images': [self.fake_image_dict, deleted_image]}, + ) + ] + ) + [ + self._compare_images(a, b) + for a, b in zip( + [self.fake_image_dict], + self.cloud.list_images(filter_deleted=False), + ) + ] self.assert_calls() def test_list_images_filter_deleted(self): deleted_image = self.fake_image_dict.copy() deleted_image['status'] = 'deleted' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': [self.fake_image_dict, deleted_image]}) - ]) - [self._compare_images(a, b) for a, b in zip( - [self.fake_image_dict], - self.cloud.list_images())] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={'images': [self.fake_image_dict, deleted_image]}, + ) + ] + ) + [ + self._compare_images(a, b) + for a, b in zip([self.fake_image_dict], self.cloud.list_images()) + ] self.assert_calls() def test_list_images_string_properties(self): image_dict = self.fake_image_dict.copy() image_dict['properties'] = 'list,of,properties' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': [image_dict]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={'images': [image_dict]}, + ), + ] + ) images = self.cloud.list_images() - [self._compare_images(a, b) for a, b in zip( - [image_dict], - images)] + [self._compare_images(a, b) for a, b in zip([image_dict], images)] self.assertEqual( - images[0]['properties']['properties'], - 'list,of,properties') + images[0]['properties']['properties'], 'list,of,properties' + ) self.assert_calls() def test_list_images_paginated(self): marker = str(uuid.uuid4()) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': [self.fake_image_dict], - 'next': '/v2/images?marker={marker}'.format( - marker=marker)}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2', - qs_elements=['marker={marker}'.format(marker=marker)]), - json=self.fake_search_return) - ]) - [self._compare_images(a, b) for a, b in zip( - [self.fake_image_dict], - self.cloud.list_images())] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={ + 'images': [self.fake_image_dict], + 'next': '/v2/images?marker={marker}'.format( + marker=marker + ), + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['marker={marker}'.format(marker=marker)], + ), + json=self.fake_search_return, + ), + ] + ) + [ + self._compare_images(a, b) + for a, b in zip([self.fake_image_dict], self.cloud.list_images()) + ] self.assert_calls() def test_create_image_put_v2_no_import(self): self.cloud.image_api_use_tasks = False - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_image_dict, - validate=dict( - json={ - u'container_format': u'bare', - u'disk_format': u'qcow2', - u'name': self.image_name, - u'owner_specified.openstack.md5': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - u'owner_specified.openstack.object': self.object_name, - u'owner_specified.openstack.sha256': - self.fake_image_dict[ - 'owner_specified.openstack.sha256'], - u'visibility': u'private', - u'tags': [u'tag1', u'tag2']}) - ), - dict(method='PUT', - uri=self.get_mock_url( - 'image', append=['images', self.image_id, 'file'], - base_url_append='v2'), - request_headers={'Content-Type': 'application/octet-stream'}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.fake_image_dict['id']], - base_url_append='v2' - ), - json=self.fake_image_dict), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - complete_qs=True, - json=self.fake_search_return) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_image_dict, + validate=dict( + json={ + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'name': self.image_name, + 'owner_specified.openstack.md5': self.fake_image_dict[ # noqa: E501 + 'owner_specified.openstack.md5' + ], + 'owner_specified.openstack.object': self.object_name, # noqa: E501 + 'owner_specified.openstack.sha256': self.fake_image_dict[ # noqa: E501 + 'owner_specified.openstack.sha256' + ], + 'visibility': 'private', + 'tags': ['tag1', 'tag2'], + } + ), + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id, 'file'], + base_url_append='v2', + ), + request_headers={ + 'Content-Type': 'application/octet-stream' + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.fake_image_dict['id']], + base_url_append='v2', + ), + json=self.fake_image_dict, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + complete_qs=True, + json=self.fake_search_return, + ), + ] + ) self.cloud.create_image( - self.image_name, self.imagefile.name, wait=True, timeout=1, + self.image_name, + self.imagefile.name, + wait=True, + timeout=1, tags=['tag1', 'tag2'], - is_public=False, validate_checksum=True) + is_public=False, + validate_checksum=True, + ) self.assert_calls() - self.assertEqual(self.adapter.request_history[7].text.read(), - self.output) + self.assertEqual( + self.adapter.request_history[7].text.read(), self.output + ) def test_create_image_put_v2_import_supported(self): self.cloud.image_api_use_tasks = False - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_image_dict, - headers={ - 'OpenStack-image-import-methods': IMPORT_METHODS, - }, - validate=dict( - json={ - u'container_format': u'bare', - u'disk_format': u'qcow2', - u'name': self.image_name, - u'owner_specified.openstack.md5': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - u'owner_specified.openstack.object': self.object_name, - u'owner_specified.openstack.sha256': - self.fake_image_dict[ - 'owner_specified.openstack.sha256'], - u'visibility': u'private', - u'tags': [u'tag1', u'tag2']}) - ), - dict(method='PUT', - uri=self.get_mock_url( - 'image', append=['images', self.image_id, 'file'], - base_url_append='v2'), - request_headers={'Content-Type': 'application/octet-stream'}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.fake_image_dict['id']], - base_url_append='v2' - ), - json=self.fake_image_dict), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - complete_qs=True, - json=self.fake_search_return) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_image_dict, + headers={ + 'OpenStack-image-import-methods': IMPORT_METHODS, + }, + validate=dict( + json={ + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'name': self.image_name, + 'owner_specified.openstack.md5': self.fake_image_dict[ # noqa: E501 + 'owner_specified.openstack.md5' + ], + 'owner_specified.openstack.object': self.object_name, # noqa: E501 + 'owner_specified.openstack.sha256': self.fake_image_dict[ # noqa: E501 + 'owner_specified.openstack.sha256' + ], + 'visibility': 'private', + 'tags': ['tag1', 'tag2'], + } + ), + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id, 'file'], + base_url_append='v2', + ), + request_headers={ + 'Content-Type': 'application/octet-stream' + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.fake_image_dict['id']], + base_url_append='v2', + ), + json=self.fake_image_dict, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + complete_qs=True, + json=self.fake_search_return, + ), + ] + ) self.cloud.create_image( - self.image_name, self.imagefile.name, wait=True, timeout=1, + self.image_name, + self.imagefile.name, + wait=True, + timeout=1, tags=['tag1', 'tag2'], - is_public=False, validate_checksum=True) + is_public=False, + validate_checksum=True, + ) self.assert_calls() - self.assertEqual(self.adapter.request_history[7].text.read(), - self.output) + self.assertEqual( + self.adapter.request_history[7].text.read(), self.output + ) def test_create_image_use_import(self): self.cloud.image_api_use_tasks = False - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_image_dict, - headers={ - 'OpenStack-image-import-methods': IMPORT_METHODS, - }, - validate=dict( - json={ - u'container_format': u'bare', - u'disk_format': u'qcow2', - u'name': self.image_name, - u'owner_specified.openstack.md5': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - u'owner_specified.openstack.object': self.object_name, - u'owner_specified.openstack.sha256': - self.fake_image_dict[ - 'owner_specified.openstack.sha256'], - u'visibility': u'private', - u'tags': [u'tag1', u'tag2']}) - ), - dict(method='PUT', - uri=self.get_mock_url( - 'image', append=['images', self.image_id, 'stage'], - base_url_append='v2'), - request_headers={'Content-Type': 'application/octet-stream'}), - dict(method='POST', - uri=self.get_mock_url( - 'image', append=['images', self.image_id, 'import'], - base_url_append='v2'), - json={'method': {'name': 'glance-direct'}}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.fake_image_dict['id']], - base_url_append='v2' - ), - json=self.fake_image_dict), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - complete_qs=True, - json=self.fake_search_return) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_image_dict, + headers={ + 'OpenStack-image-import-methods': IMPORT_METHODS, + }, + validate=dict( + json={ + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'name': self.image_name, + 'owner_specified.openstack.md5': self.fake_image_dict[ # noqa: E501 + 'owner_specified.openstack.md5' + ], + 'owner_specified.openstack.object': self.object_name, # noqa: E501 + 'owner_specified.openstack.sha256': self.fake_image_dict[ # noqa: E501 + 'owner_specified.openstack.sha256' + ], + 'visibility': 'private', + 'tags': ['tag1', 'tag2'], + } + ), + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id, 'stage'], + base_url_append='v2', + ), + request_headers={ + 'Content-Type': 'application/octet-stream' + }, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id, 'import'], + base_url_append='v2', + ), + json={'method': {'name': 'glance-direct'}}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.fake_image_dict['id']], + base_url_append='v2', + ), + json=self.fake_image_dict, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + complete_qs=True, + json=self.fake_search_return, + ), + ] + ) self.cloud.create_image( - self.image_name, self.imagefile.name, wait=True, timeout=1, + self.image_name, + self.imagefile.name, + wait=True, + timeout=1, tags=['tag1', 'tag2'], - is_public=False, validate_checksum=True, + is_public=False, + validate_checksum=True, use_import=True, ) self.assert_calls() - self.assertEqual(self.adapter.request_history[7].text.read(), - self.output) + self.assertEqual( + self.adapter.request_history[7].text.read(), self.output + ) def test_create_image_task(self): self.cloud.image_api_use_tasks = True @@ -565,159 +792,254 @@ class TestImage(BaseTestImage): del image_no_checksums['owner_specified.openstack.sha256'] del image_no_checksums['owner_specified.openstack.object'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='HEAD', - uri='{endpoint}/{container}'.format( - endpoint=endpoint, container=self.container_name), - status_code=404), - dict(method='PUT', - uri='{endpoint}/{container}'.format( - endpoint=endpoint, container=self.container_name), - status_code=201, - headers={'Date': 'Fri, 16 Dec 2016 18:21:20 GMT', - 'Content-Length': '0', - 'Content-Type': 'text/html; charset=UTF-8'}), - dict(method='HEAD', - uri='{endpoint}/{container}'.format( - endpoint=endpoint, container=self.container_name), - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'Content-Type': 'text/plain; charset=utf-8'}), - dict(method='GET', - # This is explicitly not using get_mock_url because that - # gets us a project-id oriented URL. - uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': 1000}, - slo={'min_segment_size': 500})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=endpoint, container=self.container_name, - object=self.image_name), - status_code=404), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=endpoint, container=self.container_name, - object=self.image_name), - status_code=201, - validate=dict( - headers={'X-Object-Meta-x-sdk-md5': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - 'X-Object-Meta-x-sdk-sha256': - self.fake_image_dict[ - 'owner_specified.openstack.sha256']}) - ), - dict(method='POST', - uri=self.get_mock_url( - 'image', append=['tasks'], base_url_append='v2'), - json={'id': task_id, 'status': 'processing'}, - validate=dict( - json=dict( - type='import', input={ - 'import_from': '{container}/{object}'.format( - container=self.container_name, - object=self.image_name), - 'image_properties': {'name': self.image_name}})) - ), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['tasks', task_id], base_url_append='v2'), - status_code=503, text='Random error'), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['tasks', task_id], base_url_append='v2'), - json=args), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_id], - base_url_append='v2'), - json=image_no_checksums), - dict(method='PATCH', - uri=self.get_mock_url( - 'image', append=['images', self.image_id], - base_url_append='v2'), - validate=dict( - json=sorted([ - {u'op': u'add', - u'value': '{container}/{object}'.format( - container=self.container_name, - object=self.image_name), - u'path': u'/owner_specified.openstack.object'}, - {u'op': u'add', u'value': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - u'path': u'/owner_specified.openstack.md5'}, - {u'op': u'add', u'value': - self.fake_image_dict[ - 'owner_specified.openstack.sha256'], - u'path': u'/owner_specified.openstack.sha256'}], - key=operator.itemgetter('path')), - headers={ - 'Content-Type': - 'application/openstack-images-v2.1-json-patch'}), - json=self.fake_search_return - ), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=endpoint, container=self.container_name, - object=self.image_name), - headers={ - 'X-Timestamp': '1429036140.50253', - 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', - 'Content-Length': '1290170880', - 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', - 'X-Object-Meta-X-Sdk-Sha256': - self.fake_image_dict[ - 'owner_specified.openstack.sha256'], - 'X-Object-Meta-X-Sdk-Md5': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', - 'Accept-Ranges': 'bytes', - 'Content-Type': 'application/octet-stream', - 'Etag': fakes.NO_MD5}), - dict(method='DELETE', - uri='{endpoint}/{container}/{object}'.format( - endpoint=endpoint, container=self.container_name, - object=self.image_name)), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - complete_qs=True, - json=self.fake_search_return) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='HEAD', + uri='{endpoint}/{container}'.format( + endpoint=endpoint, container=self.container_name + ), + status_code=404, + ), + dict( + method='PUT', + uri='{endpoint}/{container}'.format( + endpoint=endpoint, container=self.container_name + ), + status_code=201, + headers={ + 'Date': 'Fri, 16 Dec 2016 18:21:20 GMT', + 'Content-Length': '0', + 'Content-Type': 'text/html; charset=UTF-8', + }, + ), + dict( + method='HEAD', + uri='{endpoint}/{container}'.format( + endpoint=endpoint, container=self.container_name + ), + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'Content-Type': 'text/plain; charset=utf-8', + }, + ), + dict( + method='GET', + # This is explicitly not using get_mock_url because that + # gets us a project-id oriented URL. + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': 1000}, + slo={'min_segment_size': 500}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=endpoint, + container=self.container_name, + object=self.image_name, + ), + status_code=404, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=endpoint, + container=self.container_name, + object=self.image_name, + ), + status_code=201, + validate=dict( + headers={ + 'X-Object-Meta-x-sdk-md5': self.fake_image_dict[ + 'owner_specified.openstack.md5' + ], + 'X-Object-Meta-x-sdk-sha256': self.fake_image_dict[ + 'owner_specified.openstack.sha256' + ], + } + ), + ), + dict( + method='POST', + uri=self.get_mock_url( + 'image', append=['tasks'], base_url_append='v2' + ), + json={'id': task_id, 'status': 'processing'}, + validate=dict( + json=dict( + type='import', + input={ + 'import_from': '{container}/{object}'.format( + container=self.container_name, + object=self.image_name, + ), + 'image_properties': {'name': self.image_name}, + }, + ) + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['tasks', task_id], + base_url_append='v2', + ), + status_code=503, + text='Random error', + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['tasks', task_id], + base_url_append='v2', + ), + json=args, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id], + base_url_append='v2', + ), + json=image_no_checksums, + ), + dict( + method='PATCH', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id], + base_url_append='v2', + ), + validate=dict( + json=sorted( + [ + { + 'op': 'add', + 'value': '{container}/{object}'.format( + container=self.container_name, + object=self.image_name, + ), + 'path': '/owner_specified.openstack.object', # noqa: E501 + }, + { + 'op': 'add', + 'value': self.fake_image_dict[ + 'owner_specified.openstack.md5' + ], + 'path': '/owner_specified.openstack.md5', + }, + { + 'op': 'add', + 'value': self.fake_image_dict[ + 'owner_specified.openstack.sha256' + ], + 'path': '/owner_specified.openstack.sha256', # noqa: E501 + }, + ], + key=operator.itemgetter('path'), + ), + headers={ + 'Content-Type': 'application/openstack-images-v2.1-json-patch' # noqa: E501 + }, + ), + json=self.fake_search_return, + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=endpoint, + container=self.container_name, + object=self.image_name, + ), + headers={ + 'X-Timestamp': '1429036140.50253', + 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', + 'Content-Length': '1290170880', + 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', + 'X-Object-Meta-X-Sdk-Sha256': self.fake_image_dict[ + 'owner_specified.openstack.sha256' + ], + 'X-Object-Meta-X-Sdk-Md5': self.fake_image_dict[ + 'owner_specified.openstack.md5' + ], + 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', + 'Accept-Ranges': 'bytes', + 'Content-Type': 'application/octet-stream', + 'Etag': fakes.NO_MD5, + }, + ), + dict( + method='DELETE', + uri='{endpoint}/{container}/{object}'.format( + endpoint=endpoint, + container=self.container_name, + object=self.image_name, + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + complete_qs=True, + json=self.fake_search_return, + ), + ] + ) self.cloud.create_image( - self.image_name, self.imagefile.name, wait=True, timeout=1, - disk_format='vhd', container_format='ovf', - is_public=False, validate_checksum=True, - container=self.container_name) + self.image_name, + self.imagefile.name, + wait=True, + timeout=1, + disk_format='vhd', + container_format='ovf', + is_public=False, + validate_checksum=True, + container=self.container_name, + ) self.assert_calls() @@ -725,7 +1047,8 @@ class TestImage(BaseTestImage): self.use_keystone_v3() self.cloud.image_api_use_tasks = False deleted = self.cloud.delete_autocreated_image_objects( - container=self.container_name) + container=self.container_name + ) self.assertFalse(deleted) self.assert_calls([]) @@ -740,38 +1063,51 @@ class TestImage(BaseTestImage): del image_no_checksums['owner_specified.openstack.sha256'] del image_no_checksums['owner_specified.openstack.object'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return), - dict(method='DELETE', - uri='https://image.example.com/v2/images/{id}'.format( - id=self.image_id)), - dict(method='HEAD', - uri='{endpoint}/{object}'.format( - endpoint=endpoint, - object=object_path), - headers={ - 'X-Timestamp': '1429036140.50253', - 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', - 'Content-Length': '1290170880', - 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', - 'X-Object-Meta-X-Sdk-Sha256': - self.fake_image_dict[ - 'owner_specified.openstack.sha256'], - 'X-Object-Meta-X-Sdk-Md5': - self.fake_image_dict[ - 'owner_specified.openstack.md5'], - 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', - 'Accept-Ranges': 'bytes', - 'Content-Type': 'application/octet-stream', - 'Etag': fakes.NO_MD5}), - dict(method='DELETE', - uri='{endpoint}/{object}'.format( - endpoint=endpoint, - object=object_path)), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + dict( + method='DELETE', + uri='https://image.example.com/v2/images/{id}'.format( + id=self.image_id + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{object}'.format( + endpoint=endpoint, object=object_path + ), + headers={ + 'X-Timestamp': '1429036140.50253', + 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', + 'Content-Length': '1290170880', + 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', + 'X-Object-Meta-X-Sdk-Sha256': self.fake_image_dict[ + 'owner_specified.openstack.sha256' + ], + 'X-Object-Meta-X-Sdk-Md5': self.fake_image_dict[ + 'owner_specified.openstack.md5' + ], + 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', + 'Accept-Ranges': 'bytes', + 'Content-Type': 'application/octet-stream', + 'Etag': fakes.NO_MD5, + }, + ), + dict( + method='DELETE', + uri='{endpoint}/{object}'.format( + endpoint=endpoint, object=object_path + ), + ), + ] + ) self.cloud.delete_image(self.image_id) @@ -783,69 +1119,91 @@ class TestImage(BaseTestImage): endpoint = self.cloud._object_store_client.get_endpoint() other_image = self.getUniqueString('no-delete') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - service_type='object-store', - resource=self.container_name, - qs_elements=['format=json']), - json=[{ - 'content_type': 'application/octet-stream', - 'bytes': 1437258240, - 'hash': '249219347276c331b87bf1ac2152d9af', - 'last_modified': '2015-02-16T17:50:05.289600', - 'name': other_image, - }, { - 'content_type': 'application/octet-stream', - 'bytes': 1290170880, - 'hash': fakes.NO_MD5, - 'last_modified': '2015-04-14T18:29:00.502530', - 'name': self.image_name, - }]), - dict(method='HEAD', - uri=self.get_mock_url( - service_type='object-store', - resource=self.container_name, - append=[other_image]), - headers={ - 'X-Timestamp': '1429036140.50253', - 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', - 'Content-Length': '1290170880', - 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', - 'X-Object-Meta-X-Shade-Sha256': 'does not matter', - 'X-Object-Meta-X-Shade-Md5': 'does not matter', - 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', - 'Accept-Ranges': 'bytes', - 'Content-Type': 'application/octet-stream', - 'Etag': '249219347276c331b87bf1ac2152d9af', - }), - dict(method='HEAD', - uri=self.get_mock_url( - service_type='object-store', - resource=self.container_name, - append=[self.image_name]), - headers={ - 'X-Timestamp': '1429036140.50253', - 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', - 'Content-Length': '1290170880', - 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', - 'X-Object-Meta-X-Shade-Sha256': fakes.NO_SHA256, - 'X-Object-Meta-X-Shade-Md5': fakes.NO_MD5, - 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', - 'Accept-Ranges': 'bytes', - 'Content-Type': 'application/octet-stream', - ('X-Object-Meta-' - + self.cloud._OBJECT_AUTOCREATE_KEY): 'true', - 'Etag': fakes.NO_MD5, - 'X-Static-Large-Object': 'false'}), - dict(method='DELETE', - uri='{endpoint}/{container}/{object}'.format( - endpoint=endpoint, container=self.container_name, - object=self.image_name)), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + service_type='object-store', + resource=self.container_name, + qs_elements=['format=json'], + ), + json=[ + { + 'content_type': 'application/octet-stream', + 'bytes': 1437258240, + 'hash': '249219347276c331b87bf1ac2152d9af', + 'last_modified': '2015-02-16T17:50:05.289600', + 'name': other_image, + }, + { + 'content_type': 'application/octet-stream', + 'bytes': 1290170880, + 'hash': fakes.NO_MD5, + 'last_modified': '2015-04-14T18:29:00.502530', + 'name': self.image_name, + }, + ], + ), + dict( + method='HEAD', + uri=self.get_mock_url( + service_type='object-store', + resource=self.container_name, + append=[other_image], + ), + headers={ + 'X-Timestamp': '1429036140.50253', + 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', + 'Content-Length': '1290170880', + 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', + 'X-Object-Meta-X-Shade-Sha256': 'does not matter', + 'X-Object-Meta-X-Shade-Md5': 'does not matter', + 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', + 'Accept-Ranges': 'bytes', + 'Content-Type': 'application/octet-stream', + 'Etag': '249219347276c331b87bf1ac2152d9af', + }, + ), + dict( + method='HEAD', + uri=self.get_mock_url( + service_type='object-store', + resource=self.container_name, + append=[self.image_name], + ), + headers={ + 'X-Timestamp': '1429036140.50253', + 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', + 'Content-Length': '1290170880', + 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', + 'X-Object-Meta-X-Shade-Sha256': fakes.NO_SHA256, + 'X-Object-Meta-X-Shade-Md5': fakes.NO_MD5, + 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', + 'Accept-Ranges': 'bytes', + 'Content-Type': 'application/octet-stream', + ( + 'X-Object-Meta-' + + self.cloud._OBJECT_AUTOCREATE_KEY + ): 'true', + 'Etag': fakes.NO_MD5, + 'X-Static-Large-Object': 'false', + }, + ), + dict( + method='DELETE', + uri='{endpoint}/{container}/{object}'.format( + endpoint=endpoint, + container=self.container_name, + object=self.image_name, + ), + ), + ] + ) deleted = self.cloud.delete_autocreated_image_objects( - container=self.container_name) + container=self.container_name + ) self.assertTrue(deleted) self.assert_calls() @@ -858,113 +1216,169 @@ class TestImage(BaseTestImage): imagefile.write(b'\0') imagefile.close() self.cloud.create_image( - name, imagefile.name, wait=True, timeout=1, - is_public=False, validate_checksum=True, **kwargs) + name, + imagefile.name, + wait=True, + timeout=1, + is_public=False, + validate_checksum=True, + **kwargs + ) def test_create_image_put_v1(self): self.cloud.config.config['image_api_version'] = '1' - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': 'qcow2', - 'properties': { - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'is_public': False}} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'properties': { + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'is_public': False, + }, + } ret = args.copy() ret['id'] = self.image_id ret['status'] = 'success' - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v1/images/' + self.image_name, - status_code=404), - dict(method='GET', - uri='https://image.example.com/v1/images/detail?name=' - + self.image_name, - json={'images': []}), - dict(method='POST', - uri='https://image.example.com/v1/images', - json={'image': ret}, - validate=dict(json=args)), - dict(method='PUT', - uri='https://image.example.com/v1/images/{id}'.format( - id=self.image_id), - json={'image': ret}, - validate=dict(headers={ - 'x-image-meta-checksum': fakes.NO_MD5, - 'x-glance-registry-purge-props': 'false' - })), - dict(method='GET', - uri='https://image.example.com/v1/images/detail', - json={'images': [ret]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v1/images/' + + self.image_name, + status_code=404, + ), + dict( + method='GET', + uri='https://image.example.com/v1/images/detail?name=' + + self.image_name, + json={'images': []}, + ), + dict( + method='POST', + uri='https://image.example.com/v1/images', + json={'image': ret}, + validate=dict(json=args), + ), + dict( + method='PUT', + uri='https://image.example.com/v1/images/{id}'.format( + id=self.image_id + ), + json={'image': ret}, + validate=dict( + headers={ + 'x-image-meta-checksum': fakes.NO_MD5, + 'x-glance-registry-purge-props': 'false', + } + ), + ), + dict( + method='GET', + uri='https://image.example.com/v1/images/detail', + json={'images': [ret]}, + ), + ] + ) self._call_create_image(self.image_name) - [self._compare_images_v1(b, a) for a, b in zip( - self.cloud.list_images(), [ret])] + [ + self._compare_images_v1(b, a) + for a, b in zip(self.cloud.list_images(), [ret]) + ] def test_create_image_put_v1_bad_delete(self): self.cloud.config.config['image_api_version'] = '1' - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': 'qcow2', - 'properties': { - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'is_public': False}, - 'validate_checksum': True} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'properties': { + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'is_public': False, + }, + 'validate_checksum': True, + } ret = args.copy() ret['id'] = self.image_id ret['status'] = 'success' - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v1/images/' + self.image_name, - status_code=404), - dict(method='GET', - uri='https://image.example.com/v1/images/detail?name=' - + self.image_name, - json={'images': []}), - dict(method='POST', - uri='https://image.example.com/v1/images', - json={'image': ret}, - validate=dict(json=args)), - dict(method='PUT', - uri='https://image.example.com/v1/images/{id}'.format( - id=self.image_id), - status_code=400, - validate=dict(headers={ - 'x-image-meta-checksum': fakes.NO_MD5, - 'x-glance-registry-purge-props': 'false' - })), - dict(method='DELETE', - uri='https://image.example.com/v1/images/{id}'.format( - id=self.image_id), - json={'images': [ret]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v1/images/' + + self.image_name, + status_code=404, + ), + dict( + method='GET', + uri='https://image.example.com/v1/images/detail?name=' + + self.image_name, + json={'images': []}, + ), + dict( + method='POST', + uri='https://image.example.com/v1/images', + json={'image': ret}, + validate=dict(json=args), + ), + dict( + method='PUT', + uri='https://image.example.com/v1/images/{id}'.format( + id=self.image_id + ), + status_code=400, + validate=dict( + headers={ + 'x-image-meta-checksum': fakes.NO_MD5, + 'x-glance-registry-purge-props': 'false', + } + ), + ), + dict( + method='DELETE', + uri='https://image.example.com/v1/images/{id}'.format( + id=self.image_id + ), + json={'images': [ret]}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudHTTPError, self._call_create_image, - self.image_name) + self.image_name, + ) self.assert_calls() def test_update_image_no_patch(self): self.cloud.image_api_use_tasks = False - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': 'qcow2', - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'visibility': 'private'} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'visibility': 'private', + } ret = args.copy() ret['id'] = self.image_id @@ -972,67 +1386,98 @@ class TestImage(BaseTestImage): self.cloud.update_image_properties( image=image.Image.existing(**ret), - **{'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name)}) + **{ + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ) + } + ) self.assert_calls() def test_create_image_put_v2_bad_delete(self): self.cloud.image_api_use_tasks = False - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': 'qcow2', - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'visibility': 'private'} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'visibility': 'private', + } ret = args.copy() ret['id'] = self.image_id ret['status'] = 'success' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri='https://image.example.com/v2/images', - json=ret, - validate=dict(json=args)), - dict(method='PUT', - uri='https://image.example.com/v2/images/{id}/file'.format( - id=self.image_id), - status_code=400, - validate=dict( - headers={ - 'Content-Type': 'application/octet-stream', - }, - )), - dict(method='DELETE', - uri='https://image.example.com/v2/images/{id}'.format( - id=self.image_id)), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri='https://image.example.com/v2/images', + json=ret, + validate=dict(json=args), + ), + dict( + method='PUT', + uri='https://image.example.com/v2/images/{id}/file'.format( + id=self.image_id + ), + status_code=400, + validate=dict( + headers={ + 'Content-Type': 'application/octet-stream', + }, + ), + ), + dict( + method='DELETE', + uri='https://image.example.com/v2/images/{id}'.format( + id=self.image_id + ), + ), + ] + ) self.assertRaises( exc.OpenStackCloudHTTPError, self._call_create_image, - self.image_name) + self.image_name, + ) self.assert_calls() @@ -1044,47 +1489,69 @@ class TestImage(BaseTestImage): fake_image['owner_specified.openstack.md5'] = 'a' fake_image['owner_specified.openstack.sha256'] = 'b' - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_image_dict, - validate=dict( - json={ - u'container_format': u'bare', - u'disk_format': u'qcow2', - u'name': self.image_name, - u'owner_specified.openstack.md5': - fake_image[ - 'owner_specified.openstack.md5'], - u'owner_specified.openstack.object': self.object_name, - u'owner_specified.openstack.sha256': - fake_image[ - 'owner_specified.openstack.sha256'], - u'visibility': u'private'}) - ), - dict(method='PUT', - uri=self.get_mock_url( - 'image', append=['images', self.image_id, 'file'], - base_url_append='v2'), - request_headers={'Content-Type': 'application/octet-stream'}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.fake_image_dict['id']], - base_url_append='v2' - ), - json=fake_image), - dict(method='DELETE', - uri='https://image.example.com/v2/images/{id}'.format( - id=self.image_id)) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_image_dict, + validate=dict( + json={ + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'name': self.image_name, + 'owner_specified.openstack.md5': fake_image[ + 'owner_specified.openstack.md5' + ], + 'owner_specified.openstack.object': self.object_name, # noqa: E501 + 'owner_specified.openstack.sha256': fake_image[ + 'owner_specified.openstack.sha256' + ], + 'visibility': 'private', + } + ), + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'image', + append=['images', self.image_id, 'file'], + base_url_append='v2', + ), + request_headers={ + 'Content-Type': 'application/octet-stream' + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.fake_image_dict['id']], + base_url_append='v2', + ), + json=fake_image, + ), + dict( + method='DELETE', + uri='https://image.example.com/v2/images/{id}'.format( + id=self.image_id + ), + ), + ] + ) self.assertRaises( exceptions.SDKException, self.cloud.create_image, - self.image_name, self.imagefile.name, - is_public=False, md5='a', sha256='b', - allow_duplicates=True, validate_checksum=True + self.image_name, + self.imagefile.name, + is_public=False, + md5='a', + sha256='b', + allow_duplicates=True, + validate_checksum=True, ) self.assert_calls() @@ -1094,211 +1561,303 @@ class TestImage(BaseTestImage): self.assertRaises( exc.OpenStackCloudException, - self._call_create_image, self.image_name, + self._call_create_image, + self.image_name, allow_duplicates=True, - min_disk='fish', min_ram=0) + min_disk='fish', + min_ram=0, + ) self.assert_calls() def test_create_image_put_user_int(self): self.cloud.image_api_use_tasks = False - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': u'qcow2', - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'int_v': '12345', - 'visibility': 'private', - 'min_disk': 0, 'min_ram': 0} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'int_v': '12345', + 'visibility': 'private', + 'min_disk': 0, + 'min_ram': 0, + } ret = args.copy() ret['id'] = self.image_id ret['status'] = 'success' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri='https://image.example.com/v2/images', - json=ret, - validate=dict(json=args)), - dict(method='PUT', - uri='https://image.example.com/v2/images/{id}/file'.format( - id=self.image_id), - validate=dict( - headers={ - 'Content-Type': 'application/octet-stream', - }, - )), - dict(method='GET', - uri='https://image.example.com/v2/images/{id}'.format( - id=self.image_id - ), - json=ret), - dict(method='GET', - uri='https://image.example.com/v2/images', - complete_qs=True, - json={'images': [ret]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri='https://image.example.com/v2/images', + json=ret, + validate=dict(json=args), + ), + dict( + method='PUT', + uri='https://image.example.com/v2/images/{id}/file'.format( + id=self.image_id + ), + validate=dict( + headers={ + 'Content-Type': 'application/octet-stream', + }, + ), + ), + dict( + method='GET', + uri='https://image.example.com/v2/images/{id}'.format( + id=self.image_id + ), + json=ret, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + complete_qs=True, + json={'images': [ret]}, + ), + ] + ) self._call_create_image( - self.image_name, min_disk='0', min_ram=0, int_v=12345) + self.image_name, min_disk='0', min_ram=0, int_v=12345 + ) self.assert_calls() def test_create_image_put_meta_int(self): self.cloud.image_api_use_tasks = False - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': u'qcow2', - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'int_v': 12345, - 'visibility': 'private', - 'min_disk': 0, 'min_ram': 0} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'int_v': 12345, + 'visibility': 'private', + 'min_disk': 0, + 'min_ram': 0, + } ret = args.copy() ret['id'] = self.image_id ret['status'] = 'success' ret['checksum'] = fakes.NO_MD5 - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri='https://image.example.com/v2/images', - json=ret, - validate=dict(json=args)), - dict(method='PUT', - uri='https://image.example.com/v2/images/{id}/file'.format( - id=self.image_id), - validate=dict( - headers={ - 'Content-Type': 'application/octet-stream', - }, - )), - dict(method='GET', - uri='https://image.example.com/v2/images/{id}'.format( - id=self.image_id - ), - json=ret), - dict(method='GET', - uri='https://image.example.com/v2/images', - complete_qs=True, - json={'images': [ret]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri='https://image.example.com/v2/images', + json=ret, + validate=dict(json=args), + ), + dict( + method='PUT', + uri='https://image.example.com/v2/images/{id}/file'.format( + id=self.image_id + ), + validate=dict( + headers={ + 'Content-Type': 'application/octet-stream', + }, + ), + ), + dict( + method='GET', + uri='https://image.example.com/v2/images/{id}'.format( + id=self.image_id + ), + json=ret, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + complete_qs=True, + json={'images': [ret]}, + ), + ] + ) self._call_create_image( - self.image_name, min_disk='0', min_ram=0, meta={'int_v': 12345}) + self.image_name, min_disk='0', min_ram=0, meta={'int_v': 12345} + ) self.assert_calls() def test_create_image_put_protected(self): self.cloud.image_api_use_tasks = False - args = {'name': self.image_name, - 'container_format': 'bare', 'disk_format': u'qcow2', - 'owner_specified.openstack.md5': fakes.NO_MD5, - 'owner_specified.openstack.sha256': fakes.NO_SHA256, - 'owner_specified.openstack.object': 'images/{name}'.format( - name=self.image_name), - 'int_v': '12345', - 'protected': False, - 'visibility': 'private', - 'min_disk': 0, 'min_ram': 0} + args = { + 'name': self.image_name, + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'owner_specified.openstack.md5': fakes.NO_MD5, + 'owner_specified.openstack.sha256': fakes.NO_SHA256, + 'owner_specified.openstack.object': 'images/{name}'.format( + name=self.image_name + ), + 'int_v': '12345', + 'protected': False, + 'visibility': 'private', + 'min_disk': 0, + 'min_ram': 0, + } ret = args.copy() ret['id'] = self.image_id ret['status'] = 'success' ret['checksum'] = fakes.NO_MD5 - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images', self.image_name], - base_url_append='v2'), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['name=' + self.image_name]), - validate=dict(), - json={'images': []}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], - base_url_append='v2', - qs_elements=['os_hidden=True']), - json={'images': []}), - dict(method='POST', - uri='https://image.example.com/v2/images', - json=ret, - validate=dict(json=args)), - dict(method='PUT', - uri='https://image.example.com/v2/images/{id}/file'.format( - id=self.image_id), - validate=dict( - headers={ - 'Content-Type': 'application/octet-stream', - }, - )), - dict(method='GET', - uri='https://image.example.com/v2/images/{id}'.format( - id=self.image_id - ), - json=ret), - dict(method='GET', - uri='https://image.example.com/v2/images', - complete_qs=True, - json={'images': [ret]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images', self.image_name], + base_url_append='v2', + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['name=' + self.image_name], + ), + validate=dict(), + json={'images': []}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['os_hidden=True'], + ), + json={'images': []}, + ), + dict( + method='POST', + uri='https://image.example.com/v2/images', + json=ret, + validate=dict(json=args), + ), + dict( + method='PUT', + uri='https://image.example.com/v2/images/{id}/file'.format( + id=self.image_id + ), + validate=dict( + headers={ + 'Content-Type': 'application/octet-stream', + }, + ), + ), + dict( + method='GET', + uri='https://image.example.com/v2/images/{id}'.format( + id=self.image_id + ), + json=ret, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + complete_qs=True, + json={'images': [ret]}, + ), + ] + ) self._call_create_image( - self.image_name, min_disk='0', min_ram=0, - properties={'int_v': 12345}, is_protected=False) + self.image_name, + min_disk='0', + min_ram=0, + properties={'int_v': 12345}, + is_protected=False, + ) self.assert_calls() class TestImageSuburl(BaseTestImage): - def setUp(self): super(TestImageSuburl, self).setUp() self.os_fixture.use_suburl() @@ -1306,43 +1865,66 @@ class TestImageSuburl(BaseTestImage): self.use_keystone_v3() self.use_glance( image_version_json='image-version-suburl.json', - image_discovery_url='https://example.com/image') + image_discovery_url='https://example.com/image', + ) def test_list_images(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return) - ]) - [self._compare_images(b, a) for a, b in zip( - self.cloud.list_images(), - [self.fake_image_dict])] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ) + ] + ) + [ + self._compare_images(b, a) + for a, b in zip(self.cloud.list_images(), [self.fake_image_dict]) + ] self.assert_calls() def test_list_images_paginated(self): marker = str(uuid.uuid4()) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': [self.fake_image_dict], - 'next': '/v2/images?marker={marker}'.format( - marker=marker)}), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2', - qs_elements=['marker={marker}'.format(marker=marker)]), - json=self.fake_search_return) - ]) - [self._compare_images(b, a) for a, b in zip( - self.cloud.list_images(), - [self.fake_image_dict, self.fake_image_dict])] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={ + 'images': [self.fake_image_dict], + 'next': '/v2/images?marker={marker}'.format( + marker=marker + ), + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'image', + append=['images'], + base_url_append='v2', + qs_elements=['marker={marker}'.format(marker=marker)], + ), + json=self.fake_search_return, + ), + ] + ) + [ + self._compare_images(b, a) + for a, b in zip( + self.cloud.list_images(), + [self.fake_image_dict, self.fake_image_dict], + ) + ] self.assert_calls() class TestImageV1Only(base.TestCase): - def setUp(self): super(TestImageV1Only, self).setUp() self.use_glance(image_version_json='image-version-v1.json') @@ -1354,7 +1936,8 @@ class TestImageV1Only(base.TestCase): # because glance has a bug where it doesn't return https properly. self.assertEqual( 'https://image.example.com/v1/', - self.cloud._image_client.get_endpoint()) + self.cloud._image_client.get_endpoint(), + ) self.assertTrue(self.cloud._is_client_version('image', 1)) def test_config_v2(self): @@ -1363,12 +1946,12 @@ class TestImageV1Only(base.TestCase): # because glance has a bug where it doesn't return https properly. self.assertEqual( 'https://image.example.com/v1/', - self.cloud._image_client.get_endpoint()) + self.cloud._image_client.get_endpoint(), + ) self.assertFalse(self.cloud._is_client_version('image', 2)) class TestImageV2Only(base.TestCase): - def setUp(self): super(TestImageV2Only, self).setUp() self.use_glance(image_version_json='image-version-v2.json') @@ -1379,7 +1962,8 @@ class TestImageV2Only(base.TestCase): # because glance has a bug where it doesn't return https properly. self.assertEqual( 'https://image.example.com/v2/', - self.cloud._image_client.get_endpoint()) + self.cloud._image_client.get_endpoint(), + ) self.assertTrue(self.cloud._is_client_version('image', 2)) def test_config_v2(self): @@ -1388,82 +1972,118 @@ class TestImageV2Only(base.TestCase): # because glance has a bug where it doesn't return https properly. self.assertEqual( 'https://image.example.com/v2/', - self.cloud._image_client.get_endpoint()) + self.cloud._image_client.get_endpoint(), + ) self.assertTrue(self.cloud._is_client_version('image', 2)) class TestImageVolume(BaseTestImage): - def setUp(self): super(TestImageVolume, self).setUp() self.volume_id = str(uuid.uuid4()) def test_create_image_volume(self): - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', append=['volumes', self.volume_id, 'action']), - json={'os-volume_upload_image': {'image_id': self.image_id}}, - validate=dict(json={ - u'os-volume_upload_image': { - u'container_format': u'bare', - u'disk_format': u'qcow2', - u'force': False, - u'image_name': u'fake_image'}}) - ), - # NOTE(notmorgan): Glance discovery happens here, insert the - # glance discovery mock at this point, DO NOT use the - # .use_glance() method, that is intended only for use in - # .setUp - self.get_glance_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return) - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + append=['volumes', self.volume_id, 'action'], + ), + json={ + 'os-volume_upload_image': {'image_id': self.image_id} + }, + validate=dict( + json={ + 'os-volume_upload_image': { + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'force': False, + 'image_name': 'fake_image', + } + } + ), + ), + # NOTE(notmorgan): Glance discovery happens here, insert the + # glance discovery mock at this point, DO NOT use the + # .use_glance() method, that is intended only for use in + # .setUp + self.get_glance_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + ] + ) self.cloud.create_image( - 'fake_image', self.imagefile.name, wait=True, timeout=1, - volume={'id': self.volume_id}) + 'fake_image', + self.imagefile.name, + wait=True, + timeout=1, + volume={'id': self.volume_id}, + ) self.assert_calls() def test_create_image_volume_duplicate(self): - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', append=['volumes', self.volume_id, 'action']), - json={'os-volume_upload_image': {'image_id': self.image_id}}, - validate=dict(json={ - u'os-volume_upload_image': { - u'container_format': u'bare', - u'disk_format': u'qcow2', - u'force': True, - u'image_name': u'fake_image'}}) - ), - # NOTE(notmorgan): Glance discovery happens here, insert the - # glance discovery mock at this point, DO NOT use the - # .use_glance() method, that is intended only for use in - # .setUp - self.get_glance_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json=self.fake_search_return) - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + append=['volumes', self.volume_id, 'action'], + ), + json={ + 'os-volume_upload_image': {'image_id': self.image_id} + }, + validate=dict( + json={ + 'os-volume_upload_image': { + 'container_format': 'bare', + 'disk_format': 'qcow2', + 'force': True, + 'image_name': 'fake_image', + } + } + ), + ), + # NOTE(notmorgan): Glance discovery happens here, insert the + # glance discovery mock at this point, DO NOT use the + # .use_glance() method, that is intended only for use in + # .setUp + self.get_glance_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json=self.fake_search_return, + ), + ] + ) self.cloud.create_image( - 'fake_image', self.imagefile.name, wait=True, timeout=1, - volume={'id': self.volume_id}, allow_duplicates=True) + 'fake_image', + self.imagefile.name, + wait=True, + timeout=1, + volume={'id': self.volume_id}, + allow_duplicates=True, + ) self.assert_calls() class TestImageBrokenDiscovery(base.TestCase): - def setUp(self): super(TestImageBrokenDiscovery, self).setUp() self.use_glance(image_version_json='image-version-broken.json') @@ -1473,14 +2093,20 @@ class TestImageBrokenDiscovery(base.TestCase): # host. This is testing that what is discovered is https, because # that's what's in the catalog, and image.example.com for the same # reason. - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'image', append=['images'], base_url_append='v2'), - json={'images': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'image', append=['images'], base_url_append='v2' + ), + json={'images': []}, + ) + ] + ) self.assertEqual([], self.cloud.list_images()) self.assertEqual( self.cloud._image_client.get_endpoint(), - 'https://image.example.com/v2/') + 'https://image.example.com/v2/', + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_image_snapshot.py b/openstack/tests/unit/cloud/test_image_snapshot.py index 218ee8de2..2e33be1a4 100644 --- a/openstack/tests/unit/cloud/test_image_snapshot.py +++ b/openstack/tests/unit/cloud/test_image_snapshot.py @@ -20,47 +20,59 @@ from openstack.tests.unit import base class TestImageSnapshot(base.TestCase): - def setUp(self): super(TestImageSnapshot, self).setUp() self.server_id = str(uuid.uuid4()) self.image_id = str(uuid.uuid4()) self.server_name = self.getUniqueString('name') self.fake_server = fakes.make_fake_server( - self.server_id, self.server_name) + self.server_id, self.server_name + ) def test_create_image_snapshot_wait_until_active_never_active(self): snapshot_name = 'test-snapshot' fake_image = fakes.make_fake_image(self.image_id, status='pending') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict( - method='POST', - uri='{endpoint}/servers/{server_id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - server_id=self.server_id), - headers=dict( - Location='{endpoint}/images/{image_id}'.format( - endpoint='https://images.example.com', - image_id=self.image_id)), - validate=dict( - json={ - "createImage": { - "name": snapshot_name, - "metadata": {}, - }})), - self.get_glance_discovery_mock_dict(), - dict( - method='GET', - uri='https://image.example.com/v2/images', - json=dict(images=[fake_image])), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri='{endpoint}/servers/{server_id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, + server_id=self.server_id, + ), + headers=dict( + Location='{endpoint}/images/{image_id}'.format( + endpoint='https://images.example.com', + image_id=self.image_id, + ) + ), + validate=dict( + json={ + "createImage": { + "name": snapshot_name, + "metadata": {}, + } + } + ), + ), + self.get_glance_discovery_mock_dict(), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=dict(images=[fake_image]), + ), + ] + ) self.assertRaises( exc.OpenStackCloudTimeout, self.cloud.create_image_snapshot, - snapshot_name, dict(id=self.server_id), - wait=True, timeout=0.01) + snapshot_name, + dict(id=self.server_id), + wait=True, + timeout=0.01, + ) # After the fifth call, we just keep polling get images for status. # Due to mocking sleep, we have no clue how many times we'll call it. @@ -70,35 +82,46 @@ class TestImageSnapshot(base.TestCase): snapshot_name = 'test-snapshot' pending_image = fakes.make_fake_image(self.image_id, status='pending') fake_image = fakes.make_fake_image(self.image_id) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict( - method='POST', - uri='{endpoint}/servers/{server_id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - server_id=self.server_id), - headers=dict( - Location='{endpoint}/images/{image_id}'.format( - endpoint='https://images.example.com', - image_id=self.image_id)), - validate=dict( - json={ - "createImage": { - "name": snapshot_name, - "metadata": {}, - }})), - self.get_glance_discovery_mock_dict(), - dict( - method='GET', - uri='https://image.example.com/v2/images', - json=dict(images=[pending_image])), - dict( - method='GET', - uri='https://image.example.com/v2/images', - json=dict(images=[fake_image])), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri='{endpoint}/servers/{server_id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, + server_id=self.server_id, + ), + headers=dict( + Location='{endpoint}/images/{image_id}'.format( + endpoint='https://images.example.com', + image_id=self.image_id, + ) + ), + validate=dict( + json={ + "createImage": { + "name": snapshot_name, + "metadata": {}, + } + } + ), + ), + self.get_glance_discovery_mock_dict(), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=dict(images=[pending_image]), + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=dict(images=[fake_image]), + ), + ] + ) image = self.cloud.create_image_snapshot( - 'test-snapshot', dict(id=self.server_id), wait=True, timeout=2) + 'test-snapshot', dict(id=self.server_id), wait=True, timeout=2 + ) self.assertEqual(image['id'], self.image_id) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_inventory.py b/openstack/tests/unit/cloud/test_inventory.py index e51f271d7..c6520897e 100644 --- a/openstack/tests/unit/cloud/test_inventory.py +++ b/openstack/tests/unit/cloud/test_inventory.py @@ -19,7 +19,6 @@ from openstack.tests.unit import base class TestInventory(base.TestCase): - def setUp(self): super(TestInventory, self).setUp() @@ -50,8 +49,7 @@ class TestInventory(base.TestCase): self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) self.assertFalse(mock_config.return_value.get_all.called) - mock_config.return_value.get_one.assert_called_once_with( - 'supercloud') + mock_config.return_value.get_one.assert_called_once_with('supercloud') @mock.patch("openstack.config.loader.OpenStackConfig") @mock.patch("openstack.connection.Connection") @@ -68,8 +66,9 @@ class TestInventory(base.TestCase): ret = inv.list_hosts() - inv.clouds[0].list_servers.assert_called_once_with(detailed=True, - all_projects=False) + inv.clouds[0].list_servers.assert_called_once_with( + detailed=True, all_projects=False + ) self.assertFalse(inv.clouds[0].get_openstack_vars.called) self.assertEqual([server], ret) @@ -81,16 +80,17 @@ class TestInventory(base.TestCase): inv = inventory.OpenStackInventory() server = self.cloud._normalize_server( - fakes.make_fake_server( - '1234', 'test', 'ACTIVE', addresses={})) + fakes.make_fake_server('1234', 'test', 'ACTIVE', addresses={}) + ) self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) inv.clouds[0].list_servers.return_value = [server] inv.list_hosts(expand=False) - inv.clouds[0].list_servers.assert_called_once_with(detailed=False, - all_projects=False) + inv.clouds[0].list_servers.assert_called_once_with( + detailed=False, all_projects=False + ) self.assertFalse(inv.clouds[0].get_openstack_vars.called) @mock.patch("openstack.config.loader.OpenStackConfig") @@ -108,8 +108,9 @@ class TestInventory(base.TestCase): ret = inv.list_hosts(all_projects=True) - inv.clouds[0].list_servers.assert_called_once_with(detailed=True, - all_projects=True) + inv.clouds[0].list_servers.assert_called_once_with( + detailed=True, all_projects=True + ) self.assertFalse(inv.clouds[0].get_openstack_vars.called) self.assertEqual([server], ret) diff --git a/openstack/tests/unit/cloud/test_keypair.py b/openstack/tests/unit/cloud/test_keypair.py index e29c7306a..20410609f 100644 --- a/openstack/tests/unit/cloud/test_keypair.py +++ b/openstack/tests/unit/cloud/test_keypair.py @@ -19,29 +19,41 @@ from openstack.tests.unit import base class TestKeypair(base.TestCase): - def setUp(self): super(TestKeypair, self).setUp() self.keyname = self.getUniqueString('key') self.key = fakes.make_fake_keypair(self.keyname) - self.useFixture(fixtures.MonkeyPatch( - 'openstack.utils.maximum_supported_microversion', - lambda *args, **kwargs: '2.10')) + self.useFixture( + fixtures.MonkeyPatch( + 'openstack.utils.maximum_supported_microversion', + lambda *args, **kwargs: '2.10', + ) + ) def test_create_keypair(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['os-keypairs']), - json={'keypair': self.key}, - validate=dict(json={ - 'keypair': { - 'name': self.key['name'], - 'public_key': self.key['public_key']}})), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['os-keypairs'] + ), + json={'keypair': self.key}, + validate=dict( + json={ + 'keypair': { + 'name': self.key['name'], + 'public_key': self.key['public_key'], + } + } + ), + ), + ] + ) new_key = self.cloud.create_keypair( - self.keyname, self.key['public_key']) + self.keyname, self.key['public_key'] + ) new_key_cmp = new_key.to_dict(ignore_none=True) new_key_cmp.pop('location') new_key_cmp.pop('id') @@ -50,97 +62,140 @@ class TestKeypair(base.TestCase): self.assert_calls() def test_create_keypair_exception(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['os-keypairs']), - status_code=400, - validate=dict(json={ - 'keypair': { - 'name': self.key['name'], - 'public_key': self.key['public_key']}})), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['os-keypairs'] + ), + status_code=400, + validate=dict( + json={ + 'keypair': { + 'name': self.key['name'], + 'public_key': self.key['public_key'], + } + } + ), + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.create_keypair, - self.keyname, self.key['public_key']) + self.keyname, + self.key['public_key'], + ) self.assert_calls() def test_delete_keypair(self): - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-keypairs', self.keyname]), - status_code=202), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-keypairs', self.keyname], + ), + status_code=202, + ), + ] + ) self.assertTrue(self.cloud.delete_keypair(self.keyname)) self.assert_calls() def test_delete_keypair_not_found(self): - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-keypairs', self.keyname]), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-keypairs', self.keyname], + ), + status_code=404, + ), + ] + ) self.assertFalse(self.cloud.delete_keypair(self.keyname)) self.assert_calls() def test_list_keypairs(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-keypairs']), - json={'keypairs': [{'keypair': self.key}]}), - - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-keypairs'] + ), + json={'keypairs': [{'keypair': self.key}]}, + ), + ] + ) keypairs = self.cloud.list_keypairs() self.assertEqual(len(keypairs), 1) self.assertEqual(keypairs[0].name, self.key['name']) self.assert_calls() def test_list_keypairs_empty_filters(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-keypairs']), - json={'keypairs': [{'keypair': self.key}]}), - - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-keypairs'] + ), + json={'keypairs': [{'keypair': self.key}]}, + ), + ] + ) keypairs = self.cloud.list_keypairs(filters=None) self.assertEqual(len(keypairs), 1) self.assertEqual(keypairs[0].name, self.key['name']) self.assert_calls() def test_list_keypairs_notempty_filters(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-keypairs'], - qs_elements=['user_id=b']), - json={'keypairs': [{'keypair': self.key}]}), - - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-keypairs'], + qs_elements=['user_id=b'], + ), + json={'keypairs': [{'keypair': self.key}]}, + ), + ] + ) keypairs = self.cloud.list_keypairs( - filters={'user_id': 'b', 'fake': 'dummy'}) + filters={'user_id': 'b', 'fake': 'dummy'} + ) self.assertEqual(len(keypairs), 1) self.assertEqual(keypairs[0].name, self.key['name']) self.assert_calls() def test_list_keypairs_exception(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-keypairs']), - status_code=400), - - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.list_keypairs) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-keypairs'] + ), + status_code=400, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, self.cloud.list_keypairs + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_limits.py b/openstack/tests/unit/cloud/test_limits.py index f731a1ae7..8481ca50d 100644 --- a/openstack/tests/unit/cloud/test_limits.py +++ b/openstack/tests/unit/cloud/test_limits.py @@ -14,81 +14,93 @@ from openstack.tests.unit import base class TestLimits(base.TestCase): - def test_get_compute_limits(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['limits']), - json={ - "limits": { - "absolute": { - "maxImageMeta": 128, - "maxPersonality": 5, - "maxPersonalitySize": 10240, - "maxSecurityGroupRules": 20, - "maxSecurityGroups": 10, - "maxServerMeta": 128, - "maxTotalCores": 20, - "maxTotalFloatingIps": 10, - "maxTotalInstances": 10, - "maxTotalKeypairs": 100, - "maxTotalRAMSize": 51200, - "maxServerGroups": 10, - "maxServerGroupMembers": 10, - "totalCoresUsed": 0, - "totalInstancesUsed": 0, - "totalRAMUsed": 0, - "totalSecurityGroupsUsed": 0, - "totalFloatingIpsUsed": 0, - "totalServerGroupsUsed": 0 - }, - "rate": [] - } - }), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['limits'] + ), + json={ + "limits": { + "absolute": { + "maxImageMeta": 128, + "maxPersonality": 5, + "maxPersonalitySize": 10240, + "maxSecurityGroupRules": 20, + "maxSecurityGroups": 10, + "maxServerMeta": 128, + "maxTotalCores": 20, + "maxTotalFloatingIps": 10, + "maxTotalInstances": 10, + "maxTotalKeypairs": 100, + "maxTotalRAMSize": 51200, + "maxServerGroups": 10, + "maxServerGroupMembers": 10, + "totalCoresUsed": 0, + "totalInstancesUsed": 0, + "totalRAMUsed": 0, + "totalSecurityGroupsUsed": 0, + "totalFloatingIpsUsed": 0, + "totalServerGroupsUsed": 0, + }, + "rate": [], + } + }, + ), + ] + ) self.cloud.get_compute_limits() self.assert_calls() def test_other_get_compute_limits(self): - project = self.mock_for_keystone_projects(project_count=1, - list_get=True)[0] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['limits'], - qs_elements=[ - 'tenant_id={id}'.format(id=project.project_id) - ]), - json={ - "limits": { - "absolute": { - "maxImageMeta": 128, - "maxPersonality": 5, - "maxPersonalitySize": 10240, - "maxSecurityGroupRules": 20, - "maxSecurityGroups": 10, - "maxServerMeta": 128, - "maxTotalCores": 20, - "maxTotalFloatingIps": 10, - "maxTotalInstances": 10, - "maxTotalKeypairs": 100, - "maxTotalRAMSize": 51200, - "maxServerGroups": 10, - "maxServerGroupMembers": 10, - "totalCoresUsed": 0, - "totalInstancesUsed": 0, - "totalRAMUsed": 0, - "totalSecurityGroupsUsed": 0, - "totalFloatingIpsUsed": 0, - "totalServerGroupsUsed": 0 - }, - "rate": [] - } - }), - ]) + project = self.mock_for_keystone_projects( + project_count=1, list_get=True + )[0] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['limits'], + qs_elements=[ + 'tenant_id={id}'.format(id=project.project_id) + ], + ), + json={ + "limits": { + "absolute": { + "maxImageMeta": 128, + "maxPersonality": 5, + "maxPersonalitySize": 10240, + "maxSecurityGroupRules": 20, + "maxSecurityGroups": 10, + "maxServerMeta": 128, + "maxTotalCores": 20, + "maxTotalFloatingIps": 10, + "maxTotalInstances": 10, + "maxTotalKeypairs": 100, + "maxTotalRAMSize": 51200, + "maxServerGroups": 10, + "maxServerGroupMembers": 10, + "totalCoresUsed": 0, + "totalInstancesUsed": 0, + "totalRAMUsed": 0, + "totalSecurityGroupsUsed": 0, + "totalFloatingIpsUsed": 0, + "totalServerGroupsUsed": 0, + }, + "rate": [], + } + }, + ), + ] + ) self.cloud.get_compute_limits(project.project_id) diff --git a/openstack/tests/unit/cloud/test_magnum_services.py b/openstack/tests/unit/cloud/test_magnum_services.py index 94ff8da85..d30531d4b 100644 --- a/openstack/tests/unit/cloud/test_magnum_services.py +++ b/openstack/tests/unit/cloud/test_magnum_services.py @@ -27,14 +27,19 @@ magnum_service_obj = dict( class TestMagnumServices(base.TestCase): - def test_list_magnum_services(self): - self.register_uris([dict( - method='GET', - uri=self.get_mock_url( - service_type='container-infrastructure-management', - resource='mservices'), - json=dict(mservices=[magnum_service_obj]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + service_type='container-infrastructure-management', + resource='mservices', + ), + json=dict(mservices=[magnum_service_obj]), + ) + ] + ) mservices_list = self.cloud.list_magnum_services() self.assertEqual( mservices_list[0].to_dict(computed=False), diff --git a/openstack/tests/unit/cloud/test_meta.py b/openstack/tests/unit/cloud/test_meta.py index 39f0bb4a9..56d95648d 100644 --- a/openstack/tests/unit/cloud/test_meta.py +++ b/openstack/tests/unit/cloud/test_meta.py @@ -88,12 +88,14 @@ standard_fake_server = fakes.make_fake_server( server_id='test-id-0', name='test-id-0', status='ACTIVE', - addresses={'private': [{'OS-EXT-IPS:type': 'fixed', - 'addr': PRIVATE_V4, - 'version': 4}], - 'public': [{'OS-EXT-IPS:type': 'floating', - 'addr': PUBLIC_V4, - 'version': 4}]}, + addresses={ + 'private': [ + {'OS-EXT-IPS:type': 'fixed', 'addr': PRIVATE_V4, 'version': 4} + ], + 'public': [ + {'OS-EXT-IPS:type': 'floating', 'addr': PUBLIC_V4, 'version': 4} + ], + }, flavor={'id': '101'}, image={'id': '471c2475-da2f-47ac-aba5-cb4aa3d546f5'}, ) @@ -108,17 +110,14 @@ SUBNETS_WITH_NAT = [ u'dns_nameservers': [], u'ipv6_ra_mode': None, u'allocation_pools': [ - { - u'start': u'10.10.10.2', - u'end': u'10.10.10.254' - } + {u'start': u'10.10.10.2', u'end': u'10.10.10.254'} ], u'gateway_ip': u'10.10.10.1', u'ipv6_address_mode': None, u'ip_version': 4, u'host_routes': [], u'cidr': u'10.10.10.0/24', - u'id': u'14025a85-436e-4418-b0ee-f5b12a50f9b4' + u'id': u'14025a85-436e-4418-b0ee-f5b12a50f9b4', }, ] @@ -132,7 +131,7 @@ OSIC_NETWORKS = [ u'shared': True, u'status': u'ACTIVE', u'subnets': [u'cf785ee0-6cc9-4712-be3d-0bf6c86cf455'], - u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32' + u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32', }, { u'admin_state_up': True, @@ -143,7 +142,7 @@ OSIC_NETWORKS = [ u'shared': False, u'status': u'ACTIVE', u'subnets': [u'a47910bc-f649-45db-98ec-e2421c413f4e'], - u'tenant_id': u'7e9c4d5842b3451d94417bd0af03a0f4' + u'tenant_id': u'7e9c4d5842b3451d94417bd0af03a0f4', }, { u'admin_state_up': True, @@ -153,17 +152,19 @@ OSIC_NETWORKS = [ u'router:external': True, u'shared': True, u'status': u'ACTIVE', - u'subnets': [u'9c21d704-a8b9-409a-b56d-501cb518d380', - u'7cb0ce07-64c3-4a3d-92d3-6f11419b45b9'], - u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32' - } + u'subnets': [ + u'9c21d704-a8b9-409a-b56d-501cb518d380', + u'7cb0ce07-64c3-4a3d-92d3-6f11419b45b9', + ], + u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32', + }, ] OSIC_SUBNETS = [ { - u'allocation_pools': [{ - u'end': u'172.99.106.254', - u'start': u'172.99.106.5'}], + u'allocation_pools': [ + {u'end': u'172.99.106.254', u'start': u'172.99.106.5'} + ], u'cidr': u'172.99.106.0/24', u'dns_nameservers': [u'69.20.0.164', u'69.20.0.196'], u'enable_dhcp': True, @@ -176,11 +177,10 @@ OSIC_SUBNETS = [ u'name': u'GATEWAY_NET', u'network_id': u'7004a83a-13d3-4dcd-8cf5-52af1ace4cae', u'subnetpool_id': None, - u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32' + u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32', }, { - u'allocation_pools': [{ - u'end': u'10.0.1.254', u'start': u'10.0.1.2'}], + u'allocation_pools': [{u'end': u'10.0.1.254', u'start': u'10.0.1.2'}], u'cidr': u'10.0.1.0/24', u'dns_nameservers': [u'8.8.8.8', u'8.8.4.4'], u'enable_dhcp': True, @@ -193,11 +193,12 @@ OSIC_SUBNETS = [ u'name': u'openstackjenkins-subnet1', u'network_id': u'405abfcc-77dc-49b2-a271-139619ac9b26', u'subnetpool_id': None, - u'tenant_id': u'7e9c4d5842b3451d94417bd0af03a0f4' + u'tenant_id': u'7e9c4d5842b3451d94417bd0af03a0f4', }, { - u'allocation_pools': [{ - u'end': u'10.255.255.254', u'start': u'10.0.0.2'}], + u'allocation_pools': [ + {u'end': u'10.255.255.254', u'start': u'10.0.0.2'} + ], u'cidr': u'10.0.0.0/8', u'dns_nameservers': [u'8.8.8.8', u'8.8.4.4'], u'enable_dhcp': True, @@ -210,12 +211,15 @@ OSIC_SUBNETS = [ u'name': u'GATEWAY_SUBNET_V6V4', u'network_id': u'54753d2c-0a58-4928-9b32-084c59dd20a6', u'subnetpool_id': None, - u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32' + u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32', }, { - u'allocation_pools': [{ - u'end': u'2001:4800:1ae1:18:ffff:ffff:ffff:ffff', - u'start': u'2001:4800:1ae1:18::2'}], + u'allocation_pools': [ + { + u'end': u'2001:4800:1ae1:18:ffff:ffff:ffff:ffff', + u'start': u'2001:4800:1ae1:18::2', + } + ], u'cidr': u'2001:4800:1ae1:18::/64', u'dns_nameservers': [u'2001:4860:4860::8888'], u'enable_dhcp': True, @@ -228,133 +232,212 @@ OSIC_SUBNETS = [ u'name': u'GATEWAY_SUBNET_V6V6', u'network_id': u'54753d2c-0a58-4928-9b32-084c59dd20a6', u'subnetpool_id': None, - u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32' - } + u'tenant_id': u'7a1ca9f7cc4e4b13ac0ed2957f1e8c32', + }, ] class TestMeta(base.TestCase): def test_find_nova_addresses_key_name(self): # Note 198.51.100.0/24 is TEST-NET-2 from rfc5737 - addrs = {'public': [{'addr': '198.51.100.1', 'version': 4}], - 'private': [{'addr': '192.0.2.5', 'version': 4}]} + addrs = { + 'public': [{'addr': '198.51.100.1', 'version': 4}], + 'private': [{'addr': '192.0.2.5', 'version': 4}], + } self.assertEqual( ['198.51.100.1'], - meta.find_nova_addresses(addrs, key_name='public')) + meta.find_nova_addresses(addrs, key_name='public'), + ) self.assertEqual([], meta.find_nova_addresses(addrs, key_name='foo')) def test_find_nova_addresses_ext_tag(self): - addrs = {'public': [{'OS-EXT-IPS:type': 'fixed', - 'addr': '198.51.100.2', - 'version': 4}]} + addrs = { + 'public': [ + { + 'OS-EXT-IPS:type': 'fixed', + 'addr': '198.51.100.2', + 'version': 4, + } + ] + } self.assertEqual( - ['198.51.100.2'], meta.find_nova_addresses(addrs, ext_tag='fixed')) + ['198.51.100.2'], meta.find_nova_addresses(addrs, ext_tag='fixed') + ) self.assertEqual([], meta.find_nova_addresses(addrs, ext_tag='foo')) def test_find_nova_addresses_key_name_and_ext_tag(self): - addrs = {'public': [{'OS-EXT-IPS:type': 'fixed', - 'addr': '198.51.100.2', - 'version': 4}]} + addrs = { + 'public': [ + { + 'OS-EXT-IPS:type': 'fixed', + 'addr': '198.51.100.2', + 'version': 4, + } + ] + } self.assertEqual( - ['198.51.100.2'], meta.find_nova_addresses( - addrs, key_name='public', ext_tag='fixed')) - self.assertEqual([], meta.find_nova_addresses( - addrs, key_name='public', ext_tag='foo')) - self.assertEqual([], meta.find_nova_addresses( - addrs, key_name='bar', ext_tag='fixed')) + ['198.51.100.2'], + meta.find_nova_addresses( + addrs, key_name='public', ext_tag='fixed' + ), + ) + self.assertEqual( + [], + meta.find_nova_addresses(addrs, key_name='public', ext_tag='foo'), + ) + self.assertEqual( + [], + meta.find_nova_addresses(addrs, key_name='bar', ext_tag='fixed'), + ) def test_find_nova_addresses_all(self): - addrs = {'public': [{'OS-EXT-IPS:type': 'fixed', - 'addr': '198.51.100.2', - 'version': 4}]} + addrs = { + 'public': [ + { + 'OS-EXT-IPS:type': 'fixed', + 'addr': '198.51.100.2', + 'version': 4, + } + ] + } self.assertEqual( - ['198.51.100.2'], meta.find_nova_addresses( - addrs, key_name='public', ext_tag='fixed', version=4)) - self.assertEqual([], meta.find_nova_addresses( - addrs, key_name='public', ext_tag='fixed', version=6)) + ['198.51.100.2'], + meta.find_nova_addresses( + addrs, key_name='public', ext_tag='fixed', version=4 + ), + ) + self.assertEqual( + [], + meta.find_nova_addresses( + addrs, key_name='public', ext_tag='fixed', version=6 + ), + ) def test_find_nova_addresses_floating_first(self): # Note 198.51.100.0/24 is TEST-NET-2 from rfc5737 addrs = { - 'private': [{ - 'addr': '192.0.2.5', - 'version': 4, - 'OS-EXT-IPS:type': 'fixed'}], - 'public': [{ - 'addr': '198.51.100.1', - 'version': 4, - 'OS-EXT-IPS:type': 'floating'}]} + 'private': [ + {'addr': '192.0.2.5', 'version': 4, 'OS-EXT-IPS:type': 'fixed'} + ], + 'public': [ + { + 'addr': '198.51.100.1', + 'version': 4, + 'OS-EXT-IPS:type': 'floating', + } + ], + } self.assertEqual( - ['198.51.100.1', '192.0.2.5'], - meta.find_nova_addresses(addrs)) + ['198.51.100.1', '192.0.2.5'], meta.find_nova_addresses(addrs) + ) def test_get_server_ip(self): srv = meta.obj_to_munch(standard_fake_server) + self.assertEqual(PRIVATE_V4, meta.get_server_ip(srv, ext_tag='fixed')) self.assertEqual( - PRIVATE_V4, meta.get_server_ip(srv, ext_tag='fixed')) - self.assertEqual( - PUBLIC_V4, meta.get_server_ip(srv, ext_tag='floating')) + PUBLIC_V4, meta.get_server_ip(srv, ext_tag='floating') + ) def test_get_server_private_ip(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [{ - 'id': 'test-net-id', - 'name': 'test-net-name'}]} - ), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + {'id': 'test-net-id', 'name': 'test-net-name'} + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + ] + ) srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'private': [{'OS-EXT-IPS:type': 'fixed', - 'addr': PRIVATE_V4, - 'version': 4}], - 'public': [{'OS-EXT-IPS:type': 'floating', - 'addr': PUBLIC_V4, - 'version': 4}]} + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={ + 'private': [ + { + 'OS-EXT-IPS:type': 'fixed', + 'addr': PRIVATE_V4, + 'version': 4, + } + ], + 'public': [ + { + 'OS-EXT-IPS:type': 'floating', + 'addr': PUBLIC_V4, + 'version': 4, + } + ], + }, ) self.assertEqual( - PRIVATE_V4, meta.get_server_private_ip(srv, self.cloud)) + PRIVATE_V4, meta.get_server_private_ip(srv, self.cloud) + ) self.assert_calls() def test_get_server_multiple_private_ip(self): - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [{ - 'id': 'test-net-id', - 'name': 'test-net'}]} - ), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [{'id': 'test-net-id', 'name': 'test-net'}] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + ] + ) shared_mac = '11:22:33:44:55:66' distinct_mac = '66:55:44:33:22:11' srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'test-net': [{'OS-EXT-IPS:type': 'fixed', - 'OS-EXT-IPS-MAC:mac_addr': distinct_mac, - 'addr': '10.0.0.100', - 'version': 4}, - {'OS-EXT-IPS:type': 'fixed', - 'OS-EXT-IPS-MAC:mac_addr': shared_mac, - 'addr': '10.0.0.101', - 'version': 4}], - 'public': [{'OS-EXT-IPS:type': 'floating', - 'OS-EXT-IPS-MAC:mac_addr': shared_mac, - 'addr': PUBLIC_V4, - 'version': 4}]} + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={ + 'test-net': [ + { + 'OS-EXT-IPS:type': 'fixed', + 'OS-EXT-IPS-MAC:mac_addr': distinct_mac, + 'addr': '10.0.0.100', + 'version': 4, + }, + { + 'OS-EXT-IPS:type': 'fixed', + 'OS-EXT-IPS-MAC:mac_addr': shared_mac, + 'addr': '10.0.0.101', + 'version': 4, + }, + ], + 'public': [ + { + 'OS-EXT-IPS:type': 'floating', + 'OS-EXT-IPS-MAC:mac_addr': shared_mac, + 'addr': PUBLIC_V4, + 'version': 4, + } + ], + }, ) self.assertEqual( - '10.0.0.101', meta.get_server_private_ip(srv, self.cloud)) + '10.0.0.101', meta.get_server_private_ip(srv, self.cloud) + ) self.assert_calls() @mock.patch.object(connection.Connection, 'has_service') @@ -362,9 +445,12 @@ class TestMeta(base.TestCase): @mock.patch.object(connection.Connection, 'get_image_name') @mock.patch.object(connection.Connection, 'get_flavor_name') def test_get_server_private_ip_devstack( - self, - mock_get_flavor_name, mock_get_image_name, - mock_get_volumes, mock_has_service): + self, + mock_get_flavor_name, + mock_get_image_name, + mock_get_volumes, + mock_has_service, + ): mock_get_image_name.return_value = 'cirros-0.3.4-x86_64-uec' mock_get_flavor_name.return_value = 'm1.tiny' @@ -372,61 +458,92 @@ class TestMeta(base.TestCase): mock_has_service.return_value = True fake_server = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', flavor={u'id': u'1'}, image={ 'name': u'cirros-0.3.4-x86_64-uec', - u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'}, - addresses={u'test_pnztt_net': [{ - u'OS-EXT-IPS:type': u'fixed', - u'addr': PRIVATE_V4, - u'version': 4, - u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42' - }]} + u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1', + }, + addresses={ + u'test_pnztt_net': [ + { + u'OS-EXT-IPS:type': u'fixed', + u'addr': PRIVATE_V4, + u'version': 4, + u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42', + } + ] + }, ) - self.register_uris([ - dict(method='GET', - uri=('https://network.example.com/v2.0/ports?' - 'device_id=test-id'), - json={'ports': [{ - 'id': 'test_port_id', - 'mac_address': 'fa:16:3e:ae:7d:42', - 'device_id': 'test-id'}]} - ), - dict(method='GET', - uri=('https://network.example.com/v2.0/' - 'floatingips?port_id=test_port_id'), - json={'floatingips': []}), + self.register_uris( + [ + dict( + method='GET', + uri=( + 'https://network.example.com/v2.0/ports?' + 'device_id=test-id' + ), + json={ + 'ports': [ + { + 'id': 'test_port_id', + 'mac_address': 'fa:16:3e:ae:7d:42', + 'device_id': 'test-id', + } + ] + }, + ), + dict( + method='GET', + uri=( + 'https://network.example.com/v2.0/' + 'floatingips?port_id=test_port_id' + ), + json={'floatingips': []}, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test_pnztt_net', + 'name': 'test_pnztt_net', + 'router:external': False, + }, + {'id': 'private', 'name': 'private'}, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', fake_server['id']], + ), + json=fake_server, + ), + dict( + method='GET', + uri='{endpoint}/servers/test-id/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [ - {'id': 'test_pnztt_net', - 'name': 'test_pnztt_net', - 'router:external': False - }, - {'id': 'private', - 'name': 'private'}]} - ), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}), - - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', fake_server['id']]), - json=fake_server), - dict(method='GET', - uri='{endpoint}/servers/test-id/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}) - ]) - - srv = self.cloud.get_openstack_vars( - _server.Server(**fake_server)) + srv = self.cloud.get_openstack_vars(_server.Server(**fake_server)) self.assertEqual(PRIVATE_V4, srv['private_v4']) self.assert_calls() @@ -435,9 +552,8 @@ class TestMeta(base.TestCase): @mock.patch.object(connection.Connection, 'get_image_name') @mock.patch.object(connection.Connection, 'get_flavor_name') def test_get_server_private_ip_no_fip( - self, - mock_get_flavor_name, mock_get_image_name, - mock_get_volumes): + self, mock_get_flavor_name, mock_get_image_name, mock_get_volumes + ): self.cloud._floating_ip_source = None mock_get_image_name.return_value = 'cirros-0.3.4-x86_64-uec' @@ -445,47 +561,68 @@ class TestMeta(base.TestCase): mock_get_volumes.return_value = [] fake_server = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', flavor={u'id': u'1'}, image={ 'name': u'cirros-0.3.4-x86_64-uec', - u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'}, - addresses={u'test_pnztt_net': [{ - u'OS-EXT-IPS:type': u'fixed', - u'addr': PRIVATE_V4, - u'version': 4, - u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42' - }]} + u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1', + }, + addresses={ + u'test_pnztt_net': [ + { + u'OS-EXT-IPS:type': u'fixed', + u'addr': PRIVATE_V4, + u'version': 4, + u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42', + } + ] + }, ) - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [ - {'id': 'test_pnztt_net', - 'name': 'test_pnztt_net', - 'router:external': False, - }, - {'id': 'private', - 'name': 'private'}]} - ), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', fake_server['id']]), - json=fake_server), - dict(method='GET', - uri='{endpoint}/servers/test-id/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test_pnztt_net', + 'name': 'test_pnztt_net', + 'router:external': False, + }, + {'id': 'private', 'name': 'private'}, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', fake_server['id']], + ), + json=fake_server, + ), + dict( + method='GET', + uri='{endpoint}/servers/test-id/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) - srv = self.cloud.get_openstack_vars( - _server.Server(**fake_server)) + srv = self.cloud.get_openstack_vars(_server.Server(**fake_server)) self.assertEqual(PRIVATE_V4, srv['private_v4']) self.assert_calls() @@ -494,56 +631,74 @@ class TestMeta(base.TestCase): @mock.patch.object(connection.Connection, 'get_image_name') @mock.patch.object(connection.Connection, 'get_flavor_name') def test_get_server_cloud_no_fips( - self, - mock_get_flavor_name, mock_get_image_name, - mock_get_volumes): + self, mock_get_flavor_name, mock_get_image_name, mock_get_volumes + ): self.cloud._floating_ip_source = None mock_get_image_name.return_value = 'cirros-0.3.4-x86_64-uec' mock_get_flavor_name.return_value = 'm1.tiny' mock_get_volumes.return_value = [] fake_server = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', flavor={u'id': u'1'}, image={ 'name': u'cirros-0.3.4-x86_64-uec', - u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'}, - addresses={u'test_pnztt_net': [{ - u'addr': PRIVATE_V4, - u'version': 4, - }]} + u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1', + }, + addresses={ + u'test_pnztt_net': [ + { + u'addr': PRIVATE_V4, + u'version': 4, + } + ] + }, ) - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [ - { - 'id': 'test_pnztt_net', - 'name': 'test_pnztt_net', - 'router:external': False, - }, - { - 'id': 'private', - 'name': 'private'}]} - ), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', fake_server['id']]), - json=fake_server), - dict(method='GET', - uri='{endpoint}/servers/test-id/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test_pnztt_net', + 'name': 'test_pnztt_net', + 'router:external': False, + }, + {'id': 'private', 'name': 'private'}, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', fake_server['id']], + ), + json=fake_server, + ), + dict( + method='GET', + uri='{endpoint}/servers/test-id/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) - srv = self.cloud.get_openstack_vars( - _server.Server(**fake_server)) + srv = self.cloud.get_openstack_vars(_server.Server(**fake_server)) self.assertEqual(PRIVATE_V4, srv['private_v4']) self.assert_calls() @@ -553,76 +708,116 @@ class TestMeta(base.TestCase): @mock.patch.object(connection.Connection, 'get_image_name') @mock.patch.object(connection.Connection, 'get_flavor_name') def test_get_server_cloud_missing_fips( - self, - mock_get_flavor_name, mock_get_image_name, - mock_get_volumes, mock_has_service): + self, + mock_get_flavor_name, + mock_get_image_name, + mock_get_volumes, + mock_has_service, + ): mock_get_image_name.return_value = 'cirros-0.3.4-x86_64-uec' mock_get_flavor_name.return_value = 'm1.tiny' mock_get_volumes.return_value = [] mock_has_service.return_value = True fake_server = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', flavor={u'id': u'1'}, image={ 'name': u'cirros-0.3.4-x86_64-uec', - u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'}, - addresses={u'test_pnztt_net': [{ - u'addr': PRIVATE_V4, - u'version': 4, - 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:ae:7d:42', - }]} + u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1', + }, + addresses={ + u'test_pnztt_net': [ + { + u'addr': PRIVATE_V4, + u'version': 4, + 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:ae:7d:42', + } + ] + }, ) - self.register_uris([ - # self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=('https://network.example.com/v2.0/ports?' - 'device_id=test-id'), - json={'ports': [{ - 'id': 'test_port_id', - 'mac_address': 'fa:16:3e:ae:7d:42', - 'device_id': 'test-id'}]} - ), - dict(method='GET', - uri=('https://network.example.com/v2.0/floatingips' - '?port_id=test_port_id'), - json={'floatingips': [{ - 'id': 'floating-ip-id', - 'port_id': 'test_port_id', - 'fixed_ip_address': PRIVATE_V4, - 'floating_ip_address': PUBLIC_V4, - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [ - { - 'id': 'test_pnztt_net', - 'name': 'test_pnztt_net', - 'router:external': False, - }, - { - 'id': 'private', - 'name': 'private', - } - ]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', fake_server['id']]), - json=fake_server), - dict(method='GET', - uri='{endpoint}/servers/test-id/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}) - ]) + self.register_uris( + [ + # self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=( + 'https://network.example.com/v2.0/ports?' + 'device_id=test-id' + ), + json={ + 'ports': [ + { + 'id': 'test_port_id', + 'mac_address': 'fa:16:3e:ae:7d:42', + 'device_id': 'test-id', + } + ] + }, + ), + dict( + method='GET', + uri=( + 'https://network.example.com/v2.0/floatingips' + '?port_id=test_port_id' + ), + json={ + 'floatingips': [ + { + 'id': 'floating-ip-id', + 'port_id': 'test_port_id', + 'fixed_ip_address': PRIVATE_V4, + 'floating_ip_address': PUBLIC_V4, + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test_pnztt_net', + 'name': 'test_pnztt_net', + 'router:external': False, + }, + { + 'id': 'private', + 'name': 'private', + }, + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', fake_server['id']], + ), + json=fake_server, + ), + dict( + method='GET', + uri='{endpoint}/servers/test-id/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) - srv = self.cloud.get_openstack_vars( - _server.Server(**fake_server)) + srv = self.cloud.get_openstack_vars(_server.Server(**fake_server)) self.assertEqual(PUBLIC_V4, srv['public_v4']) self.assert_calls() @@ -631,8 +826,8 @@ class TestMeta(base.TestCase): @mock.patch.object(connection.Connection, 'get_image_name') @mock.patch.object(connection.Connection, 'get_flavor_name') def test_get_server_cloud_rackspace_v6( - self, mock_get_flavor_name, mock_get_image_name, - mock_get_volumes): + self, mock_get_flavor_name, mock_get_image_name, mock_get_volumes + ): self.cloud.config.config['has_network'] = False self.cloud._floating_ip_source = None self.cloud.force_ipv4 = False @@ -641,56 +836,66 @@ class TestMeta(base.TestCase): mock_get_flavor_name.return_value = 'm1.tiny' mock_get_volumes.return_value = [] fake_server = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', flavor={u'id': u'1'}, image={ 'name': u'cirros-0.3.4-x86_64-uec', - u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'}, + u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1', + }, addresses={ - 'private': [{ - 'addr': "10.223.160.141", - 'version': 4 - }], - 'public': [{ - 'addr': "104.130.246.91", - 'version': 4 - }, { - 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", - 'version': 6 - }] - } + 'private': [{'addr': "10.223.160.141", 'version': 4}], + 'public': [ + {'addr': "104.130.246.91", 'version': 4}, + { + 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", + 'version': 6, + }, + ], + }, ) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', fake_server['id']]), - json=fake_server), - dict(method='GET', - uri='{endpoint}/servers/test-id/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}) - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', fake_server['id']], + ), + json=fake_server, + ), + dict( + method='GET', + uri='{endpoint}/servers/test-id/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) - srv = self.cloud.get_openstack_vars( - _server.Server(**fake_server)) + srv = self.cloud.get_openstack_vars(_server.Server(**fake_server)) self.assertEqual("10.223.160.141", srv['private_v4']) self.assertEqual("104.130.246.91", srv['public_v4']) self.assertEqual( - "2001:4800:7819:103:be76:4eff:fe05:8525", srv['public_v6']) + "2001:4800:7819:103:be76:4eff:fe05:8525", srv['public_v6'] + ) self.assertEqual( - "2001:4800:7819:103:be76:4eff:fe05:8525", srv['interface_ip']) + "2001:4800:7819:103:be76:4eff:fe05:8525", srv['interface_ip'] + ) self.assert_calls() @mock.patch.object(connection.Connection, 'get_volumes') @mock.patch.object(connection.Connection, 'get_image_name') @mock.patch.object(connection.Connection, 'get_flavor_name') def test_get_server_cloud_osic_split( - self, mock_get_flavor_name, mock_get_image_name, - mock_get_volumes): + self, mock_get_flavor_name, mock_get_image_name, mock_get_volumes + ): self.cloud._floating_ip_source = None self.cloud.force_ipv4 = False self.cloud._local_ipv6 = True @@ -703,75 +908,99 @@ class TestMeta(base.TestCase): mock_get_volumes.return_value = [] fake_server = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', flavor={u'id': u'1'}, image={ 'name': u'cirros-0.3.4-x86_64-uec', - u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'}, + u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1', + }, addresses={ - 'private': [{ - 'addr': "10.223.160.141", - 'version': 4 - }], - 'public': [{ - 'addr': "104.130.246.91", - 'version': 4 - }, { - 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", - 'version': 6 - }] - } + 'private': [{'addr': "10.223.160.141", 'version': 4}], + 'public': [ + {'addr': "104.130.246.91", 'version': 4}, + { + 'addr': "2001:4800:7819:103:be76:4eff:fe05:8525", + 'version': 6, + }, + ], + }, ) - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': OSIC_NETWORKS}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': OSIC_SUBNETS}), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', fake_server['id']]), - json=fake_server), - dict(method='GET', - uri='{endpoint}/servers/test-id/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={'networks': OSIC_NETWORKS}, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': OSIC_SUBNETS}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', fake_server['id']], + ), + json=fake_server, + ), + dict( + method='GET', + uri='{endpoint}/servers/test-id/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) - srv = self.cloud.get_openstack_vars( - _server.Server(**fake_server)) + srv = self.cloud.get_openstack_vars(_server.Server(**fake_server)) self.assertEqual("10.223.160.141", srv['private_v4']) self.assertEqual("104.130.246.91", srv['public_v4']) self.assertEqual( - "2001:4800:7819:103:be76:4eff:fe05:8525", srv['public_v6']) + "2001:4800:7819:103:be76:4eff:fe05:8525", srv['public_v6'] + ) self.assertEqual( - "2001:4800:7819:103:be76:4eff:fe05:8525", srv['interface_ip']) + "2001:4800:7819:103:be76:4eff:fe05:8525", srv['interface_ip'] + ) self.assert_calls() def test_get_server_external_ipv4_neutron(self): # Testing Clouds with Neutron - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [{ - 'id': 'test-net-id', - 'name': 'test-net', - 'router:external': True - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test-net-id', + 'name': 'test-net', + 'router:external': True, + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + ] + ) srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'test-net': [{ - 'addr': PUBLIC_V4, - 'version': 4}]}, + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'test-net': [{'addr': PUBLIC_V4, 'version': 4}]}, ) ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) @@ -780,25 +1009,35 @@ class TestMeta(base.TestCase): def test_get_server_external_provider_ipv4_neutron(self): # Testing Clouds with Neutron - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [{ - 'id': 'test-net-id', - 'name': 'test-net', - 'provider:network_type': 'vlan', - 'provider:physical_network': 'vlan', - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test-net-id', + 'name': 'test-net', + 'provider:network_type': 'vlan', + 'provider:physical_network': 'vlan', + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + ] + ) srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'test-net': [{ - 'addr': PUBLIC_V4, - 'version': 4}]}, + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'test-net': [{'addr': PUBLIC_V4, 'version': 4}]}, ) ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) @@ -807,28 +1046,39 @@ class TestMeta(base.TestCase): def test_get_server_internal_provider_ipv4_neutron(self): # Testing Clouds with Neutron - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [{ - 'id': 'test-net-id', - 'name': 'test-net', - 'router:external': False, - 'provider:network_type': 'vxlan', - 'provider:physical_network': None, - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test-net-id', + 'name': 'test-net', + 'router:external': False, + 'provider:network_type': 'vxlan', + 'provider:physical_network': None, + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + ] + ) srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'test-net': [{ - 'addr': PRIVATE_V4, - 'version': 4}]}, + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'test-net': [{'addr': PRIVATE_V4, 'version': 4}]}, ) self.assertIsNone( - meta.get_server_external_ipv4(cloud=self.cloud, server=srv)) + meta.get_server_external_ipv4(cloud=self.cloud, server=srv) + ) int_ip = meta.get_server_private_ip(cloud=self.cloud, server=srv) self.assertEqual(PRIVATE_V4, int_ip) @@ -836,24 +1086,34 @@ class TestMeta(base.TestCase): def test_get_server_external_none_ipv4_neutron(self): # Testing Clouds with Neutron - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - json={'networks': [{ - 'id': 'test-net-id', - 'name': 'test-net', - 'router:external': False, - }]}), - dict(method='GET', - uri='https://network.example.com/v2.0/subnets', - json={'subnets': SUBNETS_WITH_NAT}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + json={ + 'networks': [ + { + 'id': 'test-net-id', + 'name': 'test-net', + 'router:external': False, + } + ] + }, + ), + dict( + method='GET', + uri='https://network.example.com/v2.0/subnets', + json={'subnets': SUBNETS_WITH_NAT}, + ), + ] + ) srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'test-net': [{ - 'addr': PUBLIC_V4, - 'version': 4}]}, + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'test-net': [{'addr': PUBLIC_V4, 'version': 4}]}, ) ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) @@ -862,7 +1122,8 @@ class TestMeta(base.TestCase): def test_get_server_external_ipv4_neutron_accessIPv4(self): srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE') + server_id='test-id', name='test-name', status='ACTIVE' + ) srv['accessIPv4'] = PUBLIC_V4 ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) @@ -870,7 +1131,8 @@ class TestMeta(base.TestCase): def test_get_server_external_ipv4_neutron_accessIPv6(self): srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE') + server_id='test-id', name='test-name', status='ACTIVE' + ) srv['accessIPv6'] = PUBLIC_V6 ip = meta.get_server_external_ipv6(server=srv) @@ -878,14 +1140,21 @@ class TestMeta(base.TestCase): def test_get_server_external_ipv4_neutron_exception(self): # Testing Clouds with a non working Neutron - self.register_uris([ - dict(method='GET', - uri='https://network.example.com/v2.0/networks', - status_code=404)]) + self.register_uris( + [ + dict( + method='GET', + uri='https://network.example.com/v2.0/networks', + status_code=404, + ) + ] + ) srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'public': [{'addr': PUBLIC_V4, 'version': 4}]} + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'public': [{'addr': PUBLIC_V4, 'version': 4}]}, ) ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) @@ -897,8 +1166,11 @@ class TestMeta(base.TestCase): self.cloud.config.config['has_network'] = False srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'public': [{'addr': PUBLIC_V4, 'version': 4}]}) + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'public': [{'addr': PUBLIC_V4, 'version': 4}]}, + ) ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertEqual(PUBLIC_V4, ip) @@ -908,48 +1180,58 @@ class TestMeta(base.TestCase): self.cloud.config.config['has_network'] = False srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', - addresses={'test-net': [{'addr': PRIVATE_V4}]}) + server_id='test-id', + name='test-name', + status='ACTIVE', + addresses={'test-net': [{'addr': PRIVATE_V4}]}, + ) ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertIsNone(ip) def test_get_server_external_ipv6(self): srv = fakes.make_fake_server( - server_id='test-id', name='test-name', status='ACTIVE', + server_id='test-id', + name='test-name', + status='ACTIVE', addresses={ 'test-net': [ {'addr': PUBLIC_V4, 'version': 4}, - {'addr': PUBLIC_V6, 'version': 6} + {'addr': PUBLIC_V6, 'version': 6}, ] - } + }, ) ip = meta.get_server_external_ipv6(srv) self.assertEqual(PUBLIC_V6, ip) def test_get_groups_from_server(self): - server_vars = {'flavor': 'test-flavor', - 'image': 'test-image', - 'az': 'test-az'} + server_vars = { + 'flavor': 'test-flavor', + 'image': 'test-image', + 'az': 'test-az', + } self.assertEqual( - ['test-name', - 'test-region', - 'test-name_test-region', - 'test-group', - 'instance-test-id-0', - 'meta-group_test-group', - 'test-az', - 'test-region_test-az', - 'test-name_test-region_test-az'], + [ + 'test-name', + 'test-region', + 'test-name_test-region', + 'test-group', + 'instance-test-id-0', + 'meta-group_test-group', + 'test-az', + 'test-region_test-az', + 'test-name_test-region_test-az', + ], meta.get_groups_from_server( FakeCloud(), meta.obj_to_munch(standard_fake_server), - server_vars - ) + server_vars, + ), ) def test_obj_list_to_munch(self): """Test conversion of a list of objects to a list of dictonaries""" + class obj0: value = 0 @@ -962,33 +1244,35 @@ class TestMeta(base.TestCase): self.assertEqual(new_list[1]['value'], 1) @mock.patch.object(FakeCloud, 'list_server_security_groups') - def test_get_security_groups(self, - mock_list_server_security_groups): + def test_get_security_groups(self, mock_list_server_security_groups): '''This test verifies that calling get_hostvars_froms_server ultimately calls list_server_security_groups, and that the return value from list_server_security_groups ends up in server['security_groups'].''' mock_list_server_security_groups.return_value = [ - {'name': 'testgroup', 'id': '1'}] + {'name': 'testgroup', 'id': '1'} + ] server = meta.obj_to_munch(standard_fake_server) hostvars = meta.get_hostvars_from_server(FakeCloud(), server) mock_list_server_security_groups.assert_called_once_with(server) - self.assertEqual('testgroup', - hostvars['security_groups'][0]['name']) + self.assertEqual('testgroup', hostvars['security_groups'][0]['name']) @mock.patch.object(meta, 'get_server_external_ipv6') @mock.patch.object(meta, 'get_server_external_ipv4') def test_basic_hostvars( - self, mock_get_server_external_ipv4, - mock_get_server_external_ipv6): + self, mock_get_server_external_ipv4, mock_get_server_external_ipv6 + ): mock_get_server_external_ipv4.return_value = PUBLIC_V4 mock_get_server_external_ipv6.return_value = PUBLIC_V6 hostvars = meta.get_hostvars_from_server( - FakeCloud(), self.cloud._normalize_server( - meta.obj_to_munch(standard_fake_server))) + FakeCloud(), + self.cloud._normalize_server( + meta.obj_to_munch(standard_fake_server) + ), + ) self.assertNotIn('links', hostvars) self.assertEqual(PRIVATE_V4, hostvars['private_v4']) self.assertEqual(PUBLIC_V4, hostvars['public_v4']) @@ -1000,13 +1284,16 @@ class TestMeta(base.TestCase): self.assertEqual('_test_cloud_', hostvars['location']['cloud']) self.assertEqual('RegionOne', hostvars['location']['region_name']) self.assertEqual( - fakes.PROJECT_ID, hostvars['location']['project']['id']) + fakes.PROJECT_ID, hostvars['location']['project']['id'] + ) self.assertEqual("test-image-name", hostvars['image']['name']) self.assertEqual( - standard_fake_server['image']['id'], hostvars['image']['id']) + standard_fake_server['image']['id'], hostvars['image']['id'] + ) self.assertNotIn('links', hostvars['image']) self.assertEqual( - standard_fake_server['flavor']['id'], hostvars['flavor']['id']) + standard_fake_server['flavor']['id'], hostvars['flavor']['id'] + ) self.assertEqual("test-flavor-name", hostvars['flavor']['name']) self.assertNotIn('links', hostvars['flavor']) # test having volumes @@ -1016,15 +1303,16 @@ class TestMeta(base.TestCase): @mock.patch.object(meta, 'get_server_external_ipv6') @mock.patch.object(meta, 'get_server_external_ipv4') def test_ipv4_hostvars( - self, mock_get_server_external_ipv4, - mock_get_server_external_ipv6): + self, mock_get_server_external_ipv4, mock_get_server_external_ipv6 + ): mock_get_server_external_ipv4.return_value = PUBLIC_V4 mock_get_server_external_ipv6.return_value = PUBLIC_V6 fake_cloud = FakeCloud() fake_cloud.force_ipv4 = True hostvars = meta.get_hostvars_from_server( - fake_cloud, meta.obj_to_munch(standard_fake_server)) + fake_cloud, meta.obj_to_munch(standard_fake_server) + ) self.assertEqual(PUBLIC_V4, hostvars['interface_ip']) self.assertEqual('', hostvars['public_v6']) @@ -1035,7 +1323,8 @@ class TestMeta(base.TestCase): cloud = FakeCloud() cloud.private = True hostvars = meta.get_hostvars_from_server( - cloud, meta.obj_to_munch(standard_fake_server)) + cloud, meta.obj_to_munch(standard_fake_server) + ) self.assertEqual(PRIVATE_V4, hostvars['interface_ip']) @mock.patch.object(meta, 'get_server_external_ipv4') @@ -1045,7 +1334,8 @@ class TestMeta(base.TestCase): server = standard_fake_server server['image'] = 'fake-image-id' hostvars = meta.get_hostvars_from_server( - FakeCloud(), meta.obj_to_munch(server)) + FakeCloud(), meta.obj_to_munch(server) + ) self.assertEqual('fake-image-id', hostvars['image']['id']) def test_az(self): @@ -1056,25 +1346,31 @@ class TestMeta(base.TestCase): self.assertEqual('az1', hostvars['az']) def test_current_location(self): - self.assertEqual({ - 'cloud': '_test_cloud_', - 'project': { + self.assertEqual( + { + 'cloud': '_test_cloud_', + 'project': { + 'id': mock.ANY, + 'name': 'admin', + 'domain_id': None, + 'domain_name': 'default', + }, + 'region_name': u'RegionOne', + 'zone': None, + }, + self.cloud.current_location, + ) + + def test_current_project(self): + self.assertEqual( + { 'id': mock.ANY, 'name': 'admin', 'domain_id': None, - 'domain_name': 'default' + 'domain_name': 'default', }, - 'region_name': u'RegionOne', - 'zone': None}, - self.cloud.current_location) - - def test_current_project(self): - self.assertEqual({ - 'id': mock.ANY, - 'name': 'admin', - 'domain_id': None, - 'domain_name': 'default'}, - self.cloud.current_project) + self.cloud.current_project, + ) def test_has_volume(self): mock_cloud = mock.MagicMock() @@ -1083,11 +1379,13 @@ class TestMeta(base.TestCase): id='volume1', status='available', name='Volume 1 Display Name', - attachments=[{'device': '/dev/sda0'}]) + attachments=[{'device': '/dev/sda0'}], + ) fake_volume_dict = meta.obj_to_munch(fake_volume) mock_cloud.get_volumes.return_value = [fake_volume_dict] hostvars = meta.get_hostvars_from_server( - mock_cloud, meta.obj_to_munch(standard_fake_server)) + mock_cloud, meta.obj_to_munch(standard_fake_server) + ) self.assertEqual('volume1', hostvars['volumes'][0]['id']) self.assertEqual('/dev/sda0', hostvars['volumes'][0]['device']) @@ -1095,7 +1393,8 @@ class TestMeta(base.TestCase): fake_cloud = FakeCloud() fake_cloud.service_val = False hostvars = meta.get_hostvars_from_server( - fake_cloud, meta.obj_to_munch(standard_fake_server)) + fake_cloud, meta.obj_to_munch(standard_fake_server) + ) self.assertEqual([], hostvars['volumes']) def test_unknown_volume_exception(self): @@ -1106,12 +1405,14 @@ class TestMeta(base.TestCase): def side_effect(*args): raise FakeException("No Volumes") + mock_cloud.get_volumes.side_effect = side_effect self.assertRaises( FakeException, meta.get_hostvars_from_server, mock_cloud, - meta.obj_to_munch(standard_fake_server)) + meta.obj_to_munch(standard_fake_server), + ) def test_obj_to_munch(self): cloud = FakeCloud() @@ -1127,6 +1428,7 @@ class TestMeta(base.TestCase): def test_obj_to_munch_subclass(self): class FakeObjDict(dict): additional = 1 + obj = FakeObjDict(foo='bar') obj_dict = meta.obj_to_munch(obj) self.assertIn('additional', obj_dict) diff --git a/openstack/tests/unit/cloud/test_network.py b/openstack/tests/unit/cloud/test_network.py index 93a3dad24..860efc3a1 100644 --- a/openstack/tests/unit/cloud/test_network.py +++ b/openstack/tests/unit/cloud/test_network.py @@ -57,7 +57,7 @@ class TestNetwork(base.TestCase): "updated": "2015-01-01T10:00:00-00:00", "description": "Availability zone support for router.", "links": [], - "name": "Network Availability Zone" + "name": "Network Availability Zone", } enabled_neutron_extensions = [network_availability_zone_extension] @@ -65,66 +65,99 @@ class TestNetwork(base.TestCase): def _compare_networks(self, exp, real): self.assertDictEqual( _network.Network(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_list_networks(self): net1 = {'id': '1', 'name': 'net1'} net2 = {'id': '2', 'name': 'net2'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': [net1, net2]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': [net1, net2]}, + ) + ] + ) nets = self.cloud.list_networks() self.assertEqual( - [_network.Network(**i).to_dict(computed=False) for i in [ - net1, net2]], - [i.to_dict(computed=False) for i in nets]) + [ + _network.Network(**i).to_dict(computed=False) + for i in [net1, net2] + ], + [i.to_dict(computed=False) for i in nets], + ) self.assert_calls() def test_list_networks_filtered(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks'], - qs_elements=["name=test"]), - json={'networks': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=["name=test"], + ), + json={'networks': []}, + ) + ] + ) self.cloud.list_networks(filters={'name': 'test'}) self.assert_calls() def test_create_network(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': self.mock_new_network_rep}, - validate=dict( - json={'network': { - 'admin_state_up': True, - 'name': 'netname'}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': self.mock_new_network_rep}, + validate=dict( + json={ + 'network': { + 'admin_state_up': True, + 'name': 'netname', + } + } + ), + ) + ] + ) network = self.cloud.create_network("netname") - self._compare_networks( - self.mock_new_network_rep, network) + self._compare_networks(self.mock_new_network_rep, network) self.assert_calls() def test_create_network_specific_tenant(self): project_id = "project_id_value" mock_new_network_rep = copy.copy(self.mock_new_network_rep) mock_new_network_rep['project_id'] = project_id - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': mock_new_network_rep}, - validate=dict( - json={'network': { - 'admin_state_up': True, - 'name': 'netname', - 'project_id': project_id}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': mock_new_network_rep}, + validate=dict( + json={ + 'network': { + 'admin_state_up': True, + 'name': 'netname', + 'project_id': project_id, + } + } + ), + ) + ] + ) network = self.cloud.create_network("netname", project_id=project_id) self._compare_networks(mock_new_network_rep, network) self.assert_calls() @@ -132,45 +165,57 @@ class TestNetwork(base.TestCase): def test_create_network_external(self): mock_new_network_rep = copy.copy(self.mock_new_network_rep) mock_new_network_rep['router:external'] = True - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': mock_new_network_rep}, - validate=dict( - json={'network': { - 'admin_state_up': True, - 'name': 'netname', - 'router:external': True}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': mock_new_network_rep}, + validate=dict( + json={ + 'network': { + 'admin_state_up': True, + 'name': 'netname', + 'router:external': True, + } + } + ), + ) + ] + ) network = self.cloud.create_network("netname", external=True) self._compare_networks(mock_new_network_rep, network) self.assert_calls() def test_create_network_provider(self): - provider_opts = {'physical_network': 'mynet', - 'network_type': 'vlan', - 'segmentation_id': 'vlan1'} + provider_opts = { + 'physical_network': 'mynet', + 'network_type': 'vlan', + 'segmentation_id': 'vlan1', + } new_network_provider_opts = { 'provider:physical_network': 'mynet', 'provider:network_type': 'vlan', - 'provider:segmentation_id': 'vlan1' + 'provider:segmentation_id': 'vlan1', } mock_new_network_rep = copy.copy(self.mock_new_network_rep) mock_new_network_rep.update(new_network_provider_opts) - expected_send_params = { - 'admin_state_up': True, - 'name': 'netname' - } + expected_send_params = {'admin_state_up': True, 'name': 'netname'} expected_send_params.update(new_network_provider_opts) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': mock_new_network_rep}, - validate=dict( - json={'network': expected_send_params})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': mock_new_network_rep}, + validate=dict(json={'network': expected_send_params}), + ) + ] + ) network = self.cloud.create_network("netname", provider=provider_opts) self._compare_networks(mock_new_network_rep, network) self.assert_calls() @@ -179,89 +224,122 @@ class TestNetwork(base.TestCase): network_id = "test-net-id" network_name = "network" network = {'id': network_id, 'name': network_name} - provider_opts = {'physical_network': 'mynet', - 'network_type': 'vlan', - 'segmentation_id': 'vlan1', - 'should_not_be_passed': 1} + provider_opts = { + 'physical_network': 'mynet', + 'network_type': 'vlan', + 'segmentation_id': 'vlan1', + 'should_not_be_passed': 1, + } update_network_provider_opts = { 'provider:physical_network': 'mynet', 'provider:network_type': 'vlan', - 'provider:segmentation_id': 'vlan1' + 'provider:segmentation_id': 'vlan1', } mock_update_rep = copy.copy(self.mock_new_network_rep) mock_update_rep.update(update_network_provider_opts) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks'], - qs_elements=['name=%s' % network_name]), - json={'networks': [network]}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_id]), - json={'network': mock_update_rep}, - validate=dict( - json={'network': update_network_provider_opts})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % network_name], + ), + json={'networks': [network]}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_id], + ), + json={'network': mock_update_rep}, + validate=dict( + json={'network': update_network_provider_opts} + ), + ), + ] + ) network = self.cloud.update_network( - network_name, - provider=provider_opts + network_name, provider=provider_opts ) self._compare_networks(mock_update_rep, network) self.assert_calls() def test_create_network_with_availability_zone_hints(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': self.mock_new_network_rep}, - validate=dict( - json={'network': { - 'admin_state_up': True, - 'name': 'netname', - 'availability_zone_hints': ['nova']}})) - ]) - network = self.cloud.create_network("netname", - availability_zone_hints=['nova']) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': self.mock_new_network_rep}, + validate=dict( + json={ + 'network': { + 'admin_state_up': True, + 'name': 'netname', + 'availability_zone_hints': ['nova'], + } + } + ), + ), + ] + ) + network = self.cloud.create_network( + "netname", availability_zone_hints=['nova'] + ) self._compare_networks(self.mock_new_network_rep, network) self.assert_calls() def test_create_network_provider_ignored_value(self): - provider_opts = {'physical_network': 'mynet', - 'network_type': 'vlan', - 'segmentation_id': 'vlan1', - 'should_not_be_passed': 1} + provider_opts = { + 'physical_network': 'mynet', + 'network_type': 'vlan', + 'segmentation_id': 'vlan1', + 'should_not_be_passed': 1, + } new_network_provider_opts = { 'provider:physical_network': 'mynet', 'provider:network_type': 'vlan', - 'provider:segmentation_id': 'vlan1' + 'provider:segmentation_id': 'vlan1', } mock_new_network_rep = copy.copy(self.mock_new_network_rep) mock_new_network_rep.update(new_network_provider_opts) - expected_send_params = { - 'admin_state_up': True, - 'name': 'netname' - } + expected_send_params = {'admin_state_up': True, 'name': 'netname'} expected_send_params.update(new_network_provider_opts) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': mock_new_network_rep}, - validate=dict( - json={'network': expected_send_params})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': mock_new_network_rep}, + validate=dict(json={'network': expected_send_params}), + ) + ] + ) network = self.cloud.create_network("netname", provider=provider_opts) self._compare_networks(mock_new_network_rep, network) self.assert_calls() @@ -270,16 +348,17 @@ class TestNetwork(base.TestCase): azh_opts = "invalid" with testtools.ExpectedException( openstack.cloud.OpenStackCloudException, - "Parameter 'availability_zone_hints' must be a list" + "Parameter 'availability_zone_hints' must be a list", ): - self.cloud.create_network("netname", - availability_zone_hints=azh_opts) + self.cloud.create_network( + "netname", availability_zone_hints=azh_opts + ) def test_create_network_provider_wrong_type(self): provider_opts = "invalid" with testtools.ExpectedException( openstack.cloud.OpenStackCloudException, - "Parameter 'provider' must be a dict" + "Parameter 'provider' must be a dict", ): self.cloud.create_network("netname", provider=provider_opts) @@ -287,20 +366,28 @@ class TestNetwork(base.TestCase): port_security_state = False mock_new_network_rep = copy.copy(self.mock_new_network_rep) mock_new_network_rep['port_security_enabled'] = port_security_state - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': mock_new_network_rep}, - validate=dict( - json={'network': { - 'admin_state_up': True, - 'name': 'netname', - 'port_security_enabled': port_security_state}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': mock_new_network_rep}, + validate=dict( + json={ + 'network': { + 'admin_state_up': True, + 'name': 'netname', + 'port_security_enabled': port_security_state, + } + } + ), + ) + ] + ) network = self.cloud.create_network( - "netname", - port_security_enabled=port_security_state + "netname", port_security_enabled=port_security_state ) self._compare_networks(mock_new_network_rep, network) self.assert_calls() @@ -309,34 +396,41 @@ class TestNetwork(base.TestCase): mtu_size = 1500 mock_new_network_rep = copy.copy(self.mock_new_network_rep) mock_new_network_rep['mtu'] = mtu_size - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'network': mock_new_network_rep}, - validate=dict( - json={'network': { - 'admin_state_up': True, - 'name': 'netname', - 'mtu': mtu_size}})) - ]) - network = self.cloud.create_network("netname", - mtu_size=mtu_size - ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'network': mock_new_network_rep}, + validate=dict( + json={ + 'network': { + 'admin_state_up': True, + 'name': 'netname', + 'mtu': mtu_size, + } + } + ), + ) + ] + ) + network = self.cloud.create_network("netname", mtu_size=mtu_size) self._compare_networks(mock_new_network_rep, network) self.assert_calls() def test_create_network_with_wrong_mtu_size(self): with testtools.ExpectedException( - openstack.cloud.OpenStackCloudException, - "Parameter 'mtu_size' must be greater than 67." + openstack.cloud.OpenStackCloudException, + "Parameter 'mtu_size' must be greater than 67.", ): self.cloud.create_network("netname", mtu_size=42) def test_create_network_with_wrong_mtu_type(self): with testtools.ExpectedException( - openstack.cloud.OpenStackCloudException, - "Parameter 'mtu_size' must be an integer." + openstack.cloud.OpenStackCloudException, + "Parameter 'mtu_size' must be an integer.", ): self.cloud.create_network("netname", mtu_size="fourty_two") @@ -344,39 +438,65 @@ class TestNetwork(base.TestCase): network_id = "test-net-id" network_name = "network" network = {'id': network_id, 'name': network_name} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks'], - qs_elements=['name=%s' % network_name]), - json={'networks': [network]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % network_name], + ), + json={'networks': [network]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_id], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_network(network_name)) self.assert_calls() def test_delete_network_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', 'test-net']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks'], - qs_elements=['name=test-net']), - json={'networks': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', 'test-net'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=test-net'], + ), + json={'networks': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_network('test-net')) self.assert_calls() @@ -384,37 +504,61 @@ class TestNetwork(base.TestCase): network_id = "test-net-id" network_name = "network" network = {'id': network_id, 'name': network_name} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks'], - qs_elements=['name=%s' % network_name]), - json={'networks': [network]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_id]), - status_code=503) - ]) - self.assertRaises(openstack.cloud.OpenStackCloudException, - self.cloud.delete_network, network_name) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % network_name], + ), + json={'networks': [network]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_id], + ), + status_code=503, + ), + ] + ) + self.assertRaises( + openstack.cloud.OpenStackCloudException, + self.cloud.delete_network, + network_name, + ) self.assert_calls() def test_get_network_by_id(self): network_id = "test-net-id" network_name = "network" network = {'id': network_id, 'name': network_name} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', network_id]), - json={'network': network}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', network_id], + ), + json={'network': network}, + ) + ] + ) self.assertTrue(self.cloud.get_network_by_id(network_id)) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_object.py b/openstack/tests/unit/cloud/test_object.py index 8635c951b..53461dc3c 100644 --- a/openstack/tests/unit/cloud/test_object.py +++ b/openstack/tests/unit/cloud/test_object.py @@ -29,7 +29,6 @@ from openstack import utils class BaseTestObject(base.TestCase): - def setUp(self): super(BaseTestObject, self).setUp() @@ -37,98 +36,126 @@ class BaseTestObject(base.TestCase): self.object = self.getUniqueString() self.endpoint = self.cloud._object_store_client.get_endpoint() self.container_endpoint = '{endpoint}/{container}'.format( - endpoint=self.endpoint, container=self.container) + endpoint=self.endpoint, container=self.container + ) self.object_endpoint = '{endpoint}/{object}'.format( - endpoint=self.container_endpoint, object=self.object) + endpoint=self.container_endpoint, object=self.object + ) def _compare_containers(self, exp, real): self.assertDictEqual( - container.Container(**exp).to_dict( - computed=False), - real.to_dict(computed=False)) + container.Container(**exp).to_dict(computed=False), + real.to_dict(computed=False), + ) def _compare_objects(self, exp, real): self.assertDictEqual( - obj.Object(**exp).to_dict( - computed=False), - real.to_dict(computed=False)) + obj.Object(**exp).to_dict(computed=False), + real.to_dict(computed=False), + ) class TestObject(BaseTestObject): - def test_create_container(self): """Test creating a (private) container""" - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, status_code=404), - dict(method='PUT', uri=self.container_endpoint, - status_code=201, - headers={ - 'Date': 'Fri, 16 Dec 2016 18:21:20 GMT', - 'Content-Length': '0', - 'Content-Type': 'text/html; charset=UTF-8', - }), - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'Content-Type': 'text/plain; charset=utf-8'}) - ]) + self.register_uris( + [ + dict( + method='HEAD', uri=self.container_endpoint, status_code=404 + ), + dict( + method='PUT', + uri=self.container_endpoint, + status_code=201, + headers={ + 'Date': 'Fri, 16 Dec 2016 18:21:20 GMT', + 'Content-Length': '0', + 'Content-Type': 'text/html; charset=UTF-8', + }, + ), + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'Content-Type': 'text/plain; charset=utf-8', + }, + ), + ] + ) self.cloud.create_container(self.container) self.assert_calls() def test_create_container_public(self): """Test creating a public container""" - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - status_code=404), - dict(method='PUT', uri=self.container_endpoint, - status_code=201, - headers={ - 'Date': 'Fri, 16 Dec 2016 18:21:20 GMT', - 'Content-Length': '0', - 'Content-Type': 'text/html; charset=UTF-8', - 'x-container-read': - oc_oc.OBJECT_CONTAINER_ACLS[ - 'public'], - }), - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'Content-Type': 'text/plain; charset=utf-8'}) - ]) + self.register_uris( + [ + dict( + method='HEAD', uri=self.container_endpoint, status_code=404 + ), + dict( + method='PUT', + uri=self.container_endpoint, + status_code=201, + headers={ + 'Date': 'Fri, 16 Dec 2016 18:21:20 GMT', + 'Content-Length': '0', + 'Content-Type': 'text/html; charset=UTF-8', + 'x-container-read': oc_oc.OBJECT_CONTAINER_ACLS[ + 'public' + ], + }, + ), + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'Content-Type': 'text/plain; charset=utf-8', + }, + ), + ] + ) self.cloud.create_container(self.container, public=True) self.assert_calls() def test_create_container_exists(self): """Test creating a container that exists.""" - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'Content-Type': 'text/plain; charset=utf-8'}) - ]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'Content-Type': 'text/plain; charset=utf-8', + }, + ) + ] + ) container = self.cloud.create_container(self.container) @@ -136,17 +163,24 @@ class TestObject(BaseTestObject): self.assertIsNotNone(container) def test_delete_container(self): - self.register_uris([ - dict(method='DELETE', uri=self.container_endpoint)]) + self.register_uris( + [dict(method='DELETE', uri=self.container_endpoint)] + ) self.assertTrue(self.cloud.delete_container(self.container)) self.assert_calls() def test_delete_container_404(self): """No exception when deleting a container that does not exist""" - self.register_uris([ - dict(method='DELETE', uri=self.container_endpoint, - status_code=404)]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.container_endpoint, + status_code=404, + ) + ] + ) self.assertFalse(self.cloud.delete_container(self.container)) self.assert_calls() @@ -154,22 +188,34 @@ class TestObject(BaseTestObject): def test_delete_container_error(self): """Non-404 swift error re-raised as OSCE""" # 409 happens if the container is not empty - self.register_uris([ - dict(method='DELETE', uri=self.container_endpoint, - status_code=409)]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.container_endpoint, + status_code=409, + ) + ] + ) self.assertRaises( openstack.cloud.OpenStackCloudException, - self.cloud.delete_container, self.container) + self.cloud.delete_container, + self.container, + ) self.assert_calls() def test_update_container(self): - headers = { - 'x-container-read': - oc_oc.OBJECT_CONTAINER_ACLS['public']} - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=204, - validate=dict(headers=headers))]) + headers = {'x-container-read': oc_oc.OBJECT_CONTAINER_ACLS['public']} + self.register_uris( + [ + dict( + method='POST', + uri=self.container_endpoint, + status_code=204, + validate=dict(headers=headers), + ) + ] + ) self.cloud.update_container(self.container, headers) self.assert_calls() @@ -181,37 +227,56 @@ class TestObject(BaseTestObject): # method, and I cannot make a synthetic failure to validate a real # error code. So we're really just testing the shade adapter error # raising logic here, rather than anything specific to swift. - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=409)]) + self.register_uris( + [dict(method='POST', uri=self.container_endpoint, status_code=409)] + ) self.assertRaises( openstack.cloud.OpenStackCloudException, - self.cloud.update_container, self.container, dict(foo='bar')) + self.cloud.update_container, + self.container, + dict(foo='bar'), + ) self.assert_calls() def test_set_container_access_public(self): - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=204, - validate=dict( - headers={ - 'x-container-read': - oc_oc.OBJECT_CONTAINER_ACLS[ - 'public']}))]) + self.register_uris( + [ + dict( + method='POST', + uri=self.container_endpoint, + status_code=204, + validate=dict( + headers={ + 'x-container-read': oc_oc.OBJECT_CONTAINER_ACLS[ + 'public' + ] + } + ), + ) + ] + ) self.cloud.set_container_access(self.container, 'public') self.assert_calls() def test_set_container_access_private(self): - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=204, - validate=dict( - headers={ - 'x-container-read': - oc_oc.OBJECT_CONTAINER_ACLS[ - 'private']}))]) + self.register_uris( + [ + dict( + method='POST', + uri=self.container_endpoint, + status_code=204, + validate=dict( + headers={ + 'x-container-read': oc_oc.OBJECT_CONTAINER_ACLS[ + 'private' + ] + } + ), + ) + ] + ) self.cloud.set_container_access(self.container, 'private') @@ -220,47 +285,69 @@ class TestObject(BaseTestObject): def test_set_container_access_invalid(self): self.assertRaises( openstack.cloud.OpenStackCloudException, - self.cloud.set_container_access, self.container, 'invalid') + self.cloud.set_container_access, + self.container, + 'invalid', + ) def test_get_container_access(self): - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'x-container-read': - str(oc_oc.OBJECT_CONTAINER_ACLS[ - 'public'])})]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'x-container-read': str( + oc_oc.OBJECT_CONTAINER_ACLS['public'] + ) + }, + ) + ] + ) access = self.cloud.get_container_access(self.container) self.assertEqual('public', access) def test_get_container_invalid(self): - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - headers={'x-container-read': 'invalid'})]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.container_endpoint, + headers={'x-container-read': 'invalid'}, + ) + ] + ) with testtools.ExpectedException( - exc.OpenStackCloudException, - "Could not determine container access for ACL: invalid" + exc.OpenStackCloudException, + "Could not determine container access for ACL: invalid", ): self.cloud.get_container_access(self.container) def test_get_container_access_not_found(self): - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - status_code=404)]) + self.register_uris( + [dict(method='HEAD', uri=self.container_endpoint, status_code=404)] + ) with testtools.ExpectedException( - exc.OpenStackCloudException, - "Container not found: %s" % self.container + exc.OpenStackCloudException, + "Container not found: %s" % self.container, ): self.cloud.get_container_access(self.container) def test_list_containers(self): - endpoint = '{endpoint}/'.format( - endpoint=self.endpoint) - containers = [ - {u'count': 0, u'bytes': 0, u'name': self.container}] + endpoint = '{endpoint}/'.format(endpoint=self.endpoint) + containers = [{u'count': 0, u'bytes': 0, u'name': self.container}] - self.register_uris([dict(method='GET', uri=endpoint, complete_qs=True, - json=containers)]) + self.register_uris( + [ + dict( + method='GET', + uri=endpoint, + complete_qs=True, + json=containers, + ) + ] + ) ret = self.cloud.list_containers() @@ -269,13 +356,21 @@ class TestObject(BaseTestObject): self._compare_containers(a, b) def test_list_containers_exception(self): - endpoint = '{endpoint}/'.format( - endpoint=self.endpoint) - self.register_uris([dict(method='GET', uri=endpoint, complete_qs=True, - status_code=416)]) + endpoint = '{endpoint}/'.format(endpoint=self.endpoint) + self.register_uris( + [ + dict( + method='GET', + uri=endpoint, + complete_qs=True, + status_code=416, + ) + ] + ) self.assertRaises( - exc.OpenStackCloudException, self.cloud.list_containers) + exc.OpenStackCloudException, self.cloud.list_containers + ) self.assert_calls() @mock.patch('time.time', autospec=True) @@ -283,20 +378,26 @@ class TestObject(BaseTestObject): mock_time.return_value = 12345 - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'X-Container-Meta-Temp-Url-Key': 'amazingly-secure-key', - 'Content-Type': 'text/plain; charset=utf-8'}) - ]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'X-Container-Meta-Temp-Url-Key': 'amazingly-secure-key', # noqa: E501 + 'Content-Type': 'text/plain; charset=utf-8', + }, + ) + ] + ) self.assertEqual( (13345, '60731fb66d46c97cdcb79b6154363179c500b9d9'), self.cloud.object_store.generate_form_signature( @@ -304,7 +405,11 @@ class TestObject(BaseTestObject): object_prefix='prefix/location', redirect_url='https://example.com/location', max_file_size=1024 * 1024 * 1024, - max_upload_count=10, timeout=1000, temp_url_key=None)) + max_upload_count=10, + timeout=1000, + temp_url_key=None, + ), + ) self.assert_calls() @mock.patch('time.time', autospec=True) @@ -312,22 +417,32 @@ class TestObject(BaseTestObject): mock_time.return_value = 12345 - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'Content-Type': 'text/plain; charset=utf-8'}), - dict(method='HEAD', uri=self.endpoint + '/', - headers={ - 'X-Account-Meta-Temp-Url-Key': 'amazingly-secure-key'}), - ]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'Content-Type': 'text/plain; charset=utf-8', + }, + ), + dict( + method='HEAD', + uri=self.endpoint + '/', + headers={ + 'X-Account-Meta-Temp-Url-Key': 'amazingly-secure-key' + }, + ), + ] + ) self.assertEqual( (13345, '3cb9bc83d5a4136421bb2c1f58b963740566646f'), self.cloud.object_store.generate_form_signature( @@ -335,7 +450,11 @@ class TestObject(BaseTestObject): object_prefix='prefix/location', redirect_url='https://example.com/location', max_file_size=1024 * 1024 * 1024, - max_upload_count=10, timeout=1000, temp_url_key=None)) + max_upload_count=10, + timeout=1000, + temp_url_key=None, + ), + ) self.assert_calls() @mock.patch('time.time') @@ -350,27 +469,35 @@ class TestObject(BaseTestObject): object_prefix='prefix/location', redirect_url='https://example.com/location', max_file_size=1024 * 1024 * 1024, - max_upload_count=10, timeout=1000, - temp_url_key='amazingly-secure-key')) + max_upload_count=10, + timeout=1000, + temp_url_key='amazingly-secure-key', + ), + ) self.assert_calls() def test_generate_form_signature_no_key(self): - self.register_uris([ - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'Content-Length': '0', - 'X-Container-Object-Count': '0', - 'Accept-Ranges': 'bytes', - 'X-Storage-Policy': 'Policy-0', - 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', - 'X-Timestamp': '1481912480.41664', - 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', - 'X-Container-Bytes-Used': '0', - 'Content-Type': 'text/plain; charset=utf-8'}), - dict(method='HEAD', uri=self.endpoint + '/', - headers={}), - ]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.container_endpoint, + headers={ + 'Content-Length': '0', + 'X-Container-Object-Count': '0', + 'Accept-Ranges': 'bytes', + 'X-Storage-Policy': 'Policy-0', + 'Date': 'Fri, 16 Dec 2016 18:29:05 GMT', + 'X-Timestamp': '1481912480.41664', + 'X-Trans-Id': 'tx60ec128d9dbf44b9add68-0058543271dfw1', + 'X-Container-Bytes-Used': '0', + 'Content-Type': 'text/plain; charset=utf-8', + }, + ), + dict(method='HEAD', uri=self.endpoint + '/', headers={}), + ] + ) self.assertRaises( exceptions.SDKException, self.cloud.object_store.generate_form_signature, @@ -378,23 +505,33 @@ class TestObject(BaseTestObject): object_prefix='prefix/location', redirect_url='https://example.com/location', max_file_size=1024 * 1024 * 1024, - max_upload_count=10, timeout=1000, temp_url_key=None) + max_upload_count=10, + timeout=1000, + temp_url_key=None, + ) self.assert_calls() def test_set_account_temp_url_key(self): key = 'super-secure-key' - self.register_uris([ - dict(method='POST', uri=self.endpoint + '/', - status_code=204, - validate=dict( - headers={ - 'x-account-meta-temp-url-key': key})), - dict(method='HEAD', uri=self.endpoint + '/', - headers={ - 'x-account-meta-temp-url-key': key}), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.endpoint + '/', + status_code=204, + validate=dict( + headers={'x-account-meta-temp-url-key': key} + ), + ), + dict( + method='HEAD', + uri=self.endpoint + '/', + headers={'x-account-meta-temp-url-key': key}, + ), + ] + ) self.cloud.object_store.set_account_temp_url_key(key) self.assert_calls() @@ -402,16 +539,23 @@ class TestObject(BaseTestObject): key = 'super-secure-key' - self.register_uris([ - dict(method='POST', uri=self.endpoint + '/', - status_code=204, - validate=dict( - headers={ - 'x-account-meta-temp-url-key-2': key})), - dict(method='HEAD', uri=self.endpoint + '/', - headers={ - 'x-account-meta-temp-url-key-2': key}), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.endpoint + '/', + status_code=204, + validate=dict( + headers={'x-account-meta-temp-url-key-2': key} + ), + ), + dict( + method='HEAD', + uri=self.endpoint + '/', + headers={'x-account-meta-temp-url-key-2': key}, + ), + ] + ) self.cloud.object_store.set_account_temp_url_key(key, secondary=True) self.assert_calls() @@ -419,16 +563,23 @@ class TestObject(BaseTestObject): key = 'super-secure-key' - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=204, - validate=dict( - headers={ - 'x-container-meta-temp-url-key': key})), - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'x-container-meta-temp-url-key': key}), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.container_endpoint, + status_code=204, + validate=dict( + headers={'x-container-meta-temp-url-key': key} + ), + ), + dict( + method='HEAD', + uri=self.container_endpoint, + headers={'x-container-meta-temp-url-key': key}, + ), + ] + ) self.cloud.object_store.set_container_temp_url_key(self.container, key) self.assert_calls() @@ -436,33 +587,46 @@ class TestObject(BaseTestObject): key = 'super-secure-key' - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=204, - validate=dict( - headers={ - 'x-container-meta-temp-url-key-2': key})), - dict(method='HEAD', uri=self.container_endpoint, - headers={ - 'x-container-meta-temp-url-key-2': key}), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.container_endpoint, + status_code=204, + validate=dict( + headers={'x-container-meta-temp-url-key-2': key} + ), + ), + dict( + method='HEAD', + uri=self.container_endpoint, + headers={'x-container-meta-temp-url-key-2': key}, + ), + ] + ) self.cloud.object_store.set_container_temp_url_key( - self.container, key, secondary=True) + self.container, key, secondary=True + ) self.assert_calls() def test_list_objects(self): endpoint = '{endpoint}?format=json'.format( - endpoint=self.container_endpoint) + endpoint=self.container_endpoint + ) - objects = [{ - u'bytes': 20304400896, - u'last_modified': u'2016-12-15T13:34:13.650090', - u'hash': u'daaf9ed2106d09bba96cf193d866445e', - u'name': self.object, - u'content_type': u'application/octet-stream'}] + objects = [ + { + u'bytes': 20304400896, + u'last_modified': u'2016-12-15T13:34:13.650090', + u'hash': u'daaf9ed2106d09bba96cf193d866445e', + u'name': self.object, + u'content_type': u'application/octet-stream', + } + ] - self.register_uris([dict(method='GET', uri=endpoint, complete_qs=True, - json=objects)]) + self.register_uris( + [dict(method='GET', uri=endpoint, complete_qs=True, json=objects)] + ) ret = self.cloud.list_objects(self.container) @@ -472,17 +636,22 @@ class TestObject(BaseTestObject): def test_list_objects_with_prefix(self): endpoint = '{endpoint}?format=json&prefix=test'.format( - endpoint=self.container_endpoint) + endpoint=self.container_endpoint + ) - objects = [{ - u'bytes': 20304400896, - u'last_modified': u'2016-12-15T13:34:13.650090', - u'hash': u'daaf9ed2106d09bba96cf193d866445e', - u'name': self.object, - u'content_type': u'application/octet-stream'}] + objects = [ + { + u'bytes': 20304400896, + u'last_modified': u'2016-12-15T13:34:13.650090', + u'hash': u'daaf9ed2106d09bba96cf193d866445e', + u'name': self.object, + u'content_type': u'application/octet-stream', + } + ] - self.register_uris([dict(method='GET', uri=endpoint, complete_qs=True, - json=objects)]) + self.register_uris( + [dict(method='GET', uri=endpoint, complete_qs=True, json=objects)] + ) ret = self.cloud.list_objects(self.container, prefix='test') @@ -492,27 +661,47 @@ class TestObject(BaseTestObject): def test_list_objects_exception(self): endpoint = '{endpoint}?format=json'.format( - endpoint=self.container_endpoint) - self.register_uris([dict(method='GET', uri=endpoint, complete_qs=True, - status_code=416)]) + endpoint=self.container_endpoint + ) + self.register_uris( + [ + dict( + method='GET', + uri=endpoint, + complete_qs=True, + status_code=416, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.list_objects, self.container) + self.cloud.list_objects, + self.container, + ) self.assert_calls() def test_delete_object(self): - self.register_uris([ - dict(method='HEAD', uri=self.object_endpoint, - headers={'X-Object-Meta': 'foo'}), - dict(method='DELETE', uri=self.object_endpoint, status_code=204)]) + self.register_uris( + [ + dict( + method='HEAD', + uri=self.object_endpoint, + headers={'X-Object-Meta': 'foo'}, + ), + dict( + method='DELETE', uri=self.object_endpoint, status_code=204 + ), + ] + ) self.assertTrue(self.cloud.delete_object(self.container, self.object)) self.assert_calls() def test_delete_object_not_found(self): - self.register_uris([dict(method='HEAD', uri=self.object_endpoint, - status_code=404)]) + self.register_uris( + [dict(method='HEAD', uri=self.object_endpoint, status_code=404)] + ) self.assertFalse(self.cloud.delete_object(self.container, self.object)) @@ -533,21 +722,27 @@ class TestObject(BaseTestObject): } response_headers = {k.lower(): v for k, v in headers.items()} text = 'test body' - self.register_uris([ - dict(method='GET', uri=self.object_endpoint, - headers={ - 'Content-Length': '20304400896', - 'Content-Type': 'application/octet-stream', - 'Accept-Ranges': 'bytes', - 'Last-Modified': 'Thu, 15 Dec 2016 13:34:14 GMT', - 'Etag': '"b5c454b44fbd5344793e3fb7e3850768"', - 'X-Timestamp': '1481808853.65009', - 'X-Trans-Id': 'tx68c2a2278f0c469bb6de1-005857ed80dfw1', - 'Date': 'Mon, 19 Dec 2016 14:24:00 GMT', - 'X-Static-Large-Object': 'True', - 'X-Object-Meta-Mtime': '1481513709.168512', - }, - text='test body')]) + self.register_uris( + [ + dict( + method='GET', + uri=self.object_endpoint, + headers={ + 'Content-Length': '20304400896', + 'Content-Type': 'application/octet-stream', + 'Accept-Ranges': 'bytes', + 'Last-Modified': 'Thu, 15 Dec 2016 13:34:14 GMT', + 'Etag': '"b5c454b44fbd5344793e3fb7e3850768"', + 'X-Timestamp': '1481808853.65009', + 'X-Trans-Id': 'tx68c2a2278f0c469bb6de1-005857ed80dfw1', + 'Date': 'Mon, 19 Dec 2016 14:24:00 GMT', + 'X-Static-Large-Object': 'True', + 'X-Object-Meta-Mtime': '1481513709.168512', + }, + text='test body', + ) + ] + ) resp = self.cloud.get_object(self.container, self.object) @@ -557,21 +752,27 @@ class TestObject(BaseTestObject): def test_stream_object(self): text = b'test body' - self.register_uris([ - dict(method='GET', uri=self.object_endpoint, - headers={ - 'Content-Length': '20304400896', - 'Content-Type': 'application/octet-stream', - 'Accept-Ranges': 'bytes', - 'Last-Modified': 'Thu, 15 Dec 2016 13:34:14 GMT', - 'Etag': '"b5c454b44fbd5344793e3fb7e3850768"', - 'X-Timestamp': '1481808853.65009', - 'X-Trans-Id': 'tx68c2a2278f0c469bb6de1-005857ed80dfw1', - 'Date': 'Mon, 19 Dec 2016 14:24:00 GMT', - 'X-Static-Large-Object': 'True', - 'X-Object-Meta-Mtime': '1481513709.168512', - }, - text='test body')]) + self.register_uris( + [ + dict( + method='GET', + uri=self.object_endpoint, + headers={ + 'Content-Length': '20304400896', + 'Content-Type': 'application/octet-stream', + 'Accept-Ranges': 'bytes', + 'Last-Modified': 'Thu, 15 Dec 2016 13:34:14 GMT', + 'Etag': '"b5c454b44fbd5344793e3fb7e3850768"', + 'X-Timestamp': '1481808853.65009', + 'X-Trans-Id': 'tx68c2a2278f0c469bb6de1-005857ed80dfw1', + 'Date': 'Mon, 19 Dec 2016 14:24:00 GMT', + 'X-Static-Large-Object': 'True', + 'X-Object-Meta-Mtime': '1481513709.168512', + }, + text='test body', + ) + ] + ) response_text = b'' for data in self.cloud.stream_object(self.container, self.object): @@ -582,9 +783,11 @@ class TestObject(BaseTestObject): self.assertEqual(text, response_text) def test_stream_object_not_found(self): - self.register_uris([ - dict(method='GET', uri=self.object_endpoint, status_code=404), - ]) + self.register_uris( + [ + dict(method='GET', uri=self.object_endpoint, status_code=404), + ] + ) response_text = b'' for data in self.cloud.stream_object(self.container, self.object): @@ -595,21 +798,25 @@ class TestObject(BaseTestObject): self.assertEqual(b'', response_text) def test_get_object_not_found(self): - self.register_uris([dict(method='GET', - uri=self.object_endpoint, status_code=404)]) + self.register_uris( + [dict(method='GET', uri=self.object_endpoint, status_code=404)] + ) self.assertIsNone(self.cloud.get_object(self.container, self.object)) self.assert_calls() def test_get_object_exception(self): - self.register_uris([dict(method='GET', uri=self.object_endpoint, - status_code=416)]) + self.register_uris( + [dict(method='GET', uri=self.object_endpoint, status_code=416)] + ) self.assertRaises( openstack.cloud.OpenStackCloudException, self.cloud.get_object, - self.container, self.object) + self.container, + self.object, + ) self.assert_calls() @@ -617,49 +824,78 @@ class TestObject(BaseTestObject): # Register directly becuase we make multiple calls. The number # of calls we make isn't interesting - what we do with the return # values is. Don't run assert_calls for the same reason. - self.register_uris([ - dict(method='GET', uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': 1000}, - slo={'min_segment_size': 500}), - headers={'Content-Type': 'application/json'})]) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': 1000}, + slo={'min_segment_size': 500}, + ), + headers={'Content-Type': 'application/json'}, + ) + ] + ) self.assertEqual(500, self.cloud.get_object_segment_size(400)) self.assertEqual(900, self.cloud.get_object_segment_size(900)) self.assertEqual(1000, self.cloud.get_object_segment_size(1000)) self.assertEqual(1000, self.cloud.get_object_segment_size(1100)) def test_get_object_segment_size_http_404(self): - self.register_uris([ - dict(method='GET', uri='https://object-store.example.com/info', - status_code=404, reason='Not Found')]) - self.assertEqual(_proxy.DEFAULT_OBJECT_SEGMENT_SIZE, - self.cloud.get_object_segment_size(None)) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + status_code=404, + reason='Not Found', + ) + ] + ) + self.assertEqual( + _proxy.DEFAULT_OBJECT_SEGMENT_SIZE, + self.cloud.get_object_segment_size(None), + ) self.assert_calls() def test_get_object_segment_size_http_412(self): - self.register_uris([ - dict(method='GET', uri='https://object-store.example.com/info', - status_code=412, reason='Precondition failed')]) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + status_code=412, + reason='Precondition failed', + ) + ] + ) self.assertEqual( _proxy.DEFAULT_OBJECT_SEGMENT_SIZE, - self.cloud.get_object_segment_size(None)) + self.cloud.get_object_segment_size(None), + ) self.assert_calls() def test_update_container_cors(self): headers = { 'X-Container-Meta-Web-Index': 'index.html', - 'X-Container-Meta-Access-Control-Allow-Origin': '*' + 'X-Container-Meta-Access-Control-Allow-Origin': '*', } - self.register_uris([ - dict(method='POST', uri=self.container_endpoint, - status_code=204, - validate=dict(headers=headers))]) + self.register_uris( + [ + dict( + method='POST', + uri=self.container_endpoint, + status_code=204, + validate=dict(headers=headers), + ) + ] + ) self.cloud.update_container(self.container, headers=headers) self.assert_calls() class TestObjectUploads(BaseTestObject): - def setUp(self): super(TestObjectUploads, self).setUp() @@ -667,83 +903,112 @@ class TestObjectUploads(BaseTestObject): self.object_file = tempfile.NamedTemporaryFile(delete=False) self.object_file.write(self.content) self.object_file.close() - (self.md5, self.sha256) = utils._get_file_hashes( - self.object_file.name) + (self.md5, self.sha256) = utils._get_file_hashes(self.object_file.name) self.endpoint = self.cloud._object_store_client.get_endpoint() def test_create_object(self): - self.register_uris([ - dict(method='GET', - uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': 1000}, - slo={'min_segment_size': 500})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, container=self.container, - object=self.object), - status_code=404), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': 1000}, + slo={'min_segment_size': 500}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + } + ), + ), + ] + ) self.cloud.create_object( - container=self.container, name=self.object, - filename=self.object_file.name) + container=self.container, + name=self.object, + filename=self.object_file.name, + ) self.assert_calls() def test_create_object_index_rax(self): - self.register_uris([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object='index.html'), - status_code=201, - validate=dict( - headers={ - 'access-control-allow-origin': '*', - 'content-type': 'text/html' - })) - ]) + self.register_uris( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object='index.html', + ), + status_code=201, + validate=dict( + headers={ + 'access-control-allow-origin': '*', + 'content-type': 'text/html', + } + ), + ) + ] + ) headers = { 'access-control-allow-origin': '*', - 'content-type': 'text/html' + 'content-type': 'text/html', } self.cloud.create_object( - self.container, name='index.html', - data='', - **headers) + self.container, name='index.html', data='', **headers + ) self.assert_calls() def test_create_directory_marker_object(self): - self.register_uris([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - headers={ - 'content-type': 'application/directory', - })) - ]) + self.register_uris( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + headers={ + 'content-type': 'application/directory', + } + ), + ) + ] + ) self.cloud.create_directory_marker_object( - container=self.container, name=self.object) + container=self.container, name=self.object + ) self.assert_calls() @@ -753,55 +1018,80 @@ class TestObjectUploads(BaseTestObject): min_file_size = 1 uris_to_mock = [ - dict(method='GET', - uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': max_file_size}, - slo={'min_segment_size': min_file_size})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, container=self.container, - object=self.object), - status_code=404) + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': max_file_size}, + slo={'min_segment_size': min_file_size}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), ] uris_to_mock.extend( - [dict(method='PUT', - uri='{endpoint}/{container}/{object}/{index:0>6}'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object, - index=index), - status_code=201) + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/{index:0>6}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + index=index, + ), + status_code=201, + ) for index, offset in enumerate( - range(0, len(self.content), max_file_size))] + range(0, len(self.content), max_file_size) + ) + ] ) uris_to_mock.append( - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - headers={ - 'x-object-manifest': '{container}/{object}'.format( - container=self.container, object=self.object), - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - }))) + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + headers={ + 'x-object-manifest': '{container}/{object}'.format( + container=self.container, object=self.object + ), + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + } + ), + ) + ) self.register_uris(uris_to_mock) self.cloud.create_object( - container=self.container, name=self.object, - filename=self.object_file.name, use_slo=False) + container=self.container, + name=self.object, + filename=self.object_file.name, + use_slo=False, + ) # After call 3, order become indeterminate because of thread pool self.assert_calls(stop_after=3) for key, value in self.calls[-1]['headers'].items(): self.assertEqual( - value, self.adapter.request_history[-1].headers[key], - 'header mismatch in manifest call') + value, + self.adapter.request_history[-1].headers[key], + 'header mismatch in manifest call', + ) def test_create_static_large_object(self): @@ -809,88 +1099,118 @@ class TestObjectUploads(BaseTestObject): min_file_size = 1 uris_to_mock = [ - dict(method='GET', uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': max_file_size}, - slo={'min_segment_size': min_file_size})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=404) + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': max_file_size}, + slo={'min_segment_size': min_file_size}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), ] - uris_to_mock.extend([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}/{index:0>6}'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object, - index=index), - status_code=201, - headers=dict(Etag='etag{index}'.format(index=index))) - for index, offset in enumerate( - range(0, len(self.content), max_file_size)) - ]) + uris_to_mock.extend( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/{index:0>6}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + index=index, + ), + status_code=201, + headers=dict(Etag='etag{index}'.format(index=index)), + ) + for index, offset in enumerate( + range(0, len(self.content), max_file_size) + ) + ] + ) uris_to_mock.append( - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - }))) + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ) + ) self.register_uris(uris_to_mock) self.cloud.create_object( - container=self.container, name=self.object, - filename=self.object_file.name, use_slo=True) + container=self.container, + name=self.object, + filename=self.object_file.name, + use_slo=True, + ) # After call 3, order become indeterminate because of thread pool self.assert_calls(stop_after=3) for key, value in self.calls[-1]['headers'].items(): self.assertEqual( - value, self.adapter.request_history[-1].headers[key], - 'header mismatch in manifest call') + value, + self.adapter.request_history[-1].headers[key], + 'header mismatch in manifest call', + ) base_object = '/{container}/{object}'.format( - container=self.container, - object=self.object) + container=self.container, object=self.object + ) - self.assertEqual([ - { - 'path': "{base_object}/000000".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag0', - }, - { - 'path': "{base_object}/000001".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag1', - }, - { - 'path': "{base_object}/000002".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag2', - }, - { - 'path': "{base_object}/000003".format( - base_object=base_object), - 'size_bytes': len(self.object) - 75, - 'etag': 'etag3', - }, - ], self.adapter.request_history[-1].json()) + self.assertEqual( + [ + { + 'path': "{base_object}/000000".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag0', + }, + { + 'path': "{base_object}/000001".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag1', + }, + { + 'path': "{base_object}/000002".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag2', + }, + { + 'path': "{base_object}/000003".format( + base_object=base_object + ), + 'size_bytes': len(self.object) - 75, + 'etag': 'etag3', + }, + ], + self.adapter.request_history[-1].json(), + ) def test_slo_manifest_retry(self): """ @@ -901,117 +1221,154 @@ class TestObjectUploads(BaseTestObject): min_file_size = 1 uris_to_mock = [ - dict(method='GET', uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': max_file_size}, - slo={'min_segment_size': min_file_size})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=404) + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': max_file_size}, + slo={'min_segment_size': min_file_size}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), ] - uris_to_mock.extend([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}/{index:0>6}'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object, - index=index), - status_code=201, - headers=dict(Etag='etag{index}'.format(index=index))) - for index, offset in enumerate( - range(0, len(self.content), max_file_size)) - ]) + uris_to_mock.extend( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/{index:0>6}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + index=index, + ), + status_code=201, + headers=dict(Etag='etag{index}'.format(index=index)), + ) + for index, offset in enumerate( + range(0, len(self.content), max_file_size) + ) + ] + ) # manifest file upload calls - uris_to_mock.extend([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=400, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=400, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })), - ]) + uris_to_mock.extend( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=400, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=400, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + ] + ) self.register_uris(uris_to_mock) self.cloud.create_object( - container=self.container, name=self.object, - filename=self.object_file.name, use_slo=True) + container=self.container, + name=self.object, + filename=self.object_file.name, + use_slo=True, + ) # After call 3, order become indeterminate because of thread pool self.assert_calls(stop_after=3) for key, value in self.calls[-1]['headers'].items(): self.assertEqual( - value, self.adapter.request_history[-1].headers[key], - 'header mismatch in manifest call') + value, + self.adapter.request_history[-1].headers[key], + 'header mismatch in manifest call', + ) base_object = '/{container}/{object}'.format( - container=self.container, - object=self.object) + container=self.container, object=self.object + ) - self.assertEqual([ - { - 'path': "{base_object}/000000".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag0', - }, - { - 'path': "{base_object}/000001".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag1', - }, - { - 'path': "{base_object}/000002".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag2', - }, - { - 'path': "{base_object}/000003".format( - base_object=base_object), - 'size_bytes': len(self.object) - 75, - 'etag': 'etag3', - }, - ], self.adapter.request_history[-1].json()) + self.assertEqual( + [ + { + 'path': "{base_object}/000000".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag0', + }, + { + 'path': "{base_object}/000001".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag1', + }, + { + 'path': "{base_object}/000002".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag2', + }, + { + 'path': "{base_object}/000003".format( + base_object=base_object + ), + 'size_bytes': len(self.object) - 75, + 'etag': 'etag3', + }, + ], + self.adapter.request_history[-1].json(), + ) def test_slo_manifest_fail(self): """ @@ -1023,114 +1380,148 @@ class TestObjectUploads(BaseTestObject): min_file_size = 1 uris_to_mock = [ - dict(method='GET', uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': max_file_size}, - slo={'min_segment_size': min_file_size})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=404) + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': max_file_size}, + slo={'min_segment_size': min_file_size}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), ] - uris_to_mock.extend([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}/{index:0>6}'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object, - index=index), - status_code=201, - headers=dict(Etag='etag{index}'.format(index=index))) - for index, offset in enumerate( - range(0, len(self.content), max_file_size)) - ]) + uris_to_mock.extend( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/{index:0>6}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + index=index, + ), + status_code=201, + headers=dict(Etag='etag{index}'.format(index=index)), + ) + for index, offset in enumerate( + range(0, len(self.content), max_file_size) + ) + ] + ) # manifest file upload calls - uris_to_mock.extend([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=400, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=400, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=400, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })), - ]) + uris_to_mock.extend( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=400, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=400, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=400, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + ] + ) # Cleaning up image upload segments involves calling the # delete_autocreated_image_objects() API method which will list # objects (LIST), get the object metadata (HEAD), then delete the # object (DELETE). - uris_to_mock.extend([ - dict(method='GET', - uri='{endpoint}/images?format=json&prefix={prefix}'.format( - endpoint=self.endpoint, - prefix=self.object), - complete_qs=True, - json=[{ - 'content_type': 'application/octet-stream', - 'bytes': 1437258240, - 'hash': '249219347276c331b87bf1ac2152d9af', - 'last_modified': '2015-02-16T17:50:05.289600', - 'name': self.object - }]), - - dict(method='HEAD', - uri='{endpoint}/images/{object}'.format( - endpoint=self.endpoint, - object=self.object), - headers={ - 'X-Timestamp': '1429036140.50253', - 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', - 'Content-Length': '1290170880', - 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', - 'X-Object-Meta-x-sdk-autocreated': 'true', - 'X-Object-Meta-X-Shade-Sha256': 'does not matter', - 'X-Object-Meta-X-Shade-Md5': 'does not matter', - 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', - 'Accept-Ranges': 'bytes', - 'X-Static-Large-Object': 'false', - 'Content-Type': 'application/octet-stream', - 'Etag': '249219347276c331b87bf1ac2152d9af', - }), - - dict(method='DELETE', - uri='{endpoint}/images/{object}'.format( - endpoint=self.endpoint, object=self.object)) - ]) + uris_to_mock.extend( + [ + dict( + method='GET', + uri='{endpoint}/images?format=json&prefix={prefix}'.format( + endpoint=self.endpoint, prefix=self.object + ), + complete_qs=True, + json=[ + { + 'content_type': 'application/octet-stream', + 'bytes': 1437258240, + 'hash': '249219347276c331b87bf1ac2152d9af', + 'last_modified': '2015-02-16T17:50:05.289600', + 'name': self.object, + } + ], + ), + dict( + method='HEAD', + uri='{endpoint}/images/{object}'.format( + endpoint=self.endpoint, object=self.object + ), + headers={ + 'X-Timestamp': '1429036140.50253', + 'X-Trans-Id': 'txbbb825960a3243b49a36f-005a0dadaedfw1', + 'Content-Length': '1290170880', + 'Last-Modified': 'Tue, 14 Apr 2015 18:29:01 GMT', + 'X-Object-Meta-x-sdk-autocreated': 'true', + 'X-Object-Meta-X-Shade-Sha256': 'does not matter', + 'X-Object-Meta-X-Shade-Md5': 'does not matter', + 'Date': 'Thu, 16 Nov 2017 15:24:30 GMT', + 'Accept-Ranges': 'bytes', + 'X-Static-Large-Object': 'false', + 'Content-Type': 'application/octet-stream', + 'Etag': '249219347276c331b87bf1ac2152d9af', + }, + ), + dict( + method='DELETE', + uri='{endpoint}/images/{object}'.format( + endpoint=self.endpoint, object=self.object + ), + ), + ] + ) self.register_uris(uris_to_mock) @@ -1141,8 +1532,11 @@ class TestObjectUploads(BaseTestObject): self.assertRaises( exc.OpenStackCloudException, self.cloud.create_object, - container=self.container, name=self.object, - filename=self.object_file.name, use_slo=True) + container=self.container, + name=self.object, + filename=self.object_file.name, + use_slo=True, + ) # After call 3, order become indeterminate because of thread pool self.assert_calls(stop_after=3) @@ -1152,52 +1546,81 @@ class TestObjectUploads(BaseTestObject): max_file_size = 25 min_file_size = 1 - self.register_uris([ - dict(method='GET', uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': max_file_size}, - slo={'min_segment_size': min_file_size})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=404), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000000'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - status_code=201), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000001'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - status_code=201), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000002'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - status_code=201), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000003'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - status_code=501), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': max_file_size}, + slo={'min_segment_size': min_file_size}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000000'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000001'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000002'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000003'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=501, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.create_object, - container=self.container, name=self.object, - filename=self.object_file.name, use_slo=True) + container=self.container, + name=self.object, + filename=self.object_file.name, + use_slo=True, + ) # After call 3, order become indeterminate because of thread pool self.assert_calls(stop_after=3) @@ -1207,152 +1630,213 @@ class TestObjectUploads(BaseTestObject): max_file_size = 25 min_file_size = 1 - self.register_uris([ - dict(method='GET', uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': max_file_size}, - slo={'min_segment_size': min_file_size})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=404), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000000'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - headers={'etag': 'etag0'}, - status_code=201), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000001'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - headers={'etag': 'etag1'}, - status_code=201), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000002'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - headers={'etag': 'etag2'}, - status_code=201), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000003'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - status_code=501), - dict(method='PUT', - uri='{endpoint}/{container}/{object}/000003'.format( - endpoint=self.endpoint, - container=self.container, - object=self.object), - status_code=201, - headers={'etag': 'etag3'}), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - params={ - 'multipart-manifest', 'put' - }, - headers={ - 'x-object-meta-x-sdk-md5': self.md5, - 'x-object-meta-x-sdk-sha256': self.sha256, - })) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': max_file_size}, + slo={'min_segment_size': min_file_size}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=404, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000000'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + headers={'etag': 'etag0'}, + status_code=201, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000001'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + headers={'etag': 'etag1'}, + status_code=201, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000002'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + headers={'etag': 'etag2'}, + status_code=201, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000003'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=501, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}/000003'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + headers={'etag': 'etag3'}, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + params={'multipart-manifest', 'put'}, + headers={ + 'x-object-meta-x-sdk-md5': self.md5, + 'x-object-meta-x-sdk-sha256': self.sha256, + }, + ), + ), + ] + ) self.cloud.create_object( - container=self.container, name=self.object, - filename=self.object_file.name, use_slo=True) + container=self.container, + name=self.object, + filename=self.object_file.name, + use_slo=True, + ) # After call 3, order become indeterminate because of thread pool self.assert_calls(stop_after=3) for key, value in self.calls[-1]['headers'].items(): self.assertEqual( - value, self.adapter.request_history[-1].headers[key], - 'header mismatch in manifest call') + value, + self.adapter.request_history[-1].headers[key], + 'header mismatch in manifest call', + ) base_object = '/{container}/{object}'.format( - container=self.container, - object=self.object) + container=self.container, object=self.object + ) - self.assertEqual([ - { - 'path': "{base_object}/000000".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag0', - }, - { - 'path': "{base_object}/000001".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag1', - }, - { - 'path': "{base_object}/000002".format( - base_object=base_object), - 'size_bytes': 25, - 'etag': 'etag2', - }, - { - 'path': "{base_object}/000003".format( - base_object=base_object), - 'size_bytes': len(self.object) - 75, - 'etag': 'etag3', - }, - ], self.adapter.request_history[-1].json()) + self.assertEqual( + [ + { + 'path': "{base_object}/000000".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag0', + }, + { + 'path': "{base_object}/000001".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag1', + }, + { + 'path': "{base_object}/000002".format( + base_object=base_object + ), + 'size_bytes': 25, + 'etag': 'etag2', + }, + { + 'path': "{base_object}/000003".format( + base_object=base_object + ), + 'size_bytes': len(self.object) - 75, + 'etag': 'etag3', + }, + ], + self.adapter.request_history[-1].json(), + ) def test_create_object_skip_checksum(self): - self.register_uris([ - dict(method='GET', - uri='https://object-store.example.com/info', - json=dict( - swift={'max_file_size': 1000}, - slo={'min_segment_size': 500})), - dict(method='HEAD', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, container=self.container, - object=self.object), - status_code=200), - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict(headers={})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://object-store.example.com/info', + json=dict( + swift={'max_file_size': 1000}, + slo={'min_segment_size': 500}, + ), + ), + dict( + method='HEAD', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=200, + ), + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict(headers={}), + ), + ] + ) self.cloud.create_object( - container=self.container, name=self.object, + container=self.container, + name=self.object, filename=self.object_file.name, - generate_checksums=False) + generate_checksums=False, + ) self.assert_calls() def test_create_object_data(self): - self.register_uris([ - dict(method='PUT', - uri='{endpoint}/{container}/{object}'.format( - endpoint=self.endpoint, - container=self.container, object=self.object), - status_code=201, - validate=dict( - headers={}, - data=self.content, - )), - ]) + self.register_uris( + [ + dict( + method='PUT', + uri='{endpoint}/{container}/{object}'.format( + endpoint=self.endpoint, + container=self.container, + object=self.object, + ), + status_code=201, + validate=dict( + headers={}, + data=self.content, + ), + ), + ] + ) self.cloud.create_object( - container=self.container, name=self.object, - data=self.content) + container=self.container, name=self.object, data=self.content + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_openstackcloud.py b/openstack/tests/unit/cloud/test_openstackcloud.py index 07d227995..d2a9e33b3 100644 --- a/openstack/tests/unit/cloud/test_openstackcloud.py +++ b/openstack/tests/unit/cloud/test_openstackcloud.py @@ -19,7 +19,6 @@ from openstack.tests.unit import base class TestSearch(base.TestCase): - class FakeResource(resource.Resource): allow_fetch = True allow_list = True @@ -33,9 +32,7 @@ class TestSearch(base.TestCase): self.session._sdk_connection = self.cloud self.session._get = mock.Mock() self.session._list = mock.Mock() - self.session._resource_registry = dict( - fake=self.FakeResource - ) + self.session._resource_registry = dict(fake=self.FakeResource) # Set the mock into the cloud connection setattr(self.cloud, "mock_session", self.session) @@ -44,7 +41,7 @@ class TestSearch(base.TestCase): exceptions.SDKException, self.cloud.search_resources, "wrong_service.wrong_resource", - "name" + "name", ) def test_raises_unknown_resource(self): @@ -52,44 +49,33 @@ class TestSearch(base.TestCase): exceptions.SDKException, self.cloud.search_resources, "mock_session.wrong_resource", - "name" + "name", ) def test_search_resources_get_finds(self): self.session._get.return_value = self.FakeResource(foo="bar") - ret = self.cloud.search_resources( - "mock_session.fake", - "fake_name" - ) - self.session._get.assert_called_with( - self.FakeResource, "fake_name") + ret = self.cloud.search_resources("mock_session.fake", "fake_name") + self.session._get.assert_called_with(self.FakeResource, "fake_name") self.assertEqual(1, len(ret)) self.assertEqual( - self.FakeResource(foo="bar").to_dict(), - ret[0].to_dict() + self.FakeResource(foo="bar").to_dict(), ret[0].to_dict() ) def test_search_resources_list(self): self.session._get.side_effect = exceptions.ResourceNotFound - self.session._list.return_value = [ - self.FakeResource(foo="bar") - ] + self.session._list.return_value = [self.FakeResource(foo="bar")] - ret = self.cloud.search_resources( - "mock_session.fake", - "fake_name" - ) - self.session._get.assert_called_with( - self.FakeResource, "fake_name") + ret = self.cloud.search_resources("mock_session.fake", "fake_name") + self.session._get.assert_called_with(self.FakeResource, "fake_name") self.session._list.assert_called_with( - self.FakeResource, name="fake_name") + self.FakeResource, name="fake_name" + ) self.assertEqual(1, len(ret)) self.assertEqual( - self.FakeResource(foo="bar").to_dict(), - ret[0].to_dict() + self.FakeResource(foo="bar").to_dict(), ret[0].to_dict() ) def test_search_resources_args(self): @@ -103,33 +89,27 @@ class TestSearch(base.TestCase): get_kwargs={"getkwarg1": "1"}, list_args=["listarg1"], list_kwargs={"listkwarg1": "1"}, - filter1="foo" + filter1="foo", ) self.session._get.assert_called_with( - self.FakeResource, "fake_name", - "getarg1", getkwarg1="1") + self.FakeResource, "fake_name", "getarg1", getkwarg1="1" + ) self.session._list.assert_called_with( self.FakeResource, - "listarg1", listkwarg1="1", - name="fake_name", filter1="foo" + "listarg1", + listkwarg1="1", + name="fake_name", + filter1="foo", ) def test_search_resources_name_empty(self): - self.session._list.return_value = [ - self.FakeResource(foo="bar") - ] + self.session._list.return_value = [self.FakeResource(foo="bar")] - ret = self.cloud.search_resources( - "mock_session.fake", - None, - foo="bar" - ) + ret = self.cloud.search_resources("mock_session.fake", None, foo="bar") self.session._get.assert_not_called() - self.session._list.assert_called_with( - self.FakeResource, foo="bar") + self.session._list.assert_called_with(self.FakeResource, foo="bar") self.assertEqual(1, len(ret)) self.assertEqual( - self.FakeResource(foo="bar").to_dict(), - ret[0].to_dict() + self.FakeResource(foo="bar").to_dict(), ret[0].to_dict() ) diff --git a/openstack/tests/unit/cloud/test_operator.py b/openstack/tests/unit/cloud/test_operator.py index 95078716c..32a2da484 100644 --- a/openstack/tests/unit/cloud/test_operator.py +++ b/openstack/tests/unit/cloud/test_operator.py @@ -22,7 +22,6 @@ from openstack.tests.unit import base class TestOperatorCloud(base.TestCase): - def test_get_image_name(self): self.use_glance() @@ -30,14 +29,20 @@ class TestOperatorCloud(base.TestCase): fake_image = fakes.make_fake_image(image_id=image_id) list_return = {'images': [fake_image]} - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json=list_return), - dict(method='GET', - uri='https://image.example.com/v2/images', - json=list_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=list_return, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=list_return, + ), + ] + ) self.assertEqual('fake_image', self.cloud.get_image_name(image_id)) self.assertEqual('fake_image', self.cloud.get_image_name('fake_image')) @@ -51,14 +56,20 @@ class TestOperatorCloud(base.TestCase): fake_image = fakes.make_fake_image(image_id=image_id) list_return = {'images': [fake_image]} - self.register_uris([ - dict(method='GET', - uri='https://image.example.com/v2/images', - json=list_return), - dict(method='GET', - uri='https://image.example.com/v2/images', - json=list_return), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=list_return, + ), + dict( + method='GET', + uri='https://image.example.com/v2/images', + json=list_return, + ), + ] + ) self.assertEqual(image_id, self.cloud.get_image_id(image_id)) self.assertEqual(image_id, self.cloud.get_image_id('fake_image')) @@ -72,15 +83,17 @@ class TestOperatorCloud(base.TestCase): def side_effect(*args, **kwargs): raise FakeException("No service") + session_mock = mock.Mock() session_mock.get_endpoint.side_effect = side_effect get_session_mock.return_value = session_mock self.cloud.name = 'testcloud' self.cloud.config.config['region_name'] = 'testregion' with testtools.ExpectedException( - exc.OpenStackCloudException, - "Error getting image endpoint on testcloud:testregion:" - " No service"): + exc.OpenStackCloudException, + "Error getting image endpoint on testcloud:testregion:" + " No service", + ): self.cloud.get_session_endpoint("image") @mock.patch.object(cloud_region.CloudRegion, 'get_session') @@ -97,8 +110,11 @@ class TestOperatorCloud(base.TestCase): get_session_mock.return_value = session_mock self.cloud.get_session_endpoint('identity') kwargs = dict( - interface='public', region_name='RegionOne', - service_name=None, service_type='identity') + interface='public', + region_name='RegionOne', + service_name=None, + service_type='identity', + ) session_mock.get_endpoint.assert_called_with(**kwargs) @@ -122,23 +138,23 @@ class TestOperatorCloud(base.TestCase): uuid1 = uuid.uuid4().hex uuid2 = uuid.uuid4().hex self.use_compute_discovery() - self.register_uris([ - dict( - method='GET', - uri='https://compute.example.com/v2.1/os-hypervisors/detail', - json={ - 'hypervisors': [ - fakes.make_fake_hypervisor(uuid1, 'testserver1'), - fakes.make_fake_hypervisor(uuid2, 'testserver2'), - ] - }, - validate={ - 'headers': { - 'OpenStack-API-Version': 'compute 2.53' - } - } - ), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://compute.example.com/v2.1/os-hypervisors/detail', # noqa: E501 + json={ + 'hypervisors': [ + fakes.make_fake_hypervisor(uuid1, 'testserver1'), + fakes.make_fake_hypervisor(uuid2, 'testserver2'), + ] + }, + validate={ + 'headers': {'OpenStack-API-Version': 'compute 2.53'} + }, + ), + ] + ) r = self.cloud.list_hypervisors() @@ -154,19 +170,22 @@ class TestOperatorCloud(base.TestCase): '''This test verifies that calling list_hypervisors on a pre-2.53 cloud calls the old version.''' self.use_compute_discovery( - compute_version_json='old-compute-version.json') - self.register_uris([ - dict( - method='GET', - uri='https://compute.example.com/v2.1/os-hypervisors/detail', - json={ - 'hypervisors': [ - fakes.make_fake_hypervisor('1', 'testserver1'), - fakes.make_fake_hypervisor('2', 'testserver2'), - ] - } - ), - ]) + compute_version_json='old-compute-version.json' + ) + self.register_uris( + [ + dict( + method='GET', + uri='https://compute.example.com/v2.1/os-hypervisors/detail', # noqa: E501 + json={ + 'hypervisors': [ + fakes.make_fake_hypervisor('1', 'testserver1'), + fakes.make_fake_hypervisor('2', 'testserver2'), + ] + }, + ), + ] + ) r = self.cloud.list_hypervisors() diff --git a/openstack/tests/unit/cloud/test_operator_noauth.py b/openstack/tests/unit/cloud/test_operator_noauth.py index 1c098962a..1dacd43ac 100644 --- a/openstack/tests/unit/cloud/test_operator_noauth.py +++ b/openstack/tests/unit/cloud/test_operator_noauth.py @@ -32,19 +32,34 @@ class TestOpenStackCloudOperatorNoAuth(base.TestCase): # By clearing the URI registry, we remove all calls to a keystone # catalog or getting a token self._uri_registry.clear() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - service_type='baremetal', base_url_append='v1'), - json={'id': 'v1', - 'links': [{"href": "https://baremetal.example.com/v1", - "rel": "self"}]}), - dict(method='GET', - uri=self.get_mock_url( - service_type='baremetal', base_url_append='v1', - resource='nodes'), - json={'nodes': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + service_type='baremetal', base_url_append='v1' + ), + json={ + 'id': 'v1', + 'links': [ + { + "href": "https://baremetal.example.com/v1", + "rel": "self", + } + ], + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + service_type='baremetal', + base_url_append='v1', + resource='nodes', + ), + json={'nodes': []}, + ), + ] + ) def test_ironic_noauth_none_auth_type(self): """Test noauth selection for Ironic in OpenStackCloud @@ -58,7 +73,8 @@ class TestOpenStackCloudOperatorNoAuth(base.TestCase): # client library. self.cloud_noauth = openstack.connect( auth_type='none', - baremetal_endpoint_override="https://baremetal.example.com/v1") + baremetal_endpoint_override="https://baremetal.example.com/v1", + ) self.cloud_noauth.list_machines() @@ -92,8 +108,9 @@ class TestOpenStackCloudOperatorNoAuth(base.TestCase): self.cloud_noauth = openstack.connect( auth_type='admin_token', auth=dict( - endpoint='https://baremetal.example.com/v1', - token='ignored')) + endpoint='https://baremetal.example.com/v1', token='ignored' + ), + ) self.cloud_noauth.list_machines() @@ -116,65 +133,94 @@ class TestOpenStackCloudOperatorNoAuthUnversioned(base.TestCase): # By clearing the URI registry, we remove all calls to a keystone # catalog or getting a token self._uri_registry.clear() - self.register_uris([ - dict(method='GET', - uri='https://baremetal.example.com/', - json={ - "default_version": { - "status": "CURRENT", - "min_version": "1.1", - "version": "1.46", - "id": "v1", - "links": [{ - "href": "https://baremetal.example.com/v1", - "rel": "self" - }]}, - "versions": [{ - "status": "CURRENT", - "min_version": "1.1", - "version": "1.46", - "id": "v1", - "links": [{ - "href": "https://baremetal.example.com/v1", - "rel": "self" - }]}], - "name": "OpenStack Ironic API", - "description": "Ironic is an OpenStack project." - }), - dict(method='GET', - uri=self.get_mock_url( - service_type='baremetal', base_url_append='v1'), - json={ - "media_types": [{ - "base": "application/json", - "type": "application/vnd.openstack.ironic.v1+json" - }], - "links": [{ - "href": "https://baremetal.example.com/v1", - "rel": "self" - }], - "ports": [{ - "href": "https://baremetal.example.com/v1/ports/", - "rel": "self" - }, { - "href": "https://baremetal.example.com/ports/", - "rel": "bookmark" - }], - "nodes": [{ - "href": "https://baremetal.example.com/v1/nodes/", - "rel": "self" - }, { - "href": "https://baremetal.example.com/nodes/", - "rel": "bookmark" - }], - "id": "v1" - }), - dict(method='GET', - uri=self.get_mock_url( - service_type='baremetal', base_url_append='v1', - resource='nodes'), - json={'nodes': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='https://baremetal.example.com/', + json={ + "default_version": { + "status": "CURRENT", + "min_version": "1.1", + "version": "1.46", + "id": "v1", + "links": [ + { + "href": "https://baremetal.example.com/v1", + "rel": "self", + } + ], + }, + "versions": [ + { + "status": "CURRENT", + "min_version": "1.1", + "version": "1.46", + "id": "v1", + "links": [ + { + "href": "https://baremetal.example.com/v1", # noqa: E501 + "rel": "self", + } + ], + } + ], + "name": "OpenStack Ironic API", + "description": "Ironic is an OpenStack project.", + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + service_type='baremetal', base_url_append='v1' + ), + json={ + "media_types": [ + { + "base": "application/json", + "type": "application/vnd.openstack.ironic.v1+json", # noqa: E501 + } + ], + "links": [ + { + "href": "https://baremetal.example.com/v1", + "rel": "self", + } + ], + "ports": [ + { + "href": "https://baremetal.example.com/v1/ports/", # noqa: E501 + "rel": "self", + }, + { + "href": "https://baremetal.example.com/ports/", + "rel": "bookmark", + }, + ], + "nodes": [ + { + "href": "https://baremetal.example.com/v1/nodes/", # noqa: E501 + "rel": "self", + }, + { + "href": "https://baremetal.example.com/nodes/", + "rel": "bookmark", + }, + ], + "id": "v1", + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + service_type='baremetal', + base_url_append='v1', + resource='nodes', + ), + json={'nodes': []}, + ), + ] + ) def test_ironic_noauth_none_auth_type(self): """Test noauth selection for Ironic in OpenStackCloud @@ -188,7 +234,8 @@ class TestOpenStackCloudOperatorNoAuthUnversioned(base.TestCase): # client library. self.cloud_noauth = openstack.connect( auth_type='none', - baremetal_endpoint_override="https://baremetal.example.com") + baremetal_endpoint_override="https://baremetal.example.com", + ) self.cloud_noauth.list_machines() diff --git a/openstack/tests/unit/cloud/test_port.py b/openstack/tests/unit/cloud/test_port.py index 04e5726de..0a21aba6f 100644 --- a/openstack/tests/unit/cloud/test_port.py +++ b/openstack/tests/unit/cloud/test_port.py @@ -42,14 +42,11 @@ class TestPort(base.TestCase): 'mac_address': '50:1c:0d:e4:f0:0d', 'binding:profile': {}, 'fixed_ips': [ - { - 'subnet_id': 'test-subnet-id', - 'ip_address': '29.29.29.29' - } + {'subnet_id': 'test-subnet-id', 'ip_address': '29.29.29.29'} ], 'id': 'test-port-id', 'security_groups': [], - 'device_id': '' + 'device_id': '', } } @@ -70,14 +67,11 @@ class TestPort(base.TestCase): 'mac_address': '50:1c:0d:e4:f0:0d', 'binding:profile': {}, 'fixed_ips': [ - { - 'subnet_id': 'test-subnet-id', - 'ip_address': '29.29.29.29' - } + {'subnet_id': 'test-subnet-id', 'ip_address': '29.29.29.29'} ], 'id': 'test-port-id', 'security_groups': [], - 'device_id': '' + 'device_id': '', } } @@ -94,7 +88,7 @@ class TestPort(base.TestCase): 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, - 'ovs_hybrid_plug': True + 'ovs_hybrid_plug': True, }, 'binding:vif_type': 'ovs', 'device_owner': 'network:router_gateway', @@ -104,12 +98,12 @@ class TestPort(base.TestCase): 'fixed_ips': [ { 'subnet_id': '008ba151-0b8c-4a67-98b5-0d2b87666062', - 'ip_address': '172.24.4.2' + 'ip_address': '172.24.4.2', } ], 'id': 'd80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', 'security_groups': [], - 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824' + 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824', }, { 'status': 'ACTIVE', @@ -122,7 +116,7 @@ class TestPort(base.TestCase): 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, - 'ovs_hybrid_plug': True + 'ovs_hybrid_plug': True, }, 'binding:vif_type': 'ovs', 'device_owner': 'network:router_interface', @@ -132,104 +126,155 @@ class TestPort(base.TestCase): 'fixed_ips': [ { 'subnet_id': '288bf4a1-51ba-43b6-9d0a-520e9005db17', - 'ip_address': '10.0.0.1' + 'ip_address': '10.0.0.1', } ], 'id': 'f71a6703-d6de-4be1-a91a-a570ede1d159', 'security_groups': [], - 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824' - } + 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824', + }, ] } def _compare_ports(self, exp, real): self.assertDictEqual( _port.Port(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_create_port(self): - self.register_uris([ - dict(method="POST", - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - json=self.mock_neutron_port_create_rep, - validate=dict( - json={'port': { - 'network_id': 'test-net-id', - 'name': 'test-port-name', - 'admin_state_up': True}})) - ]) + self.register_uris( + [ + dict( + method="POST", + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + json=self.mock_neutron_port_create_rep, + validate=dict( + json={ + 'port': { + 'network_id': 'test-net-id', + 'name': 'test-port-name', + 'admin_state_up': True, + } + } + ), + ) + ] + ) port = self.cloud.create_port( - network_id='test-net-id', name='test-port-name', - admin_state_up=True) + network_id='test-net-id', + name='test-port-name', + admin_state_up=True, + ) self._compare_ports(self.mock_neutron_port_create_rep['port'], port) self.assert_calls() def test_create_port_parameters(self): """Test that we detect invalid arguments passed to create_port""" self.assertRaises( - TypeError, self.cloud.create_port, - network_id='test-net-id', nome='test-port-name', - stato_amministrativo_porta=True) + TypeError, + self.cloud.create_port, + network_id='test-net-id', + nome='test-port-name', + stato_amministrativo_porta=True, + ) def test_create_port_exception(self): - self.register_uris([ - dict(method="POST", - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - status_code=500, - validate=dict( - json={'port': { - 'network_id': 'test-net-id', - 'name': 'test-port-name', - 'admin_state_up': True}})) - ]) + self.register_uris( + [ + dict( + method="POST", + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + status_code=500, + validate=dict( + json={ + 'port': { + 'network_id': 'test-net-id', + 'name': 'test-port-name', + 'admin_state_up': True, + } + } + ), + ) + ] + ) self.assertRaises( - OpenStackCloudException, self.cloud.create_port, - network_id='test-net-id', name='test-port-name', - admin_state_up=True) + OpenStackCloudException, + self.cloud.create_port, + network_id='test-net-id', + name='test-port-name', + admin_state_up=True, + ) self.assert_calls() def test_create_port_with_project(self): self.mock_neutron_port_create_rep["port"].update( { 'project_id': 'test-project-id', - }) - self.register_uris([ - dict(method="POST", - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - json=self.mock_neutron_port_create_rep, - validate=dict( - json={'port': { - 'network_id': 'test-net-id', - 'project_id': 'test-project-id', - 'name': 'test-port-name', - 'admin_state_up': True}})) - ]) + } + ) + self.register_uris( + [ + dict( + method="POST", + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + json=self.mock_neutron_port_create_rep, + validate=dict( + json={ + 'port': { + 'network_id': 'test-net-id', + 'project_id': 'test-project-id', + 'name': 'test-port-name', + 'admin_state_up': True, + } + } + ), + ) + ] + ) port = self.cloud.create_port( - network_id='test-net-id', name='test-port-name', - admin_state_up=True, project_id='test-project-id') + network_id='test-net-id', + name='test-port-name', + admin_state_up=True, + project_id='test-project-id', + ) self._compare_ports(self.mock_neutron_port_create_rep['port'], port) self.assert_calls() def test_update_port(self): port_id = 'd80b1a3b-4fc1-49f3-952e-1e2ab7081d8b' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports', port_id]), - json=dict(port=self.mock_neutron_port_list_rep['ports'][0])), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', port_id]), - json=self.mock_neutron_port_update_rep, - validate=dict( - json={'port': {'name': 'test-port-name-updated'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports', port_id] + ), + json=dict( + port=self.mock_neutron_port_list_rep['ports'][0] + ), + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports', port_id] + ), + json=self.mock_neutron_port_update_rep, + validate=dict( + json={'port': {'name': 'test-port-name-updated'}} + ), + ), + ] + ) port = self.cloud.update_port( - name_or_id=port_id, name='test-port-name-updated') + name_or_id=port_id, name='test-port-name-updated' + ) self._compare_ports(self.mock_neutron_port_update_rep['port'], port) self.assert_calls() @@ -237,72 +282,107 @@ class TestPort(base.TestCase): def test_update_port_parameters(self): """Test that we detect invalid arguments passed to update_port""" self.assertRaises( - TypeError, self.cloud.update_port, - name_or_id='test-port-id', nome='test-port-name-updated') + TypeError, + self.cloud.update_port, + name_or_id='test-port-id', + nome='test-port-name-updated', + ) def test_update_port_exception(self): port_id = 'd80b1a3b-4fc1-49f3-952e-1e2ab7081d8b' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports', port_id]), - json=self.mock_neutron_port_list_rep), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', port_id]), - status_code=500, - validate=dict( - json={'port': {'name': 'test-port-name-updated'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports', port_id] + ), + json=self.mock_neutron_port_list_rep, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports', port_id] + ), + status_code=500, + validate=dict( + json={'port': {'name': 'test-port-name-updated'}} + ), + ), + ] + ) self.assertRaises( - OpenStackCloudException, self.cloud.update_port, + OpenStackCloudException, + self.cloud.update_port, name_or_id='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', - name='test-port-name-updated') + name='test-port-name-updated', + ) self.assert_calls() def test_list_ports(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - json=self.mock_neutron_port_list_rep) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + json=self.mock_neutron_port_list_rep, + ) + ] + ) ports = self.cloud.list_ports() for a, b in zip(self.mock_neutron_port_list_rep['ports'], ports): self._compare_ports(a, b) self.assert_calls() def test_list_ports_filtered(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['status=DOWN']), - json=self.mock_neutron_port_list_rep) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['status=DOWN'], + ), + json=self.mock_neutron_port_list_rep, + ) + ] + ) ports = self.cloud.list_ports(filters={'status': 'DOWN'}) for a, b in zip(self.mock_neutron_port_list_rep['ports'], ports): self._compare_ports(a, b) self.assert_calls() def test_list_ports_exception(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - status_code=500) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + status_code=500, + ) + ] + ) self.assertRaises(OpenStackCloudException, self.cloud.list_ports) def test_search_ports_by_id(self): port_id = 'f71a6703-d6de-4be1-a91a-a570ede1d159' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - json=self.mock_neutron_port_list_rep) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + json=self.mock_neutron_port_list_rep, + ) + ] + ) ports = self.cloud.search_ports(name_or_id=port_id) self.assertEqual(1, len(ports)) @@ -311,12 +391,17 @@ class TestPort(base.TestCase): def test_search_ports_by_name(self): port_name = "first-port" - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - json=self.mock_neutron_port_list_rep) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + json=self.mock_neutron_port_list_rep, + ) + ] + ) ports = self.cloud.search_ports(name_or_id=port_name) self.assertEqual(1, len(ports)) @@ -324,51 +409,80 @@ class TestPort(base.TestCase): self.assert_calls() def test_search_ports_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports']), - json=self.mock_neutron_port_list_rep) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports'] + ), + json=self.mock_neutron_port_list_rep, + ) + ] + ) ports = self.cloud.search_ports(name_or_id='non-existent') self.assertEqual(0, len(ports)) self.assert_calls() def test_delete_port(self): port_id = 'd80b1a3b-4fc1-49f3-952e-1e2ab7081d8b' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', 'first-port']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['name=first-port']), - json=self.mock_neutron_port_list_rep), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', port_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', 'first-port'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['name=first-port'], + ), + json=self.mock_neutron_port_list_rep, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'ports', port_id] + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_port(name_or_id='first-port')) def test_delete_port_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports', - 'non-existent']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['name=non-existent']), - json={'ports': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', 'non-existent'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['name=non-existent'], + ), + json={'ports': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_port(name_or_id='non-existent')) self.assert_calls() @@ -376,50 +490,78 @@ class TestPort(base.TestCase): port_name = "port-name" port1 = dict(id='123', name=port_name) port2 = dict(id='456', name=port_name) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports', port_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=['name=%s' % port_name]), - json={'ports': [port1, port2]}) - ]) - self.assertRaises(OpenStackCloudException, - self.cloud.delete_port, port_name) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', port_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=['name=%s' % port_name], + ), + json={'ports': [port1, port2]}, + ), + ] + ) + self.assertRaises( + OpenStackCloudException, self.cloud.delete_port, port_name + ) self.assert_calls() def test_delete_subnet_multiple_using_id(self): port_name = "port-name" port1 = dict(id='123', name=port_name) port2 = dict(id='456', name=port_name) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', port1['id']]), - json={'ports': [port1, port2]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'ports', port1['id']]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', port1['id']], + ), + json={'ports': [port1, port2]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', port1['id']], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_port(name_or_id=port1['id'])) self.assert_calls() def test_get_port_by_id(self): fake_port = dict(id='123', name='456') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', - 'ports', - fake_port['id']]), - json={'port': fake_port}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports', fake_port['id']], + ), + json={'port': fake_port}, + ) + ] + ) r = self.cloud.get_port_by_id(fake_port['id']) self.assertIsNotNone(r) self._compare_ports(fake_port, r) diff --git a/openstack/tests/unit/cloud/test_project.py b/openstack/tests/unit/cloud/test_project.py index b7361b97e..dde72440d 100644 --- a/openstack/tests/unit/cloud/test_project.py +++ b/openstack/tests/unit/cloud/test_project.py @@ -21,10 +21,16 @@ from openstack.tests.unit import base class TestProject(base.TestCase): - - def get_mock_url(self, service_type='identity', interface='public', - resource=None, append=None, base_url_append=None, - v3=True, qs_elements=None): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource=None, + append=None, + base_url_append=None, + v3=True, + qs_elements=None, + ): if v3 and resource is None: resource = 'projects' elif not v3 and resource is None: @@ -32,170 +38,238 @@ class TestProject(base.TestCase): if base_url_append is None and v3: base_url_append = 'v3' return super(TestProject, self).get_mock_url( - service_type=service_type, interface=interface, resource=resource, - append=append, base_url_append=base_url_append, - qs_elements=qs_elements) + service_type=service_type, + interface=interface, + resource=resource, + append=append, + base_url_append=base_url_append, + qs_elements=qs_elements, + ) - def test_create_project_v3(self,): + def test_create_project_v3( + self, + ): project_data = self._get_project_data( description=self.getUniqueString('projectDesc'), - parent_id=uuid.uuid4().hex) + parent_id=uuid.uuid4().hex, + ) reference_req = project_data.json_request.copy() reference_req['project']['enabled'] = True - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=project_data.json_response, - validate=dict(json=reference_req)) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=project_data.json_response, + validate=dict(json=reference_req), + ) + ] + ) project = self.cloud.create_project( name=project_data.project_name, description=project_data.description, domain_id=project_data.domain_id, - parent_id=project_data.parent_id) + parent_id=project_data.parent_id, + ) self.assertThat(project.id, matchers.Equals(project_data.project_id)) self.assertThat( - project.name, matchers.Equals(project_data.project_name)) + project.name, matchers.Equals(project_data.project_name) + ) self.assertThat( - project.description, matchers.Equals(project_data.description)) + project.description, matchers.Equals(project_data.description) + ) self.assertThat( - project.domain_id, matchers.Equals(project_data.domain_id)) + project.domain_id, matchers.Equals(project_data.domain_id) + ) self.assert_calls() def test_delete_project_v3(self): project_data = self._get_project_data(v3=False) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(append=[project_data.project_id]), - status_code=200, - json=project_data.json_response), - dict(method='DELETE', - uri=self.get_mock_url(append=[project_data.project_id]), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[project_data.project_id]), + status_code=200, + json=project_data.json_response, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[project_data.project_id]), + status_code=204, + ), + ] + ) self.cloud.delete_project(project_data.project_id) self.assert_calls() def test_update_project_not_found(self): project_data = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(append=[project_data.project_id]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - qs_elements=['name=' + project_data.project_id]), - status_code=200, - json={'projects': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(append=[project_data.project_id]), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + qs_elements=['name=' + project_data.project_id] + ), + status_code=200, + json={'projects': []}, + ), + ] + ) # NOTE(notmorgan): This test (and shade) does not represent a case # where the project is in the project list but a 404 is raised when # the PATCH is issued. This is a bug in shade and should be fixed, # shade will raise an attribute error instead of the proper # project not found exception. with testtools.ExpectedException( - openstack.cloud.OpenStackCloudException, - "Project %s not found." % project_data.project_id + openstack.cloud.OpenStackCloudException, + "Project %s not found." % project_data.project_id, ): self.cloud.update_project(project_data.project_id) self.assert_calls() def test_update_project_v3(self): project_data = self._get_project_data( - description=self.getUniqueString('projectDesc')) + description=self.getUniqueString('projectDesc') + ) reference_req = project_data.json_request.copy() # Remove elements not actually sent in the update reference_req['project'].pop('domain_id') reference_req['project'].pop('name') reference_req['project'].pop('enabled') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - append=[project_data.project_id], - qs_elements=['domain_id=' + project_data.domain_id]), - status_code=200, - json={'projects': [project_data.json_response['project']]}), - dict(method='PATCH', - uri=self.get_mock_url(append=[project_data.project_id]), - status_code=200, json=project_data.json_response, - validate=dict(json=reference_req)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + append=[project_data.project_id], + qs_elements=['domain_id=' + project_data.domain_id], + ), + status_code=200, + json={'projects': [project_data.json_response['project']]}, + ), + dict( + method='PATCH', + uri=self.get_mock_url(append=[project_data.project_id]), + status_code=200, + json=project_data.json_response, + validate=dict(json=reference_req), + ), + ] + ) project = self.cloud.update_project( project_data.project_id, description=project_data.description, - domain_id=project_data.domain_id) + domain_id=project_data.domain_id, + ) self.assertThat(project.id, matchers.Equals(project_data.project_id)) self.assertThat( - project.name, matchers.Equals(project_data.project_name)) + project.name, matchers.Equals(project_data.project_name) + ) self.assertThat( - project.description, matchers.Equals(project_data.description)) + project.description, matchers.Equals(project_data.description) + ) self.assert_calls() def test_list_projects_v3(self): project_data = self._get_project_data( - description=self.getUniqueString('projectDesc')) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource=('projects?domain_id=%s' % - project_data.domain_id)), - status_code=200, - json={'projects': [project_data.json_response['project']]}) - ]) + description=self.getUniqueString('projectDesc') + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource=( + 'projects?domain_id=%s' % project_data.domain_id + ) + ), + status_code=200, + json={'projects': [project_data.json_response['project']]}, + ) + ] + ) projects = self.cloud.list_projects(project_data.domain_id) self.assertThat(len(projects), matchers.Equals(1)) self.assertThat( - projects[0].id, matchers.Equals(project_data.project_id)) + projects[0].id, matchers.Equals(project_data.project_id) + ) self.assert_calls() def test_list_projects_v3_kwarg(self): project_data = self._get_project_data( - description=self.getUniqueString('projectDesc')) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource=('projects?domain_id=%s' % - project_data.domain_id)), - status_code=200, - json={'projects': [project_data.json_response['project']]}) - ]) - projects = self.cloud.list_projects( - domain_id=project_data.domain_id) + description=self.getUniqueString('projectDesc') + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource=( + 'projects?domain_id=%s' % project_data.domain_id + ) + ), + status_code=200, + json={'projects': [project_data.json_response['project']]}, + ) + ] + ) + projects = self.cloud.list_projects(domain_id=project_data.domain_id) self.assertThat(len(projects), matchers.Equals(1)) self.assertThat( - projects[0].id, matchers.Equals(project_data.project_id)) + projects[0].id, matchers.Equals(project_data.project_id) + ) self.assert_calls() def test_list_projects_search_compat(self): project_data = self._get_project_data( - description=self.getUniqueString('projectDesc')) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'projects': [project_data.json_response['project']]}) - ]) + description=self.getUniqueString('projectDesc') + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'projects': [project_data.json_response['project']]}, + ) + ] + ) projects = self.cloud.search_projects(project_data.project_id) self.assertThat(len(projects), matchers.Equals(1)) self.assertThat( - projects[0].id, matchers.Equals(project_data.project_id)) + projects[0].id, matchers.Equals(project_data.project_id) + ) self.assert_calls() def test_list_projects_search_compat_v3(self): project_data = self._get_project_data( - description=self.getUniqueString('projectDesc')) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource=('projects?domain_id=%s' % - project_data.domain_id)), - status_code=200, - json={'projects': [project_data.json_response['project']]}) - ]) - projects = self.cloud.search_projects( - domain_id=project_data.domain_id) + description=self.getUniqueString('projectDesc') + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource=( + 'projects?domain_id=%s' % project_data.domain_id + ) + ), + status_code=200, + json={'projects': [project_data.json_response['project']]}, + ) + ] + ) + projects = self.cloud.search_projects(domain_id=project_data.domain_id) self.assertThat(len(projects), matchers.Equals(1)) self.assertThat( - projects[0].id, matchers.Equals(project_data.project_id)) + projects[0].id, matchers.Equals(project_data.project_id) + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_qos_bandwidth_limit_rule.py b/openstack/tests/unit/cloud/test_qos_bandwidth_limit_rule.py index 8febafa3c..468f4abf7 100644 --- a/openstack/tests/unit/cloud/test_qos_bandwidth_limit_rule.py +++ b/openstack/tests/unit/cloud/test_qos_bandwidth_limit_rule.py @@ -38,14 +38,14 @@ class TestQosBandwidthLimitRule(base.TestCase): 'project_id': project_id, 'tenant_id': project_id, 'shared': False, - 'is_default': False + 'is_default': False, } mock_rule = { 'id': rule_id, 'max_kbps': rule_max_kbps, 'max_burst_kbps': rule_max_burst, - 'direction': 'egress' + 'direction': 'egress', } qos_extension = { @@ -53,7 +53,7 @@ class TestQosBandwidthLimitRule(base.TestCase): "name": "Quality of Service", "links": [], "alias": "qos", - "description": "The Quality of Service extension." + "description": "The Quality of Service extension.", } qos_bw_limit_direction_extension = { @@ -61,316 +61,561 @@ class TestQosBandwidthLimitRule(base.TestCase): "name": "Direction for QoS bandwidth limit rule", "links": [], "alias": "qos-bw-limit-direction", - "description": ("Allow to configure QoS bandwidth limit rule with " - "specific direction: ingress or egress") + "description": ( + "Allow to configure QoS bandwidth limit rule with " + "specific direction: ingress or egress" + ), } - enabled_neutron_extensions = [qos_extension, - qos_bw_limit_direction_extension] + enabled_neutron_extensions = [ + qos_extension, + qos_bw_limit_direction_extension, + ] def _compare_rules(self, exp, real): self.assertDictEqual( qos_bandwidth_limit_rule.QoSBandwidthLimitRule(**exp).to_dict( - computed=False), - real.to_dict(computed=False)) + computed=False + ), + real.to_dict(computed=False), + ) def test_get_qos_bandwidth_limit_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', self.rule_id]), - json={'bandwidth_limit_rule': self.mock_rule}) - ]) - r = self.cloud.get_qos_bandwidth_limit_rule(self.policy_name, - self.rule_id) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + json={'bandwidth_limit_rule': self.mock_rule}, + ), + ] + ) + r = self.cloud.get_qos_bandwidth_limit_rule( + self.policy_name, self.rule_id + ) self._compare_rules(self.mock_rule, r) self.assert_calls() def test_get_qos_bandwidth_limit_rule_no_qos_policy_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': []}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudResourceNotFound, self.cloud.get_qos_bandwidth_limit_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_get_qos_bandwidth_limit_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.get_qos_bandwidth_limit_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_create_qos_bandwidth_limit_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules']), - json={'bandwidth_limit_rule': self.mock_rule}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + ], + ), + json={'bandwidth_limit_rule': self.mock_rule}, + ), + ] + ) rule = self.cloud.create_qos_bandwidth_limit_rule( - self.policy_name, max_kbps=self.rule_max_kbps) + self.policy_name, max_kbps=self.rule_max_kbps + ) self._compare_rules(self.mock_rule, rule) self.assert_calls() def test_create_qos_bandwidth_limit_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.create_qos_bandwidth_limit_rule, self.policy_name, - max_kbps=100) + self.cloud.create_qos_bandwidth_limit_rule, + self.policy_name, + max_kbps=100, + ) self.assert_calls() def test_create_qos_bandwidth_limit_rule_no_qos_direction_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules']), - json={'bandwidth_limit_rule': self.mock_rule}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + ], + ), + json={'bandwidth_limit_rule': self.mock_rule}, + ), + ] + ) rule = self.cloud.create_qos_bandwidth_limit_rule( - self.policy_name, max_kbps=self.rule_max_kbps, direction="ingress") + self.policy_name, max_kbps=self.rule_max_kbps, direction="ingress" + ) self._compare_rules(self.mock_rule, rule) self.assert_calls() def test_update_qos_bandwidth_limit_rule(self): expected_rule = copy.copy(self.mock_rule) expected_rule['max_kbps'] = self.rule_max_kbps + 100 - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json=self.mock_policy), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', self.rule_id]), - json={'bandwidth_limit_rule': self.mock_rule}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', self.rule_id]), - json={'bandwidth_limit_rule': expected_rule}, - validate=dict( - json={'bandwidth_limit_rule': { - 'max_kbps': self.rule_max_kbps + 100}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json=self.mock_policy, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + json={'bandwidth_limit_rule': self.mock_rule}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + json={'bandwidth_limit_rule': expected_rule}, + validate=dict( + json={ + 'bandwidth_limit_rule': { + 'max_kbps': self.rule_max_kbps + 100 + } + } + ), + ), + ] + ) rule = self.cloud.update_qos_bandwidth_limit_rule( - self.policy_id, self.rule_id, max_kbps=self.rule_max_kbps + 100) + self.policy_id, self.rule_id, max_kbps=self.rule_max_kbps + 100 + ) self._compare_rules(expected_rule, rule) self.assert_calls() def test_update_qos_bandwidth_limit_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.update_qos_bandwidth_limit_rule, - self.policy_id, self.rule_id, max_kbps=2000) + self.policy_id, + self.rule_id, + max_kbps=2000, + ) self.assert_calls() def test_update_qos_bandwidth_limit_rule_no_qos_direction_extension(self): expected_rule = copy.copy(self.mock_rule) expected_rule['direction'] = self.rule_max_kbps + 100 - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json=self.mock_policy), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', self.rule_id]), - json={'bandwidth_limit_rule': self.mock_rule}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', - self.rule_id]), - json={'bandwidth_limit_rule': expected_rule}, - validate=dict( - json={'bandwidth_limit_rule': { - 'max_kbps': self.rule_max_kbps + 100}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json=self.mock_policy, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + json={'bandwidth_limit_rule': self.mock_rule}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + json={'bandwidth_limit_rule': expected_rule}, + validate=dict( + json={ + 'bandwidth_limit_rule': { + 'max_kbps': self.rule_max_kbps + 100 + } + } + ), + ), + ] + ) rule = self.cloud.update_qos_bandwidth_limit_rule( - self.policy_id, self.rule_id, max_kbps=self.rule_max_kbps + 100, - direction="ingress") + self.policy_id, + self.rule_id, + max_kbps=self.rule_max_kbps + 100, + direction="ingress", + ) # Even if there was attempt to change direction to 'ingress' it should # be not changed in returned rule self._compare_rules(expected_rule, rule) self.assert_calls() def test_delete_qos_bandwidth_limit_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', self.rule_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + json={}, + ), + ] + ) self.assertTrue( self.cloud.delete_qos_bandwidth_limit_rule( - self.policy_name, self.rule_id)) + self.policy_name, self.rule_id + ) + ) self.assert_calls() def test_delete_qos_bandwidth_limit_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.delete_qos_bandwidth_limit_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_delete_qos_bandwidth_limit_rule_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'bandwidth_limit_rules', self.rule_id]), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'bandwidth_limit_rules', + self.rule_id, + ], + ), + status_code=404, + ), + ] + ) self.assertFalse( self.cloud.delete_qos_bandwidth_limit_rule( - self.policy_name, self.rule_id)) + self.policy_name, self.rule_id + ) + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_qos_dscp_marking_rule.py b/openstack/tests/unit/cloud/test_qos_dscp_marking_rule.py index 629b4cf7c..1b864b1a6 100644 --- a/openstack/tests/unit/cloud/test_qos_dscp_marking_rule.py +++ b/openstack/tests/unit/cloud/test_qos_dscp_marking_rule.py @@ -37,7 +37,7 @@ class TestQosDscpMarkingRule(base.TestCase): 'project_id': project_id, 'tenant_id': project_id, 'shared': False, - 'is_default': False + 'is_default': False, } mock_rule = { @@ -50,7 +50,7 @@ class TestQosDscpMarkingRule(base.TestCase): "name": "Quality of Service", "links": [], "alias": "qos", - "description": "The Quality of Service extension." + "description": "The Quality of Service extension.", } enabled_neutron_extensions = [qos_extension] @@ -58,233 +58,409 @@ class TestQosDscpMarkingRule(base.TestCase): def _compare_rules(self, exp, real): self.assertDictEqual( qos_dscp_marking_rule.QoSDSCPMarkingRule(**exp).to_dict( - computed=False), - real.to_dict(computed=False)) + computed=False + ), + real.to_dict(computed=False), + ) def test_get_qos_dscp_marking_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'dscp_marking_rules', self.rule_id]), - json={'dscp_marking_rule': self.mock_rule}) - ]) - r = self.cloud.get_qos_dscp_marking_rule(self.policy_name, - self.rule_id) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'dscp_marking_rules', + self.rule_id, + ], + ), + json={'dscp_marking_rule': self.mock_rule}, + ), + ] + ) + r = self.cloud.get_qos_dscp_marking_rule( + self.policy_name, self.rule_id + ) self._compare_rules(self.mock_rule, r) self.assert_calls() def test_get_qos_dscp_marking_rule_no_qos_policy_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': []}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudResourceNotFound, self.cloud.get_qos_dscp_marking_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_get_qos_dscp_marking_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.get_qos_dscp_marking_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_create_qos_dscp_marking_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'dscp_marking_rules']), - json={'dscp_marking_rule': self.mock_rule}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'dscp_marking_rules', + ], + ), + json={'dscp_marking_rule': self.mock_rule}, + ), + ] + ) rule = self.cloud.create_qos_dscp_marking_rule( - self.policy_name, dscp_mark=self.rule_dscp_mark) + self.policy_name, dscp_mark=self.rule_dscp_mark + ) self._compare_rules(self.mock_rule, rule) self.assert_calls() def test_create_qos_dscp_marking_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.create_qos_dscp_marking_rule, self.policy_name, - dscp_mark=16) + self.cloud.create_qos_dscp_marking_rule, + self.policy_name, + dscp_mark=16, + ) self.assert_calls() def test_update_qos_dscp_marking_rule(self): new_dscp_mark_value = 16 expected_rule = copy.copy(self.mock_rule) expected_rule['dscp_mark'] = new_dscp_mark_value - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json=self.mock_policy), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'dscp_marking_rules', self.rule_id]), - json={'dscp_marking_rule': self.mock_rule}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'dscp_marking_rules', self.rule_id]), - json={'dscp_marking_rule': expected_rule}, - validate=dict( - json={'dscp_marking_rule': { - 'dscp_mark': new_dscp_mark_value}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json=self.mock_policy, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'dscp_marking_rules', + self.rule_id, + ], + ), + json={'dscp_marking_rule': self.mock_rule}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'dscp_marking_rules', + self.rule_id, + ], + ), + json={'dscp_marking_rule': expected_rule}, + validate=dict( + json={ + 'dscp_marking_rule': { + 'dscp_mark': new_dscp_mark_value + } + } + ), + ), + ] + ) rule = self.cloud.update_qos_dscp_marking_rule( - self.policy_id, self.rule_id, dscp_mark=new_dscp_mark_value) + self.policy_id, self.rule_id, dscp_mark=new_dscp_mark_value + ) self._compare_rules(expected_rule, rule) self.assert_calls() def test_update_qos_dscp_marking_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.update_qos_dscp_marking_rule, - self.policy_id, self.rule_id, dscp_mark=8) + self.policy_id, + self.rule_id, + dscp_mark=8, + ) self.assert_calls() def test_delete_qos_dscp_marking_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'dscp_marking_rules', - self.rule_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'dscp_marking_rules', + self.rule_id, + ], + ), + json={}, + ), + ] + ) self.assertTrue( self.cloud.delete_qos_dscp_marking_rule( - self.policy_name, self.rule_id)) + self.policy_name, self.rule_id + ) + ) self.assert_calls() def test_delete_qos_dscp_marking_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.delete_qos_dscp_marking_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_delete_qos_dscp_marking_rule_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'dscp_marking_rules', self.rule_id]), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'dscp_marking_rules', + self.rule_id, + ], + ), + status_code=404, + ), + ] + ) self.assertFalse( self.cloud.delete_qos_dscp_marking_rule( - self.policy_name, self.rule_id)) + self.policy_name, self.rule_id + ) + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_qos_minimum_bandwidth_rule.py b/openstack/tests/unit/cloud/test_qos_minimum_bandwidth_rule.py index 6ee89a8ca..411d6d18f 100644 --- a/openstack/tests/unit/cloud/test_qos_minimum_bandwidth_rule.py +++ b/openstack/tests/unit/cloud/test_qos_minimum_bandwidth_rule.py @@ -37,13 +37,13 @@ class TestQosMinimumBandwidthRule(base.TestCase): 'project_id': project_id, 'tenant_id': project_id, 'shared': False, - 'is_default': False + 'is_default': False, } mock_rule = { 'id': rule_id, 'min_kbps': rule_min_kbps, - 'direction': 'egress' + 'direction': 'egress', } qos_extension = { @@ -51,240 +51,416 @@ class TestQosMinimumBandwidthRule(base.TestCase): "name": "Quality of Service", "links": [], "alias": "qos", - "description": "The Quality of Service extension." + "description": "The Quality of Service extension.", } enabled_neutron_extensions = [qos_extension] def _compare_rules(self, exp, real): self.assertDictEqual( - qos_minimum_bandwidth_rule.QoSMinimumBandwidthRule( - **exp).to_dict(computed=False), - real.to_dict(computed=False)) + qos_minimum_bandwidth_rule.QoSMinimumBandwidthRule(**exp).to_dict( + computed=False + ), + real.to_dict(computed=False), + ) def test_get_qos_minimum_bandwidth_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'minimum_bandwidth_rules', self.rule_id]), - json={'minimum_bandwidth_rule': self.mock_rule}) - ]) - r = self.cloud.get_qos_minimum_bandwidth_rule(self.policy_name, - self.rule_id) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'minimum_bandwidth_rules', + self.rule_id, + ], + ), + json={'minimum_bandwidth_rule': self.mock_rule}, + ), + ] + ) + r = self.cloud.get_qos_minimum_bandwidth_rule( + self.policy_name, self.rule_id + ) self._compare_rules(self.mock_rule, r) self.assert_calls() def test_get_qos_minimum_bandwidth_rule_no_qos_policy_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': []}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudResourceNotFound, self.cloud.get_qos_minimum_bandwidth_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_get_qos_minimum_bandwidth_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.get_qos_minimum_bandwidth_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_create_qos_minimum_bandwidth_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'minimum_bandwidth_rules']), - json={'minimum_bandwidth_rule': self.mock_rule}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'minimum_bandwidth_rules', + ], + ), + json={'minimum_bandwidth_rule': self.mock_rule}, + ), + ] + ) rule = self.cloud.create_qos_minimum_bandwidth_rule( - self.policy_name, min_kbps=self.rule_min_kbps) + self.policy_name, min_kbps=self.rule_min_kbps + ) self._compare_rules(self.mock_rule, rule) self.assert_calls() def test_create_qos_minimum_bandwidth_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.create_qos_minimum_bandwidth_rule, self.policy_name, - min_kbps=100) + self.cloud.create_qos_minimum_bandwidth_rule, + self.policy_name, + min_kbps=100, + ) self.assert_calls() def test_update_qos_minimum_bandwidth_rule(self): expected_rule = copy.copy(self.mock_rule) expected_rule['min_kbps'] = self.rule_min_kbps + 100 - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json=self.mock_policy), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'minimum_bandwidth_rules', self.rule_id]), - json={'minimum_bandwidth_rule': self.mock_rule}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'minimum_bandwidth_rules', self.rule_id]), - json={'minimum_bandwidth_rule': expected_rule}, - validate=dict( - json={'minimum_bandwidth_rule': { - 'min_kbps': self.rule_min_kbps + 100}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json=self.mock_policy, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'minimum_bandwidth_rules', + self.rule_id, + ], + ), + json={'minimum_bandwidth_rule': self.mock_rule}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'minimum_bandwidth_rules', + self.rule_id, + ], + ), + json={'minimum_bandwidth_rule': expected_rule}, + validate=dict( + json={ + 'minimum_bandwidth_rule': { + 'min_kbps': self.rule_min_kbps + 100 + } + } + ), + ), + ] + ) rule = self.cloud.update_qos_minimum_bandwidth_rule( - self.policy_id, self.rule_id, min_kbps=self.rule_min_kbps + 100) + self.policy_id, self.rule_id, min_kbps=self.rule_min_kbps + 100 + ) self._compare_rules(expected_rule, rule) self.assert_calls() def test_update_qos_minimum_bandwidth_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.update_qos_minimum_bandwidth_rule, - self.policy_id, self.rule_id, min_kbps=2000) + self.policy_id, + self.rule_id, + min_kbps=2000, + ) self.assert_calls() def test_delete_qos_minimum_bandwidth_rule(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'minimum_bandwidth_rules', self.rule_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'minimum_bandwidth_rules', + self.rule_id, + ], + ), + json={}, + ), + ] + ) self.assertTrue( self.cloud.delete_qos_minimum_bandwidth_rule( - self.policy_name, self.rule_id)) + self.policy_name, self.rule_id + ) + ) self.assert_calls() def test_delete_qos_minimum_bandwidth_rule_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.delete_qos_minimum_bandwidth_rule, - self.policy_name, self.rule_id) + self.policy_name, + self.rule_id, + ) self.assert_calls() def test_delete_qos_minimum_bandwidth_rule_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id, - 'minimum_bandwidth_rules', self.rule_id]), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'policies', + self.policy_id, + 'minimum_bandwidth_rules', + self.rule_id, + ], + ), + status_code=404, + ), + ] + ) self.assertFalse( self.cloud.delete_qos_minimum_bandwidth_rule( - self.policy_name, self.rule_id)) + self.policy_name, self.rule_id + ) + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_qos_policy.py b/openstack/tests/unit/cloud/test_qos_policy.py index 70da59daa..7bfd77334 100644 --- a/openstack/tests/unit/cloud/test_qos_policy.py +++ b/openstack/tests/unit/cloud/test_qos_policy.py @@ -43,7 +43,7 @@ class TestQosPolicy(base.TestCase): "name": "Quality of Service", "links": [], "alias": "qos", - "description": "The Quality of Service extension." + "description": "The Quality of Service extension.", } qos_default_extension = { @@ -51,7 +51,7 @@ class TestQosPolicy(base.TestCase): "name": "QoS default policy", "links": [], "alias": "qos-default", - "description": "Expose the QoS default policy per project" + "description": "Expose the QoS default policy per project", } enabled_neutron_extensions = [qos_extension, qos_default_extension] @@ -59,269 +59,417 @@ class TestQosPolicy(base.TestCase): def _compare_policies(self, exp, real): self.assertDictEqual( _policy.QoSPolicy(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_get_qos_policy(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', - 'policies', self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + ] + ) r = self.cloud.get_qos_policy(self.policy_name) self.assertIsNotNone(r) self._compare_policies(self.mock_policy, r) self.assert_calls() def test_get_qos_policy_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.get_qos_policy, self.policy_name) + self.cloud.get_qos_policy, + self.policy_name, + ) self.assert_calls() def test_create_qos_policy(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies']), - json={'policy': self.mock_policy}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'qos', 'policies'] + ), + json={'policy': self.mock_policy}, + ), + ] + ) policy = self.cloud.create_qos_policy( - name=self.policy_name, project_id=self.project_id) + name=self.policy_name, project_id=self.project_id + ) self._compare_policies(self.mock_policy, policy) self.assert_calls() def test_create_qos_policy_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.create_qos_policy, name=self.policy_name) + self.cloud.create_qos_policy, + name=self.policy_name, + ) self.assert_calls() def test_create_qos_policy_no_qos_default_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies']), - json={'policy': self.mock_policy}, - validate=dict( - json={'policy': { - 'name': self.policy_name, - 'project_id': self.project_id}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'qos', 'policies'] + ), + json={'policy': self.mock_policy}, + validate=dict( + json={ + 'policy': { + 'name': self.policy_name, + 'project_id': self.project_id, + } + } + ), + ), + ] + ) policy = self.cloud.create_qos_policy( - name=self.policy_name, project_id=self.project_id, default=True) + name=self.policy_name, project_id=self.project_id, default=True + ) self._compare_policies(self.mock_policy, policy) self.assert_calls() def test_delete_qos_policy(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', - self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [self.mock_policy]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [self.mock_policy]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_qos_policy(self.policy_name)) self.assert_calls() def test_delete_qos_policy_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.delete_qos_policy, self.policy_name) + self.cloud.delete_qos_policy, + self.policy_name, + ) self.assert_calls() def test_delete_qos_policy_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', 'goofy']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=goofy']), - json={'policies': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', 'goofy'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=goofy'], + ), + json={'policies': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_qos_policy('goofy')) self.assert_calls() def test_delete_qos_policy_multiple_found(self): policy1 = dict(id='123', name=self.policy_name) policy2 = dict(id='456', name=self.policy_name) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', - self.policy_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies'], - qs_elements=['name=%s' % self.policy_name]), - json={'policies': [policy1, policy2]}), - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.delete_qos_policy, - self.policy_name) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies'], + qs_elements=['name=%s' % self.policy_name], + ), + json={'policies': [policy1, policy2]}, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.delete_qos_policy, + self.policy_name, + ) self.assert_calls() def test_delete_qos_policy_using_id(self): policy1 = self.mock_policy - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', policy1['id']]), - json=policy1), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', policy1['id']], + ), + json=policy1, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_qos_policy(policy1['id'])) self.assert_calls() def test_update_qos_policy(self): expected_policy = copy.copy(self.mock_policy) expected_policy['name'] = 'goofy' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json=self.mock_policy), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json={'policy': expected_policy}, - validate=dict( - json={'policy': {'name': 'goofy'}})) - ]) - policy = self.cloud.update_qos_policy( - self.policy_id, name='goofy') + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json=self.mock_policy, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json={'policy': expected_policy}, + validate=dict(json={'policy': {'name': 'goofy'}}), + ), + ] + ) + policy = self.cloud.update_qos_policy(self.policy_id, name='goofy') self._compare_policies(expected_policy, policy) self.assert_calls() def test_update_qos_policy_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.update_qos_policy, self.policy_id, name="goofy") + self.cloud.update_qos_policy, + self.policy_id, + name="goofy", + ) self.assert_calls() def test_update_qos_policy_no_qos_default_extension(self): expected_policy = copy.copy(self.mock_policy) expected_policy['name'] = 'goofy' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json=self.mock_policy), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'policies', self.policy_id]), - json={'policy': expected_policy}, - validate=dict( - json={'policy': {'name': "goofy"}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json=self.mock_policy, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'policies', self.policy_id], + ), + json={'policy': expected_policy}, + validate=dict(json={'policy': {'name': "goofy"}}), + ), + ] + ) policy = self.cloud.update_qos_policy( - self.policy_id, name='goofy', default=True) + self.policy_id, name='goofy', default=True + ) self._compare_policies(expected_policy, policy) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_qos_rule_type.py b/openstack/tests/unit/cloud/test_qos_rule_type.py index 91244f8d7..878e162f1 100644 --- a/openstack/tests/unit/cloud/test_qos_rule_type.py +++ b/openstack/tests/unit/cloud/test_qos_rule_type.py @@ -27,131 +27,192 @@ class TestQosRuleType(base.TestCase): "name": "Quality of Service", "links": [], "alias": "qos", - "description": "The Quality of Service extension." + "description": "The Quality of Service extension.", } qos_rule_type_details_extension = { "updated": "2017-06-22T10:00:00-00:00", "name": "Details of QoS rule types", "links": [], "alias": "qos-rule-type-details", - "description": ("Expose details about QoS rule types supported by " - "loaded backend drivers") + "description": ( + "Expose details about QoS rule types supported by " + "loaded backend drivers" + ), } - mock_rule_type_bandwidth_limit = { - 'type': 'bandwidth_limit' - } - mock_rule_type_dscp_marking = { - 'type': 'dscp_marking' - } + mock_rule_type_bandwidth_limit = {'type': 'bandwidth_limit'} + mock_rule_type_dscp_marking = {'type': 'dscp_marking'} mock_rule_types = [ - mock_rule_type_bandwidth_limit, mock_rule_type_dscp_marking] + mock_rule_type_bandwidth_limit, + mock_rule_type_dscp_marking, + ] mock_rule_type_details = { - 'drivers': [{ - 'name': 'linuxbridge', - 'supported_parameters': [{ - 'parameter_values': {'start': 0, 'end': 2147483647}, - 'parameter_type': 'range', - 'parameter_name': u'max_kbps' - }, { - 'parameter_values': ['ingress', 'egress'], - 'parameter_type': 'choices', - 'parameter_name': u'direction' - }, { - 'parameter_values': {'start': 0, 'end': 2147483647}, - 'parameter_type': 'range', - 'parameter_name': 'max_burst_kbps' - }] - }], - 'type': rule_type_name + 'drivers': [ + { + 'name': 'linuxbridge', + 'supported_parameters': [ + { + 'parameter_values': {'start': 0, 'end': 2147483647}, + 'parameter_type': 'range', + 'parameter_name': u'max_kbps', + }, + { + 'parameter_values': ['ingress', 'egress'], + 'parameter_type': 'choices', + 'parameter_name': u'direction', + }, + { + 'parameter_values': {'start': 0, 'end': 2147483647}, + 'parameter_type': 'range', + 'parameter_name': 'max_burst_kbps', + }, + ], + } + ], + 'type': rule_type_name, } def _compare_rule_types(self, exp, real): self.assertDictEqual( qos_rule_type.QoSRuleType(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_list_qos_rule_types(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'rule-types']), - json={'rule_types': self.mock_rule_types}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'qos', 'rule-types'], + ), + json={'rule_types': self.mock_rule_types}, + ), + ] + ) rule_types = self.cloud.list_qos_rule_types() for a, b in zip(self.mock_rule_types, rule_types): self._compare_rule_types(a, b) self.assert_calls() def test_list_qos_rule_types_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.list_qos_rule_types) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) + self.assertRaises( + exc.OpenStackCloudException, self.cloud.list_qos_rule_types + ) self.assert_calls() def test_get_qos_rule_type_details(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [ - self.qos_extension, - self.qos_rule_type_details_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [ - self.qos_extension, - self.qos_rule_type_details_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'qos', 'rule-types', - self.rule_type_name]), - json={'rule_type': self.mock_rule_type_details}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={ + 'extensions': [ + self.qos_extension, + self.qos_rule_type_details_extension, + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={ + 'extensions': [ + self.qos_extension, + self.qos_rule_type_details_extension, + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'qos', + 'rule-types', + self.rule_type_name, + ], + ), + json={'rule_type': self.mock_rule_type_details}, + ), + ] + ) self._compare_rule_types( self.mock_rule_type_details, - self.cloud.get_qos_rule_type_details(self.rule_type_name) + self.cloud.get_qos_rule_type_details(self.rule_type_name), ) self.assert_calls() def test_get_qos_rule_type_details_no_qos_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': []}, + ) + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.get_qos_rule_type_details, self.rule_type_name) + self.cloud.get_qos_rule_type_details, + self.rule_type_name, + ) self.assert_calls() def test_get_qos_rule_type_details_no_qos_details_extension(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': [self.qos_extension]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': [self.qos_extension]}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.get_qos_rule_type_details, self.rule_type_name) + self.cloud.get_qos_rule_type_details, + self.rule_type_name, + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_quotas.py b/openstack/tests/unit/cloud/test_quotas.py index 00da3985d..3ea399919 100644 --- a/openstack/tests/unit/cloud/test_quotas.py +++ b/openstack/tests/unit/cloud/test_quotas.py @@ -28,37 +28,45 @@ fake_quota_set = { "security_group_rules": 20, "security_groups": 45, "server_groups": 10, - "server_group_members": 10 + "server_group_members": 10, } class TestQuotas(base.TestCase): def setUp(self, cloud_config_fixture='clouds.yaml'): super(TestQuotas, self).setUp( - cloud_config_fixture=cloud_config_fixture) + cloud_config_fixture=cloud_config_fixture + ) def test_update_quotas(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_nova_discovery_mock_dict(), - dict(method='PUT', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-quota-sets', project.project_id]), - json={'quota_set': fake_quota_set}, - validate=dict( - json={ - 'quota_set': { - 'cores': 1, - 'force': True - }})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='PUT', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-quota-sets', project.project_id], + ), + json={'quota_set': fake_quota_set}, + validate=dict( + json={'quota_set': {'cores': 1, 'force': True}} + ), + ), + ] + ) self.cloud.set_compute_quotas(project.project_id, cores=1) @@ -67,41 +75,64 @@ class TestQuotas(base.TestCase): def test_update_quotas_bad_request(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_nova_discovery_mock_dict(), - dict(method='PUT', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-quota-sets', project.project_id]), - status_code=400), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='PUT', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-quota-sets', project.project_id], + ), + status_code=400, + ), + ] + ) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.set_compute_quotas, project.project_id) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.set_compute_quotas, + project.project_id, + ) self.assert_calls() def test_get_quotas(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-quota-sets', project.project_id], - qs_elements=['usage=False']), - json={'quota_set': fake_quota_set}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-quota-sets', project.project_id], + qs_elements=['usage=False'], + ), + json={'quota_set': fake_quota_set}, + ), + ] + ) self.cloud.get_compute_quotas(project.project_id) @@ -110,18 +141,28 @@ class TestQuotas(base.TestCase): def test_delete_quotas(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_nova_discovery_mock_dict(), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-quota-sets', project.project_id])), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-quota-sets', project.project_id], + ), + ), + ] + ) self.cloud.delete_compute_quotas(project.project_id) @@ -130,72 +171,109 @@ class TestQuotas(base.TestCase): def test_cinder_update_quotas(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_cinder_discovery_mock_dict(), - dict(method='PUT', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['os-quota-sets', project.project_id]), - json=dict(quota_set={'volumes': 1}), - validate=dict( - json={'quota_set': { - 'volumes': 1}}))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='PUT', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['os-quota-sets', project.project_id], + ), + json=dict(quota_set={'volumes': 1}), + validate=dict(json={'quota_set': {'volumes': 1}}), + ), + ] + ) self.cloud.set_volume_quotas(project.project_id, volumes=1) self.assert_calls() def test_cinder_get_quotas(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['os-quota-sets', project.project_id], - qs_elements=['usage=False']), - json=dict(quota_set={'snapshots': 10, 'volumes': 20}))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['os-quota-sets', project.project_id], + qs_elements=['usage=False'], + ), + json=dict(quota_set={'snapshots': 10, 'volumes': 20}), + ), + ] + ) self.cloud.get_volume_quotas(project.project_id) self.assert_calls() def test_cinder_delete_quotas(self): project = self._get_project_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'identity', 'public', - append=['v3', 'projects', project.project_id]), - json={'project': project.json_response['project']}), - self.get_cinder_discovery_mock_dict(), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['os-quota-sets', project.project_id]))]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'identity', + 'public', + append=['v3', 'projects', project.project_id], + ), + json={'project': project.json_response['project']}, + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['os-quota-sets', project.project_id], + ), + ), + ] + ) self.cloud.delete_volume_quotas(project.project_id) self.assert_calls() def test_neutron_update_quotas(self): - project = self.mock_for_keystone_projects(project_count=1, - list_get=True)[0] - self.register_uris([ - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'quotas', project.project_id]), - json={}, - validate=dict( - json={'quota': {'network': 1}})) - ]) + project = self.mock_for_keystone_projects( + project_count=1, list_get=True + )[0] + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'quotas', project.project_id], + ), + json={}, + validate=dict(json={'quota': {'network': 1}}), + ) + ] + ) self.cloud.set_network_quotas(project.project_id, network=1) self.assert_calls() @@ -209,19 +287,27 @@ class TestQuotas(base.TestCase): 'security_group': 10, 'router': 10, 'rbac_policy': 10, - 'port': 500 + 'port': 500, } - project = self.mock_for_keystone_projects(project_count=1, - id_get=True)[0] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'quotas', project.project_id]), - json={'quota': quota}) - ]) + project = self.mock_for_keystone_projects( + project_count=1, id_get=True + )[0] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'quotas', project.project_id], + ), + json={'quota': quota}, + ) + ] + ) received_quota = self.cloud.get_network_quotas( - project.project_id).to_dict(computed=False) + project.project_id + ).to_dict(computed=False) expected_quota = _quota.Quota(**quota).to_dict(computed=False) received_quota.pop('id') received_quota.pop('name') @@ -234,69 +320,62 @@ class TestQuotas(base.TestCase): def test_neutron_get_quotas_details(self): quota_details = { - 'subnet': { - 'limit': 100, - 'used': 7, - 'reserved': 0}, - 'network': { - 'limit': 100, - 'used': 6, - 'reserved': 0}, - 'floatingip': { - 'limit': 50, - 'used': 0, - 'reserved': 0}, - 'subnetpool': { - 'limit': -1, - 'used': 2, - 'reserved': 0}, - 'security_group_rule': { - 'limit': 100, - 'used': 4, - 'reserved': 0}, - 'security_group': { - 'limit': 10, - 'used': 1, - 'reserved': 0}, - 'router': { - 'limit': 10, - 'used': 2, - 'reserved': 0}, - 'rbac_policy': { - 'limit': 10, - 'used': 2, - 'reserved': 0}, - 'port': { - 'limit': 500, - 'used': 7, - 'reserved': 0} + 'subnet': {'limit': 100, 'used': 7, 'reserved': 0}, + 'network': {'limit': 100, 'used': 6, 'reserved': 0}, + 'floatingip': {'limit': 50, 'used': 0, 'reserved': 0}, + 'subnetpool': {'limit': -1, 'used': 2, 'reserved': 0}, + 'security_group_rule': {'limit': 100, 'used': 4, 'reserved': 0}, + 'security_group': {'limit': 10, 'used': 1, 'reserved': 0}, + 'router': {'limit': 10, 'used': 2, 'reserved': 0}, + 'rbac_policy': {'limit': 10, 'used': 2, 'reserved': 0}, + 'port': {'limit': 500, 'used': 7, 'reserved': 0}, } - project = self.mock_for_keystone_projects(project_count=1, - id_get=True)[0] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'quotas', - project.project_id, 'details']), - json={'quota': quota_details}) - ]) + project = self.mock_for_keystone_projects( + project_count=1, id_get=True + )[0] + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'quotas', + project.project_id, + 'details', + ], + ), + json={'quota': quota_details}, + ) + ] + ) received_quota_details = self.cloud.get_network_quotas( - project.project_id, details=True) + project.project_id, details=True + ) self.assertDictEqual( _quota.QuotaDetails(**quota_details).to_dict(computed=False), - received_quota_details.to_dict(computed=False)) + received_quota_details.to_dict(computed=False), + ) self.assert_calls() def test_neutron_delete_quotas(self): - project = self.mock_for_keystone_projects(project_count=1, - list_get=True)[0] - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'quotas', project.project_id]), - json={}) - ]) + project = self.mock_for_keystone_projects( + project_count=1, list_get=True + )[0] + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'quotas', project.project_id], + ), + json={}, + ) + ] + ) self.cloud.delete_network_quotas(project.project_id) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_rebuild_server.py b/openstack/tests/unit/cloud/test_rebuild_server.py index 460d79aa9..cba4101db 100644 --- a/openstack/tests/unit/cloud/test_rebuild_server.py +++ b/openstack/tests/unit/cloud/test_rebuild_server.py @@ -27,41 +27,50 @@ from openstack.tests.unit import base class TestRebuildServer(base.TestCase): - def setUp(self): super(TestRebuildServer, self).setUp() self.server_id = str(uuid.uuid4()) self.server_name = self.getUniqueString('name') self.fake_server = fakes.make_fake_server( - self.server_id, self.server_name) + self.server_id, self.server_name + ) self.rebuild_server = fakes.make_fake_server( - self.server_id, self.server_name, 'REBUILD') + self.server_id, self.server_name, 'REBUILD' + ) self.error_server = fakes.make_fake_server( - self.server_id, self.server_name, 'ERROR') + self.server_id, self.server_name, 'ERROR' + ) def test_rebuild_server_rebuild_exception(self): """ Test that an exception in the rebuild raises an exception in rebuild_server. """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - status_code=400, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a', - 'adminPass': 'b'}})), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + status_code=400, + validate=dict( + json={'rebuild': {'imageRef': 'a', 'adminPass': 'b'}} + ), + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, self.cloud.rebuild_server, - self.fake_server['id'], "a", "b") + self.fake_server['id'], + "a", + "b", + ) self.assert_calls() @@ -70,25 +79,35 @@ class TestRebuildServer(base.TestCase): Test that a server error while waiting for the server to rebuild raises an exception in rebuild_server. """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - json={'server': self.rebuild_server}, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': self.error_server}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + json={'server': self.rebuild_server}, + validate=dict(json={'rebuild': {'imageRef': 'a'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': self.error_server}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudException, - self.cloud.rebuild_server, self.fake_server['id'], "a", wait=True) + self.cloud.rebuild_server, + self.fake_server['id'], + "a", + wait=True, + ) self.assert_calls() @@ -97,26 +116,36 @@ class TestRebuildServer(base.TestCase): Test that a timeout while waiting for the server to rebuild raises an exception in rebuild_server. """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - json={'server': self.rebuild_server}, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': self.rebuild_server}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + json={'server': self.rebuild_server}, + validate=dict(json={'rebuild': {'imageRef': 'a'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': self.rebuild_server}, + ), + ] + ) self.assertRaises( exc.OpenStackCloudTimeout, self.cloud.rebuild_server, - self.fake_server['id'], "a", wait=True, timeout=0.001) + self.fake_server['id'], + "a", + wait=True, + timeout=0.001, + ) self.assert_calls(do_count=False) @@ -125,25 +154,32 @@ class TestRebuildServer(base.TestCase): Test that rebuild_server with no wait and no exception in the rebuild call returns the server instance. """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - json={'server': self.rebuild_server}, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a'}})), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + json={'server': self.rebuild_server}, + validate=dict(json={'rebuild': {'imageRef': 'a'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.assertEqual( self.rebuild_server['status'], - self.cloud.rebuild_server(self.fake_server['id'], "a")['status']) + self.cloud.rebuild_server(self.fake_server['id'], "a")['status'], + ) self.assert_calls() @@ -155,28 +191,38 @@ class TestRebuildServer(base.TestCase): rebuild_server = self.rebuild_server.copy() rebuild_server['adminPass'] = password - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - json={'server': rebuild_server}, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a', - 'adminPass': password}})), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + json={'server': rebuild_server}, + validate=dict( + json={ + 'rebuild': {'imageRef': 'a', 'adminPass': password} + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.assertEqual( password, self.cloud.rebuild_server( - self.fake_server['id'], 'a', - admin_pass=password)['adminPass']) + self.fake_server['id'], 'a', admin_pass=password + )['adminPass'], + ) self.assert_calls() @@ -188,37 +234,53 @@ class TestRebuildServer(base.TestCase): rebuild_server = self.rebuild_server.copy() rebuild_server['adminPass'] = password - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - json={'server': rebuild_server}, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a', - 'adminPass': password}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': self.rebuild_server}), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': self.fake_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + json={'server': rebuild_server}, + validate=dict( + json={ + 'rebuild': {'imageRef': 'a', 'adminPass': password} + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': self.rebuild_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': self.fake_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.assertEqual( password, self.cloud.rebuild_server( - self.fake_server['id'], 'a', - admin_pass=password, wait=True)['adminPass']) + self.fake_server['id'], 'a', admin_pass=password, wait=True + )['adminPass'], + ) self.assert_calls() @@ -227,33 +289,47 @@ class TestRebuildServer(base.TestCase): Test that rebuild_server with a wait returns the server instance when its status changes to "ACTIVE". """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_id, 'action']), - json={'server': self.rebuild_server}, - validate=dict( - json={ - 'rebuild': { - 'imageRef': 'a'}})), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': self.rebuild_server}), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': self.fake_server}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_id, 'action'], + ), + json={'server': self.rebuild_server}, + validate=dict(json={'rebuild': {'imageRef': 'a'}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': self.rebuild_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': self.fake_server}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.assertEqual( 'ACTIVE', - self.cloud.rebuild_server( - self.fake_server['id'], 'a', wait=True)['status']) + self.cloud.rebuild_server(self.fake_server['id'], 'a', wait=True)[ + 'status' + ], + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_recordset.py b/openstack/tests/unit/cloud/test_recordset.py index 734842331..b5e376030 100644 --- a/openstack/tests/unit/cloud/test_recordset.py +++ b/openstack/tests/unit/cloud/test_recordset.py @@ -25,7 +25,7 @@ recordset = { 'records': ['192.168.1.1'], 'id': '1', 'zone_id': zone['id'], - 'zone_name': zone['name'] + 'zone_name': zone['name'], } @@ -34,7 +34,6 @@ class RecordsetTestWrapper(test_zone.ZoneTestWrapper): class TestRecordset(base.TestCase): - def setUp(self): super(TestRecordset, self).setUp() self.use_designate() @@ -42,31 +41,45 @@ class TestRecordset(base.TestCase): def test_create_recordset_zoneid(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) fake_rs = RecordsetTestWrapper(self, recordset) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()), - dict(method='POST', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', zone['id'], 'recordsets']), - json=fake_rs.get_create_response_json(), - validate=dict(json={ - "records": fake_rs['records'], - "type": fake_rs['type'], - "name": fake_rs['name'], - "description": fake_rs['description'], - "ttl": fake_rs['ttl'] - })), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ), + dict( + method='POST', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', zone['id'], 'recordsets'], + ), + json=fake_rs.get_create_response_json(), + validate=dict( + json={ + "records": fake_rs['records'], + "type": fake_rs['type'], + "name": fake_rs['name'], + "description": fake_rs['description'], + "ttl": fake_rs['ttl'], + } + ), + ), + ] + ) rs = self.cloud.create_recordset( zone=fake_zone['id'], name=fake_rs['name'], recordset_type=fake_rs['type'], records=fake_rs['records'], description=fake_rs['description'], - ttl=fake_rs['ttl']) + ttl=fake_rs['ttl'], + ) fake_rs.cmp(rs) self.assert_calls() @@ -74,66 +87,102 @@ class TestRecordset(base.TestCase): def test_create_recordset_zonename(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) fake_rs = RecordsetTestWrapper(self, recordset) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=[ - 'name={name}'.format(name=fake_zone['name'])]), - json={'zones': [fake_zone.get_get_response_json()]}), - dict(method='POST', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', zone['id'], 'recordsets']), - json=fake_rs.get_create_response_json(), - validate=dict(json={ - "records": fake_rs['records'], - "type": fake_rs['type'], - "name": fake_rs['name'], - "description": fake_rs['description'], - "ttl": fake_rs['ttl'] - })), - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['name']], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=[ + 'name={name}'.format(name=fake_zone['name']) + ], + ), + json={'zones': [fake_zone.get_get_response_json()]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', zone['id'], 'recordsets'], + ), + json=fake_rs.get_create_response_json(), + validate=dict( + json={ + "records": fake_rs['records'], + "type": fake_rs['type'], + "name": fake_rs['name'], + "description": fake_rs['description'], + "ttl": fake_rs['ttl'], + } + ), + ), + ] + ) rs = self.cloud.create_recordset( zone=fake_zone['name'], name=fake_rs['name'], recordset_type=fake_rs['type'], records=fake_rs['records'], description=fake_rs['description'], - ttl=fake_rs['ttl']) + ttl=fake_rs['ttl'], + ) fake_rs.cmp(rs) self.assert_calls() def test_create_recordset_exception(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()), - dict(method='POST', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', zone['id'], 'recordsets']), - status_code=500, - validate=dict(json={ - 'name': 'www2.example.net.', - 'records': ['192.168.1.2'], - 'type': 'A'})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ), + dict( + method='POST', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', zone['id'], 'recordsets'], + ), + status_code=500, + validate=dict( + json={ + 'name': 'www2.example.net.', + 'records': ['192.168.1.2'], + 'type': 'A', + } + ), + ), + ] + ) self.assertRaises( exceptions.SDKException, self.cloud.create_recordset, - fake_zone['id'], 'www2.example.net.', 'a', ['192.168.1.2'] + fake_zone['id'], + 'www2.example.net.', + 'a', + ['192.168.1.2'], ) self.assert_calls() @@ -145,46 +194,82 @@ class TestRecordset(base.TestCase): expected_recordset = recordset.copy() expected_recordset['ttl'] = new_ttl updated_rs = RecordsetTestWrapper(self, expected_recordset) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=[ - 'name={name}'.format(name=fake_zone['name'])]), - json={'zones': [fake_zone.get_get_response_json()]}), - # try directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets', fake_rs['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets'], - qs_elements=['name={name}'.format(name=fake_rs['name'])]), - json={'recordsets': [fake_rs.get_get_response_json()]}), - # update - dict(method='PUT', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets', fake_rs['id']]), - json=updated_rs.get_get_response_json(), - validate=dict(json={'ttl': new_ttl})) - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['name']], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=[ + 'name={name}'.format(name=fake_zone['name']) + ], + ), + json={'zones': [fake_zone.get_get_response_json()]}, + ), + # try directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + fake_rs['name'], + ], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id'], 'recordsets'], + qs_elements=[ + 'name={name}'.format(name=fake_rs['name']) + ], + ), + json={'recordsets': [fake_rs.get_get_response_json()]}, + ), + # update + dict( + method='PUT', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + fake_rs['id'], + ], + ), + json=updated_rs.get_get_response_json(), + validate=dict(json={'ttl': new_ttl}), + ), + ] + ) res = self.cloud.update_recordset( - fake_zone['name'], fake_rs['name'], ttl=new_ttl) + fake_zone['name'], fake_rs['name'], ttl=new_ttl + ) updated_rs.cmp(res) self.assert_calls() @@ -192,37 +277,65 @@ class TestRecordset(base.TestCase): def test_list_recordsets(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) fake_rs = RecordsetTestWrapper(self, recordset) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()), - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], 'recordsets']), - json={'recordsets': [fake_rs.get_get_response_json()], - 'links': { - 'next': self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets'], - qs_elements=['limit=1', 'marker=asd']), - 'self': self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets?limit=1'])}, - 'metadata':{'total_count': 2}}), - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], 'recordsets'], - qs_elements=[ - 'limit=1', 'marker=asd']), - json={'recordsets': [fake_rs.get_get_response_json()]}), - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id'], 'recordsets'], + ), + json={ + 'recordsets': [fake_rs.get_get_response_json()], + 'links': { + 'next': self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + ], + qs_elements=['limit=1', 'marker=asd'], + ), + 'self': self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets?limit=1', + ], + ), + }, + 'metadata': {'total_count': 2}, + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id'], 'recordsets'], + qs_elements=['limit=1', 'marker=asd'], + ), + json={'recordsets': [fake_rs.get_get_response_json()]}, + ), + ] + ) res = self.cloud.list_recordsets(fake_zone['id']) self.assertEqual(2, len(res)) @@ -231,72 +344,128 @@ class TestRecordset(base.TestCase): def test_delete_recordset(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) fake_rs = RecordsetTestWrapper(self, recordset) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=[ - 'name={name}'.format(name=fake_zone['name'])]), - json={'zones': [fake_zone.get_get_response_json()]}), - # try directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets', fake_rs['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets'], - qs_elements=[ - 'name={name}'.format(name=fake_rs['name'])]), - json={'recordsets': [fake_rs.get_get_response_json()]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', zone['id'], - 'recordsets', fake_rs['id']]), - status_code=202) - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['name']], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=[ + 'name={name}'.format(name=fake_zone['name']) + ], + ), + json={'zones': [fake_zone.get_get_response_json()]}, + ), + # try directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + fake_rs['name'], + ], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id'], 'recordsets'], + qs_elements=[ + 'name={name}'.format(name=fake_rs['name']) + ], + ), + json={'recordsets': [fake_rs.get_get_response_json()]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + zone['id'], + 'recordsets', + fake_rs['id'], + ], + ), + status_code=202, + ), + ] + ) self.assertTrue( - self.cloud.delete_recordset(fake_zone['name'], fake_rs['name'])) + self.cloud.delete_recordset(fake_zone['name'], fake_rs['name']) + ) self.assert_calls() def test_get_recordset_by_id(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) fake_rs = RecordsetTestWrapper(self, recordset) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=[ - 'name={name}'.format(name=fake_zone['name'])]), - json={'zones': [fake_zone.get_get_response_json()]}), - # try directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets', fake_rs['id']]), - json=fake_rs.get_get_response_json()) - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['name']], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=[ + 'name={name}'.format(name=fake_zone['name']) + ], + ), + json={'zones': [fake_zone.get_get_response_json()]}, + ), + # try directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + fake_rs['id'], + ], + ), + json=fake_rs.get_get_response_json(), + ), + ] + ) res = self.cloud.get_recordset(fake_zone['name'], fake_rs['id']) fake_rs.cmp(res) self.assert_calls() @@ -304,64 +473,109 @@ class TestRecordset(base.TestCase): def test_get_recordset_by_name(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) fake_rs = RecordsetTestWrapper(self, recordset) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=[ - 'name={name}'.format(name=fake_zone['name'])]), - json={'zones': [fake_zone.get_get_response_json()]}), - # try directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets', fake_rs['name']]), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets'], - qs_elements=['name={name}'.format(name=fake_rs['name'])]), - json={'recordsets': [fake_rs.get_get_response_json()]}) - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['name']], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=[ + 'name={name}'.format(name=fake_zone['name']) + ], + ), + json={'zones': [fake_zone.get_get_response_json()]}, + ), + # try directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + fake_rs['name'], + ], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id'], 'recordsets'], + qs_elements=[ + 'name={name}'.format(name=fake_rs['name']) + ], + ), + json={'recordsets': [fake_rs.get_get_response_json()]}, + ), + ] + ) res = self.cloud.get_recordset(fake_zone['name'], fake_rs['name']) fake_rs.cmp(res) self.assert_calls() def test_get_recordset_not_found_returns_false(self): fake_zone = test_zone.ZoneTestWrapper(self, zone) - self.register_uris([ - # try by directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()), - # try directly - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets', 'fake']), - status_code=404), - # list with name - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['id'], - 'recordsets'], - qs_elements=['name=fake']), - json={'recordsets': []}) - ]) + self.register_uris( + [ + # try by directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ), + # try directly + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=[ + 'v2', + 'zones', + fake_zone['id'], + 'recordsets', + 'fake', + ], + ), + status_code=404, + ), + # list with name + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id'], 'recordsets'], + qs_elements=['name=fake'], + ), + json={'recordsets': []}, + ), + ] + ) res = self.cloud.get_recordset(fake_zone['id'], 'fake') self.assertFalse(res) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_role_assignment.py b/openstack/tests/unit/cloud/test_role_assignment.py index 48b54f71a..5f2d465af 100644 --- a/openstack/tests/unit/cloud/test_role_assignment.py +++ b/openstack/tests/unit/cloud/test_role_assignment.py @@ -19,58 +19,84 @@ from openstack.tests.unit import base class TestRoleAssignment(base.TestCase): - - def _build_role_assignment_response(self, role_id, scope_type, scope_id, - entity_type, entity_id): + def _build_role_assignment_response( + self, role_id, scope_type, scope_id, entity_type, entity_id + ): self.assertThat(['group', 'user'], matchers.Contains(entity_type)) self.assertThat(['project', 'domain'], matchers.Contains(scope_type)) # NOTE(notmorgan): Links are thrown out by shade, but we construct them # for corectness. - link_str = ('https://identity.example.com/identity/v3/{scope_t}s' - '/{scopeid}/{entity_t}s/{entityid}/roles/{roleid}') - return [{ - 'links': {'assignment': link_str.format( - scope_t=scope_type, scopeid=scope_id, entity_t=entity_type, - entityid=entity_id, roleid=role_id)}, - 'role': {'id': role_id}, - 'scope': {scope_type: {'id': scope_id}}, - entity_type: {'id': entity_id} - }] + link_str = ( + 'https://identity.example.com/identity/v3/{scope_t}s' + '/{scopeid}/{entity_t}s/{entityid}/roles/{roleid}' + ) + return [ + { + 'links': { + 'assignment': link_str.format( + scope_t=scope_type, + scopeid=scope_id, + entity_t=entity_type, + entityid=entity_id, + roleid=role_id, + ) + }, + 'role': {'id': role_id}, + 'scope': {scope_type: {'id': scope_id}}, + entity_type: {'id': entity_id}, + } + ] def setUp(self, cloud_config_fixture='clouds.yaml'): super(TestRoleAssignment, self).setUp(cloud_config_fixture) self.role_data = self._get_role_data() self.domain_data = self._get_domain_data() self.user_data = self._get_user_data( - domain_id=self.domain_data.domain_id) + domain_id=self.domain_data.domain_id + ) self.project_data = self._get_project_data( - domain_id=self.domain_data.domain_id) + domain_id=self.domain_data.domain_id + ) self.project_data_v2 = self._get_project_data( project_name=self.project_data.project_name, project_id=self.project_data.project_id, - v3=False) + v3=False, + ) self.group_data = self._get_group_data( - domain_id=self.domain_data.domain_id) + domain_id=self.domain_data.domain_id + ) self.user_project_assignment = self._build_role_assignment_response( - role_id=self.role_data.role_id, scope_type='project', - scope_id=self.project_data.project_id, entity_type='user', - entity_id=self.user_data.user_id) + role_id=self.role_data.role_id, + scope_type='project', + scope_id=self.project_data.project_id, + entity_type='user', + entity_id=self.user_data.user_id, + ) self.group_project_assignment = self._build_role_assignment_response( - role_id=self.role_data.role_id, scope_type='project', - scope_id=self.project_data.project_id, entity_type='group', - entity_id=self.group_data.group_id) + role_id=self.role_data.role_id, + scope_type='project', + scope_id=self.project_data.project_id, + entity_type='group', + entity_id=self.group_data.group_id, + ) self.user_domain_assignment = self._build_role_assignment_response( - role_id=self.role_data.role_id, scope_type='domain', - scope_id=self.domain_data.domain_id, entity_type='user', - entity_id=self.user_data.user_id) + role_id=self.role_data.role_id, + scope_type='domain', + scope_id=self.domain_data.domain_id, + entity_type='user', + entity_id=self.user_data.user_id, + ) self.group_domain_assignment = self._build_role_assignment_response( - role_id=self.role_data.role_id, scope_type='domain', - scope_id=self.domain_data.domain_id, entity_type='group', - entity_id=self.group_data.group_id) + role_id=self.role_data.role_id, + scope_type='domain', + scope_id=self.domain_data.domain_id, + entity_type='group', + entity_id=self.group_data.group_id, + ) # Cleanup of instances to ensure garbage collection/no leaking memory # in tests. @@ -85,25 +111,40 @@ class TestRoleAssignment(base.TestCase): self.addCleanup(delattr, self, 'user_domain_assignment') self.addCleanup(delattr, self, 'group_domain_assignment') - def get_mock_url(self, service_type='identity', interface='public', - resource='role_assignments', append=None, - base_url_append='v3', qs_elements=None): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource='role_assignments', + append=None, + base_url_append='v3', + qs_elements=None, + ): return super(TestRoleAssignment, self).get_mock_url( - service_type, interface, resource, append, base_url_append, - qs_elements) + service_type, + interface, + resource, + append, + base_url_append, + qs_elements, + ) - def __get(self, resource, data, attr, qs_elements, use_name=False, - is_found=True): + def __get( + self, resource, data, attr, qs_elements, use_name=False, is_found=True + ): if not use_name: if is_found: - return [dict( - method='GET', - uri=self.get_mock_url( - resource=resource + 's', # do roles from role - append=[getattr(data, attr)], - qs_elements=qs_elements), - status_code=200, - json=data.json_response) + return [ + dict( + method='GET', + uri=self.get_mock_url( + resource=resource + 's', # do roles from role + append=[getattr(data, attr)], + qs_elements=qs_elements, + ), + status_code=200, + json=data.json_response, + ) ] else: return [ @@ -112,15 +153,19 @@ class TestRoleAssignment(base.TestCase): uri=self.get_mock_url( resource=resource + 's', # do roles from role append=[getattr(data, attr)], - qs_elements=qs_elements), - status_code=404), + qs_elements=qs_elements, + ), + status_code=404, + ), dict( method='GET', uri=self.get_mock_url( resource=resource + 's', # do roles from role - qs_elements=qs_elements), + qs_elements=qs_elements, + ), status_code=200, - json={(resource + 's'): []}) + json={(resource + 's'): []}, + ), ] else: return [ @@ -129,1187 +174,1771 @@ class TestRoleAssignment(base.TestCase): uri=self.get_mock_url( resource=resource + 's', append=[getattr(data, attr)], - qs_elements=qs_elements), - status_code=404), + qs_elements=qs_elements, + ), + status_code=404, + ), dict( method='GET', uri=self.get_mock_url( resource=resource + 's', - qs_elements=[ - 'name=' + getattr(data, attr)] + qs_elements), + qs_elements=['name=' + getattr(data, attr)] + + qs_elements, + ), status_code=200, - json={ - (resource + 's'): [data.json_response[resource]]}) + json={(resource + 's'): [data.json_response[resource]]}, + ), ] def __user_mocks(self, user_data, use_name, is_found=True): uri_mocks = [] if not use_name: - uri_mocks.append(dict( - method='GET', - uri=self.get_mock_url(resource='users'), - status_code=200, - json={'users': [user_data.json_response['user']] if is_found - else []})) + uri_mocks.append( + dict( + method='GET', + uri=self.get_mock_url(resource='users'), + status_code=200, + json={ + 'users': [user_data.json_response['user']] + if is_found + else [] + }, + ) + ) else: - uri_mocks.append(dict( - method='GET', - uri=self.get_mock_url( - resource='users', - qs_elements=[ - 'name=' + user_data.name - ] - ), - status_code=200, - json={'users': [user_data.json_response['user']] if is_found - else []})) + uri_mocks.append( + dict( + method='GET', + uri=self.get_mock_url( + resource='users', + qs_elements=['name=' + user_data.name], + ), + status_code=200, + json={ + 'users': [user_data.json_response['user']] + if is_found + else [] + }, + ) + ) return uri_mocks def _get_mock_role_query_urls( - self, role_data, domain_data=None, project_data=None, - group_data=None, user_data=None, - use_role_name=False, use_domain_name=False, use_project_name=False, - use_group_name=False, use_user_name=False, use_domain_in_query=True + self, + role_data, + domain_data=None, + project_data=None, + group_data=None, + user_data=None, + use_role_name=False, + use_domain_name=False, + use_project_name=False, + use_group_name=False, + use_user_name=False, + use_domain_in_query=True, ): - """Build uri mocks for querying role assignments - """ + """Build uri mocks for querying role assignments""" uri_mocks = [] if domain_data: uri_mocks.extend( self.__get( - 'domain', domain_data, + 'domain', + domain_data, 'domain_id' if not use_domain_name else 'domain_name', - [], use_name=use_domain_name) + [], + use_name=use_domain_name, + ) ) qs_elements = [] if domain_data and use_domain_in_query: - qs_elements = [ - 'domain_id=' + domain_data.domain_id - ] + qs_elements = ['domain_id=' + domain_data.domain_id] uri_mocks.extend( self.__get( - 'role', role_data, + 'role', + role_data, 'role_id' if not use_role_name else 'role_name', - [], use_name=use_role_name) + [], + use_name=use_role_name, + ) ) if user_data: - uri_mocks.extend(self.__user_mocks( - user_data, use_user_name, is_found=True)) + uri_mocks.extend( + self.__user_mocks(user_data, use_user_name, is_found=True) + ) if group_data: uri_mocks.extend( self.__get( - 'group', group_data, + 'group', + group_data, 'group_id' if not use_group_name else 'group_name', - qs_elements, use_name=use_group_name) + qs_elements, + use_name=use_group_name, + ) ) if project_data: uri_mocks.extend( self.__get( - 'project', project_data, + 'project', + project_data, 'project_id' if not use_project_name else 'project_name', - qs_elements, use_name=use_project_name) + qs_elements, + use_name=use_project_name, + ) ) return uri_mocks def test_grant_role_user_id_project(self): uris = self._get_mock_role_query_urls( - self.role_data, project_data=self.project_data, - user_data=self.user_data, use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + self.role_data, + project_data=self.project_data, + user_data=self.user_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, user=self.user_data.user_id, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_grant_role_user_name_project(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + project_data=self.project_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, user=self.user_data.name, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) def test_grant_role_user_id_project_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + project_data=self.project_data, + user_data=self.user_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_id, - user=self.user_data.user_id, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_id, + user=self.user_data.user_id, + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_grant_role_user_name_project_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + project_data=self.project_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_name, - user=self.user_data.name, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_name, + user=self.user_data.name, + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_grant_role_group_id_project(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data, - use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + project_data=self.project_data, + group_data=self.group_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, group=self.group_data.group_id, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_grant_role_group_name_project(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + project_data=self.project_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, group=self.group_data.group_name, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_grant_role_group_id_project_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + project_data=self.project_data, + group_data=self.group_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_id, - group=self.group_data.group_id, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_id, + group=self.group_data.group_id, + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_grant_role_group_name_project_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + project_data=self.project_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_name, - group=self.group_data.group_name, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_name, + group=self.group_data.group_name, + project=self.project_data.project_id, + ) + ) self.assert_calls() -# ===== Domain + # ===== Domain def test_grant_role_user_id_domain(self): uris = self._get_mock_role_query_urls( - self.role_data, domain_data=self.domain_data, - user_data=self.user_data, use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + self.role_data, + domain_data=self.domain_data, + user_data=self.user_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, user=self.user_data.user_id, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_role_user_name_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + domain_data=self.domain_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, user=self.user_data.name, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) def test_grant_role_user_id_domain_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, user_data=self.user_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + domain_data=self.domain_data, + user_data=self.user_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_id, - user=self.user_data.user_id, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_id, + user=self.user_data.user_id, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_role_user_name_domain_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + domain_data=self.domain_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_name, - user=self.user_data.name, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_name, + user=self.user_data.name, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_role_group_id_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data, - use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, group=self.group_data.group_id, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_role_group_name_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.grant_role( self.role_data.role_name, group=self.group_data.group_name, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_role_group_id_domain_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_id, - group=self.group_data.group_id, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_id, + group=self.group_data.group_id, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_role_group_name_domain_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.grant_role( - self.role_data.role_name, - group=self.group_data.group_name, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.grant_role( + self.role_data.role_name, + group=self.group_data.group_name, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() -# ==== Revoke + # ==== Revoke def test_revoke_role_user_id_project(self): uris = self._get_mock_role_query_urls( - self.role_data, project_data=self.project_data, - user_data=self.user_data, use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + self.role_data, + project_data=self.project_data, + user_data=self.user_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, user=self.user_data.user_id, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_revoke_role_user_name_project(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + project_data=self.project_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, user=self.user_data.name, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) def test_revoke_role_user_id_project_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + project_data=self.project_data, + user_data=self.user_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_id, - user=self.user_data.user_id, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_id, + user=self.user_data.user_id, + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_revoke_role_user_name_project_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + project_data=self.project_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_name, - user=self.user_data.name, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_name, + user=self.user_data.name, + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_revoke_role_group_id_project(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data, - use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + project_data=self.project_data, + group_data=self.group_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, group=self.group_data.group_id, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_revoke_role_group_name_project(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + project_data=self.project_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, group=self.group_data.group_name, - project=self.project_data.project_id)) + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_revoke_role_group_id_project_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + project_data=self.project_data, + group_data=self.group_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_id, - group=self.group_data.group_id, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_id, + group=self.group_data.group_id, + project=self.project_data.project_id, + ) + ) self.assert_calls() def test_revoke_role_group_name_project_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + project_data=self.project_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_name, - group=self.group_data.group_name, - project=self.project_data.project_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_name, + group=self.group_data.group_name, + project=self.project_data.project_id, + ) + ) self.assert_calls() -# ==== Domain + # ==== Domain def test_revoke_role_user_id_domain(self): uris = self._get_mock_role_query_urls( - self.role_data, domain_data=self.domain_data, - user_data=self.user_data, use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + self.role_data, + domain_data=self.domain_data, + user_data=self.user_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, user=self.user_data.user_id, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_revoke_role_user_name_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + domain_data=self.domain_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, user=self.user_data.name, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) def test_revoke_role_user_id_domain_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, user_data=self.user_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + domain_data=self.domain_data, + user_data=self.user_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_id, - user=self.user_data.user_id, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_id, + user=self.user_data.user_id, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_revoke_role_user_name_domain_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + domain_data=self.domain_data, + user_data=self.user_data, + use_role_name=True, + use_user_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_name, - user=self.user_data.name, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_name, + user=self.user_data.name, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_revoke_role_group_id_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data, - use_role_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + use_role_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, group=self.group_data.group_id, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_revoke_role_group_name_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='domains', - append=[self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( self.cloud.revoke_role( self.role_data.role_name, group=self.group_data.group_name, - domain=self.domain_data.domain_id)) + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_revoke_role_group_id_domain_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_id, - group=self.group_data.group_id, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_id, + group=self.group_data.group_id, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_revoke_role_group_name_domain_not_exists(self): uris = self._get_mock_role_query_urls( self.role_data, - domain_data=self.domain_data, group_data=self.group_data, - use_role_name=True, use_group_name=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='domains', - append=[ - self.domain_data.domain_id, - 'groups', self.group_data.group_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - ]) + domain_data=self.domain_data, + group_data=self.group_data, + use_role_name=True, + use_group_name=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='domains', + append=[ + self.domain_data.domain_id, + 'groups', + self.group_data.group_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + ] + ) self.register_uris(uris) - self.assertFalse(self.cloud.revoke_role( - self.role_data.role_name, - group=self.group_data.group_name, - domain=self.domain_data.domain_id)) + self.assertFalse( + self.cloud.revoke_role( + self.role_data.role_name, + group=self.group_data.group_name, + domain=self.domain_data.domain_id, + ) + ) self.assert_calls() def test_grant_no_role(self): uris = self.__get( - 'domain', self.domain_data, 'domain_name', [], use_name=True) - uris.extend([ - dict(method='GET', - uri=self.get_mock_url( - resource='roles', - append=[self.role_data.role_name], - ), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - resource='roles', - qs_elements=[ - 'name=' + self.role_data.role_name, - ]), - status_code=200, - json={'roles': []}) - ]) + 'domain', self.domain_data, 'domain_name', [], use_name=True + ) + uris.extend( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='roles', + append=[self.role_data.role_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='roles', + qs_elements=[ + 'name=' + self.role_data.role_name, + ], + ), + status_code=200, + json={'roles': []}, + ), + ] + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Role {0} not found'.format(self.role_data.role_name) + 'Role {0} not found'.format(self.role_data.role_name), ): self.cloud.grant_role( self.role_data.role_name, group=self.group_data.group_name, - domain=self.domain_data.domain_name) + domain=self.domain_data.domain_name, + ) self.assert_calls() def test_revoke_no_role(self): uris = self.__get( - 'domain', self.domain_data, 'domain_name', [], use_name=True) - uris.extend([ - dict(method='GET', - uri=self.get_mock_url( - resource='roles', - append=[self.role_data.role_name], - ), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - resource='roles', - qs_elements=[ - 'name=' + self.role_data.role_name, - ]), - status_code=200, - json={'roles': []}) - ]) + 'domain', self.domain_data, 'domain_name', [], use_name=True + ) + uris.extend( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='roles', + append=[self.role_data.role_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='roles', + qs_elements=[ + 'name=' + self.role_data.role_name, + ], + ), + status_code=200, + json={'roles': []}, + ), + ] + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Role {0} not found'.format(self.role_data.role_name) + 'Role {0} not found'.format(self.role_data.role_name), ): self.cloud.revoke_role( self.role_data.role_name, group=self.group_data.group_name, - domain=self.domain_data.domain_name) + domain=self.domain_data.domain_name, + ) self.assert_calls() def test_grant_no_user_or_group_specified(self): uris = self.__get( - 'role', self.role_data, 'role_name', [], use_name=True) + 'role', self.role_data, 'role_name', [], use_name=True + ) self.register_uris(uris) with testtools.ExpectedException( - exc.OpenStackCloudException, - 'Must specify either a user or a group' + exc.OpenStackCloudException, + 'Must specify either a user or a group', ): self.cloud.grant_role(self.role_data.role_name) self.assert_calls() def test_revoke_no_user_or_group_specified(self): uris = self.__get( - 'role', self.role_data, 'role_name', [], use_name=True) + 'role', self.role_data, 'role_name', [], use_name=True + ) self.register_uris(uris) with testtools.ExpectedException( - exc.OpenStackCloudException, - 'Must specify either a user or a group' + exc.OpenStackCloudException, + 'Must specify either a user or a group', ): self.cloud.revoke_role(self.role_data.role_name) self.assert_calls() def test_grant_no_user_or_group(self): uris = self.__get( - 'role', self.role_data, 'role_name', [], use_name=True) - uris.extend(self.__user_mocks(self.user_data, use_name=True, - is_found=False)) + 'role', self.role_data, 'role_name', [], use_name=True + ) + uris.extend( + self.__user_mocks(self.user_data, use_name=True, is_found=False) + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Must specify either a user or a group' + 'Must specify either a user or a group', ): self.cloud.grant_role( - self.role_data.role_name, - user=self.user_data.name) + self.role_data.role_name, user=self.user_data.name + ) self.assert_calls() def test_revoke_no_user_or_group(self): uris = self.__get( - 'role', self.role_data, 'role_name', [], use_name=True) - uris.extend(self.__user_mocks(self.user_data, use_name=True, - is_found=False)) + 'role', self.role_data, 'role_name', [], use_name=True + ) + uris.extend( + self.__user_mocks(self.user_data, use_name=True, is_found=False) + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Must specify either a user or a group' + 'Must specify either a user or a group', ): self.cloud.revoke_role( - self.role_data.role_name, - user=self.user_data.name) + self.role_data.role_name, user=self.user_data.name + ) self.assert_calls() def test_grant_both_user_and_group(self): uris = self.__get( - 'role', self.role_data, 'role_name', [], use_name=True) + 'role', self.role_data, 'role_name', [], use_name=True + ) uris.extend(self.__user_mocks(self.user_data, use_name=True)) - uris.extend(self.__get( - 'group', self.group_data, 'group_name', [], use_name=True)) + uris.extend( + self.__get( + 'group', self.group_data, 'group_name', [], use_name=True + ) + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Specify either a group or a user, not both' + 'Specify either a group or a user, not both', ): self.cloud.grant_role( self.role_data.role_name, user=self.user_data.name, - group=self.group_data.group_name) + group=self.group_data.group_name, + ) self.assert_calls() def test_revoke_both_user_and_group(self): uris = self.__get( - 'role', self.role_data, 'role_name', [], use_name=True) + 'role', self.role_data, 'role_name', [], use_name=True + ) uris.extend(self.__user_mocks(self.user_data, use_name=True)) - uris.extend(self.__get( - 'group', self.group_data, 'group_name', [], use_name=True)) + uris.extend( + self.__get( + 'group', self.group_data, 'group_name', [], use_name=True + ) + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Specify either a group or a user, not both' + 'Specify either a group or a user, not both', ): self.cloud.revoke_role( self.role_data.role_name, user=self.user_data.name, - group=self.group_data.group_name) + group=self.group_data.group_name, + ) def test_grant_both_project_and_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data, + project_data=self.project_data, + user_data=self.user_data, domain_data=self.domain_data, - use_role_name=True, use_user_name=True, use_project_name=True, - use_domain_name=True, use_domain_in_query=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=404), - dict(method='PUT', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + use_role_name=True, + use_user_name=True, + use_project_name=True, + use_domain_name=True, + use_domain_in_query=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=404, + ), + dict( + method='PUT', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( @@ -1317,34 +1946,55 @@ class TestRoleAssignment(base.TestCase): self.role_data.role_name, user=self.user_data.name, project=self.project_data.project_name, - domain=self.domain_data.domain_name)) + domain=self.domain_data.domain_name, + ) + ) def test_revoke_both_project_and_domain(self): uris = self._get_mock_role_query_urls( self.role_data, - project_data=self.project_data, user_data=self.user_data, + project_data=self.project_data, + user_data=self.user_data, domain_data=self.domain_data, - use_role_name=True, use_user_name=True, use_project_name=True, - use_domain_name=True, use_domain_in_query=True) - uris.extend([ - dict(method='HEAD', - uri=self.get_mock_url( - resource='projects', - append=[ - self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id - ]), - complete_qs=True, - status_code=204), - dict(method='DELETE', - uri=self.get_mock_url( - resource='projects', - append=[self.project_data.project_id, - 'users', self.user_data.user_id, - 'roles', self.role_data.role_id]), - status_code=200), - ]) + use_role_name=True, + use_user_name=True, + use_project_name=True, + use_domain_name=True, + use_domain_in_query=True, + ) + uris.extend( + [ + dict( + method='HEAD', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + complete_qs=True, + status_code=204, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + resource='projects', + append=[ + self.project_data.project_id, + 'users', + self.user_data.user_id, + 'roles', + self.role_data.role_id, + ], + ), + status_code=200, + ), + ] + ) self.register_uris(uris) self.assertTrue( @@ -1352,76 +2002,98 @@ class TestRoleAssignment(base.TestCase): self.role_data.role_name, user=self.user_data.name, project=self.project_data.project_name, - domain=self.domain_data.domain_name)) + domain=self.domain_data.domain_name, + ) + ) def test_grant_no_project_or_domain(self): uris = self._get_mock_role_query_urls( self.role_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) + use_role_name=True, + use_user_name=True, + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Must specify either a domain, project or system' + 'Must specify either a domain, project or system', ): self.cloud.grant_role( - self.role_data.role_name, - user=self.user_data.name) + self.role_data.role_name, user=self.user_data.name + ) self.assert_calls() def test_revoke_no_project_or_domain_or_system(self): uris = self._get_mock_role_query_urls( self.role_data, user_data=self.user_data, - use_role_name=True, use_user_name=True) + use_role_name=True, + use_user_name=True, + ) self.register_uris(uris) with testtools.ExpectedException( exc.OpenStackCloudException, - 'Must specify either a domain, project or system' + 'Must specify either a domain, project or system', ): self.cloud.revoke_role( - self.role_data.role_name, - user=self.user_data.name) + self.role_data.role_name, user=self.user_data.name + ) self.assert_calls() def test_grant_bad_domain_exception(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='domains', append=['baddomain']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - resource='domains', - qs_elements=['name=baddomain']), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='domains', append=['baddomain'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='domains', qs_elements=['name=baddomain'] + ), + status_code=404, + ), + ] + ) with testtools.ExpectedException(exc.OpenStackCloudURINotFound): self.cloud.grant_role( self.role_data.role_name, user=self.user_data.name, - domain='baddomain') + domain='baddomain', + ) self.assert_calls() def test_revoke_bad_domain_exception(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - resource='domains', append=['baddomain']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - resource='domains', - qs_elements=['name=baddomain']), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + resource='domains', append=['baddomain'] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + resource='domains', qs_elements=['name=baddomain'] + ), + status_code=404, + ), + ] + ) with testtools.ExpectedException(exc.OpenStackCloudURINotFound): self.cloud.revoke_role( self.role_data.role_name, user=self.user_data.name, - domain='baddomain') + domain='baddomain', + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_router.py b/openstack/tests/unit/cloud/test_router.py index 6f4fd4c67..a23ec6acc 100644 --- a/openstack/tests/unit/cloud/test_router.py +++ b/openstack/tests/unit/cloud/test_router.py @@ -41,14 +41,9 @@ class TestRouter(base.TestCase): 'id': router_id, 'name': router_name, 'project_id': u'861808a93da0484ea1767967c4df8a23', - 'routes': [ - { - "destination": "179.24.1.0/24", - "nexthop": "172.24.3.99" - } - ], + 'routes': [{"destination": "179.24.1.0/24", "nexthop": "172.24.3.99"}], 'status': u'ACTIVE', - 'tenant_id': u'861808a93da0484ea1767967c4df8a23' + 'tenant_id': u'861808a93da0484ea1767967c4df8a23', } mock_router_interface_rep = { @@ -58,7 +53,7 @@ class TestRouter(base.TestCase): 'subnet_ids': [subnet_id], 'port_id': '23999891-78b3-4a6b-818d-d1b713f67848', 'id': '57076620-dcfb-42ed-8ad6-79ccb4a79ed2', - 'request_ids': ['req-f1b0b1b4-ae51-4ef9-b371-0cc3c3402cf7'] + 'request_ids': ['req-f1b0b1b4-ae51-4ef9-b371-0cc3c3402cf7'], } router_availability_zone_extension = { @@ -66,7 +61,7 @@ class TestRouter(base.TestCase): "updated": "2015-01-01T10:00:00-00:00", "description": "Availability zone support for router.", "links": [], - "name": "Router Availability Zone" + "name": "Router Availability Zone", } router_extraroute_extension = { @@ -74,66 +69,100 @@ class TestRouter(base.TestCase): "updated": "2015-01-01T10:00:00-00:00", "description": "extra routes extension for router.", "links": [], - "name": "Extra Routes" + "name": "Extra Routes", } enabled_neutron_extensions = [ router_availability_zone_extension, - router_extraroute_extension] + router_extraroute_extension, + ] def _compare_routers(self, exp, real): self.assertDictEqual( _router.Router(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_get_router(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers'], - qs_elements=['name=%s' % self.router_name]), - json={'routers': [self.mock_router_rep]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', self.router_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers'], + qs_elements=['name=%s' % self.router_name], + ), + json={'routers': [self.mock_router_rep]}, + ), + ] + ) r = self.cloud.get_router(self.router_name) self.assertIsNotNone(r) self._compare_routers(self.mock_router_rep, r) self.assert_calls() def test_get_router_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', 'mickey']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers'], - qs_elements=['name=mickey']), - json={'routers': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', 'mickey'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers'], + qs_elements=['name=mickey'], + ), + json={'routers': []}, + ), + ] + ) r = self.cloud.get_router('mickey') self.assertIsNone(r) self.assert_calls() def test_create_router(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers']), - json={'router': self.mock_router_rep}, - validate=dict( - json={'router': { - 'name': self.router_name, - 'admin_state_up': True}})) - ]) - new_router = self.cloud.create_router(name=self.router_name, - admin_state_up=True) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers'] + ), + json={'router': self.mock_router_rep}, + validate=dict( + json={ + 'router': { + 'name': self.router_name, + 'admin_state_up': True, + } + } + ), + ) + ] + ) + new_router = self.cloud.create_router( + name=self.router_name, admin_state_up=True + ) self._compare_routers(self.mock_router_rep, new_router) self.assert_calls() @@ -143,136 +172,213 @@ class TestRouter(base.TestCase): mock_router_rep = copy.copy(self.mock_router_rep) mock_router_rep['tenant_id'] = new_router_tenant_id mock_router_rep['project_id'] = new_router_tenant_id - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers']), - json={'router': mock_router_rep}, - validate=dict( - json={'router': { - 'name': self.router_name, - 'admin_state_up': True, - 'project_id': new_router_tenant_id}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers'] + ), + json={'router': mock_router_rep}, + validate=dict( + json={ + 'router': { + 'name': self.router_name, + 'admin_state_up': True, + 'project_id': new_router_tenant_id, + } + } + ), + ) + ] + ) - self.cloud.create_router(self.router_name, - project_id=new_router_tenant_id) + self.cloud.create_router( + self.router_name, project_id=new_router_tenant_id + ) self.assert_calls() def test_create_router_with_availability_zone_hints(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers']), - json={'router': self.mock_router_rep}, - validate=dict( - json={'router': { - 'name': self.router_name, - 'admin_state_up': True, - 'availability_zone_hints': ['nova']}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers'] + ), + json={'router': self.mock_router_rep}, + validate=dict( + json={ + 'router': { + 'name': self.router_name, + 'admin_state_up': True, + 'availability_zone_hints': ['nova'], + } + } + ), + ), + ] + ) self.cloud.create_router( - name=self.router_name, admin_state_up=True, - availability_zone_hints=['nova']) + name=self.router_name, + admin_state_up=True, + availability_zone_hints=['nova'], + ) self.assert_calls() def test_create_router_without_enable_snat(self): """Do not send enable_snat when not given.""" - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers']), - json={'router': self.mock_router_rep}, - validate=dict( - json={'router': { - 'name': self.router_name, - 'admin_state_up': True}})) - ]) - self.cloud.create_router( - name=self.router_name, admin_state_up=True) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers'] + ), + json={'router': self.mock_router_rep}, + validate=dict( + json={ + 'router': { + 'name': self.router_name, + 'admin_state_up': True, + } + } + ), + ) + ] + ) + self.cloud.create_router(name=self.router_name, admin_state_up=True) self.assert_calls() def test_create_router_with_enable_snat_True(self): """Send enable_snat when it is True.""" - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers']), - json={'router': self.mock_router_rep}, - validate=dict( - json={'router': { - 'name': self.router_name, - 'admin_state_up': True, - 'external_gateway_info': {'enable_snat': True}}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers'] + ), + json={'router': self.mock_router_rep}, + validate=dict( + json={ + 'router': { + 'name': self.router_name, + 'admin_state_up': True, + 'external_gateway_info': {'enable_snat': True}, + } + } + ), + ) + ] + ) self.cloud.create_router( - name=self.router_name, admin_state_up=True, enable_snat=True) + name=self.router_name, admin_state_up=True, enable_snat=True + ) self.assert_calls() def test_create_router_with_enable_snat_False(self): """Send enable_snat when it is False.""" - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers']), - json={'router': self.mock_router_rep}, - validate=dict( - json={'router': { - 'name': self.router_name, - 'external_gateway_info': {'enable_snat': False}, - 'admin_state_up': True}})) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers'] + ), + json={'router': self.mock_router_rep}, + validate=dict( + json={ + 'router': { + 'name': self.router_name, + 'external_gateway_info': { + 'enable_snat': False + }, + 'admin_state_up': True, + } + } + ), + ) + ] + ) self.cloud.create_router( - name=self.router_name, admin_state_up=True, enable_snat=False) + name=self.router_name, admin_state_up=True, enable_snat=False + ) self.assert_calls() def test_create_router_wrong_availability_zone_hints_type(self): azh_opts = "invalid" with testtools.ExpectedException( exc.OpenStackCloudException, - "Parameter 'availability_zone_hints' must be a list" + "Parameter 'availability_zone_hints' must be a list", ): self.cloud.create_router( - name=self.router_name, admin_state_up=True, - availability_zone_hints=azh_opts) + name=self.router_name, + admin_state_up=True, + availability_zone_hints=azh_opts, + ) def test_add_router_interface(self): - self.register_uris([ - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_id, - 'add_router_interface']), - json={'port': self.mock_router_interface_rep}, - validate=dict( - json={'subnet_id': self.subnet_id})) - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'routers', + self.router_id, + 'add_router_interface', + ], + ), + json={'port': self.mock_router_interface_rep}, + validate=dict(json={'subnet_id': self.subnet_id}), + ) + ] + ) self.cloud.add_router_interface( - {'id': self.router_id}, subnet_id=self.subnet_id) + {'id': self.router_id}, subnet_id=self.subnet_id + ) self.assert_calls() def test_remove_router_interface(self): - self.register_uris([ - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_id, - 'remove_router_interface']), - json={'port': self.mock_router_interface_rep}, - validate=dict( - json={'subnet_id': self.subnet_id})) - ]) + self.register_uris( + [ + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'routers', + self.router_id, + 'remove_router_interface', + ], + ), + json={'port': self.mock_router_interface_rep}, + validate=dict(json={'subnet_id': self.subnet_id}), + ) + ] + ) self.cloud.remove_router_interface( - {'id': self.router_id}, subnet_id=self.subnet_id) + {'id': self.router_id}, subnet_id=self.subnet_id + ) self.assert_calls() def test_remove_router_interface_missing_argument(self): - self.assertRaises(ValueError, self.cloud.remove_router_interface, - {'id': '123'}) + self.assertRaises( + ValueError, self.cloud.remove_router_interface, {'id': '123'} + ) def test_update_router(self): new_router_name = "mickey" @@ -283,114 +389,179 @@ class TestRouter(base.TestCase): # validate_calls() asserts that these requests are done in order, # but the extensions call is only called if a non-None value is # passed in 'routes' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json={'extensions': self.enabled_neutron_extensions}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers', - self.router_id]), - json=self.mock_router_rep), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_id]), - json={'router': expected_router_rep}, - validate=dict( - json={'router': { - 'name': new_router_name, - 'routes': new_routes}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json={'extensions': self.enabled_neutron_extensions}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', self.router_id], + ), + json=self.mock_router_rep, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', self.router_id], + ), + json={'router': expected_router_rep}, + validate=dict( + json={ + 'router': { + 'name': new_router_name, + 'routes': new_routes, + } + } + ), + ), + ] + ) new_router = self.cloud.update_router( - self.router_id, name=new_router_name, routes=new_routes) + self.router_id, name=new_router_name, routes=new_routes + ) self._compare_routers(expected_router_rep, new_router) self.assert_calls() def test_delete_router(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers'], - qs_elements=['name=%s' % self.router_name]), - json={'routers': [self.mock_router_rep]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', self.router_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers'], + qs_elements=['name=%s' % self.router_name], + ), + json={'routers': [self.mock_router_rep]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', self.router_id], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_router(self.router_name)) self.assert_calls() def test_delete_router_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', self.router_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers'], - qs_elements=['name=%s' % self.router_name]), - json={'routers': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', self.router_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers'], + qs_elements=['name=%s' % self.router_name], + ), + json={'routers': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_router(self.router_name)) self.assert_calls() def test_delete_router_multiple_found(self): router1 = dict(id='123', name='mickey') router2 = dict(id='456', name='mickey') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'routers', 'mickey']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'routers'], - qs_elements=['name=mickey']), - json={'routers': [router1, router2]}) - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.delete_router, - 'mickey') + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers', 'mickey'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'routers'], + qs_elements=['name=mickey'], + ), + json={'routers': [router1, router2]}, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, self.cloud.delete_router, 'mickey' + ) self.assert_calls() - def _test_list_router_interfaces(self, router, interface_type, - expected_result=None): + def _test_list_router_interfaces( + self, router, interface_type, expected_result=None + ): internal_ports = [ { 'id': 'internal_port_id', - 'fixed_ips': [{ - 'subnet_id': 'internal_subnet_id', - 'ip_address': "10.0.0.1" - }], + 'fixed_ips': [ + { + 'subnet_id': 'internal_subnet_id', + 'ip_address': "10.0.0.1", + } + ], 'device_id': self.router_id, - 'device_owner': device_owner + 'device_owner': device_owner, } - for device_owner in ['network:router_interface', - 'network:ha_router_replicated_interface', - 'network:router_interface_distributed']] + for device_owner in [ + 'network:router_interface', + 'network:ha_router_replicated_interface', + 'network:router_interface_distributed', + ] + ] - external_ports = [{ - 'id': 'external_port_id', - 'fixed_ips': [{ - 'subnet_id': 'external_subnet_id', - 'ip_address': "1.2.3.4" - }], - 'device_id': self.router_id, - 'device_owner': 'network:router_gateway' - }] + external_ports = [ + { + 'id': 'external_port_id', + 'fixed_ips': [ + { + 'subnet_id': 'external_subnet_id', + 'ip_address': "1.2.3.4", + } + ], + 'device_id': self.router_id, + 'device_owner': 'network:router_gateway', + } + ] if expected_result is None: if interface_type == "internal": @@ -400,37 +571,43 @@ class TestRouter(base.TestCase): else: expected_result = internal_ports + external_ports - mock_uri = dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'ports'], - qs_elements=["device_id=%s" % self.router_id]), - json={'ports': (internal_ports + external_ports)}) + mock_uri = dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'ports'], + qs_elements=["device_id=%s" % self.router_id], + ), + json={'ports': (internal_ports + external_ports)}, + ) self.register_uris([mock_uri]) ret = self.cloud.list_router_interfaces(router, interface_type) self.assertEqual( [_port.Port(**i).to_dict(computed=False) for i in expected_result], - [i.to_dict(computed=False) for i in ret] + [i.to_dict(computed=False) for i in ret], ) self.assert_calls() router = { 'id': router_id, 'external_gateway_info': { - 'external_fixed_ips': [{ - 'subnet_id': 'external_subnet_id', - 'ip_address': '1.2.3.4'}] - } + 'external_fixed_ips': [ + {'subnet_id': 'external_subnet_id', 'ip_address': '1.2.3.4'} + ] + }, } def test_list_router_interfaces_all(self): - self._test_list_router_interfaces(self.router, - interface_type=None) + self._test_list_router_interfaces(self.router, interface_type=None) def test_list_router_interfaces_internal(self): - self._test_list_router_interfaces(self.router, - interface_type="internal") + self._test_list_router_interfaces( + self.router, interface_type="internal" + ) def test_list_router_interfaces_external(self): - self._test_list_router_interfaces(self.router, - interface_type="external") + self._test_list_router_interfaces( + self.router, interface_type="external" + ) diff --git a/openstack/tests/unit/cloud/test_security_groups.py b/openstack/tests/unit/cloud/test_security_groups.py index ebac22bf5..c76ca430b 100644 --- a/openstack/tests/unit/cloud/test_security_groups.py +++ b/openstack/tests/unit/cloud/test_security_groups.py @@ -25,9 +25,14 @@ neutron_grp_dict = fakes.make_fake_neutron_security_group( name='neutron-sec-group', description='Test Neutron security group', rules=[ - dict(id='1', port_range_min=80, port_range_max=81, - protocol='tcp', remote_ip_prefix='0.0.0.0/0') - ] + dict( + id='1', + port_range_min=80, + port_range_max=81, + protocol='tcp', + remote_ip_prefix='0.0.0.0/0', + ) + ], ) @@ -37,43 +42,58 @@ nova_grp_dict = fakes.make_fake_nova_security_group( description='Test Nova security group #1', rules=[ fakes.make_fake_nova_security_group_rule( - id='2', from_port=8000, to_port=8001, ip_protocol='tcp', - cidr='0.0.0.0/0'), - ] + id='2', + from_port=8000, + to_port=8001, + ip_protocol='tcp', + cidr='0.0.0.0/0', + ), + ], ) class TestSecurityGroups(base.TestCase): - def setUp(self): super(TestSecurityGroups, self).setUp() self.has_neutron = True def fake_has_service(*args, **kwargs): return self.has_neutron + self.cloud.has_service = fake_has_service def test_list_security_groups_neutron(self): project_id = 42 self.cloud.secgroup_source = 'neutron' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups'], - qs_elements=["project_id=%s" % project_id]), - json={'security_groups': [neutron_grp_dict]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'security-groups'], + qs_elements=["project_id=%s" % project_id], + ), + json={'security_groups': [neutron_grp_dict]}, + ) + ] + ) self.cloud.list_security_groups(filters={'project_id': project_id}) self.assert_calls() def test_list_security_groups_nova(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups?project_id=42'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': []}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups?project_id=42'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': []}, + ), + ] + ) self.cloud.secgroup_source = 'nova' self.has_neutron = False self.cloud.list_security_groups(filters={'project_id': 42}) @@ -84,25 +104,35 @@ class TestSecurityGroups(base.TestCase): self.cloud.secgroup_source = None self.has_neutron = False - self.assertRaises(openstack.cloud.OpenStackCloudUnavailableFeature, - self.cloud.list_security_groups) + self.assertRaises( + openstack.cloud.OpenStackCloudUnavailableFeature, + self.cloud.list_security_groups, + ) def test_delete_security_group_neutron(self): sg_id = neutron_grp_dict['id'] self.cloud.secgroup_source = 'neutron' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups', '%s' % sg_id]), - status_code=200, - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'security-groups', '%s' % sg_id], + ), + status_code=200, + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_security_group('1')) self.assert_calls() @@ -110,27 +140,39 @@ class TestSecurityGroups(base.TestCase): self.cloud.secgroup_source = 'nova' self.has_neutron = False nova_return = [nova_grp_dict] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': nova_return}), - dict(method='DELETE', - uri='{endpoint}/os-security-groups/2'.format( - endpoint=fakes.COMPUTE_ENDPOINT)), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': nova_return}, + ), + dict( + method='DELETE', + uri='{endpoint}/os-security-groups/2'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + ), + ] + ) self.cloud.delete_security_group('2') self.assert_calls() def test_delete_security_group_neutron_not_found(self): self.cloud.secgroup_source = 'neutron' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ) + ] + ) self.assertFalse(self.cloud.delete_security_group('10')) self.assert_calls() @@ -138,41 +180,53 @@ class TestSecurityGroups(base.TestCase): self.cloud.secgroup_source = 'nova' self.has_neutron = False nova_return = [nova_grp_dict] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': nova_return}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': nova_return}, + ), + ] + ) self.assertFalse(self.cloud.delete_security_group('doesNotExist')) def test_delete_security_group_none(self): self.cloud.secgroup_source = None - self.assertRaises(openstack.cloud.OpenStackCloudUnavailableFeature, - self.cloud.delete_security_group, - 'doesNotExist') + self.assertRaises( + openstack.cloud.OpenStackCloudUnavailableFeature, + self.cloud.delete_security_group, + 'doesNotExist', + ) def test_create_security_group_neutron(self): self.cloud.secgroup_source = 'neutron' group_name = self.getUniqueString() group_desc = self.getUniqueString('description') new_group = fakes.make_fake_neutron_security_group( - id='2', - name=group_name, - description=group_desc, - rules=[]) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_group': new_group}, - validate=dict( - json={'security_group': { - 'name': group_name, - 'description': group_desc - }})) - ]) + id='2', name=group_name, description=group_desc, rules=[] + ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_group': new_group}, + validate=dict( + json={ + 'security_group': { + 'name': group_name, + 'description': group_desc, + } + } + ), + ) + ] + ) r = self.cloud.create_security_group(group_name, group_desc) self.assertEqual(group_name, r['name']) @@ -185,32 +239,40 @@ class TestSecurityGroups(base.TestCase): self.cloud.secgroup_source = 'neutron' project_id = "861808a93da0484ea1767967c4df8a23" group_name = self.getUniqueString() - group_desc = 'security group from' \ - ' test_create_security_group_neutron_specific_tenant' + group_desc = ( + 'security group from' + ' test_create_security_group_neutron_specific_tenant' + ) new_group = fakes.make_fake_neutron_security_group( id='2', name=group_name, description=group_desc, project_id=project_id, - rules=[]) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_group': new_group}, - validate=dict( - json={'security_group': { - 'name': group_name, - 'description': group_desc, - 'tenant_id': project_id - }})) - ]) + rules=[], + ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_group': new_group}, + validate=dict( + json={ + 'security_group': { + 'name': group_name, + 'description': group_desc, + 'tenant_id': project_id, + } + } + ), + ) + ] + ) r = self.cloud.create_security_group( - group_name, - group_desc, - project_id + group_name, group_desc, project_id ) self.assertEqual(group_name, r['name']) self.assertEqual(group_desc, r['description']) @@ -227,23 +289,32 @@ class TestSecurityGroups(base.TestCase): name=group_name, description=group_desc, stateful=False, - rules=[]) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_group': new_group}, - validate=dict( - json={'security_group': { - 'name': group_name, - 'description': group_desc, - 'stateful': False - }})) - ]) + rules=[], + ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_group': new_group}, + validate=dict( + json={ + 'security_group': { + 'name': group_name, + 'description': group_desc, + 'stateful': False, + } + } + ), + ) + ] + ) - r = self.cloud.create_security_group(group_name, group_desc, - stateful=False) + r = self.cloud.create_security_group( + group_name, group_desc, stateful=False + ) self.assertEqual(group_name, r['name']) self.assertEqual(group_desc, r['description']) self.assertEqual(False, r['stateful']) @@ -254,21 +325,27 @@ class TestSecurityGroups(base.TestCase): self.has_neutron = False group_desc = self.getUniqueString('description') new_group = fakes.make_fake_nova_security_group( - id='2', - name=group_name, - description=group_desc, - rules=[]) - self.register_uris([ - dict(method='POST', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_group': new_group}, - validate=dict(json={ - 'security_group': { - 'name': group_name, - 'description': group_desc - }})), - ]) + id='2', name=group_name, description=group_desc, rules=[] + ) + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_group': new_group}, + validate=dict( + json={ + 'security_group': { + 'name': group_name, + 'description': group_desc, + } + } + ), + ), + ] + ) self.cloud.secgroup_source = 'nova' r = self.cloud.create_security_group(group_name, group_desc) @@ -280,9 +357,12 @@ class TestSecurityGroups(base.TestCase): def test_create_security_group_none(self): self.cloud.secgroup_source = None self.has_neutron = False - self.assertRaises(openstack.cloud.OpenStackCloudUnavailableFeature, - self.cloud.create_security_group, - '', '') + self.assertRaises( + openstack.cloud.OpenStackCloudUnavailableFeature, + self.cloud.create_security_group, + '', + '', + ) def test_update_security_group_neutron(self): self.cloud.secgroup_source = 'neutron' @@ -291,22 +371,37 @@ class TestSecurityGroups(base.TestCase): update_return = neutron_grp_dict.copy() update_return['name'] = new_name update_return['stateful'] = False - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups', '%s' % sg_id]), - json={'security_group': update_return}, - validate=dict(json={ - 'security_group': {'name': new_name, 'stateful': False}})) - ]) - r = self.cloud.update_security_group(sg_id, name=new_name, - stateful=False) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'security-groups', '%s' % sg_id], + ), + json={'security_group': update_return}, + validate=dict( + json={ + 'security_group': { + 'name': new_name, + 'stateful': False, + } + } + ), + ), + ] + ) + r = self.cloud.update_security_group( + sg_id, name=new_name, stateful=False + ) self.assertEqual(r['name'], new_name) self.assertEqual(r['stateful'], False) self.assert_calls() @@ -319,26 +414,38 @@ class TestSecurityGroups(base.TestCase): update_return = nova_grp_dict.copy() update_return['name'] = new_name - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': nova_return}), - dict(method='PUT', - uri='{endpoint}/os-security-groups/2'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_group': update_return}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': nova_return}, + ), + dict( + method='PUT', + uri='{endpoint}/os-security-groups/2'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_group': update_return}, + ), + ] + ) r = self.cloud.update_security_group( - nova_grp_dict['id'], name=new_name) + nova_grp_dict['id'], name=new_name + ) self.assertEqual(r['name'], new_name) self.assert_calls() def test_update_security_group_bad_kwarg(self): - self.assertRaises(TypeError, - self.cloud.update_security_group, - 'doesNotExist', bad_arg='') + self.assertRaises( + TypeError, + self.cloud.update_security_group, + 'doesNotExist', + bad_arg='', + ) def test_create_security_group_rule_neutron(self): self.cloud.secgroup_source = 'neutron' @@ -350,7 +457,7 @@ class TestSecurityGroups(base.TestCase): remote_group_id='456', remote_address_group_id='1234-5678', direction='egress', - ethertype='IPv6' + ethertype='IPv6', ) expected_args = copy.copy(args) # For neutron, -1 port should be converted to None @@ -362,24 +469,30 @@ class TestSecurityGroups(base.TestCase): expected_new_rule['tenant_id'] = None expected_new_rule['project_id'] = expected_new_rule['tenant_id'] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-group-rules']), - json={'security_group_rule': expected_new_rule}, - validate=dict(json={ - 'security_group_rule': expected_args})) - ]) - new_rule = self.cloud.create_security_group_rule( - secgroup_name_or_id=neutron_grp_dict['id'], **args).to_dict( - original_names=True + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'security-group-rules'], + ), + json={'security_group_rule': expected_new_rule}, + validate=dict(json={'security_group_rule': expected_args}), + ), + ] ) + new_rule = self.cloud.create_security_group_rule( + secgroup_name_or_id=neutron_grp_dict['id'], **args + ).to_dict(original_names=True) # NOTE(gtema): don't check location and not relevant properties # in new rule new_rule.pop('created_at') @@ -403,7 +516,7 @@ class TestSecurityGroups(base.TestCase): remote_address_group_id=None, direction='egress', ethertype='IPv6', - project_id='861808a93da0484ea1767967c4df8a23' + project_id='861808a93da0484ea1767967c4df8a23', ) expected_args = copy.copy(args) # For neutron, -1 port should be converted to None @@ -420,24 +533,30 @@ class TestSecurityGroups(base.TestCase): # JSON; see SecurityGroupRule where it is removed. expected_args.pop('remote_address_group_id') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-group-rules']), - json={'security_group_rule': expected_new_rule}, - validate=dict(json={ - 'security_group_rule': expected_args})) - ]) - new_rule = self.cloud.create_security_group_rule( - secgroup_name_or_id=neutron_grp_dict['id'], ** args).to_dict( - original_names=True + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'security-group-rules'], + ), + json={'security_group_rule': expected_new_rule}, + validate=dict(json={'security_group_rule': expected_args}), + ), + ] ) + new_rule = self.cloud.create_security_group_rule( + secgroup_name_or_id=neutron_grp_dict['id'], **args + ).to_dict(original_names=True) # NOTE(slaweq): don't check location and properties in new rule new_rule.pop('created_at') new_rule.pop('description') @@ -456,31 +575,52 @@ class TestSecurityGroups(base.TestCase): nova_return = [nova_grp_dict] new_rule = fakes.make_fake_nova_security_group_rule( - id='xyz', from_port=1, to_port=2000, ip_protocol='tcp', - cidr='1.2.3.4/32') + id='xyz', + from_port=1, + to_port=2000, + ip_protocol='tcp', + cidr='1.2.3.4/32', + ) - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': nova_return}), - dict(method='POST', - uri='{endpoint}/os-security-group-rules'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_group_rule': new_rule}, - validate=dict(json={ - "security_group_rule": { - "from_port": 1, - "ip_protocol": "tcp", - "to_port": 2000, - "parent_group_id": "2", - "cidr": "1.2.3.4/32", - "group_id": "123"}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': nova_return}, + ), + dict( + method='POST', + uri='{endpoint}/os-security-group-rules'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_group_rule': new_rule}, + validate=dict( + json={ + "security_group_rule": { + "from_port": 1, + "ip_protocol": "tcp", + "to_port": 2000, + "parent_group_id": "2", + "cidr": "1.2.3.4/32", + "group_id": "123", + } + } + ), + ), + ] + ) self.cloud.create_security_group_rule( - '2', port_range_min=1, port_range_max=2000, protocol='tcp', - remote_ip_prefix='1.2.3.4/32', remote_group_id='123') + '2', + port_range_min=1, + port_range_max=2000, + protocol='tcp', + remote_ip_prefix='1.2.3.4/32', + remote_group_id='123', + ) self.assert_calls() @@ -490,65 +630,100 @@ class TestSecurityGroups(base.TestCase): self.cloud.secgroup_source = 'nova' new_rule = fakes.make_fake_nova_security_group_rule( - id='xyz', from_port=1, to_port=65535, ip_protocol='tcp', - cidr='1.2.3.4/32') + id='xyz', + from_port=1, + to_port=65535, + ip_protocol='tcp', + cidr='1.2.3.4/32', + ) nova_return = [nova_grp_dict] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': nova_return}), - dict(method='POST', - uri='{endpoint}/os-security-group-rules'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_group_rule': new_rule}, - validate=dict(json={ - "security_group_rule": { - "from_port": 1, - "ip_protocol": "tcp", - "to_port": 65535, - "parent_group_id": "2", - "cidr": "1.2.3.4/32", - "group_id": "123"}})), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': nova_return}, + ), + dict( + method='POST', + uri='{endpoint}/os-security-group-rules'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_group_rule': new_rule}, + validate=dict( + json={ + "security_group_rule": { + "from_port": 1, + "ip_protocol": "tcp", + "to_port": 65535, + "parent_group_id": "2", + "cidr": "1.2.3.4/32", + "group_id": "123", + } + } + ), + ), + ] + ) self.cloud.create_security_group_rule( - '2', protocol='tcp', - remote_ip_prefix='1.2.3.4/32', remote_group_id='123') + '2', + protocol='tcp', + remote_ip_prefix='1.2.3.4/32', + remote_group_id='123', + ) self.assert_calls() def test_create_security_group_rule_none(self): self.has_neutron = False self.cloud.secgroup_source = None - self.assertRaises(openstack.cloud.OpenStackCloudUnavailableFeature, - self.cloud.create_security_group_rule, - '') + self.assertRaises( + openstack.cloud.OpenStackCloudUnavailableFeature, + self.cloud.create_security_group_rule, + '', + ) def test_delete_security_group_rule_neutron(self): rule_id = "xyz" self.cloud.secgroup_source = 'neutron' - self.register_uris([ - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-group-rules', - '%s' % rule_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'security-group-rules', + '%s' % rule_id, + ], + ), + json={}, + ) + ] + ) self.assertTrue(self.cloud.delete_security_group_rule(rule_id)) self.assert_calls() def test_delete_security_group_rule_nova(self): self.has_neutron = False self.cloud.secgroup_source = 'nova' - self.register_uris([ - dict(method='DELETE', - uri='{endpoint}/os-security-group-rules/xyz'.format( - endpoint=fakes.COMPUTE_ENDPOINT)), - ]) + self.register_uris( + [ + dict( + method='DELETE', + uri='{endpoint}/os-security-group-rules/xyz'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + ), + ] + ) r = self.cloud.delete_security_group_rule('xyz') self.assertTrue(r) self.assert_calls() @@ -556,32 +731,43 @@ class TestSecurityGroups(base.TestCase): def test_delete_security_group_rule_none(self): self.has_neutron = False self.cloud.secgroup_source = None - self.assertRaises(openstack.cloud.OpenStackCloudUnavailableFeature, - self.cloud.delete_security_group_rule, - '') + self.assertRaises( + openstack.cloud.OpenStackCloudUnavailableFeature, + self.cloud.delete_security_group_rule, + '', + ) def test_delete_security_group_rule_not_found(self): rule_id = "doesNotExist" self.cloud.secgroup_source = 'neutron' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ) + ] + ) self.assertFalse(self.cloud.delete_security_group(rule_id)) self.assert_calls() def test_delete_security_group_rule_not_found_nova(self): self.has_neutron = False self.cloud.secgroup_source = 'nova' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': [nova_grp_dict]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': [nova_grp_dict]}, + ), + ] + ) r = self.cloud.delete_security_group('doesNotExist') self.assertFalse(r) @@ -590,16 +776,23 @@ class TestSecurityGroups(base.TestCase): def test_nova_egress_security_group_rule(self): self.has_neutron = False self.cloud.secgroup_source = 'nova' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': [nova_grp_dict]}), - ]) - self.assertRaises(openstack.cloud.OpenStackCloudException, - self.cloud.create_security_group_rule, - secgroup_name_or_id='nova-sec-group', - direction='egress') + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': [nova_grp_dict]}, + ), + ] + ) + self.assertRaises( + openstack.cloud.OpenStackCloudException, + self.cloud.create_security_group_rule, + secgroup_name_or_id='nova-sec-group', + direction='egress', + ) self.assert_calls() @@ -608,24 +801,30 @@ class TestSecurityGroups(base.TestCase): server = fakes.make_fake_server('1234', 'server-name', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id']]), - json=server), - dict( - method='GET', - uri='{endpoint}/servers/{id}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - id=server['id']), - json={'security_groups': [nova_grp_dict]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', server['id']] + ), + json=server, + ), + dict( + method='GET', + uri='{endpoint}/servers/{id}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=server['id'] + ), + json={'security_groups': [nova_grp_dict]}, + ), + ] + ) groups = self.cloud.list_server_security_groups(server) self.assertEqual( groups[0]['rules'][0]['ip_range']['cidr'], - nova_grp_dict['rules'][0]['ip_range']['cidr']) + nova_grp_dict['rules'][0]['ip_range']['cidr'], + ) self.assert_calls() @@ -641,25 +840,31 @@ class TestSecurityGroups(base.TestCase): self.has_neutron = False self.cloud.secgroup_source = 'nova' - self.register_uris([ - dict( - method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT, + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT, + ), + json={'security_groups': [nova_grp_dict]}, ), - json={'security_groups': [nova_grp_dict]}), - self.get_nova_discovery_mock_dict(), - dict( - method='POST', - uri='%s/servers/%s/action' % (fakes.COMPUTE_ENDPOINT, '1234'), - validate=dict( - json={'addSecurityGroup': {'name': 'nova-sec-group'}}), - status_code=202, - ), - ]) + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri='%s/servers/%s/action' + % (fakes.COMPUTE_ENDPOINT, '1234'), + validate=dict( + json={'addSecurityGroup': {'name': 'nova-sec-group'}} + ), + status_code=202, + ), + ] + ) ret = self.cloud.add_server_security_groups( - dict(id='1234'), 'nova-sec-group') + dict(id='1234'), 'nova-sec-group' + ) self.assertTrue(ret) @@ -672,27 +877,42 @@ class TestSecurityGroups(base.TestCase): # use neutron for secgroup list and return an existing fake self.cloud.secgroup_source = 'neutron' - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', 'detail']), - json={'servers': [fake_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}), - dict(method='POST', - uri='%s/servers/%s/action' % (fakes.COMPUTE_ENDPOINT, '1234'), - validate=dict( - json={'addSecurityGroup': {'name': 'neutron-sec-group'}}), - status_code=202), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + dict( + method='POST', + uri='%s/servers/%s/action' + % (fakes.COMPUTE_ENDPOINT, '1234'), + validate=dict( + json={ + 'addSecurityGroup': {'name': 'neutron-sec-group'} + } + ), + status_code=202, + ), + ] + ) - self.assertTrue(self.cloud.add_server_security_groups( - 'server-name', 'neutron-sec-group')) + self.assertTrue( + self.cloud.add_server_security_groups( + 'server-name', 'neutron-sec-group' + ) + ) self.assert_calls() def test_remove_security_group_from_server_nova(self): @@ -700,23 +920,32 @@ class TestSecurityGroups(base.TestCase): self.has_neutron = False self.cloud.secgroup_source = 'nova' - self.register_uris([ - dict( - method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': [nova_grp_dict]}), - self.get_nova_discovery_mock_dict(), - dict( - method='POST', - uri='%s/servers/%s/action' % (fakes.COMPUTE_ENDPOINT, '1234'), - validate=dict( - json={'removeSecurityGroup': {'name': 'nova-sec-group'}}), - ), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': [nova_grp_dict]}, + ), + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri='%s/servers/%s/action' + % (fakes.COMPUTE_ENDPOINT, '1234'), + validate=dict( + json={ + 'removeSecurityGroup': {'name': 'nova-sec-group'} + } + ), + ), + ] + ) ret = self.cloud.remove_server_security_groups( - dict(id='1234'), 'nova-sec-group') + dict(id='1234'), 'nova-sec-group' + ) self.assertTrue(ret) self.assert_calls() @@ -729,25 +958,37 @@ class TestSecurityGroups(base.TestCase): self.cloud.secgroup_source = 'neutron' validate = {'removeSecurityGroup': {'name': 'neutron-sec-group'}} - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', 'detail']), - json={'servers': [fake_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}), - dict(method='POST', - uri='%s/servers/%s/action' % (fakes.COMPUTE_ENDPOINT, '1234'), - validate=dict(json=validate)), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + dict( + method='POST', + uri='%s/servers/%s/action' + % (fakes.COMPUTE_ENDPOINT, '1234'), + validate=dict(json=validate), + ), + ] + ) - self.assertTrue(self.cloud.remove_server_security_groups( - 'server-name', 'neutron-sec-group')) + self.assertTrue( + self.cloud.remove_server_security_groups( + 'server-name', 'neutron-sec-group' + ) + ) self.assert_calls() def test_add_bad_security_group_to_server_nova(self): @@ -757,22 +998,29 @@ class TestSecurityGroups(base.TestCase): # use nova for secgroup list and return an existing fake self.has_neutron = False self.cloud.secgroup_source = 'nova' - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict( - method='GET', - uri='{endpoint}/servers/detail'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'servers': [fake_server]}), - dict( - method='GET', - uri='{endpoint}/os-security-groups'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'security_groups': [nova_grp_dict]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri='{endpoint}/servers/detail'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'servers': [fake_server]}, + ), + dict( + method='GET', + uri='{endpoint}/os-security-groups'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'security_groups': [nova_grp_dict]}, + ), + ] + ) - ret = self.cloud.add_server_security_groups('server-name', - 'unknown-sec-group') + ret = self.cloud.add_server_security_groups( + 'server-name', 'unknown-sec-group' + ) self.assertFalse(ret) self.assert_calls() @@ -784,69 +1032,96 @@ class TestSecurityGroups(base.TestCase): # use neutron for secgroup list and return an existing fake self.cloud.secgroup_source = 'neutron' - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', 'detail']), - json={'servers': [fake_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'security-groups']), - json={'security_groups': [neutron_grp_dict]}) - ]) - self.assertFalse(self.cloud.add_server_security_groups( - 'server-name', 'unknown-sec-group')) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'security-groups'] + ), + json={'security_groups': [neutron_grp_dict]}, + ), + ] + ) + self.assertFalse( + self.cloud.add_server_security_groups( + 'server-name', 'unknown-sec-group' + ) + ) self.assert_calls() def test_add_security_group_to_bad_server(self): # fake to get server by name, server-name must match fake_server = fakes.make_fake_server('1234', 'server-name', 'ACTIVE') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict( - method='GET', - uri='{endpoint}/servers/detail'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={'servers': [fake_server]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri='{endpoint}/servers/detail'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={'servers': [fake_server]}, + ), + ] + ) - ret = self.cloud.add_server_security_groups('unknown-server-name', - 'nova-sec-group') + ret = self.cloud.add_server_security_groups( + 'unknown-server-name', 'nova-sec-group' + ) self.assertFalse(ret) self.assert_calls() def test_get_security_group_by_id_neutron(self): self.cloud.secgroup_source = 'neutron' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', - 'security-groups', - neutron_grp_dict['id']]), - json={'security_group': neutron_grp_dict}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=[ + 'v2.0', + 'security-groups', + neutron_grp_dict['id'], + ], + ), + json={'security_group': neutron_grp_dict}, + ) + ] + ) ret_sg = self.cloud.get_security_group_by_id(neutron_grp_dict['id']) self.assertEqual(neutron_grp_dict['id'], ret_sg['id']) self.assertEqual(neutron_grp_dict['name'], ret_sg['name']) - self.assertEqual(neutron_grp_dict['description'], - ret_sg['description']) + self.assertEqual( + neutron_grp_dict['description'], ret_sg['description'] + ) self.assertEqual(neutron_grp_dict['stateful'], ret_sg['stateful']) self.assert_calls() def test_get_security_group_by_id_nova(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/os-security-groups/{id}'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - id=nova_grp_dict['id']), - json={'security_group': nova_grp_dict}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/os-security-groups/{id}'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=nova_grp_dict['id'] + ), + json={'security_group': nova_grp_dict}, + ), + ] + ) self.cloud.secgroup_source = 'nova' self.has_neutron = False ret_sg = self.cloud.get_security_group_by_id(nova_grp_dict['id']) @@ -860,9 +1135,15 @@ class TestSecurityGroups(base.TestCase): name='nova_secgroup', description='A Nova security group', rules=[ - dict(id='123', from_port=80, to_port=81, ip_protocol='tcp', - ip_range={'cidr': '0.0.0.0/0'}, parent_group_id='xyz123') - ] + dict( + id='123', + from_port=80, + to_port=81, + ip_protocol='tcp', + ip_range={'cidr': '0.0.0.0/0'}, + parent_group_id='xyz123', + ) + ], ) expected = dict( id='abc123', @@ -878,24 +1159,37 @@ class TestSecurityGroups(base.TestCase): domain_name='default', id='1c36b64c840a42cd9e9b931a369337f0', domain_id=None, - name='admin'), - cloud='_test_cloud_'), + name='admin', + ), + cloud='_test_cloud_', + ), security_group_rules=[ - dict(id='123', direction='ingress', ethertype='IPv4', - port_range_min=80, port_range_max=81, protocol='tcp', - remote_ip_prefix='0.0.0.0/0', security_group_id='xyz123', - project_id='', tenant_id='', properties={}, - remote_group_id=None, - location=dict( - region_name='RegionOne', - zone=None, - project=dict( - domain_name='default', - id='1c36b64c840a42cd9e9b931a369337f0', - domain_id=None, - name='admin'), - cloud='_test_cloud_')) - ] + dict( + id='123', + direction='ingress', + ethertype='IPv4', + port_range_min=80, + port_range_max=81, + protocol='tcp', + remote_ip_prefix='0.0.0.0/0', + security_group_id='xyz123', + project_id='', + tenant_id='', + properties={}, + remote_group_id=None, + location=dict( + region_name='RegionOne', + zone=None, + project=dict( + domain_name='default', + id='1c36b64c840a42cd9e9b931a369337f0', + domain_id=None, + name='admin', + ), + cloud='_test_cloud_', + ), + ) + ], ) # Set secgroup source to nova for this test as stateful parameter # is only valid for neutron security groups. @@ -910,9 +1204,15 @@ class TestSecurityGroups(base.TestCase): name='nova_secgroup', description='A Nova security group with -1 ports', rules=[ - dict(id='123', from_port=-1, to_port=-1, ip_protocol='icmp', - ip_range={'cidr': '0.0.0.0/0'}, parent_group_id='xyz123') - ] + dict( + id='123', + from_port=-1, + to_port=-1, + ip_protocol='icmp', + ip_range={'cidr': '0.0.0.0/0'}, + parent_group_id='xyz123', + ) + ], ) retval = self.cloud._normalize_secgroup(nova_secgroup) self.assertIsNone(retval['security_group_rules'][0]['port_range_min']) @@ -920,24 +1220,41 @@ class TestSecurityGroups(base.TestCase): def test_normalize_secgroup_rules(self): nova_rules = [ - dict(id='123', from_port=80, to_port=81, ip_protocol='tcp', - ip_range={'cidr': '0.0.0.0/0'}, parent_group_id='xyz123') + dict( + id='123', + from_port=80, + to_port=81, + ip_protocol='tcp', + ip_range={'cidr': '0.0.0.0/0'}, + parent_group_id='xyz123', + ) ] expected = [ - dict(id='123', direction='ingress', ethertype='IPv4', - port_range_min=80, port_range_max=81, protocol='tcp', - remote_ip_prefix='0.0.0.0/0', security_group_id='xyz123', - tenant_id='', project_id='', remote_group_id=None, - properties={}, - location=dict( - region_name='RegionOne', - zone=None, - project=dict( - domain_name='default', - id='1c36b64c840a42cd9e9b931a369337f0', - domain_id=None, - name='admin'), - cloud='_test_cloud_')) + dict( + id='123', + direction='ingress', + ethertype='IPv4', + port_range_min=80, + port_range_max=81, + protocol='tcp', + remote_ip_prefix='0.0.0.0/0', + security_group_id='xyz123', + tenant_id='', + project_id='', + remote_group_id=None, + properties={}, + location=dict( + region_name='RegionOne', + zone=None, + project=dict( + domain_name='default', + id='1c36b64c840a42cd9e9b931a369337f0', + domain_id=None, + name='admin', + ), + cloud='_test_cloud_', + ), + ) ] retval = self.cloud._normalize_secgroup_rules(nova_rules) self.assertEqual(expected, retval) diff --git a/openstack/tests/unit/cloud/test_server_console.py b/openstack/tests/unit/cloud/test_server_console.py index 9b885a654..1364422ba 100644 --- a/openstack/tests/unit/cloud/test_server_console.py +++ b/openstack/tests/unit/cloud/test_server_console.py @@ -17,66 +17,80 @@ from openstack.tests.unit import base class TestServerConsole(base.TestCase): - def setUp(self): super(TestServerConsole, self).setUp() self.server_id = str(uuid.uuid4()) self.server_name = self.getUniqueString('name') self.server = fakes.make_fake_server( - server_id=self.server_id, name=self.server_name) + server_id=self.server_id, name=self.server_name + ) self.output = self.getUniqueString('output') def test_get_server_console_dict(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri='{endpoint}/servers/{id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - id=self.server_id), - json={"output": self.output}, - validate=dict( - json={'os-getConsoleOutput': {'length': 5}})) - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri='{endpoint}/servers/{id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=self.server_id + ), + json={"output": self.output}, + validate=dict(json={'os-getConsoleOutput': {'length': 5}}), + ), + ] + ) self.assertEqual( - self.output, self.cloud.get_server_console(self.server, 5)) + self.output, self.cloud.get_server_console(self.server, 5) + ) self.assert_calls() def test_get_server_console_name_or_id(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri='{endpoint}/servers/detail'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json={"servers": [self.server]}), - dict(method='POST', - uri='{endpoint}/servers/{id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - id=self.server_id), - json={"output": self.output}, - validate=dict( - json={'os-getConsoleOutput': {}})) - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri='{endpoint}/servers/detail'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json={"servers": [self.server]}, + ), + dict( + method='POST', + uri='{endpoint}/servers/{id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=self.server_id + ), + json={"output": self.output}, + validate=dict(json={'os-getConsoleOutput': {}}), + ), + ] + ) self.assertEqual( - self.output, self.cloud.get_server_console(self.server['id'])) + self.output, self.cloud.get_server_console(self.server['id']) + ) self.assert_calls() def test_get_server_console_no_console(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri='{endpoint}/servers/{id}/action'.format( - endpoint=fakes.COMPUTE_ENDPOINT, - id=self.server_id), - status_code=400, - validate=dict( - json={'os-getConsoleOutput': {}})) - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri='{endpoint}/servers/{id}/action'.format( + endpoint=fakes.COMPUTE_ENDPOINT, id=self.server_id + ), + status_code=400, + validate=dict(json={'os-getConsoleOutput': {}}), + ), + ] + ) self.assertEqual('', self.cloud.get_server_console(self.server)) diff --git a/openstack/tests/unit/cloud/test_server_delete_metadata.py b/openstack/tests/unit/cloud/test_server_delete_metadata.py index ab0b94997..0183ae182 100644 --- a/openstack/tests/unit/cloud/test_server_delete_metadata.py +++ b/openstack/tests/unit/cloud/test_server_delete_metadata.py @@ -25,52 +25,81 @@ from openstack.tests.unit import base class TestServerDeleteMetadata(base.TestCase): - def setUp(self): super(TestServerDeleteMetadata, self).setUp() self.server_id = str(uuid.uuid4()) self.server_name = self.getUniqueString('name') self.fake_server = fakes.make_fake_server( - self.server_id, self.server_name) + self.server_id, self.server_name + ) def test_server_delete_metadata_with_exception(self): """ Test that a missing metadata throws an exception. """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [self.fake_server]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.fake_server['id'], - 'metadata', 'key']), - status_code=404), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [self.fake_server]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + self.fake_server['id'], + 'metadata', + 'key', + ], + ), + status_code=404, + ), + ] + ) self.assertRaises( - OpenStackCloudURINotFound, self.cloud.delete_server_metadata, - self.server_name, ['key']) + OpenStackCloudURINotFound, + self.cloud.delete_server_metadata, + self.server_name, + ['key'], + ) self.assert_calls() def test_server_delete_metadata(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [self.fake_server]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.fake_server['id'], - 'metadata', 'key']), - status_code=200), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [self.fake_server]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + self.fake_server['id'], + 'metadata', + 'key', + ], + ), + status_code=200, + ), + ] + ) self.cloud.delete_server_metadata(self.server_id, ['key']) diff --git a/openstack/tests/unit/cloud/test_server_group.py b/openstack/tests/unit/cloud/test_server_group.py index ef29523ce..5e6d987c3 100644 --- a/openstack/tests/unit/cloud/test_server_group.py +++ b/openstack/tests/unit/cloud/test_server_group.py @@ -18,48 +18,66 @@ from openstack.tests.unit import base class TestServerGroup(base.TestCase): - def setUp(self): super(TestServerGroup, self).setUp() self.group_id = uuid.uuid4().hex self.group_name = self.getUniqueString('server-group') self.policies = ['affinity'] self.fake_group = fakes.make_fake_server_group( - self.group_id, self.group_name, self.policies) + self.group_id, self.group_name, self.policies + ) def test_create_server_group(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', append=['os-server-groups']), - json={'server_group': self.fake_group}, - validate=dict( - json={'server_group': { - 'name': self.group_name, - 'policies': self.policies, - }})), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', 'public', append=['os-server-groups'] + ), + json={'server_group': self.fake_group}, + validate=dict( + json={ + 'server_group': { + 'name': self.group_name, + 'policies': self.policies, + } + } + ), + ), + ] + ) - self.cloud.create_server_group(name=self.group_name, - policies=self.policies) + self.cloud.create_server_group( + name=self.group_name, policies=self.policies + ) self.assert_calls() def test_delete_server_group(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['os-server-groups']), - json={'server_groups': [self.fake_group]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-server-groups', self.group_id]), - json={'server_groups': [self.fake_group]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['os-server-groups'] + ), + json={'server_groups': [self.fake_group]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-server-groups', self.group_id], + ), + json={'server_groups': [self.fake_group]}, + ), + ] + ) self.assertTrue(self.cloud.delete_server_group(self.group_name)) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_server_set_metadata.py b/openstack/tests/unit/cloud/test_server_set_metadata.py index 72af0efcd..75bc0facd 100644 --- a/openstack/tests/unit/cloud/test_server_set_metadata.py +++ b/openstack/tests/unit/cloud/test_server_set_metadata.py @@ -25,53 +25,73 @@ from openstack.tests.unit import base class TestServerSetMetadata(base.TestCase): - def setUp(self): super(TestServerSetMetadata, self).setUp() self.server_id = str(uuid.uuid4()) self.server_name = self.getUniqueString('name') self.fake_server = fakes.make_fake_server( - self.server_id, self.server_name) + self.server_id, self.server_name + ) def test_server_set_metadata_with_exception(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [self.fake_server]}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.fake_server['id'], - 'metadata']), - validate=dict(json={'metadata': {'meta': 'data'}}), - json={}, - status_code=400), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [self.fake_server]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.fake_server['id'], 'metadata'], + ), + validate=dict(json={'metadata': {'meta': 'data'}}), + json={}, + status_code=400, + ), + ] + ) self.assertRaises( - OpenStackCloudBadRequest, self.cloud.set_server_metadata, - self.server_name, {'meta': 'data'}) + OpenStackCloudBadRequest, + self.cloud.set_server_metadata, + self.server_name, + {'meta': 'data'}, + ) self.assert_calls() def test_server_set_metadata(self): metadata = {'meta': 'data'} - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [self.fake_server]}), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.fake_server['id'], 'metadata']), - validate=dict(json={'metadata': metadata}), - status_code=200, - json={'metadata': metadata}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [self.fake_server]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.fake_server['id'], 'metadata'], + ), + validate=dict(json={'metadata': metadata}), + status_code=200, + json={'metadata': metadata}, + ), + ] + ) self.cloud.set_server_metadata(self.server_id, metadata) diff --git a/openstack/tests/unit/cloud/test_services.py b/openstack/tests/unit/cloud/test_services.py index fb4f5a387..c89695e3a 100644 --- a/openstack/tests/unit/cloud/test_services.py +++ b/openstack/tests/unit/cloud/test_services.py @@ -26,43 +26,59 @@ from openstack.tests.unit import base class CloudServices(base.TestCase): - def setUp(self, cloud_config_fixture='clouds.yaml'): super(CloudServices, self).setUp(cloud_config_fixture) - def get_mock_url(self, service_type='identity', interface='public', - resource='services', append=None, base_url_append='v3'): + def get_mock_url( + self, + service_type='identity', + interface='public', + resource='services', + append=None, + base_url_append='v3', + ): return super(CloudServices, self).get_mock_url( - service_type, interface, resource, append, base_url_append) + service_type, interface, resource, append, base_url_append + ) def test_create_service_v3(self): - service_data = self._get_service_data(name='a service', type='network', - description='A test service') - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url(), - status_code=200, - json=service_data.json_response_v3, - validate=dict(json={'service': service_data.json_request})) - ]) + service_data = self._get_service_data( + name='a service', type='network', description='A test service' + ) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url(), + status_code=200, + json=service_data.json_response_v3, + validate=dict(json={'service': service_data.json_request}), + ) + ] + ) service = self.cloud.create_service( name=service_data.service_name, service_type=service_data.service_type, - description=service_data.description) - self.assertThat(service.name, - matchers.Equals(service_data.service_name)) + description=service_data.description, + ) + self.assertThat( + service.name, matchers.Equals(service_data.service_name) + ) self.assertThat(service.id, matchers.Equals(service_data.service_id)) - self.assertThat(service.description, - matchers.Equals(service_data.description)) - self.assertThat(service.type, - matchers.Equals(service_data.service_type)) + self.assertThat( + service.description, matchers.Equals(service_data.description) + ) + self.assertThat( + service.type, matchers.Equals(service_data.service_type) + ) self.assert_calls() def test_update_service_v3(self): - service_data = self._get_service_data(name='a service', type='network', - description='A test service') + service_data = self._get_service_data( + name='a service', type='network', description='A test service' + ) request = service_data.json_request.copy() request['enabled'] = False resp = service_data.json_response_v3.copy() @@ -70,81 +86,114 @@ class CloudServices(base.TestCase): request.pop('description') request.pop('name') request.pop('type') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [resp['service']]}), - dict(method='PATCH', - uri=self.get_mock_url(append=[service_data.service_id]), - status_code=200, - json=resp, - validate=dict(json={'service': request})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={'services': [resp['service']]}, + ), + dict( + method='PATCH', + uri=self.get_mock_url(append=[service_data.service_id]), + status_code=200, + json=resp, + validate=dict(json={'service': request}), + ), + ] + ) service = self.cloud.update_service( - service_data.service_id, enabled=False) - self.assertThat(service.name, - matchers.Equals(service_data.service_name)) + service_data.service_id, enabled=False + ) + self.assertThat( + service.name, matchers.Equals(service_data.service_name) + ) self.assertThat(service.id, matchers.Equals(service_data.service_id)) - self.assertThat(service.description, - matchers.Equals(service_data.description)) - self.assertThat(service.type, - matchers.Equals(service_data.service_type)) + self.assertThat( + service.description, matchers.Equals(service_data.description) + ) + self.assertThat( + service.type, matchers.Equals(service_data.service_type) + ) self.assert_calls() def test_list_services(self): service_data = self._get_service_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [service_data.json_response_v3['service']]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [service_data.json_response_v3['service']] + }, + ) + ] + ) services = self.cloud.list_services() self.assertThat(len(services), matchers.Equals(1)) - self.assertThat(services[0].id, - matchers.Equals(service_data.service_id)) - self.assertThat(services[0].name, - matchers.Equals(service_data.service_name)) - self.assertThat(services[0].type, - matchers.Equals(service_data.service_type)) + self.assertThat( + services[0].id, matchers.Equals(service_data.service_id) + ) + self.assertThat( + services[0].name, matchers.Equals(service_data.service_name) + ) + self.assertThat( + services[0].type, matchers.Equals(service_data.service_type) + ) self.assert_calls() def test_get_service(self): service_data = self._get_service_data() service2_data = self._get_service_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=400), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + dict(method='GET', uri=self.get_mock_url(), status_code=400), + ] + ) # Search by id service = self.cloud.get_service(name_or_id=service_data.service_id) self.assertThat(service.id, matchers.Equals(service_data.service_id)) # Search by name - service = self.cloud.get_service( - name_or_id=service_data.service_name) + service = self.cloud.get_service(name_or_id=service_data.service_name) # test we are getting exactly 1 element self.assertThat(service.id, matchers.Equals(service_data.service_id)) @@ -154,55 +203,85 @@ class CloudServices(base.TestCase): # Multiple matches # test we are getting an Exception - self.assertRaises(OpenStackCloudException, self.cloud.get_service, - name_or_id=None, filters={'type': 'type2'}) + self.assertRaises( + OpenStackCloudException, + self.cloud.get_service, + name_or_id=None, + filters={'type': 'type2'}, + ) self.assert_calls() def test_search_services(self): service_data = self._get_service_data() service2_data = self._get_service_data(type=service_data.service_type) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service'], - service2_data.json_response_v3['service']]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [ + service_data.json_response_v3['service'], + service2_data.json_response_v3['service'], + ] + }, + ), + ] + ) # Search by id services = self.cloud.search_services( - name_or_id=service_data.service_id) + name_or_id=service_data.service_id + ) # test we are getting exactly 1 element self.assertThat(len(services), matchers.Equals(1)) - self.assertThat(services[0].id, - matchers.Equals(service_data.service_id)) + self.assertThat( + services[0].id, matchers.Equals(service_data.service_id) + ) # Search by name services = self.cloud.search_services( - name_or_id=service_data.service_name) + name_or_id=service_data.service_name + ) # test we are getting exactly 1 element self.assertThat(len(services), matchers.Equals(1)) - self.assertThat(services[0].name, - matchers.Equals(service_data.service_name)) + self.assertThat( + services[0].name, matchers.Equals(service_data.service_name) + ) # Not found services = self.cloud.search_services(name_or_id='!INVALID!') @@ -210,35 +289,50 @@ class CloudServices(base.TestCase): # Multiple matches services = self.cloud.search_services( - filters={'type': service_data.service_type}) + filters={'type': service_data.service_type} + ) # test we are getting exactly 2 elements self.assertThat(len(services), matchers.Equals(2)) - self.assertThat(services[0].id, - matchers.Equals(service_data.service_id)) - self.assertThat(services[1].id, - matchers.Equals(service2_data.service_id)) + self.assertThat( + services[0].id, matchers.Equals(service_data.service_id) + ) + self.assertThat( + services[1].id, matchers.Equals(service2_data.service_id) + ) self.assert_calls() def test_delete_service(self): service_data = self._get_service_data() - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service']]}), - dict(method='DELETE', - uri=self.get_mock_url(append=[service_data.service_id]), - status_code=204), - dict(method='GET', - uri=self.get_mock_url(), - status_code=200, - json={'services': [ - service_data.json_response_v3['service']]}), - dict(method='DELETE', - uri=self.get_mock_url(append=[service_data.service_id]), - status_code=204) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [service_data.json_response_v3['service']] + }, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[service_data.service_id]), + status_code=204, + ), + dict( + method='GET', + uri=self.get_mock_url(), + status_code=200, + json={ + 'services': [service_data.json_response_v3['service']] + }, + ), + dict( + method='DELETE', + uri=self.get_mock_url(append=[service_data.service_id]), + status_code=204, + ), + ] + ) # Delete by name self.cloud.delete_service(name_or_id=service_data.service_name) diff --git a/openstack/tests/unit/cloud/test_shade.py b/openstack/tests/unit/cloud/test_shade.py index 91a8377ff..9bc80f332 100644 --- a/openstack/tests/unit/cloud/test_shade.py +++ b/openstack/tests/unit/cloud/test_shade.py @@ -34,7 +34,6 @@ RANGE_DATA = [ class TestShade(base.TestCase): - def setUp(self): # This set of tests are not testing neutron, they're testing # rebuilding servers, but we do several network calls in service @@ -60,31 +59,35 @@ class TestShade(base.TestCase): self.cloud.config.config['dns_endpoint_override'] = dns_override self.assertEqual( 'https://compute.example.com/v2.1/', - self.cloud.endpoint_for('compute')) + self.cloud.endpoint_for('compute'), + ) self.assertEqual( 'https://internal.compute.example.com/v2.1/', - self.cloud.endpoint_for('compute', interface='internal')) + self.cloud.endpoint_for('compute', interface='internal'), + ) self.assertIsNone( - self.cloud.endpoint_for('compute', region_name='unknown-region')) - self.assertEqual( - dns_override, - self.cloud.endpoint_for('dns')) + self.cloud.endpoint_for('compute', region_name='unknown-region') + ) + self.assertEqual(dns_override, self.cloud.endpoint_for('dns')) def test_connect_as(self): # Do initial auth/catalog steps # This should authenticate a second time, but should not # need a second identity discovery project_name = 'test_project' - self.register_uris([ - self.get_keystone_v3_token(project_name=project_name), - self.get_nova_discovery_mock_dict(), - dict( - method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': []}, - ), - ]) + self.register_uris( + [ + self.get_keystone_v3_token(project_name=project_name), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': []}, + ), + ] + ) c2 = self.cloud.connect_as(project_name=project_name) self.assertEqual(c2.list_servers(), []) @@ -95,16 +98,19 @@ class TestShade(base.TestCase): # This should authenticate a second time, but should not # need a second identity discovery project_name = 'test_project' - self.register_uris([ - self.get_keystone_v3_token(project_name=project_name), - self.get_nova_discovery_mock_dict(), - dict( - method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': []}, - ), - ]) + self.register_uris( + [ + self.get_keystone_v3_token(project_name=project_name), + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': []}, + ), + ] + ) with self.cloud.connect_as(project_name=project_name) as c2: self.assertEqual(c2.list_servers(), []) @@ -126,17 +132,21 @@ class TestShade(base.TestCase): def test_global_request_id(self): request_id = uuid.uuid4().hex - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict( - method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': []}, - validate=dict( - headers={'X-Openstack-Request-Id': request_id}), - ), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': []}, + validate=dict( + headers={'X-Openstack-Request-Id': request_id} + ), + ), + ] + ) cloud2 = self.cloud.global_request(request_id) self.assertEqual([], cloud2.list_servers()) @@ -145,17 +155,21 @@ class TestShade(base.TestCase): def test_global_request_id_context(self): request_id = uuid.uuid4().hex - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict( - method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': []}, - validate=dict( - headers={'X-Openstack-Request-Id': request_id}), - ), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': []}, + validate=dict( + headers={'X-Openstack-Request-Id': request_id} + ), + ), + ] + ) with self.cloud.global_request(request_id) as c2: self.assertEqual([], c2.list_servers()) @@ -166,13 +180,18 @@ class TestShade(base.TestCase): server1 = fakes.make_fake_server('123', 'mickey') server2 = fakes.make_fake_server('345', 'mouse') - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [server1, server2]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [server1, server2]}, + ), + ] + ) r = self.cloud.get_server('mickey') self.assertIsNotNone(r) @@ -181,13 +200,18 @@ class TestShade(base.TestCase): self.assert_calls() def test_get_server_not_found(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': []}, + ), + ] + ) r = self.cloud.get_server('doesNotExist') self.assertIsNone(r) @@ -195,16 +219,20 @@ class TestShade(base.TestCase): self.assert_calls() def test_list_servers_exception(self): - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - status_code=400) - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + status_code=400, + ), + ] + ) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.list_servers) + self.assertRaises(exc.OpenStackCloudException, self.cloud.list_servers) self.assert_calls() @@ -218,13 +246,18 @@ class TestShade(base.TestCase): server_id = str(uuid.uuid4()) server_name = self.getUniqueString('name') fake_server = fakes.make_fake_server(server_id, server_name) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [fake_server]}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + ] + ) r = self.cloud.list_servers() @@ -243,30 +276,31 @@ class TestShade(base.TestCase): "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:b4:a3:07", "version": 4, "addr": "10.4.0.13", - "OS-EXT-IPS:type": "fixed" - }, { + "OS-EXT-IPS:type": "fixed", + }, + { "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:b4:a3:07", "version": 4, "addr": "89.40.216.229", - "OS-EXT-IPS:type": "floating" - }]}, + "OS-EXT-IPS:type": "floating", + }, + ] + }, "links": [ + {"href": "http://example.com/images/95e4c4", "rel": "self"}, { "href": "http://example.com/images/95e4c4", - "rel": "self" - }, { - "href": "http://example.com/images/95e4c4", - "rel": "bookmark" - } + "rel": "bookmark", + }, ], "image": { "id": "95e4c449-8abf-486e-97d9-dc3f82417d2d", "links": [ { "href": "http://example.com/images/95e4c4", - "rel": "bookmark" + "rel": "bookmark", } - ] + ], }, "OS-EXT-STS:vm_state": "active", "OS-SRV-USG:launched_at": "2018-03-01T02:44:50.000000", @@ -275,9 +309,9 @@ class TestShade(base.TestCase): "links": [ { "href": "http://example.com/flavors/95e4c4", - "rel": "bookmark" + "rel": "bookmark", } - ] + ], }, "id": "97fe35e9-756a-41a2-960a-1d057d2c9ee4", "security_groups": [{"name": "default"}], @@ -298,55 +332,59 @@ class TestShade(base.TestCase): "created": "2018-03-01T02:44:46Z", "tenant_id": "65222a4d09ea4c68934fa1028c77f394", "os-extended-volumes:volumes_attached": [], - "config_drive": "" + "config_drive": "", + } + fake_networks = { + "networks": [ + { + "status": "ACTIVE", + "router:external": True, + "availability_zone_hints": [], + "availability_zones": ["nova"], + "description": None, + "subnets": [ + "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", + "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", + "fc541f48-fc7f-48c0-a063-18de6ee7bdd7", + ], + "shared": False, + "tenant_id": "a564613210ee43708b8a7fc6274ebd63", + "tags": [], + "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", # noqa: E501 + "mtu": 1550, + "is_default": False, + "admin_state_up": True, + "revision_number": 0, + "ipv4_address_scope": None, + "port_security_enabled": True, + "project_id": "a564613210ee43708b8a7fc6274ebd63", + "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", + "name": "ext-net", + }, + { + "status": "ACTIVE", + "router:external": False, + "availability_zone_hints": [], + "availability_zones": ["nova"], + "description": "", + "subnets": ["f0ad1df5-53ee-473f-b86b-3604ea5591e9"], + "shared": False, + "tenant_id": "65222a4d09ea4c68934fa1028c77f394", + "created_at": "2016-10-22T13:46:26Z", + "tags": [], + "ipv6_address_scope": None, + "updated_at": "2016-10-22T13:46:26Z", + "admin_state_up": True, + "mtu": 1500, + "revision_number": 0, + "ipv4_address_scope": None, + "port_security_enabled": True, + "project_id": "65222a4d09ea4c68934fa1028c77f394", + "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", + "name": "private", + }, + ] } - fake_networks = {"networks": [ - { - "status": "ACTIVE", - "router:external": True, - "availability_zone_hints": [], - "availability_zones": ["nova"], - "description": None, - "subnets": [ - "df3e17fa-a4b2-47ae-9015-bc93eb076ba2", - "6b0c3dc9-b0b8-4d87-976a-7f2ebf13e7ec", - "fc541f48-fc7f-48c0-a063-18de6ee7bdd7" - ], - "shared": False, - "tenant_id": "a564613210ee43708b8a7fc6274ebd63", - "tags": [], - "ipv6_address_scope": "9f03124f-89af-483a-b6fd-10f08079db4d", - "mtu": 1550, - "is_default": False, - "admin_state_up": True, - "revision_number": 0, - "ipv4_address_scope": None, - "port_security_enabled": True, - "project_id": "a564613210ee43708b8a7fc6274ebd63", - "id": "0232c17f-2096-49bc-b205-d3dcd9a30ebf", - "name": "ext-net" - }, { - "status": "ACTIVE", - "router:external": False, - "availability_zone_hints": [], - "availability_zones": ["nova"], - "description": "", - "subnets": ["f0ad1df5-53ee-473f-b86b-3604ea5591e9"], - "shared": False, - "tenant_id": "65222a4d09ea4c68934fa1028c77f394", - "created_at": "2016-10-22T13:46:26Z", - "tags": [], - "ipv6_address_scope": None, - "updated_at": "2016-10-22T13:46:26Z", - "admin_state_up": True, - "mtu": 1500, - "revision_number": 0, - "ipv4_address_scope": None, - "port_security_enabled": True, - "project_id": "65222a4d09ea4c68934fa1028c77f394", - "id": "2c9adcb5-c123-4c5a-a2ba-1ad4c4e1481f", - "name": "private" - }]} fake_subnets = { "subnets": [ { @@ -362,10 +400,8 @@ class TestShade(base.TestCase): "gateway_ip": "10.24.4.1", "ipv6_ra_mode": None, "allocation_pools": [ - { - "start": "10.24.4.2", - "end": "10.24.4.254" - }], + {"start": "10.24.4.2", "end": "10.24.4.254"} + ], "host_routes": [], "revision_number": 0, "ip_version": 4, @@ -374,8 +410,9 @@ class TestShade(base.TestCase): "project_id": "65222a4d09ea4c68934fa1028c77f394", "id": "3f0642d9-4644-4dff-af25-bcf64f739698", "subnetpool_id": None, - "name": "foo_subnet" - }, { + "name": "foo_subnet", + }, + { "service_types": [], "description": "", "enable_dhcp": True, @@ -388,10 +425,8 @@ class TestShade(base.TestCase): "gateway_ip": "10.4.0.1", "ipv6_ra_mode": None, "allocation_pools": [ - { - "start": "10.4.0.2", - "end": "10.4.0.200" - }], + {"start": "10.4.0.2", "end": "10.4.0.200"} + ], "host_routes": [], "revision_number": 0, "ip_version": 4, @@ -400,23 +435,36 @@ class TestShade(base.TestCase): "project_id": "65222a4d09ea4c68934fa1028c77f394", "id": "f0ad1df5-53ee-473f-b86b-3604ea5591e9", "subnetpool_id": None, - "name": "private-subnet-ipv4" - }]} - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail']), - json={'servers': [fake_server]}), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json=fake_networks), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json=fake_subnets) - ]) + "name": "private-subnet-ipv4", + }, + ] + } + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', 'detail'] + ), + json={'servers': [fake_server]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json=fake_networks, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json=fake_subnets, + ), + ] + ) r = self.cloud.get_server('97fe35e9-756a-41a2-960a-1d057d2c9ee4') @@ -427,15 +475,22 @@ class TestShade(base.TestCase): def test_list_servers_all_projects(self): '''This test verifies that when list_servers is called with `all_projects=True` that it passes `all_tenants=True` to nova.''' - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['all_tenants=True']), - complete_qs=True, - json={'servers': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['all_tenants=True'], + ), + complete_qs=True, + json={'servers': []}, + ), + ] + ) self.cloud.list_servers(all_projects=True) @@ -444,38 +499,47 @@ class TestShade(base.TestCase): def test_list_servers_filters(self): '''This test verifies that when list_servers is called with `filters` dict that it passes it to nova.''' - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=[ - 'deleted=True', - 'changes-since=2014-12-03T00:00:00Z' - ]), - complete_qs=True, - json={'servers': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=[ + 'deleted=True', + 'changes-since=2014-12-03T00:00:00Z', + ], + ), + complete_qs=True, + json={'servers': []}, + ), + ] + ) - self.cloud.list_servers(filters={ - 'deleted': True, - 'changes-since': '2014-12-03T00:00:00Z' - }) + self.cloud.list_servers( + filters={'deleted': True, 'changes-since': '2014-12-03T00:00:00Z'} + ) self.assert_calls() def test_iterate_timeout_bad_wait(self): with testtools.ExpectedException( - exc.OpenStackCloudException, - "Wait value must be an int or float value."): + exc.OpenStackCloudException, + "Wait value must be an int or float value.", + ): for count in utils.iterate_timeout( - 1, "test_iterate_timeout_bad_wait", wait="timeishard"): + 1, "test_iterate_timeout_bad_wait", wait="timeishard" + ): pass @mock.patch('time.sleep') def test_iterate_timeout_str_wait(self, mock_sleep): iter = utils.iterate_timeout( - 10, "test_iterate_timeout_str_wait", wait="1.6") + 10, "test_iterate_timeout_str_wait", wait="1.6" + ) next(iter) next(iter) mock_sleep.assert_called_with(1.6) @@ -483,7 +547,8 @@ class TestShade(base.TestCase): @mock.patch('time.sleep') def test_iterate_timeout_int_wait(self, mock_sleep): iter = utils.iterate_timeout( - 10, "test_iterate_timeout_int_wait", wait=1) + 10, "test_iterate_timeout_int_wait", wait=1 + ) next(iter) next(iter) mock_sleep.assert_called_with(1.0) @@ -491,9 +556,7 @@ class TestShade(base.TestCase): @mock.patch('time.sleep') def test_iterate_timeout_timeout(self, mock_sleep): message = "timeout test" - with testtools.ExpectedException( - exc.OpenStackCloudTimeout, - message): + with testtools.ExpectedException(exc.OpenStackCloudTimeout, message): for count in utils.iterate_timeout(0.1, message, wait=1): pass mock_sleep.assert_called_with(1.0) @@ -506,7 +569,7 @@ class TestShade(base.TestCase): "links": [], "namespace": "http://openstack.org/compute/ext/fake_xml", "alias": "NMN", - "description": "Multiple network support." + "description": "Multiple network support.", }, { "updated": "2014-12-03T00:00:00Z", @@ -514,30 +577,40 @@ class TestShade(base.TestCase): "links": [], "namespace": "http://openstack.org/compute/ext/fake_xml", "alias": "OS-DCF", - "description": "Disk Management Extension." + "description": "Disk Management Extension.", }, ] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/extensions'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=dict(extensions=body)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/extensions'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=dict(extensions=body), + ) + ] + ) extensions = self.cloud._nova_extensions() self.assertEqual(set(['NMN', 'OS-DCF']), extensions) self.assert_calls() def test__nova_extensions_fails(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/extensions'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/extensions'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + status_code=404, + ), + ] + ) self.assertRaises( - exceptions.ResourceNotFound, - self.cloud._nova_extensions) + exceptions.ResourceNotFound, self.cloud._nova_extensions + ) self.assert_calls() @@ -549,7 +622,7 @@ class TestShade(base.TestCase): "links": [], "namespace": "http://openstack.org/compute/ext/fake_xml", "alias": "NMN", - "description": "Multiple network support." + "description": "Multiple network support.", }, { "updated": "2014-12-03T00:00:00Z", @@ -557,15 +630,20 @@ class TestShade(base.TestCase): "links": [], "namespace": "http://openstack.org/compute/ext/fake_xml", "alias": "OS-DCF", - "description": "Disk Management Extension." + "description": "Disk Management Extension.", }, ] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/extensions'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=dict(extensions=body)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/extensions'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=dict(extensions=body), + ) + ] + ) self.assertTrue(self.cloud._has_nova_extension('NMN')) self.assert_calls() @@ -578,7 +656,7 @@ class TestShade(base.TestCase): "links": [], "namespace": "http://openstack.org/compute/ext/fake_xml", "alias": "NMN", - "description": "Multiple network support." + "description": "Multiple network support.", }, { "updated": "2014-12-03T00:00:00Z", @@ -586,15 +664,20 @@ class TestShade(base.TestCase): "links": [], "namespace": "http://openstack.org/compute/ext/fake_xml", "alias": "OS-DCF", - "description": "Disk Management Extension." + "description": "Disk Management Extension.", }, ] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/extensions'.format( - endpoint=fakes.COMPUTE_ENDPOINT), - json=dict(extensions=body)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/extensions'.format( + endpoint=fakes.COMPUTE_ENDPOINT + ), + json=dict(extensions=body), + ) + ] + ) self.assertFalse(self.cloud._has_nova_extension('invalid')) self.assert_calls() @@ -606,38 +689,45 @@ class TestShade(base.TestCase): "name": "Distributed Virtual Router", "links": [], "alias": "dvr", - "description": - "Enables configuration of Distributed Virtual Routers." + "description": "Enables configuration of Distributed Virtual Routers.", # noqa: E501 }, { "updated": "2013-07-23T10:00:00-00:00", "name": "Allowed Address Pairs", "links": [], "alias": "allowed-address-pairs", - "description": "Provides allowed address pairs" + "description": "Provides allowed address pairs", }, ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json=dict(extensions=body)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json=dict(extensions=body), + ) + ] + ) extensions = self.cloud._neutron_extensions() self.assertEqual(set(['dvr', 'allowed-address-pairs']), extensions) self.assert_calls() def test__neutron_extensions_fails(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - status_code=404) - ]) - with testtools.ExpectedException( - exceptions.ResourceNotFound - ): + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + status_code=404, + ) + ] + ) + with testtools.ExpectedException(exceptions.ResourceNotFound): self.cloud._neutron_extensions() self.assert_calls() @@ -649,23 +739,27 @@ class TestShade(base.TestCase): "name": "Distributed Virtual Router", "links": [], "alias": "dvr", - "description": - "Enables configuration of Distributed Virtual Routers." + "description": "Enables configuration of Distributed Virtual Routers.", # noqa: E501 }, { "updated": "2013-07-23T10:00:00-00:00", "name": "Allowed Address Pairs", "links": [], "alias": "allowed-address-pairs", - "description": "Provides allowed address pairs" + "description": "Provides allowed address pairs", }, ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json=dict(extensions=body)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json=dict(extensions=body), + ) + ] + ) self.assertTrue(self.cloud._has_neutron_extension('dvr')) self.assert_calls() @@ -676,23 +770,27 @@ class TestShade(base.TestCase): "name": "Distributed Virtual Router", "links": [], "alias": "dvr", - "description": - "Enables configuration of Distributed Virtual Routers." + "description": "Enables configuration of Distributed Virtual Routers.", # noqa: E501 }, { "updated": "2013-07-23T10:00:00-00:00", "name": "Allowed Address Pairs", "links": [], "alias": "allowed-address-pairs", - "description": "Provides allowed address pairs" + "description": "Provides allowed address pairs", }, ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'extensions']), - json=dict(extensions=body)) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions'] + ), + json=dict(extensions=body), + ) + ] + ) self.assertFalse(self.cloud._has_neutron_extension('invalid')) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_shared_file_system.py b/openstack/tests/unit/cloud/test_shared_file_system.py index 51f3a25df..2463b5fe8 100644 --- a/openstack/tests/unit/cloud/test_shared_file_system.py +++ b/openstack/tests/unit/cloud/test_shared_file_system.py @@ -25,20 +25,24 @@ MANILA_AZ_DICT = { class TestSharedFileSystem(base.TestCase): - def setUp(self): super(TestSharedFileSystem, self).setUp() self.use_manila() def test_list_availability_zones(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'shared-file-system', - 'public', - append=['v2', 'availability-zones']), - json={'availability_zones': [MANILA_AZ_DICT]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'shared-file-system', + 'public', + append=['v2', 'availability-zones'], + ), + json={'availability_zones': [MANILA_AZ_DICT]}, + ), + ] + ) az_list = self.cloud.list_share_availability_zones() self.assertEqual(len(az_list), 1) self.assertEqual(MANILA_AZ_DICT['id'], az_list[0].id) diff --git a/openstack/tests/unit/cloud/test_stack.py b/openstack/tests/unit/cloud/test_stack.py index 827a465ad..85297d0a7 100644 --- a/openstack/tests/unit/cloud/test_stack.py +++ b/openstack/tests/unit/cloud/test_stack.py @@ -21,7 +21,6 @@ from openstack.tests.unit import base class TestStack(base.TestCase): - def setUp(self): super(TestStack, self).setUp() self.stack_id = self.getUniqueString('id') @@ -32,21 +31,27 @@ class TestStack(base.TestCase): def _compare_stacks(self, exp, real): self.assertDictEqual( stack.Stack(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_list_stacks(self): fake_stacks = [ self.stack, fakes.make_fake_stack( - self.getUniqueString('id'), - self.getUniqueString('name')) + self.getUniqueString('id'), self.getUniqueString('name') + ), ] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - json={"stacks": fake_stacks}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + json={"stacks": fake_stacks}, + ), + ] + ) stacks = self.cloud.list_stacks() [self._compare_stacks(b, a) for a, b in zip(stacks, fake_stacks)] @@ -56,32 +61,43 @@ class TestStack(base.TestCase): fake_stacks = [ self.stack, fakes.make_fake_stack( - self.getUniqueString('id'), - self.getUniqueString('name')) + self.getUniqueString('id'), self.getUniqueString('name') + ), ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'orchestration', 'public', - append=['stacks'], - qs_elements=['name=a', 'status=b'], - ), - json={"stacks": fake_stacks}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'orchestration', + 'public', + append=['stacks'], + qs_elements=['name=a', 'status=b'], + ), + json={"stacks": fake_stacks}, + ), + ] + ) stacks = self.cloud.list_stacks(name='a', status='b') [self._compare_stacks(b, a) for a, b in zip(stacks, fake_stacks)] self.assert_calls() def test_list_stacks_exception(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + status_code=404, + ) + ] + ) with testtools.ExpectedException( - openstack.cloud.OpenStackCloudURINotFound): + openstack.cloud.OpenStackCloudURINotFound + ): self.cloud.list_stacks() self.assert_calls() @@ -89,15 +105,20 @@ class TestStack(base.TestCase): fake_stacks = [ self.stack, fakes.make_fake_stack( - self.getUniqueString('id'), - self.getUniqueString('name')) + self.getUniqueString('id'), self.getUniqueString('name') + ), ] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - json={"stacks": fake_stacks}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + json={"stacks": fake_stacks}, + ), + ] + ) stacks = self.cloud.search_stacks() [self._compare_stacks(b, a) for a, b in zip(stacks, fake_stacks)] self.assert_calls() @@ -108,205 +129,322 @@ class TestStack(base.TestCase): fakes.make_fake_stack( self.getUniqueString('id'), self.getUniqueString('name'), - status='CREATE_FAILED') + status='CREATE_FAILED', + ), ] - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - json={"stacks": fake_stacks}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + json={"stacks": fake_stacks}, + ), + ] + ) filters = {'status': 'FAILED'} stacks = self.cloud.search_stacks(filters=filters) [self._compare_stacks(b, a) for a, b in zip(stacks, fake_stacks)] self.assert_calls() def test_search_stacks_exception(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + status_code=404, + ) + ] + ) with testtools.ExpectedException( - openstack.cloud.OpenStackCloudURINotFound): + openstack.cloud.OpenStackCloudURINotFound + ): self.cloud.search_stacks() def test_delete_stack(self): resolve = 'resolve_outputs=False' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{name}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, resolve=resolve), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, - resolve=resolve))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, resolve=resolve), - json={"stack": self.stack}), - dict(method='DELETE', - uri='{endpoint}/stacks/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id)), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{name}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + resolve=resolve, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( # noqa: E501 + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ), + json={"stack": self.stack}, + ), + dict( + method='DELETE', + uri='{endpoint}/stacks/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, id=self.stack_id + ), + ), + ] + ) self.assertTrue(self.cloud.delete_stack(self.stack_name)) self.assert_calls() def test_delete_stack_not_found(self): resolve = 'resolve_outputs=False' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/stack_name?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, resolve=resolve), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/stack_name?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, resolve=resolve + ), + status_code=404, + ), + ] + ) self.assertFalse(self.cloud.delete_stack('stack_name')) self.assert_calls() def test_delete_stack_exception(self): resolve = 'resolve_outputs=False' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, resolve=resolve), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, - resolve=resolve))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, resolve=resolve), - json={"stack": self.stack}), - dict(method='DELETE', - uri='{endpoint}/stacks/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id), - status_code=400, - reason="ouch"), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + resolve=resolve, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( # noqa: E501 + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ), + json={"stack": self.stack}, + ), + dict( + method='DELETE', + uri='{endpoint}/stacks/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, id=self.stack_id + ), + status_code=400, + reason="ouch", + ), + ] + ) with testtools.ExpectedException( - openstack.cloud.OpenStackCloudBadRequest): + openstack.cloud.OpenStackCloudBadRequest + ): self.cloud.delete_stack(self.stack_id) self.assert_calls() def test_delete_stack_by_name_wait(self): marker_event = fakes.make_fake_stack_event( - self.stack_id, self.stack_name, status='CREATE_COMPLETE', - resource_name='name') + self.stack_id, + self.stack_name, + status='CREATE_COMPLETE', + resource_name='name', + ) marker_qs = 'marker={e_id}&sort_dir=asc'.format( - e_id=marker_event['id']) + e_id=marker_event['id'] + ) resolve = 'resolve_outputs=False' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{name}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, - resolve=resolve), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, - resolve=resolve))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, resolve=resolve), - json={"stack": self.stack}), - dict(method='GET', - uri='{endpoint}/stacks/{name}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, - qs='limit=1&sort_dir=desc'), - complete_qs=True, - json={"events": [marker_event]}), - dict(method='DELETE', - uri='{endpoint}/stacks/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id)), - dict(method='GET', - uri='{endpoint}/stacks/{name}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, - qs=marker_qs), - complete_qs=True, - json={"events": [ - fakes.make_fake_stack_event( - self.stack_id, self.stack_name, - status='DELETE_COMPLETE', resource_name='name'), - ]}), - dict(method='GET', - uri='{endpoint}/stacks/{name}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, resolve=resolve), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{name}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + resolve=resolve, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( # noqa: E501 + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ), + json={"stack": self.stack}, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + qs='limit=1&sort_dir=desc', + ), + complete_qs=True, + json={"events": [marker_event]}, + ), + dict( + method='DELETE', + uri='{endpoint}/stacks/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, id=self.stack_id + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + qs=marker_qs, + ), + complete_qs=True, + json={ + "events": [ + fakes.make_fake_stack_event( + self.stack_id, + self.stack_name, + status='DELETE_COMPLETE', + resource_name='name', + ), + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + resolve=resolve, + ), + status_code=404, + ), + ] + ) self.assertTrue(self.cloud.delete_stack(self.stack_name, wait=True)) self.assert_calls() def test_delete_stack_by_id_wait(self): marker_event = fakes.make_fake_stack_event( - self.stack_id, self.stack_name, status='CREATE_COMPLETE', - resource_name='name') + self.stack_id, + self.stack_name, + status='CREATE_COMPLETE', + resource_name='name', + ) marker_qs = 'marker={e_id}&sort_dir=asc'.format( - e_id=marker_event['id']) + e_id=marker_event['id'] + ) resolve = 'resolve_outputs=False' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, - resolve=resolve), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, - resolve=resolve))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, resolve=resolve), - json={"stack": self.stack}), - dict(method='GET', - uri='{endpoint}/stacks/{id}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, - qs='limit=1&sort_dir=desc'), - complete_qs=True, - json={"events": [marker_event]}), - dict(method='DELETE', - uri='{endpoint}/stacks/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id)), - dict(method='GET', - uri='{endpoint}/stacks/{id}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, - qs=marker_qs), - complete_qs=True, - json={"events": [ - fakes.make_fake_stack_event( - self.stack_id, self.stack_name, - status='DELETE_COMPLETE'), - ]}), - dict(method='GET', - uri='{endpoint}/stacks/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, resolve=resolve), - status_code=404), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + resolve=resolve, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( # noqa: E501 + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ), + json={"stack": self.stack}, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{id}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + qs='limit=1&sort_dir=desc', + ), + complete_qs=True, + json={"events": [marker_event]}, + ), + dict( + method='DELETE', + uri='{endpoint}/stacks/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, id=self.stack_id + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{id}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + qs=marker_qs, + ), + complete_qs=True, + json={ + "events": [ + fakes.make_fake_stack_event( + self.stack_id, + self.stack_name, + status='DELETE_COMPLETE', + ), + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + resolve=resolve, + ), + status_code=404, + ), + ] + ) self.assertTrue(self.cloud.delete_stack(self.stack_id, wait=True)) self.assert_calls() @@ -315,67 +453,106 @@ class TestStack(base.TestCase): failed_stack = self.stack.copy() failed_stack['stack_status'] = 'DELETE_FAILED' marker_event = fakes.make_fake_stack_event( - self.stack_id, self.stack_name, status='CREATE_COMPLETE') + self.stack_id, self.stack_name, status='CREATE_COMPLETE' + ) marker_qs = 'marker={e_id}&sort_dir=asc'.format( - e_id=marker_event['id']) + e_id=marker_event['id'] + ) resolve = 'resolve_outputs=False' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, resolve=resolve), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, - resolve=resolve))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, resolve=resolve), - json={"stack": self.stack}), - dict(method='GET', - uri='{endpoint}/stacks/{id}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, - qs='limit=1&sort_dir=desc'), - complete_qs=True, - json={"events": [marker_event]}), - dict(method='DELETE', - uri='{endpoint}/stacks/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id)), - dict(method='GET', - uri='{endpoint}/stacks/{id}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, - qs=marker_qs), - complete_qs=True, - json={"events": [ - fakes.make_fake_stack_event( - self.stack_id, self.stack_name, - status='DELETE_COMPLETE'), - ]}), - dict(method='GET', - uri='{endpoint}/stacks/{id}?resolve_outputs=False'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, - resolve=resolve))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name, resolve=resolve), - json={"stack": failed_stack}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + resolve=resolve, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( # noqa: E501 + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ), + json={"stack": self.stack}, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{id}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + qs='limit=1&sort_dir=desc', + ), + complete_qs=True, + json={"events": [marker_event]}, + ), + dict( + method='DELETE', + uri='{endpoint}/stacks/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, id=self.stack_id + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{id}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + qs=marker_qs, + ), + complete_qs=True, + json={ + "events": [ + fakes.make_fake_stack_event( + self.stack_id, + self.stack_name, + status='DELETE_COMPLETE', + ), + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{id}?resolve_outputs=False'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, id=self.stack_id + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}?{resolve}'.format( # noqa: E501 + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}?{resolve}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + resolve=resolve, + ), + json={"stack": failed_stack}, + ), + ] + ) with testtools.ExpectedException( - openstack.cloud.OpenStackCloudException): + openstack.cloud.OpenStackCloudException + ): self.cloud.delete_stack(self.stack_id, wait=True) self.assert_calls() @@ -384,42 +561,56 @@ class TestStack(base.TestCase): test_template = tempfile.NamedTemporaryFile(delete=False) test_template.write(fakes.FAKE_TEMPLATE.encode('utf-8')) test_template.close() - self.register_uris([ - dict( - method='POST', uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - json={"stack": self.stack}, - validate=dict( - json={ - 'disable_rollback': False, - 'parameters': {}, - 'stack_name': self.stack_name, - 'tags': self.stack_tag, - 'template': fakes.FAKE_TEMPLATE_CONTENT, - 'timeout_mins': 60} - )), - dict( - method='GET', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}'.format( + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + json={"stack": self.stack}, + validate=dict( + json={ + 'disable_rollback': False, + 'parameters': {}, + 'stack_name': self.stack_name, + 'tags': self.stack_tag, + 'template': fakes.FAKE_TEMPLATE_CONTENT, + 'timeout_mins': 60, + } + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}'.format( endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name))), - dict( - method='GET', - uri='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name), - json={"stack": self.stack}), - ]) + name=self.stack_name, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ), + json={"stack": self.stack}, + ), + ] + ) self.cloud.create_stack( self.stack_name, tags=self.stack_tag, - template_file=test_template.name + template_file=test_template.name, ) self.assert_calls() @@ -430,53 +621,74 @@ class TestStack(base.TestCase): test_template.write(fakes.FAKE_TEMPLATE.encode('utf-8')) test_template.close() - self.register_uris([ - dict( - method='POST', uri='{endpoint}/stacks'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT), - json={"stack": self.stack}, - validate=dict( - json={ - 'disable_rollback': False, - 'parameters': {}, - 'stack_name': self.stack_name, - 'tags': self.stack_tag, - 'template': fakes.FAKE_TEMPLATE_CONTENT, - 'timeout_mins': 60} - )), - dict( - method='GET', - uri='{endpoint}/stacks/{name}/events?sort_dir=asc'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - json={"events": [ - fakes.make_fake_stack_event( - self.stack_id, self.stack_name, - status='CREATE_COMPLETE', - resource_name='name'), - ]}), - dict( - method='GET', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}'.format( + self.register_uris( + [ + dict( + method='POST', + uri='{endpoint}/stacks'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT + ), + json={"stack": self.stack}, + validate=dict( + json={ + 'disable_rollback': False, + 'parameters': {}, + 'stack_name': self.stack_name, + 'tags': self.stack_tag, + 'template': fakes.FAKE_TEMPLATE_CONTENT, + 'timeout_mins': 60, + } + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/events?sort_dir=asc'.format( endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name))), - dict( - method='GET', - uri='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name), - json={"stack": self.stack}), - ]) + name=self.stack_name, + ), + json={ + "events": [ + fakes.make_fake_stack_event( + self.stack_id, + self.stack_name, + status='CREATE_COMPLETE', + resource_name='name', + ), + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ), + json={"stack": self.stack}, + ), + ] + ) self.cloud.create_stack( self.stack_name, tags=self.stack_tag, template_file=test_template.name, - wait=True) + wait=True, + ) self.assert_calls() @@ -485,129 +697,183 @@ class TestStack(base.TestCase): test_template.write(fakes.FAKE_TEMPLATE.encode('utf-8')) test_template.close() - self.register_uris([ - dict( - method='PUT', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - validate=dict( - json={ - 'disable_rollback': False, - 'parameters': {}, - 'tags': self.stack_tag, - 'template': fakes.FAKE_TEMPLATE_CONTENT, - 'timeout_mins': 60}), - json={}), - dict( - method='GET', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}'.format( + self.register_uris( + [ + dict( + method='PUT', + uri='{endpoint}/stacks/{name}'.format( endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name))), - dict( - method='GET', - uri='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name), - json={"stack": self.stack}), - ]) + name=self.stack_name, + ), + validate=dict( + json={ + 'disable_rollback': False, + 'parameters': {}, + 'tags': self.stack_tag, + 'template': fakes.FAKE_TEMPLATE_CONTENT, + 'timeout_mins': 60, + } + ), + json={}, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ), + json={"stack": self.stack}, + ), + ] + ) self.cloud.update_stack( self.stack_name, tags=self.stack_tag, - template_file=test_template.name) + template_file=test_template.name, + ) self.assert_calls() def test_update_stack_wait(self): marker_event = fakes.make_fake_stack_event( - self.stack_id, self.stack_name, status='CREATE_COMPLETE', - resource_name='name') + self.stack_id, + self.stack_name, + status='CREATE_COMPLETE', + resource_name='name', + ) marker_qs = 'marker={e_id}&sort_dir=asc'.format( - e_id=marker_event['id']) + e_id=marker_event['id'] + ) test_template = tempfile.NamedTemporaryFile(delete=False) test_template.write(fakes.FAKE_TEMPLATE.encode('utf-8')) test_template.close() - self.register_uris([ - dict( - method='GET', - uri='{endpoint}/stacks/{name}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, - qs='limit=1&sort_dir=desc'), - json={"events": [marker_event]}), - dict( - method='PUT', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - validate=dict( - json={ - 'disable_rollback': False, - 'parameters': {}, - 'tags': self.stack_tag, - 'template': fakes.FAKE_TEMPLATE_CONTENT, - 'timeout_mins': 60}), - json={}), - dict( - method='GET', - uri='{endpoint}/stacks/{name}/events?{qs}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name, - qs=marker_qs), - json={"events": [ - fakes.make_fake_stack_event( - self.stack_id, self.stack_name, - status='UPDATE_COMPLETE', - resource_name='name'), - ]}), - dict( - method='GET', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}'.format( + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{name}/events?{qs}'.format( endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name))), - dict( - method='GET', - uri='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name), - json={"stack": self.stack}), - ]) + name=self.stack_name, + qs='limit=1&sort_dir=desc', + ), + json={"events": [marker_event]}, + ), + dict( + method='PUT', + uri='{endpoint}/stacks/{name}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + ), + validate=dict( + json={ + 'disable_rollback': False, + 'parameters': {}, + 'tags': self.stack_tag, + 'template': fakes.FAKE_TEMPLATE_CONTENT, + 'timeout_mins': 60, + } + ), + json={}, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/events?{qs}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + qs=marker_qs, + ), + json={ + "events": [ + fakes.make_fake_stack_event( + self.stack_id, + self.stack_name, + status='UPDATE_COMPLETE', + resource_name='name', + ), + ] + }, + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ), + json={"stack": self.stack}, + ), + ] + ) self.cloud.update_stack( self.stack_name, tags=self.stack_tag, template_file=test_template.name, - wait=True) + wait=True, + ) self.assert_calls() def test_get_stack(self): - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name), - json={"stack": self.stack}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{name}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ), + json={"stack": self.stack}, + ), + ] + ) res = self.cloud.get_stack(self.stack_name) self.assertIsNotNone(res) @@ -620,22 +886,34 @@ class TestStack(base.TestCase): def test_get_stack_in_progress(self): in_progress = self.stack.copy() in_progress['stack_status'] = 'CREATE_IN_PROGRESS' - self.register_uris([ - dict(method='GET', - uri='{endpoint}/stacks/{name}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - name=self.stack_name), - status_code=302, - headers=dict( - location='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name))), - dict(method='GET', - uri='{endpoint}/stacks/{name}/{id}'.format( - endpoint=fakes.ORCHESTRATION_ENDPOINT, - id=self.stack_id, name=self.stack_name), - json={"stack": in_progress}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri='{endpoint}/stacks/{name}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + name=self.stack_name, + ), + status_code=302, + headers=dict( + location='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ) + ), + ), + dict( + method='GET', + uri='{endpoint}/stacks/{name}/{id}'.format( + endpoint=fakes.ORCHESTRATION_ENDPOINT, + id=self.stack_id, + name=self.stack_name, + ), + json={"stack": in_progress}, + ), + ] + ) res = self.cloud.get_stack(self.stack_name) self.assertIsNotNone(res) diff --git a/openstack/tests/unit/cloud/test_subnet.py b/openstack/tests/unit/cloud/test_subnet.py index d6db4939b..fe95f4912 100644 --- a/openstack/tests/unit/cloud/test_subnet.py +++ b/openstack/tests/unit/cloud/test_subnet.py @@ -37,10 +37,9 @@ class TestSubnet(base.TestCase): } mock_subnet_rep = { - 'allocation_pools': [{ - 'start': '192.168.199.2', - 'end': '192.168.199.254' - }], + 'allocation_pools': [ + {'start': '192.168.199.2', 'end': '192.168.199.254'} + ], 'cidr': subnet_cidr, 'created_at': '2017-04-24T20:22:23Z', 'description': '', @@ -58,48 +57,63 @@ class TestSubnet(base.TestCase): 'revision_number': 2, 'service_types': [], 'subnetpool_id': None, - 'tags': [] + 'tags': [], } mock_subnetpool_rep = { 'id': 'f49a1319-423a-4ee6-ba54-1d95a4f6cc68', - 'prefixes': [ - '172.16.0.0/16' - ] + 'prefixes': ['172.16.0.0/16'], } def _compare_subnets(self, exp, real): self.assertDictEqual( _subnet.Subnet(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_get_subnet(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets'], - qs_elements=['name=%s' % self.subnet_name]), - json={'subnets': [self.mock_subnet_rep]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets'], + qs_elements=['name=%s' % self.subnet_name], + ), + json={'subnets': [self.mock_subnet_rep]}, + ), + ] + ) r = self.cloud.get_subnet(self.subnet_name) self.assertIsNotNone(r) self._compare_subnets(self.mock_subnet_rep, r) self.assert_calls() def test_get_subnet_by_id(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', - 'subnets', - self.subnet_id]), - json={'subnet': self.mock_subnet_rep}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json={'subnet': self.mock_subnet_rep}, + ) + ] + ) r = self.cloud.get_subnet_by_id(self.subnet_id) self.assertIsNotNone(r) self._compare_subnets(self.mock_subnet_rep, r) @@ -113,90 +127,138 @@ class TestSubnet(base.TestCase): mock_subnet_rep['allocation_pools'] = pool mock_subnet_rep['dns_nameservers'] = dns mock_subnet_rep['host_routes'] = routes - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [self.mock_network_rep]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnet': mock_subnet_rep}, - validate=dict( - json={'subnet': { - 'cidr': self.subnet_cidr, - 'enable_dhcp': False, - 'ip_version': 4, - 'network_id': self.mock_network_rep['id'], - 'allocation_pools': pool, - 'dns_nameservers': dns, - 'host_routes': routes}})) - ]) - subnet = self.cloud.create_subnet(self.network_name, self.subnet_cidr, - allocation_pools=pool, - dns_nameservers=dns, - host_routes=routes) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [self.mock_network_rep]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnet': mock_subnet_rep}, + validate=dict( + json={ + 'subnet': { + 'cidr': self.subnet_cidr, + 'enable_dhcp': False, + 'ip_version': 4, + 'network_id': self.mock_network_rep['id'], + 'allocation_pools': pool, + 'dns_nameservers': dns, + 'host_routes': routes, + } + } + ), + ), + ] + ) + subnet = self.cloud.create_subnet( + self.network_name, + self.subnet_cidr, + allocation_pools=pool, + dns_nameservers=dns, + host_routes=routes, + ) self._compare_subnets(mock_subnet_rep, subnet) self.assert_calls() def test_create_subnet_string_ip_version(self): '''Allow ip_version as a string''' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [self.mock_network_rep]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnet': self.mock_subnet_rep}, - validate=dict( - json={'subnet': { - 'cidr': self.subnet_cidr, - 'enable_dhcp': False, - 'ip_version': 4, - 'network_id': self.mock_network_rep['id']}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [self.mock_network_rep]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnet': self.mock_subnet_rep}, + validate=dict( + json={ + 'subnet': { + 'cidr': self.subnet_cidr, + 'enable_dhcp': False, + 'ip_version': 4, + 'network_id': self.mock_network_rep['id'], + } + } + ), + ), + ] + ) subnet = self.cloud.create_subnet( - self.network_name, self.subnet_cidr, ip_version='4') + self.network_name, self.subnet_cidr, ip_version='4' + ) self._compare_subnets(self.mock_subnet_rep, subnet) self.assert_calls() def test_create_subnet_bad_ip_version(self): '''String ip_versions must be convertable to int''' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [self.mock_network_rep]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [self.mock_network_rep]}, + ), + ] + ) with testtools.ExpectedException( - exc.OpenStackCloudException, - "ip_version must be an integer" + exc.OpenStackCloudException, "ip_version must be an integer" ): self.cloud.create_subnet( - self.network_name, self.subnet_cidr, ip_version='4x') + self.network_name, self.subnet_cidr, ip_version='4x' + ) self.assert_calls() def test_create_subnet_without_gateway_ip(self): @@ -206,36 +268,56 @@ class TestSubnet(base.TestCase): mock_subnet_rep['allocation_pools'] = pool mock_subnet_rep['dns_nameservers'] = dns mock_subnet_rep['gateway_ip'] = None - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [self.mock_network_rep]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnet': mock_subnet_rep}, - validate=dict( - json={'subnet': { - 'cidr': self.subnet_cidr, - 'enable_dhcp': False, - 'ip_version': 4, - 'network_id': self.mock_network_rep['id'], - 'allocation_pools': pool, - 'gateway_ip': None, - 'dns_nameservers': dns}})) - ]) - subnet = self.cloud.create_subnet(self.network_name, self.subnet_cidr, - allocation_pools=pool, - dns_nameservers=dns, - disable_gateway_ip=True) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [self.mock_network_rep]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnet': mock_subnet_rep}, + validate=dict( + json={ + 'subnet': { + 'cidr': self.subnet_cidr, + 'enable_dhcp': False, + 'ip_version': 4, + 'network_id': self.mock_network_rep['id'], + 'allocation_pools': pool, + 'gateway_ip': None, + 'dns_nameservers': dns, + } + } + ), + ), + ] + ) + subnet = self.cloud.create_subnet( + self.network_name, + self.subnet_cidr, + allocation_pools=pool, + dns_nameservers=dns, + disable_gateway_ip=True, + ) self._compare_subnets(mock_subnet_rep, subnet) self.assert_calls() @@ -247,98 +329,158 @@ class TestSubnet(base.TestCase): mock_subnet_rep['allocation_pools'] = pool mock_subnet_rep['dns_nameservers'] = dns mock_subnet_rep['gateway_ip'] = gateway - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [self.mock_network_rep]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnet': mock_subnet_rep}, - validate=dict( - json={'subnet': { - 'cidr': self.subnet_cidr, - 'enable_dhcp': False, - 'ip_version': 4, - 'network_id': self.mock_network_rep['id'], - 'allocation_pools': pool, - 'gateway_ip': gateway, - 'dns_nameservers': dns}})) - ]) - subnet = self.cloud.create_subnet(self.network_name, self.subnet_cidr, - allocation_pools=pool, - dns_nameservers=dns, - gateway_ip=gateway) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [self.mock_network_rep]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnet': mock_subnet_rep}, + validate=dict( + json={ + 'subnet': { + 'cidr': self.subnet_cidr, + 'enable_dhcp': False, + 'ip_version': 4, + 'network_id': self.mock_network_rep['id'], + 'allocation_pools': pool, + 'gateway_ip': gateway, + 'dns_nameservers': dns, + } + } + ), + ), + ] + ) + subnet = self.cloud.create_subnet( + self.network_name, + self.subnet_cidr, + allocation_pools=pool, + dns_nameservers=dns, + gateway_ip=gateway, + ) self._compare_subnets(mock_subnet_rep, subnet) self.assert_calls() def test_create_subnet_conflict_gw_ops(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', 'kooky']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=kooky']), - json={'networks': [self.mock_network_rep]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', 'kooky'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=kooky'], + ), + json={'networks': [self.mock_network_rep]}, + ), + ] + ) gateway = '192.168.200.3' - self.assertRaises(exc.OpenStackCloudException, - self.cloud.create_subnet, 'kooky', - self.subnet_cidr, gateway_ip=gateway, - disable_gateway_ip=True) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.create_subnet, + 'kooky', + self.subnet_cidr, + gateway_ip=gateway, + disable_gateway_ip=True, + ) self.assert_calls() def test_create_subnet_bad_network(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', 'duck']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=duck']), - json={'networks': [self.mock_network_rep]}), - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.create_subnet, - 'duck', self.subnet_cidr) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', 'duck'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=duck'], + ), + json={'networks': [self.mock_network_rep]}, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.create_subnet, + 'duck', + self.subnet_cidr, + ) self.assert_calls() def test_create_subnet_non_unique_network(self): net1 = dict(id='123', name=self.network_name) net2 = dict(id='456', name=self.network_name) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [net1, net2]}), - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.create_subnet, - self.network_name, self.subnet_cidr) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [net1, net2]}, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.create_subnet, + self.network_name, + self.subnet_cidr, + ) self.assert_calls() def test_create_subnet_from_subnetpool_with_prefixlen(self): @@ -355,138 +497,213 @@ class TestSubnet(base.TestCase): mock_subnet_rep['subnetpool_id'] = self.mock_subnetpool_rep['id'] mock_subnet_rep['cidr'] = self.subnetpool_cidr mock_subnet_rep['id'] = id - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks', self.network_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'networks'], - qs_elements=['name=%s' % self.network_name]), - json={'networks': [self.mock_network_rep]}), - dict(method='POST', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets']), - json={'subnet': mock_subnet_rep}, - validate=dict( - json={'subnet': { - 'enable_dhcp': False, - 'ip_version': 4, - 'network_id': self.mock_network_rep['id'], - 'allocation_pools': pool, - 'dns_nameservers': dns, - 'use_default_subnetpool': True, - 'prefixlen': self.prefix_length, - 'host_routes': routes}})) - ]) - subnet = self.cloud.create_subnet(self.network_name, - allocation_pools=pool, - dns_nameservers=dns, - use_default_subnetpool=True, - prefixlen=self.prefix_length, - host_routes=routes) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks', self.network_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'networks'], + qs_elements=['name=%s' % self.network_name], + ), + json={'networks': [self.mock_network_rep]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'subnets'] + ), + json={'subnet': mock_subnet_rep}, + validate=dict( + json={ + 'subnet': { + 'enable_dhcp': False, + 'ip_version': 4, + 'network_id': self.mock_network_rep['id'], + 'allocation_pools': pool, + 'dns_nameservers': dns, + 'use_default_subnetpool': True, + 'prefixlen': self.prefix_length, + 'host_routes': routes, + } + } + ), + ), + ] + ) + subnet = self.cloud.create_subnet( + self.network_name, + allocation_pools=pool, + dns_nameservers=dns, + use_default_subnetpool=True, + prefixlen=self.prefix_length, + host_routes=routes, + ) mock_subnet_rep.update( - { - 'prefixlen': self.prefix_length, - 'use_default_subnetpool': True - }) + {'prefixlen': self.prefix_length, 'use_default_subnetpool': True} + ) self._compare_subnets(mock_subnet_rep, subnet) self.assert_calls() def test_delete_subnet(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets'], - qs_elements=['name=%s' % self.subnet_name]), - json={'subnets': [self.mock_subnet_rep]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets'], + qs_elements=['name=%s' % self.subnet_name], + ), + json={'subnets': [self.mock_subnet_rep]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_subnet(self.subnet_name)) self.assert_calls() def test_delete_subnet_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', 'goofy']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets'], - qs_elements=['name=goofy']), - json={'subnets': []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', 'goofy'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets'], + qs_elements=['name=goofy'], + ), + json={'subnets': []}, + ), + ] + ) self.assertFalse(self.cloud.delete_subnet('goofy')) self.assert_calls() def test_delete_subnet_multiple_found(self): subnet1 = dict(id='123', name=self.subnet_name) subnet2 = dict(id='456', name=self.subnet_name) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets'], - qs_elements=['name=%s' % self.subnet_name]), - json={'subnets': [subnet1, subnet2]}) - ]) - self.assertRaises(exc.OpenStackCloudException, - self.cloud.delete_subnet, - self.subnet_name) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets'], + qs_elements=['name=%s' % self.subnet_name], + ), + json={'subnets': [subnet1, subnet2]}, + ), + ] + ) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.delete_subnet, + self.subnet_name, + ) self.assert_calls() def test_delete_subnet_using_id(self): subnet1 = dict(id='123', name=self.subnet_name) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'subnets', - subnet1['id']]), - json=subnet1), - dict(method='DELETE', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', subnet1['id']]), - json={}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', subnet1['id']], + ), + json=subnet1, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', subnet1['id']], + ), + json={}, + ), + ] + ) self.assertTrue(self.cloud.delete_subnet(subnet1['id'])) self.assert_calls() def test_update_subnet(self): expected_subnet = copy.copy(self.mock_subnet_rep) expected_subnet['name'] = 'goofy' - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json=self.mock_subnet_rep), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json={'subnet': expected_subnet}, - validate=dict( - json={'subnet': {'name': 'goofy'}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json=self.mock_subnet_rep, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json={'subnet': expected_subnet}, + validate=dict(json={'subnet': {'name': 'goofy'}}), + ), + ] + ) subnet = self.cloud.update_subnet(self.subnet_id, subnet_name='goofy') self._compare_subnets(expected_subnet, subnet) self.assert_calls() @@ -495,20 +712,29 @@ class TestSubnet(base.TestCase): expected_subnet = copy.copy(self.mock_subnet_rep) gateway = '192.168.199.3' expected_subnet['gateway_ip'] = gateway - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json=self.mock_subnet_rep), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json={'subnet': expected_subnet}, - validate=dict( - json={'subnet': {'gateway_ip': gateway}})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json=self.mock_subnet_rep, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json={'subnet': expected_subnet}, + validate=dict(json={'subnet': {'gateway_ip': gateway}}), + ), + ] + ) subnet = self.cloud.update_subnet(self.subnet_id, gateway_ip=gateway) self._compare_subnets(expected_subnet, subnet) self.assert_calls() @@ -516,27 +742,40 @@ class TestSubnet(base.TestCase): def test_update_subnet_disable_gateway_ip(self): expected_subnet = copy.copy(self.mock_subnet_rep) expected_subnet['gateway_ip'] = None - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json=self.mock_subnet_rep), - dict(method='PUT', - uri=self.get_mock_url( - 'network', 'public', - append=['v2.0', 'subnets', self.subnet_id]), - json={'subnet': expected_subnet}, - validate=dict( - json={'subnet': {'gateway_ip': None}})) - ]) - subnet = self.cloud.update_subnet(self.subnet_id, - disable_gateway_ip=True) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json=self.mock_subnet_rep, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'network', + 'public', + append=['v2.0', 'subnets', self.subnet_id], + ), + json={'subnet': expected_subnet}, + validate=dict(json={'subnet': {'gateway_ip': None}}), + ), + ] + ) + subnet = self.cloud.update_subnet( + self.subnet_id, disable_gateway_ip=True + ) self._compare_subnets(expected_subnet, subnet) self.assert_calls() def test_update_subnet_conflict_gw_ops(self): - self.assertRaises(exc.OpenStackCloudException, - self.cloud.update_subnet, - self.subnet_id, gateway_ip="192.168.199.3", - disable_gateway_ip=True) + self.assertRaises( + exc.OpenStackCloudException, + self.cloud.update_subnet, + self.subnet_id, + gateway_ip="192.168.199.3", + disable_gateway_ip=True, + ) diff --git a/openstack/tests/unit/cloud/test_update_server.py b/openstack/tests/unit/cloud/test_update_server.py index 7af7db1d5..88c61b32b 100644 --- a/openstack/tests/unit/cloud/test_update_server.py +++ b/openstack/tests/unit/cloud/test_update_server.py @@ -25,42 +25,60 @@ from openstack.tests.unit import base class TestUpdateServer(base.TestCase): - def setUp(self): super(TestUpdateServer, self).setUp() self.server_id = str(uuid.uuid4()) self.server_name = self.getUniqueString('name') self.updated_server_name = self.getUniqueString('name2') self.fake_server = fakes.make_fake_server( - self.server_id, self.server_name) + self.server_id, self.server_name + ) def test_update_server_with_update_exception(self): """ Test that an exception in the update raises an exception in update_server. """ - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=%s' % self.server_name]), - json={'servers': [self.fake_server]}), - dict(method='PUT', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - status_code=400, - validate=dict( - json={'server': {'name': self.updated_server_name}})), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=%s' % self.server_name], + ), + json={'servers': [self.fake_server]}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + status_code=400, + validate=dict( + json={'server': {'name': self.updated_server_name}} + ), + ), + ] + ) self.assertRaises( - OpenStackCloudException, self.cloud.update_server, - self.server_name, name=self.updated_server_name) + OpenStackCloudException, + self.cloud.update_server, + self.server_name, + name=self.updated_server_name, + ) self.assert_calls() @@ -69,34 +87,55 @@ class TestUpdateServer(base.TestCase): Test that update_server updates the name without raising any exception """ fake_update_server = fakes.make_fake_server( - self.server_id, self.updated_server_name) + self.server_id, self.updated_server_name + ) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', self.server_name]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', 'detail'], - qs_elements=['name=%s' % self.server_name]), - json={'servers': [self.fake_server]}), - dict(method='PUT', - uri=self.get_mock_url( - 'compute', 'public', append=['servers', self.server_id]), - json={'server': fake_update_server}, - validate=dict( - json={'server': {'name': self.updated_server_name}})), - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks']), - json={'networks': []}), - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', self.server_name], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['servers', 'detail'], + qs_elements=['name=%s' % self.server_name], + ), + json={'servers': [self.fake_server]}, + ), + dict( + method='PUT', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', self.server_id] + ), + json={'server': fake_update_server}, + validate=dict( + json={'server': {'name': self.updated_server_name}} + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks'] + ), + json={'networks': []}, + ), + ] + ) self.assertEqual( self.updated_server_name, self.cloud.update_server( - self.server_name, name=self.updated_server_name)['name']) + self.server_name, name=self.updated_server_name + )['name'], + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_usage.py b/openstack/tests/unit/cloud/test_usage.py index 29cea236c..1c13f7ad8 100644 --- a/openstack/tests/unit/cloud/test_usage.py +++ b/openstack/tests/unit/cloud/test_usage.py @@ -18,48 +18,56 @@ from openstack.tests.unit import base class TestUsage(base.TestCase): - def test_get_usage(self): - project = self.mock_for_keystone_projects(project_count=1, - list_get=True)[0] + project = self.mock_for_keystone_projects( + project_count=1, list_get=True + )[0] start = end = datetime.datetime.now() - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['os-simple-tenant-usage', project.project_id], - qs_elements=[ - 'start={now}'.format(now=start.isoformat()), - 'end={now}'.format(now=end.isoformat()), - ]), - json={"tenant_usage": { - "server_usages": [ - { - "ended_at": None, - "flavor": "m1.tiny", - "hours": 1.0, - "instance_id": uuid.uuid4().hex, - "local_gb": 1, - "memory_mb": 512, - "name": "instance-2", - "started_at": "2012-10-08T20:10:44.541277", - "state": "active", - "tenant_id": "6f70656e737461636b20342065766572", - "uptime": 3600, - "vcpus": 1 - } - ], - "start": "2012-10-08T20:10:44.587336", - "stop": "2012-10-08T21:10:44.587336", - "tenant_id": "6f70656e737461636b20342065766572", - "total_hours": 1.0, - "total_local_gb_usage": 1.0, - "total_memory_mb_usage": 512.0, - "total_vcpus_usage": 1.0 - }}) - ]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', + 'public', + append=['os-simple-tenant-usage', project.project_id], + qs_elements=[ + 'start={now}'.format(now=start.isoformat()), + 'end={now}'.format(now=end.isoformat()), + ], + ), + json={ + "tenant_usage": { + "server_usages": [ + { + "ended_at": None, + "flavor": "m1.tiny", + "hours": 1.0, + "instance_id": uuid.uuid4().hex, + "local_gb": 1, + "memory_mb": 512, + "name": "instance-2", + "started_at": "2012-10-08T20:10:44.541277", + "state": "active", + "tenant_id": "6f70656e737461636b20342065766572", # noqa: E501 + "uptime": 3600, + "vcpus": 1, + } + ], + "start": "2012-10-08T20:10:44.587336", + "stop": "2012-10-08T21:10:44.587336", + "tenant_id": "6f70656e737461636b20342065766572", + "total_hours": 1.0, + "total_local_gb_usage": 1.0, + "total_memory_mb_usage": 512.0, + "total_vcpus_usage": 1.0, + } + }, + ), + ] + ) self.cloud.get_compute_usage(project.project_id, start, end) diff --git a/openstack/tests/unit/cloud/test_users.py b/openstack/tests/unit/cloud/test_users.py index 955e93f29..29620591d 100644 --- a/openstack/tests/unit/cloud/test_users.py +++ b/openstack/tests/unit/cloud/test_users.py @@ -19,16 +19,19 @@ from openstack.tests.unit import base class TestUsers(base.TestCase): - - def _get_keystone_mock_url(self, resource, append=None, v3=True, - qs_elements=None): + def _get_keystone_mock_url( + self, resource, append=None, v3=True, qs_elements=None + ): base_url_append = None if v3: base_url_append = 'v3' return self.get_mock_url( - service_type='identity', resource=resource, - append=append, base_url_append=base_url_append, - qs_elements=qs_elements) + service_type='identity', + resource=resource, + append=append, + base_url_append=base_url_append, + qs_elements=qs_elements, + ) def _get_user_list(self, user_data): uri = self._get_keystone_mock_url(resource='users') @@ -40,26 +43,34 @@ class TestUsers(base.TestCase): 'self': uri, 'previous': None, 'next': None, - } + }, } def test_create_user_v3(self): user_data = self._get_user_data( domain_id=uuid.uuid4().hex, - description=self.getUniqueString('description')) + description=self.getUniqueString('description'), + ) - self.register_uris([ - dict(method='POST', - uri=self._get_keystone_mock_url(resource='users'), - status_code=200, json=user_data.json_response, - validate=dict(json=user_data.json_request)), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self._get_keystone_mock_url(resource='users'), + status_code=200, + json=user_data.json_response, + validate=dict(json=user_data.json_request), + ), + ] + ) user = self.cloud.create_user( - name=user_data.name, email=user_data.email, + name=user_data.name, + email=user_data.email, password=user_data.password, description=user_data.description, - domain_id=user_data.domain_id) + domain_id=user_data.domain_id, + ) self.assertEqual(user_data.name, user.name) self.assertEqual(user_data.email, user.email) @@ -68,59 +79,89 @@ class TestUsers(base.TestCase): self.assert_calls() def test_create_user_v3_no_domain(self): - user_data = self._get_user_data(domain_id=uuid.uuid4().hex, - email='test@example.com') + user_data = self._get_user_data( + domain_id=uuid.uuid4().hex, email='test@example.com' + ) with testtools.ExpectedException( - openstack.cloud.OpenStackCloudException, - "User or project creation requires an explicit" - " domain_id argument." + openstack.cloud.OpenStackCloudException, + "User or project creation requires an explicit" + " domain_id argument.", ): self.cloud.create_user( - name=user_data.name, email=user_data.email, - password=user_data.password) + name=user_data.name, + email=user_data.email, + password=user_data.password, + ) def test_delete_user(self): user_data = self._get_user_data(domain_id=uuid.uuid4().hex) user_resource_uri = self._get_keystone_mock_url( - resource='users', append=[user_data.user_id]) + resource='users', append=[user_data.user_id] + ) - self.register_uris([ - dict(method='GET', - uri=self._get_keystone_mock_url( - resource='users', - qs_elements=['name=%s' % user_data.name]), - status_code=200, - json=self._get_user_list(user_data)), - dict(method='DELETE', uri=user_resource_uri, status_code=204)]) + self.register_uris( + [ + dict( + method='GET', + uri=self._get_keystone_mock_url( + resource='users', + qs_elements=['name=%s' % user_data.name], + ), + status_code=200, + json=self._get_user_list(user_data), + ), + dict(method='DELETE', uri=user_resource_uri, status_code=204), + ] + ) self.cloud.delete_user(user_data.name) self.assert_calls() def test_delete_user_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self._get_keystone_mock_url(resource='users'), - status_code=200, json={'users': []})]) + self.register_uris( + [ + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='users'), + status_code=200, + json={'users': []}, + ) + ] + ) self.assertFalse(self.cloud.delete_user(self.getUniqueString())) def test_add_user_to_group(self): user_data = self._get_user_data() group_data = self._get_group_data() - self.register_uris([ - dict(method='GET', - uri=self._get_keystone_mock_url(resource='users'), - status_code=200, - json=self._get_user_list(user_data)), - dict(method='GET', - uri=self._get_keystone_mock_url(resource='groups'), - status_code=200, - json={'groups': [group_data.json_response['group']]}), - dict(method='PUT', - uri=self._get_keystone_mock_url( - resource='groups', - append=[group_data.group_id, 'users', user_data.user_id]), - status_code=200)]) + self.register_uris( + [ + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='users'), + status_code=200, + json=self._get_user_list(user_data), + ), + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='groups'), + status_code=200, + json={'groups': [group_data.json_response['group']]}, + ), + dict( + method='PUT', + uri=self._get_keystone_mock_url( + resource='groups', + append=[ + group_data.group_id, + 'users', + user_data.user_id, + ], + ), + status_code=200, + ), + ] + ) self.cloud.add_user_to_group(user_data.user_id, group_data.group_id) self.assert_calls() @@ -128,43 +169,73 @@ class TestUsers(base.TestCase): user_data = self._get_user_data() group_data = self._get_group_data() - self.register_uris([ - dict(method='GET', - uri=self._get_keystone_mock_url(resource='users'), - status_code=200, - json=self._get_user_list(user_data)), - dict(method='GET', - uri=self._get_keystone_mock_url(resource='groups'), - status_code=200, - json={'groups': [group_data.json_response['group']]}), - dict(method='HEAD', - uri=self._get_keystone_mock_url( - resource='groups', - append=[group_data.group_id, 'users', user_data.user_id]), - status_code=204)]) + self.register_uris( + [ + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='users'), + status_code=200, + json=self._get_user_list(user_data), + ), + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='groups'), + status_code=200, + json={'groups': [group_data.json_response['group']]}, + ), + dict( + method='HEAD', + uri=self._get_keystone_mock_url( + resource='groups', + append=[ + group_data.group_id, + 'users', + user_data.user_id, + ], + ), + status_code=204, + ), + ] + ) - self.assertTrue(self.cloud.is_user_in_group( - user_data.user_id, group_data.group_id)) + self.assertTrue( + self.cloud.is_user_in_group(user_data.user_id, group_data.group_id) + ) self.assert_calls() def test_remove_user_from_group(self): user_data = self._get_user_data() group_data = self._get_group_data() - self.register_uris([ - dict(method='GET', - uri=self._get_keystone_mock_url(resource='users'), - json=self._get_user_list(user_data)), - dict(method='GET', - uri=self._get_keystone_mock_url(resource='groups'), - status_code=200, - json={'groups': [group_data.json_response['group']]}), - dict(method='DELETE', - uri=self._get_keystone_mock_url( - resource='groups', - append=[group_data.group_id, 'users', user_data.user_id]), - status_code=204)]) + self.register_uris( + [ + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='users'), + json=self._get_user_list(user_data), + ), + dict( + method='GET', + uri=self._get_keystone_mock_url(resource='groups'), + status_code=200, + json={'groups': [group_data.json_response['group']]}, + ), + dict( + method='DELETE', + uri=self._get_keystone_mock_url( + resource='groups', + append=[ + group_data.group_id, + 'users', + user_data.user_id, + ], + ), + status_code=204, + ), + ] + ) self.cloud.remove_user_from_group( - user_data.user_id, group_data.group_id) + user_data.user_id, group_data.group_id + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_volume.py b/openstack/tests/unit/cloud/test_volume.py index 1f6ff2f2e..2cd42dd2e 100644 --- a/openstack/tests/unit/cloud/test_volume.py +++ b/openstack/tests/unit/cloud/test_volume.py @@ -22,59 +22,88 @@ from openstack.tests.unit import base class TestVolume(base.TestCase): - def _compare_volumes(self, exp, real): self.assertDictEqual( volume.Volume(**exp).to_dict(computed=False), - real.to_dict(computed=False) + real.to_dict(computed=False), ) def _compare_volume_attachments(self, exp, real): self.assertDictEqual( volume_attachment.VolumeAttachment(**exp).to_dict(computed=False), - real.to_dict(computed=False) + real.to_dict(computed=False), ) def test_attach_volume(self): server = dict(id='server001') - vol = {'id': 'volume001', 'status': 'available', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'available', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - rattach = {'server_id': server['id'], 'device': 'device001', - 'volumeId': volume['id'], 'id': 'attachmentId'} - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments']), - json={'volumeAttachment': rattach}, - validate=dict(json={ - 'volumeAttachment': { - 'volumeId': vol['id']}}) - )]) + rattach = { + 'server_id': server['id'], + 'device': 'device001', + 'volumeId': volume['id'], + 'id': 'attachmentId', + } + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + ], + ), + json={'volumeAttachment': rattach}, + validate=dict( + json={'volumeAttachment': {'volumeId': vol['id']}} + ), + ), + ] + ) ret = self.cloud.attach_volume(server, volume, wait=False) self._compare_volume_attachments(rattach, ret) self.assert_calls() def test_attach_volume_exception(self): server = dict(id='server001') - vol = {'id': 'volume001', 'status': 'available', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'available', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments']), - status_code=404, - validate=dict(json={ - 'volumeAttachment': { - 'volumeId': vol['id']}}) - )]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + ], + ), + status_code=404, + validate=dict( + json={'volumeAttachment': {'volumeId': vol['id']}} + ), + ), + ] + ) with testtools.ExpectedException( openstack.cloud.OpenStackCloudURINotFound ): @@ -83,36 +112,60 @@ class TestVolume(base.TestCase): def test_attach_volume_wait(self): server = dict(id='server001') - vol = {'id': 'volume001', 'status': 'available', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'available', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - vol['attachments'] = [{'server_id': server['id'], - 'device': 'device001'}] + vol['attachments'] = [ + {'server_id': server['id'], 'device': 'device001'} + ] vol['status'] = 'in-use' attached_volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - rattach = {'server_id': server['id'], 'device': 'device001', - 'volumeId': volume['id'], 'id': 'attachmentId'} - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments']), - json={'volumeAttachment': rattach}, - validate=dict(json={ - 'volumeAttachment': { - 'volumeId': vol['id']}})), - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', vol['id']]), - json={'volume': volume}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', vol['id']]), - json={'volume': attached_volume}) - ]) + rattach = { + 'server_id': server['id'], + 'device': 'device001', + 'volumeId': volume['id'], + 'id': 'attachmentId', + } + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + ], + ), + json={'volumeAttachment': rattach}, + validate=dict( + json={'volumeAttachment': {'volumeId': vol['id']}} + ), + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', vol['id']] + ), + json={'volume': volume}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', vol['id']] + ), + json={'volume': attached_volume}, + ), + ] + ) # defaults to wait=True ret = self.cloud.attach_volume(server, volume) self._compare_volume_attachments(rattach, ret) @@ -120,38 +173,59 @@ class TestVolume(base.TestCase): def test_attach_volume_wait_error(self): server = dict(id='server001') - vol = {'id': 'volume001', 'status': 'available', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'available', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) vol['status'] = 'error' errored_volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - rattach = {'server_id': server['id'], 'device': 'device001', - 'volumeId': volume['id'], 'id': 'attachmentId'} - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments']), - json={'volumeAttachment': rattach}, - validate=dict(json={ - 'volumeAttachment': { - 'volumeId': vol['id']}})), - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume['id']]), - json={'volume': errored_volume}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume['id']]), - json={'volume': errored_volume}) - ]) + rattach = { + 'server_id': server['id'], + 'device': 'device001', + 'volumeId': volume['id'], + 'id': 'attachmentId', + } + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + ], + ), + json={'volumeAttachment': rattach}, + validate=dict( + json={'volumeAttachment': {'volumeId': vol['id']}} + ), + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume['id']] + ), + json={'volume': errored_volume}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume['id']] + ), + json={'volume': errored_volume}, + ), + ] + ) - with testtools.ExpectedException( - openstack.exceptions.ResourceFailure - ): + with testtools.ExpectedException(openstack.exceptions.ResourceFailure): self.cloud.attach_volume(server, volume) self.assert_calls() @@ -161,8 +235,8 @@ class TestVolume(base.TestCase): with testtools.ExpectedException( openstack.cloud.OpenStackCloudException, - "Volume %s is not available. Status is '%s'" % ( - volume['id'], volume['status']) + "Volume %s is not available. Status is '%s'" + % (volume['id'], volume['status']), ): self.cloud.attach_volume(server, volume) self.assertEqual(0, len(self.adapter.request_history)) @@ -170,59 +244,85 @@ class TestVolume(base.TestCase): def test_attach_volume_already_attached(self): device_id = 'device001' server = dict(id='server001') - volume = dict(id='volume001', - attachments=[ - {'server_id': 'server001', 'device': device_id} - ]) + volume = dict( + id='volume001', + attachments=[{'server_id': 'server001', 'device': device_id}], + ) with testtools.ExpectedException( openstack.cloud.OpenStackCloudException, - "Volume %s already attached to server %s on device %s" % ( - volume['id'], server['id'], device_id) + "Volume %s already attached to server %s on device %s" + % (volume['id'], server['id'], device_id), ): self.cloud.attach_volume(server, volume) self.assertEqual(0, len(self.adapter.request_history)) def test_detach_volume(self): server = dict(id='server001') - volume = dict(id='volume001', - attachments=[ - {'server_id': 'server001', 'device': 'device001'} - ]) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id']]), - json={'server': server}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments', volume['id']]))]) + volume = dict( + id='volume001', + attachments=[{'server_id': 'server001', 'device': 'device001'}], + ) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', server['id']] + ), + json={'server': server}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + volume['id'], + ], + ), + ), + ] + ) self.cloud.detach_volume(server, volume, wait=False) self.assert_calls() def test_detach_volume_exception(self): server = dict(id='server001') - volume = dict(id='volume001', - attachments=[ - {'server_id': 'server001', 'device': 'device001'} - ]) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id']]), - json={'server': server}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments', volume['id']]), - status_code=404)]) + volume = dict( + id='volume001', + attachments=[{'server_id': 'server001', 'device': 'device001'}], + ) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', server['id']] + ), + json={'server': server}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + volume['id'], + ], + ), + status_code=404, + ), + ] + ) with testtools.ExpectedException( openstack.cloud.OpenStackCloudURINotFound ): @@ -232,225 +332,363 @@ class TestVolume(base.TestCase): def test_detach_volume_wait(self): server = dict(id='server001') attachments = [{'server_id': 'server001', 'device': 'device001'}] - vol = {'id': 'volume001', 'status': 'attached', 'name': '', - 'attachments': attachments} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': attachments, + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) vol['status'] = 'available' vol['attachments'] = [] avail_volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id']]), - json={'server': server}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments', volume.id])), - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [avail_volume]})]) + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', server['id']] + ), + json={'server': server}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + volume.id, + ], + ), + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [avail_volume]}, + ), + ] + ) self.cloud.detach_volume(server, volume) self.assert_calls() def test_detach_volume_wait_error(self): server = dict(id='server001') attachments = [{'server_id': 'server001', 'device': 'device001'}] - vol = {'id': 'volume001', 'status': 'attached', 'name': '', - 'attachments': attachments} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': attachments, + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) vol['status'] = 'error' vol['attachments'] = [] errored_volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_nova_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id']]), - json={'server': server}), - dict(method='DELETE', - uri=self.get_mock_url( - 'compute', 'public', - append=['servers', server['id'], - 'os-volume_attachments', volume.id])), - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [errored_volume]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', errored_volume['id']]), - json={'volume': errored_volume}) - ]) - with testtools.ExpectedException( - openstack.exceptions.ResourceFailure - ): + self.register_uris( + [ + self.get_nova_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'compute', 'public', append=['servers', server['id']] + ), + json={'server': server}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'compute', + 'public', + append=[ + 'servers', + server['id'], + 'os-volume_attachments', + volume.id, + ], + ), + ), + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [errored_volume]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['volumes', errored_volume['id']], + ), + json={'volume': errored_volume}, + ), + ] + ) + with testtools.ExpectedException(openstack.exceptions.ResourceFailure): self.cloud.detach_volume(server, volume) self.assert_calls() def test_delete_volume_deletes(self): - vol = {'id': 'volume001', 'status': 'attached', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume.id]), - json={'volumes': [volume]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume.id])), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume.id]), - status_code=404)]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume.id] + ), + json={'volumes': [volume]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume.id] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume.id] + ), + status_code=404, + ), + ] + ) self.assertTrue(self.cloud.delete_volume(volume['id'])) self.assert_calls() def test_delete_volume_gone_away(self): - vol = {'id': 'volume001', 'status': 'attached', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume.id]), - json=volume), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume.id]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume.id]), - status_code=404), - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume.id] + ), + json=volume, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume.id] + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume.id] + ), + status_code=404, + ), + ] + ) self.assertTrue(self.cloud.delete_volume(volume['id'])) self.assert_calls() def test_delete_volume_force(self): - vol = {'id': 'volume001', 'status': 'attached', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume['id']]), - json={'volumes': [volume]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', volume.id, 'action']), - validate=dict( - json={'os-force_delete': {}})), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', volume['id']]), - status_code=404)]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume['id']] + ), + json={'volumes': [volume]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['volumes', volume.id, 'action'], + ), + validate=dict(json={'os-force_delete': {}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', volume['id']] + ), + status_code=404, + ), + ] + ) self.assertTrue(self.cloud.delete_volume(volume['id'], force=True)) self.assert_calls() def test_set_volume_bootable(self): - vol = {'id': 'volume001', 'status': 'attached', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [volume]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', volume.id, 'action']), - json={'os-set_bootable': {'bootable': True}}), - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [volume]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['volumes', volume.id, 'action'], + ), + json={'os-set_bootable': {'bootable': True}}, + ), + ] + ) self.cloud.set_volume_bootable(volume['id']) self.assert_calls() def test_set_volume_bootable_false(self): - vol = {'id': 'volume001', 'status': 'attached', - 'name': '', 'attachments': []} + vol = { + 'id': 'volume001', + 'status': 'attached', + 'name': '', + 'attachments': [], + } volume = meta.obj_to_munch(fakes.FakeVolume(**vol)) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes', 'detail']), - json={'volumes': [volume]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', volume.id, 'action']), - json={'os-set_bootable': {'bootable': False}}), - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', 'detail'] + ), + json={'volumes': [volume]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['volumes', volume.id, 'action'], + ), + json={'os-set_bootable': {'bootable': False}}, + ), + ] + ) self.cloud.set_volume_bootable(volume['id']) self.assert_calls() def test_get_volume_by_id(self): vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1')) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', '01']), - json={'volume': vol1} - ) - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes', '01'] + ), + json={'volume': vol1}, + ), + ] + ) self._compare_volumes(vol1, self.cloud.get_volume_by_id('01')) self.assert_calls() def test_create_volume(self): vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1')) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes']), - json={'volume': vol1}, - validate=dict(json={ - 'volume': { - 'size': 50, - 'name': 'vol1', - }})), - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes'] + ), + json={'volume': vol1}, + validate=dict( + json={ + 'volume': { + 'size': 50, + 'name': 'vol1', + } + } + ), + ), + ] + ) self.cloud.create_volume(50, name='vol1') self.assert_calls() def test_create_bootable_volume(self): vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1')) - self.register_uris([ - self.get_cinder_discovery_mock_dict(), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', append=['volumes']), - json={'volume': vol1}, - validate=dict(json={ - 'volume': { - 'size': 50, - 'name': 'vol1', - }})), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['volumes', '01', 'action']), - validate=dict( - json={'os-set_bootable': {'bootable': True}})), - ]) + self.register_uris( + [ + self.get_cinder_discovery_mock_dict(), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['volumes'] + ), + json={'volume': vol1}, + validate=dict( + json={ + 'volume': { + 'size': 50, + 'name': 'vol1', + } + } + ), + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['volumes', '01', 'action'], + ), + validate=dict( + json={'os-set_bootable': {'bootable': True}} + ), + ), + ] + ) self.cloud.create_volume(50, name='vol1', bootable=True) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_volume_access.py b/openstack/tests/unit/cloud/test_volume_access.py index 8768df694..43cbb1df1 100644 --- a/openstack/tests/unit/cloud/test_volume_access.py +++ b/openstack/tests/unit/cloud/test_volume_access.py @@ -26,165 +26,281 @@ class TestVolumeAccess(base.TestCase): def test_list_volume_types(self): volume_type = dict( - id='voltype01', description='volume type description', - name='name', is_public=False) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]})]) + id='voltype01', + description='volume type description', + name='name', + is_public=False, + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ) + ] + ) self.assertTrue(self.cloud.list_volume_types()) self.assert_calls() def test_get_volume_type(self): volume_type = dict( - id='voltype01', description='volume type description', name='name', - is_public=False) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]})]) + id='voltype01', + description='volume type description', + name='name', + is_public=False, + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ) + ] + ) volume_type_got = self.cloud.get_volume_type(volume_type['name']) self.assertEqual(volume_type_got.id, volume_type['id']) def test_get_volume_type_access(self): volume_type = dict( - id='voltype01', description='volume type description', name='name', - is_public=False) + id='voltype01', + description='volume type description', + name='name', + is_public=False, + ) volume_type_access = [ dict(volume_type_id='voltype01', name='name', project_id='prj01'), - dict(volume_type_id='voltype01', name='name', project_id='prj02') + dict(volume_type_id='voltype01', name='name', project_id='prj02'), ] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types', volume_type['id'], - 'os-volume-type-access']), - json={'volume_type_access': volume_type_access})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=[ + 'types', + volume_type['id'], + 'os-volume-type-access', + ], + ), + json={'volume_type_access': volume_type_access}, + ), + ] + ) self.assertEqual( - len(self.cloud.get_volume_type_access(volume_type['name'])), 2) + len(self.cloud.get_volume_type_access(volume_type['name'])), 2 + ) self.assert_calls() def test_remove_volume_type_access(self): volume_type = dict( - id='voltype01', description='volume type description', name='name', - is_public=False) - project_001 = dict(volume_type_id='voltype01', name='name', - project_id='prj01') - project_002 = dict(volume_type_id='voltype01', name='name', - project_id='prj02') + id='voltype01', + description='volume type description', + name='name', + is_public=False, + ) + project_001 = dict( + volume_type_id='voltype01', name='name', project_id='prj01' + ) + project_002 = dict( + volume_type_id='voltype01', name='name', project_id='prj02' + ) volume_type_access = [project_001, project_002] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types', volume_type['id'], - 'os-volume-type-access']), - json={'volume_type_access': volume_type_access}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types', volume_type['id'], 'action']), - json={'removeProjectAccess': { - 'project': project_001['project_id']}}, - validate=dict( - json={'removeProjectAccess': { - 'project': project_001['project_id']}})), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types', volume_type['id'], - 'os-volume-type-access']), - json={'volume_type_access': [project_001]})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=[ + 'types', + volume_type['id'], + 'os-volume-type-access', + ], + ), + json={'volume_type_access': volume_type_access}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['types', volume_type['id'], 'action'], + ), + json={ + 'removeProjectAccess': { + 'project': project_001['project_id'] + } + }, + validate=dict( + json={ + 'removeProjectAccess': { + 'project': project_001['project_id'] + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=[ + 'types', + volume_type['id'], + 'os-volume-type-access', + ], + ), + json={'volume_type_access': [project_001]}, + ), + ] + ) self.assertEqual( - len(self.cloud.get_volume_type_access( - volume_type['name'])), 2) + len(self.cloud.get_volume_type_access(volume_type['name'])), 2 + ) self.cloud.remove_volume_type_access( - volume_type['name'], project_001['project_id']) + volume_type['name'], project_001['project_id'] + ) self.assertEqual( - len(self.cloud.get_volume_type_access(volume_type['name'])), 1) + len(self.cloud.get_volume_type_access(volume_type['name'])), 1 + ) self.assert_calls() def test_add_volume_type_access(self): volume_type = dict( - id='voltype01', description='volume type description', name='name', - is_public=False) - project_001 = dict(volume_type_id='voltype01', name='name', - project_id='prj01') - project_002 = dict(volume_type_id='voltype01', name='name', - project_id='prj02') + id='voltype01', + description='volume type description', + name='name', + is_public=False, + ) + project_001 = dict( + volume_type_id='voltype01', name='name', project_id='prj01' + ) + project_002 = dict( + volume_type_id='voltype01', name='name', project_id='prj02' + ) volume_type_access = [project_001, project_002] - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types', volume_type['id'], 'action']), - json={'addProjectAccess': { - 'project': project_002['project_id']}}, - validate=dict( - json={'addProjectAccess': { - 'project': project_002['project_id']}})), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types', volume_type['id'], - 'os-volume-type-access']), - json={'volume_type_access': volume_type_access})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['types', volume_type['id'], 'action'], + ), + json={ + 'addProjectAccess': { + 'project': project_002['project_id'] + } + }, + validate=dict( + json={ + 'addProjectAccess': { + 'project': project_002['project_id'] + } + } + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=[ + 'types', + volume_type['id'], + 'os-volume-type-access', + ], + ), + json={'volume_type_access': volume_type_access}, + ), + ] + ) self.cloud.add_volume_type_access( - volume_type['name'], project_002['project_id']) + volume_type['name'], project_002['project_id'] + ) self.assertEqual( - len(self.cloud.get_volume_type_access(volume_type['name'])), 2) + len(self.cloud.get_volume_type_access(volume_type['name'])), 2 + ) self.assert_calls() def test_add_volume_type_access_missing(self): volume_type = dict( - id='voltype01', description='volume type description', name='name', - is_public=False) - project_001 = dict(volume_type_id='voltype01', name='name', - project_id='prj01') - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['types']), - json={'volume_types': [volume_type]})]) + id='voltype01', + description='volume type description', + name='name', + is_public=False, + ) + project_001 = dict( + volume_type_id='voltype01', name='name', project_id='prj01' + ) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['types'] + ), + json={'volume_types': [volume_type]}, + ) + ] + ) with testtools.ExpectedException( - openstack.cloud.OpenStackCloudException, - "VolumeType not found: MISSING"): + openstack.cloud.OpenStackCloudException, + "VolumeType not found: MISSING", + ): self.cloud.add_volume_type_access( - "MISSING", project_001['project_id']) + "MISSING", project_001['project_id'] + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_volume_backups.py b/openstack/tests/unit/cloud/test_volume_backups.py index 5329f3934..41a779241 100644 --- a/openstack/tests/unit/cloud/test_volume_backups.py +++ b/openstack/tests/unit/cloud/test_volume_backups.py @@ -21,20 +21,28 @@ class TestVolumeBackups(base.TestCase): def _compare_backups(self, exp, real): self.assertDictEqual( backup.Backup(**exp).to_dict(computed=False), - real.to_dict(computed=False)) + real.to_dict(computed=False), + ) def test_search_volume_backups(self): name = 'Volume1' vol1 = {'name': name, 'availability_zone': 'az1'} vol2 = {'name': name, 'availability_zone': 'az1'} vol3 = {'name': 'Volume2', 'availability_zone': 'az2'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['backups', 'detail']), - json={"backups": [vol1, vol2, vol3]})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', 'detail'] + ), + json={"backups": [vol1, vol2, vol3]}, + ) + ] + ) result = self.cloud.search_volume_backups( - name, {'availability_zone': 'az1'}) + name, {'availability_zone': 'az1'} + ) self.assertEqual(len(result), 2) for a, b in zip([vol1, vol2], result): self._compare_backups(a, b) @@ -45,26 +53,43 @@ class TestVolumeBackups(base.TestCase): vol1 = {'name': name, 'availability_zone': 'az1'} vol2 = {'name': name, 'availability_zone': 'az2'} vol3 = {'name': 'Volume2', 'availability_zone': 'az1'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['backups', 'detail']), - json={"backups": [vol1, vol2, vol3]})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', 'detail'] + ), + json={"backups": [vol1, vol2, vol3]}, + ) + ] + ) result = self.cloud.get_volume_backup( - name, {'availability_zone': 'az1'}) + name, {'availability_zone': 'az1'} + ) self._compare_backups(vol1, result) self.assert_calls() def test_list_volume_backups(self): - backup = {'id': '6ff16bdf-44d5-4bf9-b0f3-687549c76414', - 'status': 'available'} + backup = { + 'id': '6ff16bdf-44d5-4bf9-b0f3-687549c76414', + 'status': 'available', + } search_opts = {'status': 'available'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', append=['backups', 'detail'], - qs_elements=['='.join(i) for i in search_opts.items()]), - json={"backups": [backup]})]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['backups', 'detail'], + qs_elements=['='.join(i) for i in search_opts.items()], + ), + json={"backups": [backup]}, + ) + ] + ) result = self.cloud.list_volume_backups(True, search_opts) self.assertEqual(len(result), 1) @@ -74,55 +99,78 @@ class TestVolumeBackups(base.TestCase): def test_delete_volume_backup_wait(self): backup_id = '6ff16bdf-44d5-4bf9-b0f3-687549c76414' backup = {'id': backup_id, 'status': 'available'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', 'detail']), - json={"backups": [backup]}), - dict(method='DELETE', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', backup_id])), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', backup_id]), - json={"backup": backup}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', backup_id]), - status_code=404)]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', 'detail'] + ), + json={"backups": [backup]}, + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', backup_id] + ), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', backup_id] + ), + json={"backup": backup}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', backup_id] + ), + status_code=404, + ), + ] + ) self.cloud.delete_volume_backup(backup_id, False, True, 1) self.assert_calls() def test_delete_volume_backup_force(self): backup_id = '6ff16bdf-44d5-4bf9-b0f3-687549c76414' backup = {'id': backup_id, 'status': 'available'} - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', 'detail']), - json={"backups": [backup]}), - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', backup_id, 'action']), - json={'os-force_delete': {}}, - validate=dict(json={u'os-force_delete': {}})), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', backup_id]), - json={"backup": backup}), - dict(method='GET', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups', backup_id]), - status_code=404) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', 'detail'] + ), + json={"backups": [backup]}, + ), + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', + 'public', + append=['backups', backup_id, 'action'], + ), + json={'os-force_delete': {}}, + validate=dict(json={u'os-force_delete': {}}), + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', backup_id] + ), + json={"backup": backup}, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups', backup_id] + ), + status_code=404, + ), + ] + ) self.cloud.delete_volume_backup(backup_id, True, True, 1) self.assert_calls() @@ -133,26 +181,32 @@ class TestVolumeBackups(base.TestCase): 'id': '5678', 'volume_id': volume_id, 'status': 'available', - 'name': backup_name + 'name': backup_name, } - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups']), - json={'backup': bak1}, - validate=dict(json={ - 'backup': { - 'name': backup_name, - 'volume_id': volume_id, - 'description': None, - 'force': False, - 'snapshot_id': None, - 'incremental': False - } - })), - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups'] + ), + json={'backup': bak1}, + validate=dict( + json={ + 'backup': { + 'name': backup_name, + 'volume_id': volume_id, + 'description': None, + 'force': False, + 'snapshot_id': None, + 'incremental': False, + } + } + ), + ), + ] + ) self.cloud.create_volume_backup(volume_id, name=backup_name) self.assert_calls() @@ -163,28 +217,35 @@ class TestVolumeBackups(base.TestCase): 'id': '5678', 'volume_id': volume_id, 'status': 'available', - 'name': backup_name + 'name': backup_name, } - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups']), - json={'backup': bak1}, - validate=dict(json={ - 'backup': { - 'name': backup_name, - 'volume_id': volume_id, - 'description': None, - 'force': False, - 'snapshot_id': None, - 'incremental': True - } - })), - ]) - self.cloud.create_volume_backup(volume_id, name=backup_name, - incremental=True) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups'] + ), + json={'backup': bak1}, + validate=dict( + json={ + 'backup': { + 'name': backup_name, + 'volume_id': volume_id, + 'description': None, + 'force': False, + 'snapshot_id': None, + 'incremental': True, + } + } + ), + ), + ] + ) + self.cloud.create_volume_backup( + volume_id, name=backup_name, incremental=True + ) self.assert_calls() def test_create_volume_backup_from_snapshot(self): @@ -195,27 +256,33 @@ class TestVolumeBackups(base.TestCase): 'id': '5678', 'volume_id': volume_id, 'status': 'available', - 'name': 'bak1' + 'name': 'bak1', } - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'volumev3', 'public', - append=['backups']), - json={'backup': bak1}, - validate=dict(json={ - 'backup': { - 'name': backup_name, - 'volume_id': volume_id, - 'description': None, - 'force': False, - 'snapshot_id': snapshot_id, - 'incremental': False - } - })), - - ]) - self.cloud.create_volume_backup(volume_id, name=backup_name, - snapshot_id=snapshot_id) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'volumev3', 'public', append=['backups'] + ), + json={'backup': bak1}, + validate=dict( + json={ + 'backup': { + 'name': backup_name, + 'volume_id': volume_id, + 'description': None, + 'force': False, + 'snapshot_id': snapshot_id, + 'incremental': False, + } + } + ), + ), + ] + ) + self.cloud.create_volume_backup( + volume_id, name=backup_name, snapshot_id=snapshot_id + ) self.assert_calls() diff --git a/openstack/tests/unit/cloud/test_zone.py b/openstack/tests/unit/cloud/test_zone.py index 2c3f9b1ba..81a1713c4 100644 --- a/openstack/tests/unit/cloud/test_zone.py +++ b/openstack/tests/unit/cloud/test_zone.py @@ -22,12 +22,11 @@ zone_dict = { 'email': 'test@example.net', 'description': 'Example zone', 'ttl': 3600, - 'id': '1' + 'id': '1', } class ZoneTestWrapper: - def __init__(self, ut, attrs): self.remote_res = attrs self.ut = ut @@ -39,8 +38,7 @@ class ZoneTestWrapper: return self.remote_res def __getitem__(self, key): - """Dict access to be able to access properties easily - """ + """Dict access to be able to access properties easily""" return self.remote_res[key] def cmp(self, other): @@ -54,48 +52,58 @@ class ZoneTestWrapper: class TestZone(base.TestCase): - def setUp(self): super(TestZone, self).setUp() self.use_designate() def test_create_zone(self): fake_zone = ZoneTestWrapper(self, zone_dict) - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones']), - json=fake_zone.get_create_response_json(), - validate=dict(json={ - 'description': zone_dict['description'], - 'email': zone_dict['email'], - 'name': zone_dict['name'], - 'ttl': zone_dict['ttl'], - 'type': 'PRIMARY' - })) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'dns', 'public', append=['v2', 'zones'] + ), + json=fake_zone.get_create_response_json(), + validate=dict( + json={ + 'description': zone_dict['description'], + 'email': zone_dict['email'], + 'name': zone_dict['name'], + 'ttl': zone_dict['ttl'], + 'type': 'PRIMARY', + } + ), + ) + ] + ) z = self.cloud.create_zone( name=zone_dict['name'], zone_type=zone_dict['type'], email=zone_dict['email'], description=zone_dict['description'], ttl=zone_dict['ttl'], - masters=None) + masters=None, + ) fake_zone.cmp(z) self.assert_calls() def test_create_zone_exception(self): - self.register_uris([ - dict(method='POST', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones']), - status_code=500) - ]) + self.register_uris( + [ + dict( + method='POST', + uri=self.get_mock_url( + 'dns', 'public', append=['v2', 'zones'] + ), + status_code=500, + ) + ] + ) self.assertRaises( - exceptions.SDKException, - self.cloud.create_zone, - 'example.net.' + exceptions.SDKException, self.cloud.create_zone, 'example.net.' ) self.assert_calls() @@ -105,44 +113,75 @@ class TestZone(base.TestCase): updated_zone_dict = copy.copy(zone_dict) updated_zone_dict['ttl'] = new_ttl updated_zone = ZoneTestWrapper(self, updated_zone_dict) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()), - dict(method='PATCH', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=updated_zone.get_get_response_json(), - validate=dict(json={"ttl": new_ttl})) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ), + dict( + method='PATCH', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=updated_zone.get_get_response_json(), + validate=dict(json={"ttl": new_ttl}), + ), + ] + ) z = self.cloud.update_zone(fake_zone['id'], ttl=new_ttl) updated_zone.cmp(z) self.assert_calls() def test_delete_zone(self): fake_zone = ZoneTestWrapper(self, zone_dict) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()), - dict(method='DELETE', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - status_code=202) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ), + dict( + method='DELETE', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + status_code=202, + ), + ] + ) self.assertTrue(self.cloud.delete_zone(fake_zone['id'])) self.assert_calls() def test_get_zone_by_id(self): fake_zone = ZoneTestWrapper(self, zone_dict) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones', fake_zone['id']]), - json=fake_zone.get_get_response_json()) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['id']], + ), + json=fake_zone.get_get_response_json(), + ) + ] + ) res = self.cloud.get_zone(fake_zone['id']) fake_zone.cmp(res) @@ -150,66 +189,103 @@ class TestZone(base.TestCase): def test_get_zone_by_name(self): fake_zone = ZoneTestWrapper(self, zone_dict) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', fake_zone['name']]), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=[ - 'name={name}'.format(name=fake_zone['name'])]), - json={"zones": [fake_zone.get_get_response_json()]}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', fake_zone['name']], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=[ + 'name={name}'.format(name=fake_zone['name']) + ], + ), + json={"zones": [fake_zone.get_get_response_json()]}, + ), + ] + ) res = self.cloud.get_zone(fake_zone['name']) fake_zone.cmp(res) self.assert_calls() def test_get_zone_not_found_returns_false(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones', 'nonexistingzone.net.']), - status_code=404), - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', append=['v2', 'zones'], - qs_elements=['name=nonexistingzone.net.']), - json={"zones": []}) - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones', 'nonexistingzone.net.'], + ), + status_code=404, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones'], + qs_elements=['name=nonexistingzone.net.'], + ), + json={"zones": []}, + ), + ] + ) zone = self.cloud.get_zone('nonexistingzone.net.') self.assertFalse(zone) self.assert_calls() def test_list_zones(self): fake_zone = ZoneTestWrapper(self, zone_dict) - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones']), - json={'zones': [fake_zone.get_get_response_json()], - 'links': { - 'next': self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones/'], - qs_elements=['limit=1', 'marker=asd']), - 'self': self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones/'], - qs_elements=['limit=1'])}, - 'metadata':{'total_count': 2}}), - dict(method='GET', - uri=self.get_mock_url( - 'dns', 'public', - append=['v2', 'zones/'], - qs_elements=[ - 'limit=1', 'marker=asd']), - json={'zones': [fake_zone.get_get_response_json()]}), - ]) + self.register_uris( + [ + dict( + method='GET', + uri=self.get_mock_url( + 'dns', 'public', append=['v2', 'zones'] + ), + json={ + 'zones': [fake_zone.get_get_response_json()], + 'links': { + 'next': self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones/'], + qs_elements=['limit=1', 'marker=asd'], + ), + 'self': self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones/'], + qs_elements=['limit=1'], + ), + }, + 'metadata': {'total_count': 2}, + }, + ), + dict( + method='GET', + uri=self.get_mock_url( + 'dns', + 'public', + append=['v2', 'zones/'], + qs_elements=['limit=1', 'marker=asd'], + ), + json={'zones': [fake_zone.get_get_response_json()]}, + ), + ] + ) res = self.cloud.list_zones() # updated_rs.cmp(res)