Merge "Fix up formatting on devguide"
[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     __context_type__ = "Dummy"
29
30     def __init__(self, host_name_separator='.'):
31         super(DummyContextClass, self).__init__\
32             (host_name_separator=host_name_separator)
33         self.nodes = []
34         self.controllers = []
35         self.computes = []
36         self.baremetals = []
37
38     def _get_network(self, *args):
39         pass
40
41     def _get_server(self, *args):
42         pass
43
44     def deploy(self):
45         pass
46
47     def undeploy(self):
48         pass
49
50     def _get_physical_nodes(self):
51         pass
52
53     def _get_physical_node_for_server(self, server_name):
54         pass
55
56
57 class FlagsTestCase(ut_base.BaseUnitTestCase):
58
59     def setUp(self):
60         self.flags = base.Flags()
61
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)
66
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)
71
72     def test_parse(self):
73         self.flags.parse(no_setup=True, no_teardown='False',
74                          os_cloud_config={'verify': True})
75
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)
79
80     def test_parse_forbidden_flags(self):
81         self.flags.parse(foo=42)
82         with self.assertRaises(AttributeError):
83             _ = self.flags.foo
84
85
86 class ContextTestCase(ut_base.BaseUnitTestCase):
87
88     @staticmethod
89     def _remove_ctx(ctx_obj):
90         if ctx_obj in base.Context.list:
91             base.Context.list.remove(ctx_obj)
92
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))
99
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))
106
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))
113
114     def test_get_physical_nodes(self):
115         ctx_obj = DummyContextClass()
116         self.addCleanup(self._remove_ctx, ctx_obj)
117
118         result = Context.get_physical_nodes()
119
120         self.assertEqual(result, {None: None})
121
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)
126
127         mock_get_ctx.return_value = ctx_obj
128
129         result = Context.get_physical_node_from_server("mock_server")
130
131         mock_get_ctx.assert_called_once()
132         self.assertIsNone(result)
133
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',
138                  'file': 'pod.yaml'
139                  }
140
141         ctx_obj = DummyContextClass()
142         cfg = {"nodes": [
143                     {
144                         "name": "node1",
145                         "role": "Controller",
146                         "ip": "10.229.47.137",
147                         "user": "root",
148                         "key_filename": "/root/.yardstick_key"
149                     },
150                     {
151                         "name": "node2",
152                         "role": "Compute",
153                         "ip": "10.229.47.139",
154                         "user": "root",
155                         "key_filename": "/root/.yardstick_key"
156                     }
157                 ]
158             }
159
160         mock_read_yaml_file.return_value = cfg
161         result = ctx_obj.read_pod_file(attrs)
162         self.assertEqual(result, cfg)
163
164         mock_read_yaml_file.side_effect = IOError(errno.EPERM, '')
165         with self.assertRaises(IOError):
166             ctx_obj.read_pod_file(attrs)
167
168         mock_read_yaml_file.side_effect = IOError(errno.ENOENT, '')
169         with self.assertRaises(IOError):
170             ctx_obj.read_pod_file(attrs)
171
172         file_path = os.path.join(YARDSTICK_ROOT_PATH, 'pod.yaml')
173         self.assertEqual(ctx_obj.file_path, file_path)