Skip to content

Commit 4a81895

Browse files
committed
Move validation tests into analyzer packages
1 parent 75caa08 commit 4a81895

17 files changed

+927
-423
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package conditions_test
17+
18+
import (
19+
"testing"
20+
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
func TestConditions(t *testing.T) {
26+
RegisterFailHandler(Fail)
27+
RunSpecs(t, "Conditions")
28+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package conditions_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
"sigs.k8s.io/kube-api-linter/pkg/analysis/conditions"
25+
"sigs.k8s.io/kube-api-linter/pkg/analysis/initializer"
26+
)
27+
28+
var _ = Describe("conditions initializer", func() {
29+
Context("config validation", func() {
30+
type testCase struct {
31+
config conditions.ConditionsConfig
32+
expectedErr string
33+
}
34+
35+
DescribeTable("should validate the provided config", func(in testCase) {
36+
ci, ok := conditions.Initializer().(initializer.ConfigurableAnalyzerInitializer)
37+
Expect(ok).To(BeTrue())
38+
39+
errs := ci.ValidateConfig(&in.config, field.NewPath("conditions"))
40+
if len(in.expectedErr) > 0 {
41+
Expect(errs.ToAggregate()).To(MatchError(in.expectedErr))
42+
} else {
43+
Expect(errs).To(HaveLen(0), "No errors were expected")
44+
}
45+
},
46+
Entry("With a valid ConditionsConfig", testCase{
47+
config: conditions.ConditionsConfig{
48+
IsFirstField: "",
49+
UseProtobuf: "",
50+
},
51+
expectedErr: "",
52+
}),
53+
Entry("With a valid ConditionsConfig IsFirstField: Warn", testCase{
54+
config: conditions.ConditionsConfig{
55+
IsFirstField: conditions.ConditionsFirstFieldWarn,
56+
},
57+
expectedErr: "",
58+
}),
59+
Entry("With a valid ConditionsConfig IsFirstField: Ignore", testCase{
60+
config: conditions.ConditionsConfig{
61+
IsFirstField: conditions.ConditionsFirstFieldIgnore,
62+
},
63+
expectedErr: "",
64+
}),
65+
Entry("With an invalid ConditionsConfig IsFirstField", testCase{
66+
config: conditions.ConditionsConfig{
67+
IsFirstField: "invalid",
68+
},
69+
expectedErr: "conditions.isFirstField: Invalid value: \"invalid\": invalid value, must be one of \"Warn\", \"Ignore\" or omitted",
70+
}),
71+
Entry("With a valid ConditionsConfig UseProtobuf: SuggestFix", testCase{
72+
config: conditions.ConditionsConfig{
73+
UseProtobuf: conditions.ConditionsUseProtobufSuggestFix,
74+
},
75+
expectedErr: "",
76+
}),
77+
Entry("With a valid ConditionsConfig UseProtobuf: Warn", testCase{
78+
config: conditions.ConditionsConfig{
79+
UseProtobuf: conditions.ConditionsUseProtobufWarn,
80+
},
81+
expectedErr: "",
82+
}),
83+
Entry("With a valid ConditionsConfig UseProtobuf: Ignore", testCase{
84+
config: conditions.ConditionsConfig{
85+
UseProtobuf: conditions.ConditionsUseProtobufIgnore,
86+
},
87+
expectedErr: "",
88+
}),
89+
Entry("With a valid ConditionsConfig UseProtobuf: Forbid", testCase{
90+
config: conditions.ConditionsConfig{
91+
UseProtobuf: conditions.ConditionsUseProtobufForbid,
92+
},
93+
expectedErr: "",
94+
}),
95+
Entry("With an invalid ConditionsConfig UseProtobuf", testCase{
96+
config: conditions.ConditionsConfig{
97+
UseProtobuf: "invalid",
98+
},
99+
expectedErr: "conditions.useProtobuf: Invalid value: \"invalid\": invalid value, must be one of \"SuggestFix\", \"Warn\", \"Ignore\", \"Forbid\" or omitted",
100+
}),
101+
Entry("With a valid ConditionsConfig UsePatchStrategy: SuggestFix", testCase{
102+
config: conditions.ConditionsConfig{
103+
UsePatchStrategy: conditions.ConditionsUsePatchStrategySuggestFix,
104+
},
105+
expectedErr: "",
106+
}),
107+
Entry("With a valid ConditionsConfig UsePatchStrategy: Warn", testCase{
108+
config: conditions.ConditionsConfig{
109+
UsePatchStrategy: conditions.ConditionsUsePatchStrategyWarn,
110+
},
111+
expectedErr: "",
112+
}),
113+
Entry("With a valid ConditionsConfig UsePatchStrategy: Ignore", testCase{
114+
config: conditions.ConditionsConfig{
115+
UsePatchStrategy: conditions.ConditionsUsePatchStrategyIgnore,
116+
},
117+
expectedErr: "",
118+
}),
119+
Entry("With a valid ConditionsConfig UsePatchStrategy: Forbid", testCase{
120+
config: conditions.ConditionsConfig{
121+
UsePatchStrategy: conditions.ConditionsUsePatchStrategyForbid,
122+
},
123+
expectedErr: "",
124+
}),
125+
Entry("With an invalid ConditionsConfig UsePatchStrategy", testCase{
126+
config: conditions.ConditionsConfig{
127+
UsePatchStrategy: "invalid",
128+
},
129+
expectedErr: "conditions.usePatchStrategy: Invalid value: \"invalid\": invalid value, must be one of \"SuggestFix\", \"Warn\", \"Ignore\", \"Forbid\" or omitted",
130+
}),
131+
)
132+
})
133+
})
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package jsontags_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
"sigs.k8s.io/kube-api-linter/pkg/analysis/initializer"
25+
"sigs.k8s.io/kube-api-linter/pkg/analysis/jsontags"
26+
)
27+
28+
var _ = Describe("jsontags initializer", func() {
29+
Context("config validation", func() {
30+
type testCase struct {
31+
config jsontags.JSONTagsConfig
32+
expectedErr string
33+
}
34+
35+
DescribeTable("should validate the provided config", func(in testCase) {
36+
ci, ok := jsontags.Initializer().(initializer.ConfigurableAnalyzerInitializer)
37+
Expect(ok).To(BeTrue())
38+
39+
errs := ci.ValidateConfig(&in.config, field.NewPath("jsontags"))
40+
if len(in.expectedErr) > 0 {
41+
Expect(errs.ToAggregate()).To(MatchError(in.expectedErr))
42+
} else {
43+
Expect(errs).To(HaveLen(0), "No errors were expected")
44+
}
45+
},
46+
47+
Entry("With a valid JSONTagsConfig JSONTagRegex", testCase{
48+
config: jsontags.JSONTagsConfig{
49+
JSONTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$",
50+
},
51+
expectedErr: "",
52+
}),
53+
Entry("With an invalid JSONTagsConfig JSONTagRegex", testCase{
54+
config: jsontags.JSONTagsConfig{
55+
JSONTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*",
56+
},
57+
expectedErr: "jsontags.jsonTagRegex: Invalid value: \"^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*\": invalid regex: error parsing regexp: missing closing ): `^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*`",
58+
}),
59+
)
60+
})
61+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package jsontags_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestJsonTags(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "jsontags")
29+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package nomaps_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
"sigs.k8s.io/kube-api-linter/pkg/analysis/initializer"
25+
"sigs.k8s.io/kube-api-linter/pkg/analysis/nomaps"
26+
)
27+
28+
var _ = Describe("nomaps initializer", func() {
29+
Context("config validation", func() {
30+
type testCase struct {
31+
config nomaps.NoMapsConfig
32+
expectedErr string
33+
}
34+
35+
DescribeTable("should validate the provided config", func(in testCase) {
36+
ci, ok := nomaps.Initializer().(initializer.ConfigurableAnalyzerInitializer)
37+
Expect(ok).To(BeTrue())
38+
39+
errs := ci.ValidateConfig(&in.config, field.NewPath("nomaps"))
40+
if len(in.expectedErr) > 0 {
41+
Expect(errs.ToAggregate()).To(MatchError(in.expectedErr))
42+
} else {
43+
Expect(errs).To(HaveLen(0), "No errors were expected")
44+
}
45+
},
46+
Entry("With a valid NoMapsConfig", testCase{
47+
config: nomaps.NoMapsConfig{
48+
Policy: "",
49+
},
50+
expectedErr: "",
51+
}),
52+
Entry("With a valid NoMapsConfig: enforce is specified", testCase{
53+
config: nomaps.NoMapsConfig{
54+
Policy: nomaps.NoMapsEnforce,
55+
},
56+
expectedErr: "",
57+
}),
58+
Entry("With a valid NoMapsConfig: allowStringToStringMaps is specified", testCase{
59+
config: nomaps.NoMapsConfig{
60+
Policy: nomaps.NoMapsAllowStringToStringMaps,
61+
},
62+
expectedErr: "",
63+
}),
64+
Entry("With a valid NoMapsConfig: ignore is specified", testCase{
65+
config: nomaps.NoMapsConfig{
66+
Policy: nomaps.NoMapsIgnore,
67+
},
68+
expectedErr: "",
69+
}),
70+
Entry("With a invalid NoMapsConfig", testCase{
71+
config: nomaps.NoMapsConfig{
72+
Policy: "invalid",
73+
},
74+
expectedErr: `nomaps.policy: Invalid value: "invalid": invalid value, must be one of "Enforce", "AllowStringToStringMaps", "Ignore" or omitted`,
75+
}),
76+
)
77+
})
78+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package nomaps_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestNomaps(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "nomaps")
29+
}

0 commit comments

Comments
 (0)