-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hcp: give a proper error when using conflicting build name
It was possible to put the same source 2 times in the build and when using HCP, it would error eventually since they are considered the same build from HCP side
- Loading branch information
Showing
2 changed files
with
331 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
package registry | ||
|
||
import ( | ||
"reflect" | ||
"slices" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/packer/hcl2template" | ||
) | ||
|
||
func TestNewRegisterProperBuildName(t *testing.T) { | ||
cases := map[string]struct { | ||
expectedBuilds []string | ||
err bool | ||
diagsSummaryContains string | ||
builds hcl2template.Builds | ||
}{ | ||
"single build block with single source": { | ||
err: false, | ||
expectedBuilds: []string{"docker.ubuntu"}, | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"single build block with name and with single source": { | ||
err: false, | ||
expectedBuilds: []string{"docker.ubuntu"}, | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Name: "my-build-block", | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"single build block with multiple sources": { | ||
err: false, | ||
expectedBuilds: []string{"docker.alpine", "docker.ubuntu"}, | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "alpine", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"single build block with name and multiple sources": { | ||
err: false, | ||
expectedBuilds: []string{"docker.alpine", "docker.ubuntu"}, | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Name: "my-build-block", | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "alpine", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"single build block with multiple identical sources create conflict": { | ||
err: true, | ||
diagsSummaryContains: "conflict", | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"multiple build block with different source": { | ||
err: false, | ||
expectedBuilds: []string{"docker.alpine", "docker.ubuntu"}, | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
}, | ||
}, | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "alpine", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"multiple build block with same source create conflict": { | ||
err: true, | ||
diagsSummaryContains: "conflict", | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "alpine", | ||
}, | ||
}, | ||
}, | ||
}, | ||
&hcl2template.BuildBlock{ | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "alpine", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"multiple build block with same source but with different build name": { | ||
err: false, | ||
expectedBuilds: []string{"build1.docker.ubuntu", "build2.docker.ubuntu"}, | ||
builds: hcl2template.Builds{ | ||
&hcl2template.BuildBlock{ | ||
Name: "build1", | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
}, | ||
}, | ||
&hcl2template.BuildBlock{ | ||
Name: "build2", | ||
Sources: []hcl2template.SourceUseBlock{ | ||
{ | ||
SourceRef: hcl2template.SourceRef{ | ||
Type: "docker", | ||
Name: "ubuntu", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for desc, tc := range cases { | ||
t.Run(desc, func(t *testing.T) { | ||
|
||
config := &hcl2template.PackerConfig{ | ||
Builds: tc.builds, | ||
} | ||
|
||
registry := HCLRegistry{ | ||
configuration: config, | ||
bucket: &Bucket{ | ||
Name: "test-bucket-" + desc, | ||
Version: &Version{}, | ||
}, | ||
fullBuildNames: map[string]map[string]struct{}{}, | ||
} | ||
|
||
diags := registry.registerAllComponents() | ||
if tc.diagsSummaryContains != "" { | ||
|
||
containsMsg := false | ||
for _, diag := range diags { | ||
if strings.Contains(diag.Summary, tc.diagsSummaryContains) { | ||
containsMsg = true | ||
} | ||
} | ||
if !containsMsg { | ||
t.Fatalf("diagnostics should contains '%s' in summary", tc.diagsSummaryContains) | ||
} | ||
} | ||
if !tc.err { | ||
if diags.HasErrors() { | ||
t.Fatalf("should not report error diagnostic: %v", diags) | ||
} | ||
} | ||
if tc.err { | ||
if !diags.HasErrors() { | ||
t.Fatal("should report error in this case") | ||
} | ||
return | ||
} | ||
|
||
actualExpectedBuilds := registry.bucket.Version.expectedBuilds | ||
|
||
slices.Sort(tc.expectedBuilds) | ||
slices.Sort(actualExpectedBuilds) | ||
|
||
if !reflect.DeepEqual(tc.expectedBuilds, actualExpectedBuilds) { | ||
t.Fatalf("expectedBuilds registered: %v, got: %v", tc.expectedBuilds, actualExpectedBuilds) | ||
} | ||
}) | ||
} | ||
} |