-
Notifications
You must be signed in to change notification settings - Fork 182
[WIP]CNTRLPLANE-2072:Migrating TestTokenRequestAndReview to ginkgo #1978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gangwgr
wants to merge
2
commits into
openshift:main
Choose a base branch
from
gangwgr:ote-migration-sa-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−51
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package e2e | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| g "github.com/onsi/ginkgo/v2" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| authenticationv1 "k8s.io/api/authentication/v1" | ||
| v1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| testlibrary "github.com/openshift/cluster-kube-apiserver-operator/test/library" | ||
| ) | ||
|
|
||
| // TestingT is the minimal common interface that both testing.TB and | ||
| // ginkgo.GinkgoTInterface implement. This allows test functions to be | ||
| // called from both standard Go tests and Ginkgo tests. | ||
| type TestingT interface { | ||
| Errorf(format string, args ...interface{}) | ||
| FailNow() | ||
| Helper() | ||
| } | ||
|
|
||
| var _ = g.Describe("[sig-api-machinery] kube-apiserver operator", func() { | ||
| g.It("[Operator][Serial] TestTokenRequestAndReview", func() { | ||
| testTokenRequestAndReview(g.GinkgoT()) | ||
| }) | ||
| }) | ||
|
|
||
| // testTokenRequestAndReview checks that bound sa tokens are correctly | ||
| // configured. A token is requested via the TokenRequest API and | ||
| // validated via the TokenReview API. | ||
| func testTokenRequestAndReview(t TestingT) { | ||
| kubeConfig, err := testlibrary.NewClientConfigForTest() | ||
| require.NoError(t, err) | ||
| kubeClient, err := kubernetes.NewForConfig(kubeConfig) | ||
| require.NoError(t, err) | ||
| corev1client := kubeClient.CoreV1() | ||
|
|
||
| // Create all test resources in a temp namespace that will be | ||
| // removed at the end of the test to avoid requiring explicit | ||
| // cleanup. | ||
| ns, err := corev1client.Namespaces().Create(context.TODO(), &v1.Namespace{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| GenerateName: "e2e-token-request-", | ||
| }, | ||
| }, metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| err := corev1client.Namespaces().Delete(context.TODO(), ns.Name, metav1.DeleteOptions{}) | ||
| require.NoError(t, err) | ||
| }() | ||
| namespace := ns.Name | ||
|
|
||
| sa, err := corev1client.ServiceAccounts(namespace).Create(context.TODO(), &v1.ServiceAccount{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-service-account", | ||
| }, | ||
| }, metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| treq, err := corev1client.ServiceAccounts(sa.Namespace).CreateToken(context.TODO(), | ||
| sa.Name, | ||
| &authenticationv1.TokenRequest{ | ||
| Spec: authenticationv1.TokenRequestSpec{ | ||
| // Avoid specifying any audiences so that the token will be | ||
| // issued for the default audience of the issuer. | ||
| }, | ||
| }, | ||
| metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| trev, err := kubeClient.AuthenticationV1().TokenReviews().Create(context.TODO(), &authenticationv1.TokenReview{ | ||
| Spec: authenticationv1.TokenReviewSpec{ | ||
| Token: treq.Status.Token, | ||
| }, | ||
| }, metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
| require.Empty(t, trev.Status.Error) | ||
| require.True(t, trev.Status.Authenticated) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid
requirein defer for cleanup.Using
require.NoError(which callsFailNow()) inside a defer can cause issues—if the test already failed, it may produce confusing output, andFailNow()in a defer can behave unexpectedly in some contexts.Consider logging the error instead or using
assert.NoErrorwhich doesn't abort:defer func() { err := corev1client.Namespaces().Delete(context.TODO(), ns.Name, metav1.DeleteOptions{}) - require.NoError(t, err) + if err != nil { + t.Errorf("failed to delete namespace %s: %v", ns.Name, err) + } }()📝 Committable suggestion
🤖 Prompt for AI Agents