| 1 |
"""Tests for kernel connection utilities""" |
| 2 |
|
| 3 |
# Copyright (c) IPython Development Team. |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
import errno |
| 6 |
import json |
| 7 |
import os |
| 8 |
from tempfile import TemporaryDirectory |
| 9 |
from typing import no_type_check |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
import zmq |
| 14 |
from traitlets.config.loader import Config |
| 15 |
|
| 16 |
from ipykernel import connect |
| 17 |
from ipykernel.kernelapp import IPKernelApp |
| 18 |
|
| 19 |
from .utils import TemporaryWorkingDirectory |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture(scope="module", autouse=True) |
| 23 |
def _enable_tracemalloc(): |
| 24 |
try: |
| 25 |
import tracemalloc |
| 26 |
except ModuleNotFoundError: |
| 27 |
# pypy |
| 28 |
tracemalloc = None |
| 29 |
if tracemalloc is not None: |
| 30 |
tracemalloc.start() |
| 31 |
yield |
| 32 |
if tracemalloc is not None: |
| 33 |
tracemalloc.stop() |
| 34 |
|
| 35 |
|
| 36 |
sample_info: dict = { |
| 37 |
"ip": "1.2.3.4", |
| 38 |
"transport": "ipc", |
| 39 |
"shell_port": 1, |
| 40 |
"hb_port": 2, |
| 41 |
"iopub_port": 3, |
| 42 |
"stdin_port": 4, |
| 43 |
"control_port": 5, |
| 44 |
"key": b"abc123", |
| 45 |
"signature_scheme": "hmac-md5", |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
class DummyKernelApp(IPKernelApp): |
| 50 |
def _default_shell_port(self): |
| 51 |
return 0 |
| 52 |
|
| 53 |
def initialize(self, argv=None): |
| 54 |
self.init_profile_dir() |
| 55 |
self.init_connection_file() |
| 56 |
|
| 57 |
|
| 58 |
def test_get_connection_file(): |
| 59 |
cfg = Config() |
| 60 |
with TemporaryWorkingDirectory() as d: |
| 61 |
cfg.ProfileDir.location = d |
| 62 |
cf = "kernel.json" |
| 63 |
app = DummyKernelApp(config=cfg, connection_file=cf) |
| 64 |
app.initialize() |
| 65 |
|
| 66 |
profile_cf = os.path.join(app.connection_dir, cf) |
| 67 |
assert profile_cf == app.abs_connection_file |
| 68 |
with open(profile_cf, "w") as f: |
| 69 |
f.write("{}") |
| 70 |
assert os.path.exists(profile_cf) |
| 71 |
assert connect.get_connection_file(app) == profile_cf |
| 72 |
|
| 73 |
app.connection_file = cf |
| 74 |
assert connect.get_connection_file(app) == profile_cf |
| 75 |
|
| 76 |
|
| 77 |
def test_get_connection_info(): |
| 78 |
with TemporaryDirectory() as d: |
| 79 |
cf = os.path.join(d, "kernel.json") |
| 80 |
connect.write_connection_file(cf, **sample_info) |
| 81 |
json_info = connect.get_connection_info(cf) |
| 82 |
info = connect.get_connection_info(cf, unpack=True) |
| 83 |
assert isinstance(json_info, str) |
| 84 |
|
| 85 |
sub_info = {k: v for k, v in info.items() if k in sample_info} |
| 86 |
assert sub_info == sample_info |
| 87 |
|
| 88 |
info2 = json.loads(json_info) |
| 89 |
info2["key"] = info2["key"].encode("utf-8") |
| 90 |
sub_info2 = {k: v for k, v in info.items() if k in sample_info} |
| 91 |
assert sub_info2 == sample_info |
| 92 |
|
| 93 |
|
| 94 |
def test_port_bind_failure_raises(request): |
| 95 |
cfg = Config() |
| 96 |
with TemporaryWorkingDirectory() as d: |
| 97 |
cfg.ProfileDir.location = d |
| 98 |
cf = "kernel.json" |
| 99 |
app = DummyKernelApp(config=cfg, connection_file=cf) |
| 100 |
request.addfinalizer(app.close) |
| 101 |
app.initialize() |
| 102 |
with patch.object(app, "_try_bind_socket") as mock_try_bind: |
| 103 |
mock_try_bind.side_effect = zmq.ZMQError(-100, "fails for unknown error types") |
| 104 |
with pytest.raises(zmq.ZMQError): |
| 105 |
app.init_sockets() |
| 106 |
assert mock_try_bind.call_count == 1 |
| 107 |
|
| 108 |
|
| 109 |
@no_type_check |
| 110 |
def test_port_bind_failure_recovery(request): |
| 111 |
try: |
| 112 |
errno.WSAEADDRINUSE |
| 113 |
except AttributeError: |
| 114 |
# Fake windows address in-use code |
| 115 |
p = patch.object(errno, "WSAEADDRINUSE", 12345, create=True) |
| 116 |
p.start() |
| 117 |
request.addfinalizer(p.stop) |
| 118 |
|
| 119 |
cfg = Config() |
| 120 |
with TemporaryWorkingDirectory() as d: |
| 121 |
cfg.ProfileDir.location = d |
| 122 |
cf = "kernel.json" |
| 123 |
app = DummyKernelApp(config=cfg, connection_file=cf) |
| 124 |
request.addfinalizer(app.close) |
| 125 |
app.initialize() |
| 126 |
with patch.object(app, "_try_bind_socket") as mock_try_bind: |
| 127 |
mock_try_bind.side_effect = [ |
| 128 |
zmq.ZMQError(errno.EADDRINUSE, "fails for non-bind unix"), |
| 129 |
zmq.ZMQError(errno.WSAEADDRINUSE, "fails for non-bind windows"), |
| 130 |
] + [0] * 100 |
| 131 |
# Shouldn't raise anything as retries will kick in |
| 132 |
app.init_sockets() |
| 133 |
|
| 134 |
|
| 135 |
def test_port_bind_failure_gives_up_retries(request): |
| 136 |
cfg = Config() |
| 137 |
with TemporaryWorkingDirectory() as d: |
| 138 |
cfg.ProfileDir.location = d |
| 139 |
cf = "kernel.json" |
| 140 |
app = DummyKernelApp(config=cfg, connection_file=cf) |
| 141 |
request.addfinalizer(app.close) |
| 142 |
app.initialize() |
| 143 |
with patch.object(app, "_try_bind_socket") as mock_try_bind: |
| 144 |
mock_try_bind.side_effect = zmq.ZMQError(errno.EADDRINUSE, "fails for non-bind") |
| 145 |
with pytest.raises(zmq.ZMQError): |
| 146 |
app.init_sockets() |
| 147 |
assert mock_try_bind.call_count == 100 |