From 8ba2e7e929efd726d9af267b2db176c410314d8c Mon Sep 17 00:00:00 2001 From: Roberto Aguilar Date: Tue, 7 Jan 2014 21:40:23 -0800 Subject: [PATCH] Added port binding HostPort verification Ensure the HostPort value is numeric just in case a bogus port binding was passed in. --- docker/utils/utils.py | 2 +- tests/test_util.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/test_util.py diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 8fd9e9478d..29bf7e39eb 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -78,7 +78,7 @@ def _convert_port_binding(binding): if result['HostPort'] is None: result['HostPort'] = '' else: - result['HostPort'] = str(result['HostPort']) + result['HostPort'] = str(int(result['HostPort'])) return result diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000000..fb0fa1467d --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,41 @@ +import unittest + +from docker.utils import convert_port_bindings + +class UtilsTest(unittest.TestCase): + def test_convert_port_binding_with_dict(self): + """ + Ensure the port binding raises an exception if the value is clearly unexpected + """ + binding = [{ + 'HostIp': '0.0.0.0', + 'HostPort': '4000', + }] + + bindings = { + '80': binding, + } + + self.assertRaises(TypeError, convert_port_bindings, bindings) + + def test_convert_port_binding_with_non_int_string(self): + """ + Ensure the port binding raises an exception if the value is clearly unexpected + """ + binding = ['hi'] + + bindings = { + '80': binding, + } + + self.assertRaises(ValueError, convert_port_bindings, bindings) + + def test_convert_port_binding_just_port_number(self): + binding = '4000' + bindings = { + '80': binding, + } + + converted = convert_port_bindings(bindings) + + self.assertEqual(binding, converted.values()[0][0]['HostPort'])