istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/bug-report/pkg/cluster/cluster_test.go (about) 1 // Copyright Istio Authors 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 cluster 16 17 import ( 18 "testing" 19 20 v1 "k8s.io/api/core/v1" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 23 config2 "istio.io/istio/tools/bug-report/pkg/config" 24 ) 25 26 func TestShouldSkipPod(t *testing.T) { 27 cases := []struct { 28 name string 29 pod *v1.Pod 30 config *config2.BugReportConfig 31 expected bool 32 }{ 33 { 34 "tested namespace not skip", 35 &v1.Pod{ 36 ObjectMeta: metav1.ObjectMeta{ 37 Namespace: "in-namespace1", 38 }, 39 }, 40 &config2.BugReportConfig{ 41 Include: []*config2.SelectionSpec{ 42 { 43 Namespaces: []string{"in-"}, 44 }, 45 }, 46 Exclude: []*config2.SelectionSpec{ 47 { 48 Namespaces: []string{"ex-"}, 49 }, 50 }, 51 }, 52 false, 53 }, 54 { 55 "tested namespace not skip 2", 56 &v1.Pod{ 57 ObjectMeta: metav1.ObjectMeta{ 58 Namespace: "in-namespace1", 59 }, 60 }, 61 &config2.BugReportConfig{ 62 Include: []*config2.SelectionSpec{ 63 { 64 Namespaces: []string{"in*"}, 65 }, 66 }, 67 Exclude: []*config2.SelectionSpec{ 68 { 69 Namespaces: []string{"ex*"}, 70 }, 71 }, 72 }, 73 false, 74 }, 75 { 76 "tested namespace not skip 3", 77 &v1.Pod{ 78 ObjectMeta: metav1.ObjectMeta{ 79 Namespace: "in-namespace1", 80 }, 81 }, 82 &config2.BugReportConfig{ 83 Include: []*config2.SelectionSpec{ 84 { 85 Namespaces: []string{"*name*"}, 86 }, 87 }, 88 Exclude: []*config2.SelectionSpec{ 89 { 90 Namespaces: []string{"ex*"}, 91 }, 92 }, 93 }, 94 false, 95 }, 96 { 97 "tested namespace not skip 4", 98 &v1.Pod{ 99 ObjectMeta: metav1.ObjectMeta{ 100 Namespace: "in-namespace1", 101 }, 102 }, 103 &config2.BugReportConfig{ 104 Include: []*config2.SelectionSpec{ 105 { 106 Namespaces: []string{"*space1"}, 107 }, 108 }, 109 Exclude: []*config2.SelectionSpec{ 110 { 111 Namespaces: []string{"ex*"}, 112 }, 113 }, 114 }, 115 false, 116 }, 117 { 118 "tested namespace skip", 119 &v1.Pod{ 120 ObjectMeta: metav1.ObjectMeta{ 121 Namespace: "ex-namespace1", 122 }, 123 }, 124 &config2.BugReportConfig{ 125 Include: []*config2.SelectionSpec{ 126 { 127 Namespaces: []string{"in-"}, 128 }, 129 }, 130 Exclude: []*config2.SelectionSpec{ 131 { 132 Namespaces: []string{"ex-"}, 133 }, 134 }, 135 }, 136 true, 137 }, 138 { 139 "tested pod name not skip", 140 &v1.Pod{ 141 ObjectMeta: metav1.ObjectMeta{ 142 Name: "in-pod1", 143 }, 144 }, 145 &config2.BugReportConfig{ 146 Include: []*config2.SelectionSpec{ 147 { 148 Pods: []string{"in-"}, 149 }, 150 }, 151 Exclude: []*config2.SelectionSpec{ 152 { 153 Pods: []string{"ex-"}, 154 }, 155 }, 156 }, 157 false, 158 }, 159 { 160 "tested pod name skip", 161 &v1.Pod{ 162 ObjectMeta: metav1.ObjectMeta{ 163 Name: "ex-pod1", 164 }, 165 }, 166 &config2.BugReportConfig{ 167 Include: []*config2.SelectionSpec{ 168 { 169 Pods: []string{"in-"}, 170 }, 171 }, 172 Exclude: []*config2.SelectionSpec{ 173 { 174 Pods: []string{"ex-"}, 175 }, 176 }, 177 }, 178 true, 179 }, 180 { 181 "tested container not skip", 182 &v1.Pod{ 183 ObjectMeta: metav1.ObjectMeta{ 184 Name: "in-test1", 185 }, 186 Spec: v1.PodSpec{ 187 Containers: []v1.Container{ 188 { 189 Name: "in-con1", 190 }, 191 }, 192 }, 193 }, 194 &config2.BugReportConfig{ 195 Include: []*config2.SelectionSpec{ 196 { 197 Pods: []string{"in-"}, 198 Containers: []string{"in-"}, 199 }, 200 }, 201 Exclude: []*config2.SelectionSpec{ 202 { 203 Pods: []string{"ex-"}, 204 Containers: []string{"ex-"}, 205 }, 206 }, 207 }, 208 false, 209 }, 210 { 211 "tested container skip", 212 &v1.Pod{ 213 ObjectMeta: metav1.ObjectMeta{ 214 Name: "in-test1", 215 }, 216 Spec: v1.PodSpec{ 217 Containers: []v1.Container{ 218 { 219 Name: "ex-con1", 220 }, 221 }, 222 }, 223 }, 224 &config2.BugReportConfig{ 225 Include: []*config2.SelectionSpec{ 226 { 227 Pods: []string{"in-"}, 228 Containers: []string{"in-"}, 229 }, 230 }, 231 Exclude: []*config2.SelectionSpec{ 232 { 233 Pods: []string{"ex-"}, 234 Containers: []string{"ex-"}, 235 }, 236 }, 237 }, 238 true, 239 }, 240 { 241 "tested label not skip", 242 &v1.Pod{ 243 ObjectMeta: metav1.ObjectMeta{ 244 Name: "in-test1", 245 Labels: map[string]string{ 246 "l1": "lv1", 247 }, 248 Annotations: map[string]string{ 249 "a1": "av1", 250 "a2": "av1", 251 }, 252 }, 253 }, 254 &config2.BugReportConfig{ 255 Include: []*config2.SelectionSpec{ 256 { 257 Pods: []string{"in-"}, 258 Labels: map[string]string{ 259 "l1": "lv1", 260 "l2": "lv2", 261 }, 262 }, 263 }, 264 }, 265 false, 266 }, 267 { 268 "tested label skip", 269 &v1.Pod{ 270 ObjectMeta: metav1.ObjectMeta{ 271 Name: "in-test1", 272 Labels: map[string]string{ 273 "l1": "lv1", 274 }, 275 Annotations: map[string]string{ 276 "a1": "av1", 277 "a2": "av1", 278 }, 279 }, 280 }, 281 &config2.BugReportConfig{ 282 Include: []*config2.SelectionSpec{ 283 { 284 Pods: []string{"in-"}, 285 Labels: map[string]string{ 286 "l2": "lv2", 287 }, 288 }, 289 }, 290 }, 291 true, 292 }, 293 { 294 "tested annotation not skip", 295 &v1.Pod{ 296 ObjectMeta: metav1.ObjectMeta{ 297 Name: "in-test1", 298 Labels: map[string]string{ 299 "l3": "lv3", 300 "l4": "lv4", 301 }, 302 Annotations: map[string]string{ 303 "a3": "av3", 304 "a4": "av4", 305 }, 306 }, 307 }, 308 &config2.BugReportConfig{ 309 Include: []*config2.SelectionSpec{ 310 { 311 Pods: []string{"in-"}, 312 Annotations: map[string]string{ 313 "a3": "av3", 314 "a4": "av4", 315 }, 316 }, 317 }, 318 }, 319 false, 320 }, 321 { 322 "tested annotation skip", 323 &v1.Pod{ 324 ObjectMeta: metav1.ObjectMeta{ 325 Name: "in-test1", 326 Labels: map[string]string{ 327 "l3": "lv3", 328 "l4": "lv4", 329 }, 330 Annotations: map[string]string{ 331 "a3": "av3", 332 }, 333 }, 334 }, 335 &config2.BugReportConfig{ 336 Include: []*config2.SelectionSpec{ 337 { 338 Pods: []string{"in-"}, 339 Annotations: map[string]string{ 340 "a4": "av4", 341 }, 342 }, 343 }, 344 }, 345 true, 346 }, 347 { 348 "tested difference namespace skip exclude", 349 &v1.Pod{ 350 ObjectMeta: metav1.ObjectMeta{ 351 Name: "in-test1", 352 Namespace: "test", 353 Labels: map[string]string{ 354 "l3": "lv3", 355 "l4": "lv4", 356 }, 357 Annotations: map[string]string{ 358 "a3": "av3", 359 }, 360 }, 361 }, 362 &config2.BugReportConfig{ 363 Exclude: []*config2.SelectionSpec{ 364 { 365 Namespaces: []string{ 366 "fake", 367 }, 368 }, 369 { 370 Namespaces: []string{ 371 "test", 372 }, 373 }, 374 }, 375 }, 376 true, 377 }, 378 { 379 "tested include difference namespace not skip", 380 &v1.Pod{ 381 ObjectMeta: metav1.ObjectMeta{ 382 Name: "in-test1", 383 Namespace: "test", 384 Labels: map[string]string{ 385 "l3": "lv3", 386 "l4": "lv4", 387 }, 388 Annotations: map[string]string{ 389 "a3": "av3", 390 }, 391 }, 392 }, 393 &config2.BugReportConfig{ 394 Include: []*config2.SelectionSpec{ 395 { 396 Namespaces: []string{ 397 "fake", 398 }, 399 }, 400 { 401 Namespaces: []string{ 402 "test", 403 }, 404 }, 405 }, 406 }, 407 false, 408 }, 409 { 410 "tested include difference namespace not skip", 411 &v1.Pod{ 412 ObjectMeta: metav1.ObjectMeta{ 413 Name: "in-test1", 414 Namespace: "test", 415 Labels: map[string]string{ 416 "l3": "lv3", 417 "l4": "lv4", 418 }, 419 Annotations: map[string]string{ 420 "a3": "av3", 421 }, 422 }, 423 }, 424 &config2.BugReportConfig{ 425 Include: []*config2.SelectionSpec{ 426 { 427 Namespaces: []string{ 428 "fake", 429 }, 430 }, 431 { 432 Namespaces: []string{ 433 "test", 434 }, 435 }, 436 }, 437 }, 438 false, 439 }, 440 { 441 "tested include difference namespace/pod... not skip", 442 &v1.Pod{ 443 ObjectMeta: metav1.ObjectMeta{ 444 Name: "in-test1", 445 Namespace: "test", 446 Labels: map[string]string{ 447 "l3": "lv3", 448 "l4": "lv4", 449 }, 450 Annotations: map[string]string{ 451 "a3": "av3", 452 }, 453 }, 454 }, 455 &config2.BugReportConfig{ 456 Include: []*config2.SelectionSpec{ 457 { 458 Namespaces: []string{ 459 "fake", 460 }, 461 Pods: []string{ 462 "in-test1", 463 }, 464 Labels: map[string]string{ 465 "l3": "lv3", 466 }, 467 Annotations: map[string]string{ 468 "a3": "av3", 469 }, 470 }, 471 { 472 Namespaces: []string{ 473 "test", 474 }, 475 Pods: []string{ 476 "in-test1", 477 }, 478 Labels: map[string]string{ 479 "l3": "lv3", 480 }, 481 Annotations: map[string]string{ 482 "a3": "av3", 483 }, 484 }, 485 }, 486 }, 487 false, 488 }, 489 } 490 491 for _, c := range cases { 492 t.Run(c.name, func(t *testing.T) { 493 skip := shouldSkipPod(c.pod, c.config) 494 if skip != c.expected { 495 t.Errorf("shouldSkipPod() for test case name [%s] return= %v, want %v", c.name, skip, c.expected) 496 } 497 }) 498 } 499 } 500 501 func TestShouldSkipDeployment(t *testing.T) { 502 cases := []struct { 503 name string 504 config *config2.BugReportConfig 505 deployment string 506 expected bool 507 }{ 508 { 509 "tested deployment not skip", 510 &config2.BugReportConfig{ 511 Include: []*config2.SelectionSpec{ 512 { 513 Deployments: []string{"in-"}, 514 }, 515 }, 516 Exclude: []*config2.SelectionSpec{ 517 { 518 Deployments: []string{"ex-"}, 519 }, 520 }, 521 }, 522 "in-dep1", 523 false, 524 }, 525 { 526 "tested deployment skip", 527 &config2.BugReportConfig{ 528 Include: []*config2.SelectionSpec{ 529 { 530 Deployments: []string{"in-"}, 531 }, 532 }, 533 Exclude: []*config2.SelectionSpec{ 534 { 535 Deployments: []string{"ex-"}, 536 }, 537 }, 538 }, 539 "ex-dep1", 540 true, 541 }, 542 } 543 for _, c := range cases { 544 t.Run(c.name, func(t *testing.T) { 545 skip := shouldSkipDeployment(c.deployment, c.config) 546 if skip != c.expected { 547 t.Errorf("shouldSkip() for test case name [%s] return= %v, want %v", c.name, skip, c.expected) 548 } 549 }) 550 } 551 } 552 553 func TestShouldSkipDaemonSet(t *testing.T) { 554 cases := []struct { 555 name string 556 config *config2.BugReportConfig 557 daemonset string 558 expected bool 559 }{ 560 { 561 "tested daemonset not skip", 562 &config2.BugReportConfig{ 563 Include: []*config2.SelectionSpec{ 564 { 565 Daemonsets: []string{"in-"}, 566 }, 567 }, 568 Exclude: []*config2.SelectionSpec{ 569 { 570 Daemonsets: []string{"ex-"}, 571 }, 572 }, 573 }, 574 "in-dep1", 575 false, 576 }, 577 { 578 "tested daemonset skip", 579 &config2.BugReportConfig{ 580 Include: []*config2.SelectionSpec{ 581 { 582 Daemonsets: []string{"in-"}, 583 }, 584 }, 585 Exclude: []*config2.SelectionSpec{ 586 { 587 Daemonsets: []string{"ex-"}, 588 }, 589 }, 590 }, 591 "ex-dep1", 592 true, 593 }, 594 } 595 for _, c := range cases { 596 t.Run(c.name, func(t *testing.T) { 597 skip := shouldSkipDaemonSet(c.daemonset, c.config) 598 if skip != c.expected { 599 t.Errorf("shouldSkip() for test case name [%s] return= %v, want %v", c.name, skip, c.expected) 600 } 601 }) 602 } 603 }