Skip to content

Commit 9c610d1

Browse files
authored
Merge pull request kubernetes#127768 from ii/create-serviceaccounttoken-test
Write e2e test for CoreV1 ServiceAccountToken +1 Endpoint
2 parents 0b1d123 + 28f17c6 commit 9c610d1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/e2e/auth/service_accounts.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,33 @@ var _ = SIGDescribe("ServiceAccounts", func() {
838838
gomega.Expect(updatedServiceAccount.AutomountServiceAccountToken).To(gomega.Equal(utilptr.Bool(true)), "Failed to set AutomountServiceAccountToken")
839839
framework.Logf("AutomountServiceAccountToken: %v", *updatedServiceAccount.AutomountServiceAccountToken)
840840
})
841+
842+
ginkgo.It("should create a serviceAccountToken and ensure a successful TokenReview", func(ctx context.Context) {
843+
ns := f.Namespace.Name
844+
saClient := f.ClientSet.CoreV1().ServiceAccounts(ns)
845+
saName := "e2e-sa-" + utilrand.String(5)
846+
847+
ginkgo.By(fmt.Sprintf("Creating a Serviceaccount %q in namespace %q", saName, ns))
848+
_, err := f.ClientSet.CoreV1().ServiceAccounts(ns).Create(context.TODO(), &v1.ServiceAccount{
849+
ObjectMeta: metav1.ObjectMeta{
850+
Name: saName,
851+
},
852+
}, metav1.CreateOptions{})
853+
framework.ExpectNoError(err, "Unable to create serviceaccount %q", saName)
854+
855+
ginkgo.By(fmt.Sprintf("Creating a ServiceaccountToken %q in namespace %q", saName, ns))
856+
request := &authenticationv1.TokenRequest{}
857+
response, err := saClient.CreateToken(context.TODO(), saName, request, metav1.CreateOptions{})
858+
framework.ExpectNoError(err, "Unable to create serviceAccountToken")
859+
gomega.Expect(response.Status.Token).ToNot(gomega.BeEmpty(), "confirm that a Token has been created")
860+
861+
ginkgo.By(fmt.Sprintf("Creating a TokenReview for %q in namespace %q", response.Name, ns))
862+
tokenReview := &authenticationv1.TokenReview{Spec: authenticationv1.TokenReviewSpec{Token: response.Status.Token}}
863+
tokenReview, err = f.ClientSet.AuthenticationV1().TokenReviews().Create(ctx, tokenReview, metav1.CreateOptions{})
864+
framework.ExpectNoError(err, "failed to create a TokenReview")
865+
gomega.Expect(tokenReview.Status.Authenticated).To(gomega.BeTrueBecause("expect that the TokenReview is authenticated"))
866+
gomega.Expect(tokenReview.Status.Error).To(gomega.BeEmpty(), "confirm that there are no TokenReview errors")
867+
})
841868
})
842869

843870
var reportLogsParser = regexp.MustCompile("([a-zA-Z0-9-_]*)=([a-zA-Z0-9-_]*)$")

0 commit comments

Comments
 (0)