| 1 | /* |
| 2 | Copyright 2018 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 rules |
| 18 | |
| 19 | import ( |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | |
| 23 | "k8s.io/gengo/v2/types" |
| 24 | ) |
| 25 | |
| 26 | func TestNamesMatch(t *testing.T) { |
| 27 | someStruct := &types.Type{ |
| 28 | Name: types.Name{Name: "SomeStruct"}, |
| 29 | Kind: types.Struct, |
| 30 | } |
| 31 | someStructPtr := &types.Type{ |
| 32 | Name: types.Name{Name: "SomeStructPtr"}, |
| 33 | Kind: types.Pointer, |
| 34 | Elem: someStruct, |
| 35 | } |
| 36 | intPtr := &types.Type{ |
| 37 | Name: types.Name{Name: "IntPtr"}, |
| 38 | Kind: types.Pointer, |
| 39 | Elem: types.Int, |
| 40 | } |
| 41 | listMeta := &types.Type{ |
| 42 | Name: types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ListMeta"}, |
| 43 | Kind: types.Struct, |
| 44 | } |
| 45 | listMetaPtr := &types.Type{ |
| 46 | Name: types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ListMetaPtr"}, |
| 47 | Kind: types.Pointer, |
| 48 | Elem: listMeta, |
| 49 | } |
| 50 | objectMeta := &types.Type{ |
| 51 | Name: types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ObjectMeta"}, |
| 52 | Kind: types.Struct, |
| 53 | } |
| 54 | objectMetaPtr := &types.Type{ |
| 55 | Name: types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ObjectMetaPtr"}, |
| 56 | Kind: types.Pointer, |
| 57 | Elem: objectMeta, |
| 58 | } |
| 59 | |
| 60 | tcs := []struct { |
| 61 | // name of test case |
| 62 | name string |
| 63 | t *types.Type |
| 64 | |
| 65 | // expected list of violation fields |
| 66 | expected []string |
| 67 | }{ |
| 68 | // The comments are in format of {goName, jsonName, match}, |
| 69 | // {"PodSpec", "podSpec", true}, |
| 70 | { |
| 71 | name: "simple", |
| 72 | t: &types.Type{ |
| 73 | Kind: types.Struct, |
| 74 | Members: []types.Member{ |
| 75 | { |
| 76 | Name: "PodSpec", |
| 77 | Tags: `json:"podSpec"`, |
| 78 | }, |
| 79 | }, |
| 80 | }, |
| 81 | expected: []string{}, |
| 82 | }, |
| 83 | // {"PodSpec", "podSpec", true}, |
| 84 | { |
| 85 | name: "multiple_json_tags", |
| 86 | t: &types.Type{ |
| 87 | Kind: types.Struct, |
| 88 | Members: []types.Member{ |
| 89 | { |
| 90 | Name: "PodSpec", |
| 91 | Tags: `json:"podSpec,omitempty"`, |
| 92 | }, |
| 93 | }, |
| 94 | }, |
| 95 | expected: []string{}, |
| 96 | }, |
| 97 | // {"PodSpec", "podSpec", true}, |
| 98 | { |
| 99 | name: "protobuf_tag", |
| 100 | t: &types.Type{ |
| 101 | Kind: types.Struct, |
| 102 | Members: []types.Member{ |
| 103 | { |
| 104 | Name: "PodSpec", |
| 105 | Tags: `json:"podSpec,omitempty" protobuf:"bytes,1,opt,name=podSpec"`, |
| 106 | }, |
| 107 | }, |
| 108 | }, |
| 109 | expected: []string{}, |
| 110 | }, |
| 111 | // {"", "podSpec", false}, |
| 112 | { |
| 113 | name: "empty", |
| 114 | t: &types.Type{ |
| 115 | Kind: types.Struct, |
| 116 | Members: []types.Member{ |
| 117 | { |
| 118 | Name: "", |
| 119 | Tags: `json:"podSpec"`, |
| 120 | }, |
| 121 | }, |
| 122 | }, |
| 123 | expected: []string{""}, |
| 124 | }, |
| 125 | // {"PodSpec", "PodSpec", false}, |
| 126 | { |
| 127 | name: "CamelCase_CamelCase", |
| 128 | t: &types.Type{ |
| 129 | Kind: types.Struct, |
| 130 | Members: []types.Member{ |
| 131 | { |
| 132 | Name: "PodSpec", |
| 133 | Tags: `json:"PodSpec"`, |
| 134 | }, |
| 135 | }, |
| 136 | }, |
| 137 | expected: []string{"PodSpec"}, |
| 138 | }, |
| 139 | // {"podSpec", "podSpec", false}, |
| 140 | { |
| 141 | name: "camelCase_camelCase", |
| 142 | t: &types.Type{ |
| 143 | Kind: types.Struct, |
| 144 | Members: []types.Member{ |
| 145 | { |
| 146 | Name: "podSpec", |
| 147 | Tags: `json:"podSpec"`, |
| 148 | }, |
| 149 | }, |
| 150 | }, |
| 151 | expected: []string{"podSpec"}, |
| 152 | }, |
| 153 | // {"PodSpec", "spec", false}, |
| 154 | { |
| 155 | name: "short_json_name", |
| 156 | t: &types.Type{ |
| 157 | Kind: types.Struct, |
| 158 | Members: []types.Member{ |
| 159 | { |
| 160 | Name: "PodSpec", |
| 161 | Tags: `json:"spec"`, |
| 162 | }, |
| 163 | }, |
| 164 | }, |
| 165 | expected: []string{"PodSpec"}, |
| 166 | }, |
| 167 | // {"Spec", "podSpec", false}, |
| 168 | { |
| 169 | name: "long_json_name", |
| 170 | t: &types.Type{ |
| 171 | Kind: types.Struct, |
| 172 | Members: []types.Member{ |
| 173 | { |
| 174 | Name: "Spec", |
| 175 | Tags: `json:"podSpec"`, |
| 176 | }, |
| 177 | }, |
| 178 | }, |
| 179 | expected: []string{"Spec"}, |
| 180 | }, |
| 181 | // {"JSONSpec", "jsonSpec", true}, |
| 182 | { |
| 183 | name: "acronym", |
| 184 | t: &types.Type{ |
| 185 | Kind: types.Struct, |
| 186 | Members: []types.Member{ |
| 187 | { |
| 188 | Name: "JSONSpec", |
| 189 | Tags: `json:"jsonSpec"`, |
| 190 | }, |
| 191 | }, |
| 192 | }, |
| 193 | expected: []string{}, |
| 194 | }, |
| 195 | // {"JSONSpec", "jsonspec", false}, |
| 196 | { |
| 197 | name: "acronym_invalid", |
| 198 | t: &types.Type{ |
| 199 | Kind: types.Struct, |
| 200 | Members: []types.Member{ |
| 201 | { |
| 202 | Name: "JSONSpec", |
| 203 | Tags: `json:"jsonspec"`, |
| 204 | }, |
| 205 | }, |
| 206 | }, |
| 207 | expected: []string{"JSONSpec"}, |
| 208 | }, |
| 209 | // {"HTTPJSONSpec", "httpJSONSpec", true}, |
| 210 | { |
| 211 | name: "multiple_acronym", |
| 212 | t: &types.Type{ |
| 213 | Kind: types.Struct, |
| 214 | Members: []types.Member{ |
| 215 | { |
| 216 | Name: "HTTPJSONSpec", |
| 217 | Tags: `json:"httpJSONSpec"`, |
| 218 | }, |
| 219 | }, |
| 220 | }, |
| 221 | expected: []string{}, |
| 222 | }, |
| 223 | // // NOTE: this validator cannot tell two sequential all-capital words from one word, |
| 224 | // // therefore the case below is also considered matched. |
| 225 | // {"HTTPJSONSpec", "httpjsonSpec", true}, |
| 226 | { |
| 227 | name: "multiple_acronym_as_one", |
| 228 | t: &types.Type{ |
| 229 | Kind: types.Struct, |
| 230 | Members: []types.Member{ |
| 231 | { |
| 232 | Name: "HTTPJSONSpec", |
| 233 | Tags: `json:"httpjsonSpec"`, |
| 234 | }, |
| 235 | }, |
| 236 | }, |
| 237 | expected: []string{}, |
| 238 | }, |
| 239 | // NOTE: JSON tags in jsonTagBlacklist should skip evaluation |
| 240 | { |
| 241 | name: "blacklist_tag_dash", |
| 242 | t: &types.Type{ |
| 243 | Kind: types.Struct, |
| 244 | Members: []types.Member{ |
| 245 | { |
| 246 | Name: "podSpec", |
| 247 | Tags: `json:"-"`, |
| 248 | }, |
| 249 | }, |
| 250 | }, |
| 251 | expected: []string{}, |
| 252 | }, |
| 253 | // {"PodSpec", "-", false}, |
| 254 | { |
| 255 | name: "invalid_json_name_dash", |
| 256 | t: &types.Type{ |
| 257 | Kind: types.Struct, |
| 258 | Members: []types.Member{ |
| 259 | { |
| 260 | Name: "PodSpec", |
| 261 | Tags: `json:"-,"`, |
| 262 | }, |
| 263 | }, |
| 264 | }, |
| 265 | expected: []string{"PodSpec"}, |
| 266 | }, |
| 267 | // {"podSpec", "metadata", false}, |
| 268 | { |
| 269 | name: "blacklist_metadata", |
| 270 | t: &types.Type{ |
| 271 | Kind: types.Struct, |
| 272 | Members: []types.Member{ |
| 273 | { |
| 274 | Name: "podSpec", |
| 275 | Tags: `json:"metadata"`, |
| 276 | }, |
| 277 | }, |
| 278 | }, |
| 279 | expected: []string{"podSpec"}, |
| 280 | }, |
| 281 | { |
| 282 | name: "non_struct", |
| 283 | t: &types.Type{ |
| 284 | Kind: types.Map, |
| 285 | }, |
| 286 | expected: []string{}, |
| 287 | }, |
| 288 | { |
| 289 | name: "no_json_tag", |
| 290 | t: &types.Type{ |
| 291 | Kind: types.Struct, |
| 292 | Members: []types.Member{ |
| 293 | { |
| 294 | Name: "PodSpec", |
| 295 | Tags: `podSpec`, |
| 296 | }, |
| 297 | }, |
| 298 | }, |
| 299 | expected: []string{"PodSpec"}, |
| 300 | }, |
| 301 | // NOTE: this is to expand test coverage |
| 302 | // {"S", "s", true}, |
| 303 | { |
| 304 | name: "single_character", |
| 305 | t: &types.Type{ |
| 306 | Kind: types.Struct, |
| 307 | Members: []types.Member{ |
| 308 | { |
| 309 | Name: "S", |
| 310 | Tags: `json:"s"`, |
| 311 | }, |
| 312 | }, |
| 313 | }, |
| 314 | expected: []string{}, |
| 315 | }, |
| 316 | // NOTE: names with disallowed substrings should fail evaluation |
| 317 | // {"Pod-Spec", "pod-Spec", false}, |
| 318 | { |
| 319 | name: "disallowed_substring_dash", |
| 320 | t: &types.Type{ |
| 321 | Kind: types.Struct, |
| 322 | Members: []types.Member{ |
| 323 | { |
| 324 | Name: "Pod-Spec", |
| 325 | Tags: `json:"pod-Spec"`, |
| 326 | }, |
| 327 | }, |
| 328 | }, |
| 329 | expected: []string{"Pod-Spec"}, |
| 330 | }, |
| 331 | // {"Pod_Spec", "pod_Spec", false}, |
| 332 | { |
| 333 | name: "disallowed_substring_underscore", |
| 334 | t: &types.Type{ |
| 335 | Kind: types.Struct, |
| 336 | Members: []types.Member{ |
| 337 | { |
| 338 | Name: "Pod_Spec", |
| 339 | Tags: `json:"pod_Spec"`, |
| 340 | }, |
| 341 | }, |
| 342 | }, |
| 343 | expected: []string{"Pod_Spec"}, |
| 344 | }, |
| 345 | { |
| 346 | name: "empty_JSON_name", |
| 347 | t: &types.Type{ |
| 348 | Kind: types.Struct, |
| 349 | Members: []types.Member{ |
| 350 | { |
| 351 | Name: "Int", |
| 352 | Tags: `json:""`, // Not okay! |
| 353 | Type: types.Int, |
| 354 | }, |
| 355 | { |
| 356 | Name: "Struct", |
| 357 | Tags: `json:""`, // Okay, inlined. |
| 358 | Type: someStruct, |
| 359 | }, |
| 360 | { |
| 361 | Name: "IntPtr", |
| 362 | Tags: `json:""`, // Not okay! |
| 363 | Type: intPtr, |
| 364 | }, |
| 365 | { |
| 366 | Name: "StructPtr", |
| 367 | Tags: `json:""`, // Okay, inlined. |
| 368 | Type: someStructPtr, |
| 369 | }, |
| 370 | }, |
| 371 | }, |
| 372 | expected: []string{ |
| 373 | "Int", |
| 374 | "IntPtr", |
| 375 | }, |
| 376 | }, |
| 377 | { |
| 378 | name: "metadata_no_pointers", |
| 379 | t: &types.Type{ |
| 380 | Kind: types.Struct, |
| 381 | Members: []types.Member{ |
| 382 | { |
| 383 | Name: "ListMeta", |
| 384 | Tags: `json:"listMeta"`, // Not okay, should be "metadata"! |
| 385 | Type: listMeta, |
| 386 | }, |
| 387 | { |
| 388 | Name: "ObjectMeta", |
| 389 | Tags: `json:"objectMeta"`, // Not okay, should be metadata"! |
| 390 | Type: objectMeta, |
| 391 | }, |
| 392 | { |
| 393 | Name: "SomeStruct", |
| 394 | Tags: `json:"metadata"`, // Not okay, name mismatch! |
| 395 | Type: someStruct, |
| 396 | }, |
| 397 | }, |
| 398 | }, |
| 399 | expected: []string{ |
| 400 | "ListMeta", |
| 401 | "ObjectMeta", |
| 402 | "SomeStruct", |
| 403 | }, |
| 404 | }, |
| 405 | { |
| 406 | name: "metadata_pointers", |
| 407 | t: &types.Type{ |
| 408 | Kind: types.Struct, |
| 409 | Members: []types.Member{ |
| 410 | { |
| 411 | Name: "ListMeta", |
| 412 | Tags: `json:"listMeta"`, // Okay, convention only applies to struct. |
| 413 | Type: listMetaPtr, |
| 414 | }, |
| 415 | { |
| 416 | Name: "ObjectMeta", |
| 417 | Tags: `json:"objectMeta"`, // Okay, convention only applies to struct. |
| 418 | Type: objectMetaPtr, |
| 419 | }, |
| 420 | { |
| 421 | Name: "SomeStruct", |
| 422 | Tags: `json:"metadata"`, // Not okay, name mismatch! |
| 423 | Type: someStructPtr, |
| 424 | }, |
| 425 | }, |
| 426 | }, |
| 427 | expected: []string{ |
| 428 | "SomeStruct", |
| 429 | }, |
| 430 | }, |
| 431 | } |
| 432 | |
| 433 | n := &NamesMatch{} |
| 434 | for _, tc := range tcs { |
| 435 | if violations, _ := n.Validate(tc.t); !reflect.DeepEqual(violations, tc.expected) { |
| 436 | t.Errorf("unexpected validation result: test name %v, want: %v, got: %v", |
| 437 | tc.name, tc.expected, violations) |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // TestRuleName tests the Name of API rule. This is to expand test coverage |
| 443 | func TestRuleName(t *testing.T) { |
| 444 | ruleName := "names_match" |
| 445 | n := &NamesMatch{} |
| 446 | if n.Name() != ruleName { |
| 447 | t.Errorf("unexpected API rule name: want: %v, got: %v", ruleName, n.Name()) |
| 448 | } |
| 449 | } |