Skip to content

Commit 301515e

Browse files
author
James Harris
committed
Add Support for Mac Address
The new docker api allows specifing mac address for containers. This change is to allow docker py the same functionality. Signed-off-by: James Harris <[email protected]>
1 parent d3a2d90 commit 301515e

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

docker/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _container_config(self, image, command, hostname=None, user=None,
110110
network_disabled=False, entrypoint=None,
111111
cpu_shares=None, working_dir=None,
112112
domainname=None, memswap_limit=0, cpuset=None,
113-
host_config=None):
113+
host_config=None, mac_address=None):
114114
if isinstance(command, six.string_types):
115115
command = shlex.split(str(command))
116116
if isinstance(environment, dict):
@@ -227,7 +227,8 @@ def _container_config(self, image, command, hostname=None, user=None,
227227
'Cpuset': cpuset,
228228
'WorkingDir': working_dir,
229229
'MemorySwap': memswap_limit,
230-
'HostConfig': host_config
230+
'HostConfig': host_config,
231+
'MacAddress': mac_address
231232
}
232233

233234
def _post_json(self, url, data, **kwargs):
@@ -539,7 +540,8 @@ def create_container(self, image, command=None, hostname=None, user=None,
539540
volumes=None, volumes_from=None,
540541
network_disabled=False, name=None, entrypoint=None,
541542
cpu_shares=None, working_dir=None, domainname=None,
542-
memswap_limit=0, cpuset=None, host_config=None):
543+
memswap_limit=0, cpuset=None, host_config=None,
544+
mac_address=None):
543545

544546
if isinstance(volumes, six.string_types):
545547
volumes = [volumes, ]
@@ -551,7 +553,7 @@ def create_container(self, image, command=None, hostname=None, user=None,
551553
image, command, hostname, user, detach, stdin_open, tty, mem_limit,
552554
ports, environment, dns, volumes, volumes_from, network_disabled,
553555
entrypoint, cpu_shares, working_dir, domainname,
554-
memswap_limit, cpuset, host_config
556+
memswap_limit, cpuset, host_config, mac_address
555557
)
556558
return self.create_container_from_config(config, name)
557559

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ from. Optionally a single string joining container id's with commas
204204
* domainname (str or list): Set custom DNS search domains
205205
* memswap_limit (int):
206206
* host_config (dict): A [HostConfig](hostconfig.md) dictionary
207+
* mac_address (str): The Mac Address to assign the container
207208

208209
**Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
209210

tests/fake_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def get_fake_inspect_container():
129129
"StartedAt": "2013-09-25T14:01:18.869545111+02:00",
130130
"Ghost": False
131131
},
132+
"MacAddress": "02:42:ac:11:00:0a"
132133
}
133134
return status_code, response
134135

@@ -188,7 +189,8 @@ def get_fake_port():
188189
'Ports': {
189190
'1111': None,
190191
'1111/tcp': [{'HostIp': '127.0.0.1', 'HostPort': '4567'}],
191-
'2222': None}
192+
'2222': None},
193+
'MacAddress': '02:42:ac:11:00:0a'
192194
}
193195
}
194196
return status_code, response

tests/integration_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,22 @@ def runTest(self):
671671
self.client.kill(id)
672672

673673

674+
class TestMacAddress(BaseTestCase):
675+
def runTest(self):
676+
mac_address_expected = "02:42:ac:11:00:0a"
677+
container = self.client.create_container(
678+
'busybox', ['sleep', '60'], mac_address=mac_address_expected)
679+
680+
id = container['Id']
681+
682+
self.client.start(container)
683+
res = self.client.inspect_container(container['Id'])
684+
self.assertEqual(mac_address_expected,
685+
res['NetworkSettings']['MacAddress'])
686+
687+
self.client.kill(id)
688+
689+
674690
class TestRestart(BaseTestCase):
675691
def runTest(self):
676692
container = self.client.create_container('busybox', ['sleep', '9999'])

tests/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,18 @@ def test_create_container_with_port_binds(self):
768768
docker.client.DEFAULT_TIMEOUT_SECONDS
769769
)
770770

771+
def test_create_container_with_mac_address(self):
772+
try:
773+
mac_address_expected = "02:42:ac:11:00:0a"
774+
container = self.client.create_container(
775+
'busybox', ['sleep', '60'], mac_address=mac_address_expected)
776+
except Exception as e:
777+
self.fail('Command should not raise exception: {0}'.format(e))
778+
779+
res = self.client.inspect_container(container['Id'])
780+
self.assertEqual(mac_address_expected,
781+
res['NetworkSettings']['MacAddress'])
782+
771783
def test_create_container_with_links(self):
772784
try:
773785
link_path = 'path'

0 commit comments

Comments
 (0)