Skip to content

Commit 608f29c

Browse files
committed
Unit tests for Service.containers() and get_container_name()
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
1 parent 37ed743 commit 608f29c

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

tests/unit/service_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
parse_volume_spec,
1818
build_volume_binding,
1919
APIError,
20+
get_container_name,
2021
parse_repository_tag,
2122
)
2223

@@ -49,6 +50,25 @@ def test_config_validation(self):
4950
self.assertRaises(ConfigError, lambda: Service(name='foo', port=['8000']))
5051
Service(name='foo', ports=['8000'])
5152

53+
def test_get_container_name(self):
54+
self.assertIsNone(get_container_name({}))
55+
self.assertEqual(get_container_name({'Name': 'myproject_db_1'}), 'myproject_db_1')
56+
self.assertEqual(get_container_name({'Names': ['/myproject_db_1', '/myproject_web_1/db']}), 'myproject_db_1')
57+
58+
def test_containers(self):
59+
service = Service('db', client=self.mock_client, project='myproject')
60+
61+
self.mock_client.containers.return_value = []
62+
self.assertEqual(service.containers(), [])
63+
64+
self.mock_client.containers.return_value = [
65+
{'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/myproject', '/foo/bar']},
66+
{'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/myproject_db']},
67+
{'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/db_1']},
68+
{'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/myproject_db_1', '/myproject_web_1/db']},
69+
]
70+
self.assertEqual([c.id for c in service.containers()], ['IN_1'])
71+
5272
def test_get_volumes_from_container(self):
5373
container_id = 'aabbccddee'
5474
service = Service(

0 commit comments

Comments
 (0)