forked from cloudfoundry-attic/bosh-init
/
fake_templates_compiler.go
68 lines (54 loc) · 1.74 KB
/
fake_templates_compiler.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
59
60
61
62
63
64
65
66
67
68
package fakes
import (
"fmt"
"github.com/cloudfoundry-incubator/candiedyaml"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
bmdepl "github.com/cloudfoundry/bosh-micro-cli/deployment"
bmrel "github.com/cloudfoundry/bosh-micro-cli/release"
)
type CompileInput struct {
Jobs []bmrel.Job
Deployment bmdepl.Deployment
}
type compileOutput struct {
err error
}
type FakeTemplatesCompiler struct {
CompileInputs []CompileInput
compileBehavior map[string]compileOutput
}
func NewFakeTemplatesCompiler() *FakeTemplatesCompiler {
return &FakeTemplatesCompiler{
CompileInputs: []CompileInput{},
compileBehavior: map[string]compileOutput{},
}
}
func (f *FakeTemplatesCompiler) Compile(jobs []bmrel.Job, deployment bmdepl.Deployment) error {
input := CompileInput{Jobs: jobs, Deployment: deployment}
f.CompileInputs = append(f.CompileInputs, input)
inputString, err := marshalToString(input)
if err != nil {
return bosherr.WrapError(err, "Marshaling Save input")
}
output, found := f.compileBehavior[inputString]
if found {
return output.err
}
return fmt.Errorf("Unsupported Input: Save('%#v', '%#v')", jobs, deployment)
}
func (f *FakeTemplatesCompiler) SetCompileBehavior(jobs []bmrel.Job, deployment bmdepl.Deployment, err error) error {
input := CompileInput{Jobs: jobs, Deployment: deployment}
inputString, marshalErr := marshalToString(input)
if marshalErr != nil {
return bosherr.WrapError(marshalErr, "Marshaling Save input")
}
f.compileBehavior[inputString] = compileOutput{err: err}
return nil
}
func marshalToString(input interface{}) (string, error) {
bytes, err := candiedyaml.Marshal(input)
if err != nil {
return "", bosherr.WrapError(err, "Marshaling to string: %#v", input)
}
return string(bytes), nil
}