util_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "fmt"
  16. "reflect"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/diff"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. featuregatetesting "k8s.io/component-base/featuregate/testing"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. "k8s.io/kubernetes/pkg/features"
  23. )
  24. func TestDropDisabledFields(t *testing.T) {
  25. rqWithScopeSelector := func() *api.ResourceQuota {
  26. return &api.ResourceQuota{Spec: api.ResourceQuotaSpec{Scopes: []api.ResourceQuotaScope{"scope-1"}, ScopeSelector: &api.ScopeSelector{
  27. MatchExpressions: []api.ScopedResourceSelectorRequirement{
  28. {
  29. ScopeName: api.ResourceQuotaScopePriorityClass,
  30. Operator: api.ScopeSelectorOpIn,
  31. Values: []string{"scope-1"},
  32. },
  33. },
  34. }}}
  35. }
  36. rqWithoutScopeSelector := func() *api.ResourceQuota {
  37. return &api.ResourceQuota{Spec: api.ResourceQuotaSpec{Scopes: []api.ResourceQuotaScope{"scope-1"}, ScopeSelector: nil}}
  38. }
  39. rqInfo := []struct {
  40. description string
  41. hasScopeSelector bool
  42. resourceQuota func() *api.ResourceQuota
  43. }{
  44. {
  45. description: "ResourceQuota without Scopes Selector",
  46. hasScopeSelector: false,
  47. resourceQuota: rqWithoutScopeSelector,
  48. },
  49. {
  50. description: "ResourceQuota with Scope Selector",
  51. hasScopeSelector: true,
  52. resourceQuota: rqWithScopeSelector,
  53. },
  54. {
  55. description: "is nil",
  56. hasScopeSelector: false,
  57. resourceQuota: func() *api.ResourceQuota { return nil },
  58. },
  59. }
  60. for _, enabled := range []bool{true, false} {
  61. for _, oldRQInfo := range rqInfo {
  62. for _, newRQInfo := range rqInfo {
  63. oldRQHasSelector, oldrq := oldRQInfo.hasScopeSelector, oldRQInfo.resourceQuota()
  64. newRQHasSelector, newrq := newRQInfo.hasScopeSelector, newRQInfo.resourceQuota()
  65. if newrq == nil {
  66. continue
  67. }
  68. t.Run(fmt.Sprintf("feature enabled=%v, old ResourceQuota %v, new ResourceQuota %v", enabled, oldRQInfo.description, newRQInfo.description), func(t *testing.T) {
  69. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ResourceQuotaScopeSelectors, enabled)()
  70. var oldRQSpec *api.ResourceQuotaSpec
  71. if oldrq != nil {
  72. oldRQSpec = &oldrq.Spec
  73. }
  74. DropDisabledFields(&newrq.Spec, oldRQSpec)
  75. // old ResourceQuota should never be changed
  76. if !reflect.DeepEqual(oldrq, oldRQInfo.resourceQuota()) {
  77. t.Errorf("old ResourceQuota changed: %v", diff.ObjectReflectDiff(oldrq, oldRQInfo.resourceQuota()))
  78. }
  79. switch {
  80. case enabled || oldRQHasSelector:
  81. // new ResourceQuota should not be changed if the feature is enabled, or if the old ResourceQuota had ScopeSelector
  82. if !reflect.DeepEqual(newrq, newRQInfo.resourceQuota()) {
  83. t.Errorf("new ResourceQuota changed: %v", diff.ObjectReflectDiff(newrq, newRQInfo.resourceQuota()))
  84. }
  85. case newRQHasSelector:
  86. // new ResourceQuota should be changed
  87. if reflect.DeepEqual(newrq, newRQInfo.resourceQuota()) {
  88. t.Errorf("new ResourceQuota was not changed")
  89. }
  90. // new ResourceQuota should not have ScopeSelector
  91. if !reflect.DeepEqual(newrq, rqWithoutScopeSelector()) {
  92. t.Errorf("new ResourceQuota had ScopeSelector: %v", diff.ObjectReflectDiff(newrq, rqWithoutScopeSelector()))
  93. }
  94. default:
  95. // new ResourceQuota should not need to be changed
  96. if !reflect.DeepEqual(newrq, newRQInfo.resourceQuota()) {
  97. t.Errorf("new ResourceQuota changed: %v", diff.ObjectReflectDiff(newrq, newRQInfo.resourceQuota()))
  98. }
  99. }
  100. })
  101. }
  102. }
  103. }
  104. }