...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package remote
17
18 import (
19 "fmt"
20 "testing"
21
22 "github.com/google/go-containerregistry/pkg/name"
23 v1 "github.com/google/go-containerregistry/pkg/v1"
24 "github.com/google/go-containerregistry/pkg/v1/random"
25 "github.com/google/go-containerregistry/pkg/v1/remote"
26 "github.com/sigstore/cosign/v2/pkg/oci/mutate"
27 "github.com/sigstore/cosign/v2/pkg/oci/signed"
28 "github.com/sigstore/cosign/v2/pkg/oci/static"
29 )
30
31 func TestWriteSignatures(t *testing.T) {
32 rw := remote.Write
33 t.Cleanup(func() {
34 remoteWrite = rw
35 })
36 i, err := random.Image(300 , 7 )
37 if err != nil {
38 t.Fatalf("random.Image() = %v", err)
39 }
40 si := signed.Image(i)
41
42 want := 6
43 for i := 0; i < want; i++ {
44 sig, err := static.NewSignature(nil, fmt.Sprintf("%d", i))
45 if err != nil {
46 t.Fatalf("static.NewSignature() = %v", err)
47 }
48 si, err = mutate.AttachSignatureToImage(si, sig)
49 if err != nil {
50 t.Fatalf("SignEntity() = %v", err)
51 }
52 }
53
54 ref := name.MustParseReference("gcr.io/bistroless/static:nonroot")
55
56 remoteWrite = func(_ name.Reference, img v1.Image, _ ...remote.Option) error {
57 l, err := img.Layers()
58 if err != nil {
59 return err
60 }
61
62 if got := len(l); got != want {
63 t.Errorf("got %d layers, wanted %d", got, want)
64 }
65
66 return nil
67 }
68 if err := WriteSignatures(ref.Context(), si); err != nil {
69 t.Fatalf("WriteSignature() = %v", err)
70 }
71 }
72
73 func TestWriteAttestations(t *testing.T) {
74 rw := remote.Write
75 t.Cleanup(func() {
76 remoteWrite = rw
77 })
78 i, err := random.Image(300 , 7 )
79 if err != nil {
80 t.Fatalf("random.Image() = %v", err)
81 }
82 si := signed.Image(i)
83
84 want := 6
85 for i := 0; i < want; i++ {
86 sig, err := static.NewAttestation([]byte(fmt.Sprintf("%d", i)))
87 if err != nil {
88 t.Fatalf("static.NewSignature() = %v", err)
89 }
90 si, err = mutate.AttachAttestationToImage(si, sig)
91 if err != nil {
92 t.Fatalf("SignEntity() = %v", err)
93 }
94 }
95
96 ref := name.MustParseReference("gcr.io/bistroless/static:nonroot")
97
98 remoteWrite = func(_ name.Reference, img v1.Image, _ ...remote.Option) error {
99 l, err := img.Layers()
100 if err != nil {
101 return err
102 }
103
104 if got := len(l); got != want {
105 t.Errorf("got %d layers, wanted %d", got, want)
106 }
107
108 return nil
109 }
110 if err := WriteAttestations(ref.Context(), si); err != nil {
111 t.Fatalf("WriteAttestations() = %v", err)
112 }
113 }
114
View as plain text