diff --git a/packer/core.go b/packer/core.go index e8da2b2ee..f768d00c6 100644 --- a/packer/core.go +++ b/packer/core.go @@ -421,7 +421,7 @@ func (c *Core) Build(n string) (packersdk.Build, error) { // Return a structure that contains the plugins, their types, variables, and // the raw builder config loaded from the json template cb := &CoreBuild{ - Type: configBuilder.Name, + Type: n, Builder: builder, BuilderConfig: configBuilder.Config, BuilderType: configBuilder.Type, @@ -432,6 +432,8 @@ func (c *Core) Build(n string) (packersdk.Build, error) { Variables: c.variables, } + //configBuilder.Name is left uninterpolated so we must check against + // the interpolated name. if configBuilder.Type != configBuilder.Name { cb.BuildName = configBuilder.Type } diff --git a/packer/core_test.go b/packer/core_test.go index 309e54755..8bea78ab6 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -68,6 +68,10 @@ func TestCoreBuild_basic(t *testing.T) { t.Fatalf("err: %s", err) } + if build.Name() != "test" { + t.Fatalf("bad: build name does not match expected: %q, got: %q", "test", build.Name()) + } + if _, err := build.Prepare(); err != nil { t.Fatalf("err: %s", err) } @@ -98,6 +102,10 @@ func TestCoreBuild_basicInterpolated(t *testing.T) { t.Fatalf("err: %s", err) } + if build.Name() != "test.NAME" { + t.Fatalf("bad: build name does not match expected: %q, got: %q", "NAME", build.Name()) + } + if _, err := build.Prepare(); err != nil { t.Fatalf("err: %s", err) } @@ -865,3 +873,64 @@ func TestCoreBuild_packerVersion(t *testing.T) { t.Fatalf("bad: %#v", result) } } + +func TestCoreBuild_buildNameIntepolation(t *testing.T) { + config := TestCoreConfig(t) + cases := []struct { + File string + InterpolatedName, Expected string + Vars map[string]string + }{ + { + File: "build-interpolated-name.json", + InterpolatedName: "mybuild-RandomToken", + Expected: "test.mybuild-RandomToken", + Vars: map[string]string{ + "build_name": "mybuild-RandomToken", + }, + }, + { + File: "build-interpolated-name.json", + InterpolatedName: "build-vardata", + Expected: "test.build-vardata", + Vars: map[string]string{ + "build_name": "build-vardata", + }, + }, + { + File: "build-interpolated-name.json", + InterpolatedName: "build-12345", + Expected: "test.build-12345", + Vars: map[string]string{ + "something": "build-12345", + "build_name": "{{user `something`}}", + }, + }, + { + // When no name attribute is provided in the config the builder type is the default name. + File: "build-basic.json", + InterpolatedName: "test", + Expected: "test", + }, + } + + for _, tc := range cases { + config.Variables = tc.Vars + testCoreTemplate(t, config, fixtureDir(tc.File)) + core := TestCore(t, config) + diags := core.Initialize(InitializeOptions{}) + if diags.HasErrors() { + t.Fatalf("err: %s\n\n%s", tc.File, diags) + } + + build, err := core.Build(tc.InterpolatedName) + if err != nil { + t.Fatalf("err for InterpolatedName(%q): %s", tc.InterpolatedName, err) + } + + if build.Name() != tc.Expected { + t.Errorf("build type interpolation failed; expected %q, got %q", tc.Expected, build.Name()) + } + + } +} diff --git a/packer/test-fixtures/build-interpolated-name.json b/packer/test-fixtures/build-interpolated-name.json new file mode 100644 index 000000000..9ac1dd779 --- /dev/null +++ b/packer/test-fixtures/build-interpolated-name.json @@ -0,0 +1,7 @@ + +{ + "builders": [{ + "type": "test", + "name": "{{user `build_name`}}" + }] +}