Skip to content

Commit 4207b32

Browse files
committed
add tests
1 parent 8c77a36 commit 4207b32

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pkg/client/fake/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object,
11691169
tokenRequest.Status.Token = "fake-token"
11701170
tokenRequest.Status.ExpirationTimestamp = metav1.Date(6041, 1, 1, 0, 0, 0, 0, time.UTC)
11711171

1172-
return nil
1172+
return sw.client.Get(ctx, client.ObjectKeyFromObject(obj), obj)
11731173
default:
11741174
return fmt.Errorf("fakeSubResourceWriter does not support create for %s", sw.subResource)
11751175
}

pkg/client/fake/client_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
. "github.com/onsi/ginkgo/v2"
2828
. "github.com/onsi/gomega"
2929
appsv1 "k8s.io/api/apps/v1"
30+
authenticationv1 "k8s.io/api/authentication/v1"
3031
autoscalingv1 "k8s.io/api/autoscaling/v1"
3132
coordinationv1 "k8s.io/api/coordination/v1"
3233
corev1 "k8s.io/api/core/v1"
@@ -1959,6 +1960,42 @@ var _ = Describe("Fake client", func() {
19591960
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
19601961
})
19611962

1963+
It("should create a ServiceAccount token through the token subresource", func() {
1964+
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1965+
cl := NewClientBuilder().WithObjects(sa).Build()
1966+
1967+
tokenRequest := &authenticationv1.TokenRequest{}
1968+
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
1969+
Expect(err).NotTo(HaveOccurred())
1970+
1971+
Expect(tokenRequest.Status.Token).NotTo(Equal(""))
1972+
Expect(tokenRequest.Status.ExpirationTimestamp).NotTo(Equal(metav1.Time{}))
1973+
})
1974+
1975+
It("should return not found when creating a token for a ServiceAccount that doesn't exist", func() {
1976+
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1977+
cl := NewClientBuilder().Build()
1978+
1979+
tokenRequest := &authenticationv1.TokenRequest{}
1980+
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
1981+
Expect(err).To(HaveOccurred())
1982+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
1983+
})
1984+
1985+
1986+
1987+
It("should error when creating a token with the wrong subresource type", func() {
1988+
cl := NewClientBuilder().Build()
1989+
err := cl.SubResource("token").Create(context.Background(), &corev1.ServiceAccount{}, &corev1.Namespace{})
1990+
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
1991+
})
1992+
1993+
It("should error when creating a token with the wrong type", func() {
1994+
cl := NewClientBuilder().Build()
1995+
err := cl.SubResource("token").Create(context.Background(), &corev1.Secret{}, &corev1.Namespace{})
1996+
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
1997+
})
1998+
19621999
It("should leave typemeta empty on typed get", func() {
19632000
cl := NewClientBuilder().WithObjects(&corev1.Pod{ObjectMeta: metav1.ObjectMeta{
19642001
Namespace: "default",

0 commit comments

Comments
 (0)