@@ -14,6 +14,7 @@ import (
1414 batchv1 "k8s.io/api/batch/v1"
1515 corev1 "k8s.io/api/core/v1"
1616 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+ "k8s.io/client-go/tools/record"
1718 "sigs.k8s.io/controller-runtime/pkg/client"
1819 clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
1920
@@ -56,7 +57,7 @@ func TestRedisArtifact(t *testing.T) {
5657 t .Run ("stats the snapshot with the projected credentials" , func (t * testing.T ) {
5758 l := & fakeS3Lister {statSize : 2048 }
5859 r := redisReconciler (t , l , projectedCredsSecret (nil ))
59- got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , nil )
60+ got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , nil )
6061 if got == nil || got .URI != "s3://cozy-backups/" + redisTestKey || got .SizeBytes != 2048 {
6162 t .Fatalf ("artifact = %#v" , got )
6263 }
@@ -87,7 +88,7 @@ func TestRedisArtifact(t *testing.T) {
8788 if tc .noTarget {
8889 r .CredentialsConfig .TargetSecretName = ""
8990 }
90- if got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , nil ); got != nil {
91+ if got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , nil ); got != nil {
9192 t .Errorf ("artifact = %#v, want nil" , got )
9293 }
9394 })
@@ -124,11 +125,11 @@ func TestRedisArtifact_MinioClient(t *testing.T) {
124125 r := & BackupJobReconciler {Client : c , Scheme : c .Scheme ()}
125126 r .CredentialsConfig .TargetSecretName = "cozy-backups-creds"
126127
127- if got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , & corev1.PodSpec {}); got != nil {
128+ if got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , & corev1.PodSpec {}); got != nil {
128129 t .Errorf ("without the Job's trust settings: artifact = %#v, want nil (untrusted certificate)" , got )
129130 }
130131
131- got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("s3-endpoint-ca" , "" ))
132+ got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("s3-endpoint-ca" , "" ))
132133 if got == nil || got .SizeBytes != 4096 {
133134 t .Fatalf ("with the s3-ca volume: artifact = %#v, want SizeBytes 4096" , got )
134135 }
@@ -139,15 +140,15 @@ func TestRedisArtifact_MinioClient(t *testing.T) {
139140 t .Errorf ("request not signed with the Secret's access key: %q" , gotAuth )
140141 }
141142
142- if got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("" , "true" )); got == nil || got .SizeBytes != 4096 {
143+ if got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("" , "true" )); got == nil || got .SizeBytes != 4096 {
143144 t .Errorf ("with S3_INSECURE_SKIP_VERIFY=true: artifact = %#v, want SizeBytes 4096" , got )
144145 }
145146
146147 ca .Data ["ca.crt" ] = unrelatedCAPEM (t )
147148 if err := c .Update (context .Background (), ca ); err != nil {
148149 t .Fatalf ("update CA Secret: %v" , err )
149150 }
150- if got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("s3-endpoint-ca" , "true" )); got != nil {
151+ if got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("s3-endpoint-ca" , "true" )); got != nil {
151152 t .Errorf ("with a CA that did not sign the endpoint and S3_INSECURE_SKIP_VERIFY=true: artifact = %#v, want nil" , got )
152153 }
153154}
@@ -314,6 +315,40 @@ func TestCreateRedisBackupArtifact_Artifact(t *testing.T) {
314315 }
315316 })
316317
318+ t .Run ("an unmeasured artifact is reported on the BackupJob without the raw error" , func (t * testing.T ) {
319+ l := & fakeS3Lister {statErr : errors .New ("Get https://s3.internal.example/cozy-backups: NoSuchKey" )}
320+ r := redisReconciler (t , l , j , projectedCredsSecret (nil ))
321+ rec := record .NewFakeRecorder (4 )
322+ r .Recorder = rec
323+ got , err := r .createRedisBackupArtifact (context .Background (), j , & ResolvedBackupConfig {}, redisTestKey , nil )
324+ if err != nil || got == nil || got .Status .Artifact != nil {
325+ t .Fatalf ("createRedisBackupArtifact = %#v, %v; want a Backup without an artifact" , got , err )
326+ }
327+ select {
328+ case ev := <- rec .Events :
329+ if ! strings .HasPrefix (ev , "Normal ArtifactNotMeasured " ) || ! strings .Contains (ev , "could not be found or read" ) {
330+ t .Errorf ("event = %q, want Normal ArtifactNotMeasured with the reason" , ev )
331+ }
332+ if strings .Contains (ev , "s3.internal.example" ) {
333+ t .Errorf ("event = %q leaks the raw S3 error into the application namespace" , ev )
334+ }
335+ default :
336+ t .Error ("no event on the BackupJob for an artifact left unset" )
337+ }
338+ })
339+
340+ t .Run ("a measured artifact raises no event" , func (t * testing.T ) {
341+ r := redisReconciler (t , & fakeS3Lister {statSize : 1 }, j , projectedCredsSecret (nil ))
342+ rec := record .NewFakeRecorder (4 )
343+ r .Recorder = rec
344+ if _ , err := r .createRedisBackupArtifact (context .Background (), j , & ResolvedBackupConfig {}, redisTestKey , nil ); err != nil {
345+ t .Fatalf ("createRedisBackupArtifact: %v" , err )
346+ }
347+ if len (rec .Events ) != 0 {
348+ t .Errorf ("event %q for an artifact that was recorded" , <- rec .Events )
349+ }
350+ })
351+
317352 t .Run ("an existing Backup is returned without probing" , func (t * testing.T ) {
318353 existing := & backupsv1alpha1.Backup {ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "bj-1" }}
319354 l := & fakeS3Lister {statSize : 777 }
@@ -445,7 +480,7 @@ func TestRedisArtifact_HangingEndpointIsGivenUp(t *testing.T) {
445480 r := redisReconciler (t , & fakeS3Lister {}, projectedCredsSecret (nil ))
446481 r .newS3ObjectLister = func (s3Connection ) (s3ObjectLister , error ) { return blockingLister {}, nil }
447482 withShortProbeTimeout (t , func () {
448- if got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , nil ); got != nil {
483+ if got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , nil ); got != nil {
449484 t .Errorf ("artifact = %#v, want nil" , got )
450485 }
451486 })
@@ -458,7 +493,7 @@ func TestRedisArtifact_TrustsOnlyTheCA(t *testing.T) {
458493 Data : map [string ][]byte {"ca.crt" : []byte ("PEM" )},
459494 }
460495 r := redisReconciler (t , l , projectedCredsSecret (nil ), ca )
461- if got := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("s3-endpoint-ca" , "" )); got == nil {
496+ if got , _ := r .redisArtifact (context .Background (), "tenant" , redisTestKey , redisPodSpec ("s3-endpoint-ca" , "" )); got == nil {
462497 t .Fatal ("artifact = nil" )
463498 }
464499 if ! l .conn .CAOnly {
0 commit comments