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.common import yaml_loader
23 from yardstick.tests.unit import base as ut_base
24 from yardstick.common.constants import YARDSTICK_ROOT_PATH
27 class DummyContextClass(Context):
29 def __init__(self, host_name_separator='.'):
30 super(DummyContextClass, self).__init__\
31 (host_name_separator=host_name_separator)
37 def _get_network(self, *args):
40 def _get_server(self, *args):
49 def _get_physical_nodes(self):
52 def _get_physical_node_for_server(self, server_name):
56 class FlagsTestCase(ut_base.BaseUnitTestCase):
59 self.flags = base.Flags()
61 def test___init__(self):
62 self.assertFalse(self.flags.no_setup)
63 self.assertFalse(self.flags.no_teardown)
64 self.assertEqual({'verify': False}, self.flags.os_cloud_config)
66 def test___init__with_flags(self):
67 flags = base.Flags(no_setup=True)
68 self.assertTrue(flags.no_setup)
69 self.assertFalse(flags.no_teardown)
72 self.flags.parse(no_setup=True, no_teardown='False',
73 os_cloud_config={'verify': True})
75 self.assertTrue(self.flags.no_setup)
76 self.assertEqual('False', self.flags.no_teardown)
77 self.assertEqual({'verify': True}, self.flags.os_cloud_config)
79 def test_parse_forbidden_flags(self):
80 self.flags.parse(foo=42)
81 with self.assertRaises(AttributeError):
85 class ContextTestCase(ut_base.BaseUnitTestCase):
88 def _remove_ctx(ctx_obj):
89 if ctx_obj in base.Context.list:
90 base.Context.list.remove(ctx_obj)
92 def test_split_host_name(self):
93 ctx_obj = DummyContextClass()
94 self.addCleanup(self._remove_ctx, ctx_obj)
95 config_name = 'host_name.ctx_name'
96 self.assertEqual(('host_name', 'ctx_name'),
97 ctx_obj.split_host_name(config_name))
99 def test_split_host_name_wrong_separator(self):
100 ctx_obj = DummyContextClass()
101 self.addCleanup(self._remove_ctx, ctx_obj)
102 config_name = 'host_name-ctx_name'
103 self.assertEqual((None, None),
104 ctx_obj.split_host_name(config_name))
106 def test_split_host_name_other_separator(self):
107 ctx_obj = DummyContextClass(host_name_separator='-')
108 self.addCleanup(self._remove_ctx, ctx_obj)
109 config_name = 'host_name-ctx_name'
110 self.assertEqual(('host_name', 'ctx_name'),
111 ctx_obj.split_host_name(config_name))
113 def test_get_physical_nodes(self):
114 ctx_obj = DummyContextClass()
115 self.addCleanup(self._remove_ctx, ctx_obj)
117 result = Context.get_physical_nodes()
119 self.assertEqual(result, {None: None})
121 @mock.patch.object(Context, 'get_context_from_server')
122 def test_get_physical_node_from_server(self, mock_get_ctx):
123 ctx_obj = DummyContextClass()
124 self.addCleanup(self._remove_ctx, ctx_obj)
126 mock_get_ctx.return_value = ctx_obj
128 result = Context.get_physical_node_from_server("mock_server")
130 mock_get_ctx.assert_called_once()
131 self.assertIsNone(result)
133 @mock.patch.object(yaml_loader, 'read_yaml_file')
134 def test_read_pod_file(self, mock_read_yaml_file):
135 attrs = {'name': 'foo',
136 'task_id': '12345678',
140 ctx_obj = DummyContextClass()
144 "role": "Controller",
145 "ip": "10.229.47.137",
147 "key_filename": "/root/.yardstick_key"
152 "ip": "10.229.47.139",
154 "key_filename": "/root/.yardstick_key"
159 mock_read_yaml_file.return_value = cfg
160 result = ctx_obj.read_pod_file(attrs)
161 self.assertEqual(result, cfg)
163 mock_read_yaml_file.side_effect = IOError(errno.EPERM, '')
164 with self.assertRaises(IOError):
165 ctx_obj.read_pod_file(attrs)
167 mock_read_yaml_file.side_effect = IOError(errno.ENOENT, '')
168 with self.assertRaises(IOError):
169 ctx_obj.read_pod_file(attrs)
171 file_path = os.path.join(YARDSTICK_ROOT_PATH, 'pod.yaml')
172 self.assertEqual(ctx_obj.file_path, file_path)