...

Source file src/k8s.io/kubernetes/test/integration/apiserver/max_json_patch_operations_test.go

Documentation: k8s.io/kubernetes/test/integration/apiserver

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package apiserver
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/types"
    28  	"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
    29  	"k8s.io/kubernetes/test/integration/framework"
    30  	"k8s.io/kubernetes/test/utils/ktesting"
    31  )
    32  
    33  // Tests that the apiserver limits the number of operations in a json patch.
    34  func TestMaxJSONPatchOperations(t *testing.T) {
    35  	tCtx := ktesting.Init(t)
    36  
    37  	clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
    38  		ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
    39  			opts.GenericServerRunOptions.MaxRequestBodyBytes = 1024 * 1024
    40  		},
    41  	})
    42  	defer tearDownFn()
    43  
    44  	p := `{"op":"add","path":"/x","value":"y"}`
    45  	// maxJSONPatchOperations = 10000
    46  	hugePatch := []byte("[" + strings.Repeat(p+",", 10000) + p + "]")
    47  
    48  	c := clientSet.CoreV1().RESTClient()
    49  	// Create a secret so we can patch it.
    50  	secret := &v1.Secret{
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name: "test",
    53  		},
    54  	}
    55  	_, err := clientSet.CoreV1().Secrets("default").Create(tCtx, secret, metav1.CreateOptions{})
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	err = c.Patch(types.JSONPatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
    61  		Body(hugePatch).Do(tCtx).Error()
    62  	if err == nil {
    63  		t.Fatalf("unexpected no error")
    64  	}
    65  	if !apierrors.IsRequestEntityTooLargeError(err) {
    66  		t.Errorf("expected requested entity too large err, got %v", err)
    67  	}
    68  	if !strings.Contains(err.Error(), "The allowed maximum operations in a JSON patch is") {
    69  		t.Errorf("expected the error message to be about maximum operations, got %v", err)
    70  	}
    71  }
    72  

View as plain text