Commit d81d9a58 authored by Samuel GAIST's avatar Samuel GAIST Committed by André Anjos

[dock] Add support for environment variables

parent 5c7900bc
...@@ -393,10 +393,12 @@ class Host(object): ...@@ -393,10 +393,12 @@ class Host(object):
# Mount the volumes # Mount the volumes
cmd.extend(container.volumes) cmd.extend(container.volumes)
# Expose the ports # Expose the ports
cmd.extend(container.ports) cmd.extend(container.ports)
# Environment variables
cmd.extend(container.environment_variables)
cmd.append(container.image) cmd.append(container.image)
cmd.extend(container.command) cmd.extend(container.command)
...@@ -624,6 +626,7 @@ class Container: ...@@ -624,6 +626,7 @@ class Container:
self.id = None self.id = None
self._volumes = {} self._volumes = {}
self._ports = {} self._ports = {}
self._environment_variables = {}
self._stats = None self._stats = None
self._name = None self._name = None
...@@ -670,6 +673,17 @@ class Container: ...@@ -670,6 +673,17 @@ class Container:
self._ports[container_port] = value self._ports[container_port] = value
def add_environment_variable(self, name, value):
"""Add an environment variable
Parameters:
:param str name: Name of the variable
:param str value: Content of the variable
"""
self._environment_variables[name] = value
def reset_ports(self): def reset_ports(self):
"""Empty the port bindings""" """Empty the port bindings"""
...@@ -724,6 +738,17 @@ class Container: ...@@ -724,6 +738,17 @@ class Container:
return ports return ports
@property
def environment_variables(self):
"""Returns the environment variables to set on this container.
"""
environment_variables = []
for k, v in self._environment_variables.items():
environment_variables.append('--env={}={}'.format(k, v))
return environment_variables
@property @property
def network(self): def network(self):
network = '' network = ''
...@@ -753,6 +778,7 @@ class Container: ...@@ -753,6 +778,7 @@ class Container:
cmd += "%s " % self.user cmd += "%s " % self.user
cmd += ' '.join(self.volumes) cmd += ' '.join(self.volumes)
cmd += ' '.join(self.ports) cmd += ' '.join(self.ports)
cmd += ' '.join(self.environment_variables)
cmd += "%s " % self.name cmd += "%s " % self.name
cmd += "%s " % self.image cmd += "%s " % self.image
......
...@@ -119,6 +119,23 @@ class UserTest(NoDiscoveryTests): ...@@ -119,6 +119,23 @@ class UserTest(NoDiscoveryTests):
self.assertTrue(self.host.logs(container).startswith('uid={0} gid={0}'.format(container.uid))) self.assertTrue(self.host.logs(container).startswith('uid={0} gid={0}'.format(container.uid)))
class EnvironmentVariableTest(NoDiscoveryTests):
@slow
def test_environment_variable(self):
"""Test that the uid property is correctly used.
"""
container = self.host.create_container('debian:8.4', ["env"])
container.add_environment_variable('DOCKER_TEST', 'good')
self.host.start(container)
status = self.host.wait(container)
self.assertEqual(status, 0)
self.assertTrue('DOCKER_TEST=good' in self.host.logs(container))
class AsyncTest(NoDiscoveryTests): class AsyncTest(NoDiscoveryTests):
@slow @slow
......
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