forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_stemcell_repo.go
181 lines (144 loc) · 4.62 KB
/
fake_stemcell_repo.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package fakes
import (
"fmt"
biconfig "github.com/cloudfoundry/bosh-cli/config"
bitestutils "github.com/cloudfoundry/bosh-cli/testutils"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type StemcellRepoSaveInput struct {
Name string
Version string
CID string
}
type StemcellRepoSaveOutput struct {
stemcellRecord biconfig.StemcellRecord
err error
}
type StemcellRepoFindInput struct {
Name string
Version string
}
type StemcellRepoFindOutput struct {
stemcellRecord biconfig.StemcellRecord
found bool
err error
}
type FindCurrentOutput struct {
stemcellRecord biconfig.StemcellRecord
found bool
err error
}
type FakeStemcellRepo struct {
SaveBehavior map[string]StemcellRepoSaveOutput
SaveInputs []StemcellRepoSaveInput
FindBehavior map[string]StemcellRepoFindOutput
FindInputs []StemcellRepoFindInput
UpdateCurrentRecordID string
UpdateCurrentErr error
findCurrentOutput FindCurrentOutput
ClearCurrentCalled bool
ClearCurrentErr error
DeleteStemcellRecords []biconfig.StemcellRecord
DeleteErr error
AllStemcellRecords []biconfig.StemcellRecord
AllErr error
}
func NewFakeStemcellRepo() *FakeStemcellRepo {
return &FakeStemcellRepo{
FindBehavior: map[string]StemcellRepoFindOutput{},
FindInputs: []StemcellRepoFindInput{},
SaveBehavior: map[string]StemcellRepoSaveOutput{},
SaveInputs: []StemcellRepoSaveInput{},
}
}
func (fr *FakeStemcellRepo) UpdateCurrent(recordID string) error {
fr.UpdateCurrentRecordID = recordID
return fr.UpdateCurrentErr
}
func (fr *FakeStemcellRepo) FindCurrent() (biconfig.StemcellRecord, bool, error) {
return fr.findCurrentOutput.stemcellRecord, fr.findCurrentOutput.found, fr.findCurrentOutput.err
}
func (fr *FakeStemcellRepo) ClearCurrent() error {
fr.ClearCurrentCalled = true
return fr.ClearCurrentErr
}
func (fr *FakeStemcellRepo) Delete(stemcellRecord biconfig.StemcellRecord) error {
fr.DeleteStemcellRecords = append(fr.DeleteStemcellRecords, stemcellRecord)
return fr.DeleteErr
}
func (fr *FakeStemcellRepo) All() ([]biconfig.StemcellRecord, error) {
return fr.AllStemcellRecords, fr.AllErr
}
func (fr *FakeStemcellRepo) Save(name, version, cid string) (biconfig.StemcellRecord, error) {
input := StemcellRepoSaveInput{
Name: name,
Version: version,
CID: cid,
}
fr.SaveInputs = append(fr.SaveInputs, input)
inputString, marshalErr := bitestutils.MarshalToString(input)
if marshalErr != nil {
return biconfig.StemcellRecord{}, bosherr.WrapError(marshalErr, "Marshaling Save input")
}
output, found := fr.SaveBehavior[inputString]
if !found {
return biconfig.StemcellRecord{}, fmt.Errorf("Unsupported Save Input: %s", inputString)
}
return output.stemcellRecord, output.err
}
func (fr *FakeStemcellRepo) SetSaveBehavior(name, version, cid string, stemcellRecord biconfig.StemcellRecord, err error) error {
input := StemcellRepoSaveInput{
Name: name,
Version: version,
CID: cid,
}
inputString, marshalErr := bitestutils.MarshalToString(input)
if marshalErr != nil {
return bosherr.WrapError(marshalErr, "Marshaling Save input")
}
fr.SaveBehavior[inputString] = StemcellRepoSaveOutput{
stemcellRecord: stemcellRecord,
err: err,
}
return nil
}
func (fr *FakeStemcellRepo) Find(name, version string) (biconfig.StemcellRecord, bool, error) {
input := StemcellRepoFindInput{
Name: name,
Version: version,
}
fr.FindInputs = append(fr.FindInputs, input)
inputString, marshalErr := bitestutils.MarshalToString(input)
if marshalErr != nil {
return biconfig.StemcellRecord{}, false, bosherr.WrapError(marshalErr, "Marshaling Find input")
}
output, found := fr.FindBehavior[inputString]
if !found {
return biconfig.StemcellRecord{}, false, fmt.Errorf("Unsupported Find Input: %s", inputString)
}
return output.stemcellRecord, output.found, output.err
}
func (fr *FakeStemcellRepo) SetFindBehavior(name, version string, foundRecord biconfig.StemcellRecord, found bool, err error) error {
input := StemcellRepoFindInput{
Name: name,
Version: version,
}
inputString, marshalErr := bitestutils.MarshalToString(input)
if marshalErr != nil {
return bosherr.WrapError(marshalErr, "Marshaling Find input")
}
fr.FindBehavior[inputString] = StemcellRepoFindOutput{
stemcellRecord: foundRecord,
found: found,
err: err,
}
return nil
}
func (fr *FakeStemcellRepo) SetFindCurrentBehavior(foundRecord biconfig.StemcellRecord, found bool, err error) error {
fr.findCurrentOutput = FindCurrentOutput{
stemcellRecord: foundRecord,
found: found,
err: err,
}
return nil
}