Merge "Bugfix: heat conext of test case yamls jinja2 bypass sriov type"
[yardstick.git] / yardstick / tests / unit / benchmark / contexts / test_base.py
1 # Copyright (c) 2018 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import errno
17
18 import mock
19
20 from yardstick.benchmark.contexts import base
21 from yardstick.benchmark.contexts.base import Context
22 from yardstick.tests.unit import base as ut_base
23 from yardstick.common.constants import YARDSTICK_ROOT_PATH
24
25
26 class DummyContextClass(Context):
27
28     def __init__(self, host_name_separator='.'):
29         super(DummyContextClass, self).__init__\
30             (host_name_separator=host_name_separator)
31         self.nodes = []
32         self.controllers = []
33         self.computes = []
34         self.baremetals = []
35
36     def _get_network(self, *args):
37         pass
38
39     def _get_server(self, *args):
40         pass
41
42     def deploy(self):
43         pass
44
45     def undeploy(self):
46         pass
47
48     def _get_physical_nodes(self):
49         pass
50
51     def _get_physical_node_for_server(self, server_name):
52         pass
53
54
55 class FlagsTestCase(ut_base.BaseUnitTestCase):
56
57     def setUp(self):
58         self.flags = base.Flags()
59
60     def test___init__(self):
61         self.assertFalse(self.flags.no_setup)
62         self.assertFalse(self.flags.no_teardown)
63         self.assertEqual({'verify': False}, self.flags.os_cloud_config)
64
65     def test___init__with_flags(self):
66         flags = base.Flags(no_setup=True)
67         self.assertTrue(flags.no_setup)
68         self.assertFalse(flags.no_teardown)
69
70     def test_parse(self):
71         self.flags.parse(no_setup=True, no_teardown='False',
72                          os_cloud_config={'verify': True})
73
74         self.assertTrue(self.flags.no_setup)
75         self.assertEqual('False', self.flags.no_teardown)
76         self.assertEqual({'verify': True}, self.flags.os_cloud_config)
77
78     def test_parse_forbidden_flags(self):
79         self.flags.parse(foo=42)
80         with self.assertRaises(AttributeError):
81             _ = self.flags.foo
82
83
84 class ContextTestCase(ut_base.BaseUnitTestCase):
85
86     @staticmethod
87     def _remove_ctx(ctx_obj):
88         if ctx_obj in base.Context.list:
89             base.Context.list.remove(ctx_obj)
90
91     def test_split_host_name(self):
92         ctx_obj = DummyContextClass()
93         self.addCleanup(self._remove_ctx, ctx_obj)
94         config_name = 'host_name.ctx_name'
95         self.assertEqual(('host_name', 'ctx_name'),
96                          ctx_obj.split_host_name(config_name))
97
98     def test_split_host_name_wrong_separator(self):
99         ctx_obj = DummyContextClass()
100         self.addCleanup(self._remove_ctx, ctx_obj)
101         config_name = 'host_name-ctx_name'
102         self.assertEqual((None, None),
103                          ctx_obj.split_host_name(config_name))
104
105     def test_split_host_name_other_separator(self):
106         ctx_obj = DummyContextClass(host_name_separator='-')
107         self.addCleanup(self._remove_ctx, ctx_obj)
108         config_name = 'host_name-ctx_name'
109         self.assertEqual(('host_name', 'ctx_name'),
110                          ctx_obj.split_host_name(config_name))
111
112     def test_get_physical_nodes(self):
113         ctx_obj = DummyContextClass()
114         self.addCleanup(self._remove_ctx, ctx_obj)
115
116         result = Context.get_physical_nodes()
117
118         self.assertEqual(result, {None: None})
119
120     @mock.patch.object(Context, 'get_context_from_server')
121     def test_get_physical_node_from_server(self, mock_get_ctx):
122         ctx_obj = DummyContextClass()
123         self.addCleanup(self._remove_ctx, ctx_obj)
124
125         mock_get_ctx.return_value = ctx_obj
126
127         result = Context.get_physical_node_from_server("mock_server")
128
129         mock_get_ctx.assert_called_once()
130         self.assertIsNone(result)
131
132     @mock.patch('yardstick.common.utils.read_yaml_file')
133     def test_read_pod_file(self, mock_read_yaml_file):
134         attrs = {'name': 'foo',
135                  'task_id': '12345678',
136                  'file': 'pod.yaml'
137                  }
138
139         ctx_obj = DummyContextClass()
140         cfg = {"nodes": [
141                     {
142                         "name": "node1",
143                         "role": "Controller",
144                         "ip": "10.229.47.137",
145                         "user": "root",
146                         "key_filename": "/root/.yardstick_key"
147                     },
148                     {
149                         "name": "node2",
150                         "role": "Compute",
151                         "ip": "10.229.47.139",
152                         "user": "root",
153                         "key_filename": "/root/.yardstick_key"
154                     }
155                 ]
156             }
157
158         mock_read_yaml_file.return_value = cfg
159         result = ctx_obj.read_pod_file(attrs)
160         self.assertEqual(result, cfg)
161
162         mock_read_yaml_file.side_effect = IOError(errno.EPERM, '')
163         with self.assertRaises(IOError):
164             ctx_obj.read_pod_file(attrs)
165
166         mock_read_yaml_file.side_effect = IOError(errno.ENOENT, '')
167         with self.assertRaises(IOError):
168             ctx_obj.read_pod_file(attrs)
169
170         file_path = os.path.join(YARDSTICK_ROOT_PATH, 'pod.yaml')
171         self.assertEqual(ctx_obj.file_path, file_path)