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