Commit 6d9265a2 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: add mock implementations and more template tests

parent 1e6f39c1
package command
import (
"github.com/mitchellh/packer/packer"
"testing"
)
func testTemplate() (*packer.Template, *packer.ComponentFinder) {
tplData := `{
"builders": [
{
"type": "foo"
},
{
"type": "bar"
}
]
}`
tpl, err := packer.ParseTemplate([]byte(tplData))
if err != nil {
panic(err)
}
cf := &packer.ComponentFinder{
Builder: func(string) (packer.Builder, error) { return new(packer.MockBuilder), nil },
}
return tpl, cf
}
func TestBuildOptionsBuilds(t *testing.T) {
opts := new(BuildOptions)
bs, err := opts.Builds(testTemplate())
if err != nil {
t.Fatalf("err: %s", err)
}
if len(bs) != 2 {
t.Fatalf("bad: %d", len(bs))
}
}
func TestBuildOptionsValidate(t *testing.T) {
bf := new(BuildOptions)
......
package packer
// MockArtifact is an implementation of Artifact that can be used for tests.
type MockArtifact struct {
IdValue string
DestroyCalled bool
}
func (*MockArtifact) BuilderId() string {
return "bid"
}
func (*MockArtifact) Files() []string {
return []string{"a", "b"}
}
func (a *MockArtifact) Id() string {
id := a.IdValue
if id == "" {
id = "id"
}
return id
}
func (*MockArtifact) String() string {
return "string"
}
func (a *MockArtifact) Destroy() error {
a.DestroyCalled = true
return nil
}
package packer
// MockBuilder is an implementation of Builder that can be used for tests.
// You can set some fake return values and you can keep track of what
// methods were called on the builder. It is fairly basic.
type MockBuilder struct {
ArtifactId string
PrepareCalled bool
PrepareConfig []interface{}
RunCalled bool
RunCache Cache
RunHook Hook
RunUi Ui
CancelCalled bool
}
func (tb *MockBuilder) Prepare(config ...interface{}) error {
tb.PrepareCalled = true
tb.PrepareConfig = config
return nil
}
func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
tb.RunCalled = true
tb.RunHook = h
tb.RunUi = ui
tb.RunCache = c
return &MockArtifact{
IdValue: tb.ArtifactId,
}, nil
}
func (tb *MockBuilder) Cancel() {
tb.CancelCalled = true
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment