...
1
16
17 package v1beta2
18
19 import (
20 "reflect"
21 "testing"
22
23 "github.com/google/go-cmp/cmp"
24
25 flowcontrolv1beta2 "k8s.io/api/flowcontrol/v1beta2"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/utils/pointer"
28 )
29
30 func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
31 tests := []struct {
32 name string
33 original runtime.Object
34 expected runtime.Object
35 }{
36 {
37 name: "Defaulting for Exempt",
38 original: &flowcontrolv1beta2.PriorityLevelConfiguration{
39 Spec: flowcontrolv1beta2.PriorityLevelConfigurationSpec{
40 Type: flowcontrolv1beta2.PriorityLevelEnablementExempt,
41 Exempt: &flowcontrolv1beta2.ExemptPriorityLevelConfiguration{},
42 },
43 },
44 expected: &flowcontrolv1beta2.PriorityLevelConfiguration{
45 Spec: flowcontrolv1beta2.PriorityLevelConfigurationSpec{
46 Type: flowcontrolv1beta2.PriorityLevelEnablementExempt,
47 Exempt: &flowcontrolv1beta2.ExemptPriorityLevelConfiguration{
48 NominalConcurrencyShares: pointer.Int32(0),
49 LendablePercent: pointer.Int32(0),
50 },
51 },
52 },
53 },
54 {
55 name: "LendablePercent is not specified, should default to zero",
56 original: &flowcontrolv1beta2.PriorityLevelConfiguration{
57 Spec: flowcontrolv1beta2.PriorityLevelConfigurationSpec{
58 Type: flowcontrolv1beta2.PriorityLevelEnablementLimited,
59 Limited: &flowcontrolv1beta2.LimitedPriorityLevelConfiguration{
60 AssuredConcurrencyShares: 5,
61 LimitResponse: flowcontrolv1beta2.LimitResponse{
62 Type: flowcontrolv1beta2.LimitResponseTypeReject,
63 },
64 },
65 },
66 },
67 expected: &flowcontrolv1beta2.PriorityLevelConfiguration{
68 Spec: flowcontrolv1beta2.PriorityLevelConfigurationSpec{
69 Type: flowcontrolv1beta2.PriorityLevelEnablementLimited,
70 Limited: &flowcontrolv1beta2.LimitedPriorityLevelConfiguration{
71 AssuredConcurrencyShares: 5,
72 LendablePercent: pointer.Int32(0),
73 LimitResponse: flowcontrolv1beta2.LimitResponse{
74 Type: flowcontrolv1beta2.LimitResponseTypeReject,
75 },
76 },
77 },
78 },
79 },
80 }
81
82 scheme := runtime.NewScheme()
83 if err := AddToScheme(scheme); err != nil {
84 t.Fatalf("Failed to add to scheme: %v", err)
85 }
86
87 for _, test := range tests {
88 t.Run(test.name, func(t *testing.T) {
89 original := test.original
90 expected := test.expected
91
92 scheme.Default(original)
93 if !reflect.DeepEqual(expected, original) {
94 t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original))
95 }
96 })
97 }
98 }
99
View as plain text