|
17 | 17 | parse_volume_spec, |
18 | 18 | build_volume_binding, |
19 | 19 | APIError, |
| 20 | + get_container_name, |
20 | 21 | parse_repository_tag, |
21 | 22 | ) |
22 | 23 |
|
@@ -49,6 +50,37 @@ def test_config_validation(self): |
49 | 50 | self.assertRaises(ConfigError, lambda: Service(name='foo', port=['8000'])) |
50 | 51 | Service(name='foo', ports=['8000']) |
51 | 52 |
|
| 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 | + self.assertEqual(get_container_name({'Names': ['/swarm-host-1/myproject_db_1', '/swarm-host-1/myproject_web_1/db']}), 'myproject_db_1') |
| 58 | + |
| 59 | + def test_containers(self): |
| 60 | + service = Service('db', client=self.mock_client, project='myproject') |
| 61 | + |
| 62 | + self.mock_client.containers.return_value = [] |
| 63 | + self.assertEqual(service.containers(), []) |
| 64 | + |
| 65 | + self.mock_client.containers.return_value = [ |
| 66 | + {'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/myproject', '/foo/bar']}, |
| 67 | + {'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/myproject_db']}, |
| 68 | + {'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/db_1']}, |
| 69 | + {'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/myproject_db_1', '/myproject_web_1/db']}, |
| 70 | + ] |
| 71 | + self.assertEqual([c.id for c in service.containers()], ['IN_1']) |
| 72 | + |
| 73 | + def test_containers_prefixed(self): |
| 74 | + service = Service('db', client=self.mock_client, project='myproject') |
| 75 | + |
| 76 | + self.mock_client.containers.return_value = [ |
| 77 | + {'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/swarm-host-1/myproject', '/swarm-host-1/foo/bar']}, |
| 78 | + {'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/swarm-host-1/myproject_db']}, |
| 79 | + {'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/swarm-host-1/db_1']}, |
| 80 | + {'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/swarm-host-1/myproject_db_1', '/swarm-host-1/myproject_web_1/db']}, |
| 81 | + ] |
| 82 | + self.assertEqual([c.id for c in service.containers()], ['IN_1']) |
| 83 | + |
52 | 84 | def test_get_volumes_from_container(self): |
53 | 85 | container_id = 'aabbccddee' |
54 | 86 | service = Service( |
|
0 commit comments