3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
12 # Unittest for yardstick.benchmark.contexts.node
14 from __future__ import absolute_import
19 from yardstick.benchmark.contexts import node
22 class NodeContextTestCase(unittest.TestCase):
24 NODES_SAMPLE = "nodes_sample.yaml"
25 NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"
28 self.test_context = node.NodeContext()
30 def test_construct(self):
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, [])
39 def test_unsuccessful_init(self):
43 'file': self._get_file_abspath("error_file")
46 self.assertRaises(IOError, self.test_context.init, attrs)
48 def test_successful_init(self):
52 'file': self._get_file_abspath(self.NODES_SAMPLE)
55 self.test_context.init(attrs)
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")
65 def test__get_server_with_dic_attr_name(self):
69 'file': self._get_file_abspath(self.NODES_SAMPLE)
72 self.test_context.init(attrs)
74 attr_name = {'name': 'foo.bar'}
75 result = self.test_context._get_server(attr_name)
77 self.assertEqual(result, None)
79 def test__get_server_not_found(self):
83 'file': self._get_file_abspath(self.NODES_SAMPLE)
86 self.test_context.init(attrs)
89 result = self.test_context._get_server(attr_name)
91 self.assertEqual(result, None)
93 def test__get_server_duplicate(self):
97 'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
100 self.test_context.init(attrs)
102 attr_name = 'node1.foo'
104 self.assertRaises(ValueError, self.test_context._get_server, attr_name)
106 def test__get_server_found(self):
110 'file': self._get_file_abspath(self.NODES_SAMPLE)
113 self.test_context.init(attrs)
115 attr_name = 'node1.foo'
116 result = self.test_context._get_server(attr_name)
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')
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)
128 prefix = 'yardstick.benchmark.contexts.node'
130 @mock.patch('{}.NodeContext._execute_script'.format(prefix))
131 def test_deploy(self, execute_script_mock):
132 obj = node.NodeContext()
139 self.assertTrue(execute_script_mock.called)
141 @mock.patch('{}.NodeContext._execute_script'.format(prefix))
142 def test_undeploy(self, execute_script_mock):
143 obj = node.NodeContext()
150 self.assertTrue(execute_script_mock.called)
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'
159 'name': node_name_args,
165 info = {'script': 'computecapacity.bash'}
166 execute_mock.return_value = (0, '', '')
167 obj._execute_remote_script('node5', info)
169 self.assertTrue(put_file_mock.called)
170 self.assertTrue(execute_mock.called)
172 @mock.patch('{}.NodeContext._execute_local_script'.format(prefix))
173 def test_execute_script_local(self, local_execute_mock):
176 node.NodeContext()._execute_script(node_name, info)
177 self.assertTrue(local_execute_mock.called)
179 @mock.patch('{}.NodeContext._execute_remote_script'.format(prefix))
180 def test_execute_script_remote(self, remote_execute_mock):
183 node.NodeContext()._execute_script(node_name, info)
184 self.assertTrue(remote_execute_mock.called)
186 def test_get_script(self):
187 script_args = 'hello.bash'
189 'script': script_args
191 script, options = node.NodeContext()._get_script(info_args)
192 self.assertEqual(script_args, script)
193 self.assertEqual('', options)
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)
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()
207 'name': node_name_args,
212 obj._get_client(node_name_args)
213 self.assertTrue(wait_mock.called)
220 if __name__ == '__main__':