diff --git a/common/command/template_test.go b/common/command/template_test.go index ae1bd6ea3..d7db9f151 100644 --- a/common/command/template_test.go +++ b/common/command/template_test.go @@ -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) diff --git a/packer/artifact_mock.go b/packer/artifact_mock.go new file mode 100644 index 000000000..ec88007f3 --- /dev/null +++ b/packer/artifact_mock.go @@ -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 +} diff --git a/packer/builder_mock.go b/packer/builder_mock.go new file mode 100644 index 000000000..72167bd80 --- /dev/null +++ b/packer/builder_mock.go @@ -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 +}