def test_format_stdstreams_tail_for_task( test_case, ): def gen_mesos_cli_fobj(file_path, file_lines): """mesos.cli.cluster.files (0.1.5), returns a list of mesos.cli.mesos_file.File `File` is an iterator-like object. """ fake_iter = mock.MagicMock() fake_iter.return_value = reversed(file_lines) fobj = mock.create_autospec(mesos.mesos_file.File) fobj.path = file_path fobj.__reversed__ = fake_iter return fobj def get_short_task_id(task_id): return task_id def gen_mock_cluster_files(file1, file2, raise_what): def retfunc(*args, **kwargs): # If we're asked to raise a particular exception we do so. # .message is set to the exception class name. if raise_what: raise raise_what(raise_what) return [ gen_mesos_cli_fobj(file1[0], file1[1]), gen_mesos_cli_fobj(file2[0], file2[1]) ] mock_cluster_files = mock.MagicMock() mock_cluster_files.side_effect = retfunc return mock_cluster_files def gen_output(task_id, file1, file2, nlines, raise_what): error_message = PaastaColors.red(" couldn't read stdout/stderr for %s (%s)") output = [] if not raise_what: files = [file1, file2] # reverse sort because stdout is supposed to always come before stderr in the output files.sort(key=lambda f: f[0], reverse=True) for f in files: output.append(PaastaColors.blue(" %s tail for %s" % (f[0], task_id))) output.extend(f[1][-nlines:]) output.append(PaastaColors.blue(" %s EOF" % f[0])) else: if raise_what == utils.TimeoutError: raise_what = 'timeout' output.append(error_message % (task_id, raise_what)) return output task_id, file1, file2, nlines, raise_what = test_case mock_cluster_files = gen_mock_cluster_files(file1, file2, raise_what) fake_task = {'id': task_id} expected = gen_output(task_id, file1, file2, nlines, raise_what) with mock.patch('paasta_tools.mesos_tools.get_mesos_config', autospec=True): with mock.patch('paasta_tools.mesos_tools.cluster.get_files_for_tasks', mock_cluster_files, autospec=None): result = mesos_tools.format_stdstreams_tail_for_task(fake_task, get_short_task_id) assert result == expected
def test_format_stdstreams_tail_for_task( test_case, ): def gen_mesos_cli_fobj(file_path, file_lines): """mesos.cli.cluster.files (0.1.5), returns a list of mesos.cli.mesos_file.File `File` is an iterator-like object. """ fake_iter = mock.MagicMock() fake_iter.return_value = reversed(file_lines) fobj = mock.create_autospec(mesos.mesos_file.File) fobj.path = file_path fobj.__reversed__ = fake_iter return fobj def get_short_task_id(task_id): return task_id def gen_mock_cluster_files(file1, file2, raise_what): def retfunc(*args, **kwargs): # If we're asked to raise a particular exception we do so. # .message is set to the exception class name. if raise_what: raise raise_what(raise_what) return [ gen_mesos_cli_fobj(file1[0], file1[1]), gen_mesos_cli_fobj(file2[0], file2[1]) ] mock_cluster_files = mock.MagicMock() mock_cluster_files.side_effect = retfunc return mock_cluster_files def gen_output(task_id, file1, file2, nlines, raise_what): error_message = PaastaColors.red(" couldn't read stdout/stderr for %s (%s)") output = [] if not raise_what: files = [file1, file2] # reverse sort because stdout is supposed to always come before stderr in the output files.sort(key=lambda f: f[0], reverse=True) for f in files: output.append(PaastaColors.blue(" %s tail for %s" % (f[0], task_id))) output.extend(f[1][-nlines:]) output.append(PaastaColors.blue(" %s EOF" % f[0])) else: if raise_what == utils.TimeoutError: raise_what = 'timeout' output.append(error_message % (task_id, raise_what)) return output task_id, file1, file2, nlines, raise_what = test_case mock_cluster_files = gen_mock_cluster_files(file1, file2, raise_what) fake_task = {'id': task_id} expected = gen_output(task_id, file1, file2, nlines, raise_what) with mock.patch('paasta_tools.mesos_tools.get_mesos_config', autospec=True): with mock.patch('paasta_tools.mesos_tools.cluster.get_files_for_tasks', mock_cluster_files, autospec=None): result = mesos_tools.format_stdstreams_tail_for_task(fake_task, get_short_task_id) assert result == expected