Added ulimits functionality to docker compose

Signed-off-by: Kevin Greene <kevin@spantree.net>
This commit is contained in:
Kevin Greene 2015-10-26 17:39:50 -04:00 • committed by Daniel Nephin
commit 22d90d2180
6 changed files with 152 additions and 0 deletions

View file

@ -22,6 +22,7 @@ from compose.const import LABEL_SERVICE
from compose.const import LABEL_VERSION
from compose.container import Container
from compose.service import build_extra_hosts
from compose.service import build_ulimits
from compose.service import ConfigError
from compose.service import ConvergencePlan
from compose.service import ConvergenceStrategy
@ -164,6 +165,36 @@ class ServiceTest(DockerClientTestCase):
{'www.example.com': '192.168.0.17',
'api.example.com': '192.168.0.18'})
def sort_dicts_by_name(self, dictionary_list):
return sorted(dictionary_list, key=lambda k: k['name'])
def test_build_ulimits_with_invalid_options(self):
self.assertRaises(ConfigError, lambda: build_ulimits({'nofile': {'soft': 10000, 'hard': 10}}))
def test_build_ulimits_with_integers(self):
self.assertEqual(build_ulimits(
{'nofile': {'soft': 10000, 'hard': 20000}}),
[{'name': 'nofile', 'soft': 10000, 'hard': 20000}])
self.assertEqual(self.sort_dicts_by_name(build_ulimits(
{'nofile': {'soft': 10000, 'hard': 20000}, 'nproc': {'soft': 65535, 'hard': 65535}})),
self.sort_dicts_by_name([{'name': 'nofile', 'soft': 10000, 'hard': 20000},
{'name': 'nproc', 'soft': 65535, 'hard': 65535}]))
def test_build_ulimits_with_dicts(self):
self.assertEqual(build_ulimits(
{'nofile': 20000}),
[{'name': 'nofile', 'soft': 20000, 'hard': 20000}])
self.assertEqual(self.sort_dicts_by_name(build_ulimits(
{'nofile': 20000, 'nproc': 65535})),
self.sort_dicts_by_name([{'name': 'nofile', 'soft': 20000, 'hard': 20000},
{'name': 'nproc', 'soft': 65535, 'hard': 65535}]))
def test_build_ulimits_with_integers_and_dicts(self):
self.assertEqual(self.sort_dicts_by_name(build_ulimits(
{'nproc': 65535, 'nofile': {'soft': 10000, 'hard': 20000}})),
self.sort_dicts_by_name([{'name': 'nofile', 'soft': 10000, 'hard': 20000},
{'name': 'nproc', 'soft': 65535, 'hard': 65535}]))
def test_create_container_with_extra_hosts_list(self):
extra_hosts = ['somehost:162.242.195.82', 'otherhost:50.31.209.229']
service = self.create_service('db', extra_hosts=extra_hosts)

View file

@ -349,6 +349,66 @@ class ConfigTest(unittest.TestCase):
)
)
def test_config_ulimits_invalid_keys_validation_error(self):
expected_error_msg = "Service 'web' configuration key 'ulimits' contains unsupported option: 'not_soft_or_hard'"
with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
config.load(
build_config_details(
{'web': {
'image': 'busybox',
'ulimits': {
'nofile': {
"not_soft_or_hard": 100,
"soft": 10000,
"hard": 20000,
}
}
}},
'working_dir',
'filename.yml'
)
)
def test_config_ulimits_required_keys_validation_error(self):
expected_error_msg = "Service 'web' configuration key 'ulimits' u?'hard' is a required property"
with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
config.load(
build_config_details(
{'web': {
'image': 'busybox',
'ulimits': {
'nofile': {
"soft": 10000,
}
}
}},
'working_dir',
'filename.yml'
)
)
def test_config_ulimits_soft_greater_than_hard_error(self):
expected_error_msg = "cannot contain a 'soft' value higher than 'hard' value"
with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
config.load(
build_config_details(
{'web': {
'image': 'busybox',
'ulimits': {
'nofile': {
"soft": 10000,
"hard": 1000
}
}
}},
'working_dir',
'filename.yml'
)
)
def test_valid_config_which_allows_two_type_definitions(self):
expose_values = [["8000"], [8000]]
for expose in expose_values: