From 95ca846a08edc1fa9484f479dbebb2e2f396b96d Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Mon, 12 Jun 2017 21:08:25 +0300 Subject: [PATCH] Unit tests --- builder_test.go | 14 +++++++++++ config.go | 10 +++----- config_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 builder_test.go create mode 100644 config_test.go diff --git a/builder_test.go b/builder_test.go new file mode 100644 index 000000000..20243b659 --- /dev/null +++ b/builder_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "github.com/hashicorp/packer/packer" + "testing" +) + +func TestBuilder_ImplementsBuilder(t *testing.T) { + var raw interface{} + raw = &Builder{} + if _, ok := raw.(packer.Builder); !ok { + t.Fatalf("Builder should be a builder") + } +} diff --git a/config.go b/config.go index b98ff9b94..9d11afa80 100644 --- a/config.go +++ b/config.go @@ -57,6 +57,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { // Accumulate any errors errs := new(packer.MultiError) + var warnings []string // Prepare config(s) errs = packer.MultiErrorAppend(errs, c.Config.Prepare(&c.ctx)...) @@ -100,12 +101,9 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err)) } - - // Warnings - var warnings []string - if c.Datastore == "" { - warnings = append(warnings, "Datastore is not specified, will try to find the default one") - } + //if c.Datastore == "" { + // warnings = append(warnings, "Datastore is not specified, will try to find the default one") + //} if len(errs.Errors) > 0 { return nil, warnings, errs diff --git a/config_test.go b/config_test.go new file mode 100644 index 000000000..f6bed1613 --- /dev/null +++ b/config_test.go @@ -0,0 +1,67 @@ +package main + +import ( + "testing" + "time" +) + +func TestMinimalConfig(t *testing.T) { + _, warns, errs := NewConfig(minimalConfig()) + + testConfigOk(t, warns, errs) +} + +func TestInvalidCpu(t *testing.T) { + raw := minimalConfig() + raw["cpus"] = "string" + _, warns, errs := NewConfig(raw) + testConfigErr(t, warns, errs) +} + +func TestInvalidRam(t *testing.T) { + raw := minimalConfig() + raw["RAM"] = "string" + _, warns, errs := NewConfig(raw) + testConfigErr(t, warns, errs) +} + +func TestTimeout(t *testing.T) { + raw := minimalConfig() + raw["shutdown_timeout"] = "3m" + conf, warns, err := NewConfig(raw) + testConfigOk(t, warns, err) + if conf.ShutdownTimeout != 3 * time.Minute { + t.Fatalf("shutdown_timeout sourld equal 3 minutes") + } +} + +func minimalConfig() map[string]interface{} { + return map[string]interface{}{ + "url": "https://vcenter.domain.local/sdk", + "username": "root", + "password": "vmware", + "template": "ubuntu", + "vm_name": "vm1", + "host": "esxi1.domain.local", + "ssh_username": "root", + "ssh_password": "secret", + } +} + +func testConfigOk(t *testing.T, warns []string, err error) { + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("bad: %s", err) + } +} + +func testConfigErr(t *testing.T, warns []string, err error) { + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err == nil { + t.Fatal("should error") + } +}