Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glennpratt committed Oct 4, 2024
1 parent 8c77a36 commit b3ab5db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object,
tokenRequest.Status.Token = "fake-token"
tokenRequest.Status.ExpirationTimestamp = metav1.Date(6041, 1, 1, 0, 0, 0, 0, time.UTC)

return nil
return sw.client.Get(ctx, client.ObjectKeyFromObject(obj), obj)
default:
return fmt.Errorf("fakeSubResourceWriter does not support create for %s", sw.subResource)
}
Expand Down
33 changes: 31 additions & 2 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
authenticationv1 "k8s.io/api/authentication/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -1953,9 +1954,37 @@ var _ = Describe("Fake client", func() {
})
}

It("should error when creating an eviction with the wrong type", func() {
It("should create a ServiceAccount token through the token subresource", func() {
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
cl := NewClientBuilder().WithObjects(sa).Build()

tokenRequest := &authenticationv1.TokenRequest{}
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
Expect(err).NotTo(HaveOccurred())

Expect(tokenRequest.Status.Token).NotTo(Equal(""))
Expect(tokenRequest.Status.ExpirationTimestamp).NotTo(Equal(metav1.Time{}))
})

It("should return not found when creating a token for a ServiceAccount that doesn't exist", func() {
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
cl := NewClientBuilder().Build()

tokenRequest := &authenticationv1.TokenRequest{}
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
Expect(err).To(HaveOccurred())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("should error when creating a token with the wrong subresource type", func() {
cl := NewClientBuilder().Build()
err := cl.SubResource("token").Create(context.Background(), &corev1.ServiceAccount{}, &corev1.Namespace{})
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
})

It("should error when creating a token with the wrong type", func() {
cl := NewClientBuilder().Build()
err := cl.SubResource("eviction").Create(context.Background(), &corev1.Pod{}, &corev1.Namespace{})
err := cl.SubResource("token").Create(context.Background(), &corev1.Secret{}, &corev1.Namespace{})
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
})

Expand Down

0 comments on commit b3ab5db

Please sign in to comment.