@@ -28,6 +28,8 @@ import (
2828 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929 "k8s.io/apimachinery/pkg/runtime"
3030 fakek8s "k8s.io/client-go/kubernetes/fake"
31+ "k8s.io/client-go/tools/record"
32+ "knative.dev/pkg/controller"
3133 logtesting "knative.dev/pkg/logging/testing"
3234 "knative.dev/pkg/system"
3335)
@@ -49,6 +51,7 @@ func TestCredsInit(t *testing.T) {
4951 wantVolumeMounts []corev1.VolumeMount
5052 objs []runtime.Object
5153 envVars []corev1.EnvVar
54+ events []string
5255 ctx context.Context
5356 }{{
5457 desc : "service account exists with no secrets; nothing to initialize" ,
@@ -288,11 +291,52 @@ func TestCredsInit(t *testing.T) {
288291 MountPath : "/tekton/creds-secrets/my-creds" ,
289292 }},
290293 ctx : context .Background (),
294+ }, {
295+ desc : "service account has not found secrets" ,
296+ objs : []runtime.Object {
297+ & corev1.ServiceAccount {
298+ ObjectMeta : metav1.ObjectMeta {Name : serviceAccountName , Namespace : namespace },
299+ Secrets : []corev1.ObjectReference {
300+ {Name : "my-creds" },
301+ {Name : "not-exist-1" },
302+ {Name : "not-exist-2" },
303+ },
304+ },
305+ & corev1.Secret {
306+ ObjectMeta : metav1.ObjectMeta {
307+ Name : "my-creds" ,
308+ Namespace : namespace ,
309+ Annotations : map [string ]string {
310+ "tekton.dev/git-0" : "github.com" ,
311+ },
312+ },
313+ Type : "kubernetes.io/basic-auth" ,
314+ Data : map [string ][]byte {
315+ "username" : []byte ("foo" ),
316+ "password" : []byte ("BestEver" ),
317+ },
318+ },
319+ },
320+ envVars : []corev1.EnvVar {},
321+ wantArgs : []string {
322+ "-basic-git=my-creds=github.com" ,
323+ },
324+ wantVolumeMounts : []corev1.VolumeMount {{
325+ Name : "tekton-internal-secret-volume-my-creds-9l9zj" ,
326+ MountPath : "/tekton/creds-secrets/my-creds" ,
327+ }},
328+ events : []string {
329+ `Warning FailedToRetrieveSecret Unable to retrieve some secrets (not-exist-1, not-exist-2); attempting to use them may not succeed.` ,
330+ },
331+ ctx : context .Background (),
291332 }} {
292333 t .Run (c .desc , func (t * testing.T ) {
293334 names .TestingSeed ()
335+ eventObj := & corev1.Event {}
294336 kubeclient := fakek8s .NewSimpleClientset (c .objs ... )
295- args , volumes , volumeMounts , err := credsInit (c .ctx , serviceAccountName , namespace , kubeclient )
337+ recorder := record .NewFakeRecorder (1000 )
338+ c .ctx = controller .WithEventRecorder (c .ctx , recorder )
339+ args , volumes , volumeMounts , err := credsInit (c .ctx , eventObj , serviceAccountName , namespace , kubeclient )
296340 if err != nil {
297341 t .Fatalf ("credsInit: %v" , err )
298342 }
@@ -305,6 +349,15 @@ func TestCredsInit(t *testing.T) {
305349 if d := cmp .Diff (c .wantVolumeMounts , volumeMounts ); d != "" {
306350 t .Fatalf ("Diff %s" , diff .PrintWantGot (d ))
307351 }
352+ if len (recorder .Events ) != len (c .events ) {
353+ t .Fatalf ("Expected %d events, got %d" , len (c .events ), len (recorder .Events ))
354+ }
355+ for i , e := range c .events {
356+ message := <- recorder .Events
357+ if message != e {
358+ t .Fatalf ("Expected event %d to be %q, got %q" , i , e , message )
359+ }
360+ }
308361 })
309362 }
310363}
0 commit comments