Merge pull request #1694 from ceh/issue-1125

Test for user variables in vagrantfile_template [GH-1125]
This commit is contained in:
Armon Dadgar 2014-12-08 11:15:12 -08:00
commit fdeb2bc8a2
2 changed files with 50 additions and 8 deletions

View File

@ -113,14 +113,8 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p
// Write our Vagrantfile // Write our Vagrantfile
var customVagrantfile string var customVagrantfile string
if config.VagrantfileTemplate != "" { if config.VagrantfileTemplate != "" {
vagrantfilePath, err := config.tpl.Process(config.VagrantfileTemplate, nil) ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
if err != nil { customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
return nil, false, err
}
ui.Message(fmt.Sprintf(
"Using custom Vagrantfile: %s", vagrantfilePath))
customBytes, err := ioutil.ReadFile(vagrantfilePath)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
@ -200,6 +194,17 @@ func (p *PostProcessor) configureSingle(config *Config, raws ...interface{}) err
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
templates := map[string]*string{
"vagrantfile_template": &config.VagrantfileTemplate,
}
for key, ptr := range templates {
*ptr, err = config.tpl.Process(*ptr, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Error processing %s: %s", key, err))
}
}
validates := map[string]*string{ validates := map[string]*string{
"output": &config.OutputPath, "output": &config.OutputPath,
"vagrantfile_template": &config.VagrantfileTemplate, "vagrantfile_template": &config.VagrantfileTemplate,

View File

@ -4,6 +4,8 @@ import (
"bytes" "bytes"
"compress/flate" "compress/flate"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"strings" "strings"
"testing" "testing"
) )
@ -124,6 +126,41 @@ func TestPostProcessorPostProcess_badId(t *testing.T) {
} }
} }
func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) {
var p PostProcessor
f, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(f.Name())
c := map[string]interface{}{
"packer_user_variables": map[string]string{
"foo": f.Name(),
},
"vagrantfile_template": "{{user `foo`}}",
}
err = p.Configure(c)
if err != nil {
t.Fatalf("err: %s", err)
}
a := &packer.MockArtifact{
BuilderIdValue: "packer.parallels",
}
a2, _, err := p.PostProcess(testUi(), a)
if a2 != nil {
for _, fn := range a2.Files() {
defer os.Remove(fn)
}
}
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProviderForName(t *testing.T) { func TestProviderForName(t *testing.T) {
if v, ok := providerForName("virtualbox").(*VBoxProvider); !ok { if v, ok := providerForName("virtualbox").(*VBoxProvider); !ok {
t.Fatalf("bad: %#v", v) t.Fatalf("bad: %#v", v)