-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex_test.go
156 lines (128 loc) · 4.35 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package remote_test
import (
"os"
"testing"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"
remote2 "github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/imgutil"
"github.com/buildpacks/imgutil/remote"
h "github.com/buildpacks/imgutil/testhelpers"
)
func TestRemoteNewIndex(t *testing.T) {
dockerConfigDir, err := os.MkdirTemp("", "test.docker.config.remote.index.dir")
h.AssertNil(t, err)
defer os.RemoveAll(dockerConfigDir)
dockerRegistry = h.NewDockerRegistry(h.WithAuth(dockerConfigDir))
dockerRegistry.Start(t)
defer dockerRegistry.Stop(t)
os.Setenv("DOCKER_CONFIG", dockerConfigDir)
defer os.Unsetenv("DOCKER_CONFIG")
spec.Run(t, "RemoteNewIndex", testNewIndex, spec.Parallel(), spec.Report(report.Terminal{}))
}
const numberOfManifests = 2
func testNewIndex(t *testing.T, when spec.G, it spec.S) {
var (
idx imgutil.ImageIndex
manifests []v1.Hash
remoteIndexRepoName string
xdgPath string
err error
)
it.Before(func() {
// creates the directory to save all the OCI images on disk
remoteIndexRepoName = newTestImageIndexName("random")
randomIndex := setUpRandomRemoteIndex(t, remoteIndexRepoName, 1, numberOfManifests)
manifests = h.DigestsFromImageIndex(t, randomIndex)
})
it.After(func() {
err = os.RemoveAll(xdgPath)
h.AssertNil(t, err)
})
when("#NewIndex", func() {
it("should have expected indexOptions", func() {
idx, err = remote.NewIndex(
"some-index",
imgutil.WithInsecure(),
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.WithXDGRuntimePath(xdgPath),
)
h.AssertNil(t, err)
imgIx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)
h.AssertEq(t, imgIx.XdgPath, xdgPath)
h.AssertEq(t, imgIx.RepoName, "some-index")
})
it("should return an error when index with the given repoName doesn't exists", func() {
_, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex("some-none-existing-index"),
)
h.AssertNotEq(t, err, nil)
})
it("should return ImageIndex with expected output", func() {
idx, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex(remoteIndexRepoName),
)
h.AssertNil(t, err)
imgIx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)
mfest, err := imgIx.IndexManifest()
h.AssertNil(t, err)
h.AssertNotNil(t, mfest)
h.AssertEq(t, len(mfest.Manifests), numberOfManifests)
})
it("should able to call #ImageIndex", func() {
idx, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex(remoteIndexRepoName),
)
h.AssertNil(t, err)
imgIx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)
// some none existing hash
hash1, err := v1.NewHash(
"sha256:b9d056b83bb6446fee29e89a7fcf10203c562c1f59586a6e2f39c903597bda34",
)
h.AssertNil(t, err)
_, err = imgIx.ImageIndex.ImageIndex(hash1)
// err is "no child with digest"
h.AssertNotEq(t, err.Error(), "empty index")
})
it("should able to call #Image", func() {
idx, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex(remoteIndexRepoName),
)
h.AssertNil(t, err)
imgIdx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)
// select one valid digest from the index
_, err = imgIdx.Image(manifests[0])
h.AssertNil(t, err)
})
})
}
func newTestImageIndexName(name string) string {
return dockerRegistry.RepoName(name + "-" + h.RandString(10))
}
// setUpRandomRemoteIndex creates a random image index with the provided (count) number of manifest
// each manifest will have the provided number of layers
func setUpRandomRemoteIndex(t *testing.T, repoName string, layers, count int64) v1.ImageIndex {
ref, err := name.ParseReference(repoName, name.WeakValidation)
h.AssertNil(t, err)
randomIndex, err := random.Index(1024, layers, count)
h.AssertNil(t, err)
err = remote2.WriteIndex(ref, randomIndex, remote2.WithAuthFromKeychain(authn.DefaultKeychain))
h.AssertNil(t, err)
return randomIndex
}