Skip to content

Commit 092caf0

Browse files
authored
Add static.NewLayer (#1093)
* Add static.NewLayer * Document that static layer contents aren't compressed * remove unnecessary comment * update copyright year
1 parent 4759a5d commit 092caf0

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

Diff for: pkg/v1/static/layer.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2021 Google LLC 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+
package static
16+
17+
import (
18+
"bytes"
19+
"io"
20+
"io/ioutil"
21+
"sync"
22+
23+
v1 "github.com/google/go-containerregistry/pkg/v1"
24+
"github.com/google/go-containerregistry/pkg/v1/types"
25+
)
26+
27+
// NewLayer returns a layer containing the given bytes, with the given mediaType.
28+
//
29+
// Contents will not be compressed.
30+
func NewLayer(b []byte, mt types.MediaType) v1.Layer {
31+
return &staticLayer{b: b, mt: mt}
32+
}
33+
34+
type staticLayer struct {
35+
b []byte
36+
mt types.MediaType
37+
38+
once sync.Once
39+
h v1.Hash
40+
}
41+
42+
func (l *staticLayer) Digest() (v1.Hash, error) {
43+
var err error
44+
// Only calculate digest the first time we're asked.
45+
l.once.Do(func() {
46+
l.h, _, err = v1.SHA256(bytes.NewReader(l.b))
47+
})
48+
return l.h, err
49+
}
50+
51+
func (l *staticLayer) DiffID() (v1.Hash, error) {
52+
return l.Digest()
53+
}
54+
55+
func (l *staticLayer) Compressed() (io.ReadCloser, error) {
56+
return ioutil.NopCloser(bytes.NewReader(l.b)), nil
57+
}
58+
59+
func (l *staticLayer) Uncompressed() (io.ReadCloser, error) {
60+
return ioutil.NopCloser(bytes.NewReader(l.b)), nil
61+
}
62+
63+
func (l *staticLayer) Size() (int64, error) {
64+
return int64(len(l.b)), nil
65+
}
66+
67+
func (l *staticLayer) MediaType() (types.MediaType, error) {
68+
return l.mt, nil
69+
}

Diff for: pkg/v1/static/static_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2021 Google LLC 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+
package static
16+
17+
import (
18+
"io/ioutil"
19+
"strings"
20+
"testing"
21+
22+
v1 "github.com/google/go-containerregistry/pkg/v1"
23+
"github.com/google/go-containerregistry/pkg/v1/types"
24+
"github.com/google/go-containerregistry/pkg/v1/validate"
25+
)
26+
27+
func TestNewLayer(t *testing.T) {
28+
b := []byte(strings.Repeat(".", 10))
29+
l := NewLayer(b, types.OCILayer)
30+
31+
// This does basically nothing.
32+
if err := validate.Layer(l, validate.Fast); err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
// Digest and DiffID match, and match expectations.
37+
h, err := l.Digest()
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
h2, err := l.DiffID()
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
if h != h2 {
46+
t.Errorf("Digest != DiffID; digest is %v, diffid is %v", h, h2)
47+
}
48+
wantDigest, err := v1.NewHash("sha256:537f3fb69ba01fc388a3a5c920c485b2873d5f327305c3dd2004d6a04451659b")
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
if h != wantDigest {
53+
t.Errorf("Digest mismatch; got %v, want %v", h, wantDigest)
54+
}
55+
56+
sz, err := l.Size()
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
if sz != 10 {
61+
t.Errorf("Size mismatch; got %d, want %d", sz, 10)
62+
}
63+
64+
mt, err := l.MediaType()
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
if mt != types.OCILayer {
69+
t.Errorf("MediaType mismatch; got %v, want %v", mt, types.OCILayer)
70+
}
71+
72+
r, err := l.Uncompressed()
73+
if err != nil {
74+
t.Fatal(err)
75+
}
76+
got, err := ioutil.ReadAll(r)
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
if string(got) != string(b) {
81+
t.Errorf("Contents mismatch: got %q, want %q", string(got), string(b))
82+
}
83+
}

0 commit comments

Comments
 (0)