|
1 | 1 | package generator |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/devfile/library/pkg/devfile/parser/data" |
| 5 | + "github.com/devfile/library/pkg/util" |
4 | 6 | "reflect" |
| 7 | + "strings" |
5 | 8 | "testing" |
6 | 9 |
|
7 | 10 | v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
@@ -487,3 +490,186 @@ func TestGetVolumeMountPath(t *testing.T) { |
487 | 490 | } |
488 | 491 |
|
489 | 492 | } |
| 493 | + |
| 494 | +func TestGetInitContainers(t *testing.T) { |
| 495 | + shellExecutable := "/bin/sh" |
| 496 | + containers := []v1.Component{ |
| 497 | + { |
| 498 | + Name: "container1", |
| 499 | + ComponentUnion: v1.ComponentUnion{ |
| 500 | + Container: &v1.ContainerComponent{ |
| 501 | + Container: v1.Container{ |
| 502 | + Image: "container1", |
| 503 | + Command: []string{shellExecutable, "-c", "cd execworkdir1 && execcommand1"}, |
| 504 | + }, |
| 505 | + }, |
| 506 | + }, |
| 507 | + }, |
| 508 | + { |
| 509 | + Name: "container2", |
| 510 | + ComponentUnion: v1.ComponentUnion{ |
| 511 | + Container: &v1.ContainerComponent{ |
| 512 | + Container: v1.Container{ |
| 513 | + Image: "container2", |
| 514 | + Command: []string{shellExecutable, "-c", "cd execworkdir3 && execcommand3"}, |
| 515 | + }, |
| 516 | + }, |
| 517 | + }, |
| 518 | + }, |
| 519 | + } |
| 520 | + |
| 521 | + execCommands := []v1.Command{ |
| 522 | + { |
| 523 | + Id: "apply1", |
| 524 | + CommandUnion: v1.CommandUnion{ |
| 525 | + Apply: &v1.ApplyCommand{ |
| 526 | + Component: "container1", |
| 527 | + }, |
| 528 | + }, |
| 529 | + }, |
| 530 | + { |
| 531 | + Id: "apply2", |
| 532 | + CommandUnion: v1.CommandUnion{ |
| 533 | + Apply: &v1.ApplyCommand{ |
| 534 | + Component: "container1", |
| 535 | + }, |
| 536 | + }, |
| 537 | + }, |
| 538 | + { |
| 539 | + Id: "apply3", |
| 540 | + CommandUnion: v1.CommandUnion{ |
| 541 | + Apply: &v1.ApplyCommand{ |
| 542 | + Component: "container2", |
| 543 | + }, |
| 544 | + }, |
| 545 | + }, |
| 546 | + } |
| 547 | + |
| 548 | + compCommands := []v1.Command{ |
| 549 | + { |
| 550 | + Id: "comp1", |
| 551 | + CommandUnion: v1.CommandUnion{ |
| 552 | + Composite: &v1.CompositeCommand{ |
| 553 | + Commands: []string{ |
| 554 | + "apply1", |
| 555 | + "apply3", |
| 556 | + }, |
| 557 | + }, |
| 558 | + }, |
| 559 | + }, |
| 560 | + } |
| 561 | + |
| 562 | + longContainerName := "thisisaverylongcontainerandkuberneteshasalimitforanamesize-exec2" |
| 563 | + trimmedLongContainerName := util.TruncateString(longContainerName, containerNameMaxLen) |
| 564 | + |
| 565 | + tests := []struct { |
| 566 | + name string |
| 567 | + eventCommands []string |
| 568 | + wantInitContainer map[string]corev1.Container |
| 569 | + longName bool |
| 570 | + wantErr bool |
| 571 | + }{ |
| 572 | + { |
| 573 | + name: "Composite and Exec events", |
| 574 | + eventCommands: []string{ |
| 575 | + "apply1", |
| 576 | + "apply3", |
| 577 | + "apply2", |
| 578 | + }, |
| 579 | + wantInitContainer: map[string]corev1.Container{ |
| 580 | + "container1-apply1": { |
| 581 | + Command: []string{shellExecutable, "-c", "cd execworkdir1 && execcommand1"}, |
| 582 | + }, |
| 583 | + "container1-apply2": { |
| 584 | + Command: []string{shellExecutable, "-c", "cd execworkdir1 && execcommand1"}, |
| 585 | + }, |
| 586 | + "container2-apply3": { |
| 587 | + Command: []string{shellExecutable, "-c", "cd execworkdir3 && execcommand3"}, |
| 588 | + }, |
| 589 | + }, |
| 590 | + }, |
| 591 | + { |
| 592 | + name: "Long Container Name", |
| 593 | + eventCommands: []string{ |
| 594 | + "apply2", |
| 595 | + }, |
| 596 | + wantInitContainer: map[string]corev1.Container{ |
| 597 | + trimmedLongContainerName: { |
| 598 | + Command: []string{shellExecutable, "-c", "cd execworkdir1 && execcommand1"}, |
| 599 | + }, |
| 600 | + }, |
| 601 | + longName: true, |
| 602 | + }, |
| 603 | + } |
| 604 | + for _, tt := range tests { |
| 605 | + t.Run(tt.name, func(t *testing.T) { |
| 606 | + |
| 607 | + if tt.longName { |
| 608 | + containers[0].Name = longContainerName |
| 609 | + execCommands[1].Apply.Component = longContainerName |
| 610 | + } |
| 611 | + |
| 612 | + devObj := parser.DevfileObj{ |
| 613 | + Data: func() data.DevfileData { |
| 614 | + devfileData, err := data.NewDevfileData(string(data.APISchemaVersion210)) |
| 615 | + if err != nil { |
| 616 | + t.Error(err) |
| 617 | + } |
| 618 | + err = devfileData.AddComponents(containers) |
| 619 | + if err != nil { |
| 620 | + t.Error(err) |
| 621 | + } |
| 622 | + err = devfileData.AddCommands(execCommands) |
| 623 | + if err != nil { |
| 624 | + t.Error(err) |
| 625 | + } |
| 626 | + err = devfileData.AddCommands(compCommands) |
| 627 | + if err != nil { |
| 628 | + t.Error(err) |
| 629 | + } |
| 630 | + err = devfileData.AddEvents(v1.Events{ |
| 631 | + DevWorkspaceEvents: v1.DevWorkspaceEvents{ |
| 632 | + PreStart: tt.eventCommands, |
| 633 | + }, |
| 634 | + }) |
| 635 | + if err != nil { |
| 636 | + t.Error(err) |
| 637 | + } |
| 638 | + return devfileData |
| 639 | + }(), |
| 640 | + } |
| 641 | + |
| 642 | + initContainers, err := GetInitContainers(devObj) |
| 643 | + if (err != nil) != tt.wantErr { |
| 644 | + t.Errorf("TestGetInitContainers() error = %v, wantErr %v", err, tt.wantErr) |
| 645 | + } |
| 646 | + |
| 647 | + if len(tt.wantInitContainer) != len(initContainers) { |
| 648 | + t.Errorf("TestGetInitContainers() error: init container length mismatch, wanted %v got %v", len(tt.wantInitContainer), len(initContainers)) |
| 649 | + } |
| 650 | + |
| 651 | + for _, initContainer := range initContainers { |
| 652 | + nameMatched := false |
| 653 | + commandMatched := false |
| 654 | + for containerName, container := range tt.wantInitContainer { |
| 655 | + if strings.Contains(initContainer.Name, containerName) { |
| 656 | + nameMatched = true |
| 657 | + } |
| 658 | + |
| 659 | + if reflect.DeepEqual(initContainer.Command, container.Command) { |
| 660 | + commandMatched = true |
| 661 | + } |
| 662 | + } |
| 663 | + |
| 664 | + if !nameMatched { |
| 665 | + t.Errorf("TestGetInitContainers() error: init container name mismatch, container name not present in %v", initContainer.Name) |
| 666 | + } |
| 667 | + |
| 668 | + if !commandMatched { |
| 669 | + t.Errorf("TestGetInitContainers() error: init container command mismatch, command not found in %v", initContainer.Command) |
| 670 | + } |
| 671 | + } |
| 672 | + }) |
| 673 | + } |
| 674 | + |
| 675 | +} |
0 commit comments