diff --git a/compose/container.py b/compose/container.py index 3ee45c8f3..026306866 100644 --- a/compose/container.py +++ b/compose/container.py @@ -96,6 +96,8 @@ class Container(object): @property def slug(self): + if not self.full_slug: + return None return truncate_id(self.full_slug) @property diff --git a/tests/unit/container_test.py b/tests/unit/container_test.py index 64c9cc344..66c6c1578 100644 --- a/tests/unit/container_test.py +++ b/tests/unit/container_test.py @@ -5,6 +5,7 @@ import docker from .. import mock from .. import unittest +from compose.const import LABEL_SLUG from compose.container import Container from compose.container import get_container_name @@ -87,7 +88,7 @@ class ContainerTest(unittest.TestCase): assert container.name == "composetest_db_1" def test_name_without_project(self): - self.container_dict['Name'] = "/composetest_web_7" + self.container_dict['Name'] = "/composetest_web_7_092cd63296fd" container = Container(None, self.container_dict, has_been_inspected=True) assert container.name_without_project == "web_7_092cd63296fd" @@ -96,6 +97,12 @@ class ContainerTest(unittest.TestCase): container = Container(None, self.container_dict, has_been_inspected=True) assert container.name_without_project == "custom_name_of_container" + def test_name_without_project_noslug(self): + self.container_dict['Name'] = "/composetest_web_7" + del self.container_dict['Config']['Labels'][LABEL_SLUG] + container = Container(None, self.container_dict, has_been_inspected=True) + assert container.name_without_project == 'web_7' + def test_inspect_if_not_inspected(self): mock_client = mock.create_autospec(docker.APIClient) container = Container(mock_client, dict(Id="the_id"))