|
| 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