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-03-24 17:47:59 -04:00
|
|
|
"cgl.tideland.biz/asserts"
|
2013-05-07 23:47:56 -04:00
|
|
|
"errors"
|
2013-05-07 23:42:49 -04:00
|
|
|
"fmt"
|
2013-06-19 02:18:21 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2013-03-24 17:47:59 -04:00
|
|
|
"os"
|
2013-04-20 20:30:09 -04:00
|
|
|
"strings"
|
2013-03-24 17:47:59 -04:00
|
|
|
"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 {
|
|
|
|
builderFactory := func(n string) (Builder, error) { return testBuilder(), nil }
|
|
|
|
ppFactory := func(n string) (PostProcessor, error) { return new(TestPostProcessor), nil }
|
|
|
|
provFactory := func(n string) (Provisioner, error) { return new(TestProvisioner), 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()
|
2013-08-11 21:20:27 -04:00
|
|
|
config.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
|
|
|
}
|
|
|
|
|
2013-04-20 21:00:03 -04:00
|
|
|
env, err := NewEnvironment(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2013-05-05 17:47:17 -04:00
|
|
|
func TestEnvironment_DefaultConfig_Commands(t *testing.T) {
|
2013-04-21 22:04:35 -04:00
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
2013-05-05 17:47:17 -04:00
|
|
|
assert.Empty(config.Commands, "should have no commands")
|
2013-04-21 22:04:35 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 21:00:03 -04:00
|
|
|
func TestEnvironment_DefaultConfig_Ui(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
assert.NotNil(config.Ui, "default UI should not be nil")
|
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
rwUi, ok := config.Ui.(*BasicUi)
|
|
|
|
assert.True(ok, "default UI should be BasicUi")
|
2013-04-20 21:00:03 -04:00
|
|
|
assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout")
|
|
|
|
assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewEnvironment_NoConfig(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
env, err := NewEnvironment(nil)
|
|
|
|
assert.Nil(env, "env should be nil")
|
|
|
|
assert.NotNil(err, "should be an error")
|
2013-04-15 18:03:41 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:15:13 -04:00
|
|
|
func TestEnvironment_NilComponents(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components = *new(ComponentFinder)
|
|
|
|
|
|
|
|
env, err := NewEnvironment(config)
|
|
|
|
assert.Nil(err, "should not have an error")
|
|
|
|
|
|
|
|
// 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.Cli([]string{"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) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
builder := &TestBuilder{}
|
|
|
|
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")
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
assert.Equal(returnedBuilder, builder, "should return correct builder")
|
2013-05-05 18:12:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-07 23:47:56 -04:00
|
|
|
func TestEnvironment_Builder_NilError(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
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")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Nil(returnedBuilder, "should be no builder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Builder_Error(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
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")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Equal(err.Error(), "foo", "should be correct error")
|
|
|
|
assert.Nil(returnedBuilder, "should be no builder")
|
|
|
|
}
|
|
|
|
|
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-07 23:50:22 -04:00
|
|
|
func TestEnvironment_Cli_Error(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
2013-05-11 02:15:13 -04:00
|
|
|
config.Components.Command = func(n string) (Command, error) { return nil, errors.New("foo") }
|
2013-05-07 23:50:22 -04:00
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
_, err := env.Cli([]string{"foo"})
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Equal(err.Error(), "foo", "should be correct error")
|
|
|
|
}
|
|
|
|
|
2013-04-15 18:03:41 -04:00
|
|
|
func TestEnvironment_Cli_CallsRun(t *testing.T) {
|
2013-04-20 20:17:53 -04:00
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
command := &TestCommand{}
|
2013-05-05 17:47:17 -04:00
|
|
|
commands := make(map[string]Command)
|
|
|
|
commands["foo"] = command
|
2013-04-20 20:17:53 -04:00
|
|
|
|
|
|
|
config := &EnvironmentConfig{}
|
2013-05-05 17:47:17 -04:00
|
|
|
config.Commands = []string{"foo"}
|
2013-05-11 02:15:13 -04:00
|
|
|
config.Components.Command = func(n string) (Command, error) { return commands[n], nil }
|
2013-04-15 18:03:41 -04:00
|
|
|
|
2013-04-20 21:00:03 -04:00
|
|
|
env, _ := NewEnvironment(config)
|
2013-05-07 23:42:49 -04:00
|
|
|
exitCode, err := env.Cli([]string{"foo", "bar", "baz"})
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
assert.Equal(exitCode, 0, "runs foo command")
|
2013-04-20 20:17:53 -04:00
|
|
|
assert.True(command.runCalled, "run should've been called")
|
2013-04-20 20:22:27 -04:00
|
|
|
assert.Equal(command.runEnv, env, "should've ran with env")
|
|
|
|
assert.Equal(command.runArgs, []string{"bar", "baz"}, "should have right args")
|
2013-04-15 18:03:41 -04:00
|
|
|
}
|
|
|
|
|
2013-03-24 17:47:59 -04:00
|
|
|
func TestEnvironment_DefaultCli_Empty(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := testEnvironment()
|
2013-03-24 17:47:59 -04:00
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
exitCode, _ := defaultEnv.Cli([]string{})
|
|
|
|
assert.Equal(exitCode, 1, "CLI with no args")
|
2013-03-24 17:47:59 -04:00
|
|
|
}
|
|
|
|
|
2013-03-24 19:35:33 -04:00
|
|
|
func TestEnvironment_DefaultCli_Help(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := testEnvironment()
|
2013-03-24 19:35:33 -04:00
|
|
|
|
2013-04-20 20:30:09 -04:00
|
|
|
// A little lambda to help us test the output actually contains help
|
|
|
|
testOutput := func() {
|
2013-08-11 21:20:27 -04:00
|
|
|
buffer := defaultEnv.Ui().(*BasicUi).Writer.(*bytes.Buffer)
|
2013-04-20 20:30:09 -04:00
|
|
|
output := buffer.String()
|
|
|
|
buffer.Reset()
|
|
|
|
assert.True(strings.Contains(output, "usage: packer"), "should print help")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test "--help"
|
2013-05-07 23:42:49 -04:00
|
|
|
exitCode, _ := defaultEnv.Cli([]string{"--help"})
|
|
|
|
assert.Equal(exitCode, 1, "--help should print")
|
2013-04-20 20:30:09 -04:00
|
|
|
testOutput()
|
|
|
|
|
|
|
|
// Test "-h"
|
2013-05-07 23:42:49 -04:00
|
|
|
exitCode, _ = defaultEnv.Cli([]string{"--help"})
|
|
|
|
assert.Equal(exitCode, 1, "--help should print")
|
2013-04-20 20:30:09 -04:00
|
|
|
testOutput()
|
2013-03-24 19:35:33 -04:00
|
|
|
}
|
|
|
|
|
2013-03-24 17:47:59 -04:00
|
|
|
func TestEnvironment_DefaultCli_Version(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := testEnvironment()
|
2013-03-24 17:47:59 -04:00
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
versionCommands := []string{"version", "--version", "-v"}
|
|
|
|
for _, command := range versionCommands {
|
|
|
|
exitCode, _ := defaultEnv.Cli([]string{command})
|
|
|
|
assert.Equal(exitCode, 0, fmt.Sprintf("%s should work", command))
|
2013-03-24 17:47:59 -04:00
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
// Test the --version and -v can appear anywhere
|
|
|
|
exitCode, _ = defaultEnv.Cli([]string{"bad", command})
|
2013-03-24 17:47:59 -04:00
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
if command != "version" {
|
|
|
|
assert.Equal(exitCode, 0, fmt.Sprintf("%s should work anywhere", command))
|
|
|
|
} else {
|
|
|
|
assert.Equal(exitCode, 1, fmt.Sprintf("%s should NOT work anywhere", command))
|
|
|
|
}
|
|
|
|
}
|
2013-03-24 17:47:59 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:15:13 -04:00
|
|
|
func TestEnvironment_Hook(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
hook := &TestHook{}
|
|
|
|
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")
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
assert.Equal(returned, hook, "should return correct hook")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Hook_NilError(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components.Hook = func(n string) (Hook, error) { return nil, nil }
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
returned, err := env.Hook("foo")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Nil(returned, "should be no hook")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Hook_Error(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components.Hook = func(n string) (Hook, error) { return nil, errors.New("foo") }
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
returned, err := env.Hook("foo")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Equal(err.Error(), "foo", "should be correct error")
|
|
|
|
assert.Nil(returned, "should be no hook")
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:49:05 -04:00
|
|
|
func TestEnvironment_PostProcessor(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
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")
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
assert.Equal(returned, pp, "should return correct pp")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_PostProcessor_NilError(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil }
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
returned, err := env.PostProcessor("foo")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Nil(returned, "should be no pp")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_PostProcessor_Error(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") }
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
returned, err := env.PostProcessor("foo")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Equal(err.Error(), "foo", "should be correct error")
|
|
|
|
assert.Nil(returned, "should be no pp")
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:39:30 -04:00
|
|
|
func TestEnvironmentProvisioner(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
p := &TestProvisioner{}
|
|
|
|
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")
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
assert.Equal(returned, p, "should return correct provisioner")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentProvisioner_NilError(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components.Provisioner = func(n string) (Provisioner, error) { return nil, nil }
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
returned, err := env.Provisioner("foo")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Nil(returned, "should be no provisioner")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentProvisioner_Error(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
config.Components.Provisioner = func(n string) (Provisioner, error) {
|
|
|
|
return nil, errors.New("foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
returned, err := env.Provisioner("foo")
|
|
|
|
assert.NotNil(err, "should be an error")
|
|
|
|
assert.Equal(err.Error(), "foo", "should be correct error")
|
|
|
|
assert.Nil(returned, "should be no provisioner")
|
|
|
|
}
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
func TestEnvironment_SettingUi(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
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
|
|
|
|
|
|
|
assert.Equal(env.Ui(), ui, "UIs should be equal")
|
2013-03-24 19:28:35 -04:00
|
|
|
}
|