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