launchnotebook.py
87 lines
| 2.5 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r13038 | """Base class for notebook tests.""" | |
MinRK
|
r13180 | import os | |
Brian E. Granger
|
r13038 | import sys | |
import time | |||
Zachary Sailer
|
r13045 | import requests | |
Thomas Kluyver
|
r13099 | from contextlib import contextmanager | |
Brian E. Granger
|
r13038 | from subprocess import Popen, PIPE | |
from unittest import TestCase | |||
from IPython.utils.tempdir import TemporaryDirectory | |||
class NotebookTestBase(TestCase): | |||
"""A base class for tests that need a running notebook. | |||
This creates an empty profile in a temp ipython_dir | |||
and then starts the notebook server with a separate temp notebook_dir. | |||
""" | |||
Zachary Sailer
|
r13046 | port = 12341 | |
Brian E. Granger
|
r13038 | ||
MinRK
|
r13049 | @classmethod | |
def wait_until_alive(cls): | |||
"""Wait for the server to be alive""" | |||
url = 'http://localhost:%i/api/notebooks' % cls.port | |||
Zachary Sailer
|
r13045 | while True: | |
try: | |||
MinRK
|
r13049 | requests.get(url) | |
Zachary Sailer
|
r13045 | except requests.exceptions.ConnectionError: | |
MinRK
|
r13049 | time.sleep(.1) | |
else: | |||
break | |||
@classmethod | |||
def wait_until_dead(cls): | |||
"""Wait for the server to stop getting requests after shutdown""" | |||
url = 'http://localhost:%i/api/notebooks' % cls.port | |||
Zachary Sailer
|
r13045 | while True: | |
try: | |||
MinRK
|
r13049 | requests.get(url) | |
Zachary Sailer
|
r13045 | except requests.exceptions.ConnectionError: | |
break | |||
MinRK
|
r13049 | else: | |
time.sleep(.1) | |||
@classmethod | |||
def setup_class(cls): | |||
cls.ipython_dir = TemporaryDirectory() | |||
cls.notebook_dir = TemporaryDirectory() | |||
Brian E. Granger
|
r13038 | notebook_args = [ | |
sys.executable, '-c', | |||
'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()', | |||
MinRK
|
r13049 | '--port=%d' % cls.port, | |
Brian E. Granger
|
r13038 | '--no-browser', | |
MinRK
|
r13049 | '--ipython-dir=%s' % cls.ipython_dir.name, | |
MinRK
|
r13180 | '--notebook-dir=%s' % cls.notebook_dir.name, | |
] | |||
devnull = open(os.devnull, 'w') | |||
cls.notebook = Popen(notebook_args, | |||
stdout=devnull, | |||
stderr=devnull, | |||
) | |||
MinRK
|
r13049 | cls.wait_until_alive() | |
@classmethod | |||
def teardown_class(cls): | |||
cls.notebook.terminate() | |||
cls.ipython_dir.cleanup() | |||
cls.notebook_dir.cleanup() | |||
cls.wait_until_dead() | |||
@classmethod | |||
def base_url(cls): | |||
return 'http://localhost:%i/' % cls.port | |||
Thomas Kluyver
|
r13099 | ||
@contextmanager | |||
Thomas Kluyver
|
r13105 | def assert_http_error(status, msg=None): | |
Thomas Kluyver
|
r13099 | try: | |
yield | |||
except requests.HTTPError as e: | |||
real_status = e.response.status_code | |||
assert real_status == status, \ | |||
"Expected status %d, got %d" % (real_status, status) | |||
Thomas Kluyver
|
r13105 | if msg: | |
assert msg in str(e), e | |||
Thomas Kluyver
|
r13099 | else: | |
assert False, "Expected HTTP error status" |