From 0021c29a758ed4fcbbf43ef9fe160b4db22e2d29 Mon Sep 17 00:00:00 2001 From: Jordan Pittier Date: Tue, 29 Mar 2016 21:33:34 +0200 Subject: [PATCH] Replace Mox with Mock According to [1] "New test cases should be implemented using Mock. It is part of the Python standard library in Python 3 and as such is the preferred method for OpenStack." So let's kill Mox in favor of Mock. [1] http://docs.openstack.org/infra/manual/developers.html#peer-review Change-Id: I0db1cf3969a8d5ae90e1a13330799e69c7751207 --- .../tests/cmd/test_verify_tempest_config.py | 3 ++- tempest/tests/common/test_admin_available.py | 3 ++- tempest/tests/common/test_alt_available.py | 3 ++- tempest/tests/common/test_configured_creds.py | 7 ++++--- tempest/tests/common/test_credentials.py | 3 ++- tempest/tests/common/test_dynamic_creds.py | 7 ++++--- tempest/tests/common/test_preprov_creds.py | 13 +++++++------ .../common/utils/linux/test_remote_client.py | 3 ++- tempest/tests/lib/base.py | 19 ++++++++++++------- tempest/tests/lib/test_auth.py | 10 +++++----- tempest/tests/lib/test_credentials.py | 4 ++-- tempest/tests/lib/test_rest_client.py | 4 +--- .../tests/negative/test_negative_auto_test.py | 3 ++- tempest/tests/test_decorators.py | 3 ++- tempest/tests/test_microversions.py | 4 ++-- tempest/tests/test_negative_rest_client.py | 3 ++- test-requirements.txt | 1 - 17 files changed, 53 insertions(+), 40 deletions(-) diff --git a/tempest/tests/cmd/test_verify_tempest_config.py b/tempest/tests/cmd/test_verify_tempest_config.py index 9df07a160f..330c393f50 100644 --- a/tempest/tests/cmd/test_verify_tempest_config.py +++ b/tempest/tests/cmd/test_verify_tempest_config.py @@ -41,7 +41,8 @@ class TestDiscovery(base.TestCase): def setUp(self): super(TestDiscovery, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) def test_get_keystone_api_versions(self): self.useFixture(mockpatch.PatchObject( diff --git a/tempest/tests/common/test_admin_available.py b/tempest/tests/common/test_admin_available.py index c8035413b4..7461c4b411 100644 --- a/tempest/tests/common/test_admin_available.py +++ b/tempest/tests/common/test_admin_available.py @@ -28,7 +28,8 @@ class TestAdminAvailable(base.TestCase): def setUp(self): super(TestAdminAvailable, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) def run_test(self, dynamic_creds, use_accounts_file, admin_creds): diff --git a/tempest/tests/common/test_alt_available.py b/tempest/tests/common/test_alt_available.py index cb1de16669..1a36068c5f 100644 --- a/tempest/tests/common/test_alt_available.py +++ b/tempest/tests/common/test_alt_available.py @@ -28,7 +28,8 @@ class TestAltAvailable(base.TestCase): def setUp(self): super(TestAltAvailable, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) def run_test(self, dynamic_creds, use_accounts_file, creds): diff --git a/tempest/tests/common/test_configured_creds.py b/tempest/tests/common/test_configured_creds.py index 3c104b2e48..2ce354d50f 100644 --- a/tempest/tests/common/test_configured_creds.py +++ b/tempest/tests/common/test_configured_creds.py @@ -41,9 +41,10 @@ class ConfiguredV2CredentialsTests(base.TestCase): def setUp(self): super(ConfiguredV2CredentialsTests, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) - self.stubs.Set(self.tokenclient_class, 'raw_request', - self.identity_response) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) + self.patchobject(self.tokenclient_class, 'raw_request', + self.identity_response) def _get_credentials(self, attributes=None): if attributes is None: diff --git a/tempest/tests/common/test_credentials.py b/tempest/tests/common/test_credentials.py index 6fc490e4b2..3d09d9f8b2 100644 --- a/tempest/tests/common/test_credentials.py +++ b/tempest/tests/common/test_credentials.py @@ -26,7 +26,8 @@ class TestLegacyCredentialsProvider(base.TestCase): def setUp(self): super(TestLegacyCredentialsProvider, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) def test_get_creds_roles_legacy_invalid(self): test_accounts_class = credentials.LegacyCredentialProvider( diff --git a/tempest/tests/common/test_dynamic_creds.py b/tempest/tests/common/test_dynamic_creds.py index e1d9023b24..1488793ac2 100644 --- a/tempest/tests/common/test_dynamic_creds.py +++ b/tempest/tests/common/test_dynamic_creds.py @@ -46,9 +46,10 @@ class TestDynamicCredentialProvider(base.TestCase): def setUp(self): super(TestDynamicCredentialProvider, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) - self.stubs.Set(json_token_client.TokenClient, 'raw_request', - fake_identity._fake_v2_response) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) + self.patchobject(json_token_client.TokenClient, 'raw_request', + fake_identity._fake_v2_response) cfg.CONF.set_default('operator_role', 'FakeRole', group='object-storage') self._mock_list_ec2_credentials('fake_user_id', 'fake_tenant_id') diff --git a/tempest/tests/common/test_preprov_creds.py b/tempest/tests/common/test_preprov_creds.py index 36d6c3db4e..fba5b1f292 100644 --- a/tempest/tests/common/test_preprov_creds.py +++ b/tempest/tests/common/test_preprov_creds.py @@ -46,9 +46,10 @@ class TestPreProvisionedCredentials(base.TestCase): def setUp(self): super(TestPreProvisionedCredentials, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) - self.stubs.Set(token_client.TokenClient, 'raw_request', - fake_identity._fake_v2_response) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) + self.patchobject(token_client.TokenClient, 'raw_request', + fake_identity._fake_v2_response) self.useFixture(lockutils_fixtures.ExternalLockFixture()) self.test_accounts = [ {'username': 'test_user1', 'tenant_name': 'test_tenant1', @@ -96,8 +97,8 @@ class TestPreProvisionedCredentials(base.TestCase): return hash_list def test_get_hash(self): - self.stubs.Set(token_client.TokenClient, 'raw_request', - fake_identity._fake_v2_response) + self.patchobject(token_client.TokenClient, 'raw_request', + fake_identity._fake_v2_response) test_account_class = preprov_creds.PreProvisionedCredentialProvider( **self.fixed_params) hash_list = self._get_hash_list(self.test_accounts) @@ -188,7 +189,7 @@ class TestPreProvisionedCredentials(base.TestCase): return False return True - self.stubs.Set(os.path, 'isfile', _fake_is_file) + self.patchobject(os.path, 'isfile', _fake_is_file) with mock.patch('six.moves.builtins.open', mock.mock_open(), create=True) as open_mock: test_account_class._get_free_hash(hash_list) diff --git a/tempest/tests/common/utils/linux/test_remote_client.py b/tempest/tests/common/utils/linux/test_remote_client.py index 22cf47a76c..b3bba72b3e 100644 --- a/tempest/tests/common/utils/linux/test_remote_client.py +++ b/tempest/tests/common/utils/linux/test_remote_client.py @@ -27,7 +27,8 @@ class TestRemoteClient(base.TestCase): def setUp(self): super(TestRemoteClient, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) cfg.CONF.set_default('ip_version_for_ssh', 4, group='validation') cfg.CONF.set_default('network_for_ssh', 'public', group='validation') cfg.CONF.set_default('connect_timeout', 1, group='validation') diff --git a/tempest/tests/lib/base.py b/tempest/tests/lib/base.py index fe9268e766..ca81d4d605 100644 --- a/tempest/tests/lib/base.py +++ b/tempest/tests/lib/base.py @@ -14,17 +14,10 @@ import mock from oslotest import base -from oslotest import moxstubout class TestCase(base.BaseTestCase): - def setUp(self): - super(TestCase, self).setUp() - mox_fixture = self.useFixture(moxstubout.MoxStubout()) - self.mox = mox_fixture.mox - self.stubs = mox_fixture.stubs - def patch(self, target, **kwargs): """Returns a started `mock.patch` object for the supplied target. @@ -42,3 +35,15 @@ class TestCase(base.BaseTestCase): m = p.start() self.addCleanup(p.stop) return m + + def patchobject(self, target, attribute, new=mock.DEFAULT): + """Convenient wrapper around `mock.patch.object` + + Returns a started mock that will be automatically stopped after the + test ran. + """ + + p = mock.patch.object(target, attribute, new) + m = p.start() + self.addCleanup(p.stop) + return m diff --git a/tempest/tests/lib/test_auth.py b/tempest/tests/lib/test_auth.py index df571479e5..2724a428eb 100644 --- a/tempest/tests/lib/test_auth.py +++ b/tempest/tests/lib/test_auth.py @@ -41,7 +41,7 @@ class BaseAuthTestsSetUp(base.TestCase): def setUp(self): super(BaseAuthTestsSetUp, self).setUp() - self.stubs.Set(auth, 'get_credentials', fake_get_credentials) + self.patchobject(auth, 'get_credentials', fake_get_credentials) self.auth_provider = self._auth(self.credentials, fake_identity.FAKE_AUTH_URL) @@ -118,8 +118,8 @@ class TestKeystoneV2AuthProvider(BaseAuthTestsSetUp): def setUp(self): super(TestKeystoneV2AuthProvider, self).setUp() - self.stubs.Set(v2_client.TokenClient, 'raw_request', - fake_identity._fake_v2_response) + self.patchobject(v2_client.TokenClient, 'raw_request', + fake_identity._fake_v2_response) self.target_url = 'test_api' def _get_fake_identity(self): @@ -433,8 +433,8 @@ class TestKeystoneV3AuthProvider(TestKeystoneV2AuthProvider): def setUp(self): super(TestKeystoneV3AuthProvider, self).setUp() - self.stubs.Set(v3_client.V3TokenClient, 'raw_request', - fake_identity._fake_v3_response) + self.patchobject(v3_client.V3TokenClient, 'raw_request', + fake_identity._fake_v3_response) def _get_fake_identity(self): return fake_identity.IDENTITY_V3_RESPONSE['token'] diff --git a/tempest/tests/lib/test_credentials.py b/tempest/tests/lib/test_credentials.py index 791fbb5a7c..b8acf83a7e 100644 --- a/tempest/tests/lib/test_credentials.py +++ b/tempest/tests/lib/test_credentials.py @@ -72,8 +72,8 @@ class KeystoneV2CredentialsTests(CredentialsTests): def setUp(self): super(KeystoneV2CredentialsTests, self).setUp() - self.stubs.Set(self.tokenclient_class, 'raw_request', - self.identity_response) + self.patchobject(self.tokenclient_class, 'raw_request', + self.identity_response) def _verify_credentials(self, credentials_class, creds_dict, filled=True): creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL, diff --git a/tempest/tests/lib/test_rest_client.py b/tempest/tests/lib/test_rest_client.py index 90651b06c3..572b4462ce 100644 --- a/tempest/tests/lib/test_rest_client.py +++ b/tempest/tests/lib/test_rest_client.py @@ -37,9 +37,7 @@ class BaseRestClientTestClass(base.TestCase): self.fake_auth_provider = fake_auth_provider.FakeAuthProvider() self.rest_client = rest_client.RestClient( self.fake_auth_provider, None, None) - self.stubs.Set(http.ClosingHttp, 'request', - self.fake_http.request) - + self.patchobject(http.ClosingHttp, 'request', self.fake_http.request) self.useFixture(mockpatch.PatchObject(self.rest_client, '_log_request')) diff --git a/tempest/tests/negative/test_negative_auto_test.py b/tempest/tests/negative/test_negative_auto_test.py index c666bd302d..179546663d 100644 --- a/tempest/tests/negative/test_negative_auto_test.py +++ b/tempest/tests/negative/test_negative_auto_test.py @@ -37,7 +37,8 @@ class TestNegativeAutoTest(base.TestCase): def setUp(self): super(TestNegativeAutoTest, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) def _check_prop_entries(self, result, entry): entries = [a for a in result if entry in a[0]] diff --git a/tempest/tests/test_decorators.py b/tempest/tests/test_decorators.py index 4c9a3b7c0f..e3dc53af0b 100644 --- a/tempest/tests/test_decorators.py +++ b/tempest/tests/test_decorators.py @@ -30,7 +30,8 @@ class BaseDecoratorsTest(base.TestCase): def setUp(self): super(BaseDecoratorsTest, self).setUp() self.config_fixture = self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) class TestAttrDecorator(BaseDecoratorsTest): diff --git a/tempest/tests/test_microversions.py b/tempest/tests/test_microversions.py index 1ac1232de9..b80cf803a2 100644 --- a/tempest/tests/test_microversions.py +++ b/tempest/tests/test_microversions.py @@ -57,8 +57,8 @@ class TestMicroversionsTestsClass(base.TestCase): def setUp(self): super(TestMicroversionsTestsClass, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', - fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) def _test_version(self, cfg_min, cfg_max, expected_pass_tests, diff --git a/tempest/tests/test_negative_rest_client.py b/tempest/tests/test_negative_rest_client.py index a1b5f0ee81..e46649c0ee 100644 --- a/tempest/tests/test_negative_rest_client.py +++ b/tempest/tests/test_negative_rest_client.py @@ -32,7 +32,8 @@ class TestNegativeRestClient(base.TestCase): def setUp(self): super(TestNegativeRestClient, self).setUp() self.useFixture(fake_config.ConfigFixture()) - self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) + self.patchobject(config, 'TempestConfigPrivate', + fake_config.FakePrivate) self.negative_rest_client = negative_rest_client.NegativeRestClient( fake_auth_provider.FakeAuthProvider(), None) self.useFixture(mockpatch.PatchObject(self.negative_rest_client, diff --git a/test-requirements.txt b/test-requirements.txt index bb4b27f636..9ef956a899 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,7 +7,6 @@ sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD python-subunit>=0.0.18 # Apache-2.0/BSD oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0 reno>=1.6.2 # Apache2 -mox>=0.5.3 # Apache-2.0 mock>=1.2 # BSD coverage>=3.6 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0