github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/kubernetes/cache_test.go (about) 1 // +build !windows 2 3 package kubernetesmonitor 4 5 import ( 6 "reflect" 7 "sync" 8 "testing" 9 10 "go.aporeto.io/trireme-lib/policy" 11 ) 12 13 func Test_cache_updatePUIDCache(t *testing.T) { 14 15 // Pregenerating a couple fake runtimes 16 runtime1 := policy.NewPURuntimeWithDefaults() 17 runtime1.SetPid(1) 18 runtime2 := policy.NewPURuntimeWithDefaults() 19 runtime2.SetPid(2) 20 runtime3 := policy.NewPURuntimeWithDefaults() 21 runtime3.SetPid(3) 22 23 type fields struct { 24 puidCache map[string]*puidCacheEntry 25 podCache map[string]*podCacheEntry 26 RWMutex sync.RWMutex 27 } 28 type args struct { 29 podNamespace string 30 podName string 31 puID string 32 dockerRuntime policy.RuntimeReader 33 kubernetesRuntime policy.RuntimeReader 34 } 35 tests := []struct { 36 name string 37 fields *fields 38 fieldsResult *fields 39 args *args 40 }{ 41 { 42 name: "test empty all", 43 fields: &fields{ 44 puidCache: map[string]*puidCacheEntry{}, 45 podCache: map[string]*podCacheEntry{}, 46 }, 47 fieldsResult: &fields{ 48 puidCache: map[string]*puidCacheEntry{}, 49 podCache: map[string]*podCacheEntry{}, 50 }, 51 args: &args{ 52 podNamespace: "", 53 podName: "", 54 puID: "", 55 dockerRuntime: policy.NewPURuntimeWithDefaults(), 56 kubernetesRuntime: policy.NewPURuntimeWithDefaults(), 57 }, 58 }, 59 { 60 name: "test empty NS", 61 fields: &fields{ 62 puidCache: map[string]*puidCacheEntry{}, 63 podCache: map[string]*podCacheEntry{}, 64 }, 65 fieldsResult: &fields{ 66 puidCache: map[string]*puidCacheEntry{}, 67 podCache: map[string]*podCacheEntry{}, 68 }, 69 args: &args{ 70 podNamespace: "", 71 podName: "xcvxcv", 72 puID: "xcvxcv", 73 dockerRuntime: policy.NewPURuntimeWithDefaults(), 74 kubernetesRuntime: policy.NewPURuntimeWithDefaults(), 75 }, 76 }, 77 { 78 name: "test empty Name", 79 fields: &fields{ 80 puidCache: map[string]*puidCacheEntry{}, 81 podCache: map[string]*podCacheEntry{}, 82 }, 83 fieldsResult: &fields{ 84 puidCache: map[string]*puidCacheEntry{}, 85 podCache: map[string]*podCacheEntry{}, 86 }, 87 args: &args{ 88 podNamespace: "xcvxcv", 89 podName: "", 90 puID: "xcvxcv", 91 dockerRuntime: policy.NewPURuntimeWithDefaults(), 92 kubernetesRuntime: policy.NewPURuntimeWithDefaults(), 93 }, 94 }, 95 { 96 name: "test empty PUID", 97 fields: &fields{ 98 puidCache: map[string]*puidCacheEntry{}, 99 podCache: map[string]*podCacheEntry{}, 100 }, 101 fieldsResult: &fields{ 102 puidCache: map[string]*puidCacheEntry{}, 103 podCache: map[string]*podCacheEntry{}, 104 }, 105 args: &args{ 106 podNamespace: "xcvxcv", 107 podName: "xcvxcv", 108 puID: "", 109 dockerRuntime: policy.NewPURuntimeWithDefaults(), 110 kubernetesRuntime: policy.NewPURuntimeWithDefaults(), 111 }, 112 }, 113 { 114 name: "test normal behavior", 115 fields: &fields{ 116 puidCache: map[string]*puidCacheEntry{}, 117 podCache: map[string]*podCacheEntry{}, 118 }, 119 fieldsResult: &fields{ 120 puidCache: map[string]*puidCacheEntry{ 121 "123456": { 122 kubeIdentifier: "namespace/name", 123 dockerRuntime: runtime1, 124 kubernetesRuntime: runtime2, 125 }, 126 }, 127 podCache: map[string]*podCacheEntry{ 128 "namespace/name": { 129 puIDs: map[string]bool{ 130 "123456": true, 131 }, 132 }, 133 }, 134 }, 135 args: &args{ 136 podNamespace: "namespace", 137 podName: "name", 138 puID: "123456", 139 dockerRuntime: runtime1, 140 kubernetesRuntime: runtime2, 141 }, 142 }, 143 { 144 name: "test additive behavior", 145 fields: &fields{ 146 puidCache: map[string]*puidCacheEntry{ 147 "123456": { 148 kubeIdentifier: "namespace/name", 149 dockerRuntime: runtime1, 150 kubernetesRuntime: runtime2, 151 }, 152 }, 153 podCache: map[string]*podCacheEntry{ 154 "namespace/name": { 155 puIDs: map[string]bool{ 156 "123456": true, 157 }, 158 }, 159 }, 160 }, 161 fieldsResult: &fields{ 162 puidCache: map[string]*puidCacheEntry{ 163 "123456": { 164 kubeIdentifier: "namespace/name", 165 dockerRuntime: runtime1, 166 kubernetesRuntime: runtime2, 167 }, 168 "abcdef": { 169 kubeIdentifier: "namespace2/name2", 170 dockerRuntime: runtime3, 171 kubernetesRuntime: runtime2, 172 }, 173 }, 174 podCache: map[string]*podCacheEntry{ 175 "namespace/name": { 176 puIDs: map[string]bool{ 177 "123456": true, 178 }, 179 }, 180 "namespace2/name2": { 181 puIDs: map[string]bool{ 182 "abcdef": true, 183 }, 184 }, 185 }, 186 }, 187 args: &args{ 188 podNamespace: "namespace2", 189 podName: "name2", 190 puID: "abcdef", 191 dockerRuntime: runtime3, 192 kubernetesRuntime: runtime2, 193 }, 194 }, 195 { 196 name: "test additive same pod", 197 fields: &fields{ 198 puidCache: map[string]*puidCacheEntry{ 199 "123456": { 200 kubeIdentifier: "namespace/name", 201 dockerRuntime: runtime1, 202 kubernetesRuntime: runtime2, 203 }, 204 }, 205 podCache: map[string]*podCacheEntry{ 206 "namespace/name": { 207 puIDs: map[string]bool{ 208 "123456": true, 209 }, 210 }, 211 }, 212 }, 213 fieldsResult: &fields{ 214 puidCache: map[string]*puidCacheEntry{ 215 "123456": { 216 kubeIdentifier: "namespace/name", 217 dockerRuntime: runtime1, 218 kubernetesRuntime: runtime2, 219 }, 220 "abcdef": { 221 kubeIdentifier: "namespace/name", 222 dockerRuntime: runtime3, 223 kubernetesRuntime: runtime2, 224 }, 225 }, 226 podCache: map[string]*podCacheEntry{ 227 "namespace/name": { 228 puIDs: map[string]bool{ 229 "123456": true, 230 "abcdef": true, 231 }, 232 }, 233 }, 234 }, 235 args: &args{ 236 podNamespace: "namespace", 237 podName: "name", 238 puID: "abcdef", 239 dockerRuntime: runtime3, 240 kubernetesRuntime: runtime2, 241 }, 242 }, 243 } 244 for _, tt := range tests { 245 t.Run(tt.name, func(t *testing.T) { 246 c := &cache{ 247 puidCache: tt.fields.puidCache, 248 podCache: tt.fields.podCache, 249 } 250 c.updatePUIDCache(tt.args.podNamespace, tt.args.podName, tt.args.puID, tt.args.dockerRuntime, tt.args.kubernetesRuntime) 251 if !reflect.DeepEqual(c.puidCache, tt.fieldsResult.puidCache) { 252 t.Errorf("updatePUIDCache() field. got %v, want %v", c.puidCache, tt.fieldsResult.puidCache) 253 } 254 if !reflect.DeepEqual(c.podCache, tt.fieldsResult.podCache) { 255 t.Errorf("updatePUIDCache() field. got %v, want %v", c.podCache, tt.fieldsResult.podCache) 256 } 257 }) 258 } 259 } 260 261 func Test_cache_getPUIDsbyPod(t *testing.T) { 262 puid1 := "12350" 263 pod1 := "test/test5" 264 puidEntry1 := &puidCacheEntry{ 265 kubeIdentifier: pod1, 266 } 267 podEntry1 := &podCacheEntry{ 268 puIDs: map[string]bool{ 269 puid1: true, 270 }, 271 } 272 273 type fields struct { 274 puidCache map[string]*puidCacheEntry 275 podCache map[string]*podCacheEntry 276 RWMutex sync.RWMutex 277 } 278 type args struct { 279 podNamespace string 280 podName string 281 } 282 tests := []struct { 283 name string 284 fields *fields 285 args *args 286 want []string 287 }{ 288 { 289 name: "simple get", 290 fields: &fields{ 291 puidCache: map[string]*puidCacheEntry{ 292 puid1: puidEntry1, 293 }, 294 podCache: map[string]*podCacheEntry{ 295 pod1: podEntry1, 296 }, 297 }, 298 args: &args{ 299 podName: "test5", 300 podNamespace: "test", 301 }, 302 want: []string{puid1}, 303 }, 304 { 305 name: "non existing", 306 fields: &fields{ 307 puidCache: map[string]*puidCacheEntry{ 308 puid1: puidEntry1, 309 }, 310 podCache: map[string]*podCacheEntry{ 311 pod1: podEntry1, 312 }, 313 }, 314 args: &args{ 315 podName: "test1", 316 podNamespace: "test", 317 }, 318 want: []string{}, 319 }, 320 } 321 322 for _, tt := range tests { 323 t.Run(tt.name, func(t *testing.T) { 324 c := &cache{ 325 puidCache: tt.fields.puidCache, 326 podCache: tt.fields.podCache, 327 } 328 if got := c.getPUIDsbyPod(tt.args.podNamespace, tt.args.podName); !reflect.DeepEqual(got, tt.want) { 329 t.Errorf("cache.getPUIDsbyPod() = %v, want %v", got, tt.want) 330 } 331 }) 332 } 333 } 334 335 func Test_cache_getDockerRuntimeByPUID(t *testing.T) { 336 337 puid1 := "12346" 338 pod1 := "test/test1" 339 containerRuntime := policy.NewPURuntimeWithDefaults() 340 containerRuntime.SetPid(123) 341 puidEntry1 := &puidCacheEntry{ 342 kubeIdentifier: pod1, 343 dockerRuntime: containerRuntime, 344 } 345 podEntry1 := &podCacheEntry{ 346 puIDs: map[string]bool{ 347 puid1: true, 348 }, 349 } 350 351 type fields struct { 352 puidCache map[string]*puidCacheEntry 353 podCache map[string]*podCacheEntry 354 RWMutex sync.RWMutex 355 } 356 type args struct { 357 puid string 358 } 359 tests := []struct { 360 name string 361 fields *fields 362 args *args 363 want policy.RuntimeReader 364 }{ 365 { 366 name: "simple get", 367 fields: &fields{ 368 puidCache: map[string]*puidCacheEntry{ 369 puid1: puidEntry1, 370 }, 371 podCache: map[string]*podCacheEntry{ 372 pod1: podEntry1, 373 }, 374 }, 375 args: &args{ 376 puid: puid1, 377 }, 378 want: containerRuntime, 379 }, 380 { 381 name: "empty get", 382 fields: &fields{ 383 puidCache: map[string]*puidCacheEntry{ 384 puid1: puidEntry1, 385 }, 386 podCache: map[string]*podCacheEntry{ 387 pod1: podEntry1, 388 }, 389 }, 390 args: &args{ 391 puid: "123123", 392 }, 393 want: nil, 394 }, 395 } 396 for _, tt := range tests { 397 t.Run(tt.name, func(t *testing.T) { 398 c := &cache{ 399 puidCache: tt.fields.puidCache, 400 podCache: tt.fields.podCache, 401 } 402 if got := c.getDockerRuntimeByPUID(tt.args.puid); !reflect.DeepEqual(got, tt.want) { 403 t.Errorf("cache.getDockerRuntimeByPUID() = %v, want %v", got, tt.want) 404 } 405 }) 406 } 407 } 408 409 func Test_cache_getKubernetesRuntimeByPUID(t *testing.T) { 410 411 puid1 := "12347" 412 pod1 := "test/test2" 413 containerRuntime := policy.NewPURuntimeWithDefaults() 414 containerRuntime.SetPid(123) 415 puidEntry1 := &puidCacheEntry{ 416 kubeIdentifier: pod1, 417 kubernetesRuntime: containerRuntime, 418 } 419 podEntry1 := &podCacheEntry{ 420 puIDs: map[string]bool{ 421 puid1: true, 422 }, 423 } 424 425 type fields struct { 426 puidCache map[string]*puidCacheEntry 427 podCache map[string]*podCacheEntry 428 RWMutex sync.RWMutex 429 } 430 type args struct { 431 puid string 432 } 433 tests := []struct { 434 name string 435 fields *fields 436 args *args 437 want policy.RuntimeReader 438 }{ 439 { 440 name: "simple get", 441 fields: &fields{ 442 puidCache: map[string]*puidCacheEntry{ 443 puid1: puidEntry1, 444 }, 445 podCache: map[string]*podCacheEntry{ 446 pod1: podEntry1, 447 }, 448 }, 449 args: &args{ 450 puid: puid1, 451 }, 452 want: containerRuntime, 453 }, 454 { 455 name: "empty get", 456 fields: &fields{ 457 puidCache: map[string]*puidCacheEntry{ 458 puid1: puidEntry1, 459 }, 460 podCache: map[string]*podCacheEntry{ 461 pod1: podEntry1, 462 }, 463 }, 464 args: &args{ 465 puid: "123123", 466 }, 467 want: nil, 468 }, 469 } 470 for _, tt := range tests { 471 t.Run(tt.name, func(t *testing.T) { 472 c := &cache{ 473 puidCache: tt.fields.puidCache, 474 podCache: tt.fields.podCache, 475 } 476 if got := c.getKubernetesRuntimeByPUID(tt.args.puid); !reflect.DeepEqual(got, tt.want) { 477 t.Errorf("cache.getKubernetesRuntimeByPUID() = %v, want %v", got, tt.want) 478 } 479 }) 480 } 481 } 482 483 func Test_cache_deletePodEntry(t *testing.T) { 484 485 puid1 := "12348" 486 pod1 := "test/test3" 487 containerRuntime := policy.NewPURuntimeWithDefaults() 488 containerRuntime.SetPid(123) 489 puidEntry1 := &puidCacheEntry{ 490 kubeIdentifier: pod1, 491 kubernetesRuntime: containerRuntime, 492 } 493 podEntry1 := &podCacheEntry{ 494 puIDs: map[string]bool{ 495 puid1: true, 496 }, 497 } 498 499 type fields struct { 500 puidCache map[string]*puidCacheEntry 501 podCache map[string]*podCacheEntry 502 RWMutex sync.RWMutex 503 } 504 type args struct { 505 podNamespace string 506 podName string 507 } 508 tests := []struct { 509 name string 510 fields *fields 511 args *args 512 want1 map[string]*puidCacheEntry 513 want2 map[string]*podCacheEntry 514 }{ 515 { 516 name: "simple delete", 517 fields: &fields{ 518 puidCache: map[string]*puidCacheEntry{ 519 puid1: puidEntry1, 520 }, 521 podCache: map[string]*podCacheEntry{ 522 pod1: podEntry1, 523 }, 524 }, 525 args: &args{ 526 podName: "test3", 527 podNamespace: "test", 528 }, 529 want1: map[string]*puidCacheEntry{ 530 puid1: puidEntry1, 531 }, 532 want2: map[string]*podCacheEntry{}, 533 }, 534 { 535 name: "non mexisting delete", 536 fields: &fields{ 537 puidCache: map[string]*puidCacheEntry{ 538 puid1: puidEntry1, 539 }, 540 podCache: map[string]*podCacheEntry{ 541 pod1: podEntry1, 542 }, 543 }, 544 args: &args{ 545 podName: "test2", 546 podNamespace: "test", 547 }, 548 want1: map[string]*puidCacheEntry{ 549 puid1: puidEntry1, 550 }, 551 want2: map[string]*podCacheEntry{ 552 pod1: podEntry1, 553 }, 554 }, 555 } 556 for _, tt := range tests { 557 t.Run(tt.name, func(t *testing.T) { 558 c := &cache{ 559 puidCache: tt.fields.puidCache, 560 podCache: tt.fields.podCache, 561 } 562 c.deletePodEntry(tt.args.podNamespace, tt.args.podName) 563 564 if got := tt.fields.puidCache; !reflect.DeepEqual(got, tt.want1) { 565 t.Errorf("after cache.deleteByPod = %v, want %v", got, tt.want1) 566 } 567 if got := tt.fields.podCache; !reflect.DeepEqual(got, tt.want2) { 568 t.Errorf("after cache.deleteByPod = %v, want %v", got, tt.want2) 569 } 570 }) 571 } 572 } 573 574 func Test_cache_deletePUIDEntry(t *testing.T) { 575 576 puid1 := "12349" 577 pod1 := "test/test4" 578 containerRuntime := policy.NewPURuntimeWithDefaults() 579 containerRuntime.SetPid(123) 580 puidEntry1 := &puidCacheEntry{ 581 kubeIdentifier: pod1, 582 kubernetesRuntime: containerRuntime, 583 } 584 podEntry1 := &podCacheEntry{ 585 puIDs: map[string]bool{ 586 puid1: true, 587 }, 588 } 589 590 type fields struct { 591 puidCache map[string]*puidCacheEntry 592 podCache map[string]*podCacheEntry 593 RWMutex sync.RWMutex 594 } 595 type args struct { 596 puid string 597 } 598 tests := []struct { 599 name string 600 fields *fields 601 args *args 602 want1 map[string]*puidCacheEntry 603 want2 map[string]*podCacheEntry 604 }{ 605 { 606 name: "simple delete", 607 fields: &fields{ 608 puidCache: map[string]*puidCacheEntry{ 609 puid1: puidEntry1, 610 }, 611 podCache: map[string]*podCacheEntry{ 612 pod1: podEntry1, 613 }, 614 }, 615 args: &args{ 616 puid: puid1, 617 }, 618 want1: map[string]*puidCacheEntry{}, 619 want2: map[string]*podCacheEntry{ 620 pod1: podEntry1, 621 }, 622 }, 623 { 624 name: "non mexisting delete", 625 fields: &fields{ 626 puidCache: map[string]*puidCacheEntry{ 627 puid1: puidEntry1, 628 }, 629 podCache: map[string]*podCacheEntry{ 630 pod1: podEntry1, 631 }, 632 }, 633 args: &args{ 634 puid: "123123", 635 }, 636 want1: map[string]*puidCacheEntry{ 637 puid1: puidEntry1, 638 }, 639 want2: map[string]*podCacheEntry{ 640 pod1: podEntry1, 641 }, 642 }, 643 } 644 for _, tt := range tests { 645 t.Run(tt.name, func(t *testing.T) { 646 c := &cache{ 647 puidCache: tt.fields.puidCache, 648 podCache: tt.fields.podCache, 649 } 650 c.deletePUIDEntry(tt.args.puid) 651 652 if got := tt.fields.puidCache; !reflect.DeepEqual(got, tt.want1) { 653 t.Errorf("after cache.deleteByPod = %v, want %v", got, tt.want1) 654 } 655 if got := tt.fields.podCache; !reflect.DeepEqual(got, tt.want2) { 656 t.Errorf("after cache.deleteByPod = %v, want %v", got, tt.want2) 657 } 658 659 }) 660 } 661 } 662 663 func Test_cache_deletePUIDCache(t *testing.T) { 664 665 puid1 := "12349" 666 pod1 := "test/test4" 667 containerRuntime := policy.NewPURuntimeWithDefaults() 668 containerRuntime.SetPid(123) 669 puidEntry1 := &puidCacheEntry{ 670 kubeIdentifier: pod1, 671 kubernetesRuntime: containerRuntime, 672 } 673 podEntry1 := &podCacheEntry{ 674 puIDs: map[string]bool{ 675 puid1: true, 676 }, 677 } 678 679 type fields struct { 680 puidCache map[string]*puidCacheEntry 681 podCache map[string]*podCacheEntry 682 RWMutex sync.RWMutex 683 } 684 type args struct { 685 puID string 686 } 687 tests := []struct { 688 name string 689 fields *fields 690 args *args 691 want1 map[string]*puidCacheEntry 692 want2 map[string]*podCacheEntry 693 }{ 694 { 695 name: "Delete on empty cache", 696 fields: &fields{}, 697 args: &args{ 698 puID: "1234", 699 }, 700 want1: nil, 701 want2: nil, 702 }, 703 { 704 name: "Deleting a non existent puid", 705 fields: &fields{ 706 puidCache: map[string]*puidCacheEntry{ 707 puid1: puidEntry1, 708 }, 709 podCache: map[string]*podCacheEntry{ 710 pod1: podEntry1, 711 }, 712 }, 713 args: &args{ 714 puID: "1234", 715 }, 716 want1: map[string]*puidCacheEntry{ 717 puid1: puidEntry1, 718 }, 719 want2: map[string]*podCacheEntry{ 720 pod1: podEntry1, 721 }, 722 }, 723 { 724 name: "Normal case", 725 fields: &fields{ 726 puidCache: map[string]*puidCacheEntry{ 727 puid1: puidEntry1, 728 }, 729 podCache: map[string]*podCacheEntry{ 730 pod1: podEntry1, 731 }, 732 }, 733 args: &args{ 734 puID: "12349", 735 }, 736 want1: map[string]*puidCacheEntry{}, 737 want2: map[string]*podCacheEntry{}, 738 }, 739 } 740 for _, tt := range tests { 741 t.Run(tt.name, func(t *testing.T) { 742 c := &cache{ 743 puidCache: tt.fields.puidCache, 744 podCache: tt.fields.podCache, 745 } 746 c.deletePUIDCache(tt.args.puID) 747 if got := tt.fields.puidCache; !reflect.DeepEqual(got, tt.want1) { 748 t.Errorf("after cache.deleteByPod = %v, want %v", got, tt.want1) 749 } 750 if got := tt.fields.podCache; !reflect.DeepEqual(got, tt.want2) { 751 t.Errorf("after cache.deleteByPod = %v, want %v", got, tt.want2) 752 } 753 }) 754 } 755 }