test: use cancelation from ktesting

The return type of ktesting.NewTestContext is now a TContext. Code
which combined it WithCancel often didn't compile anymore (cannot overwrite
ktesting.TContext with context.Context). This is a good thing because all of
that code can be simplified to let ktesting handle the cancelation.
This commit is contained in:
Patrick Ohly
2023-12-25 19:40:56 +01:00
parent 3df07e446b
commit 1d653e6185
34 changed files with 458 additions and 695 deletions

View File

@@ -17,7 +17,6 @@ limitations under the License.
package apiserver
import (
"context"
"sync/atomic"
"testing"
"time"
@@ -42,11 +41,8 @@ func TestWebhookLoopback(t *testing.T) {
called := int32(0)
_, ctx := ktesting.NewTestContext(t)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
client, _, tearDownFn := framework.StartTestServer(ctx, t, framework.TestServerSetup{
tCtx := ktesting.Init(t)
client, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
},
ModifyServerConfig: func(config *controlplane.Config) {
@@ -72,7 +68,7 @@ func TestWebhookLoopback(t *testing.T) {
fail := admissionregistrationv1.Fail
noSideEffects := admissionregistrationv1.SideEffectClassNone
_, err := client.AdmissionregistrationV1().MutatingWebhookConfigurations().Create(ctx, &admissionregistrationv1.MutatingWebhookConfiguration{
_, err := client.AdmissionregistrationV1().MutatingWebhookConfigurations().Create(tCtx, &admissionregistrationv1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{Name: "webhooktest.example.com"},
Webhooks: []admissionregistrationv1.MutatingWebhook{{
Name: "webhooktest.example.com",
@@ -93,7 +89,7 @@ func TestWebhookLoopback(t *testing.T) {
}
err = wait.PollImmediate(100*time.Millisecond, 30*time.Second, func() (done bool, err error) {
_, err = client.CoreV1().ConfigMaps("default").Create(ctx, &v1.ConfigMap{
_, err = client.CoreV1().ConfigMaps("default").Create(tCtx, &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: "webhook-test"},
Data: map[string]string{"invalid key": "value"},
}, metav1.CreateOptions{})