This repository has been archived by the owner on Apr 15, 2021. It is now read-only.
forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fake_cdrom.go
94 lines (73 loc) · 1.75 KB
/
fake_cdrom.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
package fakes
import (
"errors"
"path/filepath"
fakesys "github.com/cloudfoundry/bosh-utils/system/fakes"
)
type FakeCdrom struct {
WaitForMediaError error
MountError error
UnmountError error
EjectError error
MediaAvailable bool
Fs *fakesys.FakeFileSystem
MediaFilePath string
MediaFileContents string
MountMountPath string
Mounted bool
}
func NewFakeCdrom(fs *fakesys.FakeFileSystem, filepath, contents string) *FakeCdrom {
return &FakeCdrom{
Fs: fs,
MediaFilePath: filepath,
MediaFileContents: contents,
}
}
func (cdrom *FakeCdrom) WaitForMedia() error {
if cdrom.WaitForMediaError != nil {
return cdrom.WaitForMediaError
}
cdrom.MediaAvailable = true
return nil
}
func (cdrom *FakeCdrom) Mount(mountPath string) error {
switch {
case !cdrom.MediaAvailable:
return errors.New("media not available")
case cdrom.Mounted:
return errors.New("already mounted")
case cdrom.MountError != nil:
return cdrom.MountError
}
cdrom.MountMountPath = mountPath
err := cdrom.Fs.WriteFileString(filepath.Join(mountPath, cdrom.MediaFilePath), cdrom.MediaFileContents)
if err != nil {
return err
}
cdrom.Mounted = true
return nil
}
func (cdrom *FakeCdrom) Unmount() error {
switch {
case !cdrom.Mounted:
return errors.New("device not mounted")
case cdrom.UnmountError != nil:
return cdrom.UnmountError
}
err := cdrom.Fs.RemoveAll(filepath.Join(cdrom.MountMountPath, cdrom.MediaFilePath))
if err != nil {
return err
}
cdrom.Mounted = false
return nil
}
func (cdrom *FakeCdrom) Eject() (err error) {
switch {
case cdrom.Mounted:
return errors.New("device busy")
case cdrom.EjectError != nil:
return cdrom.EjectError
}
cdrom.MediaAvailable = false
return
}