packer: no more asserts lib

This commit is contained in:
Mitchell Hashimoto 2013-10-16 21:09:27 -10:00
parent cf9dafd373
commit 7d3987d840
7 changed files with 462 additions and 267 deletions

View File

@ -1,7 +1,6 @@
package packer package packer
import ( import (
"cgl.tideland.biz/asserts"
"reflect" "reflect"
"testing" "testing"
) )
@ -41,32 +40,43 @@ func testDefaultPackerConfig() map[string]interface{} {
} }
} }
func TestBuild_Name(t *testing.T) { func TestBuild_Name(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
build := testBuild() build := testBuild()
assert.Equal(build.Name(), "test", "should have a name") if build.Name() != "test" {
t.Fatalf("bad: %s", build.Name())
}
} }
func TestBuild_Prepare(t *testing.T) { func TestBuild_Prepare(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
packerConfig := testDefaultPackerConfig() packerConfig := testDefaultPackerConfig()
build := testBuild() build := testBuild()
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
build.Prepare(nil) build.Prepare(nil)
assert.True(builder.prepareCalled, "prepare should be called") if !builder.prepareCalled {
assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42") t.Fatal("should be called")
}
if !reflect.DeepEqual(builder.prepareConfig, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", builder.prepareConfig)
}
coreProv := build.provisioners[0] coreProv := build.provisioners[0]
prov := coreProv.provisioner.(*MockProvisioner) prov := coreProv.provisioner.(*MockProvisioner)
assert.True(prov.PrepCalled, "prepare should be called") if !prov.PrepCalled {
assert.Equal(prov.PrepConfigs, []interface{}{42, packerConfig}, "prepare should be called with proper config") t.Fatal("prep should be called")
}
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", prov.PrepConfigs)
}
corePP := build.postProcessors[0][0] corePP := build.postProcessors[0][0]
pp := corePP.processor.(*TestPostProcessor) pp := corePP.processor.(*TestPostProcessor)
assert.True(pp.configCalled, "config should be called") if !pp.configCalled {
assert.Equal(pp.configVal, []interface{}{make(map[string]interface{}), packerConfig}, "config should have right value") t.Fatal("should be called")
}
if !reflect.DeepEqual(pp.configVal, []interface{}{make(map[string]interface{}), packerConfig}) {
t.Fatalf("bad: %#v", pp.configVal)
}
} }
func TestBuild_Prepare_Twice(t *testing.T) { func TestBuild_Prepare_Twice(t *testing.T) {
@ -90,8 +100,6 @@ func TestBuild_Prepare_Twice(t *testing.T) {
} }
func TestBuild_Prepare_Debug(t *testing.T) { func TestBuild_Prepare_Debug(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
packerConfig := testDefaultPackerConfig() packerConfig := testDefaultPackerConfig()
packerConfig[DebugConfigKey] = true packerConfig[DebugConfigKey] = true
@ -100,13 +108,21 @@ func TestBuild_Prepare_Debug(t *testing.T) {
build.SetDebug(true) build.SetDebug(true)
build.Prepare(nil) build.Prepare(nil)
assert.True(builder.prepareCalled, "prepare should be called") if !builder.prepareCalled {
assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42") t.Fatalf("should be called")
}
if !reflect.DeepEqual(builder.prepareConfig, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", builder.prepareConfig)
}
coreProv := build.provisioners[0] coreProv := build.provisioners[0]
prov := coreProv.provisioner.(*MockProvisioner) prov := coreProv.provisioner.(*MockProvisioner)
assert.True(prov.PrepCalled, "prepare should be called") if !prov.PrepCalled {
assert.Equal(prov.PrepConfigs, []interface{}{42, packerConfig}, "prepare should be called with proper config") t.Fatal("prepare should be called")
}
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", prov.PrepConfigs)
}
} }
func TestBuildPrepare_variables_default(t *testing.T) { func TestBuildPrepare_variables_default(t *testing.T) {
@ -186,37 +202,49 @@ func TestBuildPrepare_variablesRequired(t *testing.T) {
} }
func TestBuild_Run(t *testing.T) { func TestBuild_Run(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
cache := &TestCache{} cache := &TestCache{}
ui := testUi() ui := testUi()
build := testBuild() build := testBuild()
build.Prepare(nil) build.Prepare(nil)
artifacts, err := build.Run(ui, cache) artifacts, err := build.Run(ui, cache)
assert.Nil(err, "should not error") if err != nil {
assert.Equal(len(artifacts), 2, "should have two artifacts") t.Fatalf("err: %s", err)
}
if len(artifacts) != 2 {
t.Fatalf("bad: %#v", artifacts)
}
// Verify builder was run // Verify builder was run
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
assert.True(builder.runCalled, "run should be called") if !builder.runCalled {
t.Fatal("should be called")
}
// Verify hooks are disapatchable // Verify hooks are disapatchable
dispatchHook := builder.runHook dispatchHook := builder.runHook
dispatchHook.Run("foo", nil, nil, 42) dispatchHook.Run("foo", nil, nil, 42)
hook := build.hooks["foo"][0].(*MockHook) hook := build.hooks["foo"][0].(*MockHook)
assert.True(hook.RunCalled, "run should be called") if !hook.RunCalled {
assert.Equal(hook.RunData, 42, "should have correct data") t.Fatal("should be called")
}
if hook.RunData != 42 {
t.Fatalf("bad: %#v", hook.RunData)
}
// Verify provisioners run // Verify provisioners run
dispatchHook.Run(HookProvision, nil, nil, 42) dispatchHook.Run(HookProvision, nil, nil, 42)
prov := build.provisioners[0].provisioner.(*MockProvisioner) prov := build.provisioners[0].provisioner.(*MockProvisioner)
assert.True(prov.ProvCalled, "provision should be called") if !prov.ProvCalled {
t.Fatal("should be called")
}
// Verify post-processor was run // Verify post-processor was run
pp := build.postProcessors[0][0].processor.(*TestPostProcessor) pp := build.postProcessors[0][0].processor.(*TestPostProcessor)
assert.True(pp.ppCalled, "post processor should be called") if !pp.ppCalled {
t.Fatal("should be called")
}
} }
func TestBuild_Run_Artifacts(t *testing.T) { func TestBuild_Run_Artifacts(t *testing.T) {
@ -356,23 +384,26 @@ func TestBuild_Run_Artifacts(t *testing.T) {
} }
func TestBuild_RunBeforePrepare(t *testing.T) { func TestBuild_RunBeforePrepare(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defer func() { defer func() {
p := recover() p := recover()
assert.NotNil(p, "should panic") if p == nil {
assert.Equal(p.(string), "Prepare must be called first", "right panic") t.Fatal("should panic")
}
if p.(string) != "Prepare must be called first" {
t.Fatalf("bad: %s", p.(string))
}
}() }()
testBuild().Run(testUi(), &TestCache{}) testBuild().Run(testUi(), &TestCache{})
} }
func TestBuild_Cancel(t *testing.T) { func TestBuild_Cancel(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
build := testBuild() build := testBuild()
build.Cancel() build.Cancel()
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
assert.True(builder.cancelCalled, "cancel should be called") if !builder.cancelCalled {
t.Fatal("cancel should be called")
}
} }

View File

@ -80,7 +80,7 @@ func TestConfigTemplateProcess_uuid(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if len(result) != 32 { if len(result) != 36 {
t.Fatalf("err: %s", result) t.Fatalf("err: %s", result)
} }
} }

View File

@ -2,12 +2,11 @@ package packer
import ( import (
"bytes" "bytes"
"cgl.tideland.biz/asserts"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"reflect"
"strings" "strings"
"testing" "testing"
) )
@ -44,40 +43,48 @@ func testEnvironment() Environment {
} }
func TestEnvironment_DefaultConfig_Commands(t *testing.T) { func TestEnvironment_DefaultConfig_Commands(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
assert.Empty(config.Commands, "should have no commands") if len(config.Commands) != 0 {
t.Fatalf("bad: %#v", config.Commands)
}
} }
func TestEnvironment_DefaultConfig_Ui(t *testing.T) { func TestEnvironment_DefaultConfig_Ui(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
assert.NotNil(config.Ui, "default UI should not be nil") if config.Ui == nil {
t.Fatal("config.Ui should not be nil")
}
rwUi, ok := config.Ui.(*BasicUi) rwUi, ok := config.Ui.(*BasicUi)
assert.True(ok, "default UI should be BasicUi") if !ok {
assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout") t.Fatal("default UI should be BasicUi")
assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin") }
if rwUi.Writer != os.Stdout {
t.Fatal("default UI should go to stdout")
}
if rwUi.Reader != os.Stdin {
t.Fatal("default UI reader should go to stdin")
}
} }
func TestNewEnvironment_NoConfig(t *testing.T) { func TestNewEnvironment_NoConfig(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
env, err := NewEnvironment(nil) env, err := NewEnvironment(nil)
assert.Nil(env, "env should be nil") if env != nil {
assert.NotNil(err, "should be an error") t.Fatal("env should be nil")
}
if err == nil {
t.Fatal("should have error")
}
} }
func TestEnvironment_NilComponents(t *testing.T) { func TestEnvironment_NilComponents(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components = *new(ComponentFinder) config.Components = *new(ComponentFinder)
env, err := NewEnvironment(config) env, err := NewEnvironment(config)
assert.Nil(err, "should not have an error") if err != nil {
t.Fatalf("err: %s", err)
}
// All of these should not cause panics... so we don't assert // All of these should not cause panics... so we don't assert
// anything but if there is a panic in the test then yeah, something // anything but if there is a panic in the test then yeah, something
@ -90,8 +97,6 @@ func TestEnvironment_NilComponents(t *testing.T) {
} }
func TestEnvironment_Builder(t *testing.T) { func TestEnvironment_Builder(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
builder := &TestBuilder{} builder := &TestBuilder{}
builders := make(map[string]Builder) builders := make(map[string]Builder)
builders["foo"] = builder builders["foo"] = builder
@ -101,33 +106,43 @@ func TestEnvironment_Builder(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returnedBuilder, err := env.Builder("foo") returnedBuilder, err := env.Builder("foo")
assert.Nil(err, "should be no error") if err != nil {
assert.Equal(returnedBuilder, builder, "should return correct builder") t.Fatalf("err: %s", err)
}
if returnedBuilder != builder {
t.Fatalf("bad: %#v", returnedBuilder)
}
} }
func TestEnvironment_Builder_NilError(t *testing.T) { func TestEnvironment_Builder_NilError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Builder = func(n string) (Builder, error) { return nil, nil } config.Components.Builder = func(n string) (Builder, error) { return nil, nil }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returnedBuilder, err := env.Builder("foo") returnedBuilder, err := env.Builder("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Nil(returnedBuilder, "should be no builder") t.Fatal("should have error")
}
if returnedBuilder != nil {
t.Fatalf("bad: %#v", returnedBuilder)
}
} }
func TestEnvironment_Builder_Error(t *testing.T) { func TestEnvironment_Builder_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Builder = func(n string) (Builder, error) { return nil, errors.New("foo") } config.Components.Builder = func(n string) (Builder, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returnedBuilder, err := env.Builder("foo") returnedBuilder, err := env.Builder("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Equal(err.Error(), "foo", "should be correct error") t.Fatal("should have error")
assert.Nil(returnedBuilder, "should be no builder") }
if err.Error() != "foo" {
t.Fatalf("bad err: %s", err)
}
if returnedBuilder != nil {
t.Fatalf("should be nil: %#v", returnedBuilder)
}
} }
func TestEnvironment_Cache(t *testing.T) { func TestEnvironment_Cache(t *testing.T) {
@ -139,20 +154,20 @@ func TestEnvironment_Cache(t *testing.T) {
} }
func TestEnvironment_Cli_Error(t *testing.T) { func TestEnvironment_Cli_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Command = func(n string) (Command, error) { return nil, errors.New("foo") } config.Components.Command = func(n string) (Command, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
_, err := env.Cli([]string{"foo"}) _, err := env.Cli([]string{"foo"})
assert.NotNil(err, "should be an error") if err == nil {
assert.Equal(err.Error(), "foo", "should be correct error") t.Fatal("should have error")
}
if err.Error() != "foo" {
t.Fatalf("bad: %s", err)
}
} }
func TestEnvironment_Cli_CallsRun(t *testing.T) { func TestEnvironment_Cli_CallsRun(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := &TestCommand{} command := &TestCommand{}
commands := make(map[string]Command) commands := make(map[string]Command)
commands["foo"] = command commands["foo"] = command
@ -163,25 +178,33 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
exitCode, err := env.Cli([]string{"foo", "bar", "baz"}) exitCode, err := env.Cli([]string{"foo", "bar", "baz"})
assert.Nil(err, "should be no error") if err != nil {
assert.Equal(exitCode, 0, "runs foo command") t.Fatalf("err: %s", err)
assert.True(command.runCalled, "run should've been called") }
assert.Equal(command.runEnv, env, "should've ran with env") if exitCode != 0 {
assert.Equal(command.runArgs, []string{"bar", "baz"}, "should have right args") t.Fatalf("bad: %d", exitCode)
}
if !command.runCalled {
t.Fatal("command should be run")
}
if command.runEnv != env {
t.Fatalf("bad env: %#v", command.runEnv)
}
if !reflect.DeepEqual(command.runArgs, []string{"bar", "baz"}) {
t.Fatalf("bad: %#v", command.runArgs)
}
} }
func TestEnvironment_DefaultCli_Empty(t *testing.T) { func TestEnvironment_DefaultCli_Empty(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := testEnvironment() defaultEnv := testEnvironment()
exitCode, _ := defaultEnv.Cli([]string{}) exitCode, _ := defaultEnv.Cli([]string{})
assert.Equal(exitCode, 1, "CLI with no args") if exitCode != 1 {
t.Fatalf("bad: %d", exitCode)
}
} }
func TestEnvironment_DefaultCli_Help(t *testing.T) { func TestEnvironment_DefaultCli_Help(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := testEnvironment() defaultEnv := testEnvironment()
// A little lambda to help us test the output actually contains help // A little lambda to help us test the output actually contains help
@ -189,44 +212,52 @@ func TestEnvironment_DefaultCli_Help(t *testing.T) {
buffer := defaultEnv.Ui().(*BasicUi).Writer.(*bytes.Buffer) buffer := defaultEnv.Ui().(*BasicUi).Writer.(*bytes.Buffer)
output := buffer.String() output := buffer.String()
buffer.Reset() buffer.Reset()
assert.True(strings.Contains(output, "usage: packer"), "should print help") if !strings.Contains(output, "usage: packer") {
t.Fatalf("should contain help: %#v", output)
}
} }
// Test "--help" // Test "--help"
exitCode, _ := defaultEnv.Cli([]string{"--help"}) exitCode, _ := defaultEnv.Cli([]string{"--help"})
assert.Equal(exitCode, 1, "--help should print") if exitCode != 1 {
t.Fatalf("bad: %d", exitCode)
}
testOutput() testOutput()
// Test "-h" // Test "-h"
exitCode, _ = defaultEnv.Cli([]string{"--help"}) exitCode, _ = defaultEnv.Cli([]string{"--help"})
assert.Equal(exitCode, 1, "--help should print") if exitCode != 1 {
t.Fatalf("bad: %d", exitCode)
}
testOutput() testOutput()
} }
func TestEnvironment_DefaultCli_Version(t *testing.T) { func TestEnvironment_DefaultCli_Version(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := testEnvironment() defaultEnv := testEnvironment()
versionCommands := []string{"version", "--version", "-v"} versionCommands := []string{"version", "--version", "-v"}
for _, command := range versionCommands { for _, command := range versionCommands {
exitCode, _ := defaultEnv.Cli([]string{command}) exitCode, _ := defaultEnv.Cli([]string{command})
assert.Equal(exitCode, 0, fmt.Sprintf("%s should work", command)) if exitCode != 0 {
t.Fatalf("bad: %d", exitCode)
}
// Test the --version and -v can appear anywhere // Test the --version and -v can appear anywhere
exitCode, _ = defaultEnv.Cli([]string{"bad", command}) exitCode, _ = defaultEnv.Cli([]string{"bad", command})
if command != "version" { if command != "version" {
assert.Equal(exitCode, 0, fmt.Sprintf("%s should work anywhere", command)) if exitCode != 0 {
t.Fatalf("bad: %d", exitCode)
}
} else { } else {
assert.Equal(exitCode, 1, fmt.Sprintf("%s should NOT work anywhere", command)) if exitCode != 1 {
t.Fatalf("bad: %d", exitCode)
}
} }
} }
} }
func TestEnvironment_Hook(t *testing.T) { func TestEnvironment_Hook(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
hook := &MockHook{} hook := &MockHook{}
hooks := make(map[string]Hook) hooks := make(map[string]Hook)
hooks["foo"] = hook hooks["foo"] = hook
@ -236,38 +267,46 @@ func TestEnvironment_Hook(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.Hook("foo") returned, err := env.Hook("foo")
assert.Nil(err, "should be no error") if err != nil {
assert.Equal(returned, hook, "should return correct hook") t.Fatalf("err: %s", err)
}
if returned != hook {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironment_Hook_NilError(t *testing.T) { func TestEnvironment_Hook_NilError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Hook = func(n string) (Hook, error) { return nil, nil } config.Components.Hook = func(n string) (Hook, error) { return nil, nil }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.Hook("foo") returned, err := env.Hook("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Nil(returned, "should be no hook") t.Fatal("should have error")
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironment_Hook_Error(t *testing.T) { func TestEnvironment_Hook_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Hook = func(n string) (Hook, error) { return nil, errors.New("foo") } config.Components.Hook = func(n string) (Hook, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.Hook("foo") returned, err := env.Hook("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Equal(err.Error(), "foo", "should be correct error") t.Fatal("should have error")
assert.Nil(returned, "should be no hook") }
if err.Error() != "foo" {
t.Fatalf("err: %s", err)
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironment_PostProcessor(t *testing.T) { func TestEnvironment_PostProcessor(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
pp := &TestPostProcessor{} pp := &TestPostProcessor{}
pps := make(map[string]PostProcessor) pps := make(map[string]PostProcessor)
pps["foo"] = pp pps["foo"] = pp
@ -277,38 +316,46 @@ func TestEnvironment_PostProcessor(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.PostProcessor("foo") returned, err := env.PostProcessor("foo")
assert.Nil(err, "should be no error") if err != nil {
assert.Equal(returned, pp, "should return correct pp") t.Fatalf("err: %s", err)
}
if returned != pp {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironment_PostProcessor_NilError(t *testing.T) { func TestEnvironment_PostProcessor_NilError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil } config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.PostProcessor("foo") returned, err := env.PostProcessor("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Nil(returned, "should be no pp") t.Fatal("should have error")
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironment_PostProcessor_Error(t *testing.T) { func TestEnvironment_PostProcessor_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") } config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.PostProcessor("foo") returned, err := env.PostProcessor("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Equal(err.Error(), "foo", "should be correct error") t.Fatal("should be an error")
assert.Nil(returned, "should be no pp") }
if err.Error() != "foo" {
t.Fatalf("bad err: %s", err)
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironmentProvisioner(t *testing.T) { func TestEnvironmentProvisioner(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
p := &MockProvisioner{} p := &MockProvisioner{}
ps := make(map[string]Provisioner) ps := make(map[string]Provisioner)
ps["foo"] = p ps["foo"] = p
@ -318,25 +365,29 @@ func TestEnvironmentProvisioner(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.Provisioner("foo") returned, err := env.Provisioner("foo")
assert.Nil(err, "should be no error") if err != nil {
assert.Equal(returned, p, "should return correct provisioner") t.Fatalf("err: %s", err)
}
if returned != p {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironmentProvisioner_NilError(t *testing.T) { func TestEnvironmentProvisioner_NilError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Provisioner = func(n string) (Provisioner, error) { return nil, nil } config.Components.Provisioner = func(n string) (Provisioner, error) { return nil, nil }
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.Provisioner("foo") returned, err := env.Provisioner("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Nil(returned, "should be no provisioner") t.Fatal("should have error")
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironmentProvisioner_Error(t *testing.T) { func TestEnvironmentProvisioner_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig() config := DefaultEnvironmentConfig()
config.Components.Provisioner = func(n string) (Provisioner, error) { config.Components.Provisioner = func(n string) (Provisioner, error) {
return nil, errors.New("foo") return nil, errors.New("foo")
@ -344,14 +395,18 @@ func TestEnvironmentProvisioner_Error(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
returned, err := env.Provisioner("foo") returned, err := env.Provisioner("foo")
assert.NotNil(err, "should be an error") if err == nil {
assert.Equal(err.Error(), "foo", "should be correct error") t.Fatal("should have error")
assert.Nil(returned, "should be no provisioner") }
if err.Error() != "foo" {
t.Fatalf("err: %s", err)
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
} }
func TestEnvironment_SettingUi(t *testing.T) { func TestEnvironment_SettingUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
ui := &BasicUi{ ui := &BasicUi{
Reader: new(bytes.Buffer), Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer), Writer: new(bytes.Buffer),
@ -362,5 +417,7 @@ func TestEnvironment_SettingUi(t *testing.T) {
env, _ := NewEnvironment(config) env, _ := NewEnvironment(config)
assert.Equal(env.Ui(), ui, "UIs should be equal") if env.Ui() != ui {
t.Fatalf("UI should be equal: %#v", env.Ui())
}
} }

View File

@ -1,7 +1,6 @@
package packer package packer
import ( import (
"cgl.tideland.biz/asserts"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -42,12 +41,7 @@ func (h *CancelHook) Cancel() {
} }
func TestDispatchHook_Implements(t *testing.T) { func TestDispatchHook_Implements(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) var _ Hook = new(DispatchHook)
var r Hook
c := &DispatchHook{}
assert.Implementor(c, &r, "should be a Hook")
} }
func TestDispatchHook_Run_NoHooks(t *testing.T) { func TestDispatchHook_Run_NoHooks(t *testing.T) {
@ -57,8 +51,6 @@ func TestDispatchHook_Run_NoHooks(t *testing.T) {
} }
func TestDispatchHook_Run(t *testing.T) { func TestDispatchHook_Run(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
hook := &MockHook{} hook := &MockHook{}
mapping := make(map[string][]Hook) mapping := make(map[string][]Hook)
@ -66,9 +58,15 @@ func TestDispatchHook_Run(t *testing.T) {
dh := &DispatchHook{Mapping: mapping} dh := &DispatchHook{Mapping: mapping}
dh.Run("foo", nil, nil, 42) dh.Run("foo", nil, nil, 42)
assert.True(hook.RunCalled, "run should be called") if !hook.RunCalled {
assert.Equal(hook.RunName, "foo", "should be proper event") t.Fatal("should be called")
assert.Equal(hook.RunData, 42, "should be correct data") }
if hook.RunName != "foo" {
t.Fatalf("bad: %s", hook.RunName)
}
if hook.RunData != 42 {
t.Fatalf("bad: %#v", hook.RunData)
}
} }
func TestDispatchHook_cancel(t *testing.T) { func TestDispatchHook_cancel(t *testing.T) {

View File

@ -1,7 +1,6 @@
package packer package packer
import ( import (
"cgl.tideland.biz/asserts"
"errors" "errors"
"testing" "testing"
) )
@ -15,8 +14,6 @@ func TestMultiError_Impl(t *testing.T) {
} }
func TestMultiErrorError(t *testing.T) { func TestMultiErrorError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
expected := `2 error(s) occurred: expected := `2 error(s) occurred:
* foo * foo
@ -28,7 +25,9 @@ func TestMultiErrorError(t *testing.T) {
} }
multi := &MultiError{errors} multi := &MultiError{errors}
assert.Equal(multi.Error(), expected, "should have proper error") if multi.Error() != expected {
t.Fatalf("bad: %s", multi.Error())
}
} }
func TestMultiErrorAppend_MultiError(t *testing.T) { func TestMultiErrorAppend_MultiError(t *testing.T) {

View File

@ -1,7 +1,6 @@
package packer package packer
import ( import (
"cgl.tideland.biz/asserts"
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
@ -94,8 +93,6 @@ func TestParseTemplateFile_stdin(t *testing.T) {
} }
func TestParseTemplate_Basic(t *testing.T) { func TestParseTemplate_Basic(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [{"type": "something"}] "builders": [{"type": "something"}]
@ -103,14 +100,18 @@ func TestParseTemplate_Basic(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
assert.NotNil(result, "template should not be nil") t.Fatalf("err: %s", err)
assert.Length(result.Builders, 1, "one builder") }
if result == nil {
t.Fatal("should have result")
}
if len(result.Builders) != 1 {
t.Fatalf("bad: %#v", result.Builders)
}
} }
func TestParseTemplate_Invalid(t *testing.T) { func TestParseTemplate_Invalid(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Note there is an extra comma below for a purposeful // Note there is an extra comma below for a purposeful
// syntax error in the JSON. // syntax error in the JSON.
data := ` data := `
@ -120,13 +121,15 @@ func TestParseTemplate_Invalid(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have an error") if err == nil {
assert.Nil(result, "should have no result") t.Fatal("shold have error")
}
if result != nil {
t.Fatal("should not have result")
}
} }
func TestParseTemplate_InvalidKeys(t *testing.T) { func TestParseTemplate_InvalidKeys(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Note there is an extra comma below for a purposeful // Note there is an extra comma below for a purposeful
// syntax error in the JSON. // syntax error in the JSON.
data := ` data := `
@ -137,13 +140,15 @@ func TestParseTemplate_InvalidKeys(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have an error") if err == nil {
assert.Nil(result, "should have no result") t.Fatal("should have error")
}
if result != nil {
t.Fatal("should not have result")
}
} }
func TestParseTemplate_BuilderWithoutType(t *testing.T) { func TestParseTemplate_BuilderWithoutType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [{}] "builders": [{}]
@ -151,12 +156,12 @@ func TestParseTemplate_BuilderWithoutType(t *testing.T) {
` `
_, err := ParseTemplate([]byte(data)) _, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error") if err == nil {
t.Fatal("should have error")
}
} }
func TestParseTemplate_BuilderWithNonStringType(t *testing.T) { func TestParseTemplate_BuilderWithNonStringType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [{ "builders": [{
@ -166,12 +171,12 @@ func TestParseTemplate_BuilderWithNonStringType(t *testing.T) {
` `
_, err := ParseTemplate([]byte(data)) _, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error") if err == nil {
t.Fatal("should have error")
}
} }
func TestParseTemplate_BuilderWithoutName(t *testing.T) { func TestParseTemplate_BuilderWithoutName(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -183,18 +188,26 @@ func TestParseTemplate_BuilderWithoutName(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
assert.NotNil(result, "template should not be nil") t.Fatalf("err: %s", err)
assert.Length(result.Builders, 1, "should have one builder") }
if result == nil {
t.Fatal("should have result")
}
if len(result.Builders) != 1 {
t.Fatalf("bad: %#v", result.Builders)
}
builder, ok := result.Builders["amazon-ebs"] builder, ok := result.Builders["amazon-ebs"]
assert.True(ok, "should have amazon-ebs builder") if !ok {
assert.Equal(builder.Type, "amazon-ebs", "builder should be amazon-ebs") t.Fatal("should be ok")
}
if builder.Type != "amazon-ebs" {
t.Fatalf("bad: %#v", builder.Type)
}
} }
func TestParseTemplate_BuilderWithName(t *testing.T) { func TestParseTemplate_BuilderWithName(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -207,13 +220,23 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
assert.NotNil(result, "template should not be nil") t.Fatalf("err: %s", err)
assert.Length(result.Builders, 1, "should have one builder") }
if result == nil {
t.Fatal("should have result")
}
if len(result.Builders) != 1 {
t.Fatalf("bad: %#v", result.Builders)
}
builder, ok := result.Builders["bob"] builder, ok := result.Builders["bob"]
assert.True(ok, "should have bob builder") if !ok {
assert.Equal(builder.Type, "amazon-ebs", "builder should be amazon-ebs") t.Fatal("should be ok")
}
if builder.Type != "amazon-ebs" {
t.Fatalf("bad: %#v", builder.Type)
}
RawConfig := builder.RawConfig RawConfig := builder.RawConfig
if RawConfig == nil { if RawConfig == nil {
@ -230,8 +253,6 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
} }
func TestParseTemplate_BuilderWithConflictingName(t *testing.T) { func TestParseTemplate_BuilderWithConflictingName(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -248,12 +269,12 @@ func TestParseTemplate_BuilderWithConflictingName(t *testing.T) {
` `
_, err := ParseTemplate([]byte(data)) _, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error") if err == nil {
t.Fatal("should have error")
}
} }
func TestParseTemplate_Hooks(t *testing.T) { func TestParseTemplate_Hooks(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
@ -266,13 +287,23 @@ func TestParseTemplate_Hooks(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
assert.NotNil(result, "template should not be nil") t.Fatalf("err: %s", err)
assert.Length(result.Hooks, 1, "should have one hook") }
if result == nil {
t.Fatal("should have result")
}
if len(result.Hooks) != 1 {
t.Fatalf("bad: %#v", result.Hooks)
}
hooks, ok := result.Hooks["event"] hooks, ok := result.Hooks["event"]
assert.True(ok, "should have hook") if !ok {
assert.Equal(hooks, []string{"foo", "bar"}, "hooks should be correct") t.Fatal("should be okay")
}
if !reflect.DeepEqual(hooks, []string{"foo", "bar"}) {
t.Fatalf("bad: %#v", hooks)
}
} }
func TestParseTemplate_PostProcessors(t *testing.T) { func TestParseTemplate_PostProcessors(t *testing.T) {
@ -332,8 +363,6 @@ func TestParseTemplate_PostProcessors(t *testing.T) {
} }
func TestParseTemplate_ProvisionerWithoutType(t *testing.T) { func TestParseTemplate_ProvisionerWithoutType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [{"type": "foo"}], "builders": [{"type": "foo"}],
@ -343,12 +372,12 @@ func TestParseTemplate_ProvisionerWithoutType(t *testing.T) {
` `
_, err := ParseTemplate([]byte(data)) _, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error") if err == nil {
t.Fatal("err should not be nil")
}
} }
func TestParseTemplate_ProvisionerWithNonStringType(t *testing.T) { func TestParseTemplate_ProvisionerWithNonStringType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [{"type": "foo"}], "builders": [{"type": "foo"}],
@ -360,12 +389,12 @@ func TestParseTemplate_ProvisionerWithNonStringType(t *testing.T) {
` `
_, err := ParseTemplate([]byte(data)) _, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have error") if err == nil {
t.Fatal("should have error")
}
} }
func TestParseTemplate_Provisioners(t *testing.T) { func TestParseTemplate_Provisioners(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [{"type": "foo"}], "builders": [{"type": "foo"}],
@ -379,11 +408,21 @@ func TestParseTemplate_Provisioners(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
assert.NotNil(result, "template should not be nil") t.Fatal("err: %s", err)
assert.Length(result.Provisioners, 1, "should have one provisioner") }
assert.Equal(result.Provisioners[0].Type, "shell", "provisioner should be shell") if result == nil {
assert.NotNil(result.Provisioners[0].RawConfig, "should have raw config") t.Fatal("should have result")
}
if len(result.Provisioners) != 1 {
t.Fatalf("bad: %#v", result.Provisioners)
}
if result.Provisioners[0].Type != "shell" {
t.Fatalf("bad: %#v", result.Provisioners[0].Type)
}
if result.Provisioners[0].RawConfig == nil {
t.Fatal("should have raw config")
}
} }
func TestParseTemplate_Variables(t *testing.T) { func TestParseTemplate_Variables(t *testing.T) {
@ -451,8 +490,6 @@ func TestParseTemplate_variablesBadDefault(t *testing.T) {
} }
func TestTemplate_BuildNames(t *testing.T) { func TestTemplate_BuildNames(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -469,16 +506,18 @@ func TestTemplate_BuildNames(t *testing.T) {
` `
result, err := ParseTemplate([]byte(data)) result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
buildNames := result.BuildNames() buildNames := result.BuildNames()
sort.Strings(buildNames) sort.Strings(buildNames)
assert.Equal(buildNames, []string{"bob", "chris"}, "should have proper builds") if !reflect.DeepEqual(buildNames, []string{"bob", "chris"}) {
t.Fatalf("bad: %#v", buildNames)
}
} }
func TestTemplate_BuildUnknown(t *testing.T) { func TestTemplate_BuildUnknown(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -491,16 +530,20 @@ func TestTemplate_BuildUnknown(t *testing.T) {
` `
template, err := ParseTemplate([]byte(data)) template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("bad: %s", err)
}
build, err := template.Build("nope", nil) build, err := template.Build("nope", nil)
assert.Nil(build, "build should be nil") if build != nil {
assert.NotNil(err, "should have error") t.Fatalf("build should be nil: %#v", build)
}
if err == nil {
t.Fatal("should have error")
}
} }
func TestTemplate_BuildUnknownBuilder(t *testing.T) { func TestTemplate_BuildUnknownBuilder(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -513,18 +556,22 @@ func TestTemplate_BuildUnknownBuilder(t *testing.T) {
` `
template, err := ParseTemplate([]byte(data)) template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
builderFactory := func(string) (Builder, error) { return nil, nil } builderFactory := func(string) (Builder, error) { return nil, nil }
components := &ComponentFinder{Builder: builderFactory} components := &ComponentFinder{Builder: builderFactory}
build, err := template.Build("test1", components) build, err := template.Build("test1", components)
assert.Nil(build, "build should be nil") if err == nil {
assert.NotNil(err, "should have error") t.Fatal("should have error")
}
if build != nil {
t.Fatalf("bad: %#v", build)
}
} }
func TestTemplate_Build_NilBuilderFunc(t *testing.T) { func TestTemplate_Build_NilBuilderFunc(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -543,14 +590,18 @@ func TestTemplate_Build_NilBuilderFunc(t *testing.T) {
` `
template, err := ParseTemplate([]byte(data)) template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
defer func() { defer func() {
p := recover() p := recover()
assert.NotNil(p, "should panic") if p == nil {
t.Fatal("should panic")
}
if p != nil { if p.(string) != "no builder function" {
assert.Equal(p.(string), "no builder function", "right panic") t.Fatalf("bad panic: %s", p.(string))
} }
}() }()
@ -558,8 +609,6 @@ func TestTemplate_Build_NilBuilderFunc(t *testing.T) {
} }
func TestTemplate_Build_NilProvisionerFunc(t *testing.T) { func TestTemplate_Build_NilProvisionerFunc(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -578,14 +627,18 @@ func TestTemplate_Build_NilProvisionerFunc(t *testing.T) {
` `
template, err := ParseTemplate([]byte(data)) template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
defer func() { defer func() {
p := recover() p := recover()
assert.NotNil(p, "should panic") if p == nil {
t.Fatal("should panic")
}
if p != nil { if p.(string) != "no provisioner function" {
assert.Equal(p.(string), "no provisioner function", "right panic") t.Fatalf("bad panic: %s", p.(string))
} }
}() }()
@ -595,8 +648,6 @@ func TestTemplate_Build_NilProvisionerFunc(t *testing.T) {
} }
func TestTemplate_Build_NilProvisionerFunc_WithNoProvisioners(t *testing.T) { func TestTemplate_Build_NilProvisionerFunc_WithNoProvisioners(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -611,7 +662,9 @@ func TestTemplate_Build_NilProvisionerFunc_WithNoProvisioners(t *testing.T) {
` `
template, err := ParseTemplate([]byte(data)) template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
template.Build("test1", &ComponentFinder{ template.Build("test1", &ComponentFinder{
Builder: func(string) (Builder, error) { return nil, nil }, Builder: func(string) (Builder, error) { return nil, nil },
@ -619,8 +672,6 @@ func TestTemplate_Build_NilProvisionerFunc_WithNoProvisioners(t *testing.T) {
} }
func TestTemplate_Build(t *testing.T) { func TestTemplate_Build(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -651,7 +702,9 @@ func TestTemplate_Build(t *testing.T) {
} }
template, err := ParseTemplate([]byte(data)) template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
builder := testBuilder() builder := testBuilder()
builderMap := map[string]Builder{ builderMap := map[string]Builder{
@ -680,18 +733,40 @@ func TestTemplate_Build(t *testing.T) {
// Get the build, verifying we can get it without issue, but also // Get the build, verifying we can get it without issue, but also
// that the proper builder was looked up and used for the build. // that the proper builder was looked up and used for the build.
build, err := template.Build("test1", components) build, err := template.Build("test1", components)
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
coreBuild, ok := build.(*coreBuild) coreBuild, ok := build.(*coreBuild)
assert.True(ok, "should be a core build") if !ok {
assert.Equal(coreBuild.builder, builder, "should have the same builder") t.Fatal("should be ok")
assert.Equal(coreBuild.builderConfig, expectedConfig, "should have proper config") }
assert.Equal(len(coreBuild.provisioners), 1, "should have one provisioner") if coreBuild.builder != builder {
assert.Equal(len(coreBuild.postProcessors), 2, "should have pps") t.Fatalf("bad: %#v", coreBuild.builder)
assert.Equal(len(coreBuild.postProcessors[0]), 1, "should have correct number") }
assert.Equal(len(coreBuild.postProcessors[1]), 2, "should have correct number") if !reflect.DeepEqual(coreBuild.builderConfig, expectedConfig) {
assert.False(coreBuild.postProcessors[1][0].keepInputArtifact, "shoule be correct") t.Fatalf("bad: %#v", coreBuild.builderConfig)
assert.True(coreBuild.postProcessors[1][1].keepInputArtifact, "shoule be correct") }
if len(coreBuild.provisioners) != 1 {
t.Fatalf("bad: %#v", coreBuild.provisioners)
}
if len(coreBuild.postProcessors) != 2 {
t.Fatalf("bad: %#v", coreBuild.postProcessors)
}
if len(coreBuild.postProcessors[0]) != 1 {
t.Fatalf("bad: %#v", coreBuild.postProcessors[0])
}
if len(coreBuild.postProcessors[1]) != 2 {
t.Fatalf("bad: %#v", coreBuild.postProcessors[1])
}
if coreBuild.postProcessors[1][0].keepInputArtifact {
t.Fatal("postProcessors[1][0] should not keep input artifact")
}
if !coreBuild.postProcessors[1][1].keepInputArtifact {
t.Fatal("postProcessors[1][1] should keep input artifact")
}
config := coreBuild.postProcessors[1][1].config config := coreBuild.postProcessors[1][1].config
if _, ok := config["keep_input_artifact"]; ok { if _, ok := config["keep_input_artifact"]; ok {
@ -1080,8 +1155,6 @@ func TestTemplateBuild_onlyProv(t *testing.T) {
} }
func TestTemplate_Build_ProvisionerOverride(t *testing.T) { func TestTemplate_Build_ProvisionerOverride(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := ` data := `
{ {
"builders": [ "builders": [
@ -1141,12 +1214,20 @@ func TestTemplate_Build_ProvisionerOverride(t *testing.T) {
// Get the build, verifying we can get it without issue, but also // Get the build, verifying we can get it without issue, but also
// that the proper builder was looked up and used for the build. // that the proper builder was looked up and used for the build.
build, err := template.Build("test1", components) build, err := template.Build("test1", components)
assert.Nil(err, "should not error") if err != nil {
t.Fatalf("err: %s", err)
}
coreBuild, ok := build.(*coreBuild) coreBuild, ok := build.(*coreBuild)
assert.True(ok, "should be a core build") if !ok {
assert.Equal(len(coreBuild.provisioners), 1, "should have one provisioner") t.Fatal("should be okay")
assert.Equal(len(coreBuild.provisioners[0].config), 2, "should have two configs on the provisioner") }
if len(coreBuild.provisioners) != 1 {
t.Fatalf("bad: %#v", coreBuild.provisioners)
}
if len(coreBuild.provisioners[0].config) != 2 {
t.Fatalf("bad: %#v", coreBuild.provisioners[0].config)
}
} }
func TestTemplate_Build_ProvisionerOverrideBad(t *testing.T) { func TestTemplate_Build_ProvisionerOverrideBad(t *testing.T) {

View File

@ -2,7 +2,6 @@ package packer
import ( import (
"bytes" "bytes"
"cgl.tideland.biz/asserts"
"strings" "strings"
"testing" "testing"
) )
@ -38,25 +37,40 @@ func TestColoredUi(t *testing.T) {
} }
func TestTargettedUi(t *testing.T) { func TestTargettedUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi() bufferUi := testUi()
targettedUi := &TargettedUi{ targettedUi := &TargettedUi{
Target: "foo", Target: "foo",
Ui: bufferUi, Ui: bufferUi,
} }
var actual, expected string
targettedUi.Say("foo") targettedUi.Say("foo")
assert.Equal(readWriter(bufferUi), "==> foo: foo\n", "should have prefix") actual = readWriter(bufferUi)
expected = "==> foo: foo\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
targettedUi.Message("foo") targettedUi.Message("foo")
assert.Equal(readWriter(bufferUi), " foo: foo\n", "should have prefix") actual = readWriter(bufferUi)
expected = " foo: foo\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
targettedUi.Error("bar") targettedUi.Error("bar")
assert.Equal(readWriter(bufferUi), "==> foo: bar\n", "should have prefix") actual = readWriter(bufferUi)
expected = "==> foo: bar\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
targettedUi.Say("foo\nbar") targettedUi.Say("foo\nbar")
assert.Equal(readWriter(bufferUi), "==> foo: foo\n==> foo: bar\n", "should multiline") actual = readWriter(bufferUi)
expected = "==> foo: foo\n==> foo: bar\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
} }
func TestColoredUi_ImplUi(t *testing.T) { func TestColoredUi_ImplUi(t *testing.T) {
@ -84,27 +98,42 @@ func TestBasicUi_ImplUi(t *testing.T) {
} }
func TestBasicUi_Error(t *testing.T) { func TestBasicUi_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi() bufferUi := testUi()
var actual, expected string
bufferUi.Error("foo") bufferUi.Error("foo")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output") actual = readWriter(bufferUi)
expected = "foo\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
bufferUi.Error("5") bufferUi.Error("5")
assert.Equal(readWriter(bufferUi), "5\n", "formatting") actual = readWriter(bufferUi)
expected = "5\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
} }
func TestBasicUi_Say(t *testing.T) { func TestBasicUi_Say(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi() bufferUi := testUi()
var actual, expected string
bufferUi.Say("foo") bufferUi.Say("foo")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output") actual = readWriter(bufferUi)
expected = "foo\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
bufferUi.Say("5") bufferUi.Say("5")
assert.Equal(readWriter(bufferUi), "5\n", "formatting") actual = readWriter(bufferUi)
expected = "5\n"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
} }
func TestMachineReadableUi_ImplUi(t *testing.T) { func TestMachineReadableUi_ImplUi(t *testing.T) {