|
| 1 | +package authentication |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + authv1 "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 TestNewTokenGetter(t *testing.T) { |
| 19 | + fakeClient := fake.NewSimpleClientset() |
| 20 | + fakeClient.PrependReactor("create", "serviceaccounts/token", func(action ctest.Action) (handled bool, ret runtime.Object, err error) { |
| 21 | + act, ok := action.(ctest.CreateActionImpl) |
| 22 | + if !ok { |
| 23 | + return false, nil, nil |
| 24 | + } |
| 25 | + tokenRequest := act.GetObject().(*authv1.TokenRequest) |
| 26 | + if act.Name == "test-service-account-1" { |
| 27 | + tokenRequest.Status = authv1.TokenRequestStatus{ |
| 28 | + Token: "test-token-1", |
| 29 | + ExpirationTimestamp: metav1.NewTime(metav1.Now().Add(5 * time.Minute)), |
| 30 | + } |
| 31 | + } |
| 32 | + if act.Name == "test-service-account-2" { |
| 33 | + tokenRequest.Status = authv1.TokenRequestStatus{ |
| 34 | + Token: "test-token-2", |
| 35 | + ExpirationTimestamp: metav1.NewTime(metav1.Now().Add(1 * time.Second)), |
| 36 | + } |
| 37 | + } |
| 38 | + if act.Name == "test-service-account-3" { |
| 39 | + tokenRequest = nil |
| 40 | + err = fmt.Errorf("error when fetching token") |
| 41 | + } |
| 42 | + |
| 43 | + return true, tokenRequest, err |
| 44 | + }) |
| 45 | + tg := NewTokenGetter(fakeClient.CoreV1(), int64(5*time.Minute)) |
| 46 | + t.Log("Testing NewTokenGetter with fake client") |
| 47 | + token, err := tg.Get(context.Background(), types.NamespacedName{ |
| 48 | + Namespace: "test-namespace-1", |
| 49 | + Name: "test-service-account-1", |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("failed to get token: %v", err) |
| 53 | + return |
| 54 | + } |
| 55 | + t.Log("token:", token) |
| 56 | + if token != "test-token-1" { |
| 57 | + t.Errorf("token does not match") |
| 58 | + } |
| 59 | + t.Log("Testing getting token from cache") |
| 60 | + token, err = tg.Get(context.Background(), types.NamespacedName{ |
| 61 | + Namespace: "test-namespace-1", |
| 62 | + Name: "test-service-account-1", |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("failed to get token from cache: %v", err) |
| 66 | + return |
| 67 | + } |
| 68 | + t.Log("token:", token) |
| 69 | + if token != "test-token-1" { |
| 70 | + t.Errorf("token does not match") |
| 71 | + } |
| 72 | + t.Log("Testing getting short lived token from fake client") |
| 73 | + token, err = tg.Get(context.Background(), types.NamespacedName{ |
| 74 | + Namespace: "test-namespace-2", |
| 75 | + Name: "test-service-account-2", |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("failed to get token: %v", err) |
| 79 | + return |
| 80 | + } |
| 81 | + t.Log("token:", token) |
| 82 | + if token != "test-token-2" { |
| 83 | + t.Errorf("token does not match") |
| 84 | + } |
| 85 | + //wait for token to expire |
| 86 | + time.Sleep(1 * time.Second) |
| 87 | + t.Log("Testing getting expired token from cache") |
| 88 | + token, err = tg.Get(context.Background(), types.NamespacedName{ |
| 89 | + Namespace: "test-namespace-2", |
| 90 | + Name: "test-service-account-2", |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("failed to refresh token: %v", err) |
| 94 | + return |
| 95 | + } |
| 96 | + t.Log("token:", token) |
| 97 | + if token != "test-token-2" { |
| 98 | + t.Errorf("token does not match") |
| 99 | + } |
| 100 | + t.Log("Testing error when getting token from fake client") |
| 101 | + token, err = tg.Get(context.Background(), types.NamespacedName{ |
| 102 | + Namespace: "test-namespace-3", |
| 103 | + Name: "test-service-account-3", |
| 104 | + }) |
| 105 | + assert.EqualError(t, err, "error when fetching token") |
| 106 | +} |
0 commit comments