|
8 | 8 | "github.com/devfile/api/v2/pkg/attributes"
|
9 | 9 | "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
|
10 | 10 | "github.com/devfile/library/pkg/testingutil"
|
| 11 | + "github.com/stretchr/testify/assert" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | func TestDevfile200_AddComponent(t *testing.T) {
|
@@ -403,3 +404,94 @@ func TestGetDevfileVolumeComponents(t *testing.T) {
|
403 | 404 | }
|
404 | 405 |
|
405 | 406 | }
|
| 407 | + |
| 408 | +func TestDeleteComponents(t *testing.T) { |
| 409 | + |
| 410 | + d := &DevfileV2{ |
| 411 | + v1.Devfile{ |
| 412 | + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ |
| 413 | + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ |
| 414 | + Components: []v1.Component{ |
| 415 | + { |
| 416 | + Name: "comp2", |
| 417 | + ComponentUnion: v1.ComponentUnion{ |
| 418 | + Container: &v1.ContainerComponent{ |
| 419 | + Container: v1.Container{ |
| 420 | + VolumeMounts: []v1.VolumeMount{ |
| 421 | + testingutil.GetFakeVolumeMount("comp2", "/path"), |
| 422 | + testingutil.GetFakeVolumeMount("comp2", "/path2"), |
| 423 | + testingutil.GetFakeVolumeMount("comp3", "/path"), |
| 424 | + }, |
| 425 | + }, |
| 426 | + }, |
| 427 | + }, |
| 428 | + }, |
| 429 | + { |
| 430 | + Name: "comp2", |
| 431 | + ComponentUnion: v1.ComponentUnion{ |
| 432 | + Volume: &v1.VolumeComponent{}, |
| 433 | + }, |
| 434 | + }, |
| 435 | + { |
| 436 | + Name: "comp3", |
| 437 | + ComponentUnion: v1.ComponentUnion{ |
| 438 | + Volume: &v1.VolumeComponent{}, |
| 439 | + }, |
| 440 | + }, |
| 441 | + }, |
| 442 | + }, |
| 443 | + }, |
| 444 | + }, |
| 445 | + } |
| 446 | + |
| 447 | + tests := []struct { |
| 448 | + name string |
| 449 | + componentToDelete string |
| 450 | + wantComponents []v1.Component |
| 451 | + wantErr bool |
| 452 | + }{ |
| 453 | + { |
| 454 | + name: "Successfully delete a Component", |
| 455 | + componentToDelete: "comp3", |
| 456 | + wantComponents: []v1.Component{ |
| 457 | + { |
| 458 | + Name: "comp2", |
| 459 | + ComponentUnion: v1.ComponentUnion{ |
| 460 | + Container: &v1.ContainerComponent{ |
| 461 | + Container: v1.Container{ |
| 462 | + VolumeMounts: []v1.VolumeMount{ |
| 463 | + testingutil.GetFakeVolumeMount("comp2", "/path"), |
| 464 | + testingutil.GetFakeVolumeMount("comp2", "/path2"), |
| 465 | + testingutil.GetFakeVolumeMount("comp3", "/path"), |
| 466 | + }, |
| 467 | + }, |
| 468 | + }, |
| 469 | + }, |
| 470 | + }, |
| 471 | + { |
| 472 | + Name: "comp2", |
| 473 | + ComponentUnion: v1.ComponentUnion{ |
| 474 | + Volume: &v1.VolumeComponent{}, |
| 475 | + }, |
| 476 | + }, |
| 477 | + }, |
| 478 | + wantErr: false, |
| 479 | + }, |
| 480 | + { |
| 481 | + name: "Missing Component", |
| 482 | + componentToDelete: "comp12", |
| 483 | + wantErr: true, |
| 484 | + }, |
| 485 | + } |
| 486 | + for _, tt := range tests { |
| 487 | + t.Run(tt.name, func(t *testing.T) { |
| 488 | + err := d.DeleteComponent(tt.componentToDelete) |
| 489 | + if (err != nil) != tt.wantErr { |
| 490 | + t.Errorf("DeleteComponent() error = %v, wantErr %v", err, tt.wantErr) |
| 491 | + } else if err == nil { |
| 492 | + assert.Equal(t, tt.wantComponents, d.Components, "The two values should be the same.") |
| 493 | + } |
| 494 | + }) |
| 495 | + } |
| 496 | + |
| 497 | +} |
0 commit comments