Merge "Create Dockerfile to create a yardstick-image of docker"
[yardstick.git] / yardstick / tests / unit / benchmark / contexts / test_node.py
index 9761f6d..da16074 100644 (file)
@@ -8,12 +8,16 @@
 ##############################################################################
 
 import os
-import unittest
 import errno
+
 import mock
+import unittest
 
-from yardstick.common import constants as consts
+from yardstick.benchmark.contexts import base
 from yardstick.benchmark.contexts import node
+from yardstick.common import constants as consts
+from yardstick.common import exceptions
+from yardstick.common import yaml_loader
 
 
 class NodeContextTestCase(unittest.TestCase):
@@ -33,9 +37,11 @@ class NodeContextTestCase(unittest.TestCase):
             'file': self._get_file_abspath(self.NODES_SAMPLE)
         }
 
-    def _remove_contexts(self):
-        if self.test_context in self.test_context.list:
-            self.test_context._delete_context()
+    @staticmethod
+    def _remove_contexts():
+        for context in base.Context.list:
+            context._delete_context()
+        base.Context.list = []
 
     def _get_file_abspath(self, filename):
         curr_path = os.path.dirname(os.path.abspath(__file__))
@@ -52,8 +58,9 @@ class NodeContextTestCase(unittest.TestCase):
         self.assertEqual(self.test_context.env, {})
         self.assertEqual(self.test_context.attrs, {})
 
+    @mock.patch.object(yaml_loader, 'read_yaml_file')
     @mock.patch('{}.os.path.join'.format(PREFIX))
-    def test_init_negative(self, mock_path_join):
+    def test_init_negative(self, mock_path_join, read_mock):
         special_path = '/foo/bar/error_file'
         error_path = self._get_file_abspath("error_file")
 
@@ -65,7 +72,6 @@ class NodeContextTestCase(unittest.TestCase):
         # we can't count mock_path_join calls because
         # it can catch join calls for .pyc files.
         mock_path_join.side_effect = path_join
-        self.test_context.read_config_file = read_mock = mock.Mock()
         read_calls = 0
 
         with self.assertRaises(KeyError):
@@ -83,7 +89,7 @@ class NodeContextTestCase(unittest.TestCase):
             self.test_context.init(attrs)
 
         read_calls += 1
-        self.assertEqual(read_mock.called, read_calls)
+        self.assertEqual(read_mock.call_count, read_calls)
         self.assertIn(attrs['file'], self.test_context.file_path)
         self.assertEqual(raised.exception.errno, errno.EBUSY)
         self.assertEqual(str(raised.exception), str(read_mock.side_effect))
@@ -98,11 +104,6 @@ class NodeContextTestCase(unittest.TestCase):
         self.assertEqual(raised.exception.errno, errno.ENOENT)
         self.assertEqual(str(raised.exception), str(read_mock.side_effect))
 
-    def test_read_config_file(self):
-        self.test_context.init(self.attrs)
-
-        self.assertIsNotNone(self.test_context.read_config_file())
-
     def test__dispatch_script(self):
         self.test_context.init(self.attrs)
 
@@ -168,6 +169,39 @@ class NodeContextTestCase(unittest.TestCase):
         self.assertEqual(result['user'], 'root')
         self.assertEqual(result['key_filename'], '/root/.yardstick_key')
 
+    def test__get_physical_nodes(self):
+        self.test_context.init(self.attrs)
+        nodes = self.test_context._get_physical_nodes()
+        self.assertEqual(nodes, self.test_context.nodes)
+
+    def test__get_physical_node_for_server(self):
+        self.test_context.init(self.attrs)
+
+        # When server is not from this context
+        result = self.test_context._get_physical_node_for_server('node1.another-context')
+        self.assertIsNone(result)
+
+        # When node_name is not from this context
+        result = self.test_context._get_physical_node_for_server('fake.foo-12345678')
+        self.assertIsNone(result)
+
+        result = self.test_context._get_physical_node_for_server('node1.foo-12345678')
+        self.assertEqual(result, 'node1.foo')
+
+    def test_update_collectd_options_for_node(self):
+        self.test_context.init(self.attrs)
+        options = {'collectd': {'interval': 5}}
+
+        with self.assertRaises(exceptions.ContextUpdateCollectdForNodeError):
+            self.test_context.update_collectd_options_for_node(options, 'fake.foo-12345678')
+
+        self.test_context.update_collectd_options_for_node(options, 'node1.foo-12345678')
+
+        node_collectd_options = [node for node in self.test_context.nodes
+                                 if node['name'] == 'node1'][0]['collectd']
+
+        self.assertEqual(node_collectd_options, options)
+
     @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
     def test_deploy(self, dispatch_script_mock):
         obj = node.NodeContext()
@@ -176,7 +210,7 @@ class NodeContextTestCase(unittest.TestCase):
             'type': 'script'
         }
         obj.deploy()
-        self.assertTrue(dispatch_script_mock.called)
+        dispatch_script_mock.assert_called_once()
 
     @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
     def test_deploy_anisible(self, dispatch_ansible_mock):
@@ -186,7 +220,7 @@ class NodeContextTestCase(unittest.TestCase):
             'type': 'ansible'
         }
         obj.deploy()
-        self.assertTrue(dispatch_ansible_mock.called)
+        dispatch_ansible_mock.assert_called_once()
 
     @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
     def test_undeploy(self, dispatch_script_mock):
@@ -195,7 +229,7 @@ class NodeContextTestCase(unittest.TestCase):
             'type': 'script'
         }
         obj.undeploy()
-        self.assertTrue(dispatch_script_mock.called)
+        dispatch_script_mock.assert_called_once()
 
     @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
     def test_undeploy_anisble(self, dispatch_ansible_mock):
@@ -204,7 +238,7 @@ class NodeContextTestCase(unittest.TestCase):
             'type': 'ansible'
         }
         obj.undeploy()
-        self.assertTrue(dispatch_ansible_mock.called)
+        dispatch_ansible_mock.assert_called_once()
 
     @mock.patch('{}.ssh.SSH._put_file_shell'.format(PREFIX))
     @mock.patch('{}.ssh.SSH.execute'.format(PREFIX))
@@ -224,8 +258,8 @@ class NodeContextTestCase(unittest.TestCase):
         execute_mock.return_value = (0, '', '')
         obj._execute_remote_script('node5', info)
 
-        self.assertTrue(put_file_mock.called)
-        self.assertTrue(execute_mock.called)
+        put_file_mock.assert_called_once()
+        execute_mock.assert_called()
 
     @mock.patch('{}.NodeContext._execute_local_script'.format(PREFIX))
     def test_execute_script_local(self, local_execute_mock):
@@ -234,7 +268,7 @@ class NodeContextTestCase(unittest.TestCase):
         obj = node.NodeContext()
         self.addCleanup(obj._delete_context)
         obj._execute_script(node_name, info)
-        self.assertTrue(local_execute_mock.called)
+        local_execute_mock.assert_called_once()
 
     @mock.patch('{}.NodeContext._execute_remote_script'.format(PREFIX))
     def test_execute_script_remote(self, remote_execute_mock):
@@ -243,7 +277,7 @@ class NodeContextTestCase(unittest.TestCase):
         obj = node.NodeContext()
         self.addCleanup(obj._delete_context)
         obj._execute_script(node_name, info)
-        self.assertTrue(remote_execute_mock.called)
+        remote_execute_mock.assert_called_once()
 
     def test_get_script(self):
         script_args = 'hello.bash'
@@ -276,7 +310,7 @@ class NodeContextTestCase(unittest.TestCase):
             'pwd': 'ubuntu',
         }]
         obj._get_client(node_name_args)
-        self.assertTrue(wait_mock.called)
+        wait_mock.assert_called_once()
 
     def test_get_server(self):
         self.test_context.init(self.attrs)