2013-12-20 21:28:24 +01:00
|
|
|
from fig import Service
|
2013-12-10 21:47:08 +01:00
|
|
|
from .testcases import DockerClientTestCase
|
2013-12-09 16:00:01 +01:00
|
|
|
|
|
|
|
|
2013-12-18 18:01:50 +01:00
|
|
|
class ServiceTest(DockerClientTestCase):
|
2013-12-09 13:19:27 +01:00
|
|
|
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'))
|
2013-12-19 15:47:43 +01:00
|
|
|
self.assertRaises(ValueError, lambda: Service(name='_'))
|
|
|
|
self.assertRaises(ValueError, lambda: Service(name='____'))
|
|
|
|
self.assertRaises(ValueError, lambda: Service(name='foo_bar'))
|
|
|
|
self.assertRaises(ValueError, lambda: Service(name='__foo_bar__'))
|
2013-12-09 13:19:27 +01:00
|
|
|
|
|
|
|
Service('a')
|
|
|
|
Service('foo')
|
|
|
|
|
2013-12-19 16:16:17 +01:00
|
|
|
def test_project_validation(self):
|
|
|
|
self.assertRaises(ValueError, lambda: Service(name='foo', project='_'))
|
|
|
|
Service(name='foo', project='bar')
|
|
|
|
|
2013-12-09 16:00:41 +01:00
|
|
|
def test_containers(self):
|
|
|
|
foo = self.create_service('foo')
|
|
|
|
bar = self.create_service('bar')
|
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
foo.start_container()
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(foo.containers()), 1)
|
2013-12-20 20:33:41 +01:00
|
|
|
self.assertEqual(foo.containers()[0].name, 'default_foo_1')
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(bar.containers()), 0)
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
bar.start_container()
|
|
|
|
bar.start_container()
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(foo.containers()), 1)
|
|
|
|
self.assertEqual(len(bar.containers()), 2)
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
names = [c.name for c in bar.containers()]
|
2013-12-20 20:33:41 +01:00
|
|
|
self.assertIn('default_bar_1', names)
|
|
|
|
self.assertIn('default_bar_2', names)
|
2013-12-19 16:16:17 +01:00
|
|
|
|
2013-12-20 11:46:55 +01:00
|
|
|
def test_containers_one_off(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
container = db.create_container(one_off=True)
|
|
|
|
self.assertEqual(db.containers(stopped=True), [])
|
|
|
|
self.assertEqual(db.containers(one_off=True, stopped=True), [container])
|
|
|
|
|
2013-12-19 16:16:17 +01:00
|
|
|
def test_project_is_added_to_container_name(self):
|
|
|
|
service = self.create_service('web', project='myproject')
|
2013-12-20 17:22:54 +01:00
|
|
|
service.start_container()
|
2013-12-20 20:33:41 +01:00
|
|
|
self.assertEqual(service.containers()[0].name, 'myproject_web_1')
|
2013-12-09 19:42:33 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
def test_start_stop(self):
|
2013-12-19 15:47:43 +01:00
|
|
|
service = self.create_service('scalingtest')
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 0)
|
2013-12-09 12:41:05 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
service.create_container()
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 12:41:05 +01:00
|
|
|
|
2013-12-10 21:47:08 +01:00
|
|
|
service.start()
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 12:41:05 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
service.stop(timeout=1)
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(service.containers()), 0)
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
service.stop(timeout=1)
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(service.containers()), 0)
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2013-12-20 17:53:07 +01:00
|
|
|
def test_kill_remove(self):
|
|
|
|
service = self.create_service('scalingtest')
|
|
|
|
|
|
|
|
service.start_container()
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
service.remove_stopped()
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
service.kill()
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
service.remove_stopped()
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 0)
|
|
|
|
|
2013-12-20 11:46:55 +01:00
|
|
|
def test_create_container_with_one_off(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
container = db.create_container(one_off=True)
|
2013-12-20 20:33:41 +01:00
|
|
|
self.assertEqual(container.name, 'default_db_run_1')
|
2013-12-20 11:46:55 +01:00
|
|
|
|
2013-12-20 13:55:45 +01:00
|
|
|
def test_create_container_with_one_off_when_existing_container_is_running(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
db.start()
|
|
|
|
container = db.create_container(one_off=True)
|
2013-12-20 20:33:41 +01:00
|
|
|
self.assertEqual(container.name, 'default_db_run_1')
|
2013-12-20 13:55:45 +01:00
|
|
|
|
2013-12-10 21:51:55 +01:00
|
|
|
def test_start_container_passes_through_options(self):
|
2013-12-10 22:01:39 +01:00
|
|
|
db = self.create_service('db')
|
|
|
|
db.start_container(environment={'FOO': 'BAR'})
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(db.containers()[0].environment['FOO'], 'BAR')
|
2013-12-10 22:01:39 +01:00
|
|
|
|
|
|
|
def test_start_container_inherits_options_from_constructor(self):
|
2013-12-10 21:51:55 +01:00
|
|
|
db = self.create_service('db', environment={'FOO': 'BAR'})
|
|
|
|
db.start_container()
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(db.containers()[0].environment['FOO'], 'BAR')
|
2013-12-10 21:51:55 +01:00
|
|
|
|
|
|
|
def test_start_container_creates_links(self):
|
2013-12-09 22:39:11 +01:00
|
|
|
db = self.create_service('db')
|
|
|
|
web = self.create_service('web', links=[db])
|
2013-12-10 21:51:55 +01:00
|
|
|
db.start_container()
|
|
|
|
web.start_container()
|
2013-12-19 16:16:17 +01:00
|
|
|
self.assertIn('default_db_1', web.containers()[0].links())
|
2013-12-20 17:22:54 +01:00
|
|
|
db.stop(timeout=1)
|
|
|
|
web.stop(timeout=1)
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2013-12-13 21:36:10 +01:00
|
|
|
def test_start_container_builds_images(self):
|
|
|
|
service = Service(
|
|
|
|
name='test',
|
|
|
|
client=self.client,
|
2013-12-14 17:34:24 +01:00
|
|
|
build='tests/fixtures/simple-dockerfile',
|
2013-12-13 21:36:10 +01:00
|
|
|
)
|
2013-12-20 17:22:54 +01:00
|
|
|
container = service.start_container()
|
2013-12-18 19:37:48 +01:00
|
|
|
container.wait()
|
|
|
|
self.assertIn('success', container.logs())
|
2013-12-20 17:23:40 +01:00
|
|
|
self.assertEqual(len(self.client.images(name='default_test')), 1)
|
|
|
|
|
|
|
|
def test_start_container_uses_tagged_image_if_it_exists(self):
|
|
|
|
self.client.build('tests/fixtures/simple-dockerfile', tag='default_test')
|
|
|
|
service = Service(
|
|
|
|
name='test',
|
|
|
|
client=self.client,
|
|
|
|
build='this/does/not/exist/and/will/throw/error',
|
|
|
|
)
|
2013-12-20 20:30:31 +01:00
|
|
|
container = service.start_container()
|
2013-12-20 17:23:40 +01:00
|
|
|
container.wait()
|
|
|
|
self.assertIn('success', container.logs())
|
2013-12-18 12:14:14 +01:00
|
|
|
|
|
|
|
def test_start_container_creates_ports(self):
|
|
|
|
service = self.create_service('web', ports=[8000])
|
2013-12-18 19:37:48 +01:00
|
|
|
container = service.start_container().inspect()
|
2013-12-18 12:14:14 +01:00
|
|
|
self.assertIn('8000/tcp', container['HostConfig']['PortBindings'])
|
|
|
|
self.assertNotEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8000')
|
|
|
|
|
|
|
|
def test_start_container_creates_fixed_external_ports(self):
|
|
|
|
service = self.create_service('web', ports=['8000:8000'])
|
2013-12-18 19:37:48 +01:00
|
|
|
container = service.start_container().inspect()
|
2013-12-18 12:14:14 +01:00
|
|
|
self.assertIn('8000/tcp', container['HostConfig']['PortBindings'])
|
|
|
|
self.assertEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8000')
|
|
|
|
|
2013-12-09 22:39:11 +01:00
|
|
|
|