diff options
Diffstat (limited to 'dovetail/tests')
-rw-r--r-- | dovetail/tests/unit/test_container.py | 51 | ||||
-rw-r--r-- | dovetail/tests/unit/test_report.py | 151 | ||||
-rw-r--r-- | dovetail/tests/unit/test_test_runner.py | 42 | ||||
-rw-r--r-- | dovetail/tests/unit/test_testcase.py | 2 |
4 files changed, 232 insertions, 14 deletions
diff --git a/dovetail/tests/unit/test_container.py b/dovetail/tests/unit/test_container.py index 25509a70..803566b1 100644 --- a/dovetail/tests/unit/test_container.py +++ b/dovetail/tests/unit/test_container.py @@ -126,14 +126,28 @@ class ContainerTesting(unittest.TestCase): self.assertEqual(expected, result) + @patch('dovetail.container.dt_cfg') @patch('dovetail.container.dt_utils') - def test_exec_cmd(self, mock_utils): + def test_exec_cmd(self, mock_utils, mock_config): expected = (0, 'success') mock_utils.exec_cmd.return_value = expected + mock_utils.get_value_from_dict.return_value = 'shell' + mock_config.dovetail_config = {'bottlenecks': 'value'} result = self.container.exec_cmd('command') mock_utils.exec_cmd.assert_called_once_with( - 'sudo docker exec None /bin/bash -c "command"', self.logger, False) + 'sudo docker exec None shell -c "command"', self.logger, False) + self.assertEqual(expected, result) + + @patch('dovetail.container.dt_cfg') + @patch('dovetail.container.dt_utils') + def test_exec_cmd_no_shell(self, mock_utils, mock_config): + expected = (1, 'shell is empty') + mock_utils.exec_cmd.return_value = expected + mock_utils.get_value_from_dict.return_value = None + mock_config.dovetail_config = {'bottlenecks': 'value'} + result = self.container.exec_cmd('command') + self.assertEqual(expected, result) @patch('dovetail.container.dt_cfg') @@ -455,7 +469,7 @@ class ContainerTesting(unittest.TestCase): docker_image = 'docker_image' container_id = 'container_id' mock_utils.get_value_from_dict.side_effect = [ - 'opts', 'envs', ['volume_one', 'volume_two']] + 'opts', 'shell', 'envs', ['volume_one', 'volume_two']] mock_utils.get_hosts_info.return_value = 'host_info' mock_utils.exec_cmd.return_value = (0, container_id) project_config = {} @@ -466,21 +480,37 @@ class ContainerTesting(unittest.TestCase): mock_utils.get_value_from_dict.assert_has_calls([ call('opts', project_config), + call('shell', project_config), call('envs', project_config), call('volumes', project_config)]) mock_utils.get_hosts_info.assert_called_once_with(self.logger) mock_utils.exec_cmd.assert_called_once_with( 'sudo docker run opts envs volume_one volume_two host_info ' - 'docker_image /bin/bash', self.logger) + 'docker_image shell', self.logger) self.assertEquals(expected, result) @patch('dovetail.container.dt_utils') @patch('dovetail.container.dt_cfg') + def test_create_no_shell(self, mock_config, mock_utils): + docker_image = 'docker_image' + mock_config.dovetail_config = {'bottlenecks': 'value'} + mock_utils.get_value_from_dict.side_effect = ['opts', None] + mock_utils.get_hosts_info.return_value = 'host_info' + + result = self.container.create(docker_image) + + mock_utils.get_value_from_dict.assert_has_calls([ + call('opts', 'value'), + call('shell', 'value')]) + self.assertEquals(None, result) + + @patch('dovetail.container.dt_utils') + @patch('dovetail.container.dt_cfg') @patch('dovetail.container.os.getenv') def test_create_error(self, mock_getenv, mock_config, mock_utils): docker_image = 'docker_image' mock_utils.get_value_from_dict.side_effect = [ - 'opts', 'envs', ['volume_one']] + 'opts', 'shell', 'envs', ['volume_one']] mock_getenv.side_effect = ['True', 'dovetail_home', None, 'True'] mock_utils.get_hosts_info.return_value = 'host_info' mock_utils.check_https_enabled.return_value = True @@ -491,12 +521,13 @@ class ContainerTesting(unittest.TestCase): mock_utils.get_value_from_dict.assert_has_calls([ call('opts', project_config), + call('shell', project_config), call('envs', project_config), call('volumes', project_config)]) mock_utils.get_hosts_info.assert_called_once_with(self.logger) mock_utils.exec_cmd.assert_called_once_with( 'sudo docker run opts envs volume_one host_info ' - 'docker_image /bin/bash', self.logger) + 'docker_image shell', self.logger) self.assertEquals(None, result) @patch('dovetail.container.dt_utils') @@ -509,7 +540,7 @@ class ContainerTesting(unittest.TestCase): docker_image = 'docker_image' container_id = 'container_id' mock_utils.get_value_from_dict.side_effect = [ - 'opts', 'envs', ['volume_one']] + 'opts', 'shell', 'envs', ['volume_one']] mock_getenv.side_effect = ['False', 'dovetail_home', 'cacert', 'True'] mock_setvnfconf.return_value = 'vnftest_config' mock_utils.get_hosts_info.return_value = 'host_info' @@ -524,6 +555,7 @@ class ContainerTesting(unittest.TestCase): mock_utils.get_value_from_dict.assert_has_calls([ call('opts', project_config), + call('shell', project_config), call('envs', project_config), call('volumes', project_config)]) mock_utils.get_hosts_info.assert_called_once_with(self.logger) @@ -531,7 +563,7 @@ class ContainerTesting(unittest.TestCase): mock_setvnffile.assert_called_once_with(container_id) mock_utils.exec_cmd.assert_called_once_with( 'sudo docker run opts envs volume_one vnftest_config host_info ' - 'docker_image /bin/bash', + 'docker_image shell', self.logger) self.assertEquals(expected, result) @@ -543,7 +575,7 @@ class ContainerTesting(unittest.TestCase): mock_getenv, mock_config, mock_utils): docker_image = 'docker_image' mock_utils.get_value_from_dict.side_effect = [ - 'opts', 'envs', ['volume_one']] + 'opts', 'shell', 'envs', ['volume_one']] mock_getenv.return_value = 'True' mock_setvnfconf.return_value = None mock_config.dovetail_config = { @@ -556,6 +588,7 @@ class ContainerTesting(unittest.TestCase): mock_utils.get_value_from_dict.assert_has_calls([ call('opts', 'value'), + call('shell', 'value'), call('envs', 'value'), call('volumes', 'value')]) mock_utils.get_hosts_info.assert_called_once_with(self.logger) diff --git a/dovetail/tests/unit/test_report.py b/dovetail/tests/unit/test_report.py index 9f5369ec..8cf2e025 100644 --- a/dovetail/tests/unit/test_report.py +++ b/dovetail/tests/unit/test_report.py @@ -8,6 +8,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 ## +import json import os import unittest import yaml @@ -35,16 +36,19 @@ class ReportTesting(unittest.TestCase): dt_report.BottlenecksCrawler.logger = None dt_report.VnftestCrawler.logger = None dt_report.OnapVtpCrawler.logger = None + dt_report.OnapVvpCrawler.logger = None dt_report.FunctestChecker.logger = None dt_report.FunctestK8sChecker.logger = None dt_report.YardstickChecker.logger = None dt_report.BottlenecksChecker.logger = None dt_report.VnftestChecker.logger = None dt_report.OnapVtpChecker.logger = None + dt_report.OnapVvpChecker.logger = None dt_report.Report.logger = None dt_report.Report.results = { 'functest': {}, 'yardstick': {}, 'functest-k8s': {}, - 'bottlenecks': {}, 'shell': {}, 'vnftest': {}, 'onap-vtp': {}} + 'bottlenecks': {}, 'shell': {}, 'vnftest': {}, 'onap-vtp': {}, + 'onap-vvp': {}} def _produce_report_initial_text(self, report_data): report_txt = '' @@ -960,6 +964,17 @@ class ReportTesting(unittest.TestCase): self.assertEquals(getlogger_obj, dt_report.OnapVtpCrawler.logger) + @patch('dovetail.report.dt_logger') + def test_onapvvp_crawler_create_log(self, mock_logger): + getlogger_obj = Mock() + logger_obj = Mock() + logger_obj.getLogger.return_value = getlogger_obj + mock_logger.Logger.return_value = logger_obj + + dt_report.OnapVvpCrawler.create_log() + + self.assertEquals(getlogger_obj, dt_report.OnapVvpCrawler.logger) + @patch('dovetail.report.os.path') def test_onapvtp_crawler_crawl_not_exists(self, mock_path): logger_obj = Mock() @@ -975,6 +990,113 @@ class ReportTesting(unittest.TestCase): 'Result file not found: {}'.format(file_path)) self.assertEquals(None, result) + @patch('dovetail.report.os.path') + def test_onapvvp_crawler_crawl_not_exists(self, mock_path): + logger_obj = Mock() + dt_report.OnapVvpCrawler.logger = logger_obj + mock_path.exists.return_value = False + file_path = 'file_path' + + crawler = dt_report.OnapVvpCrawler() + result = crawler.crawl(None, file_path) + + mock_path.exists.assert_called_once_with(file_path) + logger_obj.error.assert_called_once_with( + 'Result file not found: {}'.format(file_path)) + self.assertEquals(None, result) + + @patch('__builtin__.open') + @patch('dovetail.report.os.path') + def test_onapvvp_crawler_crawl_pass(self, mock_path, + mock_open): + dt_report.OnapVvpCrawler.logger = Mock() + mock_path.exists.return_value = True + file_path = 'file_path' + testcase_obj = Mock() + file_obj = Mock() + file_obj.read.return_value = json.dumps({'outcome': 'PASS'}) + mock_open.return_value.__enter__.return_value = file_obj + + crawler = dt_report.OnapVvpCrawler() + result = crawler.crawl(testcase_obj, file_path) + expected = {'criteria': 'PASS'} + + mock_path.exists.assert_called_once_with(file_path) + mock_open.assert_called_once_with(file_path, 'r') + file_obj.read.assert_called_once_with() + testcase_obj.set_results.assert_called_once_with(expected) + self.assertEquals(expected, result) + + @patch('__builtin__.open') + @patch('dovetail.report.os.path') + def test_onapvvp_crawler_crawl_fail(self, mock_path, + mock_open): + dt_report.OnapVvpCrawler.logger = Mock() + mock_path.exists.return_value = True + file_path = 'file_path' + testcase_obj = Mock() + file_obj = Mock() + file_obj.read.return_value = json.dumps({'outcome': 'FAIL'}) + mock_open.return_value.__enter__.return_value = file_obj + + crawler = dt_report.OnapVvpCrawler() + result = crawler.crawl(testcase_obj, file_path) + expected = {'criteria': 'FAIL'} + + mock_path.exists.assert_called_once_with(file_path) + mock_open.assert_called_once_with(file_path, 'r') + file_obj.read.assert_called_once_with() + testcase_obj.set_results.assert_called_once_with(expected) + self.assertEquals(expected, result) + + @patch('__builtin__.open') + @patch('dovetail.report.os.path') + def test_onapvvp_crawler_crawl_value_exception(self, mock_path, + mock_open): + dt_report.OnapVvpCrawler.logger = Mock() + mock_path.exists.return_value = True + file_path = 'file_path' + testcase_obj = Mock() + file_obj = Mock() + file_obj.read.return_value = 'error' + mock_open.return_value.__enter__.return_value = file_obj + + crawler = dt_report.OnapVvpCrawler() + result = crawler.crawl(testcase_obj, file_path) + expected = {'criteria': 'FAIL'} + + mock_path.exists.assert_called_once_with(file_path) + mock_open.assert_called_once_with(file_path, 'r') + file_obj.read.assert_called_once_with() + dt_report.OnapVvpCrawler.logger.exception.assert_called_once_with( + 'Result file has invalid format') + testcase_obj.set_results.assert_called_once_with(expected) + self.assertEquals(expected, result) + + @patch('__builtin__.open') + @patch('dovetail.report.os.path') + def test_onapvvp_crawler_crawl_key_exception(self, mock_path, + mock_open): + dt_report.OnapVvpCrawler.logger = Mock() + mock_path.exists.return_value = True + file_path = 'file_path' + testcase_obj = Mock() + file_obj = Mock() + file_obj.read.return_value = json.dumps({'key': 'value'}) + mock_open.return_value.__enter__.return_value = file_obj + + crawler = dt_report.OnapVvpCrawler() + result = crawler.crawl(testcase_obj, file_path) + expected = {'criteria': 'FAIL'} + + mock_path.exists.assert_called_once_with(file_path) + mock_open.assert_called_once_with(file_path, 'r') + file_obj.read.assert_called_once_with() + dt_report.OnapVvpCrawler.logger.exception.assert_called_once_with( + "Outcome field not found 'outcome'") + testcase_obj.set_results.assert_called_once_with(expected) + self.assertEquals(expected, result) + @patch('__builtin__.open') @patch('dovetail.report.json.loads') @patch('dovetail.report.os.path') @@ -1359,3 +1481,30 @@ class ReportTesting(unittest.TestCase): dt_report.OnapVtpChecker.check(testcase_obj, result) testcase_obj.passed.assert_called_once_with('PASS') + + @patch('dovetail.report.dt_logger') + def test_onapvvp_checker_create_log(self, mock_logger): + getlogger_obj = Mock() + logger_obj = Mock() + logger_obj.getLogger.return_value = getlogger_obj + mock_logger.Logger.return_value = logger_obj + + dt_report.OnapVvpChecker.create_log() + + self.assertEquals(getlogger_obj, dt_report.OnapVvpChecker.logger) + + def test_onapvvp_check_result_none(self): + testcase_obj = Mock() + result = {} + + dt_report.OnapVvpChecker.check(testcase_obj, result) + + testcase_obj.passed.assert_called_once_with('FAIL') + + def test_onapvvp_check_result(self): + testcase_obj = Mock() + result = {'criteria': 'PASS'} + + dt_report.OnapVvpChecker.check(testcase_obj, result) + + testcase_obj.passed.assert_called_once_with('PASS') diff --git a/dovetail/tests/unit/test_test_runner.py b/dovetail/tests/unit/test_test_runner.py index 4b5c00bb..0eb12135 100644 --- a/dovetail/tests/unit/test_test_runner.py +++ b/dovetail/tests/unit/test_test_runner.py @@ -323,7 +323,8 @@ class TestRunnerTesting(unittest.TestCase): @patch('dovetail.test_runner.os') def test_add_testcase_info(self, mock_os, mock_config): mock_os.getenv.side_effect = ['os_insecure', 'dovetail_home', 'debug', - 'os_cacert', 'host_url', 'csar_file'] + 'os_cacert', 'host_url', 'csar_file', + 'heat_templates_dir'] mock_os.environ = {'DEPLOY_SCENARIO': 'deploy_scenario'} mock_config.dovetail_config = {'build_tag': 'build_tag'} @@ -333,14 +334,17 @@ class TestRunnerTesting(unittest.TestCase): 'deploy_scenario': 'deploy_scenario', 'dovetail_home': 'dovetail_home', 'debug': 'debug', 'build_tag': 'build_tag', 'cacert': 'os_cacert', - 'host_url': 'host_url', 'csar_file': 'csar_file'} + 'host_url': 'host_url', 'csar_file': 'csar_file', + 'heat_templates_dir': 'heat_templates_dir' + } result = t_runner.FunctestRunner._add_testcase_info(self.testcase) self.testcase.validate_testcase.assert_called_once_with() self.testcase.name.assert_called_once_with() mock_os.getenv.assert_has_calls([ call('OS_INSECURE'), call('DOVETAIL_HOME'), call('DEBUG'), - call('OS_CACERT')]) + call('OS_CACERT'), call('HOST_URL'), call('CSAR_FILE'), + call('VNF_DIRECTORY')]) self.assertEquals(expected, result) @patch('dovetail.test_runner.dt_utils') @@ -672,3 +676,35 @@ class TestRunnerTesting(unittest.TestCase): mock_path.join.assert_has_calls([call('one', 'two')]) mock_path.isfile.assert_called_once() mock_utils.source_env.assert_called_once_with('env_file') + + @patch('dovetail.test_runner.dt_utils') + @patch('dovetail.test_runner.os.path') + @patch('dovetail.test_runner.dt_cfg') + def test_init_onapvvprunner_no_env_file(self, mock_config, mock_path, + mock_utils): + t_runner.OnapVvpRunner.create_log() + mock_path.join.side_effect = ['env_file'] + mock_config.dovetail_config = {'config_dir': 'one', 'env_file': 'two'} + mock_path.isfile.return_value = False + + docker_runner = t_runner.OnapVvpRunner(self.testcase) + + mock_path.join.assert_has_calls([call('one', 'two')]) + mock_path.isfile.assert_called_once() + docker_runner.logger.error.assert_called_once_with( + 'File env_file does not exist.') + + @patch('dovetail.test_runner.dt_utils') + @patch('dovetail.test_runner.os.path') + @patch('dovetail.test_runner.dt_cfg') + def test_init_onapvvprunner(self, mock_config, mock_path, mock_utils): + t_runner.OnapVvpRunner.create_log() + mock_path.join.side_effect = ['env_file'] + mock_config.dovetail_config = {'config_dir': 'one', 'env_file': 'two'} + mock_path.isfile.return_value = True + + t_runner.OnapVvpRunner(self.testcase) + + mock_path.join.assert_has_calls([call('one', 'two')]) + mock_path.isfile.assert_called_once() + mock_utils.source_env.assert_called_once_with('env_file') diff --git a/dovetail/tests/unit/test_testcase.py b/dovetail/tests/unit/test_testcase.py index 7224c1ae..b224a130 100644 --- a/dovetail/tests/unit/test_testcase.py +++ b/dovetail/tests/unit/test_testcase.py @@ -120,7 +120,7 @@ class TestcaseTesting(unittest.TestCase): self.assertEquals(testcase.testcase, result) def test_objective(self): - testcase = tcase.Testcase(self.testcase_yaml) + testcase = tcase.OnapVvpTestcase(self.testcase_yaml) testcase.testcase['objective'] = 'objective' result = testcase.objective() |