|
| 1 | +/* |
| 2 | +Copyright 2026 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/fluxcd/pkg/apis/meta" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 29 | +) |
| 30 | + |
| 31 | +func TestReadTrustedRootFromSecret(t *testing.T) { |
| 32 | + scheme := runtime.NewScheme() |
| 33 | + _ = corev1.AddToScheme(scheme) |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + namespace string |
| 38 | + ref *meta.LocalObjectReference |
| 39 | + secret *corev1.Secret |
| 40 | + wantData []byte |
| 41 | + wantErr string |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "reads trusted_root.json from secret", |
| 45 | + namespace: "default", |
| 46 | + ref: &meta.LocalObjectReference{Name: "sigstore-root"}, |
| 47 | + secret: &corev1.Secret{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "sigstore-root", |
| 50 | + Namespace: "default", |
| 51 | + }, |
| 52 | + Data: map[string][]byte{ |
| 53 | + "trusted_root.json": []byte(`{"mediaType":"application/vnd.dev.sigstore.trustedroot+json;version=0.1"}`), |
| 54 | + }, |
| 55 | + }, |
| 56 | + wantData: []byte(`{"mediaType":"application/vnd.dev.sigstore.trustedroot+json;version=0.1"}`), |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "error when secret does not exist", |
| 60 | + namespace: "default", |
| 61 | + ref: &meta.LocalObjectReference{Name: "missing-secret"}, |
| 62 | + wantErr: `"missing-secret" not found`, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "error when key is missing from secret", |
| 66 | + namespace: "default", |
| 67 | + ref: &meta.LocalObjectReference{Name: "no-key-secret"}, |
| 68 | + secret: &corev1.Secret{ |
| 69 | + ObjectMeta: metav1.ObjectMeta{ |
| 70 | + Name: "no-key-secret", |
| 71 | + Namespace: "default", |
| 72 | + }, |
| 73 | + Data: map[string][]byte{ |
| 74 | + "other-key": []byte("data"), |
| 75 | + }, |
| 76 | + }, |
| 77 | + wantErr: "'trusted_root.json' not found in secret", |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tt := range tests { |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + g := NewWithT(t) |
| 84 | + |
| 85 | + builder := fake.NewClientBuilder().WithScheme(scheme) |
| 86 | + if tt.secret != nil { |
| 87 | + builder = builder.WithObjects(tt.secret) |
| 88 | + } |
| 89 | + c := builder.Build() |
| 90 | + |
| 91 | + data, err := readTrustedRootFromSecret(context.Background(), c, tt.namespace, tt.ref) |
| 92 | + |
| 93 | + if tt.wantErr != "" { |
| 94 | + g.Expect(err).To(HaveOccurred()) |
| 95 | + g.Expect(err.Error()).To(ContainSubstring(tt.wantErr)) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + g.Expect(err).NotTo(HaveOccurred()) |
| 100 | + g.Expect(data).To(Equal(tt.wantData)) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments