From 6825263609be062649997f81d0f4fdde091eaf43 Mon Sep 17 00:00:00 2001 From: Huanxuan Ao Date: Mon, 11 Jul 2016 14:46:13 +0800 Subject: [PATCH] Use FakeProject and FakeDomain classes in unit tests of networkv2 FakeProject and FakeDomain classes have been completed in identityv2_0 and v3, Now we can use these classes instead of old test data in networkv2. Change-Id: I2f698e54ff35e24ffbdcaba29da6f96eb263cc0a Partially-Implements: blueprint use-fake-project --- .../tests/network/v2/test_address_scope.py | 27 ++++------ .../tests/network/v2/test_ip_availability.py | 14 ++---- .../tests/network/v2/test_network.py | 50 ++++++++----------- .../tests/network/v2/test_security_group.py | 31 +++++------- .../network/v2/test_security_group_rule.py | 30 +++++------ .../tests/network/v2/test_subnet.py | 21 +++----- .../tests/network/v2/test_subnet_pool.py | 25 ++++------ 7 files changed, 77 insertions(+), 121 deletions(-) diff --git a/openstackclient/tests/network/v2/test_address_scope.py b/openstackclient/tests/network/v2/test_address_scope.py index 722371f9d9..16e74f46ec 100644 --- a/openstackclient/tests/network/v2/test_address_scope.py +++ b/openstackclient/tests/network/v2/test_address_scope.py @@ -11,7 +11,6 @@ # under the License. # -import copy import mock from mock import call @@ -35,11 +34,13 @@ class TestAddressScope(network_fakes.TestNetworkV2): class TestCreateAddressScope(TestAddressScope): + project = identity_fakes_v3.FakeProject.create_one_project() + domain = identity_fakes_v3.FakeDomain.create_one_domain() # The new address scope created. new_address_scope = ( network_fakes.FakeAddressScope.create_one_address_scope( attrs={ - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': project.id, } )) columns = ( @@ -75,19 +76,11 @@ class TestCreateAddressScope(TestAddressScope): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # Get a shortcut to the DomainManager Mock self.domains_mock = self.identity.domains - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain def test_create_no_options(self): arglist = [] @@ -121,15 +114,15 @@ class TestCreateAddressScope(TestAddressScope): arglist = [ '--ip-version', str(self.new_address_scope.ip_version), '--share', - '--project', identity_fakes_v3.project_name, - '--project-domain', identity_fakes_v3.domain_name, + '--project', self.project.name, + '--project-domain', self.domain.name, self.new_address_scope.name, ] verifylist = [ ('ip_version', self.new_address_scope.ip_version), ('share', True), - ('project', identity_fakes_v3.project_name), - ('project_domain', identity_fakes_v3.domain_name), + ('project', self.project.name), + ('project_domain', self.domain.name), ('name', self.new_address_scope.name), ] @@ -139,7 +132,7 @@ class TestCreateAddressScope(TestAddressScope): self.network.create_address_scope.assert_called_once_with(**{ 'ip_version': self.new_address_scope.ip_version, 'shared': True, - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': self.project.id, 'name': self.new_address_scope.name, }) self.assertEqual(self.columns, columns) diff --git a/openstackclient/tests/network/v2/test_ip_availability.py b/openstackclient/tests/network/v2/test_ip_availability.py index c6ec2b0b06..21d44d0754 100644 --- a/openstackclient/tests/network/v2/test_ip_availability.py +++ b/openstackclient/tests/network/v2/test_ip_availability.py @@ -11,7 +11,6 @@ # under the License. # -import copy import mock from osc_lib import utils as common_utils @@ -41,11 +40,8 @@ class TestIPAvailability(network_fakes.TestNetworkV2): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) + self.project = identity_fakes.FakeProject.create_one_project() + self.projects_mock.get.return_value = self.project class TestListIPAvailability(TestIPAvailability): @@ -109,16 +105,16 @@ class TestListIPAvailability(TestIPAvailability): def test_list_project(self): arglist = [ - '--project', identity_fakes.project_name + '--project', self.project.name ] verifylist = [ - ('project', identity_fakes.project_name) + ('project', self.project.name) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) - filters = {'tenant_id': identity_fakes.project_id, + filters = {'tenant_id': self.project.id, 'ip_version': 4} self.network.network_ip_availabilities.assert_called_once_with( diff --git a/openstackclient/tests/network/v2/test_network.py b/openstackclient/tests/network/v2/test_network.py index 8fc9dadf8f..4322bf9a55 100644 --- a/openstackclient/tests/network/v2/test_network.py +++ b/openstackclient/tests/network/v2/test_network.py @@ -11,7 +11,6 @@ # under the License. # -import copy import mock from mock import call @@ -40,10 +39,12 @@ class TestNetwork(network_fakes.TestNetworkV2): class TestCreateNetworkIdentityV3(TestNetwork): + project = identity_fakes_v3.FakeProject.create_one_project() + domain = identity_fakes_v3.FakeDomain.create_one_domain() # The new network created. _network = network_fakes.FakeNetwork.create_one_network( attrs={ - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': project.id, 'availability_zone_hints': ["nova"], } ) @@ -96,19 +97,11 @@ class TestCreateNetworkIdentityV3(TestNetwork): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # Get a shortcut to the DomainManager Mock self.domains_mock = self.identity.domains - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain def test_create_no_options(self): arglist = [] @@ -143,8 +136,8 @@ class TestCreateNetworkIdentityV3(TestNetwork): arglist = [ "--disable", "--share", - "--project", identity_fakes_v3.project_name, - "--project-domain", identity_fakes_v3.domain_name, + "--project", self.project.name, + "--project-domain", self.domain.name, "--availability-zone-hint", "nova", "--external", "--default", "--provider-network-type", "vlan", @@ -156,8 +149,8 @@ class TestCreateNetworkIdentityV3(TestNetwork): verifylist = [ ('disable', True), ('share', True), - ('project', identity_fakes_v3.project_name), - ('project_domain', identity_fakes_v3.domain_name), + ('project', self.project.name), + ('project_domain', self.domain.name), ('availability_zone_hints', ["nova"]), ('external', True), ('default', True), @@ -176,7 +169,7 @@ class TestCreateNetworkIdentityV3(TestNetwork): 'availability_zone_hints': ["nova"], 'name': self._network.name, 'shared': True, - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': self.project.id, 'is_default': True, 'router:external': True, 'provider:network_type': 'vlan', @@ -214,9 +207,10 @@ class TestCreateNetworkIdentityV3(TestNetwork): class TestCreateNetworkIdentityV2(TestNetwork): + project = identity_fakes_v2.FakeProject.create_one_project() # The new network created. _network = network_fakes.FakeNetwork.create_one_network( - attrs={'tenant_id': identity_fakes_v2.project_id} + attrs={'tenant_id': project.id} ) columns = ( @@ -267,24 +261,20 @@ class TestCreateNetworkIdentityV2(TestNetwork): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.tenants - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v2.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # There is no DomainManager Mock in fake identity v2. def test_create_with_project_identityv2(self): arglist = [ - "--project", identity_fakes_v2.project_name, + "--project", self.project.name, self._network.name, ] verifylist = [ ('enable', True), ('share', None), ('name', self._network.name), - ('project', identity_fakes_v2.project_name), + ('project', self.project.name), ('external', False), ] @@ -294,22 +284,22 @@ class TestCreateNetworkIdentityV2(TestNetwork): self.network.create_network.assert_called_once_with(**{ 'admin_state_up': True, 'name': self._network.name, - 'tenant_id': identity_fakes_v2.project_id, + 'tenant_id': self.project.id, }) self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) def test_create_with_domain_identityv2(self): arglist = [ - "--project", identity_fakes_v3.project_name, - "--project-domain", identity_fakes_v3.domain_name, + "--project", self.project.name, + "--project-domain", "domain-name", self._network.name, ] verifylist = [ ('enable', True), ('share', None), - ('project', identity_fakes_v3.project_name), - ('project_domain', identity_fakes_v3.domain_name), + ('project', self.project.name), + ('project_domain', "domain-name"), ('name', self._network.name), ('external', False), ] diff --git a/openstackclient/tests/network/v2/test_security_group.py b/openstackclient/tests/network/v2/test_security_group.py index b0c1498519..7ac925ac2f 100644 --- a/openstackclient/tests/network/v2/test_security_group.py +++ b/openstackclient/tests/network/v2/test_security_group.py @@ -11,7 +11,6 @@ # under the License. # -import copy import mock from mock import call @@ -45,6 +44,8 @@ class TestSecurityGroupCompute(compute_fakes.TestComputev2): class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): + project = identity_fakes.FakeProject.create_one_project() + domain = identity_fakes.FakeDomain.create_one_domain() # The security group to be created. _security_group = \ network_fakes.FakeSecurityGroup.create_one_security_group() @@ -81,19 +82,11 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # Get a shortcut to the DomainManager Mock self.domains_mock = self.identity.domains - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain # Get the command object to test self.cmd = security_group.CreateSecurityGroup(self.app, self.namespace) @@ -123,15 +116,15 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): def test_create_all_options(self): arglist = [ '--description', self._security_group.description, - '--project', identity_fakes.project_name, - '--project-domain', identity_fakes.domain_name, + '--project', self.project.name, + '--project-domain', self.domain.name, self._security_group.name, ] verifylist = [ ('description', self._security_group.description), ('name', self._security_group.name), - ('project', identity_fakes.project_name), - ('project_domain', identity_fakes.domain_name), + ('project', self.project.name), + ('project_domain', self.domain.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -140,7 +133,7 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): self.network.create_security_group.assert_called_once_with(**{ 'description': self._security_group.description, 'name': self._security_group.name, - 'tenant_id': identity_fakes.project_id, + 'tenant_id': self.project.id, }) self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) @@ -148,6 +141,8 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): class TestCreateSecurityGroupCompute(TestSecurityGroupCompute): + project = identity_fakes.FakeProject.create_one_project() + domain = identity_fakes.FakeDomain.create_one_domain() # The security group to be shown. _security_group = \ compute_fakes.FakeSecurityGroup.create_one_security_group() @@ -184,8 +179,8 @@ class TestCreateSecurityGroupCompute(TestSecurityGroupCompute): def test_create_network_options(self): arglist = [ - '--project', identity_fakes.project_name, - '--project-domain', identity_fakes.domain_name, + '--project', self.project.name, + '--project-domain', self.domain.name, self._security_group.name, ] self.assertRaises(tests_utils.ParserException, diff --git a/openstackclient/tests/network/v2/test_security_group_rule.py b/openstackclient/tests/network/v2/test_security_group_rule.py index b2862679cb..67a123aa10 100644 --- a/openstackclient/tests/network/v2/test_security_group_rule.py +++ b/openstackclient/tests/network/v2/test_security_group_rule.py @@ -46,6 +46,8 @@ class TestSecurityGroupRuleCompute(compute_fakes.TestComputev2): class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): + project = identity_fakes.FakeProject.create_one_project() + domain = identity_fakes.FakeDomain.create_one_domain() # The security group rule to be created. _security_group_rule = None @@ -103,19 +105,11 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # Get a shortcut to the DomainManager Mock self.domains_mock = self.identity.domains - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain # Get the command object to test self.cmd = security_group_rule.CreateSecurityGroupRule( @@ -306,8 +300,8 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): '--dst-port', str(self._security_group_rule.port_range_min), '--egress', '--ethertype', self._security_group_rule.ethertype, - '--project', identity_fakes.project_name, - '--project-domain', identity_fakes.domain_name, + '--project', self.project.name, + '--project-domain', self.domain.name, '--protocol', self._security_group_rule.protocol, self._security_group.id, ] @@ -316,8 +310,8 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): self._security_group_rule.port_range_max)), ('egress', True), ('ethertype', self._security_group_rule.ethertype), - ('project', identity_fakes.project_name), - ('project_domain', identity_fakes.domain_name), + ('project', self.project.name), + ('project_domain', self.domain.name), ('protocol', self._security_group_rule.protocol), ('group', self._security_group.id), ] @@ -332,7 +326,7 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): 'port_range_min': self._security_group_rule.port_range_min, 'protocol': self._security_group_rule.protocol, 'security_group_id': self._security_group.id, - 'tenant_id': identity_fakes.project_id, + 'tenant_id': self.project.id, }) self.assertEqual(self.expected_columns, columns) self.assertEqual(self.expected_data, data) @@ -470,6 +464,8 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): class TestCreateSecurityGroupRuleCompute(TestSecurityGroupRuleCompute): + project = identity_fakes.FakeProject.create_one_project() + domain = identity_fakes.FakeDomain.create_one_domain() # The security group rule to be created. _security_group_rule = None @@ -534,8 +530,8 @@ class TestCreateSecurityGroupRuleCompute(TestSecurityGroupRuleCompute): '--ethertype', 'IPv4', '--icmp-type', '3', '--icmp-code', '11', - '--project', identity_fakes.project_name, - '--project-domain', identity_fakes.domain_name, + '--project', self.project.name, + '--project-domain', self.domain.name, self._security_group.id, ] self.assertRaises(tests_utils.ParserException, diff --git a/openstackclient/tests/network/v2/test_subnet.py b/openstackclient/tests/network/v2/test_subnet.py index 82813d6c10..46af44fb5b 100644 --- a/openstackclient/tests/network/v2/test_subnet.py +++ b/openstackclient/tests/network/v2/test_subnet.py @@ -11,7 +11,6 @@ # under the License. # -import copy import mock from mock import call @@ -36,10 +35,12 @@ class TestSubnet(network_fakes.TestNetworkV2): class TestCreateSubnet(TestSubnet): + project = identity_fakes_v3.FakeProject.create_one_project() + domain = identity_fakes_v3.FakeDomain.create_one_domain() # An IPv4 subnet to be created with mostly default values _subnet = network_fakes.FakeSubnet.create_one_subnet( attrs={ - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': project.id, } ) @@ -49,7 +50,7 @@ class TestCreateSubnet(TestSubnet): # An IPv4 subnet to be created using a specific subnet pool _subnet_from_pool = network_fakes.FakeSubnet.create_one_subnet( attrs={ - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': project.id, 'subnetpool_id': _subnet_pool.id, 'dns_nameservers': ['8.8.8.8', '8.8.4.4'], @@ -63,7 +64,7 @@ class TestCreateSubnet(TestSubnet): # An IPv6 subnet to be created with most options specified _subnet_ipv6 = network_fakes.FakeSubnet.create_one_subnet( attrs={ - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': project.id, 'cidr': 'fe80:0:0:a00a::/64', 'enable_dhcp': True, 'dns_nameservers': ['fe80:27ff:a00a:f00f::ffff', @@ -187,19 +188,11 @@ class TestCreateSubnet(TestSubnet): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # Get a shortcut to the DomainManager Mock self.domains_mock = self.identity.domains - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain # Mock SDK calls for all tests. self.network.find_network = mock.Mock(return_value=self._network) diff --git a/openstackclient/tests/network/v2/test_subnet_pool.py b/openstackclient/tests/network/v2/test_subnet_pool.py index 41b6170f42..8269af0be7 100644 --- a/openstackclient/tests/network/v2/test_subnet_pool.py +++ b/openstackclient/tests/network/v2/test_subnet_pool.py @@ -12,7 +12,6 @@ # import argparse -import copy import mock from mock import call @@ -37,6 +36,8 @@ class TestSubnetPool(network_fakes.TestNetworkV2): class TestCreateSubnetPool(TestSubnetPool): + project = identity_fakes_v3.FakeProject.create_one_project() + domain = identity_fakes_v3.FakeDomain.create_one_domain() # The new subnet pool to create. _subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool() @@ -93,19 +94,11 @@ class TestCreateSubnetPool(TestSubnetPool): # Get a shortcut to the ProjectManager Mock self.projects_mock = self.identity.projects - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project # Get a shortcut to the DomainManager Mock self.domains_mock = self.identity.domains - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes_v3.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain def test_create_no_options(self): arglist = [] @@ -191,14 +184,14 @@ class TestCreateSubnetPool(TestSubnetPool): def test_create_project_domain(self): arglist = [ '--pool-prefix', '10.0.10.0/24', - "--project", identity_fakes_v3.project_name, - "--project-domain", identity_fakes_v3.domain_name, + "--project", self.project.name, + "--project-domain", self.domain.name, self._subnet_pool.name, ] verifylist = [ ('prefixes', ['10.0.10.0/24']), - ('project', identity_fakes_v3.project_name), - ('project_domain', identity_fakes_v3.domain_name), + ('project', self.project.name), + ('project_domain', self.domain.name), ('name', self._subnet_pool.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -207,7 +200,7 @@ class TestCreateSubnetPool(TestSubnetPool): self.network.create_subnet_pool.assert_called_once_with(**{ 'prefixes': ['10.0.10.0/24'], - 'tenant_id': identity_fakes_v3.project_id, + 'tenant_id': self.project.id, 'name': self._subnet_pool.name, }) self.assertEqual(self.columns, columns)