Show More
@@ -1,96 +1,92 b'' | |||
|
1 | 1 | """Base class for notebook tests.""" |
|
2 | 2 | |
|
3 | 3 | import sys |
|
4 | 4 | import time |
|
5 | 5 | import requests |
|
6 | 6 | from contextlib import contextmanager |
|
7 | 7 | from subprocess import Popen, STDOUT |
|
8 | 8 | from unittest import TestCase |
|
9 | 9 | |
|
10 | 10 | import nose |
|
11 | 11 | |
|
12 | 12 | from IPython.utils.tempdir import TemporaryDirectory |
|
13 | 13 | |
|
14 | 14 | class NotebookTestBase(TestCase): |
|
15 | 15 | """A base class for tests that need a running notebook. |
|
16 | 16 | |
|
17 | 17 | This creates an empty profile in a temp ipython_dir |
|
18 | 18 | and then starts the notebook server with a separate temp notebook_dir. |
|
19 | 19 | """ |
|
20 | 20 | |
|
21 | 21 | port = 12341 |
|
22 | 22 | |
|
23 | 23 | @classmethod |
|
24 | 24 | def wait_until_alive(cls): |
|
25 | 25 | """Wait for the server to be alive""" |
|
26 | 26 | url = 'http://localhost:%i/api/notebooks' % cls.port |
|
27 | 27 | for _ in range(300): |
|
28 | 28 | try: |
|
29 | 29 | requests.get(url) |
|
30 | 30 | except requests.exceptions.ConnectionError: |
|
31 | 31 | if cls.notebook.poll() is not None: |
|
32 | 32 | raise RuntimeError("The notebook server exited with status %s" \ |
|
33 | 33 | % cls.notebook.poll()) |
|
34 | 34 | time.sleep(.1) |
|
35 | 35 | else: |
|
36 | 36 | return |
|
37 | 37 | |
|
38 | 38 | raise TimeoutError("The notebook server didn't start up correctly.") |
|
39 | 39 | |
|
40 | 40 | @classmethod |
|
41 | 41 | def wait_until_dead(cls): |
|
42 |
"""Wait for the server |
|
|
43 | url = 'http://localhost:%i/api/notebooks' % cls.port | |
|
42 | """Wait for the server process to terminate after shutdown""" | |
|
44 | 43 | for _ in range(300): |
|
45 | try: | |
|
46 |
re |
|
|
47 | except requests.exceptions.ConnectionError: | |
|
48 | break | |
|
49 | else: | |
|
44 | if cls.notebook.poll() is not None: | |
|
45 | return | |
|
50 | 46 |
|
|
51 | 47 | |
|
52 | 48 | raise TimeoutError("Undead notebook server") |
|
53 | 49 | |
|
54 | 50 | @classmethod |
|
55 | 51 | def setup_class(cls): |
|
56 | 52 | cls.ipython_dir = TemporaryDirectory() |
|
57 | 53 | cls.notebook_dir = TemporaryDirectory() |
|
58 | 54 | notebook_args = [ |
|
59 | 55 | sys.executable, '-c', |
|
60 | 56 | 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()', |
|
61 | 57 | '--port=%d' % cls.port, |
|
62 | 58 | '--port-retries=0', |
|
63 | 59 | '--no-browser', |
|
64 | 60 | '--ipython-dir=%s' % cls.ipython_dir.name, |
|
65 | 61 | '--notebook-dir=%s' % cls.notebook_dir.name, |
|
66 | 62 | ] |
|
67 | 63 | cls.notebook = Popen(notebook_args, |
|
68 | 64 | stdout=nose.iptest_stdstreams_fileno(), |
|
69 | 65 | stderr=STDOUT, |
|
70 | 66 | ) |
|
71 | 67 | cls.wait_until_alive() |
|
72 | 68 | |
|
73 | 69 | @classmethod |
|
74 | 70 | def teardown_class(cls): |
|
75 | 71 | cls.notebook.terminate() |
|
76 | 72 | cls.ipython_dir.cleanup() |
|
77 | 73 | cls.notebook_dir.cleanup() |
|
78 | 74 | cls.wait_until_dead() |
|
79 | 75 | |
|
80 | 76 | @classmethod |
|
81 | 77 | def base_url(cls): |
|
82 | 78 | return 'http://localhost:%i/' % cls.port |
|
83 | 79 | |
|
84 | 80 | |
|
85 | 81 | @contextmanager |
|
86 | 82 | def assert_http_error(status, msg=None): |
|
87 | 83 | try: |
|
88 | 84 | yield |
|
89 | 85 | except requests.HTTPError as e: |
|
90 | 86 | real_status = e.response.status_code |
|
91 | 87 | assert real_status == status, \ |
|
92 | 88 | "Expected status %d, got %d" % (real_status, status) |
|
93 | 89 | if msg: |
|
94 | 90 | assert msg in str(e), e |
|
95 | 91 | else: |
|
96 | 92 | assert False, "Expected HTTP error status" No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now