diff options
Diffstat (limited to 'yardstick/tests/unit/benchmark/scenarios/lib/test_create_image.py')
-rw-r--r-- | yardstick/tests/unit/benchmark/scenarios/lib/test_create_image.py | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/yardstick/tests/unit/benchmark/scenarios/lib/test_create_image.py b/yardstick/tests/unit/benchmark/scenarios/lib/test_create_image.py index 639cf2906..aebd1dfe8 100644 --- a/yardstick/tests/unit/benchmark/scenarios/lib/test_create_image.py +++ b/yardstick/tests/unit/benchmark/scenarios/lib/test_create_image.py @@ -6,30 +6,50 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import unittest + import mock +from oslo_utils import uuidutils +import unittest -from yardstick.benchmark.scenarios.lib import create_image from yardstick.common import openstack_utils +from yardstick.common import exceptions +from yardstick.benchmark.scenarios.lib import create_image + -# NOTE(elfoley): There should be more tests here. class CreateImageTestCase(unittest.TestCase): - @mock.patch.object(openstack_utils, 'create_image') - @mock.patch.object(openstack_utils, 'get_glance_client') - def test_create_image(self, mock_get_glance_client, mock_create_image): - options = { - 'image_name': 'yardstick_test_image_01', - 'disk_format': 'qcow2', - 'container_format': 'bare', - 'min_disk': '1', - 'min_ram': '512', - 'protected': 'False', - 'tags': '["yardstick automatic test image"]', - 'file_path': '/home/opnfv/images/cirros-0.3.5-x86_64-disk.img' - } - args = {"options": options} - obj = create_image.CreateImage(args, {}) - obj.run({}) - mock_create_image.assert_called_once() - mock_get_glance_client.assert_called_once() + def setUp(self): + self._mock_create_image = mock.patch.object( + openstack_utils, 'create_image') + self.mock_create_image = ( + self._mock_create_image.start()) + self._mock_get_shade_client = mock.patch.object( + openstack_utils, 'get_shade_client') + self.mock_get_shade_client = self._mock_get_shade_client.start() + self._mock_log = mock.patch.object(create_image, 'LOG') + self.mock_log = self._mock_log.start() + self.args = {'options': {'image_name': 'yardstick_image'}} + self.result = {} + self.cimage_obj = create_image.CreateImage(self.args, mock.ANY) + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_create_image.stop() + self._mock_get_shade_client.stop() + self._mock_log.stop() + + def test_run(self): + _uuid = uuidutils.generate_uuid() + self.cimage_obj.scenario_cfg = {'output': 'id'} + self.mock_create_image.return_value = _uuid + output = self.cimage_obj.run(self.result) + self.assertEqual({'image_create': 1}, self.result) + self.assertEqual({'id': _uuid}, output) + self.mock_log.info.asset_called_once_with('Create image successful!') + + def test_run_fail(self): + self.mock_create_image.return_value = None + with self.assertRaises(exceptions.ScenarioCreateImageError): + self.cimage_obj.run(self.result) + self.assertEqual({'image_create': 0}, self.result) + self.mock_log.error.assert_called_once_with('Create image failed!') |