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_create():
cfile = create_config(("listen=:9999, :::9999", "workers=2"))
Config(cfile).load()
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"), mock.patch(
"blackhole.worker.Worker.start"
):
supervisor = Supervisor(loop=loop)
supervisor.start_workers()
assert len(supervisor.workers) == 2
supervisor.close_socks()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
def test_run():
cfile = create_config(("listen=:9999, :::9999", "workers=2"))
Config(cfile).load()
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"), mock.patch(
"blackhole.worker.Worker.start"
):
supervisor = Supervisor(loop=loop)
with mock.patch("{0}.run_forever".format(_LOOP)):
supervisor.run()
assert len(supervisor.workers) == 2
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
def test_spawn_ipv6():
cfile = create_config(("listen=:::9999",))
Config(cfile).load()
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"):
supervisor = Supervisor(loop=loop)
assert len(supervisor.socks) == 1
supervisor.close_socks()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
key = create_file("key.key")
cfile = create_config(
(
"listen=127.0.0.1:9998",
"tls_listen=127.0.0.1:9999",
"tls_cert={}".format(cert),
"tls_key={}".format(key),
)
)
conf = Config(cfile).load()
conf.args = Args((("less_secure", False),))
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"), mock.patch(
"ssl.create_default_context"
):
supervisor = Supervisor(loop=loop)
assert len(supervisor.socks) == 2
supervisor.close_socks()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
key = create_file("test.key", key_data)
dhparams = create_file("dhparams.pem", dhparams_data)
cfile = create_config(
(
"listen=127.0.0.1:9998",
"tls_listen=127.0.0.1:9999",
"tls_cert={}".format(cert),
"tls_key={}".format(key),
"tls_dhparams={}".format(dhparams),
)
)
conf = Config(cfile).load()
conf.args = Args((("less_secure", False),))
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"):
supervisor = Supervisor(loop=loop)
assert len(supervisor.socks) == 2
supervisor.close_socks()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
def test_stop_runtime_error():
cfile = create_config(("listen=:9999, :::9999", "workers=2"))
Config(cfile).load()
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"), mock.patch(
"blackhole.worker.Worker.start"
):
supervisor = Supervisor(loop=loop)
supervisor.start_workers()
assert len(supervisor.workers) == 2
with mock.patch(
"blackhole.worker.Worker.stop"
) as mock_stop, mock.patch(
"{0}.stop".format(_LOOP), side_effect=RuntimeError
) as mock_rt, pytest.raises(
SystemExit
) as exc:
supervisor.stop()
assert mock_rt.call_count == 1
assert mock_stop.call_count == 2
assert exc.value.code == 0
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
def test_spawn_ipv6_fail():
cfile = create_config(("listen=:::9999",))
Config(cfile).load()
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind", side_effect=OSError), pytest.raises(
BlackholeRuntimeException
):
Supervisor(loop=loop)
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
key = create_file("key.key")
cfile = create_config(
(
"listen=127.0.0.1:9998",
"tls_listen=127.0.0.1:9999",
"tls_cert={}".format(cert),
"tls_key={}".format(key),
)
)
conf = Config(cfile).load()
conf.args = Args((("less_secure", True),))
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"), mock.patch(
"ssl.create_default_context"
):
supervisor = Supervisor(loop=loop)
assert len(supervisor.socks) == 2
supervisor.close_socks()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
def test_stop():
cfile = create_config(("listen=:9999, :::9999", "workers=2"))
Config(cfile).load()
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"), mock.patch(
"blackhole.worker.Worker.start"
):
supervisor = Supervisor(loop=loop)
supervisor.start_workers()
assert len(supervisor.workers) == 2
with mock.patch(
"blackhole.worker.Worker.stop"
) as mock_stop, pytest.raises(SystemExit) as exc:
supervisor.stop()
assert mock_stop.call_count == 2
assert exc.value.code == 0
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
key = create_file("test.key", key_data)
dhparams = create_file("dhparams.pem", dhparams_data)
cfile = create_config(
(
"listen=:::9998",
"tls_listen=:::9999",
"tls_cert={}".format(cert),
"tls_key={}".format(key),
"tls_dhparams={}".format(dhparams),
)
)
conf = Config(cfile).load()
conf.args = Args((("less_secure", False),))
loop = asyncio.new_event_loop()
with mock.patch("socket.socket.bind"):
supervisor = Supervisor(loop=loop)
assert len(supervisor.socks) == 2
assert supervisor.socks[1]["ssl"] is not None
supervisor.close_socks()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()