...

Source file src/k8s.io/apiextensions-apiserver/test/integration/limit_test.go

Documentation: k8s.io/apiextensions-apiserver/test/integration

     1  /*
     2  Copyright 2019 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 integration
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    26  	"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    27  	"k8s.io/apiextensions-apiserver/test/integration/fixtures"
    28  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    29  	"k8s.io/apimachinery/pkg/types"
    30  	"k8s.io/client-go/dynamic"
    31  )
    32  
    33  func TestLimits(t *testing.T) {
    34  	tearDown, config, _, err := fixtures.StartDefaultServer(t)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer tearDown()
    39  
    40  	apiExtensionClient, err := clientset.NewForConfig(config)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	dynamicClient, err := dynamic.NewForConfig(config)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	noxuDefinition := fixtures.NewNoxuV1CustomResourceDefinition(apiextensionsv1.ClusterScoped)
    50  	noxuDefinition, err = fixtures.CreateNewV1CustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	kind := noxuDefinition.Spec.Names.Kind
    56  	apiVersion := noxuDefinition.Spec.Group + "/" + noxuDefinition.Spec.Versions[0].Name
    57  
    58  	rest := apiExtensionClient.Discovery().RESTClient()
    59  
    60  	// Create YAML over 3MB limit
    61  	t.Run("create YAML over limit", func(t *testing.T) {
    62  		yamlBody := []byte(fmt.Sprintf(`
    63  apiVersion: %s
    64  kind: %s
    65  metadata:
    66    name: test
    67  values: `+strings.Repeat("[", 3*1024*1024), apiVersion, kind))
    68  
    69  		_, err := rest.Post().
    70  			SetHeader("Accept", "application/yaml").
    71  			SetHeader("Content-Type", "application/yaml").
    72  			AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
    73  			Body(yamlBody).
    74  			DoRaw(context.TODO())
    75  		if !apierrors.IsRequestEntityTooLargeError(err) {
    76  			t.Errorf("expected too large error, got %v", err)
    77  		}
    78  	})
    79  
    80  	// Create YAML just under 3MB limit, nested
    81  	t.Run("create YAML doc under limit, nested", func(t *testing.T) {
    82  		if testing.Short() {
    83  			t.Skip("skipping expensive test")
    84  		}
    85  		yamlBody := []byte(fmt.Sprintf(`
    86  	apiVersion: %s
    87  	kind: %s
    88  	metadata:
    89  	  name: test
    90  	values: `+strings.Repeat("[", 3*1024*1024/2-500)+strings.Repeat("]", 3*1024*1024/2-500), apiVersion, kind))
    91  
    92  		_, err := rest.Post().
    93  			SetHeader("Accept", "application/yaml").
    94  			SetHeader("Content-Type", "application/yaml").
    95  			AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
    96  			Body(yamlBody).
    97  			DoRaw(context.TODO())
    98  		if !apierrors.IsBadRequest(err) {
    99  			t.Errorf("expected bad request, got %v", err)
   100  		}
   101  	})
   102  
   103  	// Create YAML just under 3MB limit, not nested
   104  	t.Run("create YAML doc under limit, not nested", func(t *testing.T) {
   105  		if testing.Short() {
   106  			t.Skip("skipping expensive test")
   107  		}
   108  		yamlBody := []byte(fmt.Sprintf(`
   109  		apiVersion: %s
   110  		kind: %s
   111  		metadata:
   112  		  name: test
   113  		values: `+strings.Repeat("[", 3*1024*1024-1000), apiVersion, kind))
   114  
   115  		_, err := rest.Post().
   116  			SetHeader("Accept", "application/yaml").
   117  			SetHeader("Content-Type", "application/yaml").
   118  			AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
   119  			Body(yamlBody).
   120  			DoRaw(context.TODO())
   121  		if !apierrors.IsBadRequest(err) {
   122  			t.Errorf("expected bad request, got %v", err)
   123  		}
   124  	})
   125  
   126  	// Create JSON over 3MB limit
   127  	t.Run("create JSON over limit", func(t *testing.T) {
   128  		jsonBody := []byte(fmt.Sprintf(`{
   129  	"apiVersion": %q,
   130  	"kind": %q,
   131  	"metadata": {
   132  	  "name": "test"
   133  	},
   134  	"values": `+strings.Repeat("[", 3*1024*1024/2)+strings.Repeat("]", 3*1024*1024/2)+"}", apiVersion, kind))
   135  
   136  		_, err := rest.Post().
   137  			SetHeader("Accept", "application/json").
   138  			SetHeader("Content-Type", "application/json").
   139  			AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
   140  			Body(jsonBody).
   141  			DoRaw(context.TODO())
   142  		if !apierrors.IsRequestEntityTooLargeError(err) {
   143  			t.Errorf("expected too large error, got %v", err)
   144  		}
   145  	})
   146  
   147  	// Create JSON just under 3MB limit, nested
   148  	t.Run("create JSON doc under limit, nested", func(t *testing.T) {
   149  		if testing.Short() {
   150  			t.Skip("skipping expensive test")
   151  		}
   152  		jsonBody := []byte(fmt.Sprintf(`{
   153  		"apiVersion": %q,
   154  		"kind": %q,
   155  		"metadata": {
   156  		  "name": "test"
   157  		},
   158  		"values": `+strings.Repeat("[", 3*1024*1024/2-500)+strings.Repeat("]", 3*1024*1024/2-500)+"}", apiVersion, kind))
   159  
   160  		_, err := rest.Post().
   161  			SetHeader("Accept", "application/json").
   162  			SetHeader("Content-Type", "application/json").
   163  			AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
   164  			Body(jsonBody).
   165  			DoRaw(context.TODO())
   166  		if !apierrors.IsBadRequest(err) {
   167  			t.Errorf("expected bad request, got %v", err)
   168  		}
   169  	})
   170  
   171  	// Create JSON just under 3MB limit, not nested
   172  	t.Run("create JSON doc under limit, not nested", func(t *testing.T) {
   173  		if testing.Short() {
   174  			t.Skip("skipping expensive test")
   175  		}
   176  		jsonBody := []byte(fmt.Sprintf(`{
   177  			"apiVersion": %q,
   178  			"kind": %q,
   179  			"metadata": {
   180  			  "name": "test"
   181  			},
   182  			"values": `+strings.Repeat("[", 3*1024*1024-1000)+"}", apiVersion, kind))
   183  
   184  		_, err := rest.Post().
   185  			SetHeader("Accept", "application/json").
   186  			SetHeader("Content-Type", "application/json").
   187  			AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
   188  			Body(jsonBody).
   189  			DoRaw(context.TODO())
   190  		if !apierrors.IsBadRequest(err) {
   191  			t.Errorf("expected bad request, got %v", err)
   192  		}
   193  	})
   194  
   195  	// Create instance to allow patching
   196  	{
   197  		jsonBody := []byte(fmt.Sprintf(`{"apiVersion": %q, "kind": %q, "metadata": {"name": "test"}}`, apiVersion, kind))
   198  		_, err := rest.Post().AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).Body(jsonBody).DoRaw(context.TODO())
   199  		if err != nil {
   200  			t.Fatalf("error creating object: %v", err)
   201  		}
   202  	}
   203  
   204  	t.Run("JSONPatchType nested patch under limit", func(t *testing.T) {
   205  		if testing.Short() {
   206  			t.Skip("skipping expensive test")
   207  		}
   208  		patchBody := []byte(`[{"op":"add","path":"/foo","value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}]`)
   209  		err = rest.Patch(types.JSONPatchType).AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural, "test").
   210  			Body(patchBody).Do(context.TODO()).Error()
   211  		if !apierrors.IsBadRequest(err) {
   212  			t.Errorf("expected success or bad request err, got %v", err)
   213  		}
   214  	})
   215  	t.Run("MergePatchType nested patch under limit", func(t *testing.T) {
   216  		if testing.Short() {
   217  			t.Skip("skipping expensive test")
   218  		}
   219  		patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
   220  		err = rest.Patch(types.MergePatchType).AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural, "test").
   221  			Body(patchBody).Do(context.TODO()).Error()
   222  		if !apierrors.IsBadRequest(err) {
   223  			t.Errorf("expected success or bad request err, got %v", err)
   224  		}
   225  	})
   226  	t.Run("ApplyPatchType nested patch under limit", func(t *testing.T) {
   227  		if testing.Short() {
   228  			t.Skip("skipping expensive test")
   229  		}
   230  		patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
   231  		err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural, "test").
   232  			Body(patchBody).Do(context.TODO()).Error()
   233  		if !apierrors.IsBadRequest(err) {
   234  			t.Errorf("expected bad request err, got %#v", err)
   235  		}
   236  	})
   237  }
   238  

View as plain text