config_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright 2019 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 resourcequota
  14. import (
  15. "bytes"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. corev1 "k8s.io/api/core/v1"
  20. resourcequotaapi "k8s.io/kubernetes/plugin/pkg/admission/resourcequota/apis/resourcequota"
  21. )
  22. func TestLoadConfiguration(t *testing.T) {
  23. testcases := []struct {
  24. name string
  25. input string
  26. expectErr string
  27. expectConfig *resourcequotaapi.Configuration
  28. }{
  29. {
  30. name: "empty",
  31. input: ``,
  32. expectErr: `'Kind' is missing`,
  33. },
  34. {
  35. name: "unknown type",
  36. input: `{"kind":"Unknown","apiVersion":"v1"}`,
  37. expectErr: `no kind "Unknown" is registered`,
  38. },
  39. {
  40. name: "valid v1alpha1 config",
  41. input: `
  42. kind: Configuration
  43. apiVersion: resourcequota.admission.k8s.io/v1alpha1
  44. limitedResources:
  45. - apiGroup: ""
  46. resource: persistentvolumeclaims
  47. matchContains:
  48. - .storageclass.storage.k8s.io/requests.storage
  49. - apiGroup: ""
  50. resource: pods
  51. matchScopes:
  52. - scopeName: PriorityClass
  53. operator: In
  54. values:
  55. - cluster-services
  56. `,
  57. expectConfig: &resourcequotaapi.Configuration{
  58. LimitedResources: []resourcequotaapi.LimitedResource{
  59. {APIGroup: "", Resource: "persistentvolumeclaims", MatchContains: []string{".storageclass.storage.k8s.io/requests.storage"}},
  60. {APIGroup: "", Resource: "pods", MatchScopes: []corev1.ScopedResourceSelectorRequirement{
  61. {ScopeName: "PriorityClass", Operator: "In", Values: []string{"cluster-services"}},
  62. },
  63. },
  64. }},
  65. },
  66. {
  67. name: "valid v1beta1 config",
  68. input: `
  69. kind: Configuration
  70. apiVersion: resourcequota.admission.k8s.io/v1beta1
  71. limitedResources:
  72. - apiGroup: ""
  73. resource: persistentvolumeclaims
  74. matchContains:
  75. - .storageclass.storage.k8s.io/requests.storage
  76. - apiGroup: ""
  77. resource: pods
  78. matchScopes:
  79. - scopeName: PriorityClass
  80. operator: In
  81. values:
  82. - cluster-services
  83. `,
  84. expectConfig: &resourcequotaapi.Configuration{
  85. LimitedResources: []resourcequotaapi.LimitedResource{
  86. {APIGroup: "", Resource: "persistentvolumeclaims", MatchContains: []string{".storageclass.storage.k8s.io/requests.storage"}},
  87. {APIGroup: "", Resource: "pods", MatchScopes: []corev1.ScopedResourceSelectorRequirement{
  88. {ScopeName: "PriorityClass", Operator: "In", Values: []string{"cluster-services"}},
  89. },
  90. },
  91. }},
  92. },
  93. {
  94. name: "valid v1 config",
  95. input: `
  96. kind: ResourceQuotaConfiguration
  97. apiVersion: apiserver.config.k8s.io/v1
  98. limitedResources:
  99. - apiGroup: ""
  100. resource: persistentvolumeclaims
  101. matchContains:
  102. - .storageclass.storage.k8s.io/requests.storage
  103. - apiGroup: ""
  104. resource: pods
  105. matchScopes:
  106. - scopeName: PriorityClass
  107. operator: In
  108. values:
  109. - cluster-services
  110. `,
  111. expectConfig: &resourcequotaapi.Configuration{
  112. LimitedResources: []resourcequotaapi.LimitedResource{
  113. {APIGroup: "", Resource: "persistentvolumeclaims", MatchContains: []string{".storageclass.storage.k8s.io/requests.storage"}},
  114. {APIGroup: "", Resource: "pods", MatchScopes: []corev1.ScopedResourceSelectorRequirement{
  115. {ScopeName: "PriorityClass", Operator: "In", Values: []string{"cluster-services"}},
  116. },
  117. },
  118. }},
  119. },
  120. }
  121. for _, tc := range testcases {
  122. t.Run(tc.name, func(t *testing.T) {
  123. config, err := LoadConfiguration(bytes.NewBuffer([]byte(tc.input)))
  124. if len(tc.expectErr) > 0 {
  125. if err == nil {
  126. t.Fatal("expected err, got none")
  127. }
  128. if !strings.Contains(err.Error(), tc.expectErr) {
  129. t.Fatalf("expected err containing %q, got %v", tc.expectErr, err)
  130. }
  131. return
  132. }
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if !reflect.DeepEqual(config, tc.expectConfig) {
  137. t.Fatalf("expected\n%#v\ngot\n%#v", tc.expectConfig, config)
  138. }
  139. })
  140. }
  141. }