|
| 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 conflictingmarkers_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/conflictingmarkers" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/initializer" |
| 26 | +) |
| 27 | + |
| 28 | +var _ = Describe("conflictingmarkers initializer", func() { |
| 29 | + Context("config validation", func() { |
| 30 | + type testCase struct { |
| 31 | + config conflictingmarkers.ConflictingMarkersConfig |
| 32 | + expectedErr string |
| 33 | + } |
| 34 | + |
| 35 | + DescribeTable("should validate the provided config", func(in testCase) { |
| 36 | + ci, ok := conflictingmarkers.Initializer().(initializer.ConfigurableAnalyzerInitializer) |
| 37 | + Expect(ok).To(BeTrue()) |
| 38 | + |
| 39 | + errs := ci.ValidateConfig(&in.config, field.NewPath("conflictingmarkers")) |
| 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 config with single conflict", testCase{ |
| 47 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 48 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 49 | + { |
| 50 | + Name: "test_conflict", |
| 51 | + Sets: [][]string{{"marker1"}, {"marker2"}}, |
| 52 | + Description: "Test conflict", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + expectedErr: "", |
| 57 | + }), |
| 58 | + Entry("With a valid config with multiple conflicts", testCase{ |
| 59 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 60 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 61 | + { |
| 62 | + Name: "test_conflict", |
| 63 | + Sets: [][]string{{"marker1"}, {"marker2"}}, |
| 64 | + Description: "Test conflict", |
| 65 | + }, |
| 66 | + { |
| 67 | + Name: "another_conflict", |
| 68 | + Sets: [][]string{{"marker3", "marker4"}, {"marker5"}}, |
| 69 | + Description: "Another test conflict", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + expectedErr: "", |
| 74 | + }), |
| 75 | + Entry("With empty conflicts list", testCase{ |
| 76 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 77 | + Conflicts: []conflictingmarkers.ConflictSet{}, |
| 78 | + }, |
| 79 | + expectedErr: "conflictingmarkers.conflicts: Required value: at least one conflict set is required", |
| 80 | + }), |
| 81 | + Entry("With missing name", testCase{ |
| 82 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 83 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 84 | + { |
| 85 | + Name: "", |
| 86 | + Sets: [][]string{{"marker1"}, {"marker2"}}, |
| 87 | + Description: "Test conflict", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + expectedErr: "conflictingmarkers.conflicts[0].name: Required value: name is required", |
| 92 | + }), |
| 93 | + Entry("With missing description", testCase{ |
| 94 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 95 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 96 | + { |
| 97 | + Name: "test_conflict", |
| 98 | + Sets: [][]string{{"marker1"}, {"marker2"}}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + expectedErr: "conflictingmarkers.conflicts[0].description: Required value: description is required", |
| 103 | + }), |
| 104 | + Entry("With insufficient sets (only 1 set)", testCase{ |
| 105 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 106 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 107 | + { |
| 108 | + Name: "test_conflict", |
| 109 | + Sets: [][]string{{"marker1"}}, |
| 110 | + Description: "Test conflict", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + expectedErr: "conflictingmarkers.conflicts[0].sets: Required value: at least 2 sets are required", |
| 115 | + }), |
| 116 | + Entry("With empty set", testCase{ |
| 117 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 118 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 119 | + { |
| 120 | + Name: "test_conflict", |
| 121 | + Sets: [][]string{{"marker1"}, {}}, |
| 122 | + Description: "Test conflict", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + expectedErr: "conflictingmarkers.conflicts[0].sets[1]: Required value: set cannot be empty", |
| 127 | + }), |
| 128 | + Entry("With overlapping markers between sets", testCase{ |
| 129 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 130 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 131 | + { |
| 132 | + Name: "test_conflict", |
| 133 | + Sets: [][]string{{"marker1", "marker2"}, {"marker2", "marker3"}}, |
| 134 | + Description: "Test conflict", |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + expectedErr: "conflictingmarkers.conflicts[0].sets: Invalid value: conflictingmarkers.ConflictSet{Name:\"test_conflict\", Sets:[][]string{[]string{\"marker1\", \"marker2\"}, []string{\"marker2\", \"marker3\"}}, Description:\"Test conflict\"}: sets 1 and 2 cannot contain overlapping markers: [marker2]", |
| 139 | + }), |
| 140 | + Entry("With duplicate conflict names", testCase{ |
| 141 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 142 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 143 | + { |
| 144 | + Name: "duplicate_name", |
| 145 | + Sets: [][]string{{"marker1"}, {"marker2"}}, |
| 146 | + Description: "First conflict", |
| 147 | + }, |
| 148 | + { |
| 149 | + Name: "duplicate_name", |
| 150 | + Sets: [][]string{{"marker3"}, {"marker4"}}, |
| 151 | + Description: "Second conflict", |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + expectedErr: "conflictingmarkers.conflicts[1].name: Duplicate value: \"duplicate_name\"", |
| 156 | + }), |
| 157 | + Entry("With three-way conflict", testCase{ |
| 158 | + config: conflictingmarkers.ConflictingMarkersConfig{ |
| 159 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 160 | + { |
| 161 | + Name: "three_way_conflict", |
| 162 | + Sets: [][]string{{"marker1"}, {"marker2"}, {"marker3"}}, |
| 163 | + Description: "Three-way conflict", |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + expectedErr: "", |
| 168 | + }), |
| 169 | + ) |
| 170 | + }) |
| 171 | +}) |
0 commit comments