2013-08-21 12:43:40 +08:00
|
|
|
"""Base class for notebook tests."""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import time
|
2013-08-24 01:10:24 +08:00
|
|
|
import requests
|
2013-10-09 07:09:27 +08:00
|
|
|
from contextlib import contextmanager
|
2013-12-10 04:33:03 +08:00
|
|
|
from subprocess import Popen, STDOUT
|
2013-08-21 12:43:40 +08:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2013-12-10 04:33:03 +08:00
|
|
|
import nose
|
|
|
|
|
2013-08-21 12:43:40 +08:00
|
|
|
from IPython.utils.tempdir import TemporaryDirectory
|
|
|
|
|
2014-01-09 07:49:11 +08:00
|
|
|
MAX_WAITTIME = 30 # seconds to wait for notebook server to start
|
|
|
|
POLL_INTERVAL = 0.1 # time between attempts
|
|
|
|
|
2013-08-21 12:43:40 +08:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2013-08-27 02:18:34 +08:00
|
|
|
port = 12341
|
2013-08-21 12:43:40 +08:00
|
|
|
|
2013-08-23 19:16:05 +08:00
|
|
|
@classmethod
|
|
|
|
def wait_until_alive(cls):
|
|
|
|
"""Wait for the server to be alive"""
|
|
|
|
url = 'http://localhost:%i/api/notebooks' % cls.port
|
2014-01-09 07:49:11 +08:00
|
|
|
for _ in range(int(MAX_WAITTIME/POLL_INTERVAL)):
|
2013-08-24 01:10:24 +08:00
|
|
|
try:
|
2013-08-23 19:16:05 +08:00
|
|
|
requests.get(url)
|
2013-08-24 01:10:24 +08:00
|
|
|
except requests.exceptions.ConnectionError:
|
2014-01-09 06:08:45 +08:00
|
|
|
if cls.notebook.poll() is not None:
|
|
|
|
raise RuntimeError("The notebook server exited with status %s" \
|
|
|
|
% cls.notebook.poll())
|
2014-01-09 07:49:11 +08:00
|
|
|
time.sleep(POLL_INTERVAL)
|
2013-08-23 19:16:05 +08:00
|
|
|
else:
|
2014-01-09 06:08:45 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
raise TimeoutError("The notebook server didn't start up correctly.")
|
2013-08-23 19:16:05 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def wait_until_dead(cls):
|
2014-01-09 06:26:48 +08:00
|
|
|
"""Wait for the server process to terminate after shutdown"""
|
2014-01-09 07:49:11 +08:00
|
|
|
for _ in range(int(MAX_WAITTIME/POLL_INTERVAL)):
|
2014-01-09 06:26:48 +08:00
|
|
|
if cls.notebook.poll() is not None:
|
|
|
|
return
|
2014-01-09 07:49:11 +08:00
|
|
|
time.sleep(POLL_INTERVAL)
|
2013-08-23 19:16:05 +08:00
|
|
|
|
2014-01-09 06:08:45 +08:00
|
|
|
raise TimeoutError("Undead notebook server")
|
|
|
|
|
2013-08-23 19:16:05 +08:00
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
cls.ipython_dir = TemporaryDirectory()
|
|
|
|
cls.notebook_dir = TemporaryDirectory()
|
2013-08-21 12:43:40 +08:00
|
|
|
notebook_args = [
|
|
|
|
sys.executable, '-c',
|
|
|
|
'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()',
|
2013-08-23 19:16:05 +08:00
|
|
|
'--port=%d' % cls.port,
|
2014-01-09 07:49:11 +08:00
|
|
|
'--port-retries=0', # Don't try any other ports
|
2013-08-21 12:43:40 +08:00
|
|
|
'--no-browser',
|
2013-08-23 19:16:05 +08:00
|
|
|
'--ipython-dir=%s' % cls.ipython_dir.name,
|
2013-10-23 08:26:39 +08:00
|
|
|
'--notebook-dir=%s' % cls.notebook_dir.name,
|
|
|
|
]
|
|
|
|
cls.notebook = Popen(notebook_args,
|
2013-12-14 07:05:58 +08:00
|
|
|
stdout=nose.iptest_stdstreams_fileno(),
|
2013-12-10 04:33:03 +08:00
|
|
|
stderr=STDOUT,
|
2013-10-23 08:26:39 +08:00
|
|
|
)
|
2013-08-23 19:16:05 +08:00
|
|
|
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
|
2013-10-09 07:09:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2013-10-09 07:50:35 +08:00
|
|
|
def assert_http_error(status, msg=None):
|
2013-10-09 07:09:27 +08:00
|
|
|
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)
|
2013-10-09 07:50:35 +08:00
|
|
|
if msg:
|
|
|
|
assert msg in str(e), e
|
2013-10-09 07:09:27 +08:00
|
|
|
else:
|
|
|
|
assert False, "Expected HTTP error status"
|