Fix leaked mocks in the 'common' module if any
Make all test suites in the module extend 'trove_testtools.TestCase' to enable dangling mock detector. Change-Id: I378878a2e3129c2d694695fbe3bb4174826386be Partial-Bug: 1448273
This commit is contained in:
@@ -13,16 +13,16 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from hashlib import md5
|
||||
from mock import MagicMock, patch
|
||||
import httplib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import uuid
|
||||
import logging
|
||||
import swiftclient.client as swift_client
|
||||
import swiftclient
|
||||
from hashlib import md5
|
||||
from mock import MagicMock
|
||||
import swiftclient.client as swift_client
|
||||
import uuid
|
||||
|
||||
from swiftclient import client as swift
|
||||
|
||||
@@ -203,7 +203,35 @@ class FakeSwiftConnection(object):
|
||||
pass
|
||||
|
||||
|
||||
class SwiftClientStub(object):
|
||||
class Patcher(object):
|
||||
"""Objects that need to mock global symbols throughout their existence
|
||||
should extend this base class.
|
||||
The object acts as a context manager which, when used in conjunction with
|
||||
the 'with' statement, terminates all running patchers when it leaves the
|
||||
scope.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.__patchers = None
|
||||
|
||||
def __enter__(self):
|
||||
self.__patchers = []
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
# Stop patchers in the LIFO order.
|
||||
while self.__patchers:
|
||||
self.__patchers.pop().stop()
|
||||
|
||||
def _start_patcher(self, patcher):
|
||||
"""All patchers started by this method will be automatically
|
||||
terminated on __exit__().
|
||||
"""
|
||||
self.__patchers.append(patcher)
|
||||
return patcher.start()
|
||||
|
||||
|
||||
class SwiftClientStub(Patcher):
|
||||
"""
|
||||
Component for controlling behavior of Swift Client Stub. Instantiated
|
||||
before tests are invoked in "fake" mode. Invoke methods to control
|
||||
@@ -257,6 +285,7 @@ class SwiftClientStub(object):
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(SwiftClientStub, self).__init__()
|
||||
self._connection = swift_client.Connection()
|
||||
self._containers = {}
|
||||
self._containers_list = []
|
||||
@@ -296,7 +325,7 @@ class SwiftClientStub(object):
|
||||
'content-type': 'application/json; charset=utf-8',
|
||||
'x-account-object-count': '0'}, self._containers_list)
|
||||
|
||||
swift_client.Connection.get_auth = MagicMock(return_value=(
|
||||
get_auth_return_value = (
|
||||
u"http://127.0.0.1:8080/v1/AUTH_c7b038976df24d96bf1980f5da17bd89",
|
||||
u'MIINrwYJKoZIhvcNAQcCoIINoDCCDZwCAQExCTAHBgUrDgMCGjCCDIgGCSqGSIb3'
|
||||
u'DQEHAaCCDHkEggx1eyJhY2Nlc3MiOiB7InRva2VuIjogeyJpc3N1ZWRfYXQiOiAi'
|
||||
@@ -305,9 +334,17 @@ class SwiftClientStub(object):
|
||||
u'ImVuYWJsZWQiOiB0cnVlLCAiZGVzY3JpcHRpb24iOiBudWxsLCAibmFtZSI6ICJy'
|
||||
u'ZWRkd2FyZiIsICJpZCI6ICJjN2IwMzg5NzZkZjI0ZDk2YmYxOTgwZjVkYTE3YmQ4'
|
||||
u'OSJ9fSwgInNlcnZpY2VDYXRhbG9nIjogW3siZW5kcG9pbnRzIjogW3siYWRtaW5')
|
||||
)
|
||||
swift_client.Connection.get_account = MagicMock(
|
||||
return_value=account_resp())
|
||||
|
||||
get_auth_patcher = patch.object(
|
||||
swift_client.Connection, 'get_auth',
|
||||
MagicMock(return_value=get_auth_return_value))
|
||||
self._start_patcher(get_auth_patcher)
|
||||
|
||||
get_account_patcher = patch.object(
|
||||
swift_client.Connection, 'get_account',
|
||||
MagicMock(return_value=account_resp()))
|
||||
self._start_patcher(get_account_patcher)
|
||||
|
||||
return self
|
||||
|
||||
def _create_container(self, container_name):
|
||||
@@ -351,7 +388,9 @@ class SwiftClientStub(object):
|
||||
self._objects[container])
|
||||
|
||||
# if this is called multiple times then nothing happens
|
||||
swift_client.Connection.put_container = MagicMock(return_value=None)
|
||||
put_container_patcher = patch.object(swift_client.Connection,
|
||||
'put_container')
|
||||
self._start_patcher(put_container_patcher)
|
||||
|
||||
def side_effect_func(*args, **kwargs):
|
||||
if args[0] in self._containers:
|
||||
@@ -362,8 +401,10 @@ class SwiftClientStub(object):
|
||||
|
||||
self._create_container(container_name)
|
||||
# return container headers
|
||||
swift_client.Connection.get_container = MagicMock(
|
||||
side_effect=side_effect_func)
|
||||
get_container_patcher = patch.object(
|
||||
swift_client.Connection, 'get_container',
|
||||
MagicMock(side_effect=side_effect_func))
|
||||
self._start_patcher(get_container_patcher)
|
||||
|
||||
return self
|
||||
|
||||
@@ -411,8 +452,10 @@ class SwiftClientStub(object):
|
||||
:param contents: the contents of the object
|
||||
"""
|
||||
|
||||
swift_client.Connection.put_object = MagicMock(
|
||||
return_value=uuid.uuid1())
|
||||
put_object_patcher = patch.object(
|
||||
swift_client.Connection, 'put_object',
|
||||
MagicMock(return_value=uuid.uuid1()))
|
||||
self._start_patcher(put_object_patcher)
|
||||
|
||||
def side_effect_func(*args, **kwargs):
|
||||
if (args[0] in self._containers and
|
||||
@@ -432,8 +475,10 @@ class SwiftClientStub(object):
|
||||
raise swiftclient.ClientException('Resource Not Found',
|
||||
http_status=404)
|
||||
|
||||
swift_client.Connection.get_object = MagicMock(
|
||||
side_effect=side_effect_func)
|
||||
get_object_patcher = patch.object(
|
||||
swift_client.Connection, 'get_object',
|
||||
MagicMock(side_effect=side_effect_func))
|
||||
self._start_patcher(get_object_patcher)
|
||||
|
||||
self._remove_object(name, self._objects[container])
|
||||
self._objects[container].append(
|
||||
@@ -471,8 +516,10 @@ class SwiftClientStub(object):
|
||||
else:
|
||||
return None
|
||||
|
||||
swift_client.Connection.delete_object = MagicMock(
|
||||
side_effect=side_effect_func)
|
||||
delete_object_patcher = patch.object(
|
||||
swift_client.Connection, 'delete_object',
|
||||
MagicMock(side_effect=side_effect_func))
|
||||
self._start_patcher(delete_object_patcher)
|
||||
|
||||
self._remove_object(name, self._objects[container])
|
||||
return self
|
||||
|
||||
@@ -13,13 +13,12 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
import trove.common.context as context
|
||||
|
||||
import testtools
|
||||
from testtools.matchers import Equals, Is
|
||||
from trove.common import context
|
||||
from trove.tests.unittests import trove_testtools
|
||||
|
||||
|
||||
class TestTroveContext(testtools.TestCase):
|
||||
class TestTroveContext(trove_testtools.TestCase):
|
||||
def test_create_with_extended_args(self):
|
||||
expected_service_catalog = {'key': 'value'}
|
||||
ctx = context.TroveContext(user="test_user_id",
|
||||
|
||||
@@ -11,13 +11,11 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from testtools import TestCase
|
||||
from trove.common.exception import TroveError
|
||||
from trove.tests.unittests import trove_testtools
|
||||
|
||||
|
||||
class TroveErrorTest(TestCase):
|
||||
class TroveErrorTest(trove_testtools.TestCase):
|
||||
|
||||
def test_valid_error_message_format(self):
|
||||
error = TroveError("%02d" % 1)
|
||||
|
||||
@@ -15,12 +15,11 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
|
||||
import testtools
|
||||
from trove.common.pagination import PaginatedDataView
|
||||
from trove.tests.unittests import trove_testtools
|
||||
|
||||
|
||||
class TestPaginatedDataView(testtools.TestCase):
|
||||
class TestPaginatedDataView(trove_testtools.TestCase):
|
||||
|
||||
def test_creation_with_string_marker(self):
|
||||
view = PaginatedDataView("TestType", [], "http://current_page",
|
||||
|
||||
@@ -15,37 +15,33 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
|
||||
from mock import MagicMock
|
||||
import testtools
|
||||
from testtools import matchers
|
||||
|
||||
from mock import patch, MagicMock
|
||||
from testtools import ExpectedException, matchers
|
||||
from trove.common import cfg
|
||||
from trove.common import exception
|
||||
from trove.common import remote
|
||||
from trove.common.context import TroveContext
|
||||
from trove.tests.fakes.swift import SwiftClientStub
|
||||
from trove.tests.unittests import trove_testtools
|
||||
import swiftclient.client
|
||||
import uuid
|
||||
|
||||
from trove.tests.fakes.swift import SwiftClientStub
|
||||
from trove.common.context import TroveContext
|
||||
from trove.common import remote
|
||||
from trove.common import exception
|
||||
from trove.common import cfg
|
||||
|
||||
|
||||
class TestRemote(testtools.TestCase):
|
||||
class TestRemote(trove_testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestRemote, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestRemote, self).tearDown()
|
||||
|
||||
def test_creation(self):
|
||||
swiftclient.client.Connection.get_auth = MagicMock(return_value=None)
|
||||
conn = swiftclient.client.Connection()
|
||||
self.assertIsNone(conn.get_auth())
|
||||
@patch.object(swiftclient.client.Connection, 'get_auth')
|
||||
def test_creation(self, get_auth_mock):
|
||||
self.assertIsNotNone(swiftclient.client.Connection())
|
||||
|
||||
def test_create_swift_client(self):
|
||||
mock_resp = MagicMock()
|
||||
swiftclient.client.Connection.get_container = MagicMock(
|
||||
return_value=["text", mock_resp])
|
||||
with patch.object(swiftclient.client.Connection, 'get_container',
|
||||
MagicMock(return_value=["text", mock_resp])):
|
||||
service_catalog = [{'endpoints': [{'region': 'RegionOne',
|
||||
'publicURL': 'example.com'}],
|
||||
'type': 'object-store'}]
|
||||
@@ -61,7 +57,7 @@ class TestRemote(testtools.TestCase):
|
||||
this is an account with no containers and no objects
|
||||
"""
|
||||
# setup expectation
|
||||
swift_stub = SwiftClientStub()
|
||||
with SwiftClientStub() as swift_stub:
|
||||
swift_stub.with_account('123223')
|
||||
# interact
|
||||
conn = swiftclient.client.Connection()
|
||||
@@ -70,7 +66,8 @@ class TestRemote(testtools.TestCase):
|
||||
self.assertThat(len(account_info), matchers.Is(2))
|
||||
self.assertThat(account_info, matchers.IsInstance(tuple))
|
||||
self.assertThat(account_info[0], matchers.IsInstance(dict))
|
||||
self.assertThat(account_info[0],
|
||||
self.assertThat(
|
||||
account_info[0],
|
||||
matchers.KeysEqual('content-length', 'accept-ranges',
|
||||
'x-timestamp', 'x-trans-id', 'date',
|
||||
'x-account-bytes-used',
|
||||
@@ -85,7 +82,7 @@ class TestRemote(testtools.TestCase):
|
||||
tests to ensure behavior is normal with one container
|
||||
"""
|
||||
# setup expectation
|
||||
swift_stub = SwiftClientStub()
|
||||
with SwiftClientStub() as swift_stub:
|
||||
swift_stub.with_account('123223')
|
||||
cont_name = 'a-container-name'
|
||||
swift_stub.with_container(cont_name)
|
||||
@@ -100,7 +97,8 @@ class TestRemote(testtools.TestCase):
|
||||
self.assertThat(len(account_containers), matchers.Is(1))
|
||||
self.assertThat(account_containers[0],
|
||||
matchers.KeysEqual('count', 'bytes', 'name'))
|
||||
self.assertThat(account_containers[0]['name'], matchers.Is(cont_name))
|
||||
self.assertThat(account_containers[0]['name'],
|
||||
matchers.Is(cont_name))
|
||||
# get container details
|
||||
cont_info = conn.get_container(cont_name)
|
||||
self.assertIsNotNone(cont_info)
|
||||
@@ -111,13 +109,13 @@ class TestRemote(testtools.TestCase):
|
||||
self.assertThat(len(cont_info[1]), matchers.Equals(0))
|
||||
# remove container
|
||||
swift_stub.without_container(cont_name)
|
||||
with testtools.ExpectedException(swiftclient.ClientException):
|
||||
with ExpectedException(swiftclient.ClientException):
|
||||
conn.get_container(cont_name)
|
||||
# ensure there are no more containers in account
|
||||
self.assertThat(len(conn.get_account()[1]), matchers.Is(0))
|
||||
|
||||
def test_one_object(self):
|
||||
swift_stub = SwiftClientStub()
|
||||
with SwiftClientStub() as swift_stub:
|
||||
swift_stub.with_account('123223')
|
||||
swift_stub.with_container('bob')
|
||||
swift_stub.with_object('bob', 'test', 'test_contents')
|
||||
@@ -131,7 +129,9 @@ class TestRemote(testtools.TestCase):
|
||||
'x-container-object-count',
|
||||
'accept-ranges',
|
||||
'x-container-bytes-used',
|
||||
'x-timestamp', 'x-trans-id', 'date',
|
||||
'x-timestamp',
|
||||
'x-trans-id',
|
||||
'date',
|
||||
'content-type'))
|
||||
cont_objects = cont_info[1]
|
||||
self.assertThat(len(cont_objects), matchers.Equals(1))
|
||||
@@ -148,12 +148,12 @@ class TestRemote(testtools.TestCase):
|
||||
# test remove object
|
||||
swift_stub.without_object('bob', 'test')
|
||||
# interact
|
||||
with testtools.ExpectedException(swiftclient.ClientException):
|
||||
with ExpectedException(swiftclient.ClientException):
|
||||
conn.delete_object('bob', 'test')
|
||||
self.assertThat(len(conn.get_container('bob')[1]), matchers.Is(0))
|
||||
|
||||
def test_two_objects(self):
|
||||
swift_stub = SwiftClientStub()
|
||||
with SwiftClientStub() as swift_stub:
|
||||
swift_stub.with_account('123223')
|
||||
swift_stub.with_container('bob')
|
||||
swift_stub.with_container('bob2')
|
||||
@@ -170,7 +170,9 @@ class TestRemote(testtools.TestCase):
|
||||
'x-container-object-count',
|
||||
'accept-ranges',
|
||||
'x-container-bytes-used',
|
||||
'x-timestamp', 'x-trans-id', 'date',
|
||||
'x-timestamp',
|
||||
'x-trans-id',
|
||||
'date',
|
||||
'content-type'))
|
||||
self.assertThat(len(cont_info[1]), matchers.Equals(2))
|
||||
self.assertThat(cont_info[1][0], matchers.Equals(
|
||||
@@ -184,12 +186,12 @@ class TestRemote(testtools.TestCase):
|
||||
matchers.Is('test_contents2'))
|
||||
|
||||
swift_stub.without_object('bob', 'test')
|
||||
with testtools.ExpectedException(swiftclient.ClientException):
|
||||
with ExpectedException(swiftclient.ClientException):
|
||||
conn.delete_object('bob', 'test')
|
||||
self.assertThat(len(conn.get_container('bob')[1]), matchers.Is(1))
|
||||
|
||||
swift_stub.without_container('bob')
|
||||
with testtools.ExpectedException(swiftclient.ClientException):
|
||||
with ExpectedException(swiftclient.ClientException):
|
||||
conn.get_container('bob')
|
||||
|
||||
self.assertThat(len(conn.get_account()), matchers.Is(2))
|
||||
@@ -198,15 +200,14 @@ class TestRemote(testtools.TestCase):
|
||||
"""
|
||||
when a container does not exist and is accessed then a 404 is returned
|
||||
"""
|
||||
from trove.tests.fakes.swift import SwiftClientStub
|
||||
|
||||
swift_stub = SwiftClientStub()
|
||||
with SwiftClientStub() as swift_stub:
|
||||
swift_stub.with_account('123223')
|
||||
swift_stub.with_container('existing')
|
||||
|
||||
conn = swiftclient.client.Connection()
|
||||
|
||||
with testtools.ExpectedException(swiftclient.ClientException):
|
||||
with ExpectedException(swiftclient.ClientException):
|
||||
conn.get_container('nonexisting')
|
||||
|
||||
def test_replace_object(self):
|
||||
@@ -214,7 +215,7 @@ class TestRemote(testtools.TestCase):
|
||||
Test to ensure that if an object is updated the container object
|
||||
count is the same and the contents of the object are updated
|
||||
"""
|
||||
swift_stub = SwiftClientStub()
|
||||
with SwiftClientStub() as swift_stub:
|
||||
swift_stub.with_account('1223df2')
|
||||
swift_stub.with_container('new-container')
|
||||
swift_stub.with_object('new-container', 'new-object',
|
||||
@@ -222,7 +223,8 @@ class TestRemote(testtools.TestCase):
|
||||
|
||||
conn = swiftclient.client.Connection()
|
||||
|
||||
conn.put_object('new-container', 'new-object', 'new-object-contents')
|
||||
conn.put_object('new-container', 'new-object',
|
||||
'new-object-contents')
|
||||
obj_resp = conn.get_object('new-container', 'new-object')
|
||||
self.assertThat(obj_resp, matchers.Not(matchers.Is(None)))
|
||||
self.assertThat(len(obj_resp), matchers.Is(2))
|
||||
@@ -239,13 +241,14 @@ class TestRemote(testtools.TestCase):
|
||||
obj_resp = conn.get_object('new-container', 'new-object')
|
||||
self.assertThat(obj_resp, matchers.Not(matchers.Is(None)))
|
||||
self.assertThat(len(obj_resp), matchers.Is(2))
|
||||
self.assertThat(obj_resp[1], matchers.Is('updated-object-contents'))
|
||||
self.assertThat(obj_resp[1], matchers.Is(
|
||||
'updated-object-contents'))
|
||||
# ensure object count has not increased
|
||||
self.assertThat(len(conn.get_container('new-container')[1]),
|
||||
matchers.Is(1))
|
||||
|
||||
|
||||
class TestCreateCinderClient(testtools.TestCase):
|
||||
class TestCreateCinderClient(trove_testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestCreateCinderClient, self).setUp()
|
||||
self.volumev2_public_url = 'http://publicURL/v2'
|
||||
@@ -320,7 +323,7 @@ class TestCreateCinderClient(testtools.TestCase):
|
||||
client.client.management_url)
|
||||
|
||||
|
||||
class TestCreateNovaClient(testtools.TestCase):
|
||||
class TestCreateNovaClient(trove_testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestCreateNovaClient, self).setUp()
|
||||
self.compute_public_url = 'http://publicURL/v2'
|
||||
@@ -395,7 +398,7 @@ class TestCreateNovaClient(testtools.TestCase):
|
||||
client.client.management_url)
|
||||
|
||||
|
||||
class TestCreateHeatClient(testtools.TestCase):
|
||||
class TestCreateHeatClient(trove_testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestCreateHeatClient, self).setUp()
|
||||
self.heat_public_url = 'http://publicURL/v2'
|
||||
@@ -470,7 +473,7 @@ class TestCreateHeatClient(testtools.TestCase):
|
||||
client.http_client.endpoint)
|
||||
|
||||
|
||||
class TestCreateSwiftClient(testtools.TestCase):
|
||||
class TestCreateSwiftClient(trove_testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestCreateSwiftClient, self).setUp()
|
||||
self.swift_public_url = 'http://publicURL/v2'
|
||||
@@ -536,7 +539,7 @@ class TestCreateSwiftClient(testtools.TestCase):
|
||||
client.url)
|
||||
|
||||
|
||||
class TestEndpoints(testtools.TestCase):
|
||||
class TestEndpoints(trove_testtools.TestCase):
|
||||
"""
|
||||
Copied from glance/tests/unit/test_auth.py.
|
||||
"""
|
||||
|
||||
@@ -9,20 +9,17 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from mock import Mock
|
||||
import testtools
|
||||
import re
|
||||
|
||||
from trove.common import exception
|
||||
from trove.common import template
|
||||
from trove.common import utils
|
||||
from trove.datastore.models import DatastoreVersion
|
||||
from trove.tests.unittests import trove_testtools
|
||||
from trove.tests.unittests.util import util
|
||||
import re
|
||||
|
||||
|
||||
class TemplateTest(testtools.TestCase):
|
||||
class TemplateTest(trove_testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TemplateTest, self).setUp()
|
||||
util.init_db()
|
||||
@@ -106,7 +103,7 @@ class TemplateTest(testtools.TestCase):
|
||||
self.assertTrue(self._find_in_template(config.render(), "relay_log"))
|
||||
|
||||
|
||||
class HeatTemplateLoadTest(testtools.TestCase):
|
||||
class HeatTemplateLoadTest(trove_testtools.TestCase):
|
||||
|
||||
class FakeTemplate():
|
||||
def __init__(self):
|
||||
|
||||
@@ -13,16 +13,14 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
|
||||
from trove.common import exception
|
||||
import trove.common.utils as utils
|
||||
|
||||
from mock import Mock
|
||||
import testtools
|
||||
from testtools import ExpectedException
|
||||
from trove.common import exception
|
||||
from trove.common import utils
|
||||
from trove.tests.unittests import trove_testtools
|
||||
|
||||
|
||||
class TestTroveExecuteWithTimeout(testtools.TestCase):
|
||||
class TestTroveExecuteWithTimeout(trove_testtools.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestTroveExecuteWithTimeout, self).setUp()
|
||||
|
||||
@@ -13,14 +13,13 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
import trove.common.wsgi as wsgi
|
||||
from testtools.matchers import Equals, Is, Not
|
||||
from trove.common import wsgi
|
||||
from trove.tests.unittests import trove_testtools
|
||||
import webob
|
||||
|
||||
import testtools
|
||||
from testtools.matchers import Equals, Is, Not
|
||||
|
||||
|
||||
class TestWsgi(testtools.TestCase):
|
||||
class TestWsgi(trove_testtools.TestCase):
|
||||
def test_process_request(self):
|
||||
middleware = wsgi.ContextMiddleware("test_trove")
|
||||
req = webob.BaseRequest({})
|
||||
|
||||
Reference in New Issue
Block a user