Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FROM python:2.7
MAINTAINER Joffrey F <joffrey@docker.com>
ADD . /home/docker-py

RUN mkdir /home/docker-py
WORKDIR /home/docker-py

ADD requirements.txt /home/docker-py/requirements.txt
RUN pip install -r requirements.txt

ADD test-requirements.txt /home/docker-py/test-requirements.txt
RUN pip install -r test-requirements.txt

ADD . /home/docker-py
RUN pip install .
10 changes: 9 additions & 1 deletion Dockerfile-py3
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FROM python:3.4
MAINTAINER Joffrey F <joffrey@docker.com>
ADD . /home/docker-py

RUN mkdir /home/docker-py
WORKDIR /home/docker-py

ADD requirements.txt /home/docker-py/requirements.txt
RUN pip install -r requirements.txt

ADD test-requirements.txt /home/docker-py/test-requirements.txt
RUN pip install -r test-requirements.txt

ADD . /home/docker-py
RUN pip install .
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ unit-test-py3: build-py3
docker run docker-py3 py.test tests/test.py tests/utils_test.py

integration-test: build
docker run -e NOT_ON_HOST=true -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py py.test tests/integration_test.py
docker run -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py py.test -rxs tests/integration_test.py

integration-test-py3: build-py3
docker run -e NOT_ON_HOST=true -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py3 py.test tests/integration_test.py
docker run -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py3 py.test -rxs tests/integration_test.py
124 changes: 79 additions & 45 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import warnings

import docker
from docker.utils import kwargs_from_env
import six

from six.moves import BaseHTTPServer
Expand All @@ -36,15 +37,41 @@
from .test import Cleanup
from docker.errors import APIError

import pytest

# FIXME: missing tests for
# export; history; insert; port; push; tag; get; load; stats
DEFAULT_BASE_URL = os.environ.get('DOCKER_HOST')
EXEC_DRIVER_IS_NATIVE = True
NOT_ON_HOST = os.environ.get('NOT_ON_HOST', False)

warnings.simplefilter('error')
compare_version = docker.utils.compare_version

EXEC_DRIVER = []


def exec_driver_is_native():
global EXEC_DRIVER
if not EXEC_DRIVER:
c = docker_client()
EXEC_DRIVER = c.info()['ExecutionDriver']
c.close()
return EXEC_DRIVER.startswith('native')


def docker_client(**kwargs):
return docker.Client(**docker_client_kwargs(**kwargs))


def docker_client_kwargs(**kwargs):
client_kwargs = kwargs_from_env(assert_hostname=False)
client_kwargs.update(kwargs)
return client_kwargs


def setup_module():
c = docker_client()
c.pull('busybox')
c.close()


class BaseTestCase(unittest.TestCase):
tmp_imgs = []
Expand All @@ -55,7 +82,7 @@ def setUp(self):
if six.PY2:
self.assertRegex = self.assertRegexpMatches
self.assertCountEqual = self.assertItemsEqual
self.client = docker.Client(base_url=DEFAULT_BASE_URL, timeout=5)
self.client = docker_client(timeout=5)
self.tmp_imgs = []
self.tmp_containers = []
self.tmp_folders = []
Expand Down Expand Up @@ -99,7 +126,7 @@ def runTest(self):

class TestSearch(BaseTestCase):
def runTest(self):
self.client = docker.Client(base_url=DEFAULT_BASE_URL, timeout=10)
self.client = docker_client(timeout=10)
res = self.client.search('busybox')
self.assertTrue(len(res) >= 1)
base_img = [x for x in res if x['name'] == 'busybox']
Expand Down Expand Up @@ -163,6 +190,7 @@ def runTest(self):


class TestCreateContainerWithBinds(BaseTestCase):
@pytest.mark.skipif(True, reason="Doesn't work inside a container - FIXME")
def runTest(self):
mount_dest = '/mnt'
mount_origin = tempfile.mkdtemp()
Expand Down Expand Up @@ -205,6 +233,7 @@ def runTest(self):


class TestCreateContainerWithRoBinds(BaseTestCase):
@pytest.mark.skipif(True, reason="Doesn't work inside a container - FIXME")
def runTest(self):
mount_dest = '/mnt'
mount_origin = tempfile.mkdtemp()
Expand Down Expand Up @@ -278,11 +307,15 @@ def test_invalid_log_driver_raises_exception(self):
)

expected_msg = "logger: no log driver named 'asdf-nope' is registered"
with self.assertRaisesRegexp(APIError, expected_msg):

with pytest.raises(APIError) as excinfo:
# raises an internal server error 500
self.client.start(container)

@unittest.skip("Reason: https://github.com/docker/docker/issues/15633")
assert expected_msg in str(excinfo.value)

@pytest.mark.skipif(True,
reason="https://github.com/docker/docker/issues/15633")
def test_valid_no_log_driver_specified(self):
log_config = docker.utils.LogConfig(
type="",
Expand Down Expand Up @@ -322,9 +355,11 @@ def test_valid_no_config_specified(self):
self.assertEqual(container_log_config['Config'], {})


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestCreateContainerReadOnlyFs(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we don't use the @pytest.mark.skipif decorator here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - we can't evaluate exec_driver_is_native() at script execution time, because the check is done in setup_module() (as per pytest convention), which hasn't run yet.


ctnr = self.client.create_container(
'busybox', ['mkdir', '/shrine'],
host_config=self.client.create_host_config(
Expand Down Expand Up @@ -563,7 +598,7 @@ def runTest(self):
self.assertIn('State', container_info)
state = container_info['State']
self.assertIn('ExitCode', state)
if EXEC_DRIVER_IS_NATIVE:
if exec_driver_is_native():
self.assertNotEqual(state['ExitCode'], 0)
self.assertIn('Running', state)
self.assertEqual(state['Running'], False)
Expand All @@ -581,7 +616,7 @@ def runTest(self):
self.assertIn('State', container_info)
state = container_info['State']
self.assertIn('ExitCode', state)
if EXEC_DRIVER_IS_NATIVE:
if exec_driver_is_native():
self.assertNotEqual(state['ExitCode'], 0)
self.assertIn('Running', state)
self.assertEqual(state['Running'], False)
Expand All @@ -598,7 +633,7 @@ def runTest(self):
self.assertIn('State', container_info)
state = container_info['State']
self.assertIn('ExitCode', state)
if EXEC_DRIVER_IS_NATIVE:
if exec_driver_is_native():
self.assertNotEqual(state['ExitCode'], 0)
self.assertIn('Running', state)
self.assertEqual(state['Running'], False)
Expand All @@ -615,7 +650,7 @@ def runTest(self):
self.assertIn('State', container_info)
state = container_info['State']
self.assertIn('ExitCode', state)
if EXEC_DRIVER_IS_NATIVE:
if exec_driver_is_native():
self.assertNotEqual(state['ExitCode'], 0)
self.assertIn('Running', state)
self.assertEqual(state['Running'], False)
Expand Down Expand Up @@ -861,9 +896,11 @@ def runTest(self):
self.client.remove_container(id, force=True)


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecuteCommand(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')

container = self.client.create_container('busybox', 'cat',
detach=True, stdin_open=True)
id = container['Id']
Expand All @@ -874,13 +911,14 @@ def runTest(self):
self.assertIn('Id', res)

exec_log = self.client.exec_start(res)
expected = b'hello\n' if six.PY3 else 'hello\n'
self.assertEqual(exec_log, expected)
self.assertEqual(exec_log, b'hello\n')


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecuteCommandString(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')

container = self.client.create_container('busybox', 'cat',
detach=True, stdin_open=True)
id = container['Id']
Expand All @@ -891,13 +929,14 @@ def runTest(self):
self.assertIn('Id', res)

exec_log = self.client.exec_start(res)
expected = b'hello world\n' if six.PY3 else 'hello world\n'
self.assertEqual(exec_log, expected)
self.assertEqual(exec_log, b'hello world\n')


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecuteCommandStringAsUser(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')

container = self.client.create_container('busybox', 'cat',
detach=True, stdin_open=True)
id = container['Id']
Expand All @@ -908,13 +947,14 @@ def runTest(self):
self.assertIn('Id', res)

exec_log = self.client.exec_start(res)
expected = b'default' if six.PY3 else 'default\n'
self.assertEqual(exec_log, expected)
self.assertEqual(exec_log, b'default\n')


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecuteCommandStringAsRoot(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')

container = self.client.create_container('busybox', 'cat',
detach=True, stdin_open=True)
id = container['Id']
Expand All @@ -925,13 +965,14 @@ def runTest(self):
self.assertIn('Id', res)

exec_log = self.client.exec_start(res)
expected = b'root' if six.PY3 else 'root\n'
self.assertEqual(exec_log, expected)
self.assertEqual(exec_log, b'root\n')


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecuteCommandStreaming(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')

container = self.client.create_container('busybox', 'cat',
detach=True, stdin_open=True)
id = container['Id']
Expand All @@ -941,16 +982,17 @@ def runTest(self):
exec_id = self.client.exec_create(id, ['echo', 'hello\nworld'])
self.assertIn('Id', exec_id)

res = b'' if six.PY3 else ''
res = b''
for chunk in self.client.exec_start(exec_id, stream=True):
res += chunk
expected = b'hello\nworld\n' if six.PY3 else 'hello\nworld\n'
self.assertEqual(res, expected)
self.assertEqual(res, b'hello\nworld\n')


@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecInspect(BaseTestCase):
def runTest(self):
if not exec_driver_is_native():
pytest.skip('Exec driver not native')

container = self.client.create_container('busybox', 'cat',
detach=True, stdin_open=True)
id = container['Id']
Expand Down Expand Up @@ -1076,7 +1118,7 @@ def runTest(self):
class TestPull(BaseTestCase):
def runTest(self):
self.client.close()
self.client = docker.Client(base_url=DEFAULT_BASE_URL, timeout=10)
self.client = docker_client(timeout=10)
try:
self.client.remove_image('busybox')
except docker.errors.APIError:
Expand All @@ -1093,7 +1135,7 @@ def runTest(self):
class TestPullStream(BaseTestCase):
def runTest(self):
self.client.close()
self.client = docker.Client(base_url=DEFAULT_BASE_URL, timeout=10)
self.client = docker_client(timeout=10)
try:
self.client.remove_image('busybox')
except docker.errors.APIError:
Expand Down Expand Up @@ -1254,7 +1296,6 @@ def runTest(self):
self.tmp_imgs.append(img_id)


@unittest.skipIf(NOT_ON_HOST, 'Tests running inside a container')
class TestImportFromURL(ImportTestCase):
'''Tests downloading an image over HTTP.'''

Expand All @@ -1278,6 +1319,7 @@ def do_GET(self):

server.shutdown()

@pytest.mark.skipif(True, reason="Doesn't work inside a container - FIXME")
def runTest(self):
# The crappy test HTTP server doesn't handle large files well, so use
# a small file.
Expand Down Expand Up @@ -1376,6 +1418,7 @@ def runTest(self):


class TestBuildWithDockerignore(Cleanup, BaseTestCase):
@pytest.mark.skipif(True, reason='Test is brittle - FIXME')
def runTest(self):
if compare_version(self.client._version, '1.8') >= 0:
return
Expand Down Expand Up @@ -1486,7 +1529,7 @@ def runTest(self):

class TestAutoDetectVersion(unittest.TestCase):
def test_client_init(self):
client = docker.Client(base_url=DEFAULT_BASE_URL, version='auto')
client = docker_client(version='auto')
client_version = client._version
api_version = client.version(api_version=False)['ApiVersion']
self.assertEqual(client_version, api_version)
Expand All @@ -1495,15 +1538,15 @@ def test_client_init(self):
client.close()

def test_auto_client(self):
client = docker.AutoVersionClient(base_url=DEFAULT_BASE_URL)
client = docker.AutoVersionClient(**docker_client_kwargs())
client_version = client._version
api_version = client.version(api_version=False)['ApiVersion']
self.assertEqual(client_version, api_version)
api_version_2 = client.version()['ApiVersion']
self.assertEqual(client_version, api_version_2)
client.close()
with self.assertRaises(docker.errors.DockerException):
docker.AutoVersionClient(base_url=DEFAULT_BASE_URL, version='1.11')
docker.AutoVersionClient(**docker_client_kwargs(version='1.11'))


class TestConnectionTimeout(unittest.TestCase):
Expand Down Expand Up @@ -1538,7 +1581,7 @@ def test_resource_warnings(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')

client = docker.Client(base_url=DEFAULT_BASE_URL)
client = docker_client()
client.images()
client.close()
del client
Expand Down Expand Up @@ -1576,12 +1619,3 @@ def test_649(self):
ctnr = self.client.create_container('busybox', ['sleep', '2'])
self.client.start(ctnr)
self.client.stop(ctnr)


if __name__ == '__main__':
c = docker.Client(base_url=DEFAULT_BASE_URL)
c.pull('busybox')
exec_driver = c.info()['ExecutionDriver']
EXEC_DRIVER_IS_NATIVE = exec_driver.startswith('native')
c.close()
unittest.main()