This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
map_test.go
96 lines (80 loc) · 2.32 KB
/
map_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
/*
Copyright 2023 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package kontext
import (
"context"
"testing"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
)
type descriptorImpl struct {
i v1.Image
ii v1.ImageIndex
err error
}
func (di *descriptorImpl) ImageIndex() (v1.ImageIndex, error) {
return di.ii, di.err
}
func (di *descriptorImpl) Image() (v1.Image, error) {
return di.i, di.err
}
func TestBundleIndex(t *testing.T) {
want := int64(5)
remoteGet = func(name.Reference, ...remote.Option) (types.MediaType, descriptor, error) {
ii, err := random.Index(3, 4, want)
return types.OCIImageIndex, &descriptorImpl{ii: ii}, err
}
remoteWriteIndex = func(name.Reference, v1.ImageIndex, ...remote.Option) error {
return nil
}
remoteWrite = func(name.Reference, v1.Image, ...remote.Option) error {
t.Error("Unexpected call to remoteWrite")
return nil
}
source, _ := name.NewTag("ghcr.io/blah/blurg")
tag, _ := name.NewTag("gcr.io/buffoon/banana")
got := int64(0)
_, err := Map(context.Background(), source, tag,
func(ctx context.Context, img v1.Image) (v1.Image, error) {
got++
return img, nil
})
if err != nil {
t.Error("Map() =", err)
}
if got != want {
t.Errorf("callback called %d times, wanted %d", got, want)
}
}
func TestBundleImage(t *testing.T) {
remoteGet = func(name.Reference, ...remote.Option) (types.MediaType, descriptor, error) {
i, err := random.Image(3, 4)
return types.OCIManifestSchema1, &descriptorImpl{i: i}, err
}
remoteWriteIndex = func(name.Reference, v1.ImageIndex, ...remote.Option) error {
t.Error("Unexpected call to remoteWriteIndex")
return nil
}
remoteWrite = func(name.Reference, v1.Image, ...remote.Option) error {
return nil
}
source, _ := name.NewTag("ghcr.io/blah/blurg")
tag, _ := name.NewTag("gcr.io/buffoon/banana")
got := int64(0)
_, err := Map(context.Background(), source, tag,
func(ctx context.Context, img v1.Image) (v1.Image, error) {
got++
return img, nil
})
if err != nil {
t.Error("Map() =", err)
}
if want := int64(1); got != want {
t.Errorf("callback called %d times, wanted %d", got, want)
}
}