forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_mock.go
58 lines (48 loc) · 1.13 KB
/
builder_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package packer
import (
"errors"
)
// 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
PrepareWarnings []string
RunErrResult bool
RunNilResult bool
PrepareCalled bool
PrepareConfig []interface{}
RunCalled bool
RunCache Cache
RunHook Hook
RunUi Ui
CancelCalled bool
}
func (tb *MockBuilder) Prepare(config ...interface{}) ([]string, error) {
tb.PrepareCalled = true
tb.PrepareConfig = config
return tb.PrepareWarnings, 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
if tb.RunErrResult {
return nil, errors.New("foo")
}
if tb.RunNilResult {
return nil, nil
}
if h != nil {
if err := h.Run(HookProvision, ui, new(MockCommunicator), nil); err != nil {
return nil, err
}
}
return &MockArtifact{
IdValue: tb.ArtifactId,
}, nil
}
func (tb *MockBuilder) Cancel() {
tb.CancelCalled = true
}