From 023678fe7b28edf56c2e1c13b63cd79443b8a2cd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 7 Sep 2013 23:31:28 -0700 Subject: [PATCH] provisioner/puppet-masterless: more validation and tests --- provisioner/puppet-masterless/provisioner.go | 11 +++ .../puppet-masterless/provisioner_test.go | 67 ++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/provisioner/puppet-masterless/provisioner.go b/provisioner/puppet-masterless/provisioner.go index 5a0288cba..682aa584e 100644 --- a/provisioner/puppet-masterless/provisioner.go +++ b/provisioner/puppet-masterless/provisioner.go @@ -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 } diff --git a/provisioner/puppet-masterless/provisioner_test.go b/provisioner/puppet-masterless/provisioner_test.go index e2e4b3c96..88dea6029 100644 --- a/provisioner/puppet-masterless/provisioner_test.go +++ b/provisioner/puppet-masterless/provisioner_test.go @@ -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) + } +}