forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_platform.go
502 lines (399 loc) · 14.7 KB
/
fake_platform.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package fakes
import (
"path"
boshdpresolv "github.com/cloudfoundry/bosh-agent/infrastructure/devicepathresolver"
fakedpresolv "github.com/cloudfoundry/bosh-agent/infrastructure/devicepathresolver/fakes"
"github.com/cloudfoundry/bosh-agent/platform"
boshcert "github.com/cloudfoundry/bosh-agent/platform/cert"
fakecert "github.com/cloudfoundry/bosh-agent/platform/cert/fakes"
boshvitals "github.com/cloudfoundry/bosh-agent/platform/vitals"
fakevitals "github.com/cloudfoundry/bosh-agent/platform/vitals/fakes"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
fakecmd "github.com/cloudfoundry/bosh-utils/fileutil/fakes"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
fakesys "github.com/cloudfoundry/bosh-utils/system/fakes"
)
type FakePlatform struct {
Fs *fakesys.FakeFileSystem
Runner *fakesys.FakeCmdRunner
FakeCompressor *fakecmd.FakeCompressor
FakeCopier *fakecmd.FakeCopier
FakeVitalsService *fakevitals.FakeService
fsType string
logger boshlog.Logger
auditLogger platform.AuditLogger
DevicePathResolver boshdpresolv.DevicePathResolver
SetupRuntimeConfigurationWasInvoked bool
CreateUserUsername string
CreateUserBasePath string
AddUserToGroupsGroups map[string][]string
DeleteEphemeralUsersMatchingRegex string
SetupSSHPublicKeys map[string][]string
SetupSSHCalled bool
SetupSSHPublicKey []string
SetupSSHUsername string
SetupSSHErr error
UserPasswords map[string]string
SetupHostnameHostname string
SetupIPv6Config boshsettings.IPv6
SetupIPv6Error error
SaveDNSRecordsError error
SaveDNSRecordsHostname string
SaveDNSRecordsDNSRecords boshsettings.DNSRecords
SetTimeWithNtpServersServers []string
SetupEphemeralDiskWithPathDevicePath string
SetupEphemeralDiskWithPathSwapSize *uint64
SetupEphemeralDiskWithPathErr error
SetupRawEphemeralDisksDevices []boshsettings.DiskSettings
SetupRawEphemeralDisksErr error
SetupRawEphemeralDisksCallCount int
SetupDataDirCalled bool
SetupDataDirErr error
SetupTmpDirCalled bool
SetupTmpDirErr error
SetupHomeDirCalled bool
SetupHomeDirErr error
SetupBlobsDirCalled bool
SetupBlobsDirErr error
SetupLogDirCalled bool
SetupLogDirErr error
SetupLoggingAndAuditingCalled bool
SetupLoggingAndAuditingErr error
SetupNetworkingCalled bool
SetupNetworkingNetworks boshsettings.Networks
SetupNetworkingErr error
MountPersistentDiskCalled bool
MountPersistentDiskSettings boshsettings.DiskSettings
MountPersistentDiskMountPoint string
MountPersistentDiskErr error
UnmountPersistentDiskDidUnmount bool
UnmountPersistentDiskSettings boshsettings.DiskSettings
GetFileContentsFromCDROMPath string
GetFileContentsFromCDROMContents []byte
GetFileContentsFromCDROMErr error
GetFileContentsFromCDROMCalledTimes int
GetFileContentsFromDiskDiskPaths []string
GetFileContentsFromDiskFileNames [][]string
GetFileContentsFromDiskContents map[string][]byte
GetFileContentsFromDiskErrs map[string]error
GetFileContentsFromDiskCalledTimes int
GetEphemeralDiskPathCalled bool
GetEphemeralDiskPathSettings boshsettings.DiskSettings
GetEphemeralDiskPathRealPath string
ScsiDiskMap map[string]string
MigratePersistentDiskFromMountPoint string
MigratePersistentDiskToMountPoint string
IsPersistentDiskMountableResult bool
IsPersistentDiskMountableErr error
AssociateDiskCallCount int
AssociateDiskArgs []struct {
n string
s boshsettings.DiskSettings
}
AssociateDiskError error
IsMountPointPath string
IsMountPointPartitionPath string
IsMountPointResult bool
IsMountPointErr error
PackageFileListPath string
IsRemoveDevToolsCalled bool
IsRemoveDevToolsError error
IsRemoveStaticLibrariesCalled bool
IsRemoveStaticLibrariesError error
MountedDevicePaths []string
StartMonitStarted bool
SetupMonitUserSetup bool
GetMonitCredentialsUsername string
GetMonitCredentialsPassword string
PrepareForNetworkingChangeCalled bool
PrepareForNetworkingChangeErr error
GetDefaultNetworkNetwork boshsettings.Network
GetDefaultNetworkErr error
GetConfiguredNetworkInterfacesInterfaces []string
GetConfiguredNetworkInterfacesErr error
LastIPDeletedFromARP string
DeleteARPEntryWithIPErr error
certManager boshcert.Manager
GetHostPublicKeyValue string
GetHostPublicKeyError error
SetupRootDiskCalledTimes int
SetupRootDiskError error
SetupRecordsJSONPermissionPath string
SetupRecordsJSONPermissionErr error
}
func NewFakePlatform() (platform *FakePlatform) {
platform = new(FakePlatform)
platform.Fs = fakesys.NewFakeFileSystem()
platform.Runner = fakesys.NewFakeCmdRunner()
platform.FakeCompressor = fakecmd.NewFakeCompressor()
platform.FakeCopier = fakecmd.NewFakeCopier()
platform.FakeVitalsService = fakevitals.NewFakeService()
platform.DevicePathResolver = fakedpresolv.NewFakeDevicePathResolver()
platform.AddUserToGroupsGroups = make(map[string][]string)
platform.SetupSSHPublicKeys = make(map[string][]string)
platform.UserPasswords = make(map[string]string)
platform.ScsiDiskMap = make(map[string]string)
platform.GetFileContentsFromDiskDiskPaths = []string{}
platform.GetFileContentsFromDiskFileNames = [][]string{}
platform.GetFileContentsFromDiskContents = map[string][]byte{}
platform.GetFileContentsFromDiskErrs = map[string]error{}
platform.certManager = new(fakecert.FakeManager)
platform.SetupRawEphemeralDisksCallCount = 0
platform.SetupRawEphemeralDisksDevices = nil
platform.SetupRawEphemeralDisksErr = nil
platform.GetHostPublicKeyError = nil
platform.SetupRootDiskCalledTimes = 0
platform.SetupRootDiskError = nil
platform.IsPersistentDiskMountableErr = nil
platform.auditLogger = NewFakeAuditLogger()
return
}
func (p *FakePlatform) GetFs() (fs boshsys.FileSystem) {
return p.Fs
}
func (p *FakePlatform) GetPersistentDiskFS() (fsType string) {
return p.fsType
}
func (p *FakePlatform) GetRunner() (runner boshsys.CmdRunner) {
return p.Runner
}
func (p *FakePlatform) GetCompressor() (compressor boshcmd.Compressor) {
return p.FakeCompressor
}
func (p *FakePlatform) GetCopier() (copier boshcmd.Copier) {
return p.FakeCopier
}
func (p *FakePlatform) GetDirProvider() (dirProvider boshdir.Provider) {
return boshdir.NewProvider("/var/vcap")
}
func (p *FakePlatform) GetVitalsService() (service boshvitals.Service) {
return p.FakeVitalsService
}
func (p *FakePlatform) GetDevicePathResolver() (devicePathResolver boshdpresolv.DevicePathResolver) {
return p.DevicePathResolver
}
func (p *FakePlatform) GetAuditLogger() platform.AuditLogger {
return p.auditLogger
}
func (p *FakePlatform) SetupRuntimeConfiguration() (err error) {
p.SetupRuntimeConfigurationWasInvoked = true
return
}
func (p *FakePlatform) CreateUser(username, basePath string) (err error) {
p.CreateUserUsername = username
p.CreateUserBasePath = basePath
return
}
func (p *FakePlatform) AddUserToGroups(username string, groups []string) (err error) {
p.AddUserToGroupsGroups[username] = groups
return
}
func (p *FakePlatform) DeleteEphemeralUsersMatching(regex string) (err error) {
p.DeleteEphemeralUsersMatchingRegex = regex
return
}
func (p *FakePlatform) SetupRootDisk(ephemeralDiskPath string) (err error) {
p.SetupRootDiskCalledTimes++
if p.SetupRootDiskError != nil {
err = p.SetupRootDiskError
}
return
}
func (p *FakePlatform) SetupSSH(publicKey []string, username string) error {
p.SetupSSHCalled = true
p.SetupSSHPublicKeys[username] = publicKey
p.SetupSSHPublicKey = publicKey
p.SetupSSHUsername = username
return p.SetupSSHErr
}
func (p *FakePlatform) SetUserPassword(user, encryptedPwd string) (err error) {
p.UserPasswords[user] = encryptedPwd
return
}
func (p *FakePlatform) SaveDNSRecords(dnsRecords boshsettings.DNSRecords, hostname string) error {
p.SaveDNSRecordsDNSRecords = dnsRecords
p.SaveDNSRecordsHostname = hostname
return p.SaveDNSRecordsError
}
func (p *FakePlatform) SetupIPv6(config boshsettings.IPv6) error {
p.SetupIPv6Config = config
return p.SetupIPv6Error
}
func (p *FakePlatform) SetupHostname(hostname string) (err error) {
p.SetupHostnameHostname = hostname
return
}
func (p *FakePlatform) SetupNetworking(networks boshsettings.Networks) error {
p.SetupNetworkingCalled = true
p.SetupNetworkingNetworks = networks
return p.SetupNetworkingErr
}
func (p *FakePlatform) GetConfiguredNetworkInterfaces() ([]string, error) {
return p.GetConfiguredNetworkInterfacesInterfaces, p.GetConfiguredNetworkInterfacesErr
}
func (p *FakePlatform) GetCertManager() (certManager boshcert.Manager) {
return p.certManager
}
func (p *FakePlatform) SetupLogrotate(groupName, basePath, size string) (err error) {
return
}
func (p *FakePlatform) SetTimeWithNtpServers(servers []string) (err error) {
p.SetTimeWithNtpServersServers = servers
return
}
func (p *FakePlatform) SetupEphemeralDiskWithPath(devicePath string, desiredSwapSizeInBytes *uint64) (err error) {
p.SetupEphemeralDiskWithPathDevicePath = devicePath
p.SetupEphemeralDiskWithPathSwapSize = desiredSwapSizeInBytes
return p.SetupEphemeralDiskWithPathErr
}
func (p *FakePlatform) SetupRawEphemeralDisks(devices []boshsettings.DiskSettings) (err error) {
p.SetupRawEphemeralDisksDevices = devices
p.SetupRawEphemeralDisksCallCount++
return p.SetupRawEphemeralDisksErr
}
func (p *FakePlatform) SetupDataDir() error {
p.SetupDataDirCalled = true
return p.SetupDataDirErr
}
func (p *FakePlatform) SetupTmpDir() error {
p.SetupTmpDirCalled = true
return p.SetupTmpDirErr
}
func (p *FakePlatform) SetupHomeDir() error {
p.SetupHomeDirCalled = true
return p.SetupHomeDirErr
}
func (p *FakePlatform) SetupBlobsDir() error {
p.SetupBlobsDirCalled = true
return p.SetupBlobsDirErr
}
func (p *FakePlatform) SetupLogDir() error {
p.SetupLogDirCalled = true
return p.SetupLogDirErr
}
func (p *FakePlatform) SetupLoggingAndAuditing() error {
p.SetupLoggingAndAuditingCalled = true
return p.SetupLoggingAndAuditingErr
}
func (p *FakePlatform) MountPersistentDisk(diskSettings boshsettings.DiskSettings, mountPoint string) (err error) {
p.MountPersistentDiskCalled = true
p.MountPersistentDiskSettings = diskSettings
p.MountPersistentDiskMountPoint = mountPoint
return p.MountPersistentDiskErr
}
func (p *FakePlatform) UnmountPersistentDisk(diskSettings boshsettings.DiskSettings) (didUnmount bool, err error) {
p.UnmountPersistentDiskSettings = diskSettings
didUnmount = p.UnmountPersistentDiskDidUnmount
return
}
func (p *FakePlatform) GetEphemeralDiskPath(diskSettings boshsettings.DiskSettings) string {
p.GetEphemeralDiskPathCalled = true
p.GetEphemeralDiskPathSettings = diskSettings
return p.GetEphemeralDiskPathRealPath
}
func (p *FakePlatform) GetFileContentsFromCDROM(path string) ([]byte, error) {
p.GetFileContentsFromCDROMCalledTimes++
p.GetFileContentsFromCDROMPath = path
return p.GetFileContentsFromCDROMContents, p.GetFileContentsFromCDROMErr
}
func (p *FakePlatform) GetFilesContentsFromDisk(diskPath string, fileNames []string) ([][]byte, error) {
p.GetFileContentsFromDiskCalledTimes++
p.GetFileContentsFromDiskDiskPaths = append(p.GetFileContentsFromDiskDiskPaths, diskPath)
p.GetFileContentsFromDiskFileNames = append(p.GetFileContentsFromDiskFileNames, fileNames)
result := [][]byte{}
for _, fileName := range fileNames {
fileDiskPath := path.Join(diskPath, fileName)
err := p.GetFileContentsFromDiskErrs[fileDiskPath]
if err != nil {
return [][]byte{}, err
}
result = append(result, p.GetFileContentsFromDiskContents[fileDiskPath])
}
return result, nil
}
func (p *FakePlatform) SetGetFilesContentsFromDisk(fileName string, contents []byte, err error) {
p.GetFileContentsFromDiskContents[fileName] = contents
p.GetFileContentsFromDiskErrs[fileName] = err
}
func (p *FakePlatform) MigratePersistentDisk(fromMountPoint, toMountPoint string) (err error) {
p.MigratePersistentDiskFromMountPoint = fromMountPoint
p.MigratePersistentDiskToMountPoint = toMountPoint
return
}
func (p *FakePlatform) IsMountPoint(path string) (string, bool, error) {
p.IsMountPointPath = path
return p.IsMountPointPartitionPath, p.IsMountPointResult, p.IsMountPointErr
}
func (p *FakePlatform) IsPersistentDiskMounted(diskSettings boshsettings.DiskSettings) (result bool, err error) {
for _, mountedPath := range p.MountedDevicePaths {
if mountedPath == diskSettings.Path {
return true, nil
}
}
return
}
func (p *FakePlatform) SetIsPersistentDiskMountable(isPartitioned bool, err error) {
p.IsPersistentDiskMountableResult = isPartitioned
p.IsPersistentDiskMountableErr = err
}
func (p *FakePlatform) IsPersistentDiskMountable(diskSettings boshsettings.DiskSettings) (bool, error) {
return p.IsPersistentDiskMountableResult, p.IsPersistentDiskMountableErr
}
func (p *FakePlatform) StartMonit() (err error) {
p.StartMonitStarted = true
return
}
func (p *FakePlatform) SetupMonitUser() (err error) {
p.SetupMonitUserSetup = true
return
}
func (p *FakePlatform) GetMonitCredentials() (username, password string, err error) {
username = p.GetMonitCredentialsUsername
password = p.GetMonitCredentialsPassword
return
}
func (p *FakePlatform) DeleteARPEntryWithIP(ip string) error {
p.LastIPDeletedFromARP = ip
return p.DeleteARPEntryWithIPErr
}
func (p *FakePlatform) PrepareForNetworkingChange() error {
p.PrepareForNetworkingChangeCalled = true
return p.PrepareForNetworkingChangeErr
}
func (p *FakePlatform) GetDefaultNetwork() (boshsettings.Network, error) {
return p.GetDefaultNetworkNetwork, p.GetDefaultNetworkErr
}
func (p *FakePlatform) GetHostPublicKey() (string, error) {
return p.GetHostPublicKeyValue, p.GetHostPublicKeyError
}
func (p *FakePlatform) RemoveDevTools(packageFileListPath string) error {
p.IsRemoveDevToolsCalled = true
p.PackageFileListPath = packageFileListPath
return p.IsRemoveDevToolsError
}
func (p *FakePlatform) RemoveStaticLibraries(packageFileListPath string) error {
p.IsRemoveStaticLibrariesCalled = true
p.PackageFileListPath = packageFileListPath
return p.IsRemoveStaticLibrariesError
}
func (p *FakePlatform) AssociateDisk(name string, settings boshsettings.DiskSettings) error {
p.AssociateDiskCallCount++
p.AssociateDiskArgs = append(p.AssociateDiskArgs, struct {
n string
s boshsettings.DiskSettings
}{
n: name,
s: settings,
})
return p.AssociateDiskError
}
func (p *FakePlatform) AssociateDiskArgsForCall(i int) (string, boshsettings.DiskSettings) {
return p.AssociateDiskArgs[i].n, p.AssociateDiskArgs[i].s
}
func (p *FakePlatform) SetupRecordsJSONPermission(path string) error {
p.SetupRecordsJSONPermissionPath = path
return p.SetupRecordsJSONPermissionErr
}