packer: add mock implementations and more template tests
This commit is contained in:
parent
1e6f39c1c3
commit
6d9265a244
|
@ -1,9 +1,46 @@
|
|||
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)
|
||||
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
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
|
||||
}
|
Loading…
Reference in New Issue