Commit 023678fe authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

provisioner/puppet-masterless: more validation and tests

parent d854b086
...@@ -122,6 +122,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { ...@@ -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 { if errs != nil && len(errs.Errors) > 0 {
return errs return errs
} }
......
...@@ -2,12 +2,19 @@ package puppetmasterless ...@@ -2,12 +2,19 @@ package puppetmasterless
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"testing" "testing"
) )
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
tf, err := ioutil.TempFile("", "packer")
if err != nil {
panic(err)
}
return map[string]interface{}{ return map[string]interface{}{
// "inline": []interface{}{"foo", "bar"}, "manifest_file": tf.Name(),
} }
} }
...@@ -18,3 +25,61 @@ func TestProvisioner_Impl(t *testing.T) { ...@@ -18,3 +25,61 @@ func TestProvisioner_Impl(t *testing.T) {
t.Fatalf("must be a Provisioner") 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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment