packer-cn/packer/environment_test.go

145 lines
4.0 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"
2013-03-24 17:47:59 -04:00
"cgl.tideland.biz/asserts"
"os"
"strings"
2013-03-24 17:47:59 -04:00
"testing"
)
2013-03-24 17:31:18 -04:00
2013-05-02 17:03:55 -04:00
func testEnvironment() Environment {
2013-05-05 17:47:17 -04:00
config := DefaultEnvironmentConfig()
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),
}
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) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig()
2013-05-05 17:47:17 -04:00
assert.Empty(config.Commands, "should have no commands")
}
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")
}
func TestEnvironment_Builder(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
builder := &TestBuilder{}
builders := make(map[string]Builder)
builders["foo"] = builder
config := DefaultEnvironmentConfig()
config.BuilderFunc = func(n string) Builder { return builders[n] }
env, _ := NewEnvironment(config)
assert.Equal(env.Builder("foo"), builder, "should return correct builder")
}
func TestEnvironment_Cli_CallsRun(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := &TestCommand{}
2013-05-05 17:47:17 -04:00
commands := make(map[string]Command)
commands["foo"] = command
config := &EnvironmentConfig{}
2013-05-05 17:47:17 -04:00
config.Commands = []string{"foo"}
config.CommandFunc = func(n string) Command { return commands[n] }
env, _ := NewEnvironment(config)
2013-04-20 20:22:27 -04:00
assert.Equal(env.Cli([]string{"foo", "bar", "baz"}), 0, "runs foo command")
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-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")
}
func TestEnvironment_DefaultCli_Help(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
2013-04-15 23:26:38 -04:00
defaultEnv := testEnvironment()
// 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"
assert.Equal(defaultEnv.Cli([]string{"--help"}), 1, "--help should print")
testOutput()
// Test "-h"
assert.Equal(defaultEnv.Cli([]string{"-h"}), 1, "--help should print")
testOutput()
}
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-04-15 23:26:38 -04:00
func TestEnvironment_SettingUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
ui := &ReaderWriterUi{new(bytes.Buffer), new(bytes.Buffer)}
config := &EnvironmentConfig{}
config.Ui = ui
2013-04-15 23:26:38 -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
}