async def test_pipe_to_log(context): cmd = r""">&2 echo "foo" && echo "bar" && exit 0""" proc = await asyncio.create_subprocess_exec( "bash", "-c", cmd, stdout=PIPE, stderr=PIPE, stdin=None ) tasks = [] with swlog.get_log_filehandle(context) as log_fh: tasks.append(swlog.pipe_to_log(proc.stderr, filehandles=[log_fh])) tasks.append(swlog.pipe_to_log(proc.stdout, filehandles=[log_fh])) await asyncio.wait(tasks) await proc.wait() log_file = swlog.get_log_filename(context) assert read(log_file) in ("foo\nbar\n", "bar\nfoo\n")
async def test_run_task_negative_11(context, mocker): async def fake_wait(): return -11 fake_proc = mock.MagicMock() fake_proc.wait = fake_wait async def fake_exec(*args, **kwargs): return fake_proc mocker.patch.object(asyncio, 'create_subprocess_exec', new=fake_exec) status = await swtask.run_task(context, noop_to_cancellable_process) log_file = log.get_log_filename(context) contents = read(log_file) assert contents == "Automation Error: python exited with signal -11\n"
async def test_run_task_negative_11(context, mocker): async def fake_wait(): return -11 fake_proc = mock.MagicMock() fake_proc.wait = fake_wait async def fake_exec(*args, **kwargs): return fake_proc mocker.patch.object(asyncio, 'create_subprocess_exec', new=fake_exec) status = await swtask.run_task(context) log_file = log.get_log_filename(context) contents = read(log_file) assert contents == "Automation Error: python exited with signal -11\n"
async def test_pipe_to_log(context): cmd = r""">&2 echo "foo" && echo "bar" && exit 0""" proc = await asyncio.create_subprocess_exec("bash", "-c", cmd, stdout=PIPE, stderr=PIPE, stdin=None) tasks = [] with swlog.get_log_filehandle(context) as log_fh: tasks.append(swlog.pipe_to_log(proc.stderr, filehandles=[log_fh])) tasks.append(swlog.pipe_to_log(proc.stdout, filehandles=[log_fh])) await asyncio.wait(tasks) await proc.wait() log_file = swlog.get_log_filename(context) assert read(log_file) in ("foo\nbar\n", "bar\nfoo\n")
def test_run_task(context, event_loop): status = event_loop.run_until_complete(task.run_task(context)) log_file = log.get_log_filename(context) assert read(log_file) in ("bar\nfoo\nexit code: 1\n", "foo\nbar\nexit code: 1\n") assert status == 1
async def test_run_task(context): status = await swtask.run_task(context, noop_to_cancellable_process) log_file = log.get_log_filename(context) assert read(log_file) in ("bar\nfoo\nexit code: 1\n", "foo\nbar\nexit code: 1\n") assert status == 1
async def test_run_task(context): status = await swtask.run_task(context) log_file = log.get_log_filename(context) assert read(log_file) in ("bar\nfoo\nexit code: 1\n", "foo\nbar\nexit code: 1\n") assert status == 1
def test_get_log_filehandle(context, text): log_file = swlog.get_log_filename(context) with swlog.get_log_filehandle(context) as log_fh: log_fh.write(text) log_fh.write(text) assert read(log_file) == text + text
def test_get_log_filename(context): log_file = swlog.get_log_filename(context) assert log_file == os.path.join(context.config['task_log_dir'], 'live_backing.log')
def test_get_log_filename(rw_context): log_file = swlog.get_log_filename(rw_context) assert log_file == os.path.join(rw_context.config["task_log_dir"], "live_backing.log")
def test_get_log_filehandle(context, text): log_file = swlog.get_log_filename(context) with swlog.get_log_filehandle(context) as log_fh: log_fh.write(text) log_fh.write(text) assert read(log_file) == text + text
def test_get_log_filename(context): log_file = swlog.get_log_filename(context) assert log_file == os.path.join(context.config['task_log_dir'], 'live_backing.log')