Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_command(command):
"""Check that the command and command parts are equivalent."""
executor = SimpleExecutor(command)
assert executor.command == SLEEP_300
assert executor.command_parts == SLEEP_300.split()
def test_start_and_wait():
"""Test if executor await for process to accept connections."""
command = 'bash -c "sleep 2 && nc -l 3000"'
executor = TCPExecutor(command, 'localhost', port=3000, timeout=5)
executor.start()
assert executor.running() is True
executor.stop()
# check proper __str__ and __repr__ rendering:
assert 'TCPExecutor' in repr(executor)
assert command in str(executor)
def test_start_and_wait():
"""Test if the executor will await for the process to create a file."""
process = f'bash -c "sleep 2 && touch {FILENAME} && sleep 10"'
with PidExecutor(process, FILENAME, timeout=5) as executor:
assert executor.running() is True
# check proper __str__ and __repr__ rendering:
assert 'PidExecutor' in repr(executor)
assert process in str(executor)
def test_slow_method_server_starting(method):
"""
Test whether or not executor awaits for slow starting servers.
Simple example. You run Gunicorn and it is working but you have to
wait for worker processes.
"""
http_method_slow_cmd = '{python} {srv} {host}:{port} False {method}'.format(
python=sys.executable,
srv=TEST_SERVER_PATH,
host=HOST,
port=PORT,
method=method
)
with HTTPExecutor(
http_method_slow_cmd,
'http://{0}:{1}/'.format(HOST, PORT), method=method, timeout=30
) as executor:
assert executor.running() is True
connect_to_server()
def test_fail_if_other_running():
"""Test raising AlreadyRunning exception when port is blocked."""
executor = HTTPExecutor(
HTTP_NORMAL_CMD, 'http://{0}:{1}/'.format(HOST, PORT),
)
executor2 = HTTPExecutor(
HTTP_NORMAL_CMD, 'http://{0}:{1}/'.format(HOST, PORT),
)
with executor:
assert executor.running() is True
with pytest.raises(AlreadyRunning):
executor2.start()
with pytest.raises(AlreadyRunning) as exc:
with executor2:
pass
assert 'seems to be already running' in str(exc.value)
def test_fail_if_other_running():
"""Test raising AlreadyRunning exception when port is blocked."""
executor = HTTPExecutor(
HTTP_NORMAL_CMD, 'http://{0}:{1}/'.format(HOST, PORT),
)
executor2 = HTTPExecutor(
HTTP_NORMAL_CMD, 'http://{0}:{1}/'.format(HOST, PORT),
)
with executor:
assert executor.running() is True
with pytest.raises(AlreadyRunning):
executor2.start()
with pytest.raises(AlreadyRunning) as exc:
with executor2:
pass
def test_shell_started_server_stops():
"""Test if executor terminates properly executor with shell=True."""
executor = HTTPExecutor(
HTTP_NORMAL_CMD,
'http://{0}:{1}/'.format(HOST, PORT),
timeout=20,
shell=True
)
with pytest.raises(socket.error):
connect_to_server()
with executor:
assert executor.running() is True
connect_to_server()
assert executor.running() is False
with pytest.raises(socket.error):
def test_slow_post_payload_server_starting():
"""
Test whether or not executor awaits for slow starting servers.
Simple example. You run Gunicorn and it is working but you have to
wait for worker processes.
"""
http_method_slow_cmd = '{python} {srv} {host}:{port} False {method}'.format(
python=sys.executable,
srv=TEST_SERVER_PATH,
host=HOST,
port=PORT,
method='Key'
)
with HTTPExecutor(
http_method_slow_cmd,
'http://{0}:{1}/'.format(HOST, PORT),
method='POST',
timeout=30,
payload={'key': 'hole'}
) as executor:
assert executor.running() is True
connect_to_server()
def test_stopping_brutally():
"""
Test if SimpleExecutor is stopping insubordinate process.
Check if the process that doesn't react to SIGTERM signal will be killed
by executor with SIGKILL automatically.
"""
host_port = "127.0.0.1:8000"
cmd = f'{sys.executable} {TEST_SERVER_PATH} {host_port} True'
executor = HTTPExecutor(cmd, f'http://{host_port!s}/', timeout=20)
executor.start()
assert executor.running() is True
stop_at = time.time() + 10
executor.stop()
assert executor.running() is False
assert stop_at <= time.time(), "Subprocess killed earlier than in 10 secs"
def test_default_port():
"""
Test default port for the base TCP check.
Check if HTTP executor fills in the default port for the TCP check
from the base class if no port is provided in the URL.
"""
executor = HTTPExecutor(HTTP_NORMAL_CMD, 'http://{0}/'.format(HOST))
assert executor.url.port is None
assert executor.port == PORT
assert TCPExecutor.pre_start_check(executor) is False
executor.start()
assert TCPExecutor.pre_start_check(executor) is True
executor.stop()