packer-cn/packer/environment_test.go

311 lines
7.3 KiB
Go
Raw Normal View History

2013-03-24 17:31:18 -04:00
package packer
2013-03-24 17:47:59 -04:00
import (
2013-04-15 23:26:38 -04:00
"bytes"
"errors"
2013-06-19 02:18:21 -04:00
"io/ioutil"
"log"
2013-03-24 17:47:59 -04:00
"os"
"testing"
)
2013-03-24 17:31:18 -04:00
2013-06-19 02:18:21 -04:00
func init() {
// Disable log output for tests
log.SetOutput(ioutil.Discard)
}
func testComponentFinder() *ComponentFinder {
builderFactory := func(n string) (Builder, error) { return new(MockBuilder), nil }
ppFactory := func(n string) (PostProcessor, error) { return new(TestPostProcessor), nil }
provFactory := func(n string) (Provisioner, error) { return new(MockProvisioner), nil }
return &ComponentFinder{
Builder: builderFactory,
PostProcessor: ppFactory,
Provisioner: provFactory,
}
}
2013-05-02 17:03:55 -04:00
func testEnvironment() Environment {
2013-05-05 17:47:17 -04:00
config := DefaultEnvironmentConfig()
config.Ui = &BasicUi{
2014-02-21 23:02:55 -05:00
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
2014-02-21 21:29:15 -05:00
ErrorWriter: new(bytes.Buffer),
2013-04-15 23:26:38 -04:00
}
env, err := NewEnvironment(config)
if err != nil {
panic(err)
}
return env
}
func TestEnvironment_DefaultConfig_Ui(t *testing.T) {
config := DefaultEnvironmentConfig()
2013-10-17 03:09:27 -04:00
if config.Ui == nil {
t.Fatal("config.Ui should not be nil")
}
rwUi, ok := config.Ui.(*BasicUi)
2013-10-17 03:09:27 -04:00
if !ok {
t.Fatal("default UI should be BasicUi")
}
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) {
env, err := NewEnvironment(nil)
2013-10-17 03:09:27 -04:00
if env != nil {
t.Fatal("env should be nil")
}
if err == nil {
t.Fatal("should have error")
}
}
func TestEnvironment_NilComponents(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components = *new(ComponentFinder)
env, err := NewEnvironment(config)
2013-10-17 03:09:27 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
// 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
// went wrong.
env.Builder("foo")
env.Hook("foo")
env.PostProcessor("foo")
2013-05-22 18:39:30 -04:00
env.Provisioner("foo")
}
func TestEnvironment_Builder(t *testing.T) {
builder := &MockBuilder{}
builders := make(map[string]Builder)
builders["foo"] = builder
config := DefaultEnvironmentConfig()
config.Components.Builder = func(n string) (Builder, error) { return builders[n], nil }
env, _ := NewEnvironment(config)
returnedBuilder, err := env.Builder("foo")
2013-10-17 03:09:27 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
if returnedBuilder != builder {
t.Fatalf("bad: %#v", returnedBuilder)
}
}
func TestEnvironment_Builder_NilError(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.Builder = func(n string) (Builder, error) { return nil, nil }
env, _ := NewEnvironment(config)
returnedBuilder, err := env.Builder("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
if returnedBuilder != nil {
t.Fatalf("bad: %#v", returnedBuilder)
}
}
func TestEnvironment_Builder_Error(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.Builder = func(n string) (Builder, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config)
returnedBuilder, err := env.Builder("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
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) {
config := DefaultEnvironmentConfig()
env, _ := NewEnvironment(config)
if env.Cache() == nil {
t.Fatal("cache should not be nil")
}
}
func TestEnvironment_Hook(t *testing.T) {
2013-08-30 20:03:55 -04:00
hook := &MockHook{}
hooks := make(map[string]Hook)
hooks["foo"] = hook
config := DefaultEnvironmentConfig()
config.Components.Hook = func(n string) (Hook, error) { return hooks[n], nil }
env, _ := NewEnvironment(config)
returned, err := env.Hook("foo")
2013-10-17 03:09:27 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
if returned != hook {
t.Fatalf("bad: %#v", returned)
}
}
func TestEnvironment_Hook_NilError(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.Hook = func(n string) (Hook, error) { return nil, nil }
env, _ := NewEnvironment(config)
returned, err := env.Hook("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
}
func TestEnvironment_Hook_Error(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.Hook = func(n string) (Hook, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config)
returned, err := env.Hook("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
if err.Error() != "foo" {
t.Fatalf("err: %s", err)
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
}
func TestEnvironment_PostProcessor(t *testing.T) {
pp := &TestPostProcessor{}
pps := make(map[string]PostProcessor)
pps["foo"] = pp
config := DefaultEnvironmentConfig()
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return pps[n], nil }
env, _ := NewEnvironment(config)
returned, err := env.PostProcessor("foo")
2013-10-17 03:09:27 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
if returned != pp {
t.Fatalf("bad: %#v", returned)
}
}
func TestEnvironment_PostProcessor_NilError(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil }
env, _ := NewEnvironment(config)
returned, err := env.PostProcessor("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
}
func TestEnvironment_PostProcessor_Error(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") }
env, _ := NewEnvironment(config)
returned, err := env.PostProcessor("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should be an error")
}
if err.Error() != "foo" {
t.Fatalf("bad err: %s", err)
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
}
2013-05-22 18:39:30 -04:00
func TestEnvironmentProvisioner(t *testing.T) {
p := &MockProvisioner{}
2013-05-22 18:39:30 -04:00
ps := make(map[string]Provisioner)
ps["foo"] = p
config := DefaultEnvironmentConfig()
config.Components.Provisioner = func(n string) (Provisioner, error) { return ps[n], nil }
env, _ := NewEnvironment(config)
returned, err := env.Provisioner("foo")
2013-10-17 03:09:27 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
if returned != p {
t.Fatalf("bad: %#v", returned)
}
2013-05-22 18:39:30 -04:00
}
func TestEnvironmentProvisioner_NilError(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.Provisioner = func(n string) (Provisioner, error) { return nil, nil }
env, _ := NewEnvironment(config)
returned, err := env.Provisioner("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
2013-05-22 18:39:30 -04:00
}
func TestEnvironmentProvisioner_Error(t *testing.T) {
config := DefaultEnvironmentConfig()
config.Components.Provisioner = func(n string) (Provisioner, error) {
return nil, errors.New("foo")
}
env, _ := NewEnvironment(config)
returned, err := env.Provisioner("foo")
2013-10-17 03:09:27 -04:00
if err == nil {
t.Fatal("should have error")
}
if err.Error() != "foo" {
t.Fatalf("err: %s", err)
}
if returned != nil {
t.Fatalf("bad: %#v", returned)
}
2013-05-22 18:39:30 -04:00
}
2013-04-15 23:26:38 -04:00
func TestEnvironment_SettingUi(t *testing.T) {
ui := &BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
}
2013-04-15 23:26:38 -04:00
config := &EnvironmentConfig{}
config.Ui = ui
2013-04-15 23:26:38 -04:00
env, _ := NewEnvironment(config)
2013-04-15 23:26:38 -04:00
2013-10-17 03:09:27 -04:00
if env.Ui() != ui {
t.Fatalf("UI should be equal: %#v", env.Ui())
}
2013-03-24 19:28:35 -04:00
}