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