provisioner/puppet-masterless: more validation and tests

This commit is contained in:
Mitchell Hashimoto 2013-09-07 23:31:28 -07:00
parent d854b08642
commit 023678fe7b
2 changed files with 77 additions and 1 deletions

View File

@ -122,6 +122,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
}
for i, path := range p.config.ModulePaths {
info, err := os.Stat(path)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("module_path[%d] is invalid: %s", i, err))
} else if !info.IsDir() {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("module_path[%d] must point to a directory"))
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}

View File

@ -2,12 +2,19 @@ package puppetmasterless
import (
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"testing"
)
func testConfig() map[string]interface{} {
tf, err := ioutil.TempFile("", "packer")
if err != nil {
panic(err)
}
return map[string]interface{}{
// "inline": []interface{}{"foo", "bar"},
"manifest_file": tf.Name(),
}
}
@ -18,3 +25,61 @@ func TestProvisioner_Impl(t *testing.T) {
t.Fatalf("must be a Provisioner")
}
}
func TestProvisionerPrepare_manifestFile(t *testing.T) {
config := testConfig()
delete(config, "manifest_file")
p := new(Provisioner)
err := p.Prepare(config)
if err == nil {
t.Fatal("should be an error")
}
// Test with a good one
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("error tempfile: %s", err)
}
defer os.Remove(tf.Name())
config["manifest_file"] = tf.Name()
p = new(Provisioner)
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvisionerPrepare_modulePaths(t *testing.T) {
config := testConfig()
delete(config, "module_paths")
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
// Test with bad paths
config["module_paths"] = []string{"i-should-not-exist"}
p = new(Provisioner)
err = p.Prepare(config)
if err == nil {
t.Fatal("should be an error")
}
// Test with a good one
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("error: %s", err)
}
defer os.RemoveAll(td)
config["module_paths"] = []string{td}
p = new(Provisioner)
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
}