Bug 1288567 - Add basic test for context tar creation; r=dustin
authorGregory Szorc <gps@mozilla.com>
Fri, 22 Jul 2016 10:57:27 -0700
changeset 348597 a8b8e6806110c51de467dadadc573ab11c1f5a1c
parent 348596 f13b4d398a1b6df8c19216994194c073dd6e188b
child 348598 d5555a753cc6726a2c9bc0bbb87bb38aa6a62d16
push id1230
push userjlund@mozilla.com
push dateMon, 31 Oct 2016 18:13:35 +0000
treeherdermozilla-release@5e06e3766db2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdustin
bugs1288567
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1288567 - Add basic test for context tar creation; r=dustin Now that the context tar creation function is standalone and doesn't rely on external state, we can start unit testing it easier. We establish a basic unit test that verifies the function works as advertised and that output is deterministic. MozReview-Commit-ID: H4MY28PiHSN
taskcluster/taskgraph/test/test_util_docker.py
--- a/taskcluster/taskgraph/test/test_util_docker.py
+++ b/taskcluster/taskgraph/test/test_util_docker.py
@@ -1,23 +1,28 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import os
 import shutil
+import stat
+import tarfile
 import tempfile
 import unittest
 
 from ..util import docker
 from mozunit import MockedOpen
 
 
+MODE_STANDARD = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
+
+
 class TestDocker(unittest.TestCase):
 
     def test_generate_context_hash(self):
         tmpdir = tempfile.mkdtemp()
         old_GECKO = docker.GECKO
         docker.GECKO = tmpdir
         try:
             os.makedirs(os.path.join(tmpdir, 'docker', 'my-image'))
@@ -41,8 +46,34 @@ class TestDocker(unittest.TestCase):
             self.assertEqual(docker.docker_image('myimage'), "cool-images/myimage:1.2.3")
 
     def test_docker_image_default_registry(self):
         files = {}
         files["{}/REGISTRY".format(docker.DOCKER_ROOT)] = "mozilla"
         files["{}/myimage/VERSION".format(docker.DOCKER_ROOT)] = "1.2.3"
         with MockedOpen(files):
             self.assertEqual(docker.docker_image('myimage'), "mozilla/myimage:1.2.3")
+
+    def test_create_context_tar_basic(self):
+        tmp = tempfile.mkdtemp()
+        try:
+            d = os.path.join(tmp, 'test_image')
+            os.mkdir(d)
+            with open(os.path.join(d, 'Dockerfile'), 'a'):
+                pass
+            os.chmod(os.path.join(d, 'Dockerfile'), MODE_STANDARD)
+
+            with open(os.path.join(d, 'extra'), 'a'):
+                pass
+            os.chmod(os.path.join(d, 'extra'), MODE_STANDARD)
+
+            tp = os.path.join(tmp, 'tar')
+            h = docker.create_context_tar(tmp, d, tp, 'my_image')
+            self.assertEqual(h, '2a6d7f1627eba60daf85402418e041d728827d309143c6bc1c6bb3035bde6717')
+
+            # File prefix should be "my_image"
+            with tarfile.open(tp, 'r:gz') as tf:
+                self.assertEqual(tf.getnames(), [
+                    'my_image/Dockerfile',
+                    'my_image/extra',
+                ])
+        finally:
+            shutil.rmtree(tmp)