|
1 | 1 | import os |
2 | 2 | from http.client import HTTPConnection |
3 | 3 | import subprocess |
4 | | -from time import sleep |
5 | | -from urllib.parse import quote |
| 4 | +import time |
| 5 | +from urllib.parse import urlencode |
6 | 6 | from uuid import uuid4 |
7 | 7 | import pytest |
8 | 8 |
|
|
12 | 12 |
|
13 | 13 |
|
14 | 14 | def request_api(params, host='localhost'): |
| 15 | + query_args = {"token": "secret"} |
| 16 | + query_args.update(params) |
| 17 | + query = urlencode(query_args) |
| 18 | + url = f'/git-pull/api?{query}' |
15 | 19 | h = HTTPConnection(host, PORT, 10) |
16 | | - query = '&'.join('{}={}'.format(k, quote(v)) for (k, v) in params.items()) |
17 | | - url = '/git-pull/api?token=secret&{}'.format(query) |
18 | 20 | h.request('GET', url) |
19 | 21 | return h.getresponse() |
20 | 22 |
|
| 23 | +def wait_for_server(host='localhost', port=PORT, timeout=10): |
| 24 | + """Wait for an HTTP server to be responsive""" |
| 25 | + t = 0.1 |
| 26 | + deadline = time.monotonic() + timeout |
| 27 | + while time.monotonic() < deadline: |
| 28 | + try: |
| 29 | + h = HTTPConnection(host, port, 10) |
| 30 | + h.request("GET", "/") |
| 31 | + r = h.getresponse() |
| 32 | + except Exception as e: |
| 33 | + print(f"Server not ready: {e}") |
| 34 | + time.sleep(t) |
| 35 | + t *= 2 |
| 36 | + t = min(t, 1) |
| 37 | + else: |
| 38 | + # success |
| 39 | + return |
| 40 | + assert False, f"Server never showed up at http://{host}:{port}" |
21 | 41 |
|
22 | | -class TestNbGitPullerApi: |
23 | 42 |
|
24 | | - def setup(self): |
25 | | - self.jupyter_proc = None |
| 43 | +@pytest.fixture |
| 44 | +def jupyterdir(tmpdir): |
| 45 | + path = tmpdir.join("jupyter") |
| 46 | + path.mkdir() |
| 47 | + return str(path) |
26 | 48 |
|
27 | | - def teardown(self): |
28 | | - if self.jupyter_proc: |
29 | | - self.jupyter_proc.kill() |
30 | 49 |
|
31 | | - def start_jupyter(self, jupyterdir, extraenv, backend_type): |
32 | | - env = os.environ.copy() |
33 | | - env.update(extraenv) |
34 | | - if "server" in backend_type: |
35 | | - command = [ |
36 | | - 'jupyter-server', |
37 | | - '--NotebookApp.token=secret', |
38 | | - '--port={}'.format(PORT), |
39 | | - ] |
40 | | - else: |
41 | | - command = [ |
42 | | - 'jupyter-notebook', |
43 | | - '--no-browser', |
44 | | - '--NotebookApp.token=secret', |
45 | | - '--port={}'.format(PORT), |
46 | | - ] |
47 | | - self.jupyter_proc = subprocess.Popen(command, cwd=jupyterdir, env=env) |
48 | | - sleep(2) |
49 | | - |
50 | | - @pytest.mark.parametrize( |
51 | | - "backend_type", |
52 | | - [ |
53 | | - ("jupyter-server"), |
54 | | - ("jupyter-notebook"), |
55 | | - ], |
56 | | - ) |
57 | | - def test_clone_default(self, tmpdir, backend_type): |
58 | | - """ |
59 | | - Tests use of 'repo' and 'branch' parameters. |
60 | | - """ |
61 | | - jupyterdir = str(tmpdir) |
62 | | - self.start_jupyter(jupyterdir, {}, backend_type) |
63 | | - |
64 | | - with Remote() as remote, Pusher(remote) as pusher: |
65 | | - pusher.push_file('README.md', 'Testing some content') |
66 | | - print(f'path: {remote.path}') |
67 | | - params = { |
68 | | - 'repo': remote.path, |
69 | | - 'branch': 'master', |
70 | | - } |
71 | | - r = request_api(params) |
72 | | - assert r.code == 200 |
73 | | - s = r.read().decode() |
74 | | - print(s) |
75 | | - target_path = os.path.join(jupyterdir, os.path.basename(remote.path)) |
76 | | - assert '--branch master' in s |
77 | | - assert f"Cloning into '{target_path}" in s |
78 | | - assert os.path.isdir(os.path.join(target_path, '.git')) |
79 | | - |
80 | | - @pytest.mark.parametrize( |
81 | | - "backend_type", |
82 | | - [ |
83 | | - ("jupyter-server"), |
84 | | - ("jupyter-notebook"), |
85 | | - ], |
86 | | - ) |
87 | | - def test_clone_targetpath(self, tmpdir, backend_type): |
88 | | - """ |
89 | | - Tests use of 'targetpath' parameter. |
90 | | - """ |
91 | | - jupyterdir = str(tmpdir) |
92 | | - target = str(uuid4()) |
93 | | - self.start_jupyter(jupyterdir, {}, backend_type) |
94 | | - with Remote() as remote, Pusher(remote) as pusher: |
95 | | - pusher.push_file('README.md', 'Testing some content') |
96 | | - params = { |
97 | | - 'repo': remote.path, |
98 | | - 'branch': 'master', |
99 | | - 'targetpath': target, |
100 | | - } |
101 | | - r = request_api(params) |
102 | | - assert r.code == 200 |
103 | | - s = r.read().decode() |
104 | | - print(s) |
105 | | - target_path = os.path.join(jupyterdir, target) |
106 | | - assert f"Cloning into '{target_path}" in s |
107 | | - assert os.path.isdir(os.path.join(target_path, '.git')) |
108 | | - |
109 | | - @pytest.mark.parametrize( |
110 | | - "backend_type", |
111 | | - [ |
112 | | - ("jupyter-server"), |
113 | | - ("jupyter-notebook"), |
114 | | - ], |
115 | | - ) |
116 | | - def test_clone_parenttargetpath(self, tmpdir, backend_type): |
117 | | - """ |
118 | | - Tests use of the NBGITPULLER_PARENTPATH environment variable. |
119 | | - """ |
120 | | - jupyterdir = str(tmpdir) |
121 | | - parent = str(uuid4()) |
122 | | - target = str(uuid4()) |
123 | | - self.start_jupyter(jupyterdir, {'NBGITPULLER_PARENTPATH': parent}, backend_type) |
124 | | - |
125 | | - with Remote() as remote, Pusher(remote) as pusher: |
126 | | - pusher.push_file('README.md', 'Testing some content') |
127 | | - params = { |
128 | | - 'repo': remote.path, |
129 | | - 'branch': 'master', |
130 | | - 'targetpath': target, |
131 | | - } |
132 | | - r = request_api(params) |
133 | | - assert r.code == 200 |
134 | | - s = r.read().decode() |
135 | | - print(s) |
136 | | - target_path = os.path.join(jupyterdir, parent, target) |
137 | | - assert f"Cloning into '{target_path}" in s |
138 | | - assert os.path.isdir(os.path.join(target_path, '.git')) |
| 50 | +@pytest.fixture(params=["jupyter-server", "jupyter-notebook"]) |
| 51 | +def jupyter_server(request, tmpdir, jupyterdir): |
| 52 | + # allow passing extra_env via @pytest.mark.jupyter_server(extra_env={"key": "value"}) |
| 53 | + if "jupyter_server" in request.keywords: |
| 54 | + extra_env = request.keywords["jupyter_server"].kwargs.get("extra_env") |
| 55 | + else: |
| 56 | + extra_env = None |
| 57 | + |
| 58 | + backend_type = request.param |
| 59 | + |
| 60 | + env = os.environ.copy() |
| 61 | + # avoid interacting with user configuration, state |
| 62 | + env["JUPYTER_CONFIG_DIR"] = str(tmpdir / "dotjupyter") |
| 63 | + env["JUPYTER_RUNTIME_DIR"] = str(tmpdir / "runjupyter") |
| 64 | + |
| 65 | + if extra_env: |
| 66 | + env.update(extra_env) |
| 67 | + |
| 68 | + if backend_type == "jupyter-server": |
| 69 | + command = [ |
| 70 | + 'jupyter-server', |
| 71 | + '--ServerApp.token=secret', |
| 72 | + '--port={}'.format(PORT), |
| 73 | + ] |
| 74 | + extension_command = ["jupyter", "server", "extension"] |
| 75 | + elif backend_type == "jupyter-notebook": |
| 76 | + command = [ |
| 77 | + 'jupyter-notebook', |
| 78 | + '--no-browser', |
| 79 | + '--NotebookApp.token=secret', |
| 80 | + '--port={}'.format(PORT), |
| 81 | + ] |
| 82 | + extension_command = ["jupyter", "serverextension"] |
| 83 | + else: |
| 84 | + raise ValueError( |
| 85 | + f"backend_type must be 'jupyter-server' or 'jupyter-notebook' not {backend_type!r}" |
| 86 | + ) |
| 87 | + |
| 88 | + # enable the extension |
| 89 | + subprocess.check_call(extension_command + ["enable", "nbgitpuller"], env=env) |
| 90 | + |
| 91 | + # launch the server |
| 92 | + jupyter_proc = subprocess.Popen(command, cwd=jupyterdir, env=env) |
| 93 | + wait_for_server() |
| 94 | + |
| 95 | + with jupyter_proc: |
| 96 | + yield jupyter_proc |
| 97 | + jupyter_proc.terminate() |
| 98 | + |
| 99 | + |
| 100 | +def test_clone_default(jupyterdir, jupyter_server): |
| 101 | + """ |
| 102 | + Tests use of 'repo' and 'branch' parameters. |
| 103 | + """ |
| 104 | + with Remote() as remote, Pusher(remote) as pusher: |
| 105 | + pusher.push_file('README.md', 'Testing some content') |
| 106 | + print(f'path: {remote.path}') |
| 107 | + params = { |
| 108 | + 'repo': remote.path, |
| 109 | + 'branch': 'master', |
| 110 | + } |
| 111 | + r = request_api(params) |
| 112 | + assert r.code == 200 |
| 113 | + s = r.read().decode() |
| 114 | + print(s) |
| 115 | + target_path = os.path.join(jupyterdir, os.path.basename(remote.path)) |
| 116 | + assert '--branch master' in s |
| 117 | + assert f"Cloning into '{target_path}" in s |
| 118 | + assert os.path.isdir(os.path.join(target_path, '.git')) |
| 119 | + |
| 120 | + |
| 121 | +def test_clone_targetpath(jupyterdir, jupyter_server): |
| 122 | + """ |
| 123 | + Tests use of 'targetpath' parameter. |
| 124 | + """ |
| 125 | + target = str(uuid4()) |
| 126 | + with Remote() as remote, Pusher(remote) as pusher: |
| 127 | + pusher.push_file('README.md', 'Testing some content') |
| 128 | + params = { |
| 129 | + 'repo': remote.path, |
| 130 | + 'branch': 'master', |
| 131 | + 'targetpath': target, |
| 132 | + } |
| 133 | + r = request_api(params) |
| 134 | + assert r.code == 200 |
| 135 | + s = r.read().decode() |
| 136 | + print(s) |
| 137 | + target_path = os.path.join(jupyterdir, target) |
| 138 | + assert f"Cloning into '{target_path}" in s |
| 139 | + assert os.path.isdir(os.path.join(target_path, '.git')) |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.jupyter_server(extra_env={'NBGITPULLER_PARENTPATH': "parent"}) |
| 143 | +def test_clone_parenttargetpath(jupyterdir, jupyter_server): |
| 144 | + """ |
| 145 | + Tests use of the NBGITPULLER_PARENTPATH environment variable. |
| 146 | + """ |
| 147 | + parent = "parent" |
| 148 | + target = str(uuid4()) |
| 149 | + |
| 150 | + with Remote() as remote, Pusher(remote) as pusher: |
| 151 | + pusher.push_file('README.md', 'Testing some content') |
| 152 | + params = { |
| 153 | + 'repo': remote.path, |
| 154 | + 'branch': 'master', |
| 155 | + 'targetpath': target, |
| 156 | + } |
| 157 | + r = request_api(params) |
| 158 | + assert r.code == 200 |
| 159 | + s = r.read().decode() |
| 160 | + print(s) |
| 161 | + target_path = os.path.join(jupyterdir, parent, target) |
| 162 | + assert f"Cloning into '{target_path}" in s |
| 163 | + assert os.path.isdir(os.path.join(target_path, '.git')) |
0 commit comments