k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/validate/result_test.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package validate 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 // Test AddError() uniqueness 25 func TestResult_AddError(t *testing.T) { 26 r := Result{} 27 r.AddErrors(fmt.Errorf("one error")) 28 r.AddErrors(fmt.Errorf("another error")) 29 r.AddErrors(fmt.Errorf("one error")) 30 r.AddErrors(fmt.Errorf("one error")) 31 r.AddErrors(fmt.Errorf("one error")) 32 r.AddErrors(fmt.Errorf("one error"), fmt.Errorf("another error")) 33 34 assert.Len(t, r.Errors, 2) 35 assert.Contains(t, r.Errors, fmt.Errorf("one error")) 36 assert.Contains(t, r.Errors, fmt.Errorf("another error")) 37 } 38 39 func TestResult_AddNilError(t *testing.T) { 40 r := Result{} 41 r.AddErrors(nil) 42 assert.Len(t, r.Errors, 0) 43 44 errArray := []error{fmt.Errorf("one Error"), nil, fmt.Errorf("another error")} 45 r.AddErrors(errArray...) 46 assert.Len(t, r.Errors, 2) 47 } 48 49 func TestResult_AddWarnings(t *testing.T) { 50 r := Result{} 51 r.AddErrors(fmt.Errorf("one Error")) 52 assert.Len(t, r.Errors, 1) 53 assert.Len(t, r.Warnings, 0) 54 55 r.AddWarnings(fmt.Errorf("one Warning")) 56 assert.Len(t, r.Errors, 1) 57 assert.Len(t, r.Warnings, 1) 58 } 59 60 func TestResult_Merge(t *testing.T) { 61 r := Result{} 62 r.AddErrors(fmt.Errorf("one Error")) 63 r.AddWarnings(fmt.Errorf("one Warning")) 64 r.Inc() 65 assert.Len(t, r.Errors, 1) 66 assert.Len(t, r.Warnings, 1) 67 assert.Equal(t, r.MatchCount, 1) 68 69 // Merge with same 70 r2 := Result{} 71 r2.AddErrors(fmt.Errorf("one Error")) 72 r2.AddWarnings(fmt.Errorf("one Warning")) 73 r2.Inc() 74 75 r.Merge(&r2) 76 77 assert.Len(t, r.Errors, 1) 78 assert.Len(t, r.Warnings, 1) 79 assert.Equal(t, r.MatchCount, 2) 80 81 // Merge with new 82 r3 := Result{} 83 r3.AddErrors(fmt.Errorf("new Error")) 84 r3.AddWarnings(fmt.Errorf("new Warning")) 85 r3.Inc() 86 87 r.Merge(&r3) 88 89 assert.Len(t, r.Errors, 2) 90 assert.Len(t, r.Warnings, 2) 91 assert.Equal(t, r.MatchCount, 3) 92 } 93 94 func errorFixture() (Result, Result, Result) { 95 r := Result{} 96 r.AddErrors(fmt.Errorf("one Error")) 97 r.AddWarnings(fmt.Errorf("one Warning")) 98 r.Inc() 99 100 // same 101 r2 := Result{} 102 r2.AddErrors(fmt.Errorf("one Error")) 103 r2.AddWarnings(fmt.Errorf("one Warning")) 104 r2.Inc() 105 106 // new 107 r3 := Result{} 108 r3.AddErrors(fmt.Errorf("new Error")) 109 r3.AddWarnings(fmt.Errorf("new Warning")) 110 r3.Inc() 111 return r, r2, r3 112 } 113 114 func TestResult_MergeAsErrors(t *testing.T) { 115 r, r2, r3 := errorFixture() 116 assert.Len(t, r.Errors, 1) 117 assert.Len(t, r.Warnings, 1) 118 assert.Equal(t, r.MatchCount, 1) 119 120 r.MergeAsErrors(&r2, &r3) 121 122 assert.Len(t, r.Errors, 4) // One Warning added to Errors 123 assert.Len(t, r.Warnings, 1) 124 assert.Equal(t, r.MatchCount, 3) 125 } 126 127 func TestResult_MergeAsWarnings(t *testing.T) { 128 r, r2, r3 := errorFixture() 129 assert.Len(t, r.Errors, 1) 130 assert.Len(t, r.Warnings, 1) 131 assert.Equal(t, r.MatchCount, 1) 132 133 r.MergeAsWarnings(&r2, &r3) 134 135 assert.Len(t, r.Errors, 1) // One Warning added to Errors 136 assert.Len(t, r.Warnings, 4) 137 assert.Equal(t, r.MatchCount, 3) 138 } 139 140 func TestResult_IsValid(t *testing.T) { 141 r := Result{} 142 143 assert.True(t, r.IsValid()) 144 assert.False(t, r.HasErrors()) 145 146 r.AddWarnings(fmt.Errorf("one Warning")) 147 assert.True(t, r.IsValid()) 148 assert.False(t, r.HasErrors()) 149 150 r.AddErrors(fmt.Errorf("one Error")) 151 assert.False(t, r.IsValid()) 152 assert.True(t, r.HasErrors()) 153 } 154 155 func TestResult_HasWarnings(t *testing.T) { 156 r := Result{} 157 158 assert.False(t, r.HasWarnings()) 159 160 r.AddErrors(fmt.Errorf("one Error")) 161 assert.False(t, r.HasWarnings()) 162 163 r.AddWarnings(fmt.Errorf("one Warning")) 164 assert.True(t, r.HasWarnings()) 165 } 166 167 func TestResult_HasErrorsOrWarnings(t *testing.T) { 168 r := Result{} 169 r2 := Result{} 170 171 assert.False(t, r.HasErrorsOrWarnings()) 172 173 r.AddErrors(fmt.Errorf("one Error")) 174 assert.True(t, r.HasErrorsOrWarnings()) 175 176 r2.AddWarnings(fmt.Errorf("one Warning")) 177 assert.True(t, r2.HasErrorsOrWarnings()) 178 179 r.Merge(&r2) 180 assert.True(t, r.HasErrorsOrWarnings()) 181 } 182 183 func TestResult_keepRelevantErrors(t *testing.T) { 184 r := Result{} 185 r.AddErrors(fmt.Errorf("one Error")) 186 r.AddErrors(fmt.Errorf("IMPORTANT!Another Error")) 187 r.AddWarnings(fmt.Errorf("one warning")) 188 r.AddWarnings(fmt.Errorf("IMPORTANT!Another warning")) 189 assert.Len(t, r.keepRelevantErrors().Errors, 1) 190 assert.Len(t, r.keepRelevantErrors().Warnings, 1) 191 } 192 193 func TestResult_AsError(t *testing.T) { 194 r := Result{} 195 assert.Nil(t, r.AsError()) 196 r.AddErrors(fmt.Errorf("one Error")) 197 r.AddErrors(fmt.Errorf("additional Error")) 198 res := r.AsError() 199 if assert.NotNil(t, res) { 200 assert.Contains(t, res.Error(), "validation failure list:") // Expected from pkg errors 201 assert.Contains(t, res.Error(), "one Error") // Expected from pkg errors 202 assert.Contains(t, res.Error(), "additional Error") // Expected from pkg errors 203 } 204 } 205 206 // Test methods which suppport a call on a nil instance 207 func TestResult_NilInstance(t *testing.T) { 208 var r *Result 209 assert.True(t, r.IsValid()) 210 assert.False(t, r.HasErrors()) 211 assert.False(t, r.HasWarnings()) 212 assert.False(t, r.HasErrorsOrWarnings()) 213 }