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) = self.setup_mock_api() with contextlib.nested( patch('time.sleep'), patch('twitter.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.INIT), self.create_mock_status_query_result(ScheduleStatus.PENDING), self.create_mock_status_query_result(ScheduleStatus.PENDING), self.create_mock_status_query_result(ScheduleStatus.RUNNING), self.create_mock_status_query_result(ScheduleStatus.FINISHED) ] mock_scheduler.getTasksStatus.side_effect = mock_query_results mock_api.create_job.return_value = self.get_createjob_response() mock_api.scheduler.scheduler.return_value = mock_scheduler 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, 4) # 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('twitter.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')