From 58857a90b434081e73737464204d7c2868ac1e80 Mon Sep 17 00:00:00 2001 From: Aishwarya Mathuria Date: Mon, 14 Jun 2021 11:49:08 +0530 Subject: [PATCH] Add new test cases for dispatcher and scheduler Signed-off-by: Aishwarya Mathuria --- teuthology/test/test_dispatcher.py | 107 +++++++++++++++++++++++++++++ teuthology/test/test_schedule.py | 42 ++++++++++- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 teuthology/test/test_dispatcher.py diff --git a/teuthology/test/test_dispatcher.py b/teuthology/test/test_dispatcher.py new file mode 100644 index 0000000000..6b0dddfe2f --- /dev/null +++ b/teuthology/test/test_dispatcher.py @@ -0,0 +1,107 @@ +from teuthology import dispatcher +from unittest.mock import patch, Mock +from teuthology import report + +import unittest.mock as mock +import unittest + + +class TestDispatcher(unittest.TestCase): + + def test_mock_get_queue_job(self): + mock_get_patcher = patch('teuthology.dispatcher.report.get_queued_job') + machine_type = 'test_queue' + job_config = { + 'job_id': '1', + 'description': 'DESC', + 'email': 'EMAIL', + 'first_in_suite': False, + 'last_in_suite': True, + 'machine_type': 'test_queue', + 'name': 'NAME', + 'owner': 'OWNER', + 'priority': 99, + 'results_timeout': '6', + 'verbose': False, + } + + mock_get = mock_get_patcher.start() + mock_get.return_value = Mock(status_code = 200) + mock_get.return_value.json.return_value = job_config + + response = report.get_queued_job(machine_type) + + mock_get_patcher.stop() + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), job_config) + + + @patch("teuthology.worker.fetch_teuthology") + @patch("teuthology.dispatcher.fetch_qa_suite") + @patch("teuthology.worker.fetch_qa_suite") + @patch("teuthology.dispatcher.report.get_queued_job") + @patch("teuthology.dispatcher.report.try_push_job_info") + @patch("teuthology.dispatcher.setup_log_file") + @patch("os.path.isdir") + @patch("os.getpid") + @patch("teuthology.dispatcher.teuth_config") + @patch("subprocess.Popen") + @patch("os.path.join") + @patch("teuthology.dispatcher.create_job_archive") + @patch("yaml.safe_dump") + def test_dispatcher_main(self, m_fetch_teuthology, m_fetch_qa_suite, + m_worker_fetch_qa_suite, m_get_queued_job, + m_try_push_job_info, + m_setup_log, + m_isdir, m_getpid, + m_t_config, m_popen, m_join, m_create_archive, m_yaml_dump): + + args = { + '--owner': 'the_owner', + '--archive-dir': '/archive/dir', + '--log-dir': '/worker/log', + '--name': 'the_name', + '--description': 'the_description', + '--machine-type': 'test_queue', + '--supervisor': False, + '--verbose': False + } + + m = mock.MagicMock() + job_id = {'job_id': '1'} + m.__getitem__.side_effect = job_id.__getitem__ + m.__iter__.side_effect = job_id.__iter__ + job = { + 'job_id': '1', + 'description': 'DESC', + 'email': 'EMAIL', + 'first_in_suite': False, + 'last_in_suite': True, + 'machine_type': 'test_queue', + 'name': 'NAME', + 'owner': 'OWNER', + 'priority': 99, + 'results_timeout': '6', + 'verbose': False, + 'stop_worker': True, + 'archive_path': '/archive/dir/NAME/1' + } + + m_fetch_teuthology.return_value = '/teuth/path' + m_fetch_qa_suite.return_value = '/suite/path' + m_isdir.return_value = True + mock_get_patcher = patch('teuthology.dispatcher.report.get_queued_job') + mock_get = mock_get_patcher.start() + mock_get.return_value = job + + mock_prep_job_patcher = patch('teuthology.dispatcher.prep_job') + mock_prep_job = mock_prep_job_patcher.start() + mock_prep_job.return_value = (job, '/teuth/bin/path') + m_yaml_dump.return_value = '' + + m_try_push_job_info.called_once_with(job, dict(status='running')) + dispatcher.main(args) + mock_get_patcher.stop() + + diff --git a/teuthology/test/test_schedule.py b/teuthology/test/test_schedule.py index dd0a68f845..1add70620a 100644 --- a/teuthology/test/test_schedule.py +++ b/teuthology/test/test_schedule.py @@ -1,8 +1,14 @@ from teuthology.schedule import build_config from teuthology.misc import get_user +from unittest.mock import patch, Mock +from teuthology import report +from teuthology import schedule +import unittest +import os -class TestSchedule(object): + +class TestSchedule(unittest.TestCase): basic_args = { '--verbose': False, '--owner': 'OWNER', @@ -43,3 +49,37 @@ def test_owner(self): job_dict = build_config(self.basic_args) assert job_dict['owner'] == 'scheduled_%s' % get_user() + + def test_dump_job_to_file(self): + path = 'teuthology/test/job' + job_config = { + 'description': 'DESC', + 'email': 'EMAIL', + 'first_in_suite': False, + 'last_in_suite': True, + 'machine_type': 'tala', + 'name': 'NAME', + 'owner': 'OWNER', + 'priority': 99, + 'results_timeout': '6', + 'verbose': False, + 'tube': 'tala', + } + schedule.dump_job_to_file(path, job_config) + + count_file_path = path + '.count' + assert os.path.exists(count_file_path) == True + + + def test_mock_create_queue(self): + mock_get_patcher = patch('teuthology.schedule.report.create_machine_type_queue') + machine_type = 'test_queue' + + mock_get = mock_get_patcher.start() + mock_get.return_value = Mock(status_code = 200) + + response = report.create_machine_type_queue(machine_type) + + mock_get_patcher.stop() + + self.assertEqual(response.status_code, 200)