Skip to content

Commit 8d0ee71

Browse files
authored
Merge pull request #257 from coolljt0725/fix_media_type
Fix media type on unpacking layer
2 parents 61989fe + 0a76d61 commit 8d0ee71

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

image/manifest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (m *manifest) validate(w walker) error {
9090

9191
func (m *manifest) unpack(w walker, dest string) error {
9292
for _, d := range m.Layers {
93-
if d.MediaType != string(schema.MediaTypeImageConfig) {
93+
if d.MediaType != string(schema.MediaTypeImageLayer) {
9494
continue
9595
}
9696

image/manifest_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
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+
115
package image
216

317
import (
418
"archive/tar"
519
"bytes"
620
"compress/gzip"
21+
"crypto/sha256"
22+
"fmt"
723
"io"
824
"io/ioutil"
925
"os"
@@ -48,3 +64,61 @@ func TestUnpackLayerDuplicateEntries(t *testing.T) {
4864
t.Fatalf("Expected to fail with duplicate entry, got %v", err)
4965
}
5066
}
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

Comments
 (0)