github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/utils/cri/cri_runtime_wrapper_test.go (about) 1 package cri 2 3 import ( 4 "context" 5 "errors" 6 "reflect" 7 "testing" 8 "time" 9 10 "github.com/golang/mock/gomock" 11 "go.aporeto.io/enforcerd/trireme-lib/utils/cri/mockcri" 12 criruntimev1alpha2 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" 13 ) 14 15 func TestNewCRIExtendedRuntimeServiceWrapper(t *testing.T) { 16 type args struct { 17 ctx context.Context 18 timeout time.Duration 19 client criruntimev1alpha2.RuntimeServiceClient 20 } 21 tests := []struct { 22 name string 23 args args 24 want ExtendedRuntimeService 25 wantErr bool 26 }{ 27 { 28 name: "client is nil", 29 args: args{ 30 ctx: context.Background(), 31 timeout: connectTimeout, 32 client: nil, 33 }, 34 want: nil, 35 wantErr: true, 36 }, 37 { 38 name: "timeout is 0", 39 args: args{ 40 ctx: context.Background(), 41 timeout: 0, 42 client: criruntimev1alpha2.NewRuntimeServiceClient(nil), 43 }, 44 want: nil, 45 wantErr: true, 46 }, 47 { 48 name: "success", 49 args: args{ 50 ctx: context.Background(), 51 timeout: connectTimeout, 52 client: criruntimev1alpha2.NewRuntimeServiceClient(nil), 53 }, 54 want: &extendedServiceRuntimeWrapper{ 55 ctx: context.Background(), 56 timeout: connectTimeout, 57 rs: criruntimev1alpha2.NewRuntimeServiceClient(nil), 58 }, 59 wantErr: false, 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 got, err := NewCRIExtendedRuntimeServiceWrapper(tt.args.ctx, tt.args.timeout, tt.args.client) 65 if (err != nil) != tt.wantErr { 66 t.Errorf("NewCRIExtendedRuntimeServiceWrapper() error = %v, wantErr %v", err, tt.wantErr) 67 return 68 } 69 if !reflect.DeepEqual(got, tt.want) { 70 t.Errorf("NewCRIExtendedRuntimeServiceWrapper() = %v, want %v", got, tt.want) 71 } 72 }) 73 } 74 } 75 76 func newUnitTestCRIExtendedRuntimeServiceWrapper(t *testing.T) (*gomock.Controller, *mockcri.MockRuntimeServiceClient, context.CancelFunc, ExtendedRuntimeService) { 77 ctrl := gomock.NewController(t) 78 client := mockcri.NewMockRuntimeServiceClient(ctrl) 79 ctx, cancel := context.WithCancel(context.Background()) 80 w, err := NewCRIExtendedRuntimeServiceWrapper(ctx, connectTimeout, client) 81 if err != nil { 82 panic(err) 83 } 84 return ctrl, client, cancel, w 85 } 86 87 type prepareFunc func(*testing.T, *mockcri.MockRuntimeServiceClient) 88 89 var errMock = errors.New("mocked error has occurred") 90 91 func Test_extendedServiceRuntimeWrapper_Version(t *testing.T) { 92 type args struct { 93 apiVersion string 94 } 95 tests := []struct { 96 name string 97 args args 98 prepare prepareFunc 99 want *criruntimev1alpha2.VersionResponse 100 wantErr bool 101 }{ 102 { 103 name: "error", 104 args: args{ 105 apiVersion: "version", 106 }, 107 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 108 c.EXPECT().Version( 109 gomock.Any(), 110 gomock.Eq(&criruntimev1alpha2.VersionRequest{Version: "version"}), 111 ).Times(1).Return(nil, errMock) 112 }, 113 want: nil, 114 wantErr: true, 115 }, 116 { 117 name: "success", 118 args: args{ 119 apiVersion: "version", 120 }, 121 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 122 c.EXPECT().Version( 123 gomock.Any(), 124 gomock.Eq(&criruntimev1alpha2.VersionRequest{Version: "version"}), 125 ).Times(1).Return(&criruntimev1alpha2.VersionResponse{}, nil) 126 }, 127 want: &criruntimev1alpha2.VersionResponse{}, 128 wantErr: false, 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 // the same in every test 134 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 135 defer cancel() 136 defer ctrl.Finish() 137 if tt.prepare != nil { 138 tt.prepare(t, client) 139 } 140 got, err := w.Version(tt.args.apiVersion) 141 if (err != nil) != tt.wantErr { 142 t.Errorf("extendedServiceRuntimeWrapper.Version() error = %v, wantErr %v", err, tt.wantErr) 143 return 144 } 145 if !reflect.DeepEqual(got, tt.want) { 146 t.Errorf("extendedServiceRuntimeWrapper.Version() = %v, want %v", got, tt.want) 147 } 148 }) 149 } 150 } 151 152 func Test_extendedServiceRuntimeWrapper_CreateContainer(t *testing.T) { 153 type args struct { 154 podSandboxID string 155 config *criruntimev1alpha2.ContainerConfig 156 sandboxConfig *criruntimev1alpha2.PodSandboxConfig 157 } 158 tests := []struct { 159 name string 160 args args 161 prepare prepareFunc 162 want string 163 wantErr bool 164 }{ 165 { 166 name: "error", 167 args: args{ 168 podSandboxID: "sandboxID", 169 config: &criruntimev1alpha2.ContainerConfig{}, 170 sandboxConfig: &criruntimev1alpha2.PodSandboxConfig{}, 171 }, 172 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 173 c.EXPECT().CreateContainer( 174 gomock.Any(), 175 gomock.Eq(&criruntimev1alpha2.CreateContainerRequest{ 176 PodSandboxId: "sandboxID", 177 Config: &criruntimev1alpha2.ContainerConfig{}, 178 SandboxConfig: &criruntimev1alpha2.PodSandboxConfig{}, 179 }), 180 ).Times(1).Return(nil, errMock) 181 }, 182 want: "", 183 wantErr: true, 184 }, 185 { 186 name: "success", 187 args: args{ 188 podSandboxID: "sandboxID", 189 config: &criruntimev1alpha2.ContainerConfig{}, 190 sandboxConfig: &criruntimev1alpha2.PodSandboxConfig{}, 191 }, 192 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 193 c.EXPECT().CreateContainer( 194 gomock.Any(), 195 gomock.Eq(&criruntimev1alpha2.CreateContainerRequest{ 196 PodSandboxId: "sandboxID", 197 Config: &criruntimev1alpha2.ContainerConfig{}, 198 SandboxConfig: &criruntimev1alpha2.PodSandboxConfig{}, 199 }), 200 ).Times(1).Return(&criruntimev1alpha2.CreateContainerResponse{ 201 ContainerId: "containerID", 202 }, nil) 203 }, 204 want: "containerID", 205 wantErr: false, 206 }, 207 } 208 for _, tt := range tests { 209 t.Run(tt.name, func(t *testing.T) { 210 // the same in every test 211 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 212 defer cancel() 213 defer ctrl.Finish() 214 if tt.prepare != nil { 215 tt.prepare(t, client) 216 } 217 got, err := w.CreateContainer(tt.args.podSandboxID, tt.args.config, tt.args.sandboxConfig) 218 if (err != nil) != tt.wantErr { 219 t.Errorf("extendedServiceRuntimeWrapper.CreateContainer() error = %v, wantErr %v", err, tt.wantErr) 220 return 221 } 222 if got != tt.want { 223 t.Errorf("extendedServiceRuntimeWrapper.CreateContainer() = %v, want %v", got, tt.want) 224 } 225 }) 226 } 227 } 228 229 func Test_extendedServiceRuntimeWrapper_StartContainer(t *testing.T) { 230 type args struct { 231 containerID string 232 } 233 tests := []struct { 234 name string 235 args args 236 prepare prepareFunc 237 wantErr bool 238 }{ 239 { 240 name: "error", 241 args: args{ 242 containerID: "containerID", 243 }, 244 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 245 c.EXPECT().StartContainer( 246 gomock.Any(), 247 gomock.Eq(&criruntimev1alpha2.StartContainerRequest{ 248 ContainerId: "containerID", 249 }), 250 ).Times(1).Return(nil, errMock) 251 }, 252 wantErr: true, 253 }, 254 { 255 name: "error", 256 args: args{ 257 containerID: "containerID", 258 }, 259 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 260 c.EXPECT().StartContainer( 261 gomock.Any(), 262 gomock.Eq(&criruntimev1alpha2.StartContainerRequest{ 263 ContainerId: "containerID", 264 }), 265 ).Times(1).Return(&criruntimev1alpha2.StartContainerResponse{}, nil) 266 }, 267 wantErr: false, 268 }, 269 } 270 for _, tt := range tests { 271 t.Run(tt.name, func(t *testing.T) { 272 // the same in every test 273 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 274 defer cancel() 275 defer ctrl.Finish() 276 if tt.prepare != nil { 277 tt.prepare(t, client) 278 } 279 if err := w.StartContainer(tt.args.containerID); (err != nil) != tt.wantErr { 280 t.Errorf("extendedServiceRuntimeWrapper.StartContainer() error = %v, wantErr %v", err, tt.wantErr) 281 } 282 }) 283 } 284 } 285 286 func Test_extendedServiceRuntimeWrapper_StopContainer(t *testing.T) { 287 type args struct { 288 containerID string 289 timeout int64 290 } 291 tests := []struct { 292 name string 293 prepare prepareFunc 294 args args 295 wantErr bool 296 }{ 297 { 298 name: "error", 299 args: args{ 300 containerID: "containerID", 301 timeout: 42, 302 }, 303 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 304 c.EXPECT().StopContainer( 305 gomock.Any(), 306 gomock.Eq(&criruntimev1alpha2.StopContainerRequest{ 307 ContainerId: "containerID", 308 Timeout: 42, 309 }), 310 ).Times(1).Return(nil, errMock) 311 }, 312 wantErr: true, 313 }, 314 { 315 name: "error", 316 args: args{ 317 containerID: "containerID", 318 timeout: 42, 319 }, 320 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 321 c.EXPECT().StopContainer( 322 gomock.Any(), 323 gomock.Eq(&criruntimev1alpha2.StopContainerRequest{ 324 ContainerId: "containerID", 325 Timeout: 42, 326 }), 327 ).Times(1).Return(&criruntimev1alpha2.StopContainerResponse{}, nil) 328 }, 329 wantErr: false, 330 }, 331 } 332 for _, tt := range tests { 333 t.Run(tt.name, func(t *testing.T) { 334 // the same in every test 335 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 336 defer cancel() 337 defer ctrl.Finish() 338 if tt.prepare != nil { 339 tt.prepare(t, client) 340 } 341 if err := w.StopContainer(tt.args.containerID, tt.args.timeout); (err != nil) != tt.wantErr { 342 t.Errorf("extendedServiceRuntimeWrapper.StopContainer() error = %v, wantErr %v", err, tt.wantErr) 343 } 344 }) 345 } 346 } 347 348 func Test_extendedServiceRuntimeWrapper_RemoveContainer(t *testing.T) { 349 type args struct { 350 containerID string 351 } 352 tests := []struct { 353 name string 354 prepare prepareFunc 355 args args 356 wantErr bool 357 }{ 358 { 359 name: "error", 360 args: args{ 361 containerID: "containerID", 362 }, 363 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 364 c.EXPECT().RemoveContainer( 365 gomock.Any(), 366 gomock.Eq(&criruntimev1alpha2.RemoveContainerRequest{ 367 ContainerId: "containerID", 368 }), 369 ).Times(1).Return(nil, errMock) 370 }, 371 wantErr: true, 372 }, 373 { 374 name: "success", 375 args: args{ 376 containerID: "containerID", 377 }, 378 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 379 c.EXPECT().RemoveContainer( 380 gomock.Any(), 381 gomock.Eq(&criruntimev1alpha2.RemoveContainerRequest{ 382 ContainerId: "containerID", 383 }), 384 ).Times(1).Return(&criruntimev1alpha2.RemoveContainerResponse{}, nil) 385 }, 386 wantErr: false, 387 }, 388 } 389 for _, tt := range tests { 390 t.Run(tt.name, func(t *testing.T) { 391 // the same in every test 392 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 393 defer cancel() 394 defer ctrl.Finish() 395 if tt.prepare != nil { 396 tt.prepare(t, client) 397 } 398 if err := w.RemoveContainer(tt.args.containerID); (err != nil) != tt.wantErr { 399 t.Errorf("extendedServiceRuntimeWrapper.RemoveContainer() error = %v, wantErr %v", err, tt.wantErr) 400 } 401 }) 402 } 403 } 404 405 func Test_extendedServiceRuntimeWrapper_ListContainers(t *testing.T) { 406 type args struct { 407 filter *criruntimev1alpha2.ContainerFilter 408 } 409 tests := []struct { 410 name string 411 prepare prepareFunc 412 args args 413 want []*criruntimev1alpha2.Container 414 wantErr bool 415 }{ 416 { 417 name: "error", 418 args: args{ 419 filter: &criruntimev1alpha2.ContainerFilter{ 420 LabelSelector: map[string]string{ 421 "a": "b", 422 }, 423 }, 424 }, 425 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 426 c.EXPECT().ListContainers( 427 gomock.Any(), 428 gomock.Eq(&criruntimev1alpha2.ListContainersRequest{ 429 Filter: &criruntimev1alpha2.ContainerFilter{ 430 LabelSelector: map[string]string{ 431 "a": "b", 432 }, 433 }, 434 }), 435 ).Times(1).Return(nil, errMock) 436 }, 437 want: nil, 438 wantErr: true, 439 }, 440 { 441 name: "success", 442 args: args{ 443 filter: &criruntimev1alpha2.ContainerFilter{ 444 LabelSelector: map[string]string{ 445 "a": "b", 446 }, 447 }, 448 }, 449 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 450 c.EXPECT().ListContainers( 451 gomock.Any(), 452 gomock.Eq(&criruntimev1alpha2.ListContainersRequest{ 453 Filter: &criruntimev1alpha2.ContainerFilter{ 454 LabelSelector: map[string]string{ 455 "a": "b", 456 }, 457 }, 458 }), 459 ).Times(1).Return(&criruntimev1alpha2.ListContainersResponse{ 460 Containers: []*criruntimev1alpha2.Container{ 461 { 462 Id: "one", 463 Labels: map[string]string{ 464 "a": "b", 465 }, 466 }, 467 { 468 Id: "two", 469 Labels: map[string]string{ 470 "a": "b", 471 }, 472 }, 473 }, 474 }, nil) 475 }, 476 want: []*criruntimev1alpha2.Container{ 477 { 478 Id: "one", 479 Labels: map[string]string{ 480 "a": "b", 481 }, 482 }, 483 { 484 Id: "two", 485 Labels: map[string]string{ 486 "a": "b", 487 }, 488 }, 489 }, 490 wantErr: false, 491 }, 492 } 493 for _, tt := range tests { 494 t.Run(tt.name, func(t *testing.T) { 495 // the same in every test 496 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 497 defer cancel() 498 defer ctrl.Finish() 499 if tt.prepare != nil { 500 tt.prepare(t, client) 501 } 502 got, err := w.ListContainers(tt.args.filter) 503 if (err != nil) != tt.wantErr { 504 t.Errorf("extendedServiceRuntimeWrapper.ListContainers() error = %v, wantErr %v", err, tt.wantErr) 505 return 506 } 507 if !reflect.DeepEqual(got, tt.want) { 508 t.Errorf("extendedServiceRuntimeWrapper.ListContainers() = %v, want %v", got, tt.want) 509 } 510 }) 511 } 512 } 513 514 func Test_extendedServiceRuntimeWrapper_ContainerStatus(t *testing.T) { 515 type args struct { 516 containerID string 517 } 518 tests := []struct { 519 name string 520 prepare prepareFunc 521 args args 522 want *criruntimev1alpha2.ContainerStatus 523 wantErr bool 524 }{ 525 { 526 name: "error", 527 args: args{ 528 containerID: "containerID", 529 }, 530 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 531 c.EXPECT().ContainerStatus( 532 gomock.Any(), 533 gomock.Eq(&criruntimev1alpha2.ContainerStatusRequest{ 534 ContainerId: "containerID", 535 Verbose: false, 536 }), 537 ).Times(1).Return(nil, errMock) 538 }, 539 want: nil, 540 wantErr: true, 541 }, 542 { 543 name: "success", 544 args: args{ 545 containerID: "containerID", 546 }, 547 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 548 c.EXPECT().ContainerStatus( 549 gomock.Any(), 550 gomock.Eq(&criruntimev1alpha2.ContainerStatusRequest{ 551 ContainerId: "containerID", 552 Verbose: false, 553 }), 554 ).Times(1).Return(&criruntimev1alpha2.ContainerStatusResponse{ 555 Status: &criruntimev1alpha2.ContainerStatus{ 556 Id: "containerID", 557 State: criruntimev1alpha2.ContainerState_CONTAINER_RUNNING, 558 }, 559 }, nil) 560 }, 561 want: &criruntimev1alpha2.ContainerStatus{ 562 Id: "containerID", 563 State: criruntimev1alpha2.ContainerState_CONTAINER_RUNNING, 564 }, 565 wantErr: false, 566 }, 567 } 568 for _, tt := range tests { 569 t.Run(tt.name, func(t *testing.T) { 570 // the same in every test 571 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 572 defer cancel() 573 defer ctrl.Finish() 574 if tt.prepare != nil { 575 tt.prepare(t, client) 576 } 577 got, err := w.ContainerStatus(tt.args.containerID) 578 if (err != nil) != tt.wantErr { 579 t.Errorf("extendedServiceRuntimeWrapper.ContainerStatus() error = %v, wantErr %v", err, tt.wantErr) 580 return 581 } 582 if !reflect.DeepEqual(got, tt.want) { 583 t.Errorf("extendedServiceRuntimeWrapper.ContainerStatus() = %v, want %v", got, tt.want) 584 } 585 }) 586 } 587 } 588 589 func Test_extendedServiceRuntimeWrapper_ContainerStatusVerbose(t *testing.T) { 590 591 type args struct { 592 containerID string 593 } 594 tests := []struct { 595 name string 596 prepare prepareFunc 597 args args 598 want *criruntimev1alpha2.ContainerStatus 599 want1 map[string]string 600 wantErr bool 601 }{ 602 { 603 name: "error", 604 args: args{ 605 containerID: "containerID", 606 }, 607 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 608 c.EXPECT().ContainerStatus( 609 gomock.Any(), 610 gomock.Eq(&criruntimev1alpha2.ContainerStatusRequest{ 611 ContainerId: "containerID", 612 Verbose: true, 613 }), 614 ).Times(1).Return(nil, errMock) 615 }, 616 want: nil, 617 wantErr: true, 618 }, 619 { 620 name: "success", 621 args: args{ 622 containerID: "containerID", 623 }, 624 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 625 c.EXPECT().ContainerStatus( 626 gomock.Any(), 627 gomock.Eq(&criruntimev1alpha2.ContainerStatusRequest{ 628 ContainerId: "containerID", 629 Verbose: true, 630 }), 631 ).Times(1).Return(&criruntimev1alpha2.ContainerStatusResponse{ 632 Status: &criruntimev1alpha2.ContainerStatus{ 633 Id: "containerID", 634 State: criruntimev1alpha2.ContainerState_CONTAINER_RUNNING, 635 }, 636 }, nil) 637 }, 638 want: &criruntimev1alpha2.ContainerStatus{ 639 Id: "containerID", 640 State: criruntimev1alpha2.ContainerState_CONTAINER_RUNNING, 641 }, 642 wantErr: false, 643 }, 644 } 645 for _, tt := range tests { 646 t.Run(tt.name, func(t *testing.T) { 647 // the same in every test 648 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 649 defer cancel() 650 defer ctrl.Finish() 651 if tt.prepare != nil { 652 tt.prepare(t, client) 653 } 654 got, got1, err := w.ContainerStatusVerbose(tt.args.containerID) 655 if (err != nil) != tt.wantErr { 656 t.Errorf("extendedServiceRuntimeWrapper.ContainerStatusVerbose() error = %v, wantErr %v", err, tt.wantErr) 657 return 658 } 659 if !reflect.DeepEqual(got, tt.want) { 660 t.Errorf("extendedServiceRuntimeWrapper.ContainerStatusVerbose() got = %v, want %v", got, tt.want) 661 } 662 if !reflect.DeepEqual(got1, tt.want1) { 663 t.Errorf("extendedServiceRuntimeWrapper.ContainerStatusVerbose() got1 = %v, want %v", got1, tt.want1) 664 } 665 }) 666 } 667 } 668 669 func Test_extendedServiceRuntimeWrapper_UpdateContainerResources(t *testing.T) { 670 671 type args struct { 672 containerID string 673 resources *criruntimev1alpha2.LinuxContainerResources 674 } 675 tests := []struct { 676 name string 677 prepare prepareFunc 678 args args 679 wantErr bool 680 }{ 681 { 682 name: "error", 683 args: args{ 684 containerID: "containerID", 685 resources: &criruntimev1alpha2.LinuxContainerResources{ 686 CpuPeriod: 42, 687 }, 688 }, 689 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 690 c.EXPECT().UpdateContainerResources( 691 gomock.Any(), 692 gomock.Eq(&criruntimev1alpha2.UpdateContainerResourcesRequest{ 693 ContainerId: "containerID", 694 Linux: &criruntimev1alpha2.LinuxContainerResources{ 695 CpuPeriod: 42, 696 }, 697 }), 698 ).Times(1).Return(nil, errMock) 699 }, 700 wantErr: true, 701 }, 702 { 703 name: "success", 704 args: args{ 705 containerID: "containerID", 706 resources: &criruntimev1alpha2.LinuxContainerResources{ 707 CpuPeriod: 42, 708 }, 709 }, 710 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 711 c.EXPECT().UpdateContainerResources( 712 gomock.Any(), 713 gomock.Eq(&criruntimev1alpha2.UpdateContainerResourcesRequest{ 714 ContainerId: "containerID", 715 Linux: &criruntimev1alpha2.LinuxContainerResources{ 716 CpuPeriod: 42, 717 }, 718 }), 719 ).Times(1).Return(&criruntimev1alpha2.UpdateContainerResourcesResponse{}, nil) 720 }, 721 wantErr: false, 722 }, 723 } 724 for _, tt := range tests { 725 t.Run(tt.name, func(t *testing.T) { 726 // the same in every test 727 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 728 defer cancel() 729 defer ctrl.Finish() 730 if tt.prepare != nil { 731 tt.prepare(t, client) 732 } 733 if err := w.UpdateContainerResources(tt.args.containerID, tt.args.resources); (err != nil) != tt.wantErr { 734 t.Errorf("extendedServiceRuntimeWrapper.UpdateContainerResources() error = %v, wantErr %v", err, tt.wantErr) 735 } 736 }) 737 } 738 } 739 740 func Test_extendedServiceRuntimeWrapper_ExecSync(t *testing.T) { 741 742 type args struct { 743 containerID string 744 cmd []string 745 timeout time.Duration 746 } 747 tests := []struct { 748 name string 749 prepare prepareFunc 750 args args 751 wantStdout []byte 752 wantStderr []byte 753 wantErr bool 754 }{ 755 { 756 name: "error", 757 args: args{ 758 containerID: "containerID", 759 cmd: []string{"/bin/bash", "-c", "echo hello world"}, 760 timeout: time.Minute * 1, 761 }, 762 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 763 c.EXPECT().ExecSync( 764 gomock.Any(), 765 gomock.Eq(&criruntimev1alpha2.ExecSyncRequest{ 766 ContainerId: "containerID", 767 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 768 Timeout: int64((time.Minute * 1).Seconds()), 769 }), 770 ).Times(1).Return(nil, errMock) 771 }, 772 wantStdout: nil, 773 wantStderr: nil, 774 wantErr: true, 775 }, 776 { 777 name: "command execution failed", 778 args: args{ 779 containerID: "containerID", 780 cmd: []string{"/bin/bash", "-c", "echo hello world"}, 781 timeout: time.Minute * 1, 782 }, 783 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 784 c.EXPECT().ExecSync( 785 gomock.Any(), 786 gomock.Eq(&criruntimev1alpha2.ExecSyncRequest{ 787 ContainerId: "containerID", 788 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 789 Timeout: int64((time.Minute * 1).Seconds()), 790 }), 791 ).Times(1).Return(&criruntimev1alpha2.ExecSyncResponse{ 792 Stdout: []byte("stdout"), 793 Stderr: []byte("stderr"), 794 ExitCode: 42, 795 }, nil) 796 }, 797 wantStdout: []byte("stdout"), 798 wantStderr: []byte("stderr"), 799 wantErr: true, 800 }, 801 { 802 name: "success", 803 args: args{ 804 containerID: "containerID", 805 cmd: []string{"/bin/bash", "-c", "echo hello world"}, 806 timeout: time.Minute * 1, 807 }, 808 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 809 c.EXPECT().ExecSync( 810 gomock.Any(), 811 gomock.Eq(&criruntimev1alpha2.ExecSyncRequest{ 812 ContainerId: "containerID", 813 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 814 Timeout: int64((time.Minute * 1).Seconds()), 815 }), 816 ).Times(1).Return(&criruntimev1alpha2.ExecSyncResponse{ 817 Stdout: []byte("stdout"), 818 Stderr: []byte("stderr"), 819 ExitCode: 0, 820 }, nil) 821 }, 822 wantStdout: []byte("stdout"), 823 wantStderr: []byte("stderr"), 824 wantErr: false, 825 }, 826 } 827 for _, tt := range tests { 828 t.Run(tt.name, func(t *testing.T) { 829 // the same in every test 830 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 831 defer cancel() 832 defer ctrl.Finish() 833 if tt.prepare != nil { 834 tt.prepare(t, client) 835 } 836 gotStdout, gotStderr, err := w.ExecSync(tt.args.containerID, tt.args.cmd, tt.args.timeout) 837 if (err != nil) != tt.wantErr { 838 t.Errorf("extendedServiceRuntimeWrapper.ExecSync() error = %v, wantErr %v", err, tt.wantErr) 839 return 840 } 841 if !reflect.DeepEqual(gotStdout, tt.wantStdout) { 842 t.Errorf("extendedServiceRuntimeWrapper.ExecSync() gotStdout = %v, want %v", gotStdout, tt.wantStdout) 843 } 844 if !reflect.DeepEqual(gotStderr, tt.wantStderr) { 845 t.Errorf("extendedServiceRuntimeWrapper.ExecSync() gotStderr = %v, want %v", gotStderr, tt.wantStderr) 846 } 847 }) 848 } 849 } 850 851 func Test_extendedServiceRuntimeWrapper_Exec(t *testing.T) { 852 type args struct { 853 req *criruntimev1alpha2.ExecRequest 854 } 855 tests := []struct { 856 name string 857 prepare prepareFunc 858 args args 859 want *criruntimev1alpha2.ExecResponse 860 wantErr bool 861 }{ 862 { 863 name: "error", 864 args: args{ 865 req: &criruntimev1alpha2.ExecRequest{ 866 ContainerId: "containerID", 867 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 868 }, 869 }, 870 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 871 c.EXPECT().Exec( 872 gomock.Any(), 873 gomock.Eq(&criruntimev1alpha2.ExecRequest{ 874 ContainerId: "containerID", 875 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 876 }), 877 ).Times(1).Return(nil, errMock) 878 }, 879 want: nil, 880 wantErr: true, 881 }, 882 { 883 name: "success", 884 args: args{ 885 req: &criruntimev1alpha2.ExecRequest{ 886 ContainerId: "containerID", 887 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 888 }, 889 }, 890 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 891 c.EXPECT().Exec( 892 gomock.Any(), 893 gomock.Eq(&criruntimev1alpha2.ExecRequest{ 894 ContainerId: "containerID", 895 Cmd: []string{"/bin/bash", "-c", "echo hello world"}, 896 }), 897 ).Times(1).Return(&criruntimev1alpha2.ExecResponse{ 898 Url: "pick up status of exec request here", 899 }, nil) 900 }, 901 want: &criruntimev1alpha2.ExecResponse{ 902 Url: "pick up status of exec request here", 903 }, 904 wantErr: false, 905 }, 906 } 907 for _, tt := range tests { 908 t.Run(tt.name, func(t *testing.T) { 909 // the same in every test 910 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 911 defer cancel() 912 defer ctrl.Finish() 913 if tt.prepare != nil { 914 tt.prepare(t, client) 915 } 916 got, err := w.Exec(tt.args.req) 917 if (err != nil) != tt.wantErr { 918 t.Errorf("extendedServiceRuntimeWrapper.Exec() error = %v, wantErr %v", err, tt.wantErr) 919 return 920 } 921 if !reflect.DeepEqual(got, tt.want) { 922 t.Errorf("extendedServiceRuntimeWrapper.Exec() = %v, want %v", got, tt.want) 923 } 924 }) 925 } 926 } 927 928 func Test_extendedServiceRuntimeWrapper_Attach(t *testing.T) { 929 type args struct { 930 req *criruntimev1alpha2.AttachRequest 931 } 932 tests := []struct { 933 name string 934 prepare prepareFunc 935 args args 936 want *criruntimev1alpha2.AttachResponse 937 wantErr bool 938 }{ 939 { 940 name: "error", 941 args: args{ 942 req: &criruntimev1alpha2.AttachRequest{ 943 ContainerId: "containerID", 944 Stdout: true, 945 }, 946 }, 947 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 948 c.EXPECT().Attach( 949 gomock.Any(), 950 gomock.Eq(&criruntimev1alpha2.AttachRequest{ 951 ContainerId: "containerID", 952 Stdout: true, 953 }), 954 ).Times(1).Return(nil, errMock) 955 }, 956 want: nil, 957 wantErr: true, 958 }, 959 { 960 name: "success", 961 args: args{ 962 req: &criruntimev1alpha2.AttachRequest{ 963 ContainerId: "containerID", 964 Stdout: true, 965 }, 966 }, 967 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 968 c.EXPECT().Attach( 969 gomock.Any(), 970 gomock.Eq(&criruntimev1alpha2.AttachRequest{ 971 ContainerId: "containerID", 972 Stdout: true, 973 }), 974 ).Times(1).Return(&criruntimev1alpha2.AttachResponse{ 975 Url: "pick up status of attach request here", 976 }, nil) 977 }, 978 want: &criruntimev1alpha2.AttachResponse{ 979 Url: "pick up status of attach request here", 980 }, 981 wantErr: false, 982 }, 983 } 984 for _, tt := range tests { 985 t.Run(tt.name, func(t *testing.T) { 986 // the same in every test 987 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 988 defer cancel() 989 defer ctrl.Finish() 990 if tt.prepare != nil { 991 tt.prepare(t, client) 992 } 993 got, err := w.Attach(tt.args.req) 994 if (err != nil) != tt.wantErr { 995 t.Errorf("extendedServiceRuntimeWrapper.Attach() error = %v, wantErr %v", err, tt.wantErr) 996 return 997 } 998 if !reflect.DeepEqual(got, tt.want) { 999 t.Errorf("extendedServiceRuntimeWrapper.Attach() = %v, want %v", got, tt.want) 1000 } 1001 }) 1002 } 1003 } 1004 1005 func Test_extendedServiceRuntimeWrapper_ReopenContainerLog(t *testing.T) { 1006 type args struct { 1007 containerID string 1008 } 1009 tests := []struct { 1010 name string 1011 prepare prepareFunc 1012 args args 1013 wantErr bool 1014 }{ 1015 { 1016 name: "error", 1017 args: args{ 1018 containerID: "containerID", 1019 }, 1020 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1021 c.EXPECT().ReopenContainerLog( 1022 gomock.Any(), 1023 gomock.Eq(&criruntimev1alpha2.ReopenContainerLogRequest{ 1024 ContainerId: "containerID", 1025 }), 1026 ).Times(1).Return(nil, errMock) 1027 }, 1028 wantErr: true, 1029 }, 1030 { 1031 name: "success", 1032 args: args{ 1033 containerID: "containerID", 1034 }, 1035 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1036 c.EXPECT().ReopenContainerLog( 1037 gomock.Any(), 1038 gomock.Eq(&criruntimev1alpha2.ReopenContainerLogRequest{ 1039 ContainerId: "containerID", 1040 }), 1041 ).Times(1).Return(&criruntimev1alpha2.ReopenContainerLogResponse{}, nil) 1042 }, 1043 wantErr: false, 1044 }, 1045 } 1046 for _, tt := range tests { 1047 t.Run(tt.name, func(t *testing.T) { 1048 // the same in every test 1049 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1050 defer cancel() 1051 defer ctrl.Finish() 1052 if tt.prepare != nil { 1053 tt.prepare(t, client) 1054 } 1055 if err := w.ReopenContainerLog(tt.args.containerID); (err != nil) != tt.wantErr { 1056 t.Errorf("extendedServiceRuntimeWrapper.ReopenContainerLog() error = %v, wantErr %v", err, tt.wantErr) 1057 } 1058 }) 1059 } 1060 } 1061 1062 func Test_extendedServiceRuntimeWrapper_RunPodSandbox(t *testing.T) { 1063 type args struct { 1064 config *criruntimev1alpha2.PodSandboxConfig 1065 runtimeHandler string 1066 } 1067 tests := []struct { 1068 name string 1069 prepare prepareFunc 1070 args args 1071 want string 1072 wantErr bool 1073 }{ 1074 { 1075 name: "error", 1076 args: args{ 1077 config: &criruntimev1alpha2.PodSandboxConfig{ 1078 Metadata: &criruntimev1alpha2.PodSandboxMetadata{ 1079 Name: "pod-name", 1080 Namespace: "default", 1081 Uid: "b924b248-6395-4415-8603-4f3562e44418", 1082 Attempt: 0, 1083 }, 1084 Hostname: "sandboxHostname", 1085 }, 1086 runtimeHandler: "runtimeHandler", 1087 }, 1088 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1089 c.EXPECT().RunPodSandbox( 1090 gomock.Any(), 1091 gomock.Eq(&criruntimev1alpha2.RunPodSandboxRequest{ 1092 RuntimeHandler: "runtimeHandler", 1093 Config: &criruntimev1alpha2.PodSandboxConfig{ 1094 Metadata: &criruntimev1alpha2.PodSandboxMetadata{ 1095 Name: "pod-name", 1096 Namespace: "default", 1097 Uid: "b924b248-6395-4415-8603-4f3562e44418", 1098 Attempt: 0, 1099 }, 1100 Hostname: "sandboxHostname", 1101 }, 1102 }), 1103 ).Times(1).Return(nil, errMock) 1104 }, 1105 want: "", 1106 wantErr: true, 1107 }, 1108 { 1109 name: "success", 1110 args: args{ 1111 config: &criruntimev1alpha2.PodSandboxConfig{ 1112 Metadata: &criruntimev1alpha2.PodSandboxMetadata{ 1113 Name: "pod-name", 1114 Namespace: "default", 1115 Uid: "b924b248-6395-4415-8603-4f3562e44418", 1116 Attempt: 0, 1117 }, 1118 Hostname: "sandboxHostname", 1119 }, 1120 runtimeHandler: "runtimeHandler", 1121 }, 1122 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1123 c.EXPECT().RunPodSandbox( 1124 gomock.Any(), 1125 gomock.Eq(&criruntimev1alpha2.RunPodSandboxRequest{ 1126 RuntimeHandler: "runtimeHandler", 1127 Config: &criruntimev1alpha2.PodSandboxConfig{ 1128 Metadata: &criruntimev1alpha2.PodSandboxMetadata{ 1129 Name: "pod-name", 1130 Namespace: "default", 1131 Uid: "b924b248-6395-4415-8603-4f3562e44418", 1132 Attempt: 0, 1133 }, 1134 Hostname: "sandboxHostname", 1135 }, 1136 }), 1137 ).Times(1).Return(&criruntimev1alpha2.RunPodSandboxResponse{ 1138 PodSandboxId: "podSandboxID", 1139 }, nil) 1140 }, 1141 want: "podSandboxID", 1142 wantErr: false, 1143 }, 1144 } 1145 for _, tt := range tests { 1146 t.Run(tt.name, func(t *testing.T) { 1147 // the same in every test 1148 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1149 defer cancel() 1150 defer ctrl.Finish() 1151 if tt.prepare != nil { 1152 tt.prepare(t, client) 1153 } 1154 got, err := w.RunPodSandbox(tt.args.config, tt.args.runtimeHandler) 1155 if (err != nil) != tt.wantErr { 1156 t.Errorf("extendedServiceRuntimeWrapper.RunPodSandbox() error = %v, wantErr %v", err, tt.wantErr) 1157 return 1158 } 1159 if got != tt.want { 1160 t.Errorf("extendedServiceRuntimeWrapper.RunPodSandbox() = %v, want %v", got, tt.want) 1161 } 1162 }) 1163 } 1164 } 1165 1166 func Test_extendedServiceRuntimeWrapper_StopPodSandbox(t *testing.T) { 1167 type args struct { 1168 podSandboxID string 1169 } 1170 tests := []struct { 1171 name string 1172 prepare prepareFunc 1173 args args 1174 wantErr bool 1175 }{ 1176 { 1177 name: "error", 1178 args: args{ 1179 podSandboxID: "podSandboxID", 1180 }, 1181 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1182 c.EXPECT().StopPodSandbox( 1183 gomock.Any(), 1184 gomock.Eq(&criruntimev1alpha2.StopPodSandboxRequest{ 1185 PodSandboxId: "podSandboxID", 1186 }), 1187 ).Times(1).Return(nil, errMock) 1188 }, 1189 wantErr: true, 1190 }, 1191 { 1192 name: "success", 1193 args: args{ 1194 podSandboxID: "podSandboxID", 1195 }, 1196 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1197 c.EXPECT().StopPodSandbox( 1198 gomock.Any(), 1199 gomock.Eq(&criruntimev1alpha2.StopPodSandboxRequest{ 1200 PodSandboxId: "podSandboxID", 1201 }), 1202 ).Times(1).Return(&criruntimev1alpha2.StopPodSandboxResponse{}, nil) 1203 }, 1204 wantErr: false, 1205 }, 1206 } 1207 for _, tt := range tests { 1208 t.Run(tt.name, func(t *testing.T) { 1209 // the same in every test 1210 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1211 defer cancel() 1212 defer ctrl.Finish() 1213 if tt.prepare != nil { 1214 tt.prepare(t, client) 1215 } 1216 if err := w.StopPodSandbox(tt.args.podSandboxID); (err != nil) != tt.wantErr { 1217 t.Errorf("extendedServiceRuntimeWrapper.StopPodSandbox() error = %v, wantErr %v", err, tt.wantErr) 1218 } 1219 }) 1220 } 1221 } 1222 1223 func Test_extendedServiceRuntimeWrapper_RemovePodSandbox(t *testing.T) { 1224 type args struct { 1225 podSandboxID string 1226 } 1227 tests := []struct { 1228 name string 1229 prepare prepareFunc 1230 args args 1231 wantErr bool 1232 }{ 1233 { 1234 name: "error", 1235 args: args{ 1236 podSandboxID: "podSandboxID", 1237 }, 1238 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1239 c.EXPECT().RemovePodSandbox( 1240 gomock.Any(), 1241 gomock.Eq(&criruntimev1alpha2.RemovePodSandboxRequest{ 1242 PodSandboxId: "podSandboxID", 1243 }), 1244 ).Times(1).Return(nil, errMock) 1245 }, 1246 wantErr: true, 1247 }, 1248 { 1249 name: "error", 1250 args: args{ 1251 podSandboxID: "podSandboxID", 1252 }, 1253 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1254 c.EXPECT().RemovePodSandbox( 1255 gomock.Any(), 1256 gomock.Eq(&criruntimev1alpha2.RemovePodSandboxRequest{ 1257 PodSandboxId: "podSandboxID", 1258 }), 1259 ).Times(1).Return(&criruntimev1alpha2.RemovePodSandboxResponse{}, nil) 1260 }, 1261 wantErr: false, 1262 }, 1263 } 1264 for _, tt := range tests { 1265 t.Run(tt.name, func(t *testing.T) { 1266 // the same in every test 1267 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1268 defer cancel() 1269 defer ctrl.Finish() 1270 if tt.prepare != nil { 1271 tt.prepare(t, client) 1272 } 1273 if err := w.RemovePodSandbox(tt.args.podSandboxID); (err != nil) != tt.wantErr { 1274 t.Errorf("extendedServiceRuntimeWrapper.RemovePodSandbox() error = %v, wantErr %v", err, tt.wantErr) 1275 } 1276 }) 1277 } 1278 } 1279 1280 func Test_extendedServiceRuntimeWrapper_PodSandboxStatus(t *testing.T) { 1281 type args struct { 1282 podSandboxID string 1283 } 1284 tests := []struct { 1285 name string 1286 prepare prepareFunc 1287 args args 1288 want *criruntimev1alpha2.PodSandboxStatus 1289 wantErr bool 1290 }{ 1291 { 1292 name: "error", 1293 args: args{ 1294 podSandboxID: "podSandboxID", 1295 }, 1296 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1297 c.EXPECT().PodSandboxStatus( 1298 gomock.Any(), 1299 gomock.Eq(&criruntimev1alpha2.PodSandboxStatusRequest{ 1300 PodSandboxId: "podSandboxID", 1301 Verbose: false, 1302 }), 1303 ).Times(1).Return(nil, errMock) 1304 }, 1305 want: nil, 1306 wantErr: true, 1307 }, 1308 { 1309 name: "success", 1310 args: args{ 1311 podSandboxID: "podSandboxID", 1312 }, 1313 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1314 c.EXPECT().PodSandboxStatus( 1315 gomock.Any(), 1316 gomock.Eq(&criruntimev1alpha2.PodSandboxStatusRequest{ 1317 PodSandboxId: "podSandboxID", 1318 Verbose: false, 1319 }), 1320 ).Times(1).Return(&criruntimev1alpha2.PodSandboxStatusResponse{ 1321 Status: &criruntimev1alpha2.PodSandboxStatus{ 1322 Id: "podSandboxID", 1323 State: criruntimev1alpha2.PodSandboxState_SANDBOX_READY, 1324 }, 1325 }, nil) 1326 }, 1327 want: &criruntimev1alpha2.PodSandboxStatus{ 1328 Id: "podSandboxID", 1329 State: criruntimev1alpha2.PodSandboxState_SANDBOX_READY, 1330 }, 1331 wantErr: false, 1332 }, 1333 } 1334 for _, tt := range tests { 1335 t.Run(tt.name, func(t *testing.T) { 1336 // the same in every test 1337 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1338 defer cancel() 1339 defer ctrl.Finish() 1340 if tt.prepare != nil { 1341 tt.prepare(t, client) 1342 } 1343 got, err := w.PodSandboxStatus(tt.args.podSandboxID) 1344 if (err != nil) != tt.wantErr { 1345 t.Errorf("extendedServiceRuntimeWrapper.PodSandboxStatus() error = %v, wantErr %v", err, tt.wantErr) 1346 return 1347 } 1348 if !reflect.DeepEqual(got, tt.want) { 1349 t.Errorf("extendedServiceRuntimeWrapper.PodSandboxStatus() = %v, want %v", got, tt.want) 1350 } 1351 }) 1352 } 1353 } 1354 1355 func Test_extendedServiceRuntimeWrapper_PodSandboxStatusVerbose(t *testing.T) { 1356 1357 type args struct { 1358 podSandboxID string 1359 } 1360 tests := []struct { 1361 name string 1362 prepare prepareFunc 1363 args args 1364 want *criruntimev1alpha2.PodSandboxStatus 1365 want1 map[string]string 1366 wantErr bool 1367 }{ 1368 { 1369 name: "error", 1370 args: args{ 1371 podSandboxID: "podSandboxID", 1372 }, 1373 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1374 c.EXPECT().PodSandboxStatus( 1375 gomock.Any(), 1376 gomock.Eq(&criruntimev1alpha2.PodSandboxStatusRequest{ 1377 PodSandboxId: "podSandboxID", 1378 Verbose: true, 1379 }), 1380 ).Times(1).Return(nil, errMock) 1381 }, 1382 want: nil, 1383 wantErr: true, 1384 }, 1385 { 1386 name: "success", 1387 args: args{ 1388 podSandboxID: "podSandboxID", 1389 }, 1390 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1391 c.EXPECT().PodSandboxStatus( 1392 gomock.Any(), 1393 gomock.Eq(&criruntimev1alpha2.PodSandboxStatusRequest{ 1394 PodSandboxId: "podSandboxID", 1395 Verbose: true, 1396 }), 1397 ).Times(1).Return(&criruntimev1alpha2.PodSandboxStatusResponse{ 1398 Status: &criruntimev1alpha2.PodSandboxStatus{ 1399 Id: "podSandboxID", 1400 State: criruntimev1alpha2.PodSandboxState_SANDBOX_READY, 1401 }, 1402 }, nil) 1403 }, 1404 want: &criruntimev1alpha2.PodSandboxStatus{ 1405 Id: "podSandboxID", 1406 State: criruntimev1alpha2.PodSandboxState_SANDBOX_READY, 1407 }, 1408 wantErr: false, 1409 }, 1410 } 1411 for _, tt := range tests { 1412 t.Run(tt.name, func(t *testing.T) { 1413 // the same in every test 1414 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1415 defer cancel() 1416 defer ctrl.Finish() 1417 if tt.prepare != nil { 1418 tt.prepare(t, client) 1419 } 1420 got, got1, err := w.PodSandboxStatusVerbose(tt.args.podSandboxID) 1421 if (err != nil) != tt.wantErr { 1422 t.Errorf("extendedServiceRuntimeWrapper.PodSandboxStatusVerbose() error = %v, wantErr %v", err, tt.wantErr) 1423 return 1424 } 1425 if !reflect.DeepEqual(got, tt.want) { 1426 t.Errorf("extendedServiceRuntimeWrapper.PodSandboxStatusVerbose() got = %v, want %v", got, tt.want) 1427 } 1428 if !reflect.DeepEqual(got1, tt.want1) { 1429 t.Errorf("extendedServiceRuntimeWrapper.PodSandboxStatusVerbose() got1 = %v, want %v", got1, tt.want1) 1430 } 1431 }) 1432 } 1433 } 1434 1435 func Test_extendedServiceRuntimeWrapper_ListPodSandbox(t *testing.T) { 1436 type args struct { 1437 filter *criruntimev1alpha2.PodSandboxFilter 1438 } 1439 tests := []struct { 1440 name string 1441 prepare prepareFunc 1442 args args 1443 want []*criruntimev1alpha2.PodSandbox 1444 wantErr bool 1445 }{ 1446 { 1447 name: "error", 1448 args: args{ 1449 filter: &criruntimev1alpha2.PodSandboxFilter{ 1450 LabelSelector: map[string]string{ 1451 "a": "b", 1452 }, 1453 }, 1454 }, 1455 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1456 c.EXPECT().ListPodSandbox( 1457 gomock.Any(), 1458 gomock.Eq(&criruntimev1alpha2.ListPodSandboxRequest{ 1459 Filter: &criruntimev1alpha2.PodSandboxFilter{ 1460 LabelSelector: map[string]string{ 1461 "a": "b", 1462 }, 1463 }, 1464 }), 1465 ).Times(1).Return(nil, errMock) 1466 }, 1467 want: nil, 1468 wantErr: true, 1469 }, 1470 { 1471 name: "success", 1472 args: args{ 1473 filter: &criruntimev1alpha2.PodSandboxFilter{ 1474 LabelSelector: map[string]string{ 1475 "a": "b", 1476 }, 1477 }, 1478 }, 1479 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1480 c.EXPECT().ListPodSandbox( 1481 gomock.Any(), 1482 gomock.Eq(&criruntimev1alpha2.ListPodSandboxRequest{ 1483 Filter: &criruntimev1alpha2.PodSandboxFilter{ 1484 LabelSelector: map[string]string{ 1485 "a": "b", 1486 }, 1487 }, 1488 }), 1489 ).Times(1).Return(&criruntimev1alpha2.ListPodSandboxResponse{ 1490 Items: []*criruntimev1alpha2.PodSandbox{ 1491 { 1492 Id: "one", 1493 State: criruntimev1alpha2.PodSandboxState_SANDBOX_READY, 1494 }, 1495 { 1496 Id: "two", 1497 State: criruntimev1alpha2.PodSandboxState_SANDBOX_NOTREADY, 1498 }, 1499 }, 1500 }, nil) 1501 }, 1502 want: []*criruntimev1alpha2.PodSandbox{ 1503 { 1504 Id: "one", 1505 State: criruntimev1alpha2.PodSandboxState_SANDBOX_READY, 1506 }, 1507 { 1508 Id: "two", 1509 State: criruntimev1alpha2.PodSandboxState_SANDBOX_NOTREADY, 1510 }, 1511 }, 1512 wantErr: false, 1513 }, 1514 } 1515 for _, tt := range tests { 1516 t.Run(tt.name, func(t *testing.T) { 1517 // the same in every test 1518 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1519 defer cancel() 1520 defer ctrl.Finish() 1521 if tt.prepare != nil { 1522 tt.prepare(t, client) 1523 } 1524 got, err := w.ListPodSandbox(tt.args.filter) 1525 if (err != nil) != tt.wantErr { 1526 t.Errorf("extendedServiceRuntimeWrapper.ListPodSandbox() error = %v, wantErr %v", err, tt.wantErr) 1527 return 1528 } 1529 if !reflect.DeepEqual(got, tt.want) { 1530 t.Errorf("extendedServiceRuntimeWrapper.ListPodSandbox() = %v, want %v", got, tt.want) 1531 } 1532 }) 1533 } 1534 } 1535 1536 func Test_extendedServiceRuntimeWrapper_PortForward(t *testing.T) { 1537 type args struct { 1538 req *criruntimev1alpha2.PortForwardRequest 1539 } 1540 tests := []struct { 1541 name string 1542 prepare prepareFunc 1543 args args 1544 want *criruntimev1alpha2.PortForwardResponse 1545 wantErr bool 1546 }{ 1547 { 1548 name: "error", 1549 args: args{ 1550 req: &criruntimev1alpha2.PortForwardRequest{ 1551 PodSandboxId: "podSandboxID", 1552 Port: []int32{42}, 1553 }, 1554 }, 1555 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1556 c.EXPECT().PortForward( 1557 gomock.Any(), 1558 gomock.Eq(&criruntimev1alpha2.PortForwardRequest{ 1559 PodSandboxId: "podSandboxID", 1560 Port: []int32{42}, 1561 }), 1562 ).Times(1).Return(nil, errMock) 1563 }, 1564 want: nil, 1565 wantErr: true, 1566 }, 1567 { 1568 name: "success", 1569 args: args{ 1570 req: &criruntimev1alpha2.PortForwardRequest{ 1571 PodSandboxId: "podSandboxID", 1572 Port: []int32{42}, 1573 }, 1574 }, 1575 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1576 c.EXPECT().PortForward( 1577 gomock.Any(), 1578 gomock.Eq(&criruntimev1alpha2.PortForwardRequest{ 1579 PodSandboxId: "podSandboxID", 1580 Port: []int32{42}, 1581 }), 1582 ).Times(1).Return(&criruntimev1alpha2.PortForwardResponse{ 1583 Url: "pick up port forward request here", 1584 }, nil) 1585 }, 1586 want: &criruntimev1alpha2.PortForwardResponse{ 1587 Url: "pick up port forward request here", 1588 }, 1589 wantErr: false, 1590 }, 1591 } 1592 for _, tt := range tests { 1593 t.Run(tt.name, func(t *testing.T) { 1594 // the same in every test 1595 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1596 defer cancel() 1597 defer ctrl.Finish() 1598 if tt.prepare != nil { 1599 tt.prepare(t, client) 1600 } 1601 got, err := w.PortForward(tt.args.req) 1602 if (err != nil) != tt.wantErr { 1603 t.Errorf("extendedServiceRuntimeWrapper.PortForward() error = %v, wantErr %v", err, tt.wantErr) 1604 return 1605 } 1606 if !reflect.DeepEqual(got, tt.want) { 1607 t.Errorf("extendedServiceRuntimeWrapper.PortForward() = %v, want %v", got, tt.want) 1608 } 1609 }) 1610 } 1611 } 1612 1613 func Test_extendedServiceRuntimeWrapper_ContainerStats(t *testing.T) { 1614 type args struct { 1615 containerID string 1616 } 1617 tests := []struct { 1618 name string 1619 prepare prepareFunc 1620 args args 1621 want *criruntimev1alpha2.ContainerStats 1622 wantErr bool 1623 }{ 1624 { 1625 name: "error", 1626 args: args{ 1627 containerID: "containerID", 1628 }, 1629 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1630 c.EXPECT().ContainerStats( 1631 gomock.Any(), 1632 gomock.Eq(&criruntimev1alpha2.ContainerStatsRequest{ 1633 ContainerId: "containerID", 1634 }), 1635 ).Times(1).Return(nil, errMock) 1636 }, 1637 want: nil, 1638 wantErr: true, 1639 }, 1640 { 1641 name: "success", 1642 args: args{ 1643 containerID: "containerID", 1644 }, 1645 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1646 c.EXPECT().ContainerStats( 1647 gomock.Any(), 1648 gomock.Eq(&criruntimev1alpha2.ContainerStatsRequest{ 1649 ContainerId: "containerID", 1650 }), 1651 ).Times(1).Return(&criruntimev1alpha2.ContainerStatsResponse{ 1652 Stats: &criruntimev1alpha2.ContainerStats{ 1653 Attributes: &criruntimev1alpha2.ContainerAttributes{ 1654 Id: "containerID", 1655 }, 1656 }, 1657 }, nil) 1658 }, 1659 want: &criruntimev1alpha2.ContainerStats{ 1660 Attributes: &criruntimev1alpha2.ContainerAttributes{ 1661 Id: "containerID", 1662 }, 1663 }, 1664 wantErr: false, 1665 }, 1666 } 1667 for _, tt := range tests { 1668 t.Run(tt.name, func(t *testing.T) { 1669 // the same in every test 1670 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1671 defer cancel() 1672 defer ctrl.Finish() 1673 if tt.prepare != nil { 1674 tt.prepare(t, client) 1675 } 1676 got, err := w.ContainerStats(tt.args.containerID) 1677 if (err != nil) != tt.wantErr { 1678 t.Errorf("extendedServiceRuntimeWrapper.ContainerStats() error = %v, wantErr %v", err, tt.wantErr) 1679 return 1680 } 1681 if !reflect.DeepEqual(got, tt.want) { 1682 t.Errorf("extendedServiceRuntimeWrapper.ContainerStats() = %v, want %v", got, tt.want) 1683 } 1684 }) 1685 } 1686 } 1687 1688 func Test_extendedServiceRuntimeWrapper_ListContainerStats(t *testing.T) { 1689 type args struct { 1690 filter *criruntimev1alpha2.ContainerStatsFilter 1691 } 1692 tests := []struct { 1693 name string 1694 prepare prepareFunc 1695 args args 1696 want []*criruntimev1alpha2.ContainerStats 1697 wantErr bool 1698 }{ 1699 { 1700 name: "error", 1701 args: args{ 1702 filter: &criruntimev1alpha2.ContainerStatsFilter{ 1703 LabelSelector: map[string]string{ 1704 "a": "b", 1705 }, 1706 }, 1707 }, 1708 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1709 c.EXPECT().ListContainerStats( 1710 gomock.Any(), 1711 gomock.Eq(&criruntimev1alpha2.ListContainerStatsRequest{ 1712 Filter: &criruntimev1alpha2.ContainerStatsFilter{ 1713 LabelSelector: map[string]string{ 1714 "a": "b", 1715 }, 1716 }, 1717 }), 1718 ).Times(1).Return(nil, errMock) 1719 }, 1720 want: nil, 1721 wantErr: true, 1722 }, 1723 { 1724 name: "error", 1725 args: args{ 1726 filter: &criruntimev1alpha2.ContainerStatsFilter{ 1727 LabelSelector: map[string]string{ 1728 "a": "b", 1729 }, 1730 }, 1731 }, 1732 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1733 c.EXPECT().ListContainerStats( 1734 gomock.Any(), 1735 gomock.Eq(&criruntimev1alpha2.ListContainerStatsRequest{ 1736 Filter: &criruntimev1alpha2.ContainerStatsFilter{ 1737 LabelSelector: map[string]string{ 1738 "a": "b", 1739 }, 1740 }, 1741 }), 1742 ).Times(1).Return(&criruntimev1alpha2.ListContainerStatsResponse{ 1743 Stats: []*criruntimev1alpha2.ContainerStats{ 1744 { 1745 Attributes: &criruntimev1alpha2.ContainerAttributes{ 1746 Id: "one", 1747 Labels: map[string]string{ 1748 "a": "b", 1749 }, 1750 }, 1751 }, 1752 { 1753 Attributes: &criruntimev1alpha2.ContainerAttributes{ 1754 Id: "two", 1755 Labels: map[string]string{ 1756 "a": "b", 1757 }, 1758 }, 1759 }, 1760 }, 1761 }, nil) 1762 }, 1763 want: []*criruntimev1alpha2.ContainerStats{ 1764 { 1765 Attributes: &criruntimev1alpha2.ContainerAttributes{ 1766 Id: "one", 1767 Labels: map[string]string{ 1768 "a": "b", 1769 }, 1770 }, 1771 }, 1772 { 1773 Attributes: &criruntimev1alpha2.ContainerAttributes{ 1774 Id: "two", 1775 Labels: map[string]string{ 1776 "a": "b", 1777 }, 1778 }, 1779 }, 1780 }, 1781 wantErr: false, 1782 }, 1783 } 1784 for _, tt := range tests { 1785 t.Run(tt.name, func(t *testing.T) { 1786 // the same in every test 1787 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1788 defer cancel() 1789 defer ctrl.Finish() 1790 if tt.prepare != nil { 1791 tt.prepare(t, client) 1792 } 1793 got, err := w.ListContainerStats(tt.args.filter) 1794 if (err != nil) != tt.wantErr { 1795 t.Errorf("extendedServiceRuntimeWrapper.ListContainerStats() error = %v, wantErr %v", err, tt.wantErr) 1796 return 1797 } 1798 if !reflect.DeepEqual(got, tt.want) { 1799 t.Errorf("extendedServiceRuntimeWrapper.ListContainerStats() = %v, want %v", got, tt.want) 1800 } 1801 }) 1802 } 1803 } 1804 1805 func Test_extendedServiceRuntimeWrapper_UpdateRuntimeConfig(t *testing.T) { 1806 type args struct { 1807 runtimeConfig *criruntimev1alpha2.RuntimeConfig 1808 } 1809 tests := []struct { 1810 name string 1811 prepare prepareFunc 1812 args args 1813 wantErr bool 1814 }{ 1815 { 1816 name: "error", 1817 args: args{ 1818 runtimeConfig: &criruntimev1alpha2.RuntimeConfig{ 1819 NetworkConfig: &criruntimev1alpha2.NetworkConfig{ 1820 PodCidr: "10.10.10.0/24", 1821 }, 1822 }, 1823 }, 1824 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1825 c.EXPECT().UpdateRuntimeConfig( 1826 gomock.Any(), 1827 gomock.Eq(&criruntimev1alpha2.UpdateRuntimeConfigRequest{ 1828 RuntimeConfig: &criruntimev1alpha2.RuntimeConfig{ 1829 NetworkConfig: &criruntimev1alpha2.NetworkConfig{ 1830 PodCidr: "10.10.10.0/24", 1831 }, 1832 }, 1833 }), 1834 ).Times(1).Return(nil, errMock) 1835 }, 1836 wantErr: true, 1837 }, 1838 { 1839 name: "success", 1840 args: args{ 1841 runtimeConfig: &criruntimev1alpha2.RuntimeConfig{ 1842 NetworkConfig: &criruntimev1alpha2.NetworkConfig{ 1843 PodCidr: "10.10.10.0/24", 1844 }, 1845 }, 1846 }, 1847 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1848 c.EXPECT().UpdateRuntimeConfig( 1849 gomock.Any(), 1850 gomock.Eq(&criruntimev1alpha2.UpdateRuntimeConfigRequest{ 1851 RuntimeConfig: &criruntimev1alpha2.RuntimeConfig{ 1852 NetworkConfig: &criruntimev1alpha2.NetworkConfig{ 1853 PodCidr: "10.10.10.0/24", 1854 }, 1855 }, 1856 }), 1857 ).Times(1).Return(&criruntimev1alpha2.UpdateRuntimeConfigResponse{}, nil) 1858 }, 1859 wantErr: false, 1860 }, 1861 } 1862 for _, tt := range tests { 1863 t.Run(tt.name, func(t *testing.T) { 1864 // the same in every test 1865 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1866 defer cancel() 1867 defer ctrl.Finish() 1868 if tt.prepare != nil { 1869 tt.prepare(t, client) 1870 } 1871 if err := w.UpdateRuntimeConfig(tt.args.runtimeConfig); (err != nil) != tt.wantErr { 1872 t.Errorf("extendedServiceRuntimeWrapper.UpdateRuntimeConfig() error = %v, wantErr %v", err, tt.wantErr) 1873 } 1874 }) 1875 } 1876 } 1877 1878 func Test_extendedServiceRuntimeWrapper_Status(t *testing.T) { 1879 tests := []struct { 1880 name string 1881 prepare prepareFunc 1882 want *criruntimev1alpha2.RuntimeStatus 1883 wantErr bool 1884 }{ 1885 { 1886 name: "error", 1887 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1888 c.EXPECT().Status( 1889 gomock.Any(), 1890 gomock.Eq(&criruntimev1alpha2.StatusRequest{ 1891 Verbose: false, 1892 }), 1893 ).Times(1).Return(nil, errMock) 1894 }, 1895 want: nil, 1896 wantErr: true, 1897 }, 1898 { 1899 name: "success", 1900 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1901 c.EXPECT().Status( 1902 gomock.Any(), 1903 gomock.Eq(&criruntimev1alpha2.StatusRequest{ 1904 Verbose: false, 1905 }), 1906 ).Times(1).Return(&criruntimev1alpha2.StatusResponse{ 1907 Status: &criruntimev1alpha2.RuntimeStatus{ 1908 Conditions: []*criruntimev1alpha2.RuntimeCondition{ 1909 { 1910 Type: "RuntimeReady", 1911 Status: true, 1912 Reason: "", 1913 Message: "", 1914 }, 1915 { 1916 Type: "NetworkReady", 1917 Status: true, 1918 Reason: "", 1919 Message: "", 1920 }, 1921 }, 1922 }, 1923 }, nil) 1924 }, 1925 want: &criruntimev1alpha2.RuntimeStatus{ 1926 Conditions: []*criruntimev1alpha2.RuntimeCondition{ 1927 { 1928 Type: "RuntimeReady", 1929 Status: true, 1930 Reason: "", 1931 Message: "", 1932 }, 1933 { 1934 Type: "NetworkReady", 1935 Status: true, 1936 Reason: "", 1937 Message: "", 1938 }, 1939 }, 1940 }, 1941 wantErr: false, 1942 }, 1943 } 1944 for _, tt := range tests { 1945 t.Run(tt.name, func(t *testing.T) { 1946 // the same in every test 1947 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 1948 defer cancel() 1949 defer ctrl.Finish() 1950 if tt.prepare != nil { 1951 tt.prepare(t, client) 1952 } 1953 got, err := w.Status() 1954 if (err != nil) != tt.wantErr { 1955 t.Errorf("extendedServiceRuntimeWrapper.Status() error = %v, wantErr %v", err, tt.wantErr) 1956 return 1957 } 1958 if !reflect.DeepEqual(got, tt.want) { 1959 t.Errorf("extendedServiceRuntimeWrapper.Status() = %v, want %v", got, tt.want) 1960 } 1961 }) 1962 } 1963 } 1964 1965 func Test_extendedServiceRuntimeWrapper_StatusVerbose(t *testing.T) { 1966 tests := []struct { 1967 name string 1968 prepare prepareFunc 1969 want *criruntimev1alpha2.RuntimeStatus 1970 want1 map[string]string 1971 wantErr bool 1972 }{ 1973 { 1974 name: "error", 1975 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1976 c.EXPECT().Status( 1977 gomock.Any(), 1978 gomock.Eq(&criruntimev1alpha2.StatusRequest{ 1979 Verbose: true, 1980 }), 1981 ).Times(1).Return(nil, errMock) 1982 }, 1983 want: nil, 1984 wantErr: true, 1985 }, 1986 { 1987 name: "success", 1988 prepare: func(t *testing.T, c *mockcri.MockRuntimeServiceClient) { 1989 c.EXPECT().Status( 1990 gomock.Any(), 1991 gomock.Eq(&criruntimev1alpha2.StatusRequest{ 1992 Verbose: true, 1993 }), 1994 ).Times(1).Return(&criruntimev1alpha2.StatusResponse{ 1995 Status: &criruntimev1alpha2.RuntimeStatus{ 1996 Conditions: []*criruntimev1alpha2.RuntimeCondition{ 1997 { 1998 Type: "RuntimeReady", 1999 Status: true, 2000 Reason: "", 2001 Message: "", 2002 }, 2003 { 2004 Type: "NetworkReady", 2005 Status: true, 2006 Reason: "", 2007 Message: "", 2008 }, 2009 }, 2010 }, 2011 Info: map[string]string{ 2012 "verobse": "output", 2013 }, 2014 }, nil) 2015 }, 2016 want: &criruntimev1alpha2.RuntimeStatus{ 2017 Conditions: []*criruntimev1alpha2.RuntimeCondition{ 2018 { 2019 Type: "RuntimeReady", 2020 Status: true, 2021 Reason: "", 2022 Message: "", 2023 }, 2024 { 2025 Type: "NetworkReady", 2026 Status: true, 2027 Reason: "", 2028 Message: "", 2029 }, 2030 }, 2031 }, 2032 want1: map[string]string{ 2033 "verobse": "output", 2034 }, 2035 wantErr: false, 2036 }, 2037 } 2038 for _, tt := range tests { 2039 t.Run(tt.name, func(t *testing.T) { 2040 // the same in every test 2041 ctrl, client, cancel, w := newUnitTestCRIExtendedRuntimeServiceWrapper(t) 2042 defer cancel() 2043 defer ctrl.Finish() 2044 if tt.prepare != nil { 2045 tt.prepare(t, client) 2046 } 2047 got, got1, err := w.StatusVerbose() 2048 if (err != nil) != tt.wantErr { 2049 t.Errorf("extendedServiceRuntimeWrapper.StatusVerbose() error = %v, wantErr %v", err, tt.wantErr) 2050 return 2051 } 2052 if !reflect.DeepEqual(got, tt.want) { 2053 t.Errorf("extendedServiceRuntimeWrapper.StatusVerbose() got = %v, want %v", got, tt.want) 2054 } 2055 if !reflect.DeepEqual(got1, tt.want1) { 2056 t.Errorf("extendedServiceRuntimeWrapper.StatusVerbose() got1 = %v, want %v", got1, tt.want1) 2057 } 2058 }) 2059 } 2060 }