def test_io_error(monkeypatch): print_hello = tmp_script_commandline( """from __future__ import print_function import os import sys print("hello") sys.stdout.flush() sys.exit(0) """) stdout_from_callback = [] def on_stdout(data): stdout_from_callback.append(data) stderr_from_callback = [] def on_stderr(data): stderr_from_callback.append(data) def mock_read(*args, **kwargs): raise IOError("Nope") monkeypatch.setattr( "anaconda_project.internal.streaming_popen._read_from_stream", mock_read) with pytest.raises(IOError) as excinfo: streaming_popen.popen(print_hello, on_stdout, on_stderr) assert "Nope" in str(excinfo.value)
def get_command(extra_args): return tmp_script_commandline("""from __future__ import print_function import sys print("TEST_ERROR", file=sys.stderr) print("{}") sys.exit(0) """)
def check(dirname): local_state_file = LocalStateFile.load_for_directory(dirname) true_commandline = tmp_script_commandline("""import sys sys.exit(0) """) local_state_file.set_service_run_state('FOO', {'shutdown_commands': [true_commandline]}) status = shutdown_service_run_state(local_state_file, 'FOO') assert status assert status.status_description == "Successfully shut down FOO."
def check(dirname): local_state_file = LocalStateFile.load_for_directory(dirname) false_commandline = tmp_script_commandline("""import sys sys.exit(1) """) local_state_file.set_service_run_state('FOO', {'shutdown_commands': [false_commandline]}) status = shutdown_service_run_state(local_state_file, 'FOO') assert not status assert status.status_description == "Shutdown commands failed for FOO." assert status.errors == ["Shutting down FOO, command %r failed with code 1." % false_commandline]
def check(dirname): _monkeypatch_pwd(monkeypatch, dirname) local_state = LocalStateFile.load_for_directory(dirname) false_commandline = tmp_script_commandline("""import sys sys.exit(1) """) local_state.set_service_run_state('TEST', {'shutdown_commands': [false_commandline]}) local_state.save() code = _parse_args_and_run_subcommand(['anaconda-project', 'remove-service', 'TEST']) assert code == 1 out, err = capsys.readouterr() expected_err = ("Shutting down TEST, command %r failed with code 1.\n" + "Shutdown commands failed for TEST.\n") % false_commandline assert expected_err == err assert '' == out
def test_callbacks_are_none(): print_stuff = tmp_script_commandline(u"""# -*- coding: utf-8 -*- from __future__ import print_function import sys print("a") print("b", file=sys.stderr) sys.exit(0) """) (p, out_lines, err_lines) = streaming_popen.popen(print_stuff, None, None) expected_out = add_lineseps(['a']) expected_err = add_lineseps(['b']) assert expected_out == out_lines assert expected_err == err_lines assert p.returncode is 0
def test_bad_utf8(): print_bad = tmp_script_commandline(u"""# -*- coding: utf-8 -*- from __future__ import print_function import os import sys print("hello") sys.stdout.flush() # write some garbage os.write(sys.stdout.fileno(), b"\\x42\\xff\\xef\\xaa\\x00\\x01\\xcc") sys.stdout.flush() print("goodbye") sys.stdout.flush() sys.exit(0) """) stdout_from_callback = [] def on_stdout(data): stdout_from_callback.append(data) stderr_from_callback = [] def on_stderr(data): stderr_from_callback.append(data) (p, out_lines, err_lines) = streaming_popen.popen(print_bad, on_stdout, on_stderr) sep_out = detect_linesep(out_lines) expected_out = add_lineseps([u'hello', u'B��\x00\x01�goodbye'], sep_out) expected_err = [] assert expected_out == out_lines assert expected_err == err_lines assert "".join(expected_out) == "".join(stdout_from_callback) assert "".join(expected_err) == "".join(stderr_from_callback)
def get_command(extra_args): return tmp_script_commandline("""from __future__ import print_function import sys print("NOT_JSON") sys.exit(0) """)
def test_streaming(): print_stuff = tmp_script_commandline(u"""# -*- coding: utf-8 -*- from __future__ import print_function import sys import time def flush(): time.sleep(0.05) sys.stdout.flush() sys.stderr.flush() print("a") flush() print("x", file=sys.stderr) flush() print("b") flush() print("y", file=sys.stderr) flush() print("c") flush() print("z", file=sys.stderr) flush() print("d") flush() # print partial lines with multiple syscalls for i in [1,2,3,4,5,6]: sys.stdout.write("%d" % i) sys.stdout.write("\\n") # print unicode stuff, throws exception on Windows try: print("💯 🌟") flush() except Exception: print("Windows") # print many lines at once, and end on non-newline sys.stdout.write("1\\n2\\n3\\n4\\n5\\n6") flush() sys.exit(2) """) stdout_from_callback = [] def on_stdout(data): stdout_from_callback.append(data) stderr_from_callback = [] def on_stderr(data): stderr_from_callback.append(data) (p, out_lines, err_lines) = streaming_popen.popen(print_stuff, on_stdout, on_stderr) expected_out = add_lineseps([ u'a', u'b', u'c', u'd', u'123456', u'💯 🌟', u'1', u'2', u'3', u'4', u'5' ]) if platform.system() == 'Windows': # Windows can't output unicode if _PY2: expected_out[5] = u'\U0001f4af \U0001f31f\r\n' else: expected_out[5] = u"Windows\r\n" expected_out.append(u'6') # no newline after this one expected_err = add_lineseps([u'x', u'y', u'z']) assert expected_out == out_lines assert "".join(expected_out) == "".join(stdout_from_callback) assert expected_err == err_lines assert "".join(expected_err) == "".join(stderr_from_callback) assert p.returncode is 2
def get_failed_command(prefix, extra_args): return tmp_script_commandline(error_message_but_success_script)
def get_failed_command(prefix, extra_args): return tmp_script_commandline(error_script)
else: return real_abspath(path) monkeypatch.setattr('os.path.abspath', mock_abspath) def _monkeypatch_add_service(monkeypatch, result): def mock_add_service(*args, **kwargs): return result monkeypatch.setattr("anaconda_project.project_ops.add_service", mock_add_service) _echo_commandline = tmp_script_commandline("""from __future__ import print_function import sys print(" ".join(sys.argv)) sys.exit(0) """) def test_add_service(capsys, monkeypatch): def check(dirname): _monkeypatch_pwd(monkeypatch, dirname) status = SimpleStatus(success=True, description='Service added.') status.requirement = RedisRequirement(RequirementsRegistry(), env_var='REDIS_URL', options=dict(type='redis')) _monkeypatch_add_service(monkeypatch, status) code = _parse_args_and_run_subcommand(['anaconda-project', 'add-service', 'redis']) assert code == 0