Context improvement: add support for configing node environment
[yardstick.git] / tests / unit / benchmark / contexts / test_node.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.contexts.node
13
14 from __future__ import absolute_import
15 import os
16 import unittest
17 import mock
18
19 from yardstick.benchmark.contexts import node
20
21
22 class NodeContextTestCase(unittest.TestCase):
23
24     NODES_SAMPLE = "nodes_sample.yaml"
25     NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"
26
27     def setUp(self):
28         self.test_context = node.NodeContext()
29
30     def test_construct(self):
31
32         self.assertIsNone(self.test_context.name)
33         self.assertIsNone(self.test_context.file_path)
34         self.assertEqual(self.test_context.nodes, [])
35         self.assertEqual(self.test_context.controllers, [])
36         self.assertEqual(self.test_context.computes, [])
37         self.assertEqual(self.test_context.baremetals, [])
38
39     def test_unsuccessful_init(self):
40
41         attrs = {
42             'name': 'foo',
43             'file': self._get_file_abspath("error_file")
44         }
45
46         self.assertRaises(IOError, self.test_context.init, attrs)
47
48     def test_successful_init(self):
49
50         attrs = {
51             'name': 'foo',
52             'file': self._get_file_abspath(self.NODES_SAMPLE)
53         }
54
55         self.test_context.init(attrs)
56
57         self.assertEqual(self.test_context.name, "foo")
58         self.assertEqual(len(self.test_context.nodes), 4)
59         self.assertEqual(len(self.test_context.controllers), 2)
60         self.assertEqual(len(self.test_context.computes), 1)
61         self.assertEqual(self.test_context.computes[0]["name"], "node3")
62         self.assertEqual(len(self.test_context.baremetals), 1)
63         self.assertEqual(self.test_context.baremetals[0]["name"], "node4")
64
65     def test__get_server_with_dic_attr_name(self):
66
67         attrs = {
68             'name': 'foo',
69             'file': self._get_file_abspath(self.NODES_SAMPLE)
70         }
71
72         self.test_context.init(attrs)
73
74         attr_name = {'name': 'foo.bar'}
75         result = self.test_context._get_server(attr_name)
76
77         self.assertEqual(result, None)
78
79     def test__get_server_not_found(self):
80
81         attrs = {
82             'name': 'foo',
83             'file': self._get_file_abspath(self.NODES_SAMPLE)
84         }
85
86         self.test_context.init(attrs)
87
88         attr_name = 'bar.foo'
89         result = self.test_context._get_server(attr_name)
90
91         self.assertEqual(result, None)
92
93     def test__get_server_duplicate(self):
94
95         attrs = {
96             'name': 'foo',
97             'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
98         }
99
100         self.test_context.init(attrs)
101
102         attr_name = 'node1.foo'
103
104         self.assertRaises(ValueError, self.test_context._get_server, attr_name)
105
106     def test__get_server_found(self):
107
108         attrs = {
109             'name': 'foo',
110             'file': self._get_file_abspath(self.NODES_SAMPLE)
111         }
112
113         self.test_context.init(attrs)
114
115         attr_name = 'node1.foo'
116         result = self.test_context._get_server(attr_name)
117
118         self.assertEqual(result['ip'], '10.229.47.137')
119         self.assertEqual(result['name'], 'node1.foo')
120         self.assertEqual(result['user'], 'root')
121         self.assertEqual(result['key_filename'], '/root/.yardstick_key')
122
123     def _get_file_abspath(self, filename):
124         curr_path = os.path.dirname(os.path.abspath(__file__))
125         file_path = os.path.join(curr_path, filename)
126         return file_path
127
128     prefix = 'yardstick.benchmark.contexts.node'
129
130     @mock.patch('{}.NodeContext._execute_script'.format(prefix))
131     def test_deploy(self, execute_script_mock):
132         obj = node.NodeContext()
133         obj.env = {
134             'setup': [
135                 {'node5': {}}
136             ]
137         }
138         obj.deploy()
139         self.assertTrue(execute_script_mock.called)
140
141     @mock.patch('{}.NodeContext._execute_script'.format(prefix))
142     def test_undeploy(self, execute_script_mock):
143         obj = node.NodeContext()
144         obj.env = {
145             'teardown': [
146                 {'node5': {}}
147             ]
148         }
149         obj.undeploy()
150         self.assertTrue(execute_script_mock.called)
151
152     @mock.patch('{}.ssh.SSH._put_file_shell'.format(prefix))
153     @mock.patch('{}.ssh.SSH.execute'.format(prefix))
154     def test_execute_remote_script(self, execute_mock, put_file_mock):
155         obj = node.NodeContext()
156         obj.env = {'prefix': 'yardstick.benchmark.scenarios.compute'}
157         node_name_args = 'node5'
158         obj.nodes = [{
159             'name': node_name_args,
160             'user': 'ubuntu',
161             'ip': '10.10.10.10',
162             'pwd': 'ubuntu',
163         }]
164
165         info = {'script': 'computecapacity.bash'}
166         execute_mock.return_value = (0, '', '')
167         obj._execute_remote_script('node5', info)
168
169         self.assertTrue(put_file_mock.called)
170         self.assertTrue(execute_mock.called)
171
172     @mock.patch('{}.NodeContext._execute_local_script'.format(prefix))
173     def test_execute_script_local(self, local_execute_mock):
174         node_name = 'local'
175         info = {}
176         node.NodeContext()._execute_script(node_name, info)
177         self.assertTrue(local_execute_mock.called)
178
179     @mock.patch('{}.NodeContext._execute_remote_script'.format(prefix))
180     def test_execute_script_remote(self, remote_execute_mock):
181         node_name = 'node5'
182         info = {}
183         node.NodeContext()._execute_script(node_name, info)
184         self.assertTrue(remote_execute_mock.called)
185
186     def test_get_script(self):
187         script_args = 'hello.bash'
188         info_args = {
189             'script': script_args
190         }
191         script, options = node.NodeContext()._get_script(info_args)
192         self.assertEqual(script_args, script)
193         self.assertEqual('', options)
194
195     def test_node_info(self):
196         node_name_args = 'node5'
197         obj = node.NodeContext()
198         obj.nodes = [{'name': node_name_args, 'check': node_name_args}]
199         node_info = obj._get_node_info(node_name_args)
200         self.assertEqual(node_info.get('check'), node_name_args)
201
202     @mock.patch('{}.ssh.SSH.wait'.format(prefix))
203     def test_get_client(self, wait_mock):
204         node_name_args = 'node5'
205         obj = node.NodeContext()
206         obj.nodes = [{
207             'name': node_name_args,
208             'user': 'ubuntu',
209             'ip': '10.10.10.10',
210             'pwd': 'ubuntu',
211         }]
212         obj._get_client(node_name_args)
213         self.assertTrue(wait_mock.called)
214
215
216 def main():
217     unittest.main()
218
219
220 if __name__ == '__main__':
221     main()