From b46a402a187dacd3e5b3fa08e9eb21095637dff1 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Tue, 27 Dec 2016 14:47:13 -0600 Subject: [PATCH] provisioner(converge): add tests for Prepare --- provisioner/converge/provisioner.go | 4 +- provisioner/converge/provisioner_test.go | 124 +++++++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 provisioner/converge/provisioner_test.go diff --git a/provisioner/converge/provisioner.go b/provisioner/converge/provisioner.go index 78e607652..85dd1da6f 100644 --- a/provisioner/converge/provisioner.go +++ b/provisioner/converge/provisioner.go @@ -22,7 +22,7 @@ import ( "github.com/mitchellh/packer/template/interpolate" ) -var versionRegex = regexp.MustCompile(`[\.\-\d\w]*`) +var versionRegex = regexp.MustCompile(`^[\.\-\da-zA-Z]*$`) // Config for Converge provisioner type Config struct { @@ -96,7 +96,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return fmt.Errorf("Module (\"module\" key) is required in Converge module #%d", i) } if module.Directory == "" { - module.Directory = "/tmp" + p.config.Modules[i].Directory = "/tmp" } } diff --git a/provisioner/converge/provisioner_test.go b/provisioner/converge/provisioner_test.go new file mode 100644 index 000000000..1be925fef --- /dev/null +++ b/provisioner/converge/provisioner_test.go @@ -0,0 +1,124 @@ +package converge + +import ( + "strings" + "testing" + + "github.com/mitchellh/packer/packer" +) + +func testConfig() map[string]interface{} { + return map[string]interface{}{ + "bootstrap": false, + "version": "", + "module_dirs": []map[string]interface{}{ + { + "source": "from", + "destination": "/opt/converge", + }, + }, + "modules": []map[string]interface{}{ + { + "module": "/opt/converge/test.hcl", + }, + }, + } +} + +func TestProvisioner_Impl(t *testing.T) { + var raw interface{} + raw = &Provisioner{} + if _, ok := raw.(packer.Provisioner); !ok { + t.Fatal("must be a Provisioner") + } +} + +func TestProvisionerPrepare(t *testing.T) { + t.Run("defaults", func(t *testing.T) { + var p Provisioner + config := testConfig() + + // delete any keys that we're testing here to make sure they're actually + // being set by `Prepare` + delete(config["modules"].([]map[string]interface{})[0], "directory") + + if err := p.Prepare(config); err != nil { + t.Errorf("err: %s", err) + } + + if p.config.Modules[0].Directory != "/tmp" { + t.Errorf("unexpected module directory: %s", p.config.Modules[0].Directory) + } + }) + + t.Run("validate", func(t *testing.T) { + t.Run("bad version", func(t *testing.T) { + var p Provisioner + config := testConfig() + config["version"] = "bad version with spaces" + + err := p.Prepare(config) + if err == nil { + t.Error("expected error") + } else if !strings.HasPrefix(err.Error(), "Invalid Converge version") { + t.Errorf("expected error starting with \"Invalid Converge version\". Got: %s", err) + } + }) + + t.Run("module dir", func(t *testing.T) { + t.Run("missing source", func(t *testing.T) { + var p Provisioner + config := testConfig() + delete(config["module_dirs"].([]map[string]interface{})[0], "source") + + err := p.Prepare(config) + if err == nil { + t.Error("expected error") + } else if err.Error() != "Source (\"source\" key) is required in Converge module dir #0" { + t.Errorf("bad error message: %s", err) + } + }) + + t.Run("missing destination", func(t *testing.T) { + var p Provisioner + config := testConfig() + delete(config["module_dirs"].([]map[string]interface{})[0], "destination") + + err := p.Prepare(config) + if err == nil { + t.Error("expected error") + } else if err.Error() != "Destination (\"destination\" key) is required in Converge module dir #0" { + t.Errorf("bad error message: %s", err) + } + }) + }) + + t.Run("modules", func(t *testing.T) { + t.Run("none specified", func(t *testing.T) { + var p Provisioner + config := testConfig() + delete(config, "modules") + + err := p.Prepare(config) + if err == nil { + t.Error("expected error") + } else if err.Error() != "Converge requires at least one module (\"modules\" key) to provision the system" { + t.Errorf("bad error message: %s", err) + } + }) + + t.Run("missing module", func(t *testing.T) { + var p Provisioner + config := testConfig() + delete(config["modules"].([]map[string]interface{})[0], "module") + + err := p.Prepare(config) + if err == nil { + t.Error("expected error") + } else if err.Error() != "Module (\"module\" key) is required in Converge module #0" { + t.Errorf("bad error message: %s", err) + } + }) + }) + }) +}