Unit tests

This commit is contained in:
Michael Kuzmin 2017-06-12 21:08:25 +03:00 committed by Michael Kuzmin
parent f42df93e8d
commit 95ca846a08
3 changed files with 85 additions and 6 deletions

14
builder_test.go Normal file
View File

@ -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")
}
}

View File

@ -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

67
config_test.go Normal file
View File

@ -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")
}
}