From 52abfd7e1c7abd8d37c5573f7633e859f0e26aae Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sat, 10 Jan 2015 23:51:59 +0000 Subject: [PATCH] Add tests for objects Implements: blueprint increase-test-coverage Change-Id: I41619570a5b37ef70af1997e710019ff96a5ef42 --- magnum/tests/base.py | 2 + magnum/tests/objects/test_bay.py | 112 +++++++++++++++-- magnum/tests/objects/test_baymodel.py | 116 +++++++++++++++++ magnum/tests/objects/test_container.py | 114 ++++++++++++++--- magnum/tests/objects/test_node.py | 116 +++++++++++++++++ magnum/tests/objects/test_pod.py | 116 +++++++++++++++++ .../objects/test_replicationcontroller.py | 119 ++++++++++++++++++ magnum/tests/objects/test_service.py | 116 +++++++++++++++++ 8 files changed, 783 insertions(+), 28 deletions(-) create mode 100644 magnum/tests/objects/test_baymodel.py create mode 100644 magnum/tests/objects/test_node.py create mode 100644 magnum/tests/objects/test_pod.py create mode 100644 magnum/tests/objects/test_replicationcontroller.py create mode 100644 magnum/tests/objects/test_service.py diff --git a/magnum/tests/base.py b/magnum/tests/base.py index 86ef18159f..a399f7caea 100644 --- a/magnum/tests/base.py +++ b/magnum/tests/base.py @@ -23,6 +23,7 @@ import pecan from pecan import testing import testscenarios +from magnum.common import context as magnum_context from magnum.tests import conf_fixture @@ -47,6 +48,7 @@ class TestCase(base.BaseTestCase): os.path.dirname(__file__), 'config.py' )) + self.context = magnum_context.RequestContext() self.useFixture(conf_fixture.ConfFixture(cfg.CONF)) def tearDown(self): diff --git a/magnum/tests/objects/test_bay.py b/magnum/tests/objects/test_bay.py index 9002e24740..04973636b8 100644 --- a/magnum/tests/objects/test_bay.py +++ b/magnum/tests/objects/test_bay.py @@ -1,4 +1,5 @@ -# Copyright 2014 NEC Corporation. All rights reserved. +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -12,19 +13,104 @@ # License for the specific language governing permissions and limitations # under the License. -# import magnum.objects -# from magnum.objects import bay -from magnum.tests import base -from magnum.tests import utils +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils -class TestBay(base.BaseTestCase): +class TestBayObject(base.DbTestCase): + def setUp(self): - super(TestBay, self).setUp() - self.ctx = utils.dummy_context() + super(TestBayObject, self).setUp() + self.fake_bay = utils.get_test_bay() - self.data = [{'uuid': 'ce43e347f0b0422825245b3e5f140a81cef6e65b', - 'name': 'bay1', - 'type': 'virt', - 'ip_address': '10.0.0.3', - 'external_ip_address': '192.0.2.3'}] + def test_get_by_id(self): + bay_id = self.fake_bay['id'] + with mock.patch.object(self.dbapi, 'get_bay_by_id', + autospec=True) as mock_get_bay: + mock_get_bay.return_value = self.fake_bay + bay = objects.Bay.get(self.context, bay_id) + mock_get_bay.assert_called_once_with(bay_id) + self.assertEqual(self.context, bay._context) + + def test_get_by_uuid(self): + uuid = self.fake_bay['uuid'] + with mock.patch.object(self.dbapi, 'get_bay_by_uuid', + autospec=True) as mock_get_bay: + mock_get_bay.return_value = self.fake_bay + bay = objects.Bay.get(self.context, uuid) + mock_get_bay.assert_called_once_with(uuid) + self.assertEqual(self.context, bay._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.Bay.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_bay_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_bay] + bays = objects.Bay.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(bays, HasLength(1)) + self.assertIsInstance(bays[0], objects.Bay) + self.assertEqual(self.context, bays[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_bay', + autospec=True) as mock_create_bay: + mock_create_bay.return_value = self.fake_bay + bay = objects.Bay(self.context, **self.fake_bay) + bay.create() + mock_create_bay.assert_called_once_with(self.fake_bay) + self.assertEqual(self.context, bay._context) + + def test_destroy(self): + uuid = self.fake_bay['uuid'] + with mock.patch.object(self.dbapi, 'get_bay_by_uuid', + autospec=True) as mock_get_bay: + mock_get_bay.return_value = self.fake_bay + with mock.patch.object(self.dbapi, 'destroy_bay', + autospec=True) as mock_destroy_bay: + bay = objects.Bay.get_by_uuid(self.context, uuid) + bay.destroy() + mock_get_bay.assert_called_once_with(uuid) + mock_destroy_bay.assert_called_once_with(uuid) + self.assertEqual(self.context, bay._context) + + def test_save(self): + uuid = self.fake_bay['uuid'] + with mock.patch.object(self.dbapi, 'get_bay_by_uuid', + autospec=True) as mock_get_bay: + mock_get_bay.return_value = self.fake_bay + with mock.patch.object(self.dbapi, 'update_bay', + autospec=True) as mock_update_bay: + bay = objects.Bay.get_by_uuid(self.context, uuid) + bay.node_count = 10 + bay.save() + + mock_get_bay.assert_called_once_with(uuid) + mock_update_bay.assert_called_once_with( + uuid, {'node_count': 10}) + self.assertEqual(self.context, bay._context) + + def test_refresh(self): + uuid = self.fake_bay['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_bay, uuid=uuid), + dict(self.fake_bay, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_bay_by_uuid', + side_effect=returns, + autospec=True) as mock_get_bay: + bay = objects.Bay.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, bay.uuid) + bay.refresh() + self.assertEqual(new_uuid, bay.uuid) + self.assertEqual(expected, mock_get_bay.call_args_list) + self.assertEqual(self.context, bay._context) \ No newline at end of file diff --git a/magnum/tests/objects/test_baymodel.py b/magnum/tests/objects/test_baymodel.py new file mode 100644 index 0000000000..92488f252c --- /dev/null +++ b/magnum/tests/objects/test_baymodel.py @@ -0,0 +1,116 @@ +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, 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. + +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils + + +class TestBayModelObject(base.DbTestCase): + + def setUp(self): + super(TestBayModelObject, self).setUp() + self.fake_baymodel = utils.get_test_baymodel() + + def test_get_by_id(self): + baymodel_id = self.fake_baymodel['id'] + with mock.patch.object(self.dbapi, 'get_baymodel_by_id', + autospec=True) as mock_get_baymodel: + mock_get_baymodel.return_value = self.fake_baymodel + baymodel = objects.BayModel.get(self.context, baymodel_id) + mock_get_baymodel.assert_called_once_with(baymodel_id) + self.assertEqual(self.context, baymodel._context) + + def test_get_by_uuid(self): + uuid = self.fake_baymodel['uuid'] + with mock.patch.object(self.dbapi, 'get_baymodel_by_uuid', + autospec=True) as mock_get_baymodel: + mock_get_baymodel.return_value = self.fake_baymodel + baymodel = objects.BayModel.get(self.context, uuid) + mock_get_baymodel.assert_called_once_with(uuid) + self.assertEqual(self.context, baymodel._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.BayModel.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_baymodel_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_baymodel] + baymodels = objects.BayModel.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(baymodels, HasLength(1)) + self.assertIsInstance(baymodels[0], objects.BayModel) + self.assertEqual(self.context, baymodels[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_baymodel', + autospec=True) as mock_create_baymodel: + mock_create_baymodel.return_value = self.fake_baymodel + baymodel = objects.BayModel(self.context, **self.fake_baymodel) + baymodel.create() + mock_create_baymodel.assert_called_once_with(self.fake_baymodel) + self.assertEqual(self.context, baymodel._context) + + def test_destroy(self): + uuid = self.fake_baymodel['uuid'] + with mock.patch.object(self.dbapi, 'get_baymodel_by_uuid', + autospec=True) as mock_get_baymodel: + mock_get_baymodel.return_value = self.fake_baymodel + with mock.patch.object(self.dbapi, 'destroy_baymodel', + autospec=True) as mock_destroy_baymodel: + bm = objects.BayModel.get_by_uuid(self.context, uuid) + bm.destroy() + mock_get_baymodel.assert_called_once_with(uuid) + mock_destroy_baymodel.assert_called_once_with(uuid) + self.assertEqual(self.context, bm._context) + + def test_save(self): + uuid = self.fake_baymodel['uuid'] + with mock.patch.object(self.dbapi, 'get_baymodel_by_uuid', + autospec=True) as mock_get_baymodel: + mock_get_baymodel.return_value = self.fake_baymodel + with mock.patch.object(self.dbapi, 'update_baymodel', + autospec=True) as mock_update_baymodel: + bm = objects.BayModel.get_by_uuid(self.context, uuid) + bm.image_id = 'test-image' + bm.save() + + mock_get_baymodel.assert_called_once_with(uuid) + mock_update_baymodel.assert_called_once_with( + uuid, {'image_id': 'test-image'}) + self.assertEqual(self.context, bm._context) + + def test_refresh(self): + uuid = self.fake_baymodel['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_baymodel, uuid=uuid), + dict(self.fake_baymodel, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_baymodel_by_uuid', + side_effect=returns, + autospec=True) as mock_get_baymodel: + bm = objects.BayModel.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, bm.uuid) + bm.refresh() + self.assertEqual(new_uuid, bm.uuid) + self.assertEqual(expected, mock_get_baymodel.call_args_list) + self.assertEqual(self.context, bm._context) \ No newline at end of file diff --git a/magnum/tests/objects/test_container.py b/magnum/tests/objects/test_container.py index 1dd4f1ead7..529088a635 100644 --- a/magnum/tests/objects/test_container.py +++ b/magnum/tests/objects/test_container.py @@ -1,4 +1,5 @@ -# Copyright 2014 NEC Corporation. All rights reserved. +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -12,21 +13,104 @@ # License for the specific language governing permissions and limitations # under the License. -# import magnum.objects -# from magnum.objects import container -from magnum.tests import base -from magnum.tests import utils +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils -class TestContainer(base.BaseTestCase): +class TestContainerObject(base.DbTestCase): + def setUp(self): - super(TestContainer, self).setUp() - self.ctx = utils.dummy_context() + super(TestContainerObject, self).setUp() + self.fake_container = utils.get_test_container() - self.data = [{'uuid': 'ce43e347f0b0422825245b3e5f140a81cef6e65b', - 'name': 'container1', - 'dns': ['8.8.8.8'], - 'image': 'ubuntu:latest', - 'command': ['echo', 'Hello World!'], - 'ports': [{"container_port": 80, "host_port": 8080}], - 'env': {'FOO': 'BAR'}}] + def test_get_by_id(self): + container_id = self.fake_container['id'] + with mock.patch.object(self.dbapi, 'get_container_by_id', + autospec=True) as mock_get_container: + mock_get_container.return_value = self.fake_container + container = objects.Container.get(self.context, container_id) + mock_get_container.assert_called_once_with(container_id) + self.assertEqual(self.context, container._context) + + def test_get_by_uuid(self): + uuid = self.fake_container['uuid'] + with mock.patch.object(self.dbapi, 'get_container_by_uuid', + autospec=True) as mock_get_container: + mock_get_container.return_value = self.fake_container + container = objects.Container.get(self.context, uuid) + mock_get_container.assert_called_once_with(uuid) + self.assertEqual(self.context, container._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.Container.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_container_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_container] + containers = objects.Container.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(containers, HasLength(1)) + self.assertIsInstance(containers[0], objects.Container) + self.assertEqual(self.context, containers[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_container', + autospec=True) as mock_create_container: + mock_create_container.return_value = self.fake_container + container = objects.Container(self.context, **self.fake_container) + container.create() + mock_create_container.assert_called_once_with(self.fake_container) + self.assertEqual(self.context, container._context) + + def test_destroy(self): + uuid = self.fake_container['uuid'] + with mock.patch.object(self.dbapi, 'get_container_by_uuid', + autospec=True) as mock_get_container: + mock_get_container.return_value = self.fake_container + with mock.patch.object(self.dbapi, 'destroy_container', + autospec=True) as mock_destroy_container: + container = objects.Container.get_by_uuid(self.context, uuid) + container.destroy() + mock_get_container.assert_called_once_with(uuid) + mock_destroy_container.assert_called_once_with(uuid) + self.assertEqual(self.context, container._context) + + def test_save(self): + uuid = self.fake_container['uuid'] + with mock.patch.object(self.dbapi, 'get_container_by_uuid', + autospec=True) as mock_get_container: + mock_get_container.return_value = self.fake_container + with mock.patch.object(self.dbapi, 'update_container', + autospec=True) as mock_update_container: + container = objects.Container.get_by_uuid(self.context, uuid) + container.image_id = 'container.img' + container.save() + + mock_get_container.assert_called_once_with(uuid) + mock_update_container.assert_called_once_with( + uuid, {'image_id': 'container.img'}) + self.assertEqual(self.context, container._context) + + def test_refresh(self): + uuid = self.fake_container['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_container, uuid=uuid), + dict(self.fake_container, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_container_by_uuid', + side_effect=returns, + autospec=True) as mock_get_container: + container = objects.Container.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, container.uuid) + container.refresh() + self.assertEqual(new_uuid, container.uuid) + self.assertEqual(expected, mock_get_container.call_args_list) + self.assertEqual(self.context, container._context) \ No newline at end of file diff --git a/magnum/tests/objects/test_node.py b/magnum/tests/objects/test_node.py new file mode 100644 index 0000000000..fb5ee055f2 --- /dev/null +++ b/magnum/tests/objects/test_node.py @@ -0,0 +1,116 @@ +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, 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. + +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils + + +class TestNodeObject(base.DbTestCase): + + def setUp(self): + super(TestNodeObject, self).setUp() + self.fake_node = utils.get_test_node() + + def test_get_by_id(self): + node_id = self.fake_node['id'] + with mock.patch.object(self.dbapi, 'get_node_by_id', + autospec=True) as mock_get_node: + mock_get_node.return_value = self.fake_node + node = objects.Node.get(self.context, node_id) + mock_get_node.assert_called_once_with(node_id) + self.assertEqual(self.context, node._context) + + def test_get_by_uuid(self): + uuid = self.fake_node['uuid'] + with mock.patch.object(self.dbapi, 'get_node_by_uuid', + autospec=True) as mock_get_node: + mock_get_node.return_value = self.fake_node + node = objects.Node.get(self.context, uuid) + mock_get_node.assert_called_once_with(uuid) + self.assertEqual(self.context, node._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.Node.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_node_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_node] + nodes = objects.Node.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(nodes, HasLength(1)) + self.assertIsInstance(nodes[0], objects.Node) + self.assertEqual(self.context, nodes[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_node', + autospec=True) as mock_create_node: + mock_create_node.return_value = self.fake_node + node = objects.Node(self.context, **self.fake_node) + node.create() + mock_create_node.assert_called_once_with(self.fake_node) + self.assertEqual(self.context, node._context) + + def test_destroy(self): + uuid = self.fake_node['uuid'] + with mock.patch.object(self.dbapi, 'get_node_by_uuid', + autospec=True) as mock_get_node: + mock_get_node.return_value = self.fake_node + with mock.patch.object(self.dbapi, 'destroy_node', + autospec=True) as mock_destroy_node: + node = objects.Node.get_by_uuid(self.context, uuid) + node.destroy() + mock_get_node.assert_called_once_with(uuid) + mock_destroy_node.assert_called_once_with(uuid) + self.assertEqual(self.context, node._context) + + def test_save(self): + uuid = self.fake_node['uuid'] + with mock.patch.object(self.dbapi, 'get_node_by_uuid', + autospec=True) as mock_get_node: + mock_get_node.return_value = self.fake_node + with mock.patch.object(self.dbapi, 'update_node', + autospec=True) as mock_update_node: + node = objects.Node.get_by_uuid(self.context, uuid) + node.type = 'bare' + node.save() + + mock_get_node.assert_called_once_with(uuid) + mock_update_node.assert_called_once_with( + uuid, {'type': 'bare'}) + self.assertEqual(self.context, node._context) + + def test_refresh(self): + uuid = self.fake_node['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_node, uuid=uuid), + dict(self.fake_node, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_node_by_uuid', + side_effect=returns, + autospec=True) as mock_get_node: + node = objects.Node.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, node.uuid) + node.refresh() + self.assertEqual(new_uuid, node.uuid) + self.assertEqual(expected, mock_get_node.call_args_list) + self.assertEqual(self.context, node._context) \ No newline at end of file diff --git a/magnum/tests/objects/test_pod.py b/magnum/tests/objects/test_pod.py new file mode 100644 index 0000000000..723aeae6e8 --- /dev/null +++ b/magnum/tests/objects/test_pod.py @@ -0,0 +1,116 @@ +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, 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. + +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils + + +class TestPodObject(base.DbTestCase): + + def setUp(self): + super(TestPodObject, self).setUp() + self.fake_pod = utils.get_test_pod() + + def test_get_by_id(self): + pod_id = self.fake_pod['id'] + with mock.patch.object(self.dbapi, 'get_pod_by_id', + autospec=True) as mock_get_pod: + mock_get_pod.return_value = self.fake_pod + pod = objects.Pod.get(self.context, pod_id) + mock_get_pod.assert_called_once_with(pod_id) + self.assertEqual(self.context, pod._context) + + def test_get_by_uuid(self): + uuid = self.fake_pod['uuid'] + with mock.patch.object(self.dbapi, 'get_pod_by_uuid', + autospec=True) as mock_get_pod: + mock_get_pod.return_value = self.fake_pod + pod = objects.Pod.get(self.context, uuid) + mock_get_pod.assert_called_once_with(uuid) + self.assertEqual(self.context, pod._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.Pod.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_pod_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_pod] + pods = objects.Pod.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(pods, HasLength(1)) + self.assertIsInstance(pods[0], objects.Pod) + self.assertEqual(self.context, pods[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_pod', + autospec=True) as mock_create_pod: + mock_create_pod.return_value = self.fake_pod + pod = objects.Pod(self.context, **self.fake_pod) + pod.create() + mock_create_pod.assert_called_once_with(self.fake_pod) + self.assertEqual(self.context, pod._context) + + def test_destroy(self): + uuid = self.fake_pod['uuid'] + with mock.patch.object(self.dbapi, 'get_pod_by_uuid', + autospec=True) as mock_get_pod: + mock_get_pod.return_value = self.fake_pod + with mock.patch.object(self.dbapi, 'destroy_pod', + autospec=True) as mock_destroy_pod: + pod = objects.Pod.get_by_uuid(self.context, uuid) + pod.destroy() + mock_get_pod.assert_called_once_with(uuid) + mock_destroy_pod.assert_called_once_with(uuid) + self.assertEqual(self.context, pod._context) + + def test_save(self): + uuid = self.fake_pod['uuid'] + with mock.patch.object(self.dbapi, 'get_pod_by_uuid', + autospec=True) as mock_get_pod: + mock_get_pod.return_value = self.fake_pod + with mock.patch.object(self.dbapi, 'update_pod', + autospec=True) as mock_update_pod: + pod = objects.Pod.get_by_uuid(self.context, uuid) + pod.desc = 'test-pod' + pod.save() + + mock_get_pod.assert_called_once_with(uuid) + mock_update_pod.assert_called_once_with( + uuid, {'desc': 'test-pod'}) + self.assertEqual(self.context, pod._context) + + def test_refresh(self): + uuid = self.fake_pod['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_pod, uuid=uuid), + dict(self.fake_pod, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_pod_by_uuid', + side_effect=returns, + autospec=True) as mock_get_pod: + pod = objects.Pod.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, pod.uuid) + pod.refresh() + self.assertEqual(new_uuid, pod.uuid) + self.assertEqual(expected, mock_get_pod.call_args_list) + self.assertEqual(self.context, pod._context) \ No newline at end of file diff --git a/magnum/tests/objects/test_replicationcontroller.py b/magnum/tests/objects/test_replicationcontroller.py new file mode 100644 index 0000000000..93b4321025 --- /dev/null +++ b/magnum/tests/objects/test_replicationcontroller.py @@ -0,0 +1,119 @@ +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, 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. + +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils + + +class TestReplicationControllerObject(base.DbTestCase): + + def setUp(self): + super(TestReplicationControllerObject, self).setUp() + self.fake_rc = utils.get_test_rc() + + def test_get_by_id(self): + rc_id = self.fake_rc['id'] + with mock.patch.object(self.dbapi, 'get_rc_by_id', + autospec=True) as mock_get_rc: + mock_get_rc.return_value = self.fake_rc + rc = objects.ReplicationController.get(self.context, rc_id) + mock_get_rc.assert_called_once_with(rc_id) + self.assertEqual(self.context, rc._context) + + def test_get_by_uuid(self): + uuid = self.fake_rc['uuid'] + with mock.patch.object(self.dbapi, 'get_rc_by_uuid', + autospec=True) as mock_get_rc: + mock_get_rc.return_value = self.fake_rc + rc = objects.ReplicationController.get(self.context, uuid) + mock_get_rc.assert_called_once_with(uuid) + self.assertEqual(self.context, rc._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.ReplicationController.get, + self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_rc_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_rc] + rcs = objects.ReplicationController.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(rcs, HasLength(1)) + self.assertIsInstance(rcs[0], objects.ReplicationController) + self.assertEqual(self.context, rcs[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_rc', + autospec=True) as mock_create_rc: + mock_create_rc.return_value = self.fake_rc + rc = objects.ReplicationController(self.context, **self.fake_rc) + rc.create() + mock_create_rc.assert_called_once_with(self.fake_rc) + self.assertEqual(self.context, rc._context) + + def test_destroy(self): + uuid = self.fake_rc['uuid'] + with mock.patch.object(self.dbapi, 'get_rc_by_uuid', + autospec=True) as mock_get_rc: + mock_get_rc.return_value = self.fake_rc + with mock.patch.object(self.dbapi, 'destroy_rc', + autospec=True) as mock_destroy_rc: + rc = objects.ReplicationController.get_by_uuid(self.context, + uuid) + rc.destroy() + mock_get_rc.assert_called_once_with(uuid) + mock_destroy_rc.assert_called_once_with(uuid) + self.assertEqual(self.context, rc._context) + + def test_save(self): + uuid = self.fake_rc['uuid'] + with mock.patch.object(self.dbapi, 'get_rc_by_uuid', + autospec=True) as mock_get_rc: + mock_get_rc.return_value = self.fake_rc + with mock.patch.object(self.dbapi, 'update_rc', + autospec=True) as mock_update_rc: + rc = objects.ReplicationController.get_by_uuid(self.context, + uuid) + rc.replicas = 10 + rc.save() + + mock_get_rc.assert_called_once_with(uuid) + mock_update_rc.assert_called_once_with( + uuid, {'replicas': 10}) + self.assertEqual(self.context, rc._context) + + def test_refresh(self): + uuid = self.fake_rc['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_rc, uuid=uuid), + dict(self.fake_rc, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_rc_by_uuid', + side_effect=returns, + autospec=True) as mock_get_rc: + rc = objects.ReplicationController.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, rc.uuid) + rc.refresh() + self.assertEqual(new_uuid, rc.uuid) + self.assertEqual(expected, mock_get_rc.call_args_list) + self.assertEqual(self.context, rc._context) \ No newline at end of file diff --git a/magnum/tests/objects/test_service.py b/magnum/tests/objects/test_service.py new file mode 100644 index 0000000000..00ce5eaf7e --- /dev/null +++ b/magnum/tests/objects/test_service.py @@ -0,0 +1,116 @@ +# Copyright 2015 OpenStack Foundation +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, 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. + +import mock +from testtools.matchers import HasLength + +from magnum.common import exception +from magnum.common import utils as magnum_utils +from magnum import objects +from magnum.tests.db import base +from magnum.tests.db import utils + + +class TestServiceObject(base.DbTestCase): + + def setUp(self): + super(TestServiceObject, self).setUp() + self.fake_service = utils.get_test_service() + + def test_get_by_id(self): + service_id = self.fake_service['id'] + with mock.patch.object(self.dbapi, 'get_service_by_id', + autospec=True) as mock_get_service: + mock_get_service.return_value = self.fake_service + service = objects.Service.get(self.context, service_id) + mock_get_service.assert_called_once_with(service_id) + self.assertEqual(self.context, service._context) + + def test_get_by_uuid(self): + uuid = self.fake_service['uuid'] + with mock.patch.object(self.dbapi, 'get_service_by_uuid', + autospec=True) as mock_get_service: + mock_get_service.return_value = self.fake_service + service = objects.Service.get(self.context, uuid) + mock_get_service.assert_called_once_with(uuid) + self.assertEqual(self.context, service._context) + + def test_get_bad_id_and_uuid(self): + self.assertRaises(exception.InvalidIdentity, + objects.Service.get, self.context, 'not-a-uuid') + + def test_list(self): + with mock.patch.object(self.dbapi, 'get_service_list', + autospec=True) as mock_get_list: + mock_get_list.return_value = [self.fake_service] + services = objects.Service.list(self.context) + self.assertEqual(mock_get_list.call_count, 1) + self.assertThat(services, HasLength(1)) + self.assertIsInstance(services[0], objects.Service) + self.assertEqual(self.context, services[0]._context) + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_service', + autospec=True) as mock_create_service: + mock_create_service.return_value = self.fake_service + service = objects.Service(self.context, **self.fake_service) + service.create() + mock_create_service.assert_called_once_with(self.fake_service) + self.assertEqual(self.context, service._context) + + def test_destroy(self): + uuid = self.fake_service['uuid'] + with mock.patch.object(self.dbapi, 'get_service_by_uuid', + autospec=True) as mock_get_service: + mock_get_service.return_value = self.fake_service + with mock.patch.object(self.dbapi, 'destroy_service', + autospec=True) as mock_destroy_service: + service = objects.Service.get_by_uuid(self.context, uuid) + service.destroy() + mock_get_service.assert_called_once_with(uuid) + mock_destroy_service.assert_called_once_with(uuid) + self.assertEqual(self.context, service._context) + + def test_save(self): + uuid = self.fake_service['uuid'] + with mock.patch.object(self.dbapi, 'get_service_by_uuid', + autospec=True) as mock_get_service: + mock_get_service.return_value = self.fake_service + with mock.patch.object(self.dbapi, 'update_service', + autospec=True) as mock_update_service: + service = objects.Service.get_by_uuid(self.context, uuid) + service.port = 4567 + service.save() + + mock_get_service.assert_called_once_with(uuid) + mock_update_service.assert_called_once_with( + uuid, {'port': 4567}) + self.assertEqual(self.context, service._context) + + def test_refresh(self): + uuid = self.fake_service['uuid'] + new_uuid = magnum_utils.generate_uuid() + returns = [dict(self.fake_service, uuid=uuid), + dict(self.fake_service, uuid=new_uuid)] + expected = [mock.call(uuid), mock.call(uuid)] + with mock.patch.object(self.dbapi, 'get_service_by_uuid', + side_effect=returns, + autospec=True) as mock_get_service: + service = objects.Service.get_by_uuid(self.context, uuid) + self.assertEqual(uuid, service.uuid) + service.refresh() + self.assertEqual(new_uuid, service.uuid) + self.assertEqual(expected, mock_get_service.call_args_list) + self.assertEqual(self.context, service._context) \ No newline at end of file