def test_delayed_job(self): """Run a test of the "create" command against a mocked-out API: this time, make the monitor check status several times before successful completion. """ mock_options = self.setup_mock_options() (mock_api, mock_scheduler_proxy) = self.create_mock_api() with contextlib.nested( patch('threading._Event.wait'), patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options)) as (sleep, make_client, options): mock_query = self.create_mock_query() mock_query_results = [ self.create_mock_status_query_result(ScheduleStatus.PENDING), self.create_mock_status_query_result(ScheduleStatus.PENDING), self.create_mock_status_query_result(ScheduleStatus.RUNNING) ] mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = mock_query_results mock_api.create_job.return_value = self.get_createjob_response() 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. self.assert_create_job_called(mock_api) self.assert_scheduler_called(mock_api, mock_query, 3) # make_client should have been called once. make_client.assert_called_with('west')
def test_simple_successful_create_job(self): """Run a test of the "create" command against a mocked-out API: Verifies that the creation command sends the right API RPCs, and performs the correct tests on the result.""" mock_options = self.setup_mock_options() # 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) = self.setup_mock_api() with contextlib.nested( patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options)) as (make_client, options): # After making the client, create sets up a job monitor. # The monitor uses TaskQuery to get the tasks. It's called at least twice:once before # the job is created, and once after. So we need to set up mocks for the query results. mock_query = self.create_mock_query() mock_scheduler.getTasksStatus.side_effect = [ self.create_mock_status_query_result(ScheduleStatus.INIT), self.create_mock_status_query_result(ScheduleStatus.RUNNING) ] # With the monitor set up, create finally gets around to calling create_job. mock_api.create_job.return_value = self.get_createjob_response() # Then it calls handle_open; we need to provide a mock for the API calls it uses. mock_api.scheduler.scheduler.return_value = mock_scheduler # 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. # Check that create_job was called exactly once, with an AuroraConfig parameter. self.assert_create_job_called(mock_api) self.assert_scheduler_called(mock_api, mock_query, 2) # make_client should have been called once. make_client.assert_called_with('west')
def test_simple_successful_create_job(self): """Run a test of the "create" command against a mocked-out API: Verifies that the creation command sends the right API RPCs, and performs the correct tests on the result.""" mock_options = self.setup_mock_options() # 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.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options)) as (make_client, options): # After making the client, create sets up a job monitor. # The monitor uses TaskQuery to get the tasks. It's called at least twice:once before # the job is created, and once after. So we need to set up mocks for the query results. mock_query = self.create_mock_query() mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = [ self.create_mock_status_query_result(ScheduleStatus.RUNNING) ] # With the monitor set up, create finally gets around to calling create_job. mock_api.create_job.return_value = self.get_createjob_response() # 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. # Check that create_job was called exactly once, with an AuroraConfig parameter. self.assert_create_job_called(mock_api) self.assert_scheduler_called(mock_api, mock_query, 1) # make_client should have been called once. make_client.assert_called_with('west')
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_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() 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 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