|
| 1 | +// Copyright 2016 The Linux Foundation |
| 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 | + |
1 | 15 | package image |
2 | 16 |
|
3 | 17 | import ( |
4 | 18 | "archive/tar" |
5 | 19 | "bytes" |
6 | 20 | "compress/gzip" |
| 21 | + "crypto/sha256" |
| 22 | + "fmt" |
7 | 23 | "io" |
8 | 24 | "io/ioutil" |
9 | 25 | "os" |
@@ -48,3 +64,61 @@ func TestUnpackLayerDuplicateEntries(t *testing.T) { |
48 | 64 | t.Fatalf("Expected to fail with duplicate entry, got %v", err) |
49 | 65 | } |
50 | 66 | } |
| 67 | + |
| 68 | +func TestUnpackLayer(t *testing.T) { |
| 69 | + tmp1, err := ioutil.TempDir("", "test-layer") |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + defer os.RemoveAll(tmp1) |
| 74 | + err = os.MkdirAll(filepath.Join(tmp1, "blobs", "sha256"), 0700) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + tarfile := filepath.Join(tmp1, "blobs", "sha256", "test.tar") |
| 79 | + f, err := os.Create(tarfile) |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + |
| 84 | + gw := gzip.NewWriter(f) |
| 85 | + tw := tar.NewWriter(gw) |
| 86 | + |
| 87 | + tw.WriteHeader(&tar.Header{Name: "test", Size: 4, Mode: 0600}) |
| 88 | + io.Copy(tw, bytes.NewReader([]byte("test"))) |
| 89 | + tw.Close() |
| 90 | + gw.Close() |
| 91 | + f.Close() |
| 92 | + |
| 93 | + // generate sha256 hash |
| 94 | + h := sha256.New() |
| 95 | + file, err := os.Open(tarfile) |
| 96 | + if err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + defer file.Close() |
| 100 | + _, err = io.Copy(h, file) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + err = os.Rename(tarfile, filepath.Join(tmp1, "blobs", "sha256", fmt.Sprintf("%x", h.Sum(nil)))) |
| 105 | + if err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + |
| 109 | + testManifest := manifest{ |
| 110 | + Layers: []descriptor{descriptor{ |
| 111 | + MediaType: "application/vnd.oci.image.layer.tar+gzip", |
| 112 | + Digest: fmt.Sprintf("sha256:%s", fmt.Sprintf("%x", h.Sum(nil))), |
| 113 | + }}, |
| 114 | + } |
| 115 | + err = testManifest.unpack(newPathWalker(tmp1), filepath.Join(tmp1, "rootfs")) |
| 116 | + if err != nil { |
| 117 | + t.Fatal(err) |
| 118 | + } |
| 119 | + |
| 120 | + _, err = os.Stat(filepath.Join(tmp1, "rootfs", "test")) |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | +} |
0 commit comments