packer-cn/packer/build_test.go

377 lines
9.2 KiB
Go
Raw Normal View History

2013-04-20 21:55:02 -04:00
package packer
import (
"reflect"
2013-04-20 21:55:02 -04:00
"testing"
)
func testBuild() *coreBuild {
2013-05-03 23:45:38 -04:00
return &coreBuild{
name: "test",
builder: &MockBuilder{ArtifactId: "b"},
builderConfig: 42,
builderType: "foo",
hooks: map[string][]Hook{
2013-08-30 20:03:55 -04:00
"foo": []Hook{&MockHook{}},
},
provisioners: []coreBuildProvisioner{
coreBuildProvisioner{&MockProvisioner{}, []interface{}{42}},
},
2013-06-18 13:31:52 -04:00
postProcessors: [][]coreBuildPostProcessor{
[]coreBuildPostProcessor{
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp"}, "testPP", make(map[string]interface{}), true},
2013-06-18 13:31:52 -04:00
},
},
variables: make(map[string]string),
2013-04-20 21:55:02 -04:00
}
}
func testDefaultPackerConfig() map[string]interface{} {
return map[string]interface{}{
BuildNameConfigKey: "test",
BuilderTypeConfigKey: "foo",
DebugConfigKey: false,
ForceConfigKey: false,
UserVariablesConfigKey: make(map[string]string),
}
}
2013-05-09 14:32:03 -04:00
func TestBuild_Name(t *testing.T) {
build := testBuild()
2013-10-17 03:09:27 -04:00
if build.Name() != "test" {
t.Fatalf("bad: %s", build.Name())
}
2013-05-09 14:32:03 -04:00
}
2013-04-20 21:55:02 -04:00
func TestBuild_Prepare(t *testing.T) {
packerConfig := testDefaultPackerConfig()
2013-04-20 21:55:02 -04:00
build := testBuild()
builder := build.builder.(*MockBuilder)
build.Prepare()
if !builder.PrepareCalled {
2013-10-17 03:09:27 -04:00
t.Fatal("should be called")
}
if !reflect.DeepEqual(builder.PrepareConfig, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", builder.PrepareConfig)
2013-10-17 03:09:27 -04:00
}
coreProv := build.provisioners[0]
prov := coreProv.provisioner.(*MockProvisioner)
2013-10-17 03:09:27 -04:00
if !prov.PrepCalled {
t.Fatal("prep should be called")
}
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", prov.PrepConfigs)
}
2013-06-18 13:31:52 -04:00
corePP := build.postProcessors[0][0]
2013-06-18 13:31:52 -04:00
pp := corePP.processor.(*TestPostProcessor)
2013-10-17 03:09:27 -04:00
if !pp.configCalled {
t.Fatal("should be called")
}
if !reflect.DeepEqual(pp.configVal, []interface{}{make(map[string]interface{}), packerConfig}) {
t.Fatalf("bad: %#v", pp.configVal)
}
}
func TestBuild_Prepare_Twice(t *testing.T) {
build := testBuild()
warn, err := build.Prepare()
if len(warn) > 0 {
t.Fatalf("bad: %#v", warn)
}
if err != nil {
t.Fatalf("bad error: %s", err)
}
defer func() {
p := recover()
if p == nil {
t.Fatalf("should've paniced")
}
if p.(string) != "prepare already called" {
t.Fatalf("Invalid panic: %s", p)
}
}()
build.Prepare()
}
func TestBuildPrepare_BuilderWarniings(t *testing.T) {
expected := []string{"foo"}
build := testBuild()
builder := build.builder.(*MockBuilder)
builder.PrepareWarnings = expected
warn, err := build.Prepare()
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(warn, expected) {
t.Fatalf("bad: %#v", warn)
}
}
func TestBuild_Prepare_Debug(t *testing.T) {
packerConfig := testDefaultPackerConfig()
packerConfig[DebugConfigKey] = true
build := testBuild()
builder := build.builder.(*MockBuilder)
2013-04-20 21:55:02 -04:00
build.SetDebug(true)
build.Prepare()
if !builder.PrepareCalled {
2013-10-17 03:09:27 -04:00
t.Fatalf("should be called")
}
if !reflect.DeepEqual(builder.PrepareConfig, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", builder.PrepareConfig)
2013-10-17 03:09:27 -04:00
}
coreProv := build.provisioners[0]
prov := coreProv.provisioner.(*MockProvisioner)
2013-10-17 03:09:27 -04:00
if !prov.PrepCalled {
t.Fatal("prepare should be called")
}
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig}) {
t.Fatalf("bad: %#v", prov.PrepConfigs)
}
2013-04-20 21:55:02 -04:00
}
func TestBuildPrepare_variables_default(t *testing.T) {
packerConfig := testDefaultPackerConfig()
packerConfig[UserVariablesConfigKey] = map[string]string{
"foo": "bar",
}
build := testBuild()
build.variables["foo"] = "bar"
builder := build.builder.(*MockBuilder)
warn, err := build.Prepare()
if len(warn) > 0 {
t.Fatalf("bad: %#v", warn)
}
if err != nil {
t.Fatalf("err: %s", err)
}
if !builder.PrepareCalled {
t.Fatal("prepare should be called")
}
if !reflect.DeepEqual(builder.PrepareConfig[1], packerConfig) {
t.Fatalf("prepare bad: %#v", builder.PrepareConfig[1])
}
}
2013-04-20 21:55:02 -04:00
func TestBuild_Run(t *testing.T) {
2013-06-10 01:00:47 -04:00
cache := &TestCache{}
2013-04-20 21:55:02 -04:00
ui := testUi()
build := testBuild()
build.Prepare()
2013-06-18 13:54:22 -04:00
artifacts, err := build.Run(ui, cache)
2013-10-17 03:09:27 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
if len(artifacts) != 2 {
t.Fatalf("bad: %#v", artifacts)
}
2013-04-20 21:55:02 -04:00
// Verify builder was run
builder := build.builder.(*MockBuilder)
if !builder.RunCalled {
2013-10-17 03:09:27 -04:00
t.Fatal("should be called")
}
// Verify hooks are disapatchable
dispatchHook := builder.RunHook
dispatchHook.Run("foo", nil, nil, 42)
2013-08-30 20:03:55 -04:00
hook := build.hooks["foo"][0].(*MockHook)
2013-10-17 03:09:27 -04:00
if !hook.RunCalled {
t.Fatal("should be called")
}
if hook.RunData != 42 {
t.Fatalf("bad: %#v", hook.RunData)
}
// Verify provisioners run
dispatchHook.Run(HookProvision, nil, nil, 42)
prov := build.provisioners[0].provisioner.(*MockProvisioner)
2013-10-17 03:09:27 -04:00
if !prov.ProvCalled {
t.Fatal("should be called")
}
2013-06-18 13:54:22 -04:00
// Verify post-processor was run
pp := build.postProcessors[0][0].processor.(*TestPostProcessor)
2013-10-17 03:09:27 -04:00
if !pp.ppCalled {
t.Fatal("should be called")
}
2013-04-20 21:55:02 -04:00
}
2013-04-20 22:03:53 -04:00
func TestBuild_Run_Artifacts(t *testing.T) {
cache := &TestCache{}
ui := testUi()
// Test case: Test that with no post-processors, we only get the
// main build.
build := testBuild()
build.postProcessors = [][]coreBuildPostProcessor{}
build.Prepare()
artifacts, err := build.Run(ui, cache)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds := []string{"b"}
artifactIds := make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
// Test case: Test that with a single post-processor that doesn't keep
// inputs, only that post-processors results are returned.
build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{
[]coreBuildPostProcessor{
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp"}, "pp", make(map[string]interface{}), false},
},
}
build.Prepare()
artifacts, err = build.Run(ui, cache)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds = []string{"pp"}
artifactIds = make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
// Test case: Test that with multiple post-processors, as long as one
// keeps the original, the original is kept.
build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{
[]coreBuildPostProcessor{
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp1"}, "pp", make(map[string]interface{}), false},
},
[]coreBuildPostProcessor{
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp2"}, "pp", make(map[string]interface{}), true},
},
}
build.Prepare()
artifacts, err = build.Run(ui, cache)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds = []string{"b", "pp1", "pp2"}
artifactIds = make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
// Test case: Test that with sequences, intermediaries are kept if they
// want to be.
build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{
[]coreBuildPostProcessor{
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp1a"}, "pp", make(map[string]interface{}), false},
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp1b"}, "pp", make(map[string]interface{}), true},
},
[]coreBuildPostProcessor{
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp2a"}, "pp", make(map[string]interface{}), false},
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp2b"}, "pp", make(map[string]interface{}), false},
},
}
build.Prepare()
artifacts, err = build.Run(ui, cache)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds = []string{"pp1a", "pp1b", "pp2b"}
artifactIds = make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
// Test case: Test that with a single post-processor that forcibly
// keeps inputs, that the artifacts are kept.
build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{
[]coreBuildPostProcessor{
coreBuildPostProcessor{
&TestPostProcessor{artifactId: "pp", keep: true}, "pp", make(map[string]interface{}), false,
},
},
}
build.Prepare()
artifacts, err = build.Run(ui, cache)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds = []string{"b", "pp"}
artifactIds = make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
}
2013-04-20 22:03:53 -04:00
func TestBuild_RunBeforePrepare(t *testing.T) {
defer func() {
p := recover()
2013-10-17 03:09:27 -04:00
if p == nil {
t.Fatal("should panic")
}
if p.(string) != "Prepare must be called first" {
t.Fatalf("bad: %s", p.(string))
}
2013-04-20 22:03:53 -04:00
}()
2013-06-10 01:00:47 -04:00
testBuild().Run(testUi(), &TestCache{})
2013-04-20 22:03:53 -04:00
}
func TestBuild_Cancel(t *testing.T) {
build := testBuild()
build.Cancel()
builder := build.builder.(*MockBuilder)
if !builder.CancelCalled {
2013-10-17 03:09:27 -04:00
t.Fatal("cancel should be called")
}
}