Skip to content

Commit c37dacb

Browse files
committed
test: add unit tests for conflicting markers analyzer and custom configurations
1 parent c5fea18 commit c37dacb

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 conflictingmarkers_test
17+
18+
import (
19+
"testing"
20+
21+
"golang.org/x/tools/go/analysis/analysistest"
22+
"sigs.k8s.io/kube-api-linter/pkg/analysis/conflictingmarkers"
23+
)
24+
25+
func TestDefaultConfiguration(t *testing.T) {
26+
testdata := analysistest.TestData()
27+
28+
initializer := conflictingmarkers.Initializer()
29+
30+
analyzer, err := initializer.Init(&conflictingmarkers.ConflictingMarkersConfig{})
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
35+
analysistest.Run(t, testdata, analyzer, "a")
36+
}
37+
38+
func TestCustomConfiguration(t *testing.T) {
39+
testdata := analysistest.TestData()
40+
41+
config := &conflictingmarkers.ConflictingMarkersConfig{
42+
CustomConflicts: []conflictingmarkers.ConflictSet{
43+
{
44+
Name: "custom_conflict",
45+
SetA: []string{"custom:marker1", "custom:marker2"},
46+
SetB: []string{"custom:marker3", "custom:marker4"},
47+
Description: "Custom markers conflict with each other",
48+
},
49+
},
50+
}
51+
52+
initializer := conflictingmarkers.Initializer()
53+
54+
analyzer, err := initializer.Init(config)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
analysistest.Run(t, testdata, analyzer, "b")
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package a
2+
3+
type TestStruct struct {
4+
// Valid field with only optional marker
5+
// +optional
6+
ValidOptionalField string `json:"validOptionalField,omitempty"`
7+
8+
// Valid field with only required marker
9+
// +required
10+
ValidRequiredField string `json:"validRequiredField"`
11+
12+
// Conflict: optional vs required
13+
// +optional
14+
// +required
15+
OptionalVsRequiredField string `json:"optionalVsRequiredField"` // want "field OptionalVsRequiredField has conflicting markers: \\[optional\\] and \\[required\\]. A field cannot be both optional and required"
16+
17+
// Conflict: default vs required
18+
// +default
19+
// +required
20+
DefaultVsRequiredField string `json:"defaultVsRequiredField"` // want "field DefaultVsRequiredField has conflicting markers: \\[default\\] and \\[required\\]. A field with a default value cannot be required"
21+
22+
// Multiple conflicts with multiple markers in each set:
23+
// - optional set: +optional, +kubebuilder:validation:Optional, +k8s:optional
24+
// - required set: +required, +kubebuilder:validation:Required, +k8s:required
25+
// - default set: +default, +kubebuilder:default
26+
// +optional
27+
// +kubebuilder:validation:Optional
28+
// +k8s:optional
29+
// +default
30+
// +kubebuilder:default
31+
// +required
32+
// +kubebuilder:validation:Required
33+
// +k8s:required
34+
MultipleConflictsMultipleMarkersField string `json:"multipleConflictsMultipleMarkersField"` // want "field MultipleConflictsMultipleMarkersField has conflicting markers: \\[k8s:optional kubebuilder:validation:Optional optional\\] and \\[k8s:required kubebuilder:validation:Required required\\]. A field cannot be both optional and required" "field MultipleConflictsMultipleMarkersField has conflicting markers: \\[default kubebuilder:default\\] and \\[k8s:required kubebuilder:validation:Required required\\]. A field with a default value cannot be required"
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package a
2+
3+
type TestStruct struct {
4+
// Valid field with only optional marker
5+
// +optional
6+
ValidOptionalField string `json:"validOptionalField,omitempty"`
7+
8+
// Valid field with only required marker
9+
// +required
10+
ValidRequiredField string `json:"validRequiredField"`
11+
12+
// Conflict: optional vs required
13+
// +optional
14+
// +required
15+
OptionalVsRequiredField string `json:"optionalVsRequiredField"` // want "field OptionalVsRequiredField has conflicting markers: \\[optional\\] and \\[required\\]. A field cannot be both optional and required"
16+
17+
// Conflict: default vs required
18+
// +default
19+
// +required
20+
DefaultVsRequiredField string `json:"defaultVsRequiredField"` // want "field DefaultVsRequiredField has conflicting markers: \\[default\\] and \\[required\\]. A field with a default value cannot be required"
21+
22+
// Multiple conflicts with multiple markers in each set:
23+
// - optional set: +optional, +kubebuilder:validation:Optional, +k8s:optional
24+
// - required set: +required, +kubebuilder:validation:Required, +k8s:required
25+
// - default set: +default, +kubebuilder:default
26+
// +optional
27+
// +kubebuilder:validation:Optional
28+
// +k8s:optional
29+
// +default
30+
// +kubebuilder:default
31+
// +required
32+
// +kubebuilder:validation:Required
33+
// +k8s:required
34+
MultipleConflictsMultipleMarkersField string `json:"multipleConflictsMultipleMarkersField"` // want "field MultipleConflictsMultipleMarkersField has conflicting markers: \\[k8s:optional kubebuilder:validation:Optional optional\\] and \\[k8s:required kubebuilder:validation:Required required\\]. A field cannot be both optional and required" "field MultipleConflictsMultipleMarkersField has conflicting markers: \\[default kubebuilder:default\\] and \\[k8s:required kubebuilder:validation:Required required\\]. A field with a default value cannot be required"
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package b
2+
3+
type CustomConflictStruct struct {
4+
// Valid field with only custom marker1
5+
// +custom:marker1
6+
ValidCustomMarker1Field string `json:"validCustomMarker1Field"`
7+
8+
// Valid field with custom marker1 that has a value
9+
// +custom:marker1:=someValue
10+
ValidCustomMarker1WithValueField string `json:"validCustomMarker1WithValueField"`
11+
12+
// Conflict: custom marker1 vs custom marker3 (both with values)
13+
// +custom:marker1:=value1
14+
// +custom:marker3:=value2
15+
CustomConflictWithValuesField string `json:"customConflictWithValuesField"` // want "field CustomConflictWithValuesField has conflicting markers: \\[custom:marker1\\] and \\[custom:marker3\\]. Custom markers conflict with each other"
16+
17+
// Conflict: custom marker1 vs custom marker3 (mixed with and without values)
18+
// +custom:marker1
19+
// +custom:marker3:=someValue
20+
CustomConflictMixedField string `json:"customConflictMixedField"` // want "field CustomConflictMixedField has conflicting markers: \\[custom:marker1\\] and \\[custom:marker3\\]. Custom markers conflict with each other"
21+
22+
// Multiple conflicts with multiple markers in each set:
23+
// - set A: +custom:marker1, +custom:marker2
24+
// - set B: +custom:marker3, +custom:marker4
25+
// +custom:marker1
26+
// +custom:marker2
27+
// +custom:marker3
28+
// +custom:marker4
29+
MultipleCustomConflictsField string `json:"multipleCustomConflictsField"` // want "field MultipleCustomConflictsField has conflicting markers: \\[custom:marker1 custom:marker2\\] and \\[custom:marker3 custom:marker4\\]. Custom markers conflict with each other"
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package b
2+
3+
type CustomConflictStruct struct {
4+
// Valid field with only custom marker1
5+
// +custom:marker1
6+
ValidCustomMarker1Field string `json:"validCustomMarker1Field"`
7+
8+
// Valid field with custom marker1 that has a value
9+
// +custom:marker1:=someValue
10+
ValidCustomMarker1WithValueField string `json:"validCustomMarker1WithValueField"`
11+
12+
// Conflict: custom marker1 vs custom marker3 (both with values)
13+
// +custom:marker1:=value1
14+
// +custom:marker3:=value2
15+
CustomConflictWithValuesField string `json:"customConflictWithValuesField"` // want "field CustomConflictWithValuesField has conflicting markers: \\[custom:marker1\\] and \\[custom:marker3\\]. Custom markers conflict with each other"
16+
17+
// Conflict: custom marker1 vs custom marker3 (mixed with and without values)
18+
// +custom:marker1
19+
// +custom:marker3:=someValue
20+
CustomConflictMixedField string `json:"customConflictMixedField"` // want "field CustomConflictMixedField has conflicting markers: \\[custom:marker1\\] and \\[custom:marker3\\]. Custom markers conflict with each other"
21+
22+
// Multiple conflicts with multiple markers in each set:
23+
// - set A: +custom:marker1, +custom:marker2
24+
// - set B: +custom:marker3, +custom:marker4
25+
// +custom:marker1
26+
// +custom:marker2
27+
// +custom:marker3
28+
// +custom:marker4
29+
MultipleCustomConflictsField string `json:"multipleCustomConflictsField"` // want "field MultipleCustomConflictsField has conflicting markers: \\[custom:marker1 custom:marker2\\] and \\[custom:marker3 custom:marker4\\]. Custom markers conflict with each other"
30+
}

0 commit comments

Comments
 (0)