Switch swift API tests to mock
This patch changes mox to mock for the following test modules: * openstack_dashboard/test/unit/api/test_swift.py * openstack_dashboard/test/unit/api/rest/test_swift.py Partially-Implements: blueprint mock-framework-in-unit-tests Change-Id: I3c920564b781c9c1c97bd6ee22b834504c94cb60
This commit is contained in:
parent
317cd8e7da
commit
5c5159bc09
|
@ -572,6 +572,7 @@ class APITestCase(TestCase):
|
|||
return self.neutronclient
|
||||
|
||||
def stub_swiftclient(self, expected_calls=1):
|
||||
self._warn_client('swift', 'S')
|
||||
if not hasattr(self, "swiftclient"):
|
||||
self.mox.StubOutWithMock(swift_client, 'Connection')
|
||||
self.swiftclient = self.mox.CreateMock(swift_client.Connection)
|
||||
|
@ -608,6 +609,10 @@ class APIMockTestCase(APITestCase):
|
|||
self.glanceclient = mock.Mock()
|
||||
return self.glanceclient
|
||||
|
||||
def stub_swiftclient(self):
|
||||
# This method should not be called.
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
# Need this to test both Glance API V1 and V2 versions
|
||||
class ResetImageAPIVersionMixin(object):
|
||||
|
|
|
@ -23,6 +23,9 @@ TEST = TestData(swift_data.data)
|
|||
|
||||
|
||||
class SwiftRestTestCase(test.TestCase):
|
||||
|
||||
use_mox = False
|
||||
|
||||
def setUp(self):
|
||||
super(SwiftRestTestCase, self).setUp()
|
||||
self._containers = TEST.containers.list()
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
from __future__ import absolute_import
|
||||
|
||||
from mox3.mox import IsA
|
||||
import mock
|
||||
|
||||
from horizon import exceptions
|
||||
|
||||
|
@ -26,180 +26,189 @@ from openstack_dashboard import api
|
|||
from openstack_dashboard.test import helpers as test
|
||||
|
||||
|
||||
class SwiftApiTests(test.APITestCase):
|
||||
def test_swift_get_containers(self):
|
||||
@mock.patch('swiftclient.client.Connection')
|
||||
class SwiftApiTests(test.APIMockTestCase):
|
||||
def test_swift_get_containers(self, mock_swiftclient):
|
||||
containers = self.containers.list()
|
||||
cont_data = [c._apidict for c in containers]
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.get_account(limit=1001,
|
||||
marker=None,
|
||||
prefix=None,
|
||||
full_listing=True).AndReturn([{}, cont_data])
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.get_account.return_value = [{}, cont_data]
|
||||
|
||||
(conts, more) = api.swift.swift_get_containers(self.request)
|
||||
|
||||
self.assertEqual(len(containers), len(conts))
|
||||
self.assertFalse(more)
|
||||
swift_api.get_account.assert_called_once_with(
|
||||
limit=1001, marker=None, prefix=None, full_listing=True)
|
||||
|
||||
def test_swift_get_container_with_data(self):
|
||||
def test_swift_get_container_with_data(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
objects = self.objects.list()
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.get_object(container.name, "") \
|
||||
.AndReturn((container, objects))
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.get_object.return_value = (container, objects)
|
||||
|
||||
cont = api.swift.swift_get_container(self.request, container.name)
|
||||
|
||||
self.assertEqual(container.name, cont.name)
|
||||
self.assertEqual(len(objects), len(cont.data))
|
||||
swift_api.get_object.assert_called_once_with(container.name, "")
|
||||
|
||||
def test_swift_get_container_without_data(self):
|
||||
def test_swift_get_container_without_data(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.head_container(container.name).AndReturn(container)
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.head_container.return_value = container
|
||||
|
||||
cont = api.swift.swift_get_container(self.request,
|
||||
container.name,
|
||||
with_data=False)
|
||||
|
||||
self.assertEqual(cont.name, container.name)
|
||||
self.assertIsNone(cont.data)
|
||||
swift_api.head_container.assert_called_once_with(container.name)
|
||||
|
||||
def test_swift_create_duplicate_container(self):
|
||||
def test_swift_create_duplicate_container(self, mock_swiftclient):
|
||||
metadata = {'is_public': False}
|
||||
container = self.containers.first()
|
||||
headers = api.swift._metadata_to_header(metadata=(metadata))
|
||||
swift_api = self.stub_swiftclient(2)
|
||||
swift_api = mock_swiftclient.return_value
|
||||
# Check for existence, then create
|
||||
exc = self.exceptions.swift
|
||||
swift_api.head_container(container.name).AndRaise(exc)
|
||||
swift_api.put_container(container.name, headers=headers) \
|
||||
.AndReturn(container)
|
||||
self.mox.ReplayAll()
|
||||
# Verification handled by mox, no assertions needed.
|
||||
swift_api.head_container.side_effect = self.exceptions.swift
|
||||
swift_api.put_container.return_value = container
|
||||
|
||||
api.swift.swift_create_container(self.request,
|
||||
container.name,
|
||||
metadata=(metadata))
|
||||
|
||||
def test_swift_create_container(self):
|
||||
swift_api.head_container.assert_called_once_with(container.name)
|
||||
swift_api.put_container.assert_called_once_with(container.name,
|
||||
headers=headers)
|
||||
|
||||
def test_swift_create_container(self, mock_swiftclient):
|
||||
metadata = {'is_public': True}
|
||||
container = self.containers.first()
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.head_container(container.name).AndReturn(container)
|
||||
self.mox.ReplayAll()
|
||||
# Verification handled by mox, no assertions needed.
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.head_container.return_value = container
|
||||
|
||||
with self.assertRaises(exceptions.AlreadyExists):
|
||||
api.swift.swift_create_container(self.request,
|
||||
container.name,
|
||||
metadata=(metadata))
|
||||
|
||||
def test_swift_update_container(self):
|
||||
swift_api.head_container.assert_called_once_with(container.name)
|
||||
|
||||
def test_swift_update_container(self, mock_swiftclient):
|
||||
metadata = {'is_public': True}
|
||||
container = self.containers.first()
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
headers = api.swift._metadata_to_header(metadata=(metadata))
|
||||
swift_api.post_container(container.name, headers=headers)\
|
||||
.AndReturn(container)
|
||||
self.mox.ReplayAll()
|
||||
# Verification handled by mox, no assertions needed.
|
||||
swift_api.post_container.return_value = container
|
||||
|
||||
api.swift.swift_update_container(self.request,
|
||||
container.name,
|
||||
metadata=(metadata))
|
||||
|
||||
def test_swift_get_objects(self):
|
||||
swift_api.post_container.assert_called_once_with(container.name,
|
||||
headers=headers)
|
||||
|
||||
def test_swift_get_objects(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
objects = self.objects.list()
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.get_container(container.name,
|
||||
limit=1001,
|
||||
marker=None,
|
||||
prefix=None,
|
||||
delimiter='/',
|
||||
full_listing=True).AndReturn([{}, objects])
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.get_container.return_value = [{}, objects]
|
||||
|
||||
(objs, more) = api.swift.swift_get_objects(self.request,
|
||||
container.name)
|
||||
|
||||
self.assertEqual(len(objects), len(objs))
|
||||
self.assertFalse(more)
|
||||
swift_api.get_container.assert_called_once_with(
|
||||
container.name,
|
||||
limit=1001,
|
||||
marker=None,
|
||||
prefix=None,
|
||||
delimiter='/',
|
||||
full_listing=True)
|
||||
|
||||
def test_swift_get_object_with_data_non_chunked(self):
|
||||
def test_swift_get_object_with_data_non_chunked(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
object = self.objects.first()
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.get_object(
|
||||
container.name, object.name, resp_chunk_size=None
|
||||
).AndReturn([object, object.data])
|
||||
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.get_object.return_value = [object, object.data]
|
||||
|
||||
obj = api.swift.swift_get_object(self.request, container.name,
|
||||
object.name, resp_chunk_size=None)
|
||||
self.assertEqual(object.name, obj.name)
|
||||
|
||||
def test_swift_get_object_with_data_chunked(self):
|
||||
self.assertEqual(object.name, obj.name)
|
||||
swift_api.get_object.assert_called_once_with(
|
||||
container.name, object.name, resp_chunk_size=None)
|
||||
|
||||
def test_swift_get_object_with_data_chunked(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
object = self.objects.first()
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.get_object(
|
||||
container.name, object.name, resp_chunk_size=api.swift.CHUNK_SIZE
|
||||
).AndReturn([object, object.data])
|
||||
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.get_object.return_value = [object, object.data]
|
||||
|
||||
obj = api.swift.swift_get_object(
|
||||
self.request, container.name, object.name)
|
||||
self.assertEqual(object.name, obj.name)
|
||||
|
||||
def test_swift_get_object_without_data(self):
|
||||
self.assertEqual(object.name, obj.name)
|
||||
swift_api.get_object.assert_called_once_with(
|
||||
container.name, object.name, resp_chunk_size=api.swift.CHUNK_SIZE)
|
||||
|
||||
def test_swift_get_object_without_data(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
object = self.objects.first()
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.head_object(container.name, object.name) \
|
||||
.AndReturn(object)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.head_object.return_value = object
|
||||
|
||||
obj = api.swift.swift_get_object(self.request,
|
||||
container.name,
|
||||
object.name,
|
||||
with_data=False)
|
||||
|
||||
self.assertEqual(object.name, obj.name)
|
||||
self.assertIsNone(obj.data)
|
||||
swift_api.head_object.assert_called_once_with(container.name,
|
||||
object.name)
|
||||
|
||||
def test_swift_create_pseudo_folder(self):
|
||||
def test_swift_create_pseudo_folder(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
folder = self.folder.first()
|
||||
swift_api = self.stub_swiftclient(2)
|
||||
swift_api = mock_swiftclient.return_value
|
||||
exc = self.exceptions.swift
|
||||
swift_api.head_object(container.name, folder.name).AndRaise(exc)
|
||||
swift_api.put_object(container.name,
|
||||
folder.name,
|
||||
None,
|
||||
headers={}).AndReturn(folder)
|
||||
self.mox.ReplayAll()
|
||||
# Verification handled by mox, no assertions needed.
|
||||
swift_api.head_object.side_effect = exc
|
||||
swift_api.put_object.return_value = folder
|
||||
|
||||
api.swift.swift_create_pseudo_folder(self.request,
|
||||
container.name,
|
||||
folder.name)
|
||||
|
||||
def test_swift_create_duplicate_folder(self):
|
||||
swift_api.head_object.assert_called_once_with(container.name,
|
||||
folder.name)
|
||||
swift_api.put_object.assert_called_once_with(container.name,
|
||||
folder.name,
|
||||
None,
|
||||
headers={})
|
||||
|
||||
def test_swift_create_duplicate_folder(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
folder = self.folder.first()
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.head_object(container.name, folder.name).AndReturn(folder)
|
||||
self.mox.ReplayAll()
|
||||
# Verification handled by mox, no assertions needed.
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.head_object.return_value = folder
|
||||
|
||||
with self.assertRaises(exceptions.AlreadyExists):
|
||||
api.swift.swift_create_pseudo_folder(self.request,
|
||||
container.name,
|
||||
folder.name)
|
||||
|
||||
def test_swift_upload_object(self):
|
||||
swift_api.head_object.assert_called_once_with(container.name,
|
||||
folder.name)
|
||||
|
||||
def test_swift_upload_object(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
obj = self.objects.first()
|
||||
fake_name = 'fake_object.jpg'
|
||||
|
@ -212,50 +221,57 @@ class SwiftApiTests(test.APITestCase):
|
|||
|
||||
headers = {'X-Object-Meta-Orig-Filename': fake_name}
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
test_file = FakeFile()
|
||||
swift_api.put_object(container.name,
|
||||
obj.name,
|
||||
IsA(FakeFile),
|
||||
content_length=test_file.size,
|
||||
headers=headers)
|
||||
self.mox.ReplayAll()
|
||||
swift_api.put_object.return_value = None
|
||||
|
||||
api.swift.swift_upload_object(self.request,
|
||||
container.name,
|
||||
obj.name,
|
||||
test_file)
|
||||
|
||||
def test_swift_upload_object_without_file(self):
|
||||
swift_api.put_object.assert_called_once_with(
|
||||
container.name,
|
||||
obj.name,
|
||||
test.IsA(FakeFile),
|
||||
content_length=test_file.size,
|
||||
headers=headers)
|
||||
|
||||
def test_swift_upload_object_without_file(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
obj = self.objects.first()
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.put_object(container.name,
|
||||
obj.name,
|
||||
None,
|
||||
content_length=0,
|
||||
headers={})
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.put_object.return_value = None
|
||||
|
||||
response = api.swift.swift_upload_object(self.request,
|
||||
container.name,
|
||||
obj.name,
|
||||
None)
|
||||
self.assertEqual(0, response['bytes'])
|
||||
|
||||
def test_swift_object_exists(self):
|
||||
self.assertEqual(0, response['bytes'])
|
||||
swift_api.put_object.assert_called_once_with(
|
||||
container.name,
|
||||
obj.name,
|
||||
None,
|
||||
content_length=0,
|
||||
headers={})
|
||||
|
||||
def test_swift_object_exists(self, mock_swiftclient):
|
||||
container = self.containers.first()
|
||||
obj = self.objects.first()
|
||||
|
||||
swift_api = self.stub_swiftclient(2)
|
||||
swift_api.head_object(container.name, obj.name).AndReturn(container)
|
||||
|
||||
exc = self.exceptions.swift
|
||||
swift_api.head_object(container.name, obj.name).AndRaise(exc)
|
||||
self.mox.ReplayAll()
|
||||
swift_api = mock_swiftclient.return_value
|
||||
swift_api.head_object.side_effect = [container, self.exceptions.swift]
|
||||
|
||||
args = self.request, container.name, obj.name
|
||||
|
||||
self.assertTrue(api.swift.swift_object_exists(*args))
|
||||
# Again, for a "non-existent" object
|
||||
self.assertFalse(api.swift.swift_object_exists(*args))
|
||||
|
||||
self.assertEqual(2, swift_api.head_object.call_count)
|
||||
swift_api.head_object.assert_has_calls([
|
||||
mock.call(container.name, obj.name),
|
||||
mock.call(container.name, obj.name),
|
||||
])
|
||||
|
|
Loading…
Reference in New Issue