mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-12-05 07:26:56 +00:00
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:
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package apiserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -31,11 +30,8 @@ import (
|
||||
|
||||
// Tests that the apiserver limits the resource size in write operations.
|
||||
func TestMaxResourceSize(t *testing.T) {
|
||||
_, ctx := ktesting.NewTestContext(t)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
clientSet, _, tearDownFn := framework.StartTestServer(ctx, t, framework.TestServerSetup{})
|
||||
tCtx := ktesting.Init(t)
|
||||
clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{})
|
||||
defer tearDownFn()
|
||||
|
||||
hugeData := []byte(strings.Repeat("x", 3*1024*1024+1))
|
||||
@@ -45,7 +41,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
c := clientSet.CoreV1().RESTClient()
|
||||
t.Run("Create should limit the request body size", func(t *testing.T) {
|
||||
err := c.Post().AbsPath("/api/v1/namespaces/default/pods").
|
||||
Body(hugeData).Do(ctx).Error()
|
||||
Body(hugeData).Do(tCtx).Error()
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected no error")
|
||||
}
|
||||
@@ -61,14 +57,14 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
Name: "test",
|
||||
},
|
||||
}
|
||||
_, err := clientSet.CoreV1().Secrets("default").Create(ctx, secret, metav1.CreateOptions{})
|
||||
_, err := clientSet.CoreV1().Secrets("default").Create(tCtx, secret, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Run("Update should limit the request body size", func(t *testing.T) {
|
||||
err = c.Put().AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(hugeData).Do(ctx).Error()
|
||||
Body(hugeData).Do(tCtx).Error()
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected no error")
|
||||
}
|
||||
@@ -79,7 +75,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
})
|
||||
t.Run("Patch should limit the request body size", func(t *testing.T) {
|
||||
err = c.Patch(types.JSONPatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(hugeData).Do(ctx).Error()
|
||||
Body(hugeData).Do(tCtx).Error()
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected no error")
|
||||
}
|
||||
@@ -94,7 +90,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`[{"op":"add","path":"/foo","value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}]`)
|
||||
err = rest.Patch(types.JSONPatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil && !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %v", err)
|
||||
}
|
||||
@@ -105,7 +101,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`[{"op":"add","path":"/foo","value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}]`)
|
||||
err = rest.Patch(types.JSONPatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -116,7 +112,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
|
||||
err = rest.Patch(types.MergePatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil && !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %v", err)
|
||||
}
|
||||
@@ -127,7 +123,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`{"value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
|
||||
err = rest.Patch(types.MergePatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -138,7 +134,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
|
||||
err = rest.Patch(types.StrategicMergePatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil && !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %v", err)
|
||||
}
|
||||
@@ -149,7 +145,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`{"value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
|
||||
err = rest.Patch(types.StrategicMergePatchType).AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -160,7 +156,7 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
|
||||
err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil && !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %#v", err)
|
||||
}
|
||||
@@ -171,14 +167,14 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
}
|
||||
patchBody := []byte(`{"apiVersion":"v1","kind":"Secret"` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
|
||||
err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(patchBody).Do(ctx).Error()
|
||||
Body(patchBody).Do(tCtx).Error()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("Delete should limit the request body size", func(t *testing.T) {
|
||||
err = c.Delete().AbsPath("/api/v1/namespaces/default/secrets/test").
|
||||
Body(hugeData).Do(ctx).Error()
|
||||
Body(hugeData).Do(tCtx).Error()
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected no error")
|
||||
}
|
||||
@@ -202,7 +198,7 @@ values: ` + strings.Repeat("[", 3*1024*1024))
|
||||
SetHeader("Content-Type", "application/yaml").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(yamlBody).
|
||||
DoRaw(ctx)
|
||||
DoRaw(tCtx)
|
||||
if !apierrors.IsRequestEntityTooLargeError(err) {
|
||||
t.Errorf("expected too large error, got %v", err)
|
||||
}
|
||||
@@ -225,7 +221,7 @@ values: ` + strings.Repeat("[", 3*1024*1024/2-500) + strings.Repeat("]", 3*1024*
|
||||
SetHeader("Content-Type", "application/yaml").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(yamlBody).
|
||||
DoRaw(ctx)
|
||||
DoRaw(tCtx)
|
||||
if !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
@@ -248,7 +244,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
|
||||
SetHeader("Content-Type", "application/yaml").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(yamlBody).
|
||||
DoRaw(ctx)
|
||||
DoRaw(tCtx)
|
||||
if !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
@@ -269,7 +265,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
|
||||
SetHeader("Content-Type", "application/json").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(jsonBody).
|
||||
DoRaw(ctx)
|
||||
DoRaw(tCtx)
|
||||
if !apierrors.IsRequestEntityTooLargeError(err) {
|
||||
t.Errorf("expected too large error, got %v", err)
|
||||
}
|
||||
@@ -293,7 +289,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
|
||||
SetHeader("Content-Type", "application/json").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(jsonBody).
|
||||
DoRaw(ctx)
|
||||
DoRaw(tCtx)
|
||||
// TODO(liggitt): expect bad request on deep nesting, rather than success on dropped unknown field data
|
||||
if err != nil && !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
@@ -318,7 +314,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
|
||||
SetHeader("Content-Type", "application/json").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(jsonBody).
|
||||
DoRaw(ctx)
|
||||
DoRaw(tCtx)
|
||||
if !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user