1 # Copyright (c) 2018 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 class DummyContextClass(Context):
28 def __init__(self, host_name_separator='.'):
29 super(DummyContextClass, self).__init__\
30 (host_name_separator=host_name_separator)
36 def _get_network(self, *args):
39 def _get_server(self, *args):
48 def _get_physical_nodes(self):
51 def _get_physical_node_for_server(self, server_name):
55 class FlagsTestCase(ut_base.BaseUnitTestCase):
58 self.flags = base.Flags()
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)
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)
71 self.flags.parse(no_setup=True, no_teardown='False',
72 os_cloud_config={'verify': True})
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)
78 def test_parse_forbidden_flags(self):
79 self.flags.parse(foo=42)
80 with self.assertRaises(AttributeError):
84 class ContextTestCase(ut_base.BaseUnitTestCase):
87 def _remove_ctx(ctx_obj):
88 if ctx_obj in base.Context.list:
89 base.Context.list.remove(ctx_obj)
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))
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))
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))
112 def test_get_physical_nodes(self):
113 ctx_obj = DummyContextClass()
114 self.addCleanup(self._remove_ctx, ctx_obj)
116 result = Context.get_physical_nodes()
118 self.assertEqual(result, {None: None})
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)
125 mock_get_ctx.return_value = ctx_obj
127 result = Context.get_physical_node_from_server("mock_server")
129 mock_get_ctx.assert_called_once()
130 self.assertIsNone(result)
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',
139 ctx_obj = DummyContextClass()
143 "role": "Controller",
144 "ip": "10.229.47.137",
146 "key_filename": "/root/.yardstick_key"
151 "ip": "10.229.47.139",
153 "key_filename": "/root/.yardstick_key"
158 mock_read_yaml_file.return_value = cfg
159 result = ctx_obj.read_pod_file(attrs)
160 self.assertEqual(result, cfg)
162 mock_read_yaml_file.side_effect = IOError(errno.EPERM, '')
163 with self.assertRaises(IOError):
164 ctx_obj.read_pod_file(attrs)
166 mock_read_yaml_file.side_effect = IOError(errno.ENOENT, '')
167 with self.assertRaises(IOError):
168 ctx_obj.read_pod_file(attrs)
170 file_path = os.path.join(YARDSTICK_ROOT_PATH, 'pod.yaml')
171 self.assertEqual(ctx_obj.file_path, file_path)