X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Ftests%2Funit%2Fbenchmark%2Fcontexts%2Ftest_node.py;h=da16074d9cc3a2e77df03acac388641e2b56766d;hb=caee6b2a603388c02e961582d78244a2e9e98372;hp=5329d30f41a0737ac23c7b8dca2047f64d226825;hpb=3333efb08ae1d4ee92ea5274f847aabfa95e3eec;p=yardstick.git diff --git a/yardstick/tests/unit/benchmark/contexts/test_node.py b/yardstick/tests/unit/benchmark/contexts/test_node.py index 5329d30f4..da16074d9 100644 --- a/yardstick/tests/unit/benchmark/contexts/test_node.py +++ b/yardstick/tests/unit/benchmark/contexts/test_node.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################## # Copyright (c) 2015-2017 Huawei Technologies Co.,Ltd and others. # @@ -9,20 +7,17 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -# Unittest for yardstick.benchmark.contexts.node - -from __future__ import absolute_import 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 - - -# pylint: disable=unused-argument -# disable this for now because I keep forgetting mock patch arg ordering +from yardstick.common import constants as consts +from yardstick.common import exceptions +from yardstick.common import yaml_loader class NodeContextTestCase(unittest.TestCase): @@ -34,7 +29,19 @@ class NodeContextTestCase(unittest.TestCase): def setUp(self): self.test_context = node.NodeContext() + self.addCleanup(self._remove_contexts) self.os_path_join = os.path.join + self.attrs = { + 'name': 'foo', + 'task_id': '1234567890', + 'file': self._get_file_abspath(self.NODES_SAMPLE) + } + + @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__)) @@ -42,7 +49,7 @@ class NodeContextTestCase(unittest.TestCase): return file_path def test___init__(self): - self.assertIsNone(self.test_context.name) + self.assertIsNone(self.test_context._name) self.assertIsNone(self.test_context.file_path) self.assertEqual(self.test_context.nodes, []) self.assertEqual(self.test_context.controllers, []) @@ -51,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") @@ -64,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): @@ -74,6 +81,7 @@ class NodeContextTestCase(unittest.TestCase): attrs = { 'name': 'foo', + 'task_id': '1234567890', 'file': error_path, } read_mock.side_effect = IOError(errno.EBUSY, 'busy') @@ -81,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)) @@ -96,38 +104,15 @@ 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): - - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } - - self.test_context.init(attrs) - - self.assertIsNotNone(self.test_context.read_config_file()) - def test__dispatch_script(self): - - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } - - self.test_context.init(attrs) + self.test_context.init(self.attrs) self.test_context.env = {'bash': [{'script': 'dummy'}]} self.test_context._execute_script = mock.Mock() self.assertEqual(self.test_context._dispatch_script('bash'), None) def test__dispatch_ansible(self): - - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } - - self.test_context.init(attrs) + self.test_context.init(self.attrs) self.test_context.env = {'ansible': [{'script': 'dummy'}]} self.test_context._do_ansible_job = mock.Mock() @@ -136,19 +121,13 @@ class NodeContextTestCase(unittest.TestCase): self.assertEqual(self.test_context._dispatch_ansible('ansible'), None) @mock.patch("{}.AnsibleCommon".format(PREFIX)) - def test__do_ansible_job(self, mock_ansible): - self.assertEqual(None, self.test_context._do_ansible_job('dummy')) + def test__do_ansible_job(self, *args): + self.assertIsNone(self.test_context._do_ansible_job('dummy')) - def test_successful_init(self): + def test_init(self): + self.test_context.init(self.attrs) - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } - - self.test_context.init(attrs) - - self.assertEqual(self.test_context.name, "foo") + self.assertEqual(self.test_context.name, "foo-12345678") self.assertEqual(len(self.test_context.nodes), 4) self.assertEqual(len(self.test_context.controllers), 2) self.assertEqual(len(self.test_context.computes), 1) @@ -156,95 +135,92 @@ class NodeContextTestCase(unittest.TestCase): self.assertEqual(len(self.test_context.baremetals), 1) self.assertEqual(self.test_context.baremetals[0]["name"], "node4") - def test__get_server_with_dic_attr_name(self): - - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } - - self.test_context.init(attrs) - - attr_name = {'name': 'foo.bar'} - result = self.test_context._get_server(attr_name) + def test__get_server_with_dict_attr_name(self): + self.test_context.init(self.attrs) + result = self.test_context._get_server({'name': 'node1.foo-12345678'}) - self.assertEqual(result, None) + self.assertIsNone(result, None) def test__get_server_not_found(self): + self.test_context.init(self.attrs) - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } + self.assertIsNone(self.test_context._get_server('bar.foo-12345678')) - self.test_context.init(attrs) + def test__get_server_mismatch(self): + self.test_context.init(self.attrs) - attr_name = 'bar.foo' - result = self.test_context._get_server(attr_name) + self.assertIsNone(self.test_context._get_server('bar.foo1')) - self.assertEqual(result, None) + def test__get_server_duplicate(self): + self.attrs['file'] = self._get_file_abspath( + self.NODES_DUPLICATE_SAMPLE) + self.test_context.init(self.attrs) - def test__get_server_mismatch(self): + with self.assertRaises(ValueError): + self.test_context._get_server('node1.foo-12345678') - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } + def test__get_server_found(self): + self.test_context.init(self.attrs) - self.test_context.init(attrs) + result = self.test_context._get_server('node1.foo-12345678') - attr_name = 'bar.foo1' - result = self.test_context._get_server(attr_name) + self.assertEqual(result['ip'], '10.229.47.137') + self.assertEqual(result['name'], 'node1.foo-12345678') + self.assertEqual(result['user'], 'root') + self.assertEqual(result['key_filename'], '/root/.yardstick_key') - self.assertEqual(result, None) + 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_server_duplicate(self): + def test__get_physical_node_for_server(self): + self.test_context.init(self.attrs) - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE) - } + # When server is not from this context + result = self.test_context._get_physical_node_for_server('node1.another-context') + self.assertIsNone(result) - self.test_context.init(attrs) + # When node_name is not from this context + result = self.test_context._get_physical_node_for_server('fake.foo-12345678') + self.assertIsNone(result) - attr_name = 'node1.foo' - with self.assertRaises(ValueError): - self.test_context._get_server(attr_name) + result = self.test_context._get_physical_node_for_server('node1.foo-12345678') + self.assertEqual(result, 'node1.foo') - def test__get_server_found(self): + def test_update_collectd_options_for_node(self): + self.test_context.init(self.attrs) + options = {'collectd': {'interval': 5}} - attrs = { - 'name': 'foo', - 'file': self._get_file_abspath(self.NODES_SAMPLE) - } + with self.assertRaises(exceptions.ContextUpdateCollectdForNodeError): + self.test_context.update_collectd_options_for_node(options, 'fake.foo-12345678') - self.test_context.init(attrs) + self.test_context.update_collectd_options_for_node(options, 'node1.foo-12345678') - attr_name = 'node1.foo' - result = self.test_context._get_server(attr_name) + node_collectd_options = [node for node in self.test_context.nodes + if node['name'] == 'node1'][0]['collectd'] - self.assertEqual(result['ip'], '10.229.47.137') - self.assertEqual(result['name'], 'node1.foo') - self.assertEqual(result['user'], 'root') - self.assertEqual(result['key_filename'], '/root/.yardstick_key') + self.assertEqual(node_collectd_options, options) @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX)) def test_deploy(self, dispatch_script_mock): obj = node.NodeContext() + self.addCleanup(obj._delete_context) obj.env = { '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): obj = node.NodeContext() + self.addCleanup(obj._delete_context) obj.env = { '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): @@ -253,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): @@ -262,12 +238,13 @@ 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)) def test_execute_remote_script(self, execute_mock, put_file_mock): obj = node.NodeContext() + self.addCleanup(obj._delete_context) obj.env = {'prefix': 'yardstick.benchmark.scenarios.compute'} node_name_args = 'node5' obj.nodes = [{ @@ -281,35 +258,42 @@ 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): node_name = 'local' info = {} - node.NodeContext()._execute_script(node_name, info) - self.assertTrue(local_execute_mock.called) + obj = node.NodeContext() + self.addCleanup(obj._delete_context) + obj._execute_script(node_name, info) + local_execute_mock.assert_called_once() @mock.patch('{}.NodeContext._execute_remote_script'.format(PREFIX)) def test_execute_script_remote(self, remote_execute_mock): node_name = 'node5' info = {} - node.NodeContext()._execute_script(node_name, info) - self.assertTrue(remote_execute_mock.called) + obj = node.NodeContext() + self.addCleanup(obj._delete_context) + obj._execute_script(node_name, info) + remote_execute_mock.assert_called_once() def test_get_script(self): script_args = 'hello.bash' info_args = { 'script': script_args } - script, options = node.NodeContext()._get_script(info_args) + obj = node.NodeContext() + self.addCleanup(obj._delete_context) + script, options = obj._get_script(info_args) self.assertEqual(script_args, script) self.assertEqual('', options) def test_node_info(self): node_name_args = 'node5' obj = node.NodeContext() + self.addCleanup(obj._delete_context) obj.nodes = [{'name': node_name_args, 'check': node_name_args}] node_info = obj._get_node_info(node_name_args) self.assertEqual(node_info.get('check'), node_name_args) @@ -318,6 +302,7 @@ class NodeContextTestCase(unittest.TestCase): def test_get_client(self, wait_mock): node_name_args = 'node5' obj = node.NodeContext() + self.addCleanup(obj._delete_context) obj.nodes = [{ 'name': node_name_args, 'user': 'ubuntu', @@ -325,29 +310,41 @@ 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.name = 'vnf1' - self.test_context.nodes = [{'name': 'my', 'value': 100}] + self.test_context.init(self.attrs) + self.test_context._name = 'foo' + self.test_context._task_id = '1234567890' + self.test_context._name_task_id = '{}-{}'.format( + self.test_context._name, self.test_context._task_id[:8]) + self.assertEqual('foo-12345678', self.test_context.name) + self.assertIsNotNone(self.test_context._task_id) - with self.assertRaises(ValueError): - self.test_context.get_server('my.vnf2') + result = self.test_context.get_server('node1.foo-12345678') - expected = {'name': 'my.vnf1', 'value': 100, 'interfaces': {}} - result = self.test_context.get_server('my.vnf1') - self.assertDictEqual(result, expected) + self.assertEqual(result['ip'], '10.229.47.137') + self.assertEqual(result['name'], 'node1.foo-12345678') + self.assertEqual(result['user'], 'root') + self.assertEqual(result['key_filename'], '/root/.yardstick_key') + + def test_get_server_server_not_in_context(self): + self.test_context.init(self.attrs) + + with self.assertRaises(ValueError): + self.test_context.get_server('my2.foo-12345678') def test_get_context_from_server(self): - self.test_context.name = 'vnf1' + self.test_context._name = 'vnf1' + self.test_context._task_id = '1234567890' + self.test_context._name_task_id = '{}-{}'.format( + self.test_context._name, self.test_context._task_id[:8]) self.test_context.nodes = [{'name': 'my', 'value': 100}] self.test_context.attrs = {'attr1': 200} - with self.assertRaises(ValueError): - self.test_context.get_context_from_server('my.vnf2') - - result = self.test_context.get_context_from_server('my.vnf1') - self.assertIs(result, self.test_context) + self.assertIs( + self.test_context.get_context_from_server('my.vnf1-12345678'), + self.test_context) # TODO: Split this into more granular tests def test__get_network(self): @@ -393,11 +390,3 @@ class NodeContextTestCase(unittest.TestCase): expected = network1 result = self.test_context._get_network(attr_name) self.assertDictEqual(result, expected) - - -def main(): - unittest.main() - - -if __name__ == '__main__': - main()