Merge "Fix leaked mocks in the 'upgrade' module if any"

This commit is contained in:
Jenkins 2015-05-11 09:12:25 +00:00 committed by Gerrit Code Review
commit bdad2a4697
2 changed files with 29 additions and 89 deletions

View File

@ -13,14 +13,14 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
# #
from mock import Mock, MagicMock from mock import Mock, MagicMock, patch
from testtools import TestCase
from trove.extensions.mgmt.upgrade.models import UpgradeMessageSender from trove.extensions.mgmt.upgrade.models import UpgradeMessageSender
from trove.extensions.mgmt.upgrade.service import UpgradeController from trove.extensions.mgmt.upgrade.service import UpgradeController
from trove.tests.unittests import trove_testtools
import jsonschema import jsonschema
class TestUpgradeController(TestCase): class TestUpgradeController(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(TestUpgradeController, self).setUp() super(TestUpgradeController, self).setUp()
@ -59,6 +59,7 @@ class TestUpgradeController(TestCase):
validator = self._get_validator(self.body) validator = self._get_validator(self.body)
self.assertTrue(validator.is_valid(self.body)) self.assertTrue(validator.is_valid(self.body))
@patch.object(UpgradeMessageSender, 'create', Mock(return_value=Mock()))
def test_controller_with_no_metadata(self): def test_controller_with_no_metadata(self):
""" """
Test the mock controller w/out metadata Test the mock controller w/out metadata
@ -71,9 +72,6 @@ class TestUpgradeController(TestCase):
req.environ = MagicMock() req.environ = MagicMock()
req.environ.get = Mock(return_value=context) req.environ.get = Mock(return_value=context)
send = Mock()
UpgradeMessageSender.create = Mock(return_value=send)
resp = self.controller.create(req, self.body, tenant_id, instance_id) resp = self.controller.create(req, self.body, tenant_id, instance_id)
instance_version = self.body["upgrade"]["instance_version"] instance_version = self.body["upgrade"]["instance_version"]
@ -84,6 +82,7 @@ class TestUpgradeController(TestCase):
context, instance_id, instance_version, location, metadata) context, instance_id, instance_version, location, metadata)
self.assertEqual(202, resp.status) self.assertEqual(202, resp.status)
@patch.object(UpgradeMessageSender, 'create', Mock(return_value=Mock()))
def test_controller_with_metadata(self): def test_controller_with_metadata(self):
""" """
Test the mock controller with metadata Test the mock controller with metadata
@ -96,9 +95,6 @@ class TestUpgradeController(TestCase):
req.environ = MagicMock() req.environ = MagicMock()
req.environ.get = Mock(return_value=context) req.environ.get = Mock(return_value=context)
send = Mock()
UpgradeMessageSender.create = Mock(return_value=send)
# append the body w/ metadata # append the body w/ metadata
self.body["upgrade"]["metadata"] = { self.body["upgrade"]["metadata"] = {
"config_location": "swift://my.conf.location", "config_location": "swift://my.conf.location",
@ -115,6 +111,7 @@ class TestUpgradeController(TestCase):
context, instance_id, instance_version, location, metadata) context, instance_id, instance_version, location, metadata)
self.assertEqual(202, resp.status) self.assertEqual(202, resp.status)
@patch.object(UpgradeMessageSender, 'create', Mock(return_value=Mock()))
def test_controller_with_empty_metadata(self): def test_controller_with_empty_metadata(self):
""" """
Test the mock controller with metadata Test the mock controller with metadata
@ -127,9 +124,6 @@ class TestUpgradeController(TestCase):
req.environ = MagicMock() req.environ = MagicMock()
req.environ.get = Mock(return_value=context) req.environ.get = Mock(return_value=context)
send = Mock()
UpgradeMessageSender.create = Mock(return_value=send)
# append the body w/ empty metadata # append the body w/ empty metadata
self.body["upgrade"]["metadata"] = {} self.body["upgrade"]["metadata"] = {}

View File

@ -13,12 +13,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
# #
from mock import Mock from mock import Mock, patch
from testtools import TestCase
from trove.extensions.mgmt.upgrade.models import UpgradeMessageSender from trove.extensions.mgmt.upgrade.models import UpgradeMessageSender
from trove.tests.unittests import trove_testtools
class TestUpgradeModel(TestCase): class TestUpgradeModel(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(TestUpgradeModel, self).setUp() super(TestUpgradeModel, self).setUp()
@ -49,90 +49,36 @@ class TestUpgradeModel(TestCase):
self.assertTrue(UpgradeMessageSender._validate(param, 36)) self.assertTrue(UpgradeMessageSender._validate(param, 36))
def test_create(self): def test_create(self):
""" self._assert_create_with_metadata()
Test creating notification
"""
context = Mock()
instance_id = "27e25b73-88a1-4526-b2b9-919a28b8b33f",
instance_version = "v1.0.1",
location = "http://swift/trove-guestagent-v1.0.1.tar.gz"
_create_resource = Mock(return_value=None)
UpgradeMessageSender.create = Mock(return_value=_create_resource)
func = UpgradeMessageSender.create(context, instance_id,
instance_version, location)
self.assertEqual(_create_resource, func)
UpgradeMessageSender.create.assert_called_with(
context, instance_id, instance_version, location)
def test_create_with_metadata_none(self): def test_create_with_metadata_none(self):
""" self._assert_create_with_metadata(metadata=None)
Test creating notification with metadata is None
"""
context = Mock()
instance_id = "27e25b73-88a1-4526-b2b9-919a28b8b33f",
instance_version = "v1.0.1",
location = "http://swift/trove-guestagent-v1.0.1.tar.gz"
metadata = None
_create_resource = Mock(return_value=None)
UpgradeMessageSender.create = Mock(return_value=_create_resource)
func = UpgradeMessageSender.create(
context, instance_id, instance_version, location, metadata)
self.assertEqual(_create_resource, func)
UpgradeMessageSender.create.assert_called_with(
context, instance_id, instance_version, location, metadata)
def test_create_with_empty_metadata(self): def test_create_with_empty_metadata(self):
""" self._assert_create_with_metadata(metadata={})
Test creating notification with metadata {}
"""
context = Mock()
instance_id = "27e25b73-88a1-4526-b2b9-919a28b8b33f",
instance_version = "v1.0.1",
location = "http://swift/trove-guestagent-v1.0.1.tar.gz"
metadata = {}
_create_resource = Mock(return_value=None)
UpgradeMessageSender.create = Mock(return_value=_create_resource)
func = UpgradeMessageSender.create(
context, instance_id, instance_version, location, metadata)
self.assertEqual(_create_resource, func)
UpgradeMessageSender.create.assert_called_with(
context, instance_id, instance_version, location, metadata)
def test_create_with_metadata(self): def test_create_with_metadata(self):
""" self._assert_create_with_metadata(
Test creating notification with metadata metadata={"is_public": True,
"is_encrypted": True,
"config_location": "http://swift/trove-guestagent.conf"})
@patch('trove.guestagent.api.API.upgrade')
def _assert_create_with_metadata(self, api_upgrade_mock, metadata=None):
"""Excercise UpgradeMessageSender.create() call.
""" """
context = Mock() context = Mock()
instance_id = "27e25b73-88a1-4526-b2b9-919a28b8b33f", instance_id = "27e25b73-88a1-4526-b2b9-919a28b8b33f"
instance_version = "v1.0.1", instance_version = "v1.0.1"
location = "http://swift/trove-guestagent-v1.0.1.tar.gz" location = "http://swift/trove-guestagent-v1.0.1.tar.gz"
metadata = {"is_public": True,
"is_encrypted": True,
"config_location": "http://swift/trove-guestagent.conf"}
_create_resource = Mock(return_value=None) func = (UpgradeMessageSender.create(
UpgradeMessageSender.create = Mock(return_value=_create_resource)
func = UpgradeMessageSender.create(
context, instance_id, instance_version, location, metadata) context, instance_id, instance_version, location, metadata)
if metadata is not None else UpgradeMessageSender.create(
context, instance_id, instance_version, location))
self.assertEqual(_create_resource, func) self.assertTrue(callable(func))
func() # This call should translate to the API call asserted below.
UpgradeMessageSender.create.assert_called_with( api_upgrade_mock.assert_called_once_with(instance_version, location,
context, instance_id, instance_version, location, metadata) metadata)