github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/packer/builder_mock.go (about) 1 package packer 2 3 import ( 4 "errors" 5 ) 6 7 // MockBuilder is an implementation of Builder that can be used for tests. 8 // You can set some fake return values and you can keep track of what 9 // methods were called on the builder. It is fairly basic. 10 type MockBuilder struct { 11 ArtifactId string 12 PrepareWarnings []string 13 RunErrResult bool 14 RunNilResult bool 15 16 PrepareCalled bool 17 PrepareConfig []interface{} 18 RunCalled bool 19 RunCache Cache 20 RunHook Hook 21 RunUi Ui 22 CancelCalled bool 23 } 24 25 func (tb *MockBuilder) Prepare(config ...interface{}) ([]string, error) { 26 tb.PrepareCalled = true 27 tb.PrepareConfig = config 28 return tb.PrepareWarnings, nil 29 } 30 31 func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) { 32 tb.RunCalled = true 33 tb.RunHook = h 34 tb.RunUi = ui 35 tb.RunCache = c 36 37 if tb.RunErrResult { 38 return nil, errors.New("foo") 39 } 40 41 if tb.RunNilResult { 42 return nil, nil 43 } 44 45 if h != nil { 46 if err := h.Run(HookProvision, ui, new(MockCommunicator), nil); err != nil { 47 return nil, err 48 } 49 } 50 51 return &MockArtifact{ 52 IdValue: tb.ArtifactId, 53 }, nil 54 } 55 56 func (tb *MockBuilder) Cancel() { 57 tb.CancelCalled = true 58 }