From ea90f9ba9d0e317dfc5521dda9946844b7336abd Mon Sep 17 00:00:00 2001 From: Moshe Date: Sun, 25 Mar 2018 17:00:40 +0300 Subject: Add unit tests and handle coverage Issue-ID: VNFSDK-183 Change-Id: I228576f5c06b371164bcd653d23cf169098436a8 Signed-off-by: Moshe handle coverage Issue-ID: VNFSDK-183 Change-Id: I3d78293fdf5cb8bc57caa4078ab8fa5baa429a0d Signed-off-by: Moshe --- vnftest/tests/unit/core/__init__.py | 0 .../core/no_constraint_no_args_step_sample.yaml | 28 ++++ .../core/no_constraint_with_args_step_sample.yaml | 30 +++++ vnftest/tests/unit/core/test_plugin.py | 149 ++++++++++++++++++++ vnftest/tests/unit/core/test_report.py | 59 ++++++++ vnftest/tests/unit/core/test_task.py | 150 +++++++++++++++++++++ vnftest/tests/unit/core/test_testcase.py | 40 ++++++ .../core/with_constraint_no_args_step_sample.yaml | 31 +++++ .../with_constraint_with_args_step_sample.yaml | 33 +++++ 9 files changed, 520 insertions(+) create mode 100644 vnftest/tests/unit/core/__init__.py create mode 100644 vnftest/tests/unit/core/no_constraint_no_args_step_sample.yaml create mode 100644 vnftest/tests/unit/core/no_constraint_with_args_step_sample.yaml create mode 100644 vnftest/tests/unit/core/test_plugin.py create mode 100644 vnftest/tests/unit/core/test_report.py create mode 100644 vnftest/tests/unit/core/test_task.py create mode 100644 vnftest/tests/unit/core/test_testcase.py create mode 100644 vnftest/tests/unit/core/with_constraint_no_args_step_sample.yaml create mode 100644 vnftest/tests/unit/core/with_constraint_with_args_step_sample.yaml (limited to 'vnftest/tests/unit/core') diff --git a/vnftest/tests/unit/core/__init__.py b/vnftest/tests/unit/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vnftest/tests/unit/core/no_constraint_no_args_step_sample.yaml b/vnftest/tests/unit/core/no_constraint_no_args_step_sample.yaml new file mode 100644 index 0000000..4272d70 --- /dev/null +++ b/vnftest/tests/unit/core/no_constraint_no_args_step_sample.yaml @@ -0,0 +1,28 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/no_constraint_no_args_scenario_sample.yaml + +--- + +schema: "vnftest:suite:0.1" + +name: "suite_1" +test_cases_dir: "tests/onap/test_cases/" +test_cases: +- + file_name: onap_vnftest_tc001.yaml +- + file_name: onap_vnftest_tc002.yaml + diff --git a/vnftest/tests/unit/core/no_constraint_with_args_step_sample.yaml b/vnftest/tests/unit/core/no_constraint_with_args_step_sample.yaml new file mode 100644 index 0000000..3667230 --- /dev/null +++ b/vnftest/tests/unit/core/no_constraint_with_args_step_sample.yaml @@ -0,0 +1,30 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/no_constraint_with_args_scenario_sample.yaml + +--- + +schema: "vnftest:suite:0.1" + +name: "suite_1" +test_cases_dir: "tests/onap/test_cases/" +test_cases: +- + file_name: onap_vnftest_tc001.yaml +- + file_name: onap_vnftest_tc002.yaml + task_args: + huawei-pod1: '{"host": "node1.LF","target": "node2.LF"}' + diff --git a/vnftest/tests/unit/core/test_plugin.py b/vnftest/tests/unit/core/test_plugin.py new file mode 100644 index 0000000..6f1bff8 --- /dev/null +++ b/vnftest/tests/unit/core/test_plugin.py @@ -0,0 +1,149 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/test_plugin.py + +import copy +import os +import pkg_resources + +import mock +import testtools + +from vnftest import ssh +from vnftest.core import plugin +from vnftest.tests import fixture + + +class PluginTestCase(testtools.TestCase): + + FILE = """ +schema: "vnftest:plugin:0.1" + +plugins: + name: sample + +deployment: + ip: 10.1.0.50 + user: root + password: root +""" + + NAME = 'sample' + DEPLOYMENT = {'ip': '10.1.0.50', 'user': 'root', 'password': 'root'} + + def setUp(self): + super(PluginTestCase, self).setUp() + self.plugin_parser = plugin.PluginParser(mock.Mock()) + self.plugin = plugin.Plugin() + self.useFixture(fixture.PluginParserFixture(PluginTestCase.FILE)) + + self._mock_ssh_from_node = mock.patch.object(ssh.SSH, 'from_node') + self.mock_ssh_from_node = self._mock_ssh_from_node.start() + self.mock_ssh_obj = mock.Mock() + self.mock_ssh_from_node.return_value = self.mock_ssh_obj + self.mock_ssh_obj.wait = mock.Mock() + self.mock_ssh_obj._put_file_shell = mock.Mock() + + self.addCleanup(self._cleanup) + + def _cleanup(self): + self._mock_ssh_from_node.stop() + + def test_install(self): + args = mock.Mock() + args.input_file = [mock.Mock()] + with mock.patch.object(self.plugin, '_install_setup') as \ + mock_install, \ + mock.patch.object(self.plugin, '_run') as mock_run: + self.plugin.install(args) + mock_install.assert_called_once_with(PluginTestCase.NAME, + PluginTestCase.DEPLOYMENT) + mock_run.assert_called_once_with(PluginTestCase.NAME) + + def test_remove(self): + args = mock.Mock() + args.input_file = [mock.Mock()] + with mock.patch.object(self.plugin, '_remove_setup') as \ + mock_remove, \ + mock.patch.object(self.plugin, '_run') as mock_run: + self.plugin.remove(args) + mock_remove.assert_called_once_with(PluginTestCase.NAME, + PluginTestCase.DEPLOYMENT) + mock_run.assert_called_once_with(PluginTestCase.NAME) + + @mock.patch.object(pkg_resources, 'resource_filename', + return_value='script') + def test__install_setup(self, mock_resource_filename): + plugin_name = 'plugin_name' + self.plugin._install_setup(plugin_name, PluginTestCase.DEPLOYMENT) + mock_resource_filename.assert_called_once_with( + 'vnftest.resources', 'scripts/install/' + plugin_name + '.bash') + self.mock_ssh_from_node.assert_called_once_with( + PluginTestCase.DEPLOYMENT) + self.mock_ssh_obj.wait.assert_called_once_with(timeout=600) + self.mock_ssh_obj._put_file_shell.assert_called_once_with( + 'script', '~/{0}.sh'.format(plugin_name)) + + @mock.patch.object(pkg_resources, 'resource_filename', + return_value='script') + @mock.patch.object(os, 'environ', return_value='1.2.3.4') + def test__install_setup_with_ip_local(self, mock_os_environ, + mock_resource_filename): + plugin_name = 'plugin_name' + deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT) + deployment['ip'] = 'local' + self.plugin._install_setup(plugin_name, deployment) + mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP') + mock_resource_filename.assert_called_once_with( + 'vnftest.resources', + 'scripts/install/' + plugin_name + '.bash') + self.mock_ssh_from_node.assert_called_once_with( + deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]}) + self.mock_ssh_obj.wait.assert_called_once_with(timeout=600) + self.mock_ssh_obj._put_file_shell.assert_called_once_with( + 'script', '~/{0}.sh'.format(plugin_name)) + + @mock.patch.object(pkg_resources, 'resource_filename', + return_value='script') + def test__remove_setup(self, mock_resource_filename): + plugin_name = 'plugin_name' + self.plugin._remove_setup(plugin_name, PluginTestCase.DEPLOYMENT) + mock_resource_filename.assert_called_once_with( + 'vnftest.resources', + 'scripts/remove/' + plugin_name + '.bash') + self.mock_ssh_from_node.assert_called_once_with( + PluginTestCase.DEPLOYMENT) + self.mock_ssh_obj.wait.assert_called_once_with(timeout=600) + self.mock_ssh_obj._put_file_shell.assert_called_once_with( + 'script', '~/{0}.sh'.format(plugin_name)) + + @mock.patch.object(pkg_resources, 'resource_filename', + return_value='script') + @mock.patch.object(os, 'environ', return_value='1.2.3.4') + def test__remove_setup_with_ip_local(self, mock_os_environ, + mock_resource_filename): + plugin_name = 'plugin_name' + deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT) + deployment['ip'] = 'local' + self.plugin._remove_setup(plugin_name, deployment) + mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP') + mock_resource_filename.assert_called_once_with( + 'vnftest.resources', + 'scripts/remove/' + plugin_name + '.bash') + self.mock_ssh_from_node.assert_called_once_with( + deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]}) + self.mock_ssh_obj.wait.assert_called_once_with(timeout=600) + self.mock_ssh_obj._put_file_shell.mock_os_environ( + 'script', '~/{0}.sh'.format(plugin_name)) diff --git a/vnftest/tests/unit/core/test_report.py b/vnftest/tests/unit/core/test_report.py new file mode 100644 index 0000000..0bb17e3 --- /dev/null +++ b/vnftest/tests/unit/core/test_report.py @@ -0,0 +1,59 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/test_report.py + +from __future__ import print_function + +from __future__ import absolute_import + +import unittest +import uuid + +try: + from unittest import mock +except ImportError: + import mock + +from vnftest.core import report +from vnftest.cmd.commands import change_osloobj_to_paras + +FAKE_YAML_NAME = 'fake_name' +FAKE_TASK_ID = str(uuid.uuid4()) +DUMMY_TASK_ID = 'aaaaaa-aaaaaaaa-aaaaaaaaaa-aaaaaa' + + +class ReportTestCase(unittest.TestCase): + + def setUp(self): + super(ReportTestCase, self).setUp() + self.param = change_osloobj_to_paras({}) + self.param.yaml_name = [FAKE_YAML_NAME] + self.param.task_id = [FAKE_TASK_ID] + self.rep = report.Report() + + @mock.patch('vnftest.core.report.Report._validate') + def test_generate_success(self, mock_valid): + self.rep.generate(self.param) + mock_valid.assert_called_once_with(FAKE_YAML_NAME, FAKE_TASK_ID) + + # pylint: disable=deprecated-method + def test_invalid_yaml_name(self): + self.assertRaisesRegexp(ValueError, "yaml*", self.rep._validate, + 'F@KE_NAME', FAKE_TASK_ID) + + # pylint: disable=deprecated-method + def test_invalid_task_id(self): + self.assertRaisesRegexp(ValueError, "task*", self.rep._validate, + FAKE_YAML_NAME, DUMMY_TASK_ID) diff --git a/vnftest/tests/unit/core/test_task.py b/vnftest/tests/unit/core/test_task.py new file mode 100644 index 0000000..b136960 --- /dev/null +++ b/vnftest/tests/unit/core/test_task.py @@ -0,0 +1,150 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/test_task.py + +import unittest + +import mock +import os + +from vnftest.common import constants as consts +from vnftest.core import task + + +class TaskTestCase(unittest.TestCase): + + def test_set_dispatchers(self): + t = task.Task() + output_config = {"DEFAULT": {"dispatcher": "file, http"}} + t._set_dispatchers(output_config) + self.assertEqual(output_config, output_config) + + @mock.patch.object(task, 'DispatcherBase') + def test__do_output(self, mock_dispatcher): + t = task.Task() + output_config = {"DEFAULT": {"dispatcher": "file, http"}} + + dispatcher1 = mock.MagicMock() + dispatcher1.__dispatcher_type__ = 'file' + + dispatcher2 = mock.MagicMock() + dispatcher2.__dispatcher_type__ = 'http' + + mock_dispatcher.get = mock.MagicMock(return_value=[dispatcher1, + dispatcher2]) + self.assertIsNone(t._do_output(output_config, {})) + + @mock.patch.object(task, 'Context') + @mock.patch.object(task, 'base_runner') + def test_run(self, mock_base_runner, *args): + step = { + 'runner': { + 'duration': 60, + 'interval': 1, + 'type': 'Duration' + }, + 'type': 'Dummy' + } + + t = task.Task() + runner = mock.Mock() + runner.join.return_value = 0 + runner.get_output.return_value = {} + runner.get_result.return_value = [] + mock_base_runner.Runner.get.return_value = runner + t._run([step], False, "vnftest.out") + self.assertTrue(runner.run.called) + + def test_parse_suite_no_constraint_no_args(self): + SAMPLE_step_PATH = "no_constraint_no_args_step_sample.yaml" + t = task.TaskParser(self._get_file_abspath(SAMPLE_step_PATH)) + with mock.patch.object(os, 'environ', + new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}): + task_files, task_args, task_args_fnames = t.parse_suite() + + self.assertEqual(task_files[0], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc001.yaml')) + self.assertEqual(task_files[1], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc002.yaml')) + self.assertIsNone(task_args[0]) + self.assertIsNone(task_args[1]) + self.assertIsNone(task_args_fnames[0]) + self.assertIsNone(task_args_fnames[1]) + + def test_parse_suite_no_constraint_with_args(self): + SAMPLE_step_PATH = "no_constraint_with_args_step_sample.yaml" + t = task.TaskParser(self._get_file_abspath(SAMPLE_step_PATH)) + with mock.patch.object(os, 'environ', + new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}): + task_files, task_args, task_args_fnames = t.parse_suite() + + self.assertEqual(task_files[0], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc001.yaml')) + self.assertEqual(task_files[1], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc002.yaml')) + self.assertIsNone(task_args[0]) + self.assertEqual(task_args[1], + '{"host": "node1.LF","target": "node2.LF"}') + self.assertIsNone(task_args_fnames[0]) + self.assertIsNone(task_args_fnames[1]) + + def test_parse_suite_with_constraint_no_args(self): + SAMPLE_step_PATH = "with_constraint_no_args_step_sample.yaml" + t = task.TaskParser(self._get_file_abspath(SAMPLE_step_PATH)) + with mock.patch.object(os, 'environ', + new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}): + task_files, task_args, task_args_fnames = t.parse_suite() + self.assertEqual(task_files[0], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc001.yaml')) + self.assertEqual(task_files[1], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc002.yaml')) + self.assertIsNone(task_args[0]) + self.assertIsNone(task_args[1]) + self.assertIsNone(task_args_fnames[0]) + self.assertIsNone(task_args_fnames[1]) + + def test_parse_suite_with_constraint_with_args(self): + SAMPLE_step_PATH = "with_constraint_with_args_step_sample.yaml" + t = task.TaskParser(self._get_file_abspath(SAMPLE_step_PATH)) + with mock.patch('os.environ', + new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}): + task_files, task_args, task_args_fnames = t.parse_suite() + + self.assertEqual(task_files[0], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc001.yaml')) + self.assertEqual(task_files[1], self.change_to_abspath( + 'tests/onap/test_cases/onap_vnftest_tc002.yaml')) + self.assertIsNone(task_args[0]) + self.assertEqual(task_args[1], + '{"host": "node1.LF","target": "node2.LF"}') + self.assertIsNone(task_args_fnames[0]) + self.assertIsNone(task_args_fnames[1]) + + @mock.patch('six.moves.builtins.open', side_effect=mock.mock_open()) + @mock.patch.object(task, 'utils') + @mock.patch('logging.root') + def test_set_log(self, mock_logging_root, *args): + task_obj = task.Task() + task_obj.task_id = 'task_id' + task_obj._set_log() + mock_logging_root.addHandler.assert_called() + + def _get_file_abspath(self, filename): + curr_path = os.path.dirname(os.path.abspath(__file__)) + file_path = os.path.join(curr_path, filename) + return file_path + + def change_to_abspath(self, filepath): + return os.path.join(consts.VNFTEST_ROOT_PATH, filepath) diff --git a/vnftest/tests/unit/core/test_testcase.py b/vnftest/tests/unit/core/test_testcase.py new file mode 100644 index 0000000..0ba4873 --- /dev/null +++ b/vnftest/tests/unit/core/test_testcase.py @@ -0,0 +1,40 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/test_testcase.py + +from __future__ import absolute_import +import unittest + +from vnftest.core import testcase + + +class Arg(object): + + def __init__(self): + self.casename = ('onap_vnftest_tc001',) + + +class TestcaseUT(unittest.TestCase): + + def test_list_all(self): + t = testcase.Testcase() + result = t.list_all("") + self.assertIsInstance(result, list) + + def test_show(self): + t = testcase.Testcase() + casename = Arg() + result = t.show(casename) + self.assertTrue(result) diff --git a/vnftest/tests/unit/core/with_constraint_no_args_step_sample.yaml b/vnftest/tests/unit/core/with_constraint_no_args_step_sample.yaml new file mode 100644 index 0000000..f9524fb --- /dev/null +++ b/vnftest/tests/unit/core/with_constraint_no_args_step_sample.yaml @@ -0,0 +1,31 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/with_constraint_no_args_scenario_sample.yaml + +--- + +schema: "vnftest:suite:0.1" + +name: "suite_1" +test_cases_dir: "tests/onap/test_cases/" +test_cases: +- + file_name: onap_vnftest_tc001.yaml +- + file_name: onap_vnftest_tc002.yaml + constraint: + installer: compass + pod: huawei-pod1 + diff --git a/vnftest/tests/unit/core/with_constraint_with_args_step_sample.yaml b/vnftest/tests/unit/core/with_constraint_with_args_step_sample.yaml new file mode 100644 index 0000000..53c8390 --- /dev/null +++ b/vnftest/tests/unit/core/with_constraint_with_args_step_sample.yaml @@ -0,0 +1,33 @@ +############################################################################## +# Copyright 2018 EuropeanSoftwareMarketingLtd. +# =================================================================== +# Licensed under the ApacheLicense, Version2.0 (the"License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under +# the License +############################################################################## +# vnftest comment: this is a modified copy of +# yardstick/tests/unit/benchmark/core/with_constraint_with_args_scenario_sample.yaml + +--- + +schema: "vnftest:suite:0.1" + +name: "suite_1" +test_cases_dir: "tests/onap/test_cases/" +test_cases: +- + file_name: onap_vnftest_tc001.yaml +- + file_name: onap_vnftest_tc002.yaml + constraint: + installer: compass + pod: huawei-pod1 + task_args: + huawei-pod1: '{"host": "node1.LF","target": "node2.LF"}' + -- cgit 1.2.3-korg