This commit is contained in:
Mike Danese
2020-01-27 18:19:44 -08:00
parent 59e757afef
commit d55d6175f8
105 changed files with 415 additions and 325 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package apiserver
import (
"context"
"fmt"
"strings"
"testing"
@@ -41,7 +42,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(fmt.Sprintf("/api/v1/namespaces/default/pods")).
Body(hugeData).Do().Error()
Body(hugeData).Do(context.TODO()).Error()
if err == nil {
t.Fatalf("unexpected no error")
}
@@ -64,7 +65,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("Update should limit the request body size", func(t *testing.T) {
err = c.Put().AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(hugeData).Do().Error()
Body(hugeData).Do(context.TODO()).Error()
if err == nil {
t.Fatalf("unexpected no error")
}
@@ -75,7 +76,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(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(hugeData).Do().Error()
Body(hugeData).Do(context.TODO()).Error()
if err == nil {
t.Fatalf("unexpected no error")
}
@@ -87,7 +88,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("JSONPatchType should handle a patch just under the max limit", func(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(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil && !apierrors.IsBadRequest(err) {
t.Errorf("expected success or bad request err, got %v", err)
}
@@ -95,7 +96,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("JSONPatchType should handle a valid patch just under the max limit", func(t *testing.T) {
patchBody := []byte(`[{"op":"add","path":"/foo","value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}]`)
err = rest.Patch(types.JSONPatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@@ -103,7 +104,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("MergePatchType should handle a patch just under the max limit", func(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(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil && !apierrors.IsBadRequest(err) {
t.Errorf("expected success or bad request err, got %v", err)
}
@@ -111,7 +112,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("MergePatchType should handle a valid patch just under the max limit", func(t *testing.T) {
patchBody := []byte(`{"value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
err = rest.Patch(types.MergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@@ -119,7 +120,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("StrategicMergePatchType should handle a patch just under the max limit", func(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(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil && !apierrors.IsBadRequest(err) {
t.Errorf("expected success or bad request err, got %v", err)
}
@@ -127,7 +128,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("StrategicMergePatchType should handle a valid patch just under the max limit", func(t *testing.T) {
patchBody := []byte(`{"value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
err = rest.Patch(types.StrategicMergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@@ -135,7 +136,7 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("ApplyPatchType should handle a patch just under the max limit", func(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(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).Error()
if err != nil && !apierrors.IsBadRequest(err) {
t.Errorf("expected success or bad request err, got %#v", err)
}
@@ -143,14 +144,14 @@ func TestMaxResourceSize(t *testing.T) {
t.Run("ApplyPatchType should handle a valid patch just under the max limit", func(t *testing.T) {
patchBody := []byte(`{"apiVersion":"v1","kind":"Secret"` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(patchBody).Do().Error()
Body(patchBody).Do(context.TODO()).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(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
Body(hugeData).Do().Error()
Body(hugeData).Do(context.TODO()).Error()
if err == nil {
t.Fatalf("unexpected no error")
}
@@ -174,7 +175,7 @@ values: ` + strings.Repeat("[", 3*1024*1024))
SetHeader("Content-Type", "application/yaml").
AbsPath("/api/v1/namespaces/default/configmaps").
Body(yamlBody).
DoRaw()
DoRaw(context.TODO())
if !apierrors.IsRequestEntityTooLargeError(err) {
t.Errorf("expected too large error, got %v", err)
}
@@ -194,7 +195,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()
DoRaw(context.TODO())
if !apierrors.IsBadRequest(err) {
t.Errorf("expected bad request, got %v", err)
}
@@ -214,7 +215,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
SetHeader("Content-Type", "application/yaml").
AbsPath("/api/v1/namespaces/default/configmaps").
Body(yamlBody).
DoRaw()
DoRaw(context.TODO())
if !apierrors.IsBadRequest(err) {
t.Errorf("expected bad request, got %v", err)
}
@@ -235,7 +236,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
SetHeader("Content-Type", "application/json").
AbsPath("/api/v1/namespaces/default/configmaps").
Body(jsonBody).
DoRaw()
DoRaw(context.TODO())
if !apierrors.IsRequestEntityTooLargeError(err) {
t.Errorf("expected too large error, got %v", err)
}
@@ -256,7 +257,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
SetHeader("Content-Type", "application/json").
AbsPath("/api/v1/namespaces/default/configmaps").
Body(jsonBody).
DoRaw()
DoRaw(context.TODO())
// 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)
@@ -278,7 +279,7 @@ values: ` + strings.Repeat("[", 3*1024*1024-1000))
SetHeader("Content-Type", "application/json").
AbsPath("/api/v1/namespaces/default/configmaps").
Body(jsonBody).
DoRaw()
DoRaw(context.TODO())
if !apierrors.IsBadRequest(err) {
t.Errorf("expected bad request, got %v", err)
}