max_request_body_bytes_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apiserver
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/kubernetes/cmd/kube-apiserver/app/options"
  23. "k8s.io/kubernetes/test/integration/framework"
  24. )
  25. // Tests that the apiserver limits the resource size in write operations.
  26. func TestMaxResourceSize(t *testing.T) {
  27. stopCh := make(chan struct{})
  28. defer close(stopCh)
  29. clientSet, _ := framework.StartTestServer(t, stopCh, framework.TestServerSetup{
  30. ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
  31. opts.GenericServerRunOptions.MaxRequestBodyBytes = 1024 * 1024
  32. },
  33. })
  34. hugeData := []byte(strings.Repeat("x", 1024*1024+1))
  35. c := clientSet.CoreV1().RESTClient()
  36. t.Run("Create should limit the request body size", func(t *testing.T) {
  37. err := c.Post().AbsPath(fmt.Sprintf("/api/v1/namespaces/default/pods")).
  38. Body(hugeData).Do().Error()
  39. if err == nil {
  40. t.Fatalf("unexpected no error")
  41. }
  42. if !errors.IsRequestEntityTooLargeError(err) {
  43. t.Errorf("expected requested entity too large err, got %v", err)
  44. }
  45. })
  46. // Create a secret so we can update/patch/delete it.
  47. secret := &v1.Secret{
  48. ObjectMeta: metav1.ObjectMeta{
  49. Name: "test",
  50. },
  51. }
  52. _, err := clientSet.CoreV1().Secrets("default").Create(secret)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. t.Run("Update should limit the request body size", func(t *testing.T) {
  57. err = c.Put().AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
  58. Body(hugeData).Do().Error()
  59. if err == nil {
  60. t.Fatalf("unexpected no error")
  61. }
  62. if !errors.IsRequestEntityTooLargeError(err) {
  63. t.Errorf("expected requested entity too large err, got %v", err)
  64. }
  65. })
  66. t.Run("Patch should limit the request body size", func(t *testing.T) {
  67. err = c.Patch(types.JSONPatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
  68. Body(hugeData).Do().Error()
  69. if err == nil {
  70. t.Fatalf("unexpected no error")
  71. }
  72. if !errors.IsRequestEntityTooLargeError(err) {
  73. t.Errorf("expected requested entity too large err, got %v", err)
  74. }
  75. })
  76. t.Run("Delete should limit the request body size", func(t *testing.T) {
  77. err = c.Delete().AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
  78. Body(hugeData).Do().Error()
  79. if err == nil {
  80. t.Fatalf("unexpected no error")
  81. }
  82. if !errors.IsRequestEntityTooLargeError(err) {
  83. t.Errorf("expected requested entity too large err, got %v", err)
  84. }
  85. })
  86. }