Created
April 23, 2015 00:31
-
-
Save shin-/94bc282e8a41f0d09786 to your computer and use it in GitHub Desktop.
test_file_bind.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import tempfile | |
import unittest | |
import docker | |
import six | |
DEFAULT_BASE_URL = os.environ.get('DOCKER_HOST') | |
class BaseTestCase(unittest.TestCase): | |
tmp_imgs = [] | |
tmp_containers = [] | |
tmp_folders = [] | |
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.tmp_imgs = [] | |
self.tmp_containers = [] | |
self.tmp_folders = [] | |
def tearDown(self): | |
for img in self.tmp_imgs: | |
try: | |
self.client.remove_image(img) | |
except docker.errors.APIError: | |
pass | |
for container in self.tmp_containers: | |
try: | |
self.client.stop(container, timeout=1) | |
self.client.remove_container(container) | |
except docker.errors.APIError: | |
pass | |
for folder in self.tmp_folders: | |
shutil.rmtree(folder) | |
self.client.close() | |
class TestStartContainerWithFileBind(BaseTestCase): | |
def runTest(self): | |
mount_dest = '/myfile/myfile' | |
mount_origin = tempfile.mktemp() | |
try: | |
binds = { | |
mount_origin: { | |
'bind': mount_dest, | |
'ro': True | |
}, | |
} | |
with open(mount_origin, 'w') as f: | |
f.write('sakuya izayoi') | |
container = self.client.create_container( | |
'busybox', ['cat', mount_dest], volumes={mount_dest: {}} | |
) | |
self.client.start(container, binds=binds) | |
exitcode = self.client.wait(container) | |
self.assertEqual(exitcode, 0) | |
logs = self.client.logs(container) | |
if six.PY3: | |
logs = logs.decode('utf-8') | |
self.assertIn('sakuya izayoi', logs) | |
finally: | |
os.unlink(mount_origin) | |
if __name__ == '__main__': | |
c = docker.Client(base_url=DEFAULT_BASE_URL) | |
c.pull('busybox') | |
c.close() | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment