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"
|
|
|
|
"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-04-20 20:17:53 -04:00
|
|
|
type TestCommand struct {
|
2013-04-20 20:22:27 -04:00
|
|
|
runArgs []string
|
2013-04-20 20:17:53 -04:00
|
|
|
runCalled bool
|
2013-04-20 20:22:27 -04:00
|
|
|
runEnv *Environment
|
2013-04-20 20:17:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TestCommand) Run(env *Environment, args []string) int {
|
|
|
|
tc.runCalled = true
|
2013-04-20 20:22:27 -04:00
|
|
|
tc.runArgs = args
|
|
|
|
tc.runEnv = env
|
2013-04-20 20:17:53 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TestCommand) Synopsis() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2013-04-15 18:03:41 -04:00
|
|
|
func testEnvironment() *Environment {
|
2013-04-15 23:26:38 -04:00
|
|
|
config := &EnvironmentConfig{}
|
2013-04-20 20:09:54 -04:00
|
|
|
config.Ui = &ReaderWriterUi{
|
2013-04-15 23:26:38 -04:00
|
|
|
new(bytes.Buffer),
|
|
|
|
new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
|
2013-04-20 21:00:03 -04:00
|
|
|
env, err := NewEnvironment(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2013-04-21 22:04:35 -04:00
|
|
|
func TestEnvironment_DefaultConfig_Command(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
assert.NotNil(config.Command, "default Command should not be nil")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
rwUi, ok := config.Ui.(*ReaderWriterUi)
|
|
|
|
assert.True(ok, "default UI should be ReaderWriterUi")
|
|
|
|
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-04-20 21:31:42 -04:00
|
|
|
func TestEnvironment_GetBuilderFactory(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
|
|
|
|
assert.Equal(env.BuilderFactory(), config.BuilderFactory, "should match factories")
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
|
|
|
|
config := &EnvironmentConfig{}
|
|
|
|
config.Command = make(map[string]Command)
|
|
|
|
config.Command["foo"] = command
|
2013-04-15 18:03:41 -04:00
|
|
|
|
2013-04-20 21:00:03 -04:00
|
|
|
env, _ := NewEnvironment(config)
|
2013-04-20 20:22:27 -04:00
|
|
|
assert.Equal(env.Cli([]string{"foo", "bar", "baz"}), 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
|
|
|
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{}), 1, "CLI with no args")
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
buffer := defaultEnv.Ui().(*ReaderWriterUi).Writer.(*bytes.Buffer)
|
|
|
|
output := buffer.String()
|
|
|
|
buffer.Reset()
|
|
|
|
assert.True(strings.Contains(output, "usage: packer"), "should print help")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test "--help"
|
2013-03-24 19:35:33 -04:00
|
|
|
assert.Equal(defaultEnv.Cli([]string{"--help"}), 1, "--help should print")
|
2013-04-20 20:30:09 -04:00
|
|
|
testOutput()
|
|
|
|
|
|
|
|
// Test "-h"
|
2013-03-24 19:35:33 -04:00
|
|
|
assert.Equal(defaultEnv.Cli([]string{"-h"}), 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
|
|
|
|
|
|
|
// Test the basic version options
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"version"}), 0, "version should work")
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"--version"}), 0, "--version should work")
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"-v"}), 0, "-v should work")
|
|
|
|
|
|
|
|
// Test the --version and -v can appear anywhere
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"bad", "-v"}), 0, "-v should work anywhere")
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"bad", "--version"}), 0, "--version should work anywhere")
|
|
|
|
|
|
|
|
// Test that "version" can't appear anywhere
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"bad", "version"}), 1, "version should NOT work anywhere")
|
|
|
|
}
|
|
|
|
|
2013-03-24 19:28:35 -04:00
|
|
|
func TestEnvironment_PrintHelp(t *testing.T) {
|
|
|
|
// Just call the function and verify that no panics occur
|
2013-04-15 23:26:38 -04:00
|
|
|
testEnvironment().PrintHelp()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_SettingUi(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
ui := &ReaderWriterUi{new(bytes.Buffer), new(bytes.Buffer)}
|
|
|
|
|
|
|
|
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
|
|
|
}
|