@@ -19,173 +19,9 @@ package mount
19
19
import (
20
20
"errors"
21
21
"os"
22
- "path/filepath"
23
22
"sync"
24
-
25
- "k8s.io/klog"
26
23
)
27
24
28
- // FakeMounter implements mount.Interface for tests.
29
- type FakeMounter struct {
30
- MountPoints []MountPoint
31
- Log []FakeAction
32
- // Error to return for a path when calling IsLikelyNotMountPoint
33
- MountCheckErrors map [string ]error
34
- // Some tests run things in parallel, make sure the mounter does not produce
35
- // any golang's DATA RACE warnings.
36
- mutex sync.Mutex
37
- }
38
-
39
- var _ Interface = & FakeMounter {}
40
-
41
- const (
42
- // FakeActionMount is the string for specifying mount as FakeAction.Action
43
- FakeActionMount = "mount"
44
- // FakeActionUnmount is the string for specifying unmount as FakeAction.Action
45
- FakeActionUnmount = "unmount"
46
- )
47
-
48
- // FakeAction objects are logged every time a fake mount or unmount is called.
49
- type FakeAction struct {
50
- Action string // "mount" or "unmount"
51
- Target string // applies to both mount and unmount actions
52
- Source string // applies only to "mount" actions
53
- FSType string // applies only to "mount" actions
54
- }
55
-
56
- // ResetLog clears all the log entries in FakeMounter
57
- func (f * FakeMounter ) ResetLog () {
58
- f .mutex .Lock ()
59
- defer f .mutex .Unlock ()
60
-
61
- f .Log = []FakeAction {}
62
- }
63
-
64
- // Mount records the mount event and updates the in-memory mount points for FakeMounter
65
- func (f * FakeMounter ) Mount (source string , target string , fstype string , options []string ) error {
66
- f .mutex .Lock ()
67
- defer f .mutex .Unlock ()
68
-
69
- opts := []string {}
70
-
71
- for _ , option := range options {
72
- // find 'bind' option
73
- if option == "bind" {
74
- // This is a bind-mount. In order to mimic linux behaviour, we must
75
- // use the original device of the bind-mount as the real source.
76
- // E.g. when mounted /dev/sda like this:
77
- // $ mount /dev/sda /mnt/test
78
- // $ mount -o bind /mnt/test /mnt/bound
79
- // then /proc/mount contains:
80
- // /dev/sda /mnt/test
81
- // /dev/sda /mnt/bound
82
- // (and not /mnt/test /mnt/bound)
83
- // I.e. we must use /dev/sda as source instead of /mnt/test in the
84
- // bind mount.
85
- for _ , mnt := range f .MountPoints {
86
- if source == mnt .Path {
87
- source = mnt .Device
88
- break
89
- }
90
- }
91
- }
92
- // reuse MountPoint.Opts field to mark mount as readonly
93
- opts = append (opts , option )
94
- }
95
-
96
- // If target is a symlink, get its absolute path
97
- absTarget , err := filepath .EvalSymlinks (target )
98
- if err != nil {
99
- absTarget = target
100
- }
101
- f .MountPoints = append (f .MountPoints , MountPoint {Device : source , Path : absTarget , Type : fstype , Opts : opts })
102
- klog .V (5 ).Infof ("Fake mounter: mounted %s to %s" , source , absTarget )
103
- f .Log = append (f .Log , FakeAction {Action : FakeActionMount , Target : absTarget , Source : source , FSType : fstype })
104
- return nil
105
- }
106
-
107
- // Unmount records the unmount event and updates the in-memory mount points for FakeMounter
108
- func (f * FakeMounter ) Unmount (target string ) error {
109
- f .mutex .Lock ()
110
- defer f .mutex .Unlock ()
111
-
112
- // If target is a symlink, get its absolute path
113
- absTarget , err := filepath .EvalSymlinks (target )
114
- if err != nil {
115
- absTarget = target
116
- }
117
-
118
- newMountpoints := []MountPoint {}
119
- for _ , mp := range f .MountPoints {
120
- if mp .Path == absTarget {
121
- klog .V (5 ).Infof ("Fake mounter: unmounted %s from %s" , mp .Device , absTarget )
122
- // Don't copy it to newMountpoints
123
- continue
124
- }
125
- newMountpoints = append (newMountpoints , MountPoint {Device : mp .Device , Path : mp .Path , Type : mp .Type })
126
- }
127
- f .MountPoints = newMountpoints
128
- f .Log = append (f .Log , FakeAction {Action : FakeActionUnmount , Target : absTarget })
129
- delete (f .MountCheckErrors , target )
130
- return nil
131
- }
132
-
133
- // List returns all the in-memory mountpoints for FakeMounter
134
- func (f * FakeMounter ) List () ([]MountPoint , error ) {
135
- f .mutex .Lock ()
136
- defer f .mutex .Unlock ()
137
-
138
- return f .MountPoints , nil
139
- }
140
-
141
- // IsMountPointMatch tests if dir and mp are the same path
142
- func (f * FakeMounter ) IsMountPointMatch (mp MountPoint , dir string ) bool {
143
- return mp .Path == dir
144
- }
145
-
146
- // IsLikelyNotMountPoint determines whether a path is a mountpoint by checking
147
- // if the absolute path to file is in the in-memory mountpoints
148
- func (f * FakeMounter ) IsLikelyNotMountPoint (file string ) (bool , error ) {
149
- f .mutex .Lock ()
150
- defer f .mutex .Unlock ()
151
-
152
- err := f .MountCheckErrors [file ]
153
- if err != nil {
154
- return false , err
155
- }
156
-
157
- _ , err = os .Stat (file )
158
- if err != nil {
159
- return true , err
160
- }
161
-
162
- // If file is a symlink, get its absolute path
163
- absFile , err := filepath .EvalSymlinks (file )
164
- if err != nil {
165
- absFile = file
166
- }
167
-
168
- for _ , mp := range f .MountPoints {
169
- if mp .Path == absFile {
170
- klog .V (5 ).Infof ("isLikelyNotMountPoint for %s: mounted %s, false" , file , mp .Path )
171
- return false , nil
172
- }
173
- }
174
- klog .V (5 ).Infof ("isLikelyNotMountPoint for %s: true" , file )
175
- return true , nil
176
- }
177
-
178
- // GetMountRefs finds all mount references to the path, returns a
179
- // list of paths.
180
- func (f * FakeMounter ) GetMountRefs (pathname string ) ([]string , error ) {
181
- realpath , err := filepath .EvalSymlinks (pathname )
182
- if err != nil {
183
- // Ignore error in FakeMounter, because we actually didn't create files.
184
- realpath = pathname
185
- }
186
- return getMountRefsByDev (f , realpath )
187
- }
188
-
189
25
// FakeHostUtil is a fake mount.HostUtils implementation for testing
190
26
type FakeHostUtil struct {
191
27
MountPoints []MountPoint
0 commit comments