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"
|
2013-05-07 23:47:56 -04:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2013-08-15 15:45:34 -04:00
|
|
|
func testComponentFinder() *ComponentFinder {
|
2013-11-02 23:31:12 -04:00
|
|
|
builderFactory := func(n string) (Builder, error) { return new(MockBuilder), nil }
|
2013-08-15 15:45:34 -04:00
|
|
|
ppFactory := func(n string) (PostProcessor, error) { return new(TestPostProcessor), nil }
|
2013-08-31 02:21:15 -04:00
|
|
|
provFactory := func(n string) (Provisioner, error) { return new(MockProvisioner), nil }
|
2013-08-15 15:45:34 -04:00
|
|
|
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()
|
2013-08-11 21:20:27 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-20 21:00:03 -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")
|
|
|
|
}
|
2013-04-20 21:00:03 -04:00
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
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")
|
|
|
|
}
|
2013-04-20 21:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2013-04-15 18:03:41 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:15:13 -04:00
|
|
|
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)
|
|
|
|
}
|
2013-05-11 02:15:13 -04:00
|
|
|
|
|
|
|
// 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")
|
2013-06-18 12:49:05 -04:00
|
|
|
env.PostProcessor("foo")
|
2013-05-22 18:39:30 -04:00
|
|
|
env.Provisioner("foo")
|
2013-05-11 02:15:13 -04:00
|
|
|
}
|
|
|
|
|
2013-05-05 18:12:55 -04:00
|
|
|
func TestEnvironment_Builder(t *testing.T) {
|
2013-11-02 23:31:12 -04:00
|
|
|
builder := &MockBuilder{}
|
2013-05-05 18:12:55 -04:00
|
|
|
builders := make(map[string]Builder)
|
|
|
|
builders["foo"] = builder
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
2013-05-11 02:15:13 -04:00
|
|
|
config.Components.Builder = func(n string) (Builder, error) { return builders[n], nil }
|
2013-05-05 18:12:55 -04:00
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
2013-05-07 23:42:49 -04:00
|
|
|
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)
|
|
|
|
}
|
2013-05-05 18:12:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-07 23:47:56 -04:00
|
|
|
func TestEnvironment_Builder_NilError(t *testing.T) {
|
|
|
|
config := DefaultEnvironmentConfig()
|
2013-05-11 02:15:13 -04:00
|
|
|
config.Components.Builder = func(n string) (Builder, error) { return nil, nil }
|
2013-05-07 23:47:56 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-05-07 23:47:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Builder_Error(t *testing.T) {
|
|
|
|
config := DefaultEnvironmentConfig()
|
2013-05-11 02:15:13 -04:00
|
|
|
config.Components.Builder = func(n string) (Builder, error) { return nil, errors.New("foo") }
|
2013-05-07 23:47:56 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-05-07 23:47:56 -04:00
|
|
|
}
|
|
|
|
|
2013-06-10 00:18:06 -04:00
|
|
|
func TestEnvironment_Cache(t *testing.T) {
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
if env.Cache() == nil {
|
|
|
|
t.Fatal("cache should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 02:15:13 -04:00
|
|
|
func TestEnvironment_Hook(t *testing.T) {
|
2013-08-30 20:03:55 -04:00
|
|
|
hook := &MockHook{}
|
2013-05-11 02:15:13 -04:00
|
|
|
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)
|
|
|
|
}
|
2013-05-11 02:15:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-05-11 02:15:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-05-11 02:15:13 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:49:05 -04:00
|
|
|
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)
|
|
|
|
}
|
2013-06-18 12:49:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-06-18 12:49:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-18 12:49:05 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 18:39:30 -04:00
|
|
|
func TestEnvironmentProvisioner(t *testing.T) {
|
2013-08-31 02:21:15 -04:00
|
|
|
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) {
|
2013-08-11 21:20:27 -04:00
|
|
|
ui := &BasicUi{
|
2013-06-14 18:17:03 -04:00
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
2013-04-15 23:26:38 -04:00
|
|
|
|
|
|
|
config := &EnvironmentConfig{}
|
2013-04-20 20:08:43 -04:00
|
|
|
config.Ui = ui
|
2013-04-15 23:26:38 -04:00
|
|
|
|
2013-04-20 21:00:03 -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
|
|
|
}
|