Sfoglia il codice sorgente

Prevent attempts to create image names starting with - or _

Signed-off-by: Joffrey F <[email protected]>
Joffrey F 7 anni fa
parent
commit
b00db08aa9
2 ha cambiato i file con 18 aggiunte e 1 eliminazioni
  1. 3 1
      compose/service.py
  2. 15 0
      tests/integration/service_test.py

+ 3 - 1
compose/service.py

@@ -363,7 +363,9 @@ class Service(object):
 
     @property
     def image_name(self):
-        return self.options.get('image', '{s.project}_{s.name}'.format(s=self))
+        return self.options.get('image', '{project}_{s.name}'.format(
+            s=self, project=self.project.lstrip('_-')
+        ))
 
     @property
     def platform(self):

+ 15 - 0
tests/integration/service_test.py

@@ -1137,6 +1137,21 @@ class ServiceTest(DockerClientTestCase):
         service.build()
         assert service.image()
 
+    def test_build_with_illegal_leading_chars(self):
+        base_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, base_dir)
+        with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
+            f.write('FROM busybox\nRUN echo "Embodiment of Scarlet Devil"\n')
+        service = Service(
+            'build_leading_slug', client=self.client,
+            project='___-composetest', build={
+                'context': text_type(base_dir)
+            }
+        )
+        assert service.image_name == 'composetest_build_leading_slug'
+        service.build()
+        assert service.image()
+
     def test_start_container_stays_unprivileged(self):
         service = self.create_service('web')
         container = create_and_start_container(service).inspect()