Skip to content

Commit

Permalink
fix(directives): return kustomize-set-image result (#2538)
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco authored Sep 16, 2024
1 parent a03e949 commit f441c89
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/directives/kustomize_set_image_directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (d *kustomizeSetImageDirective) run(
result.Output = make(State, 1)
result.Output.Set("commitMessage", commitMsg)
}
return Result{Status: StatusSuccess}, nil
return result, nil
}

func (d *kustomizeSetImageDirective) buildTargetImages(
Expand Down
63 changes: 34 additions & 29 deletions internal/directives/kustomize_set_image_directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ kind: Kustomization
cfg: KustomizeSetImageConfig{
Path: ".",
Images: []KustomizeSetImageConfigImage{
{Image: "nginx:latest"},
{Image: "nginx"},
},
},
setupStepCtx: func(t *testing.T, workDir string) *StepContext {
Expand All @@ -51,7 +51,7 @@ kind: Kustomization
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
mockWarehouse(testNamespace, "warehouse1", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
).Build()
Expand All @@ -67,15 +67,20 @@ kind: Kustomization
Freight: map[string]kargoapi.FreightReference{
"Warehouse/warehouse1": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse1"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:123"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:123"}},
},
},
},
}
},
assertions: func(t *testing.T, workDir string, result Result, err error) {
require.NoError(t, err)
assert.Equal(t, Result{Status: StatusSuccess}, result)
assert.Equal(t, Result{
Status: StatusSuccess,
Output: State{
"commitMessage": "Updated . to use new image\n\n- nginx:1.21.0",
},
}, result)

b, err := os.ReadFile(filepath.Join(workDir, "kustomization.yaml"))
require.NoError(t, err)
Expand All @@ -90,7 +95,7 @@ kind: Kustomization
cfg: KustomizeSetImageConfig{
Path: ".",
Images: []KustomizeSetImageConfigImage{
{Image: "nginx:latest"},
{Image: "nginx"},
},
},
setupStepCtx: func(t *testing.T, workDir string) *StepContext {
Expand Down Expand Up @@ -127,7 +132,7 @@ images:
cfg: KustomizeSetImageConfig{
Path: ".",
Images: []KustomizeSetImageConfigImage{
{Image: "nginx:latest"},
{Image: "nginx"},
},
},
setupStepCtx: func(t *testing.T, workDir string) *StepContext {
Expand Down Expand Up @@ -177,8 +182,8 @@ func Test_kustomizeSetImageDirective_buildTargetImages(t *testing.T) {
{
name: "discovers origins and builds target images",
images: []KustomizeSetImageConfigImage{
{Image: "nginx:latest"},
{Image: "redis:6"},
{Image: "nginx"},
{Image: "redis"},
},
freightRequests: []kargoapi.FreightRequest{
{Origin: kargoapi.FreightOrigin{Name: "warehouse1", Kind: "Warehouse"}},
Expand All @@ -187,52 +192,52 @@ func Test_kustomizeSetImageDirective_buildTargetImages(t *testing.T) {
objects: []runtime.Object{
mockWarehouse(testNamespace, "warehouse1", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
mockWarehouse(testNamespace, "warehouse2", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "redis:6"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "redis"}},
},
}),
},
freightReferences: map[string]kargoapi.FreightReference{
"Warehouse/warehouse1": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse1"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:123"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:123"}},
},
"Warehouse/warehouse2": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse2"},
Images: []kargoapi.Image{{RepoURL: "redis:6", Tag: "6.2.5", Digest: "sha256:456"}},
Images: []kargoapi.Image{{RepoURL: "redis", Tag: "6.2.5", Digest: "sha256:456"}},
},
},
assertions: func(t *testing.T, result map[string]kustypes.Image, err error) {
require.NoError(t, err)
assert.Equal(t, map[string]kustypes.Image{
"nginx:latest": {Name: "nginx:latest", NewTag: "1.21.0"},
"redis:6": {Name: "redis:6", NewTag: "6.2.5"},
"nginx": {Name: "nginx", NewTag: "1.21.0"},
"redis": {Name: "redis", NewTag: "6.2.5"},
}, result)
},
},
{
name: "error when no origin found",
images: []KustomizeSetImageConfigImage{
{Image: "mysql:8"},
{Image: "mysql"},
},
freightRequests: []kargoapi.FreightRequest{
{Origin: kargoapi.FreightOrigin{Name: "warehouse1", Kind: "Warehouse"}},
},
objects: []runtime.Object{
mockWarehouse(testNamespace, "warehouse1", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
},
freightReferences: map[string]kargoapi.FreightReference{
"Warehouse/warehouse1": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse1"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:123"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:123"}},
},
},
assertions: func(t *testing.T, _ map[string]kustypes.Image, err error) {
Expand All @@ -242,36 +247,36 @@ func Test_kustomizeSetImageDirective_buildTargetImages(t *testing.T) {
{
name: "uses provided origin",
images: []KustomizeSetImageConfigImage{
{Image: "nginx:latest", FromOrigin: &ChartFromOrigin{Kind: "Warehouse", Name: "warehouse1"}},
{Image: "nginx", FromOrigin: &ChartFromOrigin{Kind: "Warehouse", Name: "warehouse1"}},
},
freightRequests: []kargoapi.FreightRequest{
{Origin: kargoapi.FreightOrigin{Name: "warehouse1", Kind: "Warehouse"}},
},
objects: []runtime.Object{
mockWarehouse(testNamespace, "warehouse1", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
},
freightReferences: map[string]kargoapi.FreightReference{
"Warehouse/warehouse1": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse1"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:123"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:123"}},
},
},
assertions: func(t *testing.T, result map[string]kustypes.Image, err error) {
require.NoError(t, err)
assert.Equal(t, map[string]kustypes.Image{
"nginx:latest": {Name: "nginx:latest", NewTag: "1.21.0"},
"nginx": {Name: "nginx", NewTag: "1.21.0"},
}, result)
},
},
{
name: "uses custom name and digest",
images: []KustomizeSetImageConfigImage{
{
Image: "nginx:latest",
Image: "nginx",
Name: "custom-nginx",
UseDigest: true,
FromOrigin: &ChartFromOrigin{
Expand All @@ -286,14 +291,14 @@ func Test_kustomizeSetImageDirective_buildTargetImages(t *testing.T) {
objects: []runtime.Object{
mockWarehouse(testNamespace, "warehouse1", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
},
freightReferences: map[string]kargoapi.FreightReference{
"Warehouse/warehouse1": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse1"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:123"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:123"}},
},
},
assertions: func(t *testing.T, result map[string]kustypes.Image, err error) {
Expand All @@ -306,7 +311,7 @@ func Test_kustomizeSetImageDirective_buildTargetImages(t *testing.T) {
{
name: "error when multiple origins found",
images: []KustomizeSetImageConfigImage{
{Image: "nginx:latest"},
{Image: "nginx"},
},
freightRequests: []kargoapi.FreightRequest{
{Origin: kargoapi.FreightOrigin{Name: "warehouse1", Kind: "Warehouse"}},
Expand All @@ -315,23 +320,23 @@ func Test_kustomizeSetImageDirective_buildTargetImages(t *testing.T) {
objects: []runtime.Object{
mockWarehouse(testNamespace, "warehouse1", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
mockWarehouse(testNamespace, "warehouse2", kargoapi.WarehouseSpec{
Subscriptions: []kargoapi.RepoSubscription{
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx:latest"}},
{Image: &kargoapi.ImageSubscription{RepoURL: "nginx"}},
},
}),
},
freightReferences: map[string]kargoapi.FreightReference{
"Warehouse/warehouse1": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse1"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:123"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:123"}},
},
"Warehouse/warehouse2": {
Origin: kargoapi.FreightOrigin{Kind: "Warehouse", Name: "warehouse2"},
Images: []kargoapi.Image{{RepoURL: "nginx:latest", Tag: "1.21.0", Digest: "sha256:456"}},
Images: []kargoapi.Image{{RepoURL: "nginx", Tag: "1.21.0", Digest: "sha256:456"}},
},
},
assertions: func(t *testing.T, _ map[string]kustypes.Image, err error) {
Expand Down

0 comments on commit f441c89

Please sign in to comment.