Merge "Yardstick: User interface for Yardstick."
[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._dispatch_script'.format(prefix))
131     def test_deploy(self, dispatch_script_mock):
132         obj = node.NodeContext()
133         obj.env = {
134             'type': 'script'
135         }
136         obj.deploy()
137         self.assertTrue(dispatch_script_mock.called)
138
139     @mock.patch('{}.NodeContext._dispatch_script'.format(prefix))
140     def test_undeploy(self, dispatch_script_mock):
141         obj = node.NodeContext()
142         obj.env = {
143             'type': 'script'
144         }
145         obj.undeploy()
146         self.assertTrue(dispatch_script_mock.called)
147
148     @mock.patch('{}.ssh.SSH._put_file_shell'.format(prefix))
149     @mock.patch('{}.ssh.SSH.execute'.format(prefix))
150     def test_execute_remote_script(self, execute_mock, put_file_mock):
151         obj = node.NodeContext()
152         obj.env = {'prefix': 'yardstick.benchmark.scenarios.compute'}
153         node_name_args = 'node5'
154         obj.nodes = [{
155             'name': node_name_args,
156             'user': 'ubuntu',
157             'ip': '10.10.10.10',
158             'pwd': 'ubuntu',
159         }]
160
161         info = {'script': 'computecapacity.bash'}
162         execute_mock.return_value = (0, '', '')
163         obj._execute_remote_script('node5', info)
164
165         self.assertTrue(put_file_mock.called)
166         self.assertTrue(execute_mock.called)
167
168     @mock.patch('{}.NodeContext._execute_local_script'.format(prefix))
169     def test_execute_script_local(self, local_execute_mock):
170         node_name = 'local'
171         info = {}
172         node.NodeContext()._execute_script(node_name, info)
173         self.assertTrue(local_execute_mock.called)
174
175     @mock.patch('{}.NodeContext._execute_remote_script'.format(prefix))
176     def test_execute_script_remote(self, remote_execute_mock):
177         node_name = 'node5'
178         info = {}
179         node.NodeContext()._execute_script(node_name, info)
180         self.assertTrue(remote_execute_mock.called)
181
182     def test_get_script(self):
183         script_args = 'hello.bash'
184         info_args = {
185             'script': script_args
186         }
187         script, options = node.NodeContext()._get_script(info_args)
188         self.assertEqual(script_args, script)
189         self.assertEqual('', options)
190
191     def test_node_info(self):
192         node_name_args = 'node5'
193         obj = node.NodeContext()
194         obj.nodes = [{'name': node_name_args, 'check': node_name_args}]
195         node_info = obj._get_node_info(node_name_args)
196         self.assertEqual(node_info.get('check'), node_name_args)
197
198     @mock.patch('{}.ssh.SSH.wait'.format(prefix))
199     def test_get_client(self, wait_mock):
200         node_name_args = 'node5'
201         obj = node.NodeContext()
202         obj.nodes = [{
203             'name': node_name_args,
204             'user': 'ubuntu',
205             'ip': '10.10.10.10',
206             'pwd': 'ubuntu',
207         }]
208         obj._get_client(node_name_args)
209         self.assertTrue(wait_mock.called)
210
211
212 def main():
213     unittest.main()
214
215
216 if __name__ == '__main__':
217     main()