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