...
1 package kclient
2
3 import (
4 "testing"
5
6 corev1 "k8s.io/api/core/v1"
7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8 "k8s.io/apimachinery/pkg/watch"
9 ktesting "k8s.io/client-go/testing"
10 )
11
12 func TestWaitForServiceAccountInNamespace(t *testing.T) {
13 tests := []struct {
14 name string
15 namespace string
16 serviceAccountName string
17 wantErr bool
18 }{
19 {
20 name: "Test case 1: with valid namespace and serviceAccountName",
21 namespace: "test-1",
22 serviceAccountName: "default",
23 wantErr: false,
24 },
25 {
26 name: "Test case 2: with no namespace and serviceAccountName",
27 namespace: "",
28 serviceAccountName: "",
29 wantErr: true,
30 },
31 }
32
33 for _, tt := range tests {
34 t.Run(tt.name, func(t *testing.T) {
35
36 client, fakeClientSet := FakeNew()
37 fkWatch := watch.NewFake()
38
39 go func() {
40 fkWatch.Add(&corev1.ServiceAccount{
41 ObjectMeta: metav1.ObjectMeta{
42 Name: tt.serviceAccountName,
43 },
44 })
45 }()
46
47 fakeClientSet.Kubernetes.PrependWatchReactor("serviceaccounts", func(action ktesting.Action) (handled bool, ret watch.Interface, err error) {
48 return true, fkWatch, nil
49 })
50
51 err := client.WaitForServiceAccountInNamespace(tt.namespace, tt.serviceAccountName)
52 if err == nil && !tt.wantErr {
53 if len(fakeClientSet.Kubernetes.Actions()) != 1 {
54 t.Errorf("expected 1 Kubernetes.Actions() in ServiceAccountName wait, got: %v", len(fakeClientSet.Kubernetes.Actions()))
55 }
56 }
57
58
59 if !tt.wantErr == (err != nil) {
60 t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr)
61 }
62 })
63 }
64 }
65
View as plain text