1
16
17 package v1
18
19 import (
20 "math"
21 "testing"
22
23 "github.com/stretchr/testify/assert"
24 "k8s.io/apimachinery/pkg/api/resource"
25 "k8s.io/apimachinery/pkg/util/validation/field"
26 "k8s.io/component-base/featuregate"
27 )
28
29 func TestValidation(t *testing.T) {
30 jsonOptionsEnabled := LoggingConfiguration{
31 Format: "text",
32 Options: FormatOptions{
33 JSON: JSONOptions{
34 OutputRoutingOptions: OutputRoutingOptions{
35 SplitStream: true,
36 InfoBufferSize: resource.QuantityValue{
37 Quantity: *resource.NewQuantity(1024, resource.DecimalSI),
38 },
39 },
40 },
41 },
42 }
43 jsonErrors := `[options.json.splitStream: Forbidden: Feature LoggingAlphaOptions is disabled, options.json.infoBufferSize: Forbidden: Feature LoggingAlphaOptions is disabled]`
44 testcases := map[string]struct {
45 config LoggingConfiguration
46 path *field.Path
47 featureGate featuregate.FeatureGate
48 expectErrors string
49 }{
50 "okay": {
51 config: LoggingConfiguration{
52 Format: "text",
53 Verbosity: 10,
54 VModule: VModuleConfiguration{
55 {
56 FilePattern: "gopher*",
57 Verbosity: 100,
58 },
59 },
60 },
61 },
62 "wrong-format": {
63 config: LoggingConfiguration{
64 Format: "no-such-format",
65 },
66 expectErrors: `format: Invalid value: "no-such-format": Unsupported log format`,
67 },
68 "embedded": {
69 config: LoggingConfiguration{
70 Format: "no-such-format",
71 },
72 path: field.NewPath("config"),
73 expectErrors: `config.format: Invalid value: "no-such-format": Unsupported log format`,
74 },
75 "verbosity-overflow": {
76 config: LoggingConfiguration{
77 Format: "text",
78 Verbosity: math.MaxInt32 + 1,
79 },
80 expectErrors: `verbosity: Invalid value: 0x80000000: Must be <= 2147483647`,
81 },
82 "vmodule-verbosity-overflow": {
83 config: LoggingConfiguration{
84 Format: "text",
85 VModule: VModuleConfiguration{
86 {
87 FilePattern: "gopher*",
88 Verbosity: math.MaxInt32 + 1,
89 },
90 },
91 },
92 expectErrors: `vmodule[0]: Invalid value: 0x80000000: Must be <= 2147483647`,
93 },
94 "vmodule-empty-pattern": {
95 config: LoggingConfiguration{
96 Format: "text",
97 VModule: VModuleConfiguration{
98 {
99 FilePattern: "",
100 Verbosity: 1,
101 },
102 },
103 },
104 expectErrors: `vmodule[0]: Required value: File pattern must not be empty`,
105 },
106 "vmodule-pattern-with-special-characters": {
107 config: LoggingConfiguration{
108 Format: "text",
109 VModule: VModuleConfiguration{
110 {
111 FilePattern: "foo,bar",
112 Verbosity: 1,
113 },
114 {
115 FilePattern: "foo=bar",
116 Verbosity: 1,
117 },
118 },
119 },
120 expectErrors: `[vmodule[0]: Invalid value: "foo,bar": File pattern must not contain equal sign or comma, vmodule[1]: Invalid value: "foo=bar": File pattern must not contain equal sign or comma]`,
121 },
122 "vmodule-unsupported": {
123 config: LoggingConfiguration{
124 Format: "json",
125 VModule: VModuleConfiguration{
126 {
127 FilePattern: "foo",
128 Verbosity: 1,
129 },
130 },
131 },
132 expectErrors: `[format: Invalid value: "json": Unsupported log format, vmodule: Forbidden: Only supported for text log format]`,
133 },
134 "JSON used, default gates": {
135 config: jsonOptionsEnabled,
136 featureGate: defaultFeatureGate,
137 expectErrors: jsonErrors,
138 },
139 "JSON used, disabled gates": {
140 config: jsonOptionsEnabled,
141 featureGate: disabledFeatureGate,
142 expectErrors: jsonErrors,
143 },
144 "JSON used, enabled gates": {
145 config: jsonOptionsEnabled,
146 featureGate: enabledFeatureGate,
147 },
148 }
149
150 for name, test := range testcases {
151 t.Run(name, func(t *testing.T) {
152 featureGate := test.featureGate
153 if featureGate == nil {
154 featureGate = defaultFeatureGate
155 }
156 err := Validate(&test.config, featureGate, test.path)
157 if len(err) == 0 {
158 if test.expectErrors != "" {
159 t.Fatalf("did not get expected error(s): %s", test.expectErrors)
160 }
161 } else {
162 assert.Equal(t, test.expectErrors, err.ToAggregate().Error())
163 }
164 })
165 }
166 }
167
View as plain text