def maybe_disable_hooks(options): """Checks the hooks disable option, and disables the hooks if required. This could be done with a callback in the option, but this is better for the way that we test clientv1. """ if options.disable_all_hooks_reason is not None: GlobalHookRegistry.disable_hooks() log.info('Client hooks disabled; reason given by user: %s' % options.disable_all_hooks_reason)
def test_create_job_hook_aborts(self): """Run a test of the "create" command against a mocked API; verifies that a required hook runs, even though the config doesn't mention it. """ # Create a hook on "create_job" that just adds something to a list in the test. # Patch in HookedAuroraClientAPI to replace the UnhookedAuroraClientAPI with a mock. mock_options = self.setup_mock_options() hook = CreateHookForTesting(False) GlobalHookRegistry.register_global_hook(hook) # create first calls get_job_config, which calls get_config. As long as we've got the options # set up correctly, this should work. # Next, create gets an API object via make_client. We need to replace that with a mock API. (mock_api, mock_scheduler_proxy) = self.create_mock_api() with contextlib.nested( patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS), patch('apache.aurora.client.api.SchedulerProxy', return_value=mock_scheduler_proxy), patch('twitter.common.app.get_options', return_value=mock_options)): mock_scheduler_proxy.createJob.return_value = self.get_createjob_response( ) mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = [ self.create_mock_status_query_result(ScheduleStatus.INIT), self.create_mock_status_query_result(ScheduleStatus.RUNNING) ] # Finally, it calls the monitor to watch and make sure the jobs started; # but we already set that up in the side-effects list for the query mock. # This is the real test: invoke create as if it had been called by the command line. with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() self.assertRaises(HookedAuroraClientAPI.PreHooksStoppedCall, create, ['west/mchucarroll/test/hello', fp.name]) # Now check that the right API calls got made. assert mock_scheduler_proxy.createJob.call_count == 0 assert len(hook.created_jobs) == 1
def test_block_hooks(self): """Run a test of the "create" command against a mocked API; verifies that a required hook runs, even though the config doesn't mention it. """ # Create a hook on "create_job" that just adds something to a list in the test. # Patch in HookedAuroraClientAPI to replace the UnhookedAuroraClientAPI with a mock. mock_options = self.setup_mock_options() hook = CreateHookForTesting(True) GlobalHookRegistry.register_global_hook(hook) mock_options.disable_all_hooks_reason = "Because I said so." # create first calls get_job_config, which calls get_config. As long as we've got the options # set up correctly, this should work. # Next, create gets an API object via make_client. We need to replace that with a mock API. (mock_api, mock_scheduler_proxy) = self.create_mock_api() with contextlib.nested( patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS), patch('apache.aurora.client.api.SchedulerProxy', return_value = mock_scheduler_proxy), patch('twitter.common.app.get_options', return_value=mock_options)): mock_query = self.create_mock_query() mock_scheduler_proxy.createJob.return_value=self.get_createjob_response() mock_scheduler_proxy.getTasksStatus.side_effect = [ self.create_mock_status_query_result(ScheduleStatus.INIT), self.create_mock_status_query_result(ScheduleStatus.RUNNING) ] # Finally, it calls the monitor to watch and make sure the jobs started; # but we already set that up in the side-effects list for the query mock. # This is the real test: invoke create as if it had been called by the command line. with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() create(['west/mchucarroll/test/hello', fp.name]) # Now check that the right API calls got made. assert mock_scheduler_proxy.createJob.call_count == 1 assert len(hook.created_jobs) == 0
def _yield_hooks(cls, event, config, job_key, api_call, extra_argument=None): hooks = GlobalHookRegistry.get_hooks() hooks += (config.hooks if config and config.raw().enable_hooks().get() else []) for hook in hooks: yield cls._meta_hook( hook, cls._generate_method(hook, config, job_key, event, api_call, extra_argument))
def _yield_hooks(cls, event, config, job_key, api_call, extra_argument=None): hooks = GlobalHookRegistry.get_hooks() hooks += (config.hooks if config and config.raw().enable_hooks().get() else []) for hook in hooks: yield cls._meta_hook(hook, cls._generate_method(hook, config, job_key, event, api_call, extra_argument))
def setUp(self): GlobalHookRegistry.reset()