|
| 1 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from collections import OrderedDict |
| 16 | +import io |
| 17 | +from StringIO import StringIO |
| 18 | +import tarfile |
| 19 | +import unittest |
| 20 | + |
| 21 | +from containerregistry.client.v2_2 import docker_image as v2_2_image |
| 22 | + |
| 23 | + |
| 24 | +class MockImage(object): |
| 25 | + """Mock of DockerImage, implementing only the methods called by extract().""" |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + self._fs_layers = OrderedDict() |
| 29 | + |
| 30 | + def add_layer(self, filenames): |
| 31 | + """Add a layer to the image. |
| 32 | +
|
| 33 | + Args: |
| 34 | + filenames: a list of filenames or (filename, content) pairs. Filenames |
| 35 | + with trailing slashes become directory entries in the generated tar |
| 36 | + """ |
| 37 | + buf = io.BytesIO() |
| 38 | + with tarfile.open(mode='w:', fileobj=buf) as tf: |
| 39 | + for entry in filenames: |
| 40 | + if (isinstance(entry, basestring)): |
| 41 | + name = entry |
| 42 | + content = "" |
| 43 | + else: |
| 44 | + (name, content) = entry |
| 45 | + tarinfo = tarfile.TarInfo(name) |
| 46 | + tarinfo.size = len(content) |
| 47 | + if name.endswith("/"): |
| 48 | + tarinfo.type = tarfile.DIRTYPE |
| 49 | + tf.addfile(tarinfo, fileobj=(StringIO(content) if content else None)) |
| 50 | + buf.seek(0) |
| 51 | + new_layer_id = str(len(self._fs_layers)) |
| 52 | + self._fs_layers[new_layer_id] = buf.getvalue() |
| 53 | + |
| 54 | + def diff_ids(self): |
| 55 | + return reversed(self._fs_layers.keys()) |
| 56 | + |
| 57 | + def uncompressed_layer(self, layer_id): |
| 58 | + return self._fs_layers[layer_id] |
| 59 | + |
| 60 | + |
| 61 | +class TestExtract(unittest.TestCase): |
| 62 | + |
| 63 | + def _test_flatten(self, layer_filenames, expected_flattened_output): |
| 64 | + # Construct a mock DockerImage with the specified layers: |
| 65 | + img = MockImage() |
| 66 | + for filenames in layer_filenames: |
| 67 | + img.add_layer(filenames) |
| 68 | + buf = io.BytesIO() |
| 69 | + |
| 70 | + # Run the actual extract logic: |
| 71 | + with tarfile.open(mode='w:', fileobj=buf) as tar: |
| 72 | + v2_2_image.extract(img, tar) |
| 73 | + |
| 74 | + # Compare the extract() output to the expected results: |
| 75 | + buf.seek(0) |
| 76 | + flattened_output = [] |
| 77 | + with tarfile.open(mode='r', fileobj=buf) as tar: |
| 78 | + for tarinfo in tar: |
| 79 | + if tarinfo.isdir(): |
| 80 | + flattened_output.append(tarinfo.name + "/") |
| 81 | + else: |
| 82 | + contents = tar.extractfile(tarinfo).read() |
| 83 | + if contents: |
| 84 | + flattened_output.append((tarinfo.name, contents)) |
| 85 | + else: |
| 86 | + flattened_output.append(tarinfo.name) |
| 87 | + self.assertEqual(flattened_output, expected_flattened_output) |
| 88 | + |
| 89 | + def test_single_layer(self): |
| 90 | + self._test_flatten( |
| 91 | + [["/directory/", "/file"]], |
| 92 | + ["/directory/", "/file"] |
| 93 | + ) |
| 94 | + |
| 95 | + def test_purely_additive_layers(self): |
| 96 | + self._test_flatten( |
| 97 | + [ |
| 98 | + ["dir/", "dir/file1", "file"], |
| 99 | + ["dir/file2", "file2"] |
| 100 | + ], |
| 101 | + ["dir/file2", "file2", "dir/", "dir/file1", "file"] |
| 102 | + ) |
| 103 | + |
| 104 | + def test_highest_layer_of_file_takes_precedence(self): |
| 105 | + self._test_flatten( |
| 106 | + [ |
| 107 | + [("file", "a")], |
| 108 | + [("file", "b")] |
| 109 | + ], |
| 110 | + [("file", "b")] |
| 111 | + ) |
| 112 | + |
| 113 | + def test_single_file_whiteout(self): |
| 114 | + self._test_flatten( |
| 115 | + [ |
| 116 | + ["/foo"], |
| 117 | + ["/.wh.foo"] |
| 118 | + ], |
| 119 | + [] |
| 120 | + ) |
| 121 | + |
| 122 | + def test_parent_directory_whiteout(self): |
| 123 | + self._test_flatten( |
| 124 | + [ |
| 125 | + ["/x/a/", "/x/b/", "/x/b/1"], |
| 126 | + ["/x/.wh.b"] |
| 127 | + ], |
| 128 | + ["/x/a/"] |
| 129 | + ) |
| 130 | + |
| 131 | + def test_opaque_whiteout(self): |
| 132 | + # Example from https://github.com/opencontainers/image-spec/blob/master/layer.md#whiteouts |
| 133 | + self._test_flatten( |
| 134 | + [ |
| 135 | + ["a/", "a/b/", "a/b/c/", "a/b/c/bar"], |
| 136 | + ["a/", "a/.wh..wh..opq", "a/b/", "a/b/c/", "a/b/c/foo"], |
| 137 | + ], |
| 138 | + ["a/", "a/b/", "a/b/c/", "a/b/c/foo"], |
| 139 | + ) |
| 140 | + |
| 141 | + self._test_flatten( |
| 142 | + [ |
| 143 | + ["a/", "a/b/", "a/b/c/", "a/b/c/bar"], |
| 144 | + ["a/", "a/b/", "a/b/c/", "a/b/c/foo", "a/.wh..wh..opq"], |
| 145 | + ], |
| 146 | + ["a/", "a/b/", "a/b/c/", "a/b/c/foo"], |
| 147 | + ) |
| 148 | + |
| 149 | + def test_opaque_whiteout_preserves_parent_directory(self): |
| 150 | + # Example from https://github.com/opencontainers/image-spec/blob/master/layer.md#whiteouts |
| 151 | + self._test_flatten( |
| 152 | + [ |
| 153 | + [ |
| 154 | + "bin/", |
| 155 | + "bin/my-app-binary", |
| 156 | + "bin/my-app-tools", |
| 157 | + "bin/tools/", |
| 158 | + "bin/tools/my-app-tool-one" |
| 159 | + ], |
| 160 | + ["bin/.wh..wh..opq"], |
| 161 | + ], |
| 162 | + ["bin/"], |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + unittest.main(verbosity=2) |
0 commit comments