Commit c1c63253 authored by Samuel GAIST's avatar Samuel GAIST

[dock] Add support for custom tmpfs

In order to allow container with special needs like
ROS requiring some read/write space, the Container
class now parses the new custom "beat.env.custom_tmpfs"
label and add the corresponding entries to the dictionnary
dedicated to them.

Fixes #106
parent 44d658b9
......@@ -795,6 +795,17 @@ class Container:
self._entrypoint = None
self._temporary_filesystems = {"/tmp": "500k", "/run": "500k"} # nosec
client = docker.from_env()
docker_image = client.images.get(image)
custom_tmpfs = docker_image.labels.get("beat.env.custom_tmpfs")
if custom_tmpfs is not None:
# import ipdb; ipdb.set_trace()
custom_tmpfs = json.loads(custom_tmpfs)
for path, size in custom_tmpfs.items():
self._temporary_filesystems[path] = size
def set_name(self, name):
""" Set the name to be used by the container in place of the docker
auto generated one.
......
......@@ -107,8 +107,7 @@ class NetworkTest(NoDiscoveryTests):
class UserTest(NoDiscoveryTests):
@slow
def test_user(self):
"""Test that the uid property is correctly used.
"""
"""Test that the uid property is correctly used."""
container = self.host.create_container("debian:8.4", ["id"])
container.uid = 10000
......@@ -127,8 +126,7 @@ class UserTest(NoDiscoveryTests):
class EnvironmentVariableTest(NoDiscoveryTests):
@slow
def test_environment_variable(self):
"""Test that the uid property is correctly used.
"""
"""Test that the uid property is correctly used."""
container = self.host.create_container("debian:8.4", ["env"])
container.add_environment_variable("DOCKER_TEST", "good")
......@@ -143,8 +141,7 @@ class EnvironmentVariableTest(NoDiscoveryTests):
class WorkdirTest(NoDiscoveryTests):
@slow
def test_workdir(self):
"""Test that the workdir property is correctly used.
"""
"""Test that the workdir property is correctly used."""
with TemporaryDirectory() as tmp_folder:
test_file = "test.txt"
......@@ -169,8 +166,7 @@ class WorkdirTest(NoDiscoveryTests):
class EntrypointTest(NoDiscoveryTests):
@slow
def test_entrypoint(self):
"""Test that the entrypoint property is correctly used.
"""
"""Test that the entrypoint property is correctly used."""
container = self.host.create_container("debian:8.4", ["42"])
container.set_entrypoint("echo")
......@@ -187,8 +183,7 @@ class EntrypointTest(NoDiscoveryTests):
class TmpfsTest(NoDiscoveryTests):
def test_tmpfs(self):
"""Test that the tmpfs are properly mounted and usable.
"""
"""Test that the tmpfs are properly mounted and usable."""
container = self.host.create_container(
"debian:8.4", ["touch", "/dummy/test.txt"]
......@@ -213,33 +208,6 @@ class TmpfsTest(NoDiscoveryTests):
self.assertEqual(status, 0)
self.assertEqual(logs, "")
def test_tmpfs_size(self):
"""Test that the tmpfs are respected.
"""
container = self.host.create_container(
"debian:8.4", ["dd", "if=/dev/zero", "of=/dummy/test.txt"]
)
tmpfs_list = container.temporary_filesystems
self.assertEqual(len(tmpfs_list), 2)
container.add_tmpfs("/dummy", "1M")
tmpfs_list = container.temporary_filesystems
self.assertEqual(len(tmpfs_list), 3)
self.host.start(container)
status = self.host.wait(container)
logs = self.host.logs(container)
if status != 0:
print(logs)
self.assertEqual(status, 1)
self.assertTrue("No space left" in logs)
class AsyncTest(NoDiscoveryTests):
@slow
......@@ -300,7 +268,7 @@ class AsyncTest(NoDiscoveryTests):
self.assertEqual(self.host.logs(container), "")
class AsyncWithEnvironmentTest(unittest.TestCase):
class WithDiscoveryTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.host = Host(raise_on_errors=False)
......@@ -314,6 +282,31 @@ class AsyncWithEnvironmentTest(unittest.TestCase):
self.host.teardown()
self.assertFalse(self.host.containers) # All containers are gone
class TmpfsWithEnvironmentTest(WithDiscoveryTests):
def test_tmpfs_from_label(self):
"""Test that the tmpfs are respected."""
container = self.host.create_container(
"Python for tests (1.3.0)",
["dd", "if=/dev/zero", "of=/custom_tmpfs/test.txt"],
)
tmpfs_list = container.temporary_filesystems
self.assertEqual(len(tmpfs_list), 3)
print(tmpfs_list)
self.host.start(container)
status = self.host.wait(container)
logs = self.host.logs(container)
if status != 0:
print(logs)
self.assertEqual(status, 1)
self.assertTrue("No space left" in logs)
class AsyncWithEnvironmentTest(WithDiscoveryTests):
@slow
def test_memory_limit(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment