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._dispatch_script'.format(prefix))
131 def test_deploy(self, dispatch_script_mock):
132 obj = node.NodeContext()
137 self.assertTrue(dispatch_script_mock.called)
139 @mock.patch('{}.NodeContext._dispatch_script'.format(prefix))
140 def test_undeploy(self, dispatch_script_mock):
141 obj = node.NodeContext()
146 self.assertTrue(dispatch_script_mock.called)
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'
155 'name': node_name_args,
161 info = {'script': 'computecapacity.bash'}
162 execute_mock.return_value = (0, '', '')
163 obj._execute_remote_script('node5', info)
165 self.assertTrue(put_file_mock.called)
166 self.assertTrue(execute_mock.called)
168 @mock.patch('{}.NodeContext._execute_local_script'.format(prefix))
169 def test_execute_script_local(self, local_execute_mock):
172 node.NodeContext()._execute_script(node_name, info)
173 self.assertTrue(local_execute_mock.called)
175 @mock.patch('{}.NodeContext._execute_remote_script'.format(prefix))
176 def test_execute_script_remote(self, remote_execute_mock):
179 node.NodeContext()._execute_script(node_name, info)
180 self.assertTrue(remote_execute_mock.called)
182 def test_get_script(self):
183 script_args = 'hello.bash'
185 'script': script_args
187 script, options = node.NodeContext()._get_script(info_args)
188 self.assertEqual(script_args, script)
189 self.assertEqual('', options)
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)
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()
203 'name': node_name_args,
208 obj._get_client(node_name_args)
209 self.assertTrue(wait_mock.called)
216 if __name__ == '__main__':