Show More
@@ -1,89 +1,88 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 | while True: |
|
28 | 28 | try: |
|
29 | 29 | requests.get(url) |
|
30 | 30 | except requests.exceptions.ConnectionError: |
|
31 | 31 | time.sleep(.1) |
|
32 | 32 | else: |
|
33 | 33 | break |
|
34 | 34 | |
|
35 | 35 | @classmethod |
|
36 | 36 | def wait_until_dead(cls): |
|
37 | 37 | """Wait for the server to stop getting requests after shutdown""" |
|
38 | 38 | url = 'http://localhost:%i/api/notebooks' % cls.port |
|
39 | 39 | while True: |
|
40 | 40 | try: |
|
41 | 41 | requests.get(url) |
|
42 | 42 | except requests.exceptions.ConnectionError: |
|
43 | 43 | break |
|
44 | 44 | else: |
|
45 | 45 | time.sleep(.1) |
|
46 | 46 | |
|
47 | 47 | @classmethod |
|
48 | 48 | def setup_class(cls): |
|
49 | 49 | cls.ipython_dir = TemporaryDirectory() |
|
50 | 50 | cls.notebook_dir = TemporaryDirectory() |
|
51 | 51 | notebook_args = [ |
|
52 | 52 | sys.executable, '-c', |
|
53 | 53 | 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()', |
|
54 | 54 | '--port=%d' % cls.port, |
|
55 | 55 | '--no-browser', |
|
56 | 56 | '--ipython-dir=%s' % cls.ipython_dir.name, |
|
57 | 57 | '--notebook-dir=%s' % cls.notebook_dir.name, |
|
58 | 58 | ] |
|
59 | 59 | cls.notebook = Popen(notebook_args, |
|
60 |
stdout=nose.ip |
|
|
60 | stdout=nose.iptest_stdstreams_fileno(), | |
|
61 | 61 | stderr=STDOUT, |
|
62 | 62 | ) |
|
63 | nose.ipy_stream_capturer.ensure_started() | |
|
64 | 63 | cls.wait_until_alive() |
|
65 | 64 | |
|
66 | 65 | @classmethod |
|
67 | 66 | def teardown_class(cls): |
|
68 | 67 | cls.notebook.terminate() |
|
69 | 68 | cls.ipython_dir.cleanup() |
|
70 | 69 | cls.notebook_dir.cleanup() |
|
71 | 70 | cls.wait_until_dead() |
|
72 | 71 | |
|
73 | 72 | @classmethod |
|
74 | 73 | def base_url(cls): |
|
75 | 74 | return 'http://localhost:%i/' % cls.port |
|
76 | 75 | |
|
77 | 76 | |
|
78 | 77 | @contextmanager |
|
79 | 78 | def assert_http_error(status, msg=None): |
|
80 | 79 | try: |
|
81 | 80 | yield |
|
82 | 81 | except requests.HTTPError as e: |
|
83 | 82 | real_status = e.response.status_code |
|
84 | 83 | assert real_status == status, \ |
|
85 | 84 | "Expected status %d, got %d" % (real_status, status) |
|
86 | 85 | if msg: |
|
87 | 86 | assert msg in str(e), e |
|
88 | 87 | else: |
|
89 | 88 | 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