Skip to content

Commit 1024342

Browse files
committed
add tests
1 parent 8c77a36 commit 1024342

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-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: 35 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"
@@ -1953,12 +1954,46 @@ var _ = Describe("Fake client", func() {
19531954
})
19541955
}
19551956

1957+
It("should create a ServiceAccount token through the token subresource", func() {
1958+
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1959+
cl := NewClientBuilder().WithObjects(sa).Build()
1960+
1961+
tokenRequest := &authenticationv1.TokenRequest{}
1962+
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
1963+
Expect(err).NotTo(HaveOccurred())
1964+
1965+
Expect(tokenRequest.Status.Token).NotTo(Equal(""))
1966+
Expect(tokenRequest.Status.ExpirationTimestamp).NotTo(Equal(metav1.Time{}))
1967+
})
1968+
1969+
It("should return not found when creating a token for a ServiceAccount that doesn't exist", func() {
1970+
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1971+
cl := NewClientBuilder().Build()
1972+
1973+
tokenRequest := &authenticationv1.TokenRequest{}
1974+
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
1975+
Expect(err).To(HaveOccurred())
1976+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
1977+
})
1978+
19561979
It("should error when creating an eviction with the wrong type", func() {
19571980
cl := NewClientBuilder().Build()
19581981
err := cl.SubResource("eviction").Create(context.Background(), &corev1.Pod{}, &corev1.Namespace{})
19591982
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
19601983
})
19611984

1985+
It("should error when creating a token with the wrong subresource type", func() {
1986+
cl := NewClientBuilder().Build()
1987+
err := cl.SubResource("token").Create(context.Background(), &corev1.ServiceAccount{}, &corev1.Namespace{})
1988+
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
1989+
})
1990+
1991+
It("should error when creating a token with the wrong type", func() {
1992+
cl := NewClientBuilder().Build()
1993+
err := cl.SubResource("token").Create(context.Background(), &corev1.Secret{}, &corev1.Namespace{})
1994+
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
1995+
})
1996+
19621997
It("should leave typemeta empty on typed get", func() {
19631998
cl := NewClientBuilder().WithObjects(&corev1.Pod{ObjectMeta: metav1.ObjectMeta{
19641999
Namespace: "default",

0 commit comments

Comments
 (0)