From 547d9e759e5695496c0d2e17cfa6fe31918562e3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 25 May 2015 17:58:59 -0700 Subject: [PATCH] packer: test Build --- packer/core_test.go | 39 +++++++++++++++++++++++++++ packer/test-fixtures/build-basic.json | 5 ++++ packer/testing.go | 16 +++++++++++ 3 files changed, 60 insertions(+) create mode 100644 packer/test-fixtures/build-basic.json diff --git a/packer/core_test.go b/packer/core_test.go index d66a7786e..5935b1407 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -48,6 +48,36 @@ func TestCoreBuildNames(t *testing.T) { } } +func TestCoreBuild_basic(t *testing.T) { + config := TestCoreConfig(t) + testCoreTemplate(t, config, fixtureDir("build-basic.json")) + b := TestBuilder(t, config, "test") + core := TestCore(t, config) + + b.ArtifactId = "hello" + + build, err := core.Build("test") + if err != nil { + t.Fatalf("err: %s", err) + } + + if _, err := build.Prepare(); err != nil { + t.Fatalf("err: %s", err) + } + + artifact, err := build.Run(nil, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + if len(artifact) != 1 { + t.Fatalf("bad: %#v", artifact) + } + + if artifact[0].Id() != b.ArtifactId { + t.Fatalf("bad: %s", artifact[0].Id()) + } +} + func TestCoreValidate(t *testing.T) { cases := []struct { File string @@ -110,3 +140,12 @@ func testComponentFinder() *ComponentFinder { Provisioner: provFactory, } } + +func testCoreTemplate(t *testing.T, c *CoreConfig, p string) { + tpl, err := template.ParseFile(p) + if err != nil { + t.Fatalf("err: %s\n\n%s", p, err) + } + + c.Template = tpl +} diff --git a/packer/test-fixtures/build-basic.json b/packer/test-fixtures/build-basic.json new file mode 100644 index 000000000..d14f6cad3 --- /dev/null +++ b/packer/test-fixtures/build-basic.json @@ -0,0 +1,5 @@ +{ + "builders": [{ + "type": "test" + }] +} diff --git a/packer/testing.go b/packer/testing.go index 099119180..389b02f90 100644 --- a/packer/testing.go +++ b/packer/testing.go @@ -42,3 +42,19 @@ func TestCore(t *testing.T, c *CoreConfig) *Core { return core } + +// TestBuilder sets the builder with the name n to the component finder +// and returns the mock. +func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder { + var b MockBuilder + + c.Components.Builder = func(actual string) (Builder, error) { + if actual != n { + return nil, nil + } + + return &b, nil + } + + return &b +}