diff options
| author | Aanand Prasad <aanand.prasad@gmail.com> | 2015-09-22 14:13:00 +0200 |
|---|---|---|
| committer | Joffrey F <joffrey@docker.com> | 2015-09-22 11:34:49 -0700 |
| commit | eec0465832f49e71b99f7f84fe61d686f48ade4e (patch) | |
| tree | 3921d1224f3c162e34957f219e0d461b1b04aec2 /tests | |
| parent | 3d6c91b46990213b0a7f96005cfc36c5c61e97b7 (diff) | |
| download | docker-py-eec0465832f49e71b99f7f84fe61d686f48ade4e.tar.gz | |
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>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/integration_test.py | 150 |
1 files changed, 76 insertions, 74 deletions
diff --git a/tests/integration_test.py b/tests/integration_test.py index d92500a..f801627 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -118,6 +118,21 @@ class BaseTestCase(unittest.TestCase): 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 # ######################### @@ -205,91 +220,78 @@ class TestCreateContainer(BaseTestCase): class TestCreateContainerWithBinds(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': False, - }, - } + def setUp(self): + super(TestCreateContainerWithBinds, self).setUp() + + self.mount_dest = '/mnt' + + # Get a random pathname - we don't need it to exist locally + self.mount_origin = tempfile.mkdtemp() + shutil.rmtree(self.mount_origin) + + self.filename = 'shared.txt' + + self.run_with_volume( + False, + BUSYBOX, + ['touch', os.path.join(self.mount_dest, self.filename)], + ) + + def run_with_volume(self, ro, *args, **kwargs): + 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) - 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: logs = logs.decode('utf-8') - self.assertIn(filename, logs) + self.assertIn(self.filename, logs) # FIXME: format changes in API version >= 1.20 - inspect_data = self.client.inspect_container(container_id) - self.assertIn('Volumes', inspect_data) - self.assertIn(mount_dest, inspect_data['Volumes']) - self.assertEqual(mount_origin, inspect_data['Volumes'][mount_dest]) - self.assertIn(mount_dest, inspect_data['VolumesRW']) - self.assertTrue(inspect_data['VolumesRW'][mount_dest]) - + inspect_data = self.client.inspect_container(container) + self.assertEqual( + self.mount_origin, + inspect_data['Volumes'][self.mount_dest] + ) + self.assertTrue(inspect_data['VolumesRW'][self.mount_dest]) -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, - }, - } + def test_ro(self): + container = self.run_with_volume( + True, + BUSYBOX, + ['ls', self.mount_dest], + ) + logs = self.client.logs(container) - 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: logs = logs.decode('utf-8') - self.assertIn(filename, logs) + self.assertIn(self.filename, logs) # FIXME: format changes in API version >= 1.20 - inspect_data = self.client.inspect_container(container_id) - self.assertIn('Volumes', inspect_data) - self.assertIn(mount_dest, inspect_data['Volumes']) - self.assertEqual(mount_origin, inspect_data['Volumes'][mount_dest]) - self.assertIn(mount_dest, inspect_data['VolumesRW']) - self.assertFalse(inspect_data['VolumesRW'][mount_dest]) + inspect_data = self.client.inspect_container(container) + self.assertEqual( + self.mount_origin, + inspect_data['Volumes'][self.mount_dest] + ) + self.assertFalse(inspect_data['VolumesRW'][self.mount_dest]) @requires_api_version('1.20') |
