storage_flowcontrol_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 rest
  14. import (
  15. "context"
  16. "github.com/stretchr/testify/require"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. flowcontrolv1alpha1 "k8s.io/api/flowcontrol/v1alpha1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apiserver/pkg/apis/flowcontrol/bootstrap"
  22. "k8s.io/client-go/kubernetes/fake"
  23. flowcontrolapisv1alpha1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1alpha1"
  24. )
  25. func TestShouldEnsurePredefinedSettings(t *testing.T) {
  26. testCases := []struct {
  27. name string
  28. existingPriorityLevel *flowcontrolv1alpha1.PriorityLevelConfiguration
  29. expected bool
  30. }{
  31. {
  32. name: "should ensure if exempt priority-level is absent",
  33. existingPriorityLevel: nil,
  34. expected: true,
  35. },
  36. {
  37. name: "should not ensure if exempt priority-level is present",
  38. existingPriorityLevel: bootstrap.MandatoryPriorityLevelConfigurationExempt,
  39. expected: false,
  40. },
  41. }
  42. for _, testCase := range testCases {
  43. t.Run(testCase.name, func(t *testing.T) {
  44. c := fake.NewSimpleClientset()
  45. if testCase.existingPriorityLevel != nil {
  46. c.FlowcontrolV1alpha1().PriorityLevelConfigurations().Create(context.TODO(), testCase.existingPriorityLevel, metav1.CreateOptions{})
  47. }
  48. should, err := lastMandatoryExists(c.FlowcontrolV1alpha1())
  49. assert.NoError(t, err)
  50. assert.Equal(t, testCase.expected, should)
  51. })
  52. }
  53. }
  54. func TestFlowSchemaHasWrongSpec(t *testing.T) {
  55. fs1 := &flowcontrolv1alpha1.FlowSchema{
  56. Spec: flowcontrolv1alpha1.FlowSchemaSpec{},
  57. }
  58. fs2 := &flowcontrolv1alpha1.FlowSchema{
  59. Spec: flowcontrolv1alpha1.FlowSchemaSpec{
  60. MatchingPrecedence: 1,
  61. },
  62. }
  63. fs1Defaulted := &flowcontrolv1alpha1.FlowSchema{
  64. Spec: flowcontrolv1alpha1.FlowSchemaSpec{
  65. MatchingPrecedence: flowcontrolapisv1alpha1.FlowSchemaDefaultMatchingPrecedence,
  66. },
  67. }
  68. testCases := []struct {
  69. name string
  70. expected *flowcontrolv1alpha1.FlowSchema
  71. actual *flowcontrolv1alpha1.FlowSchema
  72. hasWrongSpec bool
  73. }{
  74. {
  75. name: "identical flow-schemas should work",
  76. expected: bootstrap.MandatoryFlowSchemaCatchAll,
  77. actual: bootstrap.MandatoryFlowSchemaCatchAll,
  78. hasWrongSpec: false,
  79. },
  80. {
  81. name: "defaulted flow-schemas should work",
  82. expected: fs1,
  83. actual: fs1Defaulted,
  84. hasWrongSpec: false,
  85. },
  86. {
  87. name: "non-defaulted flow-schema has wrong spec",
  88. expected: fs1,
  89. actual: fs2,
  90. hasWrongSpec: true,
  91. },
  92. }
  93. for _, testCase := range testCases {
  94. t.Run(testCase.name, func(t *testing.T) {
  95. w, err := flowSchemaHasWrongSpec(testCase.expected, testCase.actual)
  96. require.NoError(t, err)
  97. assert.Equal(t, testCase.hasWrongSpec, w)
  98. })
  99. }
  100. }
  101. func TestPriorityLevelHasWrongSpec(t *testing.T) {
  102. pl1 := &flowcontrolv1alpha1.PriorityLevelConfiguration{
  103. Spec: flowcontrolv1alpha1.PriorityLevelConfigurationSpec{
  104. Type: flowcontrolv1alpha1.PriorityLevelEnablementLimited,
  105. Limited: &flowcontrolv1alpha1.LimitedPriorityLevelConfiguration{
  106. LimitResponse: flowcontrolv1alpha1.LimitResponse{
  107. Type: flowcontrolv1alpha1.LimitResponseTypeReject,
  108. },
  109. },
  110. },
  111. }
  112. pl2 := &flowcontrolv1alpha1.PriorityLevelConfiguration{
  113. Spec: flowcontrolv1alpha1.PriorityLevelConfigurationSpec{
  114. Type: flowcontrolv1alpha1.PriorityLevelEnablementLimited,
  115. Limited: &flowcontrolv1alpha1.LimitedPriorityLevelConfiguration{
  116. AssuredConcurrencyShares: 1,
  117. },
  118. },
  119. }
  120. pl1Defaulted := &flowcontrolv1alpha1.PriorityLevelConfiguration{
  121. Spec: flowcontrolv1alpha1.PriorityLevelConfigurationSpec{
  122. Type: flowcontrolv1alpha1.PriorityLevelEnablementLimited,
  123. Limited: &flowcontrolv1alpha1.LimitedPriorityLevelConfiguration{
  124. AssuredConcurrencyShares: flowcontrolapisv1alpha1.PriorityLevelConfigurationDefaultAssuredConcurrencyShares,
  125. LimitResponse: flowcontrolv1alpha1.LimitResponse{
  126. Type: flowcontrolv1alpha1.LimitResponseTypeReject,
  127. },
  128. },
  129. },
  130. }
  131. testCases := []struct {
  132. name string
  133. expected *flowcontrolv1alpha1.PriorityLevelConfiguration
  134. actual *flowcontrolv1alpha1.PriorityLevelConfiguration
  135. hasWrongSpec bool
  136. }{
  137. {
  138. name: "identical priority-level should work",
  139. expected: bootstrap.MandatoryPriorityLevelConfigurationCatchAll,
  140. actual: bootstrap.MandatoryPriorityLevelConfigurationCatchAll,
  141. hasWrongSpec: false,
  142. },
  143. {
  144. name: "defaulted priority-level should work",
  145. expected: pl1,
  146. actual: pl1Defaulted,
  147. hasWrongSpec: false,
  148. },
  149. {
  150. name: "non-defaulted priority-level has wrong spec",
  151. expected: pl1,
  152. actual: pl2,
  153. hasWrongSpec: true,
  154. },
  155. }
  156. for _, testCase := range testCases {
  157. t.Run(testCase.name, func(t *testing.T) {
  158. w, err := priorityLevelHasWrongSpec(testCase.expected, testCase.actual)
  159. require.NoError(t, err)
  160. assert.Equal(t, testCase.hasWrongSpec, w)
  161. })
  162. }
  163. }