|
| 1 | +package authentication |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + authenticationv1 "k8s.io/api/authentication/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/client-go/kubernetes/fake" |
| 15 | + ctest "k8s.io/client-go/testing" |
| 16 | +) |
| 17 | + |
| 18 | +func TestNewTokenGetterGet(t *testing.T) { |
| 19 | + fakeClient := fake.NewSimpleClientset() |
| 20 | + fakeClient.PrependReactor("create", "serviceaccounts/token", |
| 21 | + func(action ctest.Action) (bool, runtime.Object, error) { |
| 22 | + act, ok := action.(ctest.CreateActionImpl) |
| 23 | + if !ok { |
| 24 | + return false, nil, nil |
| 25 | + } |
| 26 | + tokenRequest := act.GetObject().(*authenticationv1.TokenRequest) |
| 27 | + var err error |
| 28 | + if act.Name == "test-service-account-1" { |
| 29 | + tokenRequest.Status = authenticationv1.TokenRequestStatus{ |
| 30 | + Token: "test-token-1", |
| 31 | + ExpirationTimestamp: metav1.NewTime(metav1.Now().Add(5 * time.Minute)), |
| 32 | + } |
| 33 | + } |
| 34 | + if act.Name == "test-service-account-2" { |
| 35 | + tokenRequest.Status = authenticationv1.TokenRequestStatus{ |
| 36 | + Token: "test-token-2", |
| 37 | + ExpirationTimestamp: metav1.NewTime(metav1.Now().Add(1 * time.Second)), |
| 38 | + } |
| 39 | + } |
| 40 | + if act.Name == "test-service-account-3" { |
| 41 | + tokenRequest = nil |
| 42 | + err = fmt.Errorf("error when fetching token") |
| 43 | + } |
| 44 | + return true, tokenRequest, err |
| 45 | + }) |
| 46 | + |
| 47 | + tg := NewTokenGetter(fakeClient.CoreV1(), |
| 48 | + WithExpirationSeconds(int64(5*time.Minute)), |
| 49 | + WithRotationThreshold(1*time.Minute)) |
| 50 | + |
| 51 | + tests := []struct { |
| 52 | + testName string |
| 53 | + serviceAccountName string |
| 54 | + namespace string |
| 55 | + want string |
| 56 | + errorMsg string |
| 57 | + }{ |
| 58 | + {"Testing getting token with fake client", "test-service-account-1", |
| 59 | + "test-namespace-1", "test-token-1", "failed to get token"}, |
| 60 | + {"Testing getting token from cache", "test-service-account-1", |
| 61 | + "test-namespace-1", "test-token-1", "failed to get token from cache"}, |
| 62 | + {"Testing getting short lived token from fake client", "test-service-account-2", |
| 63 | + "test-namespace-2", "test-token-2", "failed to get token"}, |
| 64 | + {"Testing getting expired token from cache", "test-service-account-2", |
| 65 | + "test-namespace-2", "test-token-2", "failed to refresh token"}, |
| 66 | + {"Testing error when getting token from fake client", "test-service-account-3", |
| 67 | + "test-namespace-3", "error when fetching token", "error when fetching token"}, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tc := range tests { |
| 71 | + got, err := tg.Get(context.Background(), types.NamespacedName{Namespace: tc.namespace, Name: tc.serviceAccountName}) |
| 72 | + if err != nil { |
| 73 | + t.Logf("%s: expected: %v, got: %v", tc.testName, tc.want, err) |
| 74 | + assert.EqualError(t, err, tc.errorMsg) |
| 75 | + } else { |
| 76 | + t.Logf("%s: expected: %v, got: %v", tc.testName, tc.want, got) |
| 77 | + assert.Equal(t, tc.want, got, tc.errorMsg) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestNewTokenGetterClean(t *testing.T) { |
| 83 | + fakeClient := fake.NewSimpleClientset() |
| 84 | + fakeClient.PrependReactor("create", "serviceaccounts/token", |
| 85 | + func(action ctest.Action) (bool, runtime.Object, error) { |
| 86 | + act, ok := action.(ctest.CreateActionImpl) |
| 87 | + if !ok { |
| 88 | + return false, nil, nil |
| 89 | + } |
| 90 | + tokenRequest := act.GetObject().(*authenticationv1.TokenRequest) |
| 91 | + if act.Name == "test-service-account-1" { |
| 92 | + tokenRequest.Status = authenticationv1.TokenRequestStatus{ |
| 93 | + Token: "test-token-1", |
| 94 | + ExpirationTimestamp: metav1.NewTime(metav1.Now().Add(5 * time.Minute)), |
| 95 | + } |
| 96 | + } |
| 97 | + return true, tokenRequest, nil |
| 98 | + }) |
| 99 | + |
| 100 | + tg := NewTokenGetter(fakeClient.CoreV1(), |
| 101 | + WithExpirationSeconds(int64(5*time.Minute)), |
| 102 | + WithRotationThreshold(1*time.Minute)) |
| 103 | + |
| 104 | + _, err := tg.Get(context.Background(), types.NamespacedName{Namespace: "test-namespace-1", Name: "test-service-account-1"}) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("failed to get token: %v", err) |
| 107 | + } |
| 108 | + assert.True(t, tg.TokenExists(context.Background(), types.NamespacedName{Namespace: "test-namespace-1", Name: "test-service-account-1"})) |
| 109 | + t.Logf("Testing removing token from cache") |
| 110 | + tg.Clean(context.Background(), types.NamespacedName{Namespace: "test-namespace-1", Name: "test-service-account-1"}) |
| 111 | + assert.False(t, tg.TokenExists(context.Background(), types.NamespacedName{Namespace: "test-namespace-1", Name: "test-service-account-1"})) |
| 112 | +} |
0 commit comments