packer: test Build

This commit is contained in:
Mitchell Hashimoto 2015-05-25 17:58:59 -07:00
parent dc74ec5612
commit 547d9e759e
3 changed files with 60 additions and 0 deletions

View File

@ -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
}

View File

@ -0,0 +1,5 @@
{
"builders": [{
"type": "test"
}]
}

View File

@ -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
}