mirror of https://github.com/docker/docker-py.git
Make volume binds tests work on any host
Instead of creating the test directory directly on the host, create it by starting a container with the directory bind-mounted, so that it doesn't matter whether the daemon is local, in a VM or remote. This removes the need to make /tmp a volume in the test container, and to share it with the dind container. Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
3d6c91b469
commit
eec0465832
12
Makefile
12
Makefile
|
@ -1,7 +1,5 @@
|
||||||
.PHONY: all build test integration-test unit-test build-py3 unit-test-py3 integration-test-py3
|
.PHONY: all build test integration-test unit-test build-py3 unit-test-py3 integration-test-py3
|
||||||
|
|
||||||
HOST_TMPDIR=test -n "$(TMPDIR)" && echo $(TMPDIR) || echo /tmp
|
|
||||||
|
|
||||||
all: test
|
all: test
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
@ -22,13 +20,13 @@ unit-test-py3: build-py3
|
||||||
docker run docker-py3 py.test tests/test.py tests/utils_test.py
|
docker run docker-py3 py.test tests/test.py tests/utils_test.py
|
||||||
|
|
||||||
integration-test: build
|
integration-test: build
|
||||||
docker run -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py py.test -rxs tests/integration_test.py
|
docker run -v /var/run/docker.sock:/var/run/docker.sock docker-py py.test -rxs tests/integration_test.py
|
||||||
|
|
||||||
integration-test-py3: build-py3
|
integration-test-py3: build-py3
|
||||||
docker run -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py3 py.test -rxs tests/integration_test.py
|
docker run -v /var/run/docker.sock:/var/run/docker.sock docker-py3 py.test -rxs tests/integration_test.py
|
||||||
|
|
||||||
integration-dind: build build-py3
|
integration-dind: build build-py3
|
||||||
docker run -d --name dpy-dind -v /tmp --privileged dockerswarm/dind:1.8.1 docker -d -H tcp://0.0.0.0:2375
|
docker run -d --name dpy-dind --privileged dockerswarm/dind:1.8.1 docker -d -H tcp://0.0.0.0:2375
|
||||||
docker run --volumes-from dpy-dind --env="DOCKER_HOST=tcp://docker:2375" --link=dpy-dind:docker docker-py py.test -rxs tests/integration_test.py
|
docker run --env="DOCKER_HOST=tcp://docker:2375" --link=dpy-dind:docker docker-py py.test -rxs tests/integration_test.py
|
||||||
docker run --volumes-from dpy-dind --env="DOCKER_HOST=tcp://docker:2375" --link=dpy-dind:docker docker-py3 py.test -rxs tests/integration_test.py
|
docker run --env="DOCKER_HOST=tcp://docker:2375" --link=dpy-dind:docker docker-py3 py.test -rxs tests/integration_test.py
|
||||||
docker rm -vf dpy-dind
|
docker rm -vf dpy-dind
|
||||||
|
|
|
@ -118,6 +118,21 @@ class BaseTestCase(unittest.TestCase):
|
||||||
|
|
||||||
self.client.close()
|
self.client.close()
|
||||||
|
|
||||||
|
def run_container(self, *args, **kwargs):
|
||||||
|
container = self.client.create_container(*args, **kwargs)
|
||||||
|
self.tmp_containers.append(container)
|
||||||
|
self.client.start(container)
|
||||||
|
exitcode = self.client.wait(container)
|
||||||
|
|
||||||
|
if exitcode != 0:
|
||||||
|
output = self.client.logs(container)
|
||||||
|
raise Exception(
|
||||||
|
"Container exited with code {}:\n{}"
|
||||||
|
.format(exitcode, output))
|
||||||
|
|
||||||
|
return container
|
||||||
|
|
||||||
|
|
||||||
#########################
|
#########################
|
||||||
# INFORMATION TESTS #
|
# INFORMATION TESTS #
|
||||||
#########################
|
#########################
|
||||||
|
@ -205,91 +220,78 @@ class TestCreateContainer(BaseTestCase):
|
||||||
|
|
||||||
|
|
||||||
class TestCreateContainerWithBinds(BaseTestCase):
|
class TestCreateContainerWithBinds(BaseTestCase):
|
||||||
def runTest(self):
|
def setUp(self):
|
||||||
mount_dest = '/mnt'
|
super(TestCreateContainerWithBinds, self).setUp()
|
||||||
mount_origin = tempfile.mkdtemp()
|
|
||||||
self.tmp_folders.append(mount_origin)
|
|
||||||
|
|
||||||
filename = 'shared.txt'
|
self.mount_dest = '/mnt'
|
||||||
shared_file = os.path.join(mount_origin, filename)
|
|
||||||
binds = {
|
|
||||||
mount_origin: {
|
|
||||||
'bind': mount_dest,
|
|
||||||
'ro': False,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
with open(shared_file, 'w'):
|
# Get a random pathname - we don't need it to exist locally
|
||||||
container = self.client.create_container(
|
self.mount_origin = tempfile.mkdtemp()
|
||||||
BUSYBOX,
|
shutil.rmtree(self.mount_origin)
|
||||||
['ls', mount_dest], volumes={mount_dest: {}},
|
|
||||||
host_config=self.client.create_host_config(
|
self.filename = 'shared.txt'
|
||||||
binds=binds, network_mode='none'
|
|
||||||
)
|
self.run_with_volume(
|
||||||
)
|
False,
|
||||||
container_id = container['Id']
|
BUSYBOX,
|
||||||
self.client.start(container_id)
|
['touch', os.path.join(self.mount_dest, self.filename)],
|
||||||
self.tmp_containers.append(container_id)
|
)
|
||||||
exitcode = self.client.wait(container_id)
|
|
||||||
self.assertEqual(exitcode, 0)
|
def run_with_volume(self, ro, *args, **kwargs):
|
||||||
logs = self.client.logs(container_id)
|
return self.run_container(
|
||||||
|
*args,
|
||||||
|
volumes={self.mount_dest: {}},
|
||||||
|
host_config=self.client.create_host_config(
|
||||||
|
binds={
|
||||||
|
self.mount_origin: {
|
||||||
|
'bind': self.mount_dest,
|
||||||
|
'ro': ro,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
network_mode='none'
|
||||||
|
),
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_rw(self):
|
||||||
|
container = self.run_with_volume(
|
||||||
|
False,
|
||||||
|
BUSYBOX,
|
||||||
|
['ls', self.mount_dest],
|
||||||
|
)
|
||||||
|
logs = self.client.logs(container)
|
||||||
|
|
||||||
os.unlink(shared_file)
|
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
logs = logs.decode('utf-8')
|
logs = logs.decode('utf-8')
|
||||||
self.assertIn(filename, logs)
|
self.assertIn(self.filename, logs)
|
||||||
|
|
||||||
# FIXME: format changes in API version >= 1.20
|
# FIXME: format changes in API version >= 1.20
|
||||||
inspect_data = self.client.inspect_container(container_id)
|
inspect_data = self.client.inspect_container(container)
|
||||||
self.assertIn('Volumes', inspect_data)
|
self.assertEqual(
|
||||||
self.assertIn(mount_dest, inspect_data['Volumes'])
|
self.mount_origin,
|
||||||
self.assertEqual(mount_origin, inspect_data['Volumes'][mount_dest])
|
inspect_data['Volumes'][self.mount_dest]
|
||||||
self.assertIn(mount_dest, inspect_data['VolumesRW'])
|
)
|
||||||
self.assertTrue(inspect_data['VolumesRW'][mount_dest])
|
self.assertTrue(inspect_data['VolumesRW'][self.mount_dest])
|
||||||
|
|
||||||
|
def test_ro(self):
|
||||||
|
container = self.run_with_volume(
|
||||||
|
True,
|
||||||
|
BUSYBOX,
|
||||||
|
['ls', self.mount_dest],
|
||||||
|
)
|
||||||
|
logs = self.client.logs(container)
|
||||||
|
|
||||||
class TestCreateContainerWithRoBinds(BaseTestCase):
|
|
||||||
def runTest(self):
|
|
||||||
mount_dest = '/mnt'
|
|
||||||
mount_origin = tempfile.mkdtemp()
|
|
||||||
self.tmp_folders.append(mount_origin)
|
|
||||||
|
|
||||||
filename = 'shared.txt'
|
|
||||||
shared_file = os.path.join(mount_origin, filename)
|
|
||||||
binds = {
|
|
||||||
mount_origin: {
|
|
||||||
'bind': mount_dest,
|
|
||||||
'ro': True,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
with open(shared_file, 'w'):
|
|
||||||
container = self.client.create_container(
|
|
||||||
BUSYBOX,
|
|
||||||
['ls', mount_dest], volumes={mount_dest: {}},
|
|
||||||
host_config=self.client.create_host_config(
|
|
||||||
binds=binds, network_mode='none'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
container_id = container['Id']
|
|
||||||
self.client.start(container_id)
|
|
||||||
self.tmp_containers.append(container_id)
|
|
||||||
exitcode = self.client.wait(container_id)
|
|
||||||
self.assertEqual(exitcode, 0)
|
|
||||||
logs = self.client.logs(container_id)
|
|
||||||
|
|
||||||
os.unlink(shared_file)
|
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
logs = logs.decode('utf-8')
|
logs = logs.decode('utf-8')
|
||||||
self.assertIn(filename, logs)
|
self.assertIn(self.filename, logs)
|
||||||
|
|
||||||
# FIXME: format changes in API version >= 1.20
|
# FIXME: format changes in API version >= 1.20
|
||||||
inspect_data = self.client.inspect_container(container_id)
|
inspect_data = self.client.inspect_container(container)
|
||||||
self.assertIn('Volumes', inspect_data)
|
self.assertEqual(
|
||||||
self.assertIn(mount_dest, inspect_data['Volumes'])
|
self.mount_origin,
|
||||||
self.assertEqual(mount_origin, inspect_data['Volumes'][mount_dest])
|
inspect_data['Volumes'][self.mount_dest]
|
||||||
self.assertIn(mount_dest, inspect_data['VolumesRW'])
|
)
|
||||||
self.assertFalse(inspect_data['VolumesRW'][mount_dest])
|
self.assertFalse(inspect_data['VolumesRW'][self.mount_dest])
|
||||||
|
|
||||||
|
|
||||||
@requires_api_version('1.20')
|
@requires_api_version('1.20')
|
||||||
|
|
Loading…
Reference in New Issue