class ScheduleServiceTestCase(TestCase):
    def assert_schedule_calls(self, expected_job_time_pairs):
        self.assertEqual(len(expected_job_time_pairs), len(self.scheduler_mock.enterabs.call_args_list))

        for i, pair in enumerate(expected_job_time_pairs):
            expected_time = date_utils.sec_to_datetime(pair[1])
            expected_job = pair[0]

            # the first item of call_args is actual arguments, passed to the method
            args = self.scheduler_mock.enterabs.call_args_list[i][0]

            # we schedule job as enterabs(expected_time, priority, self._execute_job, (job,))
            # to get the job, we need to get the last arg, and extract the first parameter from it
            schedule_method_args_tuple = args[3]
            schedule_method_job_arg = schedule_method_args_tuple[0]
            actual_time = date_utils.sec_to_datetime(args[0])

            self.assertEqual(expected_time, actual_time)
            self.assertDictEqual(expected_job.as_serializable_dict(),
                                 schedule_method_job_arg.as_serializable_dict())

    def mock_schedule_model_with_secure_param(self):
        model = test_utils.create_config_model('some-name', parameters=[{'name': 'p1', 'secure': True}])
        model.schedulable = True

        self.config_service.load_config_model.side_effect = lambda a, b, c: model

    def setUp(self) -> None:
        super().setUp()

        self.patcher = patch('sched.scheduler')
        self.scheduler_mock = MagicMock()
        self.patcher.start().return_value = self.scheduler_mock

        schedule_service._sleep = MagicMock()
        schedule_service._sleep.side_effect = lambda x: time.sleep(0.001)

        self.unschedulable_scripts = set()
        self.config_service = MagicMock()
        self.config_service.load_config_model.side_effect = lambda name, b, c: test_utils.create_config_model(
            name,
            schedulable=name not in self.unschedulable_scripts)

        self.execution_service = MagicMock()

        self.schedule_service = ScheduleService(self.config_service, self.execution_service, test_utils.temp_folder)

        date_utils._mocked_now = mocked_now

        test_utils.setup()

    def tearDown(self) -> None:
        super().tearDown()

        test_utils.cleanup()

        date_utils._mocked_now = None

        self.schedule_service._stop()
        self.schedule_service.scheduling_thread.join()

        schedule_service._sleep = time.sleep

        self.patcher.stop()
Exemplo n.º 2
0
class ScheduleServiceTestCase(TestCase):
    def assert_schedule_calls(self, expected_job_time_pairs):
        self.assertEqual(len(expected_job_time_pairs),
                         len(self.scheduler_mock.enterabs.call_args_list))

        for i, pair in enumerate(expected_job_time_pairs):
            expected_time = date_utils.sec_to_datetime(pair[1])
            expected_job = pair[0]

            # the first item of call_args is actual arguments, passed to the method
            args = self.scheduler_mock.enterabs.call_args_list[i][0]

            # we schedule job as enterabs(expected_time, priority, self._execute_job, (job,))
            # to get the job, we need to get the last arg, and extract the first parameter from it
            schedule_method_args_tuple = args[3]
            schedule_method_job_arg = schedule_method_args_tuple[0]
            actual_time = date_utils.sec_to_datetime(args[0])

            self.assertEqual(expected_time, actual_time)
            self.assertDictEqual(
                expected_job.as_serializable_dict(),
                schedule_method_job_arg.as_serializable_dict())

    def mock_schedule_model_with_secure_param(self):
        self.create_config('secure-config',
                           parameters=[
                               {
                                   'name': 'p1',
                                   'secure': True
                               },
                               {
                                   'name': 'param_2',
                                   'type': 'multiselect',
                                   'values': ['hello', 'world', '1', '2', '3']
                               },
                           ])

    def setUp(self) -> None:
        super().setUp()
        test_utils.setup()

        self.patcher = patch('sched.scheduler')
        self.scheduler_mock = MagicMock()
        self.patcher.start().return_value = self.scheduler_mock

        schedule_service._sleep = MagicMock()
        schedule_service._sleep.side_effect = lambda x: time.sleep(0.001)

        self.config_service = ConfigService(AnyUserAuthorizer(),
                                            test_utils.temp_folder)

        self.create_config('my_script_A')
        self.create_config('unschedulable-script', scheduling_enabled=False)

        self.execution_service = MagicMock()

        self.schedule_service = ScheduleService(self.config_service,
                                                self.execution_service,
                                                test_utils.temp_folder)

        date_utils._mocked_now = mocked_now

    def create_config(self, name, scheduling_enabled=True, parameters=None):
        if parameters is None:
            parameters = [
                {
                    'name': 'p1'
                },
                {
                    'name': 'param_2',
                    'type': 'multiselect',
                    'values': ['hello', 'world', '1', '2', '3']
                },
            ]

        test_utils.write_script_config(
            {
                'name': name,
                'script_path': 'echo 1',
                'parameters': parameters,
                'scheduling': {
                    'enabled': scheduling_enabled
                }
            }, name)

    def tearDown(self) -> None:
        super().tearDown()

        test_utils.cleanup()

        date_utils._mocked_now = None

        self.schedule_service._stop()
        self.schedule_service.scheduling_thread.join()

        schedule_service._sleep = time.sleep

        self.patcher.stop()