packer: build_name and build_type work + tests
This commit is contained in:
parent
f1b3c8a7ae
commit
472b060394
@ -104,6 +104,8 @@ func Decode(target interface{}, config *DecodeOpts, raws ...interface{}) error {
|
|||||||
// detecting things like user variables from the raw configuration params.
|
// detecting things like user variables from the raw configuration params.
|
||||||
func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
|
func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
|
||||||
var s struct {
|
var s struct {
|
||||||
|
BuildName string `mapstructure:"packer_build_name"`
|
||||||
|
BuildType string `mapstructure:"packer_builder_type"`
|
||||||
TemplatePath string `mapstructure:"packer_template_path"`
|
TemplatePath string `mapstructure:"packer_template_path"`
|
||||||
Vars map[string]string `mapstructure:"packer_user_variables"`
|
Vars map[string]string `mapstructure:"packer_user_variables"`
|
||||||
}
|
}
|
||||||
@ -115,6 +117,8 @@ func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &interpolate.Context{
|
return &interpolate.Context{
|
||||||
|
BuildName: s.BuildName,
|
||||||
|
BuildType: s.BuildType,
|
||||||
TemplatePath: s.TemplatePath,
|
TemplatePath: s.TemplatePath,
|
||||||
UserVariables: s.Vars,
|
UserVariables: s.Vars,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -142,6 +142,64 @@ func TestCoreBuild_env(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCoreBuild_buildNameVar(t *testing.T) {
|
||||||
|
config := TestCoreConfig(t)
|
||||||
|
testCoreTemplate(t, config, fixtureDir("build-var-build-name.json"))
|
||||||
|
b := TestBuilder(t, config, "test")
|
||||||
|
core := TestCore(t, config)
|
||||||
|
|
||||||
|
b.ArtifactId = "hello"
|
||||||
|
|
||||||
|
build, err := core.Build("test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := build.Prepare(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interpolate the config
|
||||||
|
var result map[string]interface{}
|
||||||
|
err = configHelper.Decode(&result, nil, b.PrepareConfig...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result["value"] != "test" {
|
||||||
|
t.Fatalf("bad: %#v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoreBuild_buildTypeVar(t *testing.T) {
|
||||||
|
config := TestCoreConfig(t)
|
||||||
|
testCoreTemplate(t, config, fixtureDir("build-var-build-type.json"))
|
||||||
|
b := TestBuilder(t, config, "test")
|
||||||
|
core := TestCore(t, config)
|
||||||
|
|
||||||
|
b.ArtifactId = "hello"
|
||||||
|
|
||||||
|
build, err := core.Build("test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := build.Prepare(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interpolate the config
|
||||||
|
var result map[string]interface{}
|
||||||
|
err = configHelper.Decode(&result, nil, b.PrepareConfig...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result["value"] != "test" {
|
||||||
|
t.Fatalf("bad: %#v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCoreBuild_nonExist(t *testing.T) {
|
func TestCoreBuild_nonExist(t *testing.T) {
|
||||||
config := TestCoreConfig(t)
|
config := TestCoreConfig(t)
|
||||||
testCoreTemplate(t, config, fixtureDir("build-basic.json"))
|
testCoreTemplate(t, config, fixtureDir("build-basic.json"))
|
||||||
|
6
packer/test-fixtures/build-var-build-name.json
Normal file
6
packer/test-fixtures/build-var-build-name.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"builders": [{
|
||||||
|
"type": "test",
|
||||||
|
"value": "{{build_name}}"
|
||||||
|
}]
|
||||||
|
}
|
6
packer/test-fixtures/build-var-build-type.json
Normal file
6
packer/test-fixtures/build-var-build-type.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"builders": [{
|
||||||
|
"type": "test",
|
||||||
|
"value": "{{build_type}}"
|
||||||
|
}]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user