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 __context_type__ = "Dummy"
31 def __init__(self, host_name_separator='.'):
32 super(DummyContextClass, self).__init__\
33 (host_name_separator=host_name_separator)
39 def _get_network(self, *args):
42 def _get_server(self, *args):
51 def _get_physical_nodes(self):
54 def _get_physical_node_for_server(self, server_name):
58 class FlagsTestCase(ut_base.BaseUnitTestCase):
61 self.flags = base.Flags()
63 def test___init__(self):
64 self.assertFalse(self.flags.no_setup)
65 self.assertFalse(self.flags.no_teardown)
66 self.assertEqual({'verify': False}, self.flags.os_cloud_config)
68 def test___init__with_flags(self):
69 flags = base.Flags(no_setup=True)
70 self.assertTrue(flags.no_setup)
71 self.assertFalse(flags.no_teardown)
74 self.flags.parse(no_setup=True, no_teardown='False',
75 os_cloud_config={'verify': True})
77 self.assertTrue(self.flags.no_setup)
78 self.assertEqual('False', self.flags.no_teardown)
79 self.assertEqual({'verify': True}, self.flags.os_cloud_config)
81 def test_parse_forbidden_flags(self):
82 self.flags.parse(foo=42)
83 with self.assertRaises(AttributeError):
87 class ContextTestCase(ut_base.BaseUnitTestCase):
90 def _remove_ctx(ctx_obj):
91 if ctx_obj in base.Context.list:
92 base.Context.list.remove(ctx_obj)
94 def test_split_host_name(self):
95 ctx_obj = DummyContextClass()
96 self.addCleanup(self._remove_ctx, ctx_obj)
97 config_name = 'host_name.ctx_name'
98 self.assertEqual(('host_name', 'ctx_name'),
99 ctx_obj.split_host_name(config_name))
101 def test_split_host_name_wrong_separator(self):
102 ctx_obj = DummyContextClass()
103 self.addCleanup(self._remove_ctx, ctx_obj)
104 config_name = 'host_name-ctx_name'
105 self.assertEqual((None, None),
106 ctx_obj.split_host_name(config_name))
108 def test_split_host_name_other_separator(self):
109 ctx_obj = DummyContextClass(host_name_separator='-')
110 self.addCleanup(self._remove_ctx, ctx_obj)
111 config_name = 'host_name-ctx_name'
112 self.assertEqual(('host_name', 'ctx_name'),
113 ctx_obj.split_host_name(config_name))
115 def test_get_physical_nodes(self):
116 ctx_obj = DummyContextClass()
117 self.addCleanup(self._remove_ctx, ctx_obj)
119 result = Context.get_physical_nodes()
121 self.assertEqual(result, {None: None})
123 @mock.patch.object(Context, 'get_context_from_server')
124 def test_get_physical_node_from_server(self, mock_get_ctx):
125 ctx_obj = DummyContextClass()
126 self.addCleanup(self._remove_ctx, ctx_obj)
128 mock_get_ctx.return_value = ctx_obj
130 result = Context.get_physical_node_from_server("mock_server")
132 mock_get_ctx.assert_called_once()
133 self.assertIsNone(result)
135 @mock.patch.object(yaml_loader, 'read_yaml_file')
136 def test_read_pod_file(self, mock_read_yaml_file):
137 attrs = {'name': 'foo',
138 'task_id': '12345678',
142 ctx_obj = DummyContextClass()
146 "role": "Controller",
147 "ip": "10.229.47.137",
149 "key_filename": "/root/.yardstick_key"
154 "ip": "10.229.47.139",
156 "key_filename": "/root/.yardstick_key"
161 mock_read_yaml_file.return_value = cfg
162 result = ctx_obj.read_pod_file(attrs)
163 self.assertEqual(result, cfg)
165 mock_read_yaml_file.side_effect = IOError(errno.EPERM, '')
166 with self.assertRaises(IOError):
167 ctx_obj.read_pod_file(attrs)
169 mock_read_yaml_file.side_effect = IOError(errno.ENOENT, '')
170 with self.assertRaises(IOError):
171 ctx_obj.read_pod_file(attrs)
173 file_path = os.path.join(YARDSTICK_ROOT_PATH, 'pod.yaml')
174 self.assertEqual(ctx_obj.file_path, file_path)