Skip to content

Commit a472e92

Browse files
committed
test: add unit tests for conflicting markers analyzer and custom configurations
1 parent 82580f0 commit a472e92

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
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+
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 TestEmptyConfiguration(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+
// With empty configuration, no conflicts should be detected
36+
analysistest.Run(t, testdata, analyzer, "a")
37+
}
38+
39+
func TestWithConfiguration(t *testing.T) {
40+
testdata := analysistest.TestData()
41+
42+
config := &conflictingmarkers.ConflictingMarkersConfig{
43+
Conflicts: []conflictingmarkers.ConflictSet{
44+
{
45+
Name: "test_conflict",
46+
SetA: []string{"marker1", "marker2"},
47+
SetB: []string{"marker3", "marker4"},
48+
Description: "Test markers conflict with each other",
49+
},
50+
},
51+
}
52+
53+
initializer := conflictingmarkers.Initializer()
54+
55+
analyzer, err := initializer.Init(config)
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
analysistest.Run(t, testdata, analyzer, "b")
61+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
// Valid field with mixed markers that don't conflict (since no conflicts are configured)
13+
// +optional
14+
// +required
15+
// +default
16+
ValidMixedMarkersField string `json:"validMixedMarkersField"`
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
// Valid field with mixed markers that don't conflict (since no conflicts are configured)
13+
// +optional
14+
// +required
15+
// +default
16+
ValidMixedMarkersField string `json:"validMixedMarkersField"`
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package b
2+
3+
type TestStruct struct {
4+
// Valid field with only marker1
5+
// +marker1
6+
ValidMarker1Field string `json:"validMarker1Field"`
7+
8+
// Valid field with marker1 that has a value
9+
// +marker1:=someValue
10+
ValidMarker1WithValueField string `json:"validMarker1WithValueField"`
11+
12+
// Conflict: marker1 vs marker3 (both with values)
13+
// +marker1:=value1
14+
// +marker3:=value2
15+
ConflictWithValuesField string `json:"conflictWithValuesField"` // want "field ConflictWithValuesField has conflicting markers: test_conflict: \\[marker1\\] and \\[marker3\\]. Test markers conflict with each other"
16+
17+
// Conflict: marker1 vs marker3 (mixed with and without values)
18+
// +marker1
19+
// +marker3:=someValue
20+
ConflictMixedField string `json:"conflictMixedField"` // want "field ConflictMixedField has conflicting markers: test_conflict: \\[marker1\\] and \\[marker3\\]. Test markers conflict with each other"
21+
22+
// Multiple conflicts with multiple markers in each set:
23+
// - set A: +marker1, +marker2
24+
// - set B: +marker3, +marker4
25+
// +marker1
26+
// +marker2
27+
// +marker3
28+
// +marker4
29+
MultipleConflictsField string `json:"multipleConflictsField"` // want "field MultipleConflictsField has conflicting markers: test_conflict: \\[marker1 marker2\\] and \\[marker3 marker4\\]. Test markers conflict with each other"
30+
31+
// Valid field with other markers that don't conflict (since no conflicts are configured for them)
32+
// +optional
33+
// +required
34+
// +default
35+
ValidOtherMarkersField string `json:"validOtherMarkersField"`
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package b
2+
3+
type TestStruct struct {
4+
// Valid field with only marker1
5+
// +marker1
6+
ValidMarker1Field string `json:"validMarker1Field"`
7+
8+
// Valid field with marker1 that has a value
9+
// +marker1:=someValue
10+
ValidMarker1WithValueField string `json:"validMarker1WithValueField"`
11+
12+
// Conflict: marker1 vs marker3 (both with values)
13+
// +marker1:=value1
14+
// +marker3:=value2
15+
ConflictWithValuesField string `json:"conflictWithValuesField"` // want "field ConflictWithValuesField has conflicting markers: test_conflict: \\[marker1\\] and \\[marker3\\]. Test markers conflict with each other"
16+
17+
// Conflict: marker1 vs marker3 (mixed with and without values)
18+
// +marker1
19+
// +marker3:=someValue
20+
ConflictMixedField string `json:"conflictMixedField"` // want "field ConflictMixedField has conflicting markers: test_conflict: \\[marker1\\] and \\[marker3\\]. Test markers conflict with each other"
21+
22+
// Multiple conflicts with multiple markers in each set:
23+
// - set A: +marker1, +marker2
24+
// - set B: +marker3, +marker4
25+
// +marker1
26+
// +marker2
27+
// +marker3
28+
// +marker4
29+
MultipleConflictsField string `json:"multipleConflictsField"` // want "field MultipleConflictsField has conflicting markers: test_conflict: \\[marker1 marker2\\] and \\[marker3 marker4\\]. Test markers conflict with each other"
30+
31+
// Valid field with other markers that don't conflict (since no conflicts are configured for them)
32+
// +optional
33+
// +required
34+
// +default
35+
ValidOtherMarkersField string `json:"validOtherMarkersField"`
36+
}

0 commit comments

Comments
 (0)