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