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