add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_image.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import mock
11 from oslo_utils import uuidutils
12 import unittest
13
14 from yardstick.common import openstack_utils
15 from yardstick.common import exceptions
16 from yardstick.benchmark.scenarios.lib import create_image
17
18
19 class CreateImageTestCase(unittest.TestCase):
20
21     def setUp(self):
22         self._mock_create_image = mock.patch.object(
23             openstack_utils, 'create_image')
24         self.mock_create_image = (
25             self._mock_create_image.start())
26         self._mock_get_shade_client = mock.patch.object(
27             openstack_utils, 'get_shade_client')
28         self.mock_get_shade_client = self._mock_get_shade_client.start()
29         self._mock_log = mock.patch.object(create_image, 'LOG')
30         self.mock_log = self._mock_log.start()
31         self.args = {'options': {'image_name': 'yardstick_image'}}
32         self.result = {}
33         self.cimage_obj = create_image.CreateImage(self.args, mock.ANY)
34         self.addCleanup(self._stop_mock)
35
36     def _stop_mock(self):
37         self._mock_create_image.stop()
38         self._mock_get_shade_client.stop()
39         self._mock_log.stop()
40
41     def test_run(self):
42         _uuid = uuidutils.generate_uuid()
43         self.cimage_obj.scenario_cfg = {'output': 'id'}
44         self.mock_create_image.return_value = _uuid
45         output = self.cimage_obj.run(self.result)
46         self.assertEqual({'image_create': 1}, self.result)
47         self.assertEqual({'id': _uuid}, output)
48         self.mock_log.info.asset_called_once_with('Create image successful!')
49
50     def test_run_fail(self):
51         self.mock_create_image.return_value = None
52         with self.assertRaises(exceptions.ScenarioCreateImageError):
53             self.cimage_obj.run(self.result)
54         self.assertEqual({'image_create': 0}, self.result)
55         self.mock_log.error.assert_called_once_with('Create image failed!')