2019-06-28 19:54:45 +02:00
|
|
|
import os
|
|
|
|
from subprocess import CalledProcessError
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from moulinette.utils.process import run_commands
|
2019-12-21 17:36:20 +01:00
|
|
|
from moulinette.utils.process import call_async_output
|
|
|
|
from moulinette.utils.process import check_output
|
2019-06-28 19:54:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_run_shell_command_list(test_file):
|
|
|
|
assert os.path.exists(str(test_file))
|
2019-11-25 17:21:13 +01:00
|
|
|
run_commands(["rm -f %s" % str(test_file)])
|
2019-06-28 19:54:45 +02:00
|
|
|
assert not os.path.exists(str(test_file))
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_shell_bad_cmd():
|
|
|
|
with pytest.raises(CalledProcessError):
|
2019-11-25 17:21:13 +01:00
|
|
|
run_commands(["yolo swag"])
|
2019-12-21 17:36:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_run_shell_bad_cmd_with_callback():
|
|
|
|
def callback(a, b, c):
|
|
|
|
assert isinstance(a, int)
|
|
|
|
assert isinstance(b, str)
|
2021-01-20 05:46:05 +01:00
|
|
|
# assert isinstance(c, str)
|
2019-12-21 17:36:20 +01:00
|
|
|
return True
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2019-12-21 17:36:20 +01:00
|
|
|
assert run_commands(["yolo swag", "yolo swag", "yolo swag"], callback=callback) == 3
|
|
|
|
|
|
|
|
def callback(a, b, c):
|
|
|
|
assert isinstance(a, int)
|
|
|
|
assert isinstance(b, str)
|
2021-01-20 05:46:05 +01:00
|
|
|
# assert isinstance(c, str)
|
2019-12-21 17:36:20 +01:00
|
|
|
return False
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2019-12-21 17:36:20 +01:00
|
|
|
assert run_commands(["yolo swag", "yolo swag"], callback=callback) == 1
|
|
|
|
|
|
|
|
def callback(a, b, c):
|
|
|
|
assert isinstance(a, int)
|
|
|
|
assert isinstance(b, str)
|
|
|
|
assert isinstance(c, tuple)
|
|
|
|
return True
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2019-12-21 17:36:20 +01:00
|
|
|
run_commands(["yolo swag"], separate_stderr=True, callback=callback)
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_shell_bad_callback():
|
|
|
|
callback = 1
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
run_commands(["ls"], callback=callback)
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_shell_kwargs():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
run_commands([""], stdout="None")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
run_commands([""], stderr="None")
|
|
|
|
|
|
|
|
run_commands(["ls"], cwd="/tmp")
|
|
|
|
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
run_commands(["ls"], cwd="/yoloswag")
|
|
|
|
|
|
|
|
|
|
|
|
def test_call_async_output(test_file):
|
2021-01-25 18:53:42 +01:00
|
|
|
def stdout_callback(a):
|
|
|
|
assert a == "foo" or a == "bar"
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
def stderr_callback(a):
|
|
|
|
assert False # we shouldn't reach this line
|
2019-12-21 17:36:20 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
callbacks = (lambda l: stdout_callback(l), lambda l: stderr_callback(l))
|
|
|
|
|
|
|
|
call_async_output(["cat", str(test_file)], callbacks)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2019-12-21 17:36:20 +01:00
|
|
|
call_async_output(["cat", str(test_file)], 1)
|
|
|
|
|
|
|
|
def callbackA(a):
|
2021-01-25 18:53:42 +01:00
|
|
|
assert a == "foo" or a == "bar"
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2019-12-21 17:36:20 +01:00
|
|
|
def callbackB(a):
|
2021-01-25 18:53:42 +01:00
|
|
|
assert "cat: doesntexists" in a
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2019-12-21 17:36:20 +01:00
|
|
|
callback = (callbackA, callbackB)
|
|
|
|
call_async_output(["cat", str(test_file)], callback)
|
2021-01-25 18:53:42 +01:00
|
|
|
call_async_output(["cat", "doesntexists"], callback)
|
2019-12-21 17:36:20 +01:00
|
|
|
|
|
|
|
|
2020-01-01 18:02:47 +01:00
|
|
|
def test_call_async_output_kwargs(test_file, mocker):
|
2021-01-25 18:53:42 +01:00
|
|
|
def stdinfo_callback(a):
|
|
|
|
assert False # we shouldn't reach this line
|
2019-12-21 17:36:20 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
def stdout_callback(a):
|
|
|
|
assert a == "foo" or a == "bar"
|
2019-12-21 17:36:20 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
def stderr_callback(a):
|
|
|
|
assert False # we shouldn't reach this line
|
2020-01-02 16:18:28 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
callbacks = (
|
|
|
|
lambda l: stdout_callback(l),
|
|
|
|
lambda l: stderr_callback(l),
|
|
|
|
lambda l: stdinfo_callback(l),
|
|
|
|
)
|
2019-12-21 17:36:20 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
call_async_output(["cat", str(test_file)], callbacks, stdout=None)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
call_async_output(["cat", str(test_file)], callbacks, stderr=None)
|
2021-01-26 16:38:36 +01:00
|
|
|
with pytest.raises(TypeError):
|
2021-01-25 18:53:42 +01:00
|
|
|
call_async_output(["cat", str(test_file)], callbacks, stdinfo=None)
|
2019-12-21 17:36:20 +01:00
|
|
|
|
2020-01-01 18:02:47 +01:00
|
|
|
dirname = os.path.dirname(str(test_file))
|
2021-01-25 18:53:42 +01:00
|
|
|
os.mkdir(os.path.join(dirname, "testcwd"))
|
2020-01-02 16:41:21 +01:00
|
|
|
call_async_output(
|
2021-01-25 18:53:42 +01:00
|
|
|
["cat", str(test_file)], callbacks, cwd=os.path.join(dirname, "testcwd")
|
2020-01-02 16:41:21 +01:00
|
|
|
)
|
2019-12-21 17:36:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_output(test_file):
|
2021-01-25 18:53:42 +01:00
|
|
|
assert check_output(["cat", str(test_file)], shell=False) == "foo\nbar"
|
2019-12-21 17:36:20 +01:00
|
|
|
|
2021-01-25 18:53:42 +01:00
|
|
|
assert check_output("cat %s" % str(test_file)) == "foo\nbar"
|