forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_mock.go
258 lines (211 loc) · 6.33 KB
/
driver_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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package googlecompute
import "fmt"
// DriverMock is a Driver implementation that is a mocked out so that
// it can be used for tests.
type DriverMock struct {
CreateImageName string
CreateImageDesc string
CreateImageFamily string
CreateImageZone string
CreateImageDisk string
CreateImageResultLicenses []string
CreateImageResultProjectId string
CreateImageResultSelfLink string
CreateImageResultSizeGb int64
CreateImageErrCh <-chan error
CreateImageResultCh <-chan *Image
DeleteImageName string
DeleteImageErrCh <-chan error
DeleteInstanceZone string
DeleteInstanceName string
DeleteInstanceErrCh <-chan error
DeleteInstanceErr error
DeleteDiskZone string
DeleteDiskName string
DeleteDiskErrCh <-chan error
DeleteDiskErr error
GetImageName string
GetImageFromFamily bool
GetImageResult *Image
GetImageErr error
GetImageFromProjectProject string
GetImageFromProjectName string
GetImageFromProjectFromFamily bool
GetImageFromProjectResult *Image
GetImageFromProjectErr error
GetInstanceMetadataZone string
GetInstanceMetadataName string
GetInstanceMetadataKey string
GetInstanceMetadataResult string
GetInstanceMetadataErr error
GetNatIPZone string
GetNatIPName string
GetNatIPResult string
GetNatIPErr error
GetInternalIPZone string
GetInternalIPName string
GetInternalIPResult string
GetInternalIPErr error
GetSerialPortOutputZone string
GetSerialPortOutputName string
GetSerialPortOutputResult string
GetSerialPortOutputErr error
ImageExistsName string
ImageExistsResult bool
RunInstanceConfig *InstanceConfig
RunInstanceErrCh <-chan error
RunInstanceErr error
CreateOrResetWindowsPasswordZone string
CreateOrResetWindowsPasswordInstance string
CreateOrResetWindowsPasswordConfig *WindowsPasswordConfig
CreateOrResetWindowsPasswordErr error
CreateOrResetWindowsPasswordErrCh <-chan error
WaitForInstanceState string
WaitForInstanceZone string
WaitForInstanceName string
WaitForInstanceErrCh <-chan error
}
func (d *DriverMock) CreateImage(name, description, family, zone, disk string) (<-chan *Image, <-chan error) {
d.CreateImageName = name
d.CreateImageDesc = description
d.CreateImageFamily = family
d.CreateImageZone = zone
d.CreateImageDisk = disk
if d.CreateImageResultProjectId == "" {
d.CreateImageResultProjectId = "test"
}
if d.CreateImageResultSelfLink == "" {
d.CreateImageResultSelfLink = fmt.Sprintf(
"http://content.googleapis.com/compute/v1/%s/global/licenses/test",
d.CreateImageResultProjectId)
}
if d.CreateImageResultSizeGb == 0 {
d.CreateImageResultSizeGb = 10
}
resultCh := d.CreateImageResultCh
if resultCh == nil {
ch := make(chan *Image, 1)
ch <- &Image{
Licenses: d.CreateImageResultLicenses,
Name: name,
ProjectId: d.CreateImageResultProjectId,
SelfLink: d.CreateImageResultSelfLink,
SizeGb: d.CreateImageResultSizeGb,
}
close(ch)
resultCh = ch
}
errCh := d.CreateImageErrCh
if errCh == nil {
ch := make(chan error)
close(ch)
errCh = ch
}
return resultCh, errCh
}
func (d *DriverMock) DeleteImage(name string) <-chan error {
d.DeleteImageName = name
resultCh := d.DeleteImageErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh
}
func (d *DriverMock) DeleteInstance(zone, name string) (<-chan error, error) {
d.DeleteInstanceZone = zone
d.DeleteInstanceName = name
resultCh := d.DeleteInstanceErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh, d.DeleteInstanceErr
}
func (d *DriverMock) DeleteDisk(zone, name string) (<-chan error, error) {
d.DeleteDiskZone = zone
d.DeleteDiskName = name
resultCh := d.DeleteDiskErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh, d.DeleteDiskErr
}
func (d *DriverMock) GetImage(name string, fromFamily bool) (*Image, error) {
d.GetImageName = name
d.GetImageFromFamily = fromFamily
return d.GetImageResult, d.GetImageErr
}
func (d *DriverMock) GetImageFromProject(project, name string, fromFamily bool) (*Image, error) {
d.GetImageFromProjectProject = project
d.GetImageFromProjectName = name
d.GetImageFromProjectFromFamily = fromFamily
return d.GetImageFromProjectResult, d.GetImageFromProjectErr
}
func (d *DriverMock) GetInstanceMetadata(zone, name, key string) (string, error) {
d.GetInstanceMetadataZone = zone
d.GetInstanceMetadataName = name
d.GetInstanceMetadataKey = key
return d.GetInstanceMetadataResult, d.GetInstanceMetadataErr
}
func (d *DriverMock) GetNatIP(zone, name string) (string, error) {
d.GetNatIPZone = zone
d.GetNatIPName = name
return d.GetNatIPResult, d.GetNatIPErr
}
func (d *DriverMock) GetInternalIP(zone, name string) (string, error) {
d.GetInternalIPZone = zone
d.GetInternalIPName = name
return d.GetInternalIPResult, d.GetInternalIPErr
}
func (d *DriverMock) GetSerialPortOutput(zone, name string) (string, error) {
d.GetSerialPortOutputZone = zone
d.GetSerialPortOutputName = name
return d.GetSerialPortOutputResult, d.GetSerialPortOutputErr
}
func (d *DriverMock) ImageExists(name string) bool {
d.ImageExistsName = name
return d.ImageExistsResult
}
func (d *DriverMock) RunInstance(c *InstanceConfig) (<-chan error, error) {
d.RunInstanceConfig = c
resultCh := d.RunInstanceErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh, d.RunInstanceErr
}
func (d *DriverMock) WaitForInstance(state, zone, name string) <-chan error {
d.WaitForInstanceState = state
d.WaitForInstanceZone = zone
d.WaitForInstanceName = name
resultCh := d.WaitForInstanceErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh
}
func (d *DriverMock) GetWindowsPassword() (string, error) {
return "", nil
}
func (d *DriverMock) CreateOrResetWindowsPassword(instance, zone string, c *WindowsPasswordConfig) (<-chan error, error) {
d.CreateOrResetWindowsPasswordInstance = instance
d.CreateOrResetWindowsPasswordZone = zone
d.CreateOrResetWindowsPasswordConfig = c
c.password = "MOCK_PASSWORD"
resultCh := d.CreateOrResetWindowsPasswordErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh, d.CreateOrResetWindowsPasswordErr
}