...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package walk
17
18 import (
19 "context"
20 "errors"
21 "testing"
22
23 "github.com/google/go-containerregistry/pkg/v1/mutate"
24 "github.com/google/go-containerregistry/pkg/v1/random"
25 "github.com/sigstore/cosign/v2/pkg/oci"
26 "github.com/sigstore/cosign/v2/pkg/oci/signed"
27 )
28
29 func TestMapImage(t *testing.T) {
30 i, err := random.Image(300 , 3 )
31 if err != nil {
32 t.Fatalf("random.Image() = %v", err)
33 }
34 si := signed.Image(i)
35
36 t.Run("walk image, no errors", func(t *testing.T) {
37 calls := 0
38 err := SignedEntity(context.Background(), si, func(_ context.Context, _ oci.SignedEntity) error {
39 calls++
40 return nil
41 })
42 if err != nil {
43 t.Fatalf("Map() = %v", err)
44 }
45 if calls != 1 {
46 t.Fatalf("Map called %d times, wanted 1", calls)
47 }
48 })
49
50 t.Run("error propagates", func(t *testing.T) {
51 want := errors.New("this is the error I expect")
52 got := SignedEntity(context.Background(), si, func(_ context.Context, _ oci.SignedEntity) error {
53 return want
54 })
55 if !errors.Is(got, want) {
56 t.Fatalf("Map() = %v, wanted %v", got, want)
57 }
58 })
59 }
60
61 func TestMapImageIndex(t *testing.T) {
62 ii, err := random.Index(300 , 3 , 2 )
63 if err != nil {
64 t.Fatalf("random.Image() = %v", err)
65 }
66 ii2, err := random.Index(300 , 3 , 2 )
67 if err != nil {
68 t.Fatalf("random.Image() = %v", err)
69 }
70 sii := signed.ImageIndex(mutate.AppendManifests(ii, mutate.IndexAddendum{
71 Add: ii2,
72 }))
73
74 t.Run("six calls to identity mutator", func(t *testing.T) {
75 calls := 0
76 err := SignedEntity(context.Background(), sii, func(_ context.Context, _ oci.SignedEntity) error {
77 calls++
78 return nil
79 })
80 if err != nil {
81 t.Fatalf("Map() = %v", err)
82 }
83 if calls != 6 {
84 t.Fatalf("Map called %d times, wanted 6", calls)
85 }
86 })
87 }
88
View as plain text