Merge "Replace all use of mox with mock"

This commit is contained in:
Jenkins 2014-04-04 19:33:30 +00:00 committed by Gerrit Code Review
commit 83ecce9ce0
6 changed files with 73 additions and 78 deletions

View File

@ -15,6 +15,7 @@
import uuid
import mock
import sqlalchemy
from sqlalchemy import exc
@ -23,7 +24,6 @@ from keystone import config
from keystone import exception
from keystone.identity.backends import sql as identity_sql
from keystone.openstack.common.db import exception as db_exception
from keystone.openstack.common.fixture import moxstubout
from keystone import tests
from keystone.tests import default_fixtures
from keystone.tests import test_backend
@ -333,42 +333,51 @@ class SqlToken(SqlTests, test_backend.TokenTests):
# This query used to be heavy with too many columns. We want
# to make sure it is only running with the minimum columns
# necessary.
fixture = self.useFixture(moxstubout.MoxStubout())
self.mox = fixture.mox
tok = token_sql.Token()
session = sql.get_session()
q = session.query(token_sql.TokenModel.id,
token_sql.TokenModel.expires)
self.mox.StubOutWithMock(session, 'query')
session.query(token_sql.TokenModel.id,
token_sql.TokenModel.expires).AndReturn(q)
self.mox.StubOutWithMock(sql, 'get_session')
sql.get_session().AndReturn(session)
self.mox.ReplayAll()
tok.list_revoked_tokens()
expected_query_args = (token_sql.TokenModel.id,
token_sql.TokenModel.expires)
with mock.patch.object(token_sql, 'sql') as mock_sql:
tok = token_sql.Token()
tok.list_revoked_tokens()
mock_query = mock_sql.get_session().query
mock_query.assert_called_with(*expected_query_args)
def test_flush_expired_tokens_batch(self):
# This test simply executes the code under test to verify
# that the code is legal. It is not possible to test
# whether records are deleted in batches using sqlite,
# because the limit function does not seem to affect
# delete subqueries; these are, however, legal.
# After several failed attempts of using mox, it would
# seem that the use of mock objects for testing
# the target code does not seem possible, because of
# the unique way the SQLAlchemy Query class's filter
# method works.
fixture = self.useFixture(moxstubout.MoxStubout())
self.mox = fixture.mox
tok = token_sql.Token()
self.mox.StubOutWithMock(tok, 'token_flush_batch_size')
# Just need a batch larger than 0; note that the code
# path with batch_size = 0 is covered by test_backend,
# where all backends' flush_expired_tokens methods
# are tested.
tok.token_flush_batch_size('sqlite').AndReturn(1)
self.mox.ReplayAll()
tok.flush_expired_tokens()
# TODO(dstanek): This test should be rewritten to be less
# brittle. The code will likely need to be changed first. I
# just copied the spirit of the existing test when I rewrote
# mox -> mock. These tests are brittle because they have the
# call structure for SQLAlchemy encoded in them.
# test sqlite dialect
with mock.patch.object(token_sql, 'sql') as mock_sql:
mock_sql.get_session().bind.dialect.name = 'sqlite'
tok = token_sql.Token()
tok.flush_expired_tokens()
self.assertFalse(mock_sql.get_session().query().filter().limit.called)
def test_flush_expired_tokens_batch_ibm_db_sa(self):
# TODO(dstanek): This test should be rewritten to be less
# brittle. The code will likely need to be changed first. I
# just copied the spirit of the existing test when I rewrote
# mox -> mock. These tests are brittle because they have the
# call structure for SQLAlchemy encoded in them.
# test ibm_db_sa
with mock.patch.object(token_sql, 'sql') as mock_sql:
# NOTE(dstanek): this will allow us to break out of the
# 'while True' loop
mock_sql.get_session().query().filter().delete.return_value = 0
mock_sql.get_session().bind.dialect.name = 'ibm_db_sa'
tok = token_sql.Token()
tok.flush_expired_tokens()
mock_limit = mock_sql.get_session().query().filter().limit
mock_limit.assert_called_with(100)
def test_token_flush_batch_size_default(self):
tok = token_sql.Token()

View File

@ -19,7 +19,7 @@ from oslo.config import cfg
from keystone.common import dependency
from keystone import notifications
from keystone.openstack.common.fixture import moxstubout
from keystone.openstack.common.fixture import mockpatch
from keystone import tests
from keystone.tests import test_v3
@ -48,10 +48,8 @@ class NotificationsWrapperTestCase(tests.TestCase):
self.assertEqual(self.exp_resource_id, resource_id)
self.send_notification_called = True
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
self.stubs.Set(notifications, '_send_notification', fake_notify)
self.useFixture(mockpatch.PatchObject(
notifications, '_send_notification', fake_notify))
@notifications.created(EXP_RESOURCE_TYPE)
def create_resource(self, resource_id, data):
@ -123,8 +121,6 @@ class NotificationsWrapperTestCase(tests.TestCase):
class NotificationsTestCase(tests.TestCase):
def setUp(self):
super(NotificationsTestCase, self).setUp()
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
# these should use self.config_fixture.config(), but they haven't
# been registered yet
@ -177,10 +173,8 @@ class NotificationsForEntities(test_v3.RestfulTestCase):
'public': public}
self._notifications.append(note)
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
self.stubs.Set(notifications, '_send_notification', fake_notify)
self.useFixture(mockpatch.PatchObject(
notifications, '_send_notification', fake_notify))
def _assertNotifySeen(self, resource_id, operation, resource_type):
self.assertIn(operation, self.exp_operations)
@ -462,11 +456,8 @@ class CadfNotificationsWrapperTestCase(test_v3.RestfulTestCase):
'send_notification_called': True}
self._notifications.append(note)
# TODO(stevemar): Look into using mock instead of mox
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
self.stubs.Set(notifications, '_send_audit_notification',
fake_notify)
self.useFixture(mockpatch.PatchObject(
notifications, '_send_audit_notification', fake_notify))
def _assertLastNotify(self, action, user_id):
self.assertTrue(self._notifications)

View File

@ -16,13 +16,13 @@
import json
import tempfile
import mock
import six
from six.moves.urllib import request as urlrequest
from testtools import matchers
from keystone import config
from keystone import exception
from keystone.openstack.common.fixture import moxstubout
from keystone.openstack.common import policy as common_policy
from keystone.policy.backends import rules
from keystone import tests
@ -86,9 +86,6 @@ class PolicyTestCase(tests.TestCase):
self.credentials = {}
self.target = {}
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
def _set_rules(self):
these_rules = common_policy.Rules(
dict((k, common_policy.parse_rule(v))
@ -114,21 +111,22 @@ class PolicyTestCase(tests.TestCase):
def fakeurlopen(url, post_data):
return six.StringIO("True")
self.stubs.Set(urlrequest, 'urlopen', fakeurlopen)
action = "example:get_http"
target = {}
result = rules.enforce(self.credentials, action, target)
with mock.patch.object(urlrequest, 'urlopen', fakeurlopen):
result = rules.enforce(self.credentials, action, target)
self.assertTrue(result)
def test_enforce_http_false(self):
def fakeurlopen(url, post_data):
return six.StringIO("False")
self.stubs.Set(urlrequest, 'urlopen', fakeurlopen)
action = "example:get_http"
target = {}
self.assertRaises(exception.ForbiddenAction, rules.enforce,
self.credentials, action, target)
with mock.patch.object(urlrequest, 'urlopen', fakeurlopen):
self.assertRaises(exception.ForbiddenAction, rules.enforce,
self.credentials, action, target)
def test_templatized_enforcement(self):
target_mine = {'project_id': 'fake'}

View File

@ -15,9 +15,10 @@
import random
import mock
from keystone import config
from keystone import controllers
from keystone.openstack.common.fixture import moxstubout
from keystone.openstack.common import jsonutils
from keystone import tests
from keystone.tests import matchers
@ -122,9 +123,6 @@ class VersionTestCase(tests.TestCase):
public_endpoint='http://localhost:%(public_port)d',
admin_endpoint='http://localhost:%(admin_port)d')
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
def config_overrides(self):
super(VersionTestCase, self).config_overrides()
port = random.randint(10000, 30000)
@ -246,8 +244,8 @@ class VersionTestCase(tests.TestCase):
self._paste_in_port(expected['version'], 'http://localhost/v3/')
self.assertEqual(data, expected)
@mock.patch.object(controllers, '_VERSIONS', ['v3'])
def test_v2_disabled(self):
self.stubs.Set(controllers, '_VERSIONS', ['v3'])
client = self.client(self.public_app)
# request to /v2.0 should fail
resp = client.get('/v2.0/')
@ -277,8 +275,8 @@ class VersionTestCase(tests.TestCase):
data = jsonutils.loads(resp.body)
self.assertEqual(data, v3_only_response)
@mock.patch.object(controllers, '_VERSIONS', ['v2.0'])
def test_v3_disabled(self):
self.stubs.Set(controllers, '_VERSIONS', ['v2.0'])
client = self.client(self.public_app)
# request to /v3 should fail
resp = client.get('/v3/')
@ -380,9 +378,6 @@ vnd.openstack.identity-v3+xml"/>
public_endpoint='http://localhost:%(public_port)d',
admin_endpoint='http://localhost:%(admin_port)d')
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
def config_overrides(self):
super(XmlVersionTestCase, self).config_overrides()
port = random.randint(10000, 30000)
@ -444,8 +439,8 @@ vnd.openstack.identity-v3+xml"/>
expected = self.v3_VERSION_RESPONSE % dict(port=CONF.admin_port)
self.assertThat(data, matchers.XMLEquals(expected))
@mock.patch.object(controllers, '_VERSIONS', ['v3'])
def test_v2_disabled(self):
self.stubs.Set(controllers, '_VERSIONS', ['v3'])
client = self.client(self.public_app)
# request to /v3 should pass
@ -467,8 +462,8 @@ vnd.openstack.identity-v3+xml"/>
data = resp.body
self.assertThat(data, matchers.XMLEquals(v3_only_response))
@mock.patch.object(controllers, '_VERSIONS', ['v2.0'])
def test_v3_disabled(self):
self.stubs.Set(controllers, '_VERSIONS', ['v2.0'])
client = self.client(self.public_app)
# request to /v2.0 should pass

View File

@ -23,7 +23,7 @@ import webob
from keystone.common import environment
from keystone.common import wsgi
from keystone import exception
from keystone.openstack.common.fixture import moxstubout
from keystone.openstack.common.fixture import mockpatch
from keystone.openstack.common import gettextutils
from keystone.openstack.common.gettextutils import _
from keystone.openstack.common import jsonutils
@ -216,15 +216,13 @@ class LocalizedResponseTest(tests.TestCase):
gettextutils._AVAILABLE_LANGUAGES.clear()
self.addCleanup(gettextutils._AVAILABLE_LANGUAGES.clear)
fixture = self.useFixture(moxstubout.MoxStubout())
self.stubs = fixture.stubs
def _set_expected_languages(self, all_locales, avail_locales=None):
# Override localedata.locale_identifiers to return some locales.
def returns_some_locales(*args, **kwargs):
return all_locales
self.stubs.Set(localedata, 'locale_identifiers', returns_some_locales)
self.useFixture(mockpatch.PatchObject(
localedata, 'locale_identifiers', returns_some_locales))
# Override gettext.find to return other than None for some languages.
def fake_gettext_find(lang_id, *args, **kwargs):
@ -237,7 +235,8 @@ class LocalizedResponseTest(tests.TestCase):
return found_ret
return None
self.stubs.Set(gettext, 'find', fake_gettext_find)
self.useFixture(mockpatch.PatchObject(
gettext, 'find', fake_gettext_find))
def test_request_match_default(self):
# The default language if no Accept-Language is provided is None

View File

@ -20,13 +20,16 @@ coverage>=3.6
fixtures>=0.3.14
# mock object framework
mock>=1.0
mox>=0.5.3
oslotest
# required to build documentation
sphinx>=1.1.2,<1.2
# test wsgi apps without starting an http server
WebTest>=2.0
# mox was removed in favor of mock. We should not re-enable this module. See
# discussion: http://lists.openstack.org/pipermail/openstack-dev/2013-July/012484.html
#mox>=0.5.3
discover
python-subunit>=0.0.18
testrepository>=0.0.18