def setup(self): TaskConfig = namedtuple('TaskConfig', 'cmd task_id cpus mem disk env') self.action_run_id = 'my_service.job.1.action' self.task_id = '123abcuuid' with mock.patch('tron.mesos.logging.getLogger', return_value=mock.Mock(handlers=[mock.Mock()]), autospec=None): self.task = MesosTask( id=self.action_run_id, task_config=TaskConfig(cmd='echo hello world', task_id=self.task_id, cpus=0.1, mem=100, disk=100, env={ 'INITIAL_VAR': 'baz', 'AWS_SECRET_ACCESS_KEY': 'THISISASECRET', 'SOME_VAR': 'bar', 'AWS_ACCESS_KEY_ID': 'THISISASECRETTOO', 'SOME_OTHER_VAR': 'foo', }), ) yield
def setup(self): self.action_run_id = 'my_service.job.1.action' self.task_id = '123abcuuid' self.task = MesosTask( id=self.action_run_id, task_config=mock.Mock( cmd='echo hello world', task_id=self.task_id, ), ) # Suppress logging with mock.patch.object(self.task, 'log'): yield
def setup(self): self.action_run_id = 'my_service.job.1.action' self.task_id = '123abcuuid' self.task = MesosTask( id=self.action_run_id, task_config=mock.Mock( cmd='echo hello world', task_id=self.task_id, cpus=0.1, mem=100, disk=100, ), ) # Suppress logging with mock.patch.object(self.task, 'log'): yield
class TestMesosTask(TestCase): @setup_teardown def setup(self): self.action_run_id = 'my_service.job.1.action' self.task_id = '123abcuuid' self.task = MesosTask( id=self.action_run_id, task_config=mock.Mock( cmd='echo hello world', task_id=self.task_id, cpus=0.1, mem=100, disk=100, ), ) # Suppress logging with mock.patch.object(self.task, 'log'): yield def test_handle_staging(self): event = mock_task_event( task_id=self.task_id, platform_type='staging', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_starting(self): event = mock_task_event( task_id=self.task_id, platform_type='starting', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_running(self): event = mock_task_event( task_id=self.task_id, platform_type='running', ) self.task.handle_event(event) assert self.task.state == MesosTask.RUNNING def test_handle_running_for_other_task(self): event = mock_task_event( task_id='other321', platform_type='running', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_finished(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='finished', terminal=True, success=True, ) self.task.handle_event(event) assert self.task.is_complete def test_handle_failed(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='failed', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_killed(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='killed', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_lost(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='lost', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_unknown assert self.task.is_done def test_handle_error(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='error', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_terminal_event_offer_timeout(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type=None, terminal=True, success=False, raw='failed due to offer timeout', message='stop', ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_success_sequence(self): self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='staging', )) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='running', )) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='finished', terminal=True, success=True, )) assert self.task.is_complete def test_log_event_error(self): with mock.patch.object( self.task, 'log_event_info', ) as mock_log_event, mock.patch.object( self.task.log, 'warning', ) as mock_log: mock_log_event.side_effect = Exception self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='running', )) assert mock_log_event.called assert mock_log.called assert self.task.state == MesosTask.RUNNING def test_get_event_logger_add_unique_handlers(self): """ Ensures that only a single handler (for stderr) is added to the MesosTask event logger, to prevent duplicate log output. """ # Call 2 times to make sure 2nd call doesn't add another handler logger = self.task.get_event_logger() logger = self.task.get_event_logger() assert len(logger.handlers) == 1
class TestMesosTask(TestCase): @setup_teardown def setup(self): TaskConfig = namedtuple('TaskConfig', 'cmd task_id cpus mem disk env') self.action_run_id = 'my_service.job.1.action' self.task_id = '123abcuuid' with mock.patch('tron.mesos.logging.getLogger', return_value=mock.Mock(handlers=[mock.Mock()]), autospec=None): self.task = MesosTask( id=self.action_run_id, task_config=TaskConfig(cmd='echo hello world', task_id=self.task_id, cpus=0.1, mem=100, disk=100, env={ 'INITIAL_VAR': 'baz', 'AWS_SECRET_ACCESS_KEY': 'THISISASECRET', 'SOME_VAR': 'bar', 'AWS_ACCESS_KEY_ID': 'THISISASECRETTOO', 'SOME_OTHER_VAR': 'foo', }), ) yield def test_aws_credentials_redacted(self): assert all([ 'THISISASECRET' not in text[0][0] for text in self.task.log.info.call_args_list ]) assert all([ 'foo' in text[0][0] for text in self.task.log.info.call_args_list ]) assert all([ 'bar' in text[0][0] for text in self.task.log.info.call_args_list ]) assert all([ 'baz' in text[0][0] for text in self.task.log.info.call_args_list ]) def test_handle_staging(self): event = mock_task_event( task_id=self.task_id, platform_type='staging', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_starting(self): event = mock_task_event( task_id=self.task_id, platform_type='starting', ) self.task.handle_event(event) assert self.task.state == MesosTask.RUNNING def test_handle_running(self): event = mock_task_event( task_id=self.task_id, platform_type='running', ) self.task.handle_event(event) assert self.task.state == MesosTask.RUNNING def test_handle_running_for_other_task(self): event = mock_task_event( task_id='other321', platform_type='running', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_finished(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='finished', terminal=True, success=True, ) self.task.handle_event(event) assert self.task.is_complete def test_handle_failed(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='failed', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_killed(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='killed', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_lost(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='lost', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_unknown assert self.task.is_done def test_handle_error(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='error', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_terminal_event_offer_timeout(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type=None, terminal=True, success=False, raw='failed due to offer timeout', message='stop', ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_success_sequence(self): self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='staging', )) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='starting', )) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='running', )) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='finished', terminal=True, success=True, )) assert self.task.is_complete def test_log_event_error(self): with mock.patch.object( self.task, 'log_event_info', ) as mock_log_event, mock.patch.object( self.task.log, 'warning', ) as mock_log: mock_log_event.side_effect = Exception self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='running', )) assert mock_log_event.called assert mock_log.called assert self.task.state == MesosTask.RUNNING def test_get_event_logger_add_unique_handlers(self): """ Ensures that only a single handler (for stderr) is added to the MesosTask event logger, to prevent duplicate log output. """ # Call 2 times to make sure 2nd call doesn't add another handler logger = self.task.get_event_logger() logger = self.task.get_event_logger() assert len(logger.handlers) == 1
class TestMesosTask(TestCase): @setup_teardown def setup(self): self.action_run_id = 'my_service.job.1.action' self.task_id = '123abcuuid' self.task = MesosTask( id=self.action_run_id, task_config=mock.Mock( cmd='echo hello world', task_id=self.task_id, cpus=0.1, mem=100, disk=100, ), ) # Suppress logging with mock.patch.object(self.task, 'log'): yield def test_handle_staging(self): event = mock_task_event( task_id=self.task_id, platform_type='staging', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_running(self): event = mock_task_event( task_id=self.task_id, platform_type='running', ) self.task.handle_event(event) assert self.task.state == MesosTask.RUNNING def test_handle_running_for_other_task(self): event = mock_task_event( task_id='other321', platform_type='running', ) self.task.handle_event(event) assert self.task.state == MesosTask.PENDING def test_handle_finished(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='finished', terminal=True, success=True, ) self.task.handle_event(event) assert self.task.is_complete def test_handle_failed(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='failed', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_killed(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='killed', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_lost(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='lost', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_unknown assert self.task.is_done def test_handle_error(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type='error', terminal=True, success=False, ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_terminal_event_offer_timeout(self): self.task.started() event = mock_task_event( task_id=self.task_id, platform_type=None, terminal=True, success=False, raw='failed due to offer timeout', message='stop', ) self.task.handle_event(event) assert self.task.is_failed assert self.task.is_done def test_handle_success_sequence(self): self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='staging', ) ) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='running', ) ) self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='finished', terminal=True, success=True, ) ) assert self.task.is_complete def test_log_event_error(self): with mock.patch.object( self.task, 'log_event_info', ) as mock_log_event, mock.patch.object( self.task.log, 'warning', ) as mock_log: mock_log_event.side_effect = Exception self.task.handle_event( mock_task_event( task_id=self.task_id, platform_type='running', ) ) assert mock_log_event.called assert mock_log.called assert self.task.state == MesosTask.RUNNING def test_get_event_logger_add_unique_handlers(self): """ Ensures that only a single handler (for stderr) is added to the MesosTask event logger, to prevent duplicate log output. """ # Call 2 times to make sure 2nd call doesn't add another handler logger = self.task.get_event_logger() logger = self.task.get_event_logger() assert len(logger.handlers) == 1