2018-07-20 09:45:01 +00:00
|
|
|
"""
|
2018-07-20 11:19:15 +00:00
|
|
|
Test if the explict hostname is supplied correctly to the container
|
2018-07-20 09:45:01 +00:00
|
|
|
"""
|
|
|
|
import requests
|
|
|
|
import time
|
|
|
|
from repo2docker.app import Repo2Docker
|
|
|
|
|
2018-12-21 18:55:49 +00:00
|
|
|
|
|
|
|
# Minimal Dockerfile to make build as fast as possible
|
|
|
|
DOCKER_FILE = """
|
|
|
|
FROM python:3.7-slim
|
|
|
|
# install the notebook package
|
|
|
|
RUN pip install --no-cache --upgrade pip && \
|
|
|
|
pip install --no-cache notebook
|
|
|
|
|
|
|
|
# create user with a home directory
|
|
|
|
ARG NB_USER
|
|
|
|
ARG NB_UID
|
|
|
|
ENV USER ${NB_USER}
|
|
|
|
ENV HOME /home/${NB_USER}
|
|
|
|
|
|
|
|
RUN adduser --disabled-password \
|
|
|
|
--gecos "Default user" \
|
|
|
|
--uid ${NB_UID} \
|
|
|
|
${NB_USER}
|
|
|
|
WORKDIR ${HOME}
|
|
|
|
USER ${NB_USER}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-07-31 09:28:20 +00:00
|
|
|
def test_connect_url(tmpdir):
|
2018-07-20 09:45:01 +00:00
|
|
|
tmpdir.chdir()
|
2018-12-21 18:55:49 +00:00
|
|
|
p = tmpdir.join("Dockerfile")
|
|
|
|
p.write(DOCKER_FILE)
|
2018-07-20 09:45:01 +00:00
|
|
|
|
2018-12-21 18:55:49 +00:00
|
|
|
# we set run=False so that we can start the container ourselves and
|
|
|
|
# get a handle to the container, used to inspect the logs
|
2018-12-17 12:11:45 +00:00
|
|
|
app = Repo2Docker(repo=str(tmpdir), run=False)
|
|
|
|
app.initialize()
|
2018-12-21 18:55:49 +00:00
|
|
|
app.start()
|
2018-07-20 09:45:01 +00:00
|
|
|
container = app.start_container()
|
2018-12-21 18:55:49 +00:00
|
|
|
|
2018-07-31 09:28:20 +00:00
|
|
|
container_url = 'http://{}:{}/api'.format(app.hostname, app.port)
|
|
|
|
expected_url = 'http://{}:{}'.format(app.hostname, app.port)
|
|
|
|
|
2018-07-20 09:45:01 +00:00
|
|
|
# wait a bit for the container to be ready
|
|
|
|
# give the container a chance to start
|
|
|
|
time.sleep(1)
|
2018-07-31 09:28:20 +00:00
|
|
|
|
2018-07-20 09:45:01 +00:00
|
|
|
try:
|
|
|
|
# try a few times to connect
|
|
|
|
success = False
|
|
|
|
for i in range(1, 4):
|
|
|
|
container.reload()
|
|
|
|
assert container.status == 'running'
|
2018-07-31 09:28:20 +00:00
|
|
|
if expected_url not in container.logs().decode("utf8"):
|
|
|
|
time.sleep(i * 3)
|
|
|
|
continue
|
2018-07-20 09:45:01 +00:00
|
|
|
try:
|
|
|
|
info = requests.get(container_url).json()
|
|
|
|
except Exception as e:
|
|
|
|
print("Error: %s" % e)
|
|
|
|
time.sleep(i * 3)
|
|
|
|
else:
|
|
|
|
print(info)
|
|
|
|
success = True
|
|
|
|
break
|
|
|
|
assert success, "Notebook never started in %s" % container
|
|
|
|
finally:
|
|
|
|
# stop the container
|
|
|
|
container.stop()
|
|
|
|
app.wait_for_container(container)
|