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 __context_type__ = "Dummy"
30 def __init__(self, host_name_separator='.'):
31 super(DummyContextClass, self).__init__\
32 (host_name_separator=host_name_separator)
38 def _get_network(self, *args):
41 def _get_server(self, *args):
50 def _get_physical_nodes(self):
53 def _get_physical_node_for_server(self, server_name):
57 class FlagsTestCase(ut_base.BaseUnitTestCase):
60 self.flags = base.Flags()
62 def test___init__(self):
63 self.assertFalse(self.flags.no_setup)
64 self.assertFalse(self.flags.no_teardown)
65 self.assertEqual({'verify': False}, self.flags.os_cloud_config)
67 def test___init__with_flags(self):
68 flags = base.Flags(no_setup=True)
69 self.assertTrue(flags.no_setup)
70 self.assertFalse(flags.no_teardown)
73 self.flags.parse(no_setup=True, no_teardown='False',
74 os_cloud_config={'verify': True})
76 self.assertTrue(self.flags.no_setup)
77 self.assertEqual('False', self.flags.no_teardown)
78 self.assertEqual({'verify': True}, self.flags.os_cloud_config)
80 def test_parse_forbidden_flags(self):
81 self.flags.parse(foo=42)
82 with self.assertRaises(AttributeError):
86 class ContextTestCase(ut_base.BaseUnitTestCase):
89 def _remove_ctx(ctx_obj):
90 if ctx_obj in base.Context.list:
91 base.Context.list.remove(ctx_obj)
93 def test_split_host_name(self):
94 ctx_obj = DummyContextClass()
95 self.addCleanup(self._remove_ctx, ctx_obj)
96 config_name = 'host_name.ctx_name'
97 self.assertEqual(('host_name', 'ctx_name'),
98 ctx_obj.split_host_name(config_name))
100 def test_split_host_name_wrong_separator(self):
101 ctx_obj = DummyContextClass()
102 self.addCleanup(self._remove_ctx, ctx_obj)
103 config_name = 'host_name-ctx_name'
104 self.assertEqual((None, None),
105 ctx_obj.split_host_name(config_name))
107 def test_split_host_name_other_separator(self):
108 ctx_obj = DummyContextClass(host_name_separator='-')
109 self.addCleanup(self._remove_ctx, ctx_obj)
110 config_name = 'host_name-ctx_name'
111 self.assertEqual(('host_name', 'ctx_name'),
112 ctx_obj.split_host_name(config_name))
114 def test_get_physical_nodes(self):
115 ctx_obj = DummyContextClass()
116 self.addCleanup(self._remove_ctx, ctx_obj)
118 result = Context.get_physical_nodes()
120 self.assertEqual(result, {None: None})
122 @mock.patch.object(Context, 'get_context_from_server')
123 def test_get_physical_node_from_server(self, mock_get_ctx):
124 ctx_obj = DummyContextClass()
125 self.addCleanup(self._remove_ctx, ctx_obj)
127 mock_get_ctx.return_value = ctx_obj
129 result = Context.get_physical_node_from_server("mock_server")
131 mock_get_ctx.assert_called_once()
132 self.assertIsNone(result)
134 @mock.patch('yardstick.common.utils.read_yaml_file')
135 def test_read_pod_file(self, mock_read_yaml_file):
136 attrs = {'name': 'foo',
137 'task_id': '12345678',
141 ctx_obj = DummyContextClass()
145 "role": "Controller",
146 "ip": "10.229.47.137",
148 "key_filename": "/root/.yardstick_key"
153 "ip": "10.229.47.139",
155 "key_filename": "/root/.yardstick_key"
160 mock_read_yaml_file.return_value = cfg
161 result = ctx_obj.read_pod_file(attrs)
162 self.assertEqual(result, cfg)
164 mock_read_yaml_file.side_effect = IOError(errno.EPERM, '')
165 with self.assertRaises(IOError):
166 ctx_obj.read_pod_file(attrs)
168 mock_read_yaml_file.side_effect = IOError(errno.ENOENT, '')
169 with self.assertRaises(IOError):
170 ctx_obj.read_pod_file(attrs)
172 file_path = os.path.join(YARDSTICK_ROOT_PATH, 'pod.yaml')
173 self.assertEqual(ctx_obj.file_path, file_path)