Merge "Document for Fraser test case results"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_sec_group.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 from oslo_utils import uuidutils
11 import unittest
12 import mock
13
14 from yardstick.common import openstack_utils
15 from yardstick.common import exceptions
16 from yardstick.benchmark.scenarios.lib import create_sec_group
17
18
19 class CreateSecurityGroupTestCase(unittest.TestCase):
20
21     def setUp(self):
22
23         self._mock_create_security_group_full = mock.patch.object(
24             openstack_utils, 'create_security_group_full')
25         self.mock_create_security_group_full = (
26             self._mock_create_security_group_full.start())
27         self._mock_get_shade_client = mock.patch.object(
28             openstack_utils, 'get_shade_client')
29         self.mock_get_shade_client = self._mock_get_shade_client.start()
30         self._mock_log = mock.patch.object(create_sec_group, 'LOG')
31         self.mock_log = self._mock_log.start()
32         self.args = {'options': {'sg_name': 'yardstick_sg'}}
33         self.result = {}
34
35         self.csecgp_obj = create_sec_group.CreateSecgroup(self.args, mock.ANY)
36         self.addCleanup(self._stop_mock)
37
38     def _stop_mock(self):
39         self._mock_create_security_group_full.stop()
40         self._mock_get_shade_client.stop()
41         self._mock_log.stop()
42
43     def test_run(self):
44         _uuid = uuidutils.generate_uuid()
45         self.csecgp_obj.scenario_cfg = {'output': 'id'}
46         self.mock_create_security_group_full.return_value = _uuid
47         output = self.csecgp_obj.run(self.result)
48         self.assertEqual({'sg_create': 1}, self.result)
49         self.assertEqual({'id': _uuid}, output)
50         self.mock_log.info.asset_called_once_with(
51             'Create security group successful!')
52
53     def test_run_fail(self):
54         self.mock_create_security_group_full.return_value = None
55         with self.assertRaises(exceptions.ScenarioCreateSecurityGroupError):
56             self.csecgp_obj.run(self.result)
57         self.assertEqual({'sg_create': 0}, self.result)
58         self.mock_log.error.assert_called_once_with(
59             'Create security group failed!')