Disable mox usage by default in test helper

- Add use_mox = True to tests which still depend on mox
- Remove use_mox = False from all tests converted into mock
- Update the warning messages
- Replace 'S'-release with 'Stein'

Part of blueprint mock-framework-in-unit-tests
Change-Id: I208185e97b2de346304a09643a9cb3f1425a7683
Signed-off-by: Akihiro Motoki <amotoki@gmail.com>
This commit is contained in:
Akihiro Motoki 2018-03-31 21:12:25 +09:00
parent 41ef5a120c
commit 58019a7411
31 changed files with 81 additions and 79 deletions

View File

@ -140,6 +140,9 @@ class TestCase(django_test.TestCase):
def setUp(self):
super(TestCase, self).setUp()
if self.use_mox:
LOG.warning("'use_mox' will be dropped at the beginning of "
"'Stein' release. If you still depend on mox, "
"you must prepare mox environment in your test case.")
self.mox = mox.Mox()
self._setup_test_data()
self._setup_factory()

View File

@ -25,6 +25,9 @@ from openstack_dashboard.test import helpers as test
class FlavorsViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.nova: ('flavor_list_paged',),
flavors.Flavor: ('get_keys',), })
def test_index(self):
@ -177,6 +180,9 @@ class FlavorsViewTests(test.BaseAdminViewTests):
class BaseFlavorWorkflowTests(test.BaseAdminViewTests):
use_mox = True
def _flavor_create_params(self, flavor, id=None):
eph = getattr(flavor, 'OS-FLV-EXT-DATA:ephemeral')
flavor_info = {"name": flavor.name,

View File

@ -26,6 +26,8 @@ NETWORKS_DETAIL_URL = 'horizon:admin:networks:detail'
class NetworkAgentTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.neutron: ('agent_list',
'network_get',
'list_dhcp_agent_hosting_networks',)})

View File

@ -31,6 +31,8 @@ NETWORKS_DETAIL_URL = 'horizon:admin:networks:detail'
class NetworkPortTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.neutron: ('network_get',
'port_get',
'is_extension_supported',)})

View File

@ -32,6 +32,8 @@ NETWORKS_DETAIL_URL = 'horizon:admin:networks:detail'
class NetworkSubnetTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.neutron: ('network_get',
'subnet_get',
'is_extension_supported')})

View File

@ -31,6 +31,9 @@ INDEX_URL = reverse('horizon:admin:networks:index')
class NetworkTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.neutron: ('network_list',
'list_dhcp_agent_hosting_networks',
'is_extension_supported'),

View File

@ -38,6 +38,8 @@ INDEX_URL = reverse('horizon:project:overview:index')
class UsageViewTests(test.BaseAdminViewTests):
use_mox = True
def _stub_api_calls(self, nova_stu_enabled):
self.mox.StubOutWithMock(api.nova, 'usage_list')
self.mox.StubOutWithMock(api.nova, 'extension_supported')

View File

@ -36,6 +36,9 @@ GROUP_ROLE_PREFIX = constants.DOMAIN_GROUP_MEMBER_SLUG + "_role_"
class DomainsViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('domain_get',
'domain_list',)})
def test_index(self):
@ -175,6 +178,9 @@ class DomainsViewTests(test.BaseAdminViewTests):
class CreateDomainWorkflowTests(test.BaseAdminViewTests):
use_mox = True
def _get_domain_info(self, domain):
domain_info = {"name": domain.name,
"description": domain.description,
@ -218,6 +224,9 @@ class CreateDomainWorkflowTests(test.BaseAdminViewTests):
class UpdateDomainWorkflowTests(test.BaseAdminViewTests):
use_mox = True
def _get_domain_info(self, domain):
domain_info = {"domain_id": domain.id,
"name": domain.name,

View File

@ -32,6 +32,9 @@ GROUP_ADD_MEMBER_URL = reverse(constants.GROUPS_ADD_MEMBER_URL, args=[1])
class GroupsViewTests(test.BaseAdminViewTests):
use_mox = True
def _get_domain_id(self):
return self.request.session.get('domain_context', None)

View File

@ -31,6 +31,8 @@ PROTOCOLS_CREATE_URL = reverse(
class ProtocolsViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('mapping_list',
'protocol_create', )})
def test_create(self):

View File

@ -31,6 +31,9 @@ IDPS_DETAIL_URL = reverse('horizon:identity:identity_providers:detail',
class IdPsViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('identity_provider_list',)})
def test_index(self):
api.keystone.identity_provider_list(IgnoreArg()). \

View File

@ -31,6 +31,9 @@ MAPPINGS_UPDATE_URL = reverse('horizon:identity:mappings:update',
class MappingsViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('mapping_list',)})
def test_index(self):
api.keystone.mapping_list(IgnoreArg()). \

View File

@ -41,6 +41,9 @@ PROJECT_DETAIL_URL = reverse('horizon:identity:projects:detail', args=[1])
class TenantsViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('domain_get',
'tenant_list',
'domain_lookup'),
@ -102,6 +105,9 @@ class TenantsViewTests(test.BaseAdminViewTests):
class ProjectsViewNonAdminTests(test.TestCase):
use_mox = True
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
@test.create_stubs({api.keystone: ('tenant_list',
'domain_lookup')})
@ -125,6 +131,9 @@ class ProjectsViewNonAdminTests(test.TestCase):
class CreateProjectWorkflowTests(test.BaseAdminViewTests):
use_mox = True
def _get_project_info(self, project):
domain = self._get_default_domain()
project_info = {"name": project.name,
@ -443,6 +452,9 @@ class CreateProjectWorkflowTests(test.BaseAdminViewTests):
class UpdateProjectWorkflowTests(test.BaseAdminViewTests):
use_mox = True
def _get_all_users(self, domain_id):
if not domain_id:
users = self.users.list()
@ -870,6 +882,9 @@ class UpdateProjectWorkflowTests(test.BaseAdminViewTests):
class UpdateQuotasWorkflowTests(test.BaseAdminViewTests):
use_mox = True
def _get_quota_info(self, quota):
cinder_quota = self.cinder_quotas.first()
neutron_quota = self.neutron_quotas.first()
@ -1048,6 +1063,9 @@ class UpdateQuotasWorkflowTests(test.BaseAdminViewTests):
class UsageViewTests(test.BaseAdminViewTests):
use_mox = True
def _stub_nova_api_calls(self, nova_stu_enabled=True):
self.mox.StubOutWithMock(api.nova, 'usage_get')
self.mox.StubOutWithMock(api.nova, 'extension_supported')
@ -1100,6 +1118,9 @@ class UsageViewTests(test.BaseAdminViewTests):
class DetailProjectViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('tenant_get',),
quotas: ('enabled_quotas',)})
def test_detail_view(self):

View File

@ -29,6 +29,9 @@ INDEX_TEMPLATE = 'horizon/common/_data_table_view.html'
class RolesViewTests(test.BaseAdminViewTests):
use_mox = True
@test.create_stubs({api.keystone: ('role_list',)})
def test_index(self):
filters = {}

View File

@ -38,6 +38,9 @@ USER_CHANGE_PASSWORD_URL = reverse('horizon:identity:users:change_password',
class UsersViewTests(test.BaseAdminViewTests):
use_mox = True
def _get_default_domain(self):
domain = {"id": self.request.session.get('domain_context',
None),

View File

@ -76,8 +76,6 @@ class InstanceTestBase(helpers.ResetImageAPIVersionMixin,
InstanceTestHelperMixin,
helpers.TestCase):
use_mox = False
def setUp(self):
super(InstanceTestBase, self).setUp()
if api.glance.VERSIONS.active < 2:
@ -4988,8 +4986,6 @@ class InstanceAjaxTests(helpers.TestCase, InstanceTestHelperMixin):
class ConsoleManagerTests(helpers.ResetImageAPIVersionMixin, helpers.TestCase):
use_mox = False
def setup_consoles(self):
# Need to refresh with mocks or will fail since mox do not detect
# the api_call() as mocked.

View File

@ -84,8 +84,6 @@ class RouterMixin(object):
class RouterTestCase(object):
use_mox = False
@test.create_mocks({api.neutron: ('router_list',
'network_list',
'is_extension_supported'),
@ -298,8 +296,6 @@ class RouterActionTests(test.TestCase):
INDEX_URL = reverse('horizon:%s:routers:index' % DASHBOARD)
DETAIL_PATH = 'horizon:%s:routers:detail' % DASHBOARD
use_mox = False
@test.create_mocks({api.neutron: ('router_create',
'get_feature_permission',
'network_list',
@ -1050,8 +1046,6 @@ class RouterActionTests(test.TestCase):
class RouterRouteTestCase(object):
use_mox = False
@test.create_mocks({api.neutron: ('router_get',
'port_list',
'network_get',
@ -1165,8 +1159,6 @@ class RouterViewTests(RouterMixin, test.TestCase):
DASHBOARD = 'project'
INDEX_URL = reverse('horizon:%s:routers:index' % DASHBOARD)
use_mox = False
@test.create_mocks({api.neutron: ('router_list',
'network_list',
'is_extension_supported'),

View File

@ -261,11 +261,6 @@ class TestCase(horizon_helpers.TestCase):
# boolean variable to store failures
missing_mocks = False
# Most openstack_dashbaord tests depends on mox,
# we mark use_mox to True by default.
# Eventually we can drop this when mock migration has good progress.
use_mox = True
def fake_conn_request(self):
# print a stacktrace to illustrate where the unmocked API call
# is being made from
@ -493,8 +488,16 @@ class APITestCase(TestCase):
For use with tests which deal with the underlying clients rather than
stubbing out the openstack_dashboard.api.* methods.
"""
# NOTE: This test class depends on mox but does not declare use_mox = True
# to notify mox is no longer recommended.
# If a consumer would like to use this class, declare use_mox = True.
def setUp(self):
super(APITestCase, self).setUp()
LOG.warning("APITestCase has been deprecated in favor of mock usage "
"and will be removed at the beginning of 'Stein' release. "
"Please convert your to use APIMockTestCase instead.")
utils.patch_middleware_get_user()
def fake_keystoneclient(request, admin=False):
@ -547,7 +550,7 @@ class APITestCase(TestCase):
)
def stub_novaclient(self):
self._warn_client('nova', 'S')
self._warn_client('nova', 'Stein')
if not hasattr(self, "novaclient"):
self.mox.StubOutWithMock(nova_client, 'Client')
# mock the api_version since MockObject.__init__ ignores it.
@ -562,14 +565,14 @@ class APITestCase(TestCase):
return self.novaclient
def stub_cinderclient(self):
self._warn_client('cinder', 'S')
self._warn_client('cinder', 'Stein')
if not hasattr(self, "cinderclient"):
self.mox.StubOutWithMock(cinder_client, 'Client')
self.cinderclient = self.mox.CreateMock(cinder_client.Client)
return self.cinderclient
def stub_keystoneclient(self):
self._warn_client('keystone', 'S')
self._warn_client('keystone', 'Stein')
if not hasattr(self, "keystoneclient"):
self.mox.StubOutWithMock(keystone_client, 'Client')
# NOTE(saschpe): Mock properties, MockObject.__init__ ignores them:
@ -583,21 +586,21 @@ class APITestCase(TestCase):
return self.keystoneclient
def stub_glanceclient(self):
self._warn_client('glance', 'S')
self._warn_client('glance', 'Stein')
if not hasattr(self, "glanceclient"):
self.mox.StubOutWithMock(glanceclient, 'Client')
self.glanceclient = self.mox.CreateMock(glanceclient.Client)
return self.glanceclient
def stub_neutronclient(self):
self._warn_client('neutron', 'S')
self._warn_client('neutron', 'Stein')
if not hasattr(self, "neutronclient"):
self.mox.StubOutWithMock(neutron_client, 'Client')
self.neutronclient = self.mox.CreateMock(neutron_client.Client)
return self.neutronclient
def stub_swiftclient(self, expected_calls=1):
self._warn_client('swift', 'S')
self._warn_client('swift', 'Stein')
if not hasattr(self, "swiftclient"):
self.mox.StubOutWithMock(swift_client, 'Connection')
self.swiftclient = self.mox.CreateMock(swift_client.Connection)
@ -617,8 +620,6 @@ class APITestCase(TestCase):
class APIMockTestCase(TestCase):
use_mox = False
def setUp(self):
super(APIMockTestCase, self).setUp()
utils.patch_middleware_get_user()

View File

@ -25,8 +25,6 @@ from openstack_dashboard.usage import quotas
class CinderRestTestCase(test.TestCase):
use_mox = False
#
# Volumes
#

View File

@ -18,8 +18,6 @@ from openstack_dashboard.test import helpers as test
class ConfigRestTestCase(test.TestCase):
use_mox = False
def test_settings_config_get(self):
request = self.mock_rest_request()
response = config.Settings().get(request)

View File

@ -21,8 +21,6 @@ from openstack_dashboard.test import helpers as test
class ImagesRestTestCase(test.ResetImageAPIVersionMixin, test.TestCase):
use_mox = False
def setUp(self):
super(ImagesRestTestCase, self).setUp()
api.glance.VERSIONS.clear_active_cache()

View File

@ -24,8 +24,6 @@ from openstack_dashboard.test import helpers as test
class KeystoneRestTestCase(test.TestCase):
use_mox = False
#
# Version
#

View File

@ -20,8 +20,6 @@ from openstack_dashboard.test import helpers as test
class RestNetworkApiSecurityGroupTests(test.TestCase):
use_mox = False
@test.create_mocks({api.neutron: ['security_group_list']})
def test_security_group_detailed(self):
request = self.mock_rest_request()
@ -38,8 +36,6 @@ class RestNetworkApiSecurityGroupTests(test.TestCase):
class RestNetworkApiFloatingIpTests(test.TestCase):
use_mox = False
@test.create_mocks({api.neutron: ['tenant_floating_ip_list']})
def test_floating_ip_list(self):
request = self.mock_rest_request()

View File

@ -25,8 +25,6 @@ from openstack_dashboard.usage import quotas
class NeutronNetworksTestCase(test.TestCase):
use_mox = False
def _dictify_network(self, network):
net_dict = network.to_dict()
net_dict['subnets'] = [s.to_dict() for s in net_dict['subnets']]
@ -112,8 +110,6 @@ class NeutronNetworksTestCase(test.TestCase):
class NeutronSubnetsTestCase(test.TestCase):
use_mox = False
@mock.patch.object(api.neutron, 'subnet_list')
def test_get(self, mock_subnet_list):
network_id = self.networks.first().id
@ -146,8 +142,6 @@ class NeutronSubnetsTestCase(test.TestCase):
class NeutronPortsTestCase(test.TestCase):
use_mox = False
@mock.patch.object(api.neutron, 'port_list_with_trunk_types')
def test_get(self, mock_port_list_with_trunk_types):
network_id = self.networks.first().id
@ -162,8 +156,6 @@ class NeutronPortsTestCase(test.TestCase):
class NeutronTrunkTestCase(test.TestCase):
use_mox = False
@mock.patch.object(api.neutron, 'trunk_delete')
def test_trunk_delete(self, mock_trunk_delete):
mock_trunk_delete.return_value = None
@ -198,8 +190,6 @@ class NeutronTrunkTestCase(test.TestCase):
class NeutronTrunksTestCase(test.TestCase):
use_mox = False
@mock.patch.object(api.neutron, 'trunk_list')
def test_trunks_get(self, mock_trunk_list):
request = self.mock_rest_request(GET=django_request.QueryDict())
@ -227,8 +217,6 @@ class NeutronTrunksTestCase(test.TestCase):
class NeutronExtensionsTestCase(test.TestCase):
use_mox = False
@mock.patch.object(api.neutron, 'list_extensions')
def test_list_extensions(self, mock_list_extensions):
request = self.mock_rest_request(**{'GET': {}})
@ -241,8 +229,6 @@ class NeutronExtensionsTestCase(test.TestCase):
class NeutronDefaultQuotasTestCase(test.TestCase):
use_mox = False
@test.create_mocks({api.base: ['is_service_enabled'],
api.neutron: ['tenant_quota_get']})
def test_quotas_sets_defaults_get_when_service_is_enabled(self):
@ -283,8 +269,6 @@ class NeutronDefaultQuotasTestCase(test.TestCase):
class NeutronQuotaSetsTestCase(test.TestCase):
use_mox = False
@test.create_mocks({api.base: ['is_service_enabled'],
api.neutron: ['is_extension_supported',
'tenant_quota_update'],

View File

@ -28,8 +28,6 @@ from openstack_dashboard.usage import quotas
class NovaRestTestCase(test.TestCase):
use_mox = False
#
# Snapshots
#

View File

@ -20,8 +20,6 @@ from openstack_dashboard.test import helpers as test
class PolicyRestTestCase(test.TestCase):
use_mox = False
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
def _test_policy(self, body, expected=True):
request = self.mock_rest_request(body=body)

View File

@ -20,8 +20,6 @@ from openstack_dashboard.test import helpers as test
class SwiftRestTestCase(test.TestCase):
use_mox = False
#
# Version
#

View File

@ -18,8 +18,6 @@ from openstack_dashboard.test import helpers as test
class RestUtilsTestCase(test.TestCase):
use_mox = False
def test_api_success(self):
@utils.ajax()
def f(self, request):
@ -168,8 +166,6 @@ class RestUtilsTestCase(test.TestCase):
class JSONEncoderTestCase(test.TestCase):
use_mox = False
# NOTE(tsufiev): NaN numeric is "conventional" in a sense that the custom
# NaNJSONEncoder encoder translates it to the same token that the standard
# JSONEncoder encoder does

View File

@ -60,8 +60,6 @@ class APIDict(api_base.APIDictWrapper):
class APIVersionTests(test.TestCase):
use_mox = False
def test_equal(self):
version = api_base.Version('1.0')
self.assertEqual(1, version)
@ -109,8 +107,6 @@ class APIVersionTests(test.TestCase):
# Wrapper classes that only define _attrs don't need extra testing.
class APIResourceWrapperTests(test.TestCase):
use_mox = False
def test_get_attribute(self):
resource = APIResource.get_instance()
self.assertEqual('foo', resource.foo)
@ -138,8 +134,6 @@ class APIResourceWrapperTests(test.TestCase):
class APIDictWrapperTests(test.TestCase):
use_mox = False
# APIDict allows for both attribute access and dictionary style [element]
# style access. Test both
def test_get_item(self):
@ -208,8 +202,6 @@ class APIDictWrapperTests(test.TestCase):
class ApiVersionTests(test.TestCase):
use_mox = False
def setUp(self):
super(ApiVersionTests, self).setUp()
self.previous_settings = settings.OPENSTACK_API_VERSIONS
@ -247,8 +239,6 @@ class ApiVersionTests(test.TestCase):
class ApiHelperTests(test.TestCase):
"""Tests for functions that don't use one of the api objects."""
use_mox = False
def test_url_for(self):
url = api_base.url_for(self.request, 'image')
self.assertEqual('http://public.glance.example.com:9292', url)
@ -304,8 +294,6 @@ class ApiHelperTests(test.TestCase):
class QuotaSetTests(test.TestCase):
use_mox = False
def test_quotaset_add_with_plus(self):
quota_dict = {'foo': 1, 'bar': 10}
other_quota_dict = {'my_test': 12}

View File

@ -36,8 +36,6 @@ from openstack_dashboard.test import helpers as test
class ServerWrapperTests(test.TestCase):
use_mox = False
def test_get_base_attribute(self):
server = api.nova.Server(self.servers.first(), self.request)
self.assertEqual(self.servers.first().id, server.id)

View File

@ -34,8 +34,6 @@ from openstack_dashboard.usage import quotas
class QuotaTests(test.APITestCase):
use_mox = False
def _mock_service_enabled(self, compute_enabled=True,
network_enabled=False, volume_enabled=True):
services = {'network': network_enabled,