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