diff --git a/plum/service.py b/plum/service.py index e9777be76..8bc5a84e1 100644 --- a/plum/service.py +++ b/plum/service.py @@ -1,5 +1,12 @@ +import re + + class Service(object): - def __init__(self, client, image, command): + def __init__(self, name, client=None, image=None, command=None): + if not re.match('^[a-zA-Z0-9_]+$', name): + raise ValueError('Invalid name: %s' % name) + + self.name = name self.client = client self.image = image self.command = command diff --git a/plum/tests/service_test.py b/plum/tests/service_test.py index 0c03a2ab8..eac2d498c 100644 --- a/plum/tests/service_test.py +++ b/plum/tests/service_test.py @@ -3,7 +3,24 @@ from docker import Client from plum import Service -class ServiceTestCase(TestCase): +class NameTestCase(TestCase): + def test_name_validations(self): + self.assertRaises(ValueError, lambda: Service(name='')) + + self.assertRaises(ValueError, lambda: Service(name=' ')) + self.assertRaises(ValueError, lambda: Service(name='/')) + self.assertRaises(ValueError, lambda: Service(name='!')) + self.assertRaises(ValueError, lambda: Service(name='\xe2')) + + Service('a') + Service('foo') + Service('foo_bar') + Service('__foo_bar__') + Service('_') + Service('_____') + + +class ScalingTestCase(TestCase): def setUp(self): self.client = Client('http://127.0.0.1:4243') self.client.pull('ubuntu') @@ -13,6 +30,7 @@ class ServiceTestCase(TestCase): self.client.remove_container(c['Id']) self.service = Service( + name="test", client=self.client, image="ubuntu", command=["/bin/sleep", "300"],