forked from google/go-containerregistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
133 lines (119 loc) · 3.43 KB
/
index_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mutate_test
import (
"log"
"strings"
"testing"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/google/go-containerregistry/pkg/v1/validate"
)
func TestAppendIndex(t *testing.T) {
base, err := random.Index(1024, 3, 3)
if err != nil {
t.Fatal(err)
}
idx, err := random.Index(2048, 1, 2)
if err != nil {
t.Fatal(err)
}
img, err := random.Image(4096, 5)
if err != nil {
t.Fatal(err)
}
l, err := random.Layer(1024, types.OCIUncompressedRestrictedLayer)
if err != nil {
t.Fatal(err)
}
weirdHash := v1.Hash{
Algorithm: "sha256",
Hex: strings.Repeat("0", 64),
}
add := mutate.AppendManifests(base, mutate.IndexAddendum{
Add: idx,
Descriptor: v1.Descriptor{
URLs: []string{"index.example.com"},
},
}, mutate.IndexAddendum{
Add: img,
Descriptor: v1.Descriptor{
MediaType: types.MediaType("application/xml"),
URLs: []string{"image.example.com"},
},
}, mutate.IndexAddendum{
Add: l,
Descriptor: v1.Descriptor{
URLs: []string{"layer.example.com"},
Size: 1337,
Digest: weirdHash,
Platform: &v1.Platform{
OS: "haiku",
Architecture: "toaster",
},
Annotations: map[string]string{"weird": "true"},
},
})
if err := validate.Index(add); err != nil {
t.Errorf("Validate() = %v", err)
}
got, err := add.MediaType()
if err != nil {
t.Fatal(err)
}
want, err := base.MediaType()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("MediaType() = %s != %s", got, want)
}
// TODO(jonjohnsonjr): There's no way to grab layers from v1.ImageIndex.
m, err := add.IndexManifest()
if err != nil {
log.Fatal(err)
}
for i, want := range map[int]string{
3: "index.example.com",
4: "image.example.com",
5: "layer.example.com",
} {
if got := m.Manifests[i].URLs[0]; got != want {
t.Errorf("wrong URLs[0] for Manifests[%d]: %s != %s", i, got, want)
}
}
if got, want := m.Manifests[4].MediaType, types.MediaType("application/xml"); got != want {
t.Errorf("wrong MediaType for layer: %s != %s", got, want)
}
if got, want := m.Manifests[5].MediaType, types.OCIUncompressedRestrictedLayer; got != want {
t.Errorf("wrong MediaType for layer: %s != %s", got, want)
}
// Append the index to itself and make sure it still validates.
add = mutate.AppendManifests(add, mutate.IndexAddendum{
Add: add,
})
if err := validate.Index(add); err != nil {
t.Errorf("Validate() = %v", err)
}
// Wrap the whole thing in another index and make sure it still validates.
add = mutate.AppendManifests(empty.Index, mutate.IndexAddendum{
Add: add,
})
if err := validate.Index(add); err != nil {
t.Errorf("Validate() = %v", err)
}
}