Debian Codegraph @

fake_mounter.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package mount
18
19 import (
20 "os"
21 "path/filepath"
22 "sync"
23
24 "k8s.io/klog/v2"
25 )
26
27 // FakeMounter implements mount.Interface for tests.
28 type FakeMounter struct {
29 MountPoints []MountPoint
30 log []FakeAction
31 // Error to return for a path when calling IsLikelyNotMountPoint
32 MountCheckErrors map[string]error
33 // Some tests run things in parallel, make sure the mounter does not produce
34 // any golang's DATA RACE warnings.
35 mutex sync.Mutex
36 UnmountFunc UnmountFunc
37 skipMountPointCheck bool
38 }
39
40 // UnmountFunc is a function callback to be executed during the Unmount() call.
41 type UnmountFunc func(path string) error
42
43 var _ Interface = &FakeMounter{}
44
45 const (
46 // FakeActionMount is the string for specifying mount as FakeAction.Action
47 FakeActionMount = "mount"
48 // FakeActionUnmount is the string for specifying unmount as FakeAction.Action
49 FakeActionUnmount = "unmount"
50 )
51
52 // FakeAction objects are logged every time a fake mount or unmount is called.
53 type FakeAction struct {
54 Action string // "mount" or "unmount"
55 Target string // applies to both mount and unmount actions
56 Source string // applies only to "mount" actions
57 FSType string // applies only to "mount" actions
58 }
59
60 // NewFakeMounter returns a FakeMounter struct that implements Interface and is
61 // suitable for testing purposes.
62 func NewFakeMounter(mps []MountPoint) *FakeMounter {
63 return &FakeMounter{
64 MountPoints: mps,
65 }
66 }
67
68 func (f *FakeMounter) WithSkipMountPointCheck() *FakeMounter {
69 f.skipMountPointCheck = true
70 return f
71 }
72
73 // ResetLog clears all the log entries in FakeMounter
74 func (f *FakeMounter) ResetLog() {
75 f.mutex.Lock()
76 defer f.mutex.Unlock()
77
78 f.log = []FakeAction{}
79 }
80
81 // GetLog returns the slice of FakeActions taken by the mounter
82 func (f *FakeMounter) GetLog() []FakeAction {
83 f.mutex.Lock()
84 defer f.mutex.Unlock()
85
86 return f.log
87 }
88
89 // Mount records the mount event and updates the in-memory mount points for FakeMounter
90 func (f *FakeMounter) Mount(source string, target string, fstype string, options []string) error {
91 return f.MountSensitive(source, target, fstype, options, nil /* sensitiveOptions */)
92 }
93
94 // Mount records the mount event and updates the in-memory mount points for FakeMounter
95 // sensitiveOptions to be passed in a separate parameter from the normal
96 // mount options and ensures the sensitiveOptions are never logged. This
97 // method should be used by callers that pass sensitive material (like
98 // passwords) as mount options.
99 func (f *FakeMounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error {
100 f.mutex.Lock()
101 defer f.mutex.Unlock()
102
103 opts := []string{}
104
105 for _, option := range options {
106 // find 'bind' option
107 if option == "bind" {
108 // This is a bind-mount. In order to mimic linux behaviour, we must
109 // use the original device of the bind-mount as the real source.
110 // E.g. when mounted /dev/sda like this:
111 // $ mount /dev/sda /mnt/test
112 // $ mount -o bind /mnt/test /mnt/bound
113 // then /proc/mount contains:
114 // /dev/sda /mnt/test
115 // /dev/sda /mnt/bound
116 // (and not /mnt/test /mnt/bound)
117 // I.e. we must use /dev/sda as source instead of /mnt/test in the
118 // bind mount.
119 for _, mnt := range f.MountPoints {
120 if source == mnt.Path {
121 source = mnt.Device
122 break
123 }
124 }
125 }
126 // reuse MountPoint.Opts field to mark mount as readonly
127 opts = append(opts, option)
128 }
129
130 // If target is a symlink, get its absolute path
131 absTarget, err := filepath.EvalSymlinks(target)
132 if err != nil {
133 absTarget = target
134 }
135 f.MountPoints = append(f.MountPoints, MountPoint{Device: source, Path: absTarget, Type: fstype, Opts: append(opts, sensitiveOptions...)})
136 klog.V(5).Infof("Fake mounter: mounted %s to %s", source, absTarget)
137 f.log = append(f.log, FakeAction{Action: FakeActionMount, Target: absTarget, Source: source, FSType: fstype})
138 return nil
139 }
140
141 func (f *FakeMounter) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error {
142 return f.MountSensitive(source, target, fstype, options, nil /* sensitiveOptions */)
143 }
144
145 func (f *FakeMounter) MountSensitiveWithoutSystemdWithMountFlags(source string, target string, fstype string, options []string, sensitiveOptions []string, mountFlags []string) error {
146 return f.MountSensitive(source, target, fstype, options, nil /* sensitiveOptions */)
147 }
148
149 // Unmount records the unmount event and updates the in-memory mount points for FakeMounter
150 func (f *FakeMounter) Unmount(target string) error {
151 f.mutex.Lock()
152 defer f.mutex.Unlock()
153
154 // If target is a symlink, get its absolute path
155 absTarget, err := filepath.EvalSymlinks(target)
156 if err != nil {
157 absTarget = target
158 }
159
160 newMountpoints := []MountPoint{}
161 for _, mp := range f.MountPoints {
162 if mp.Path == absTarget {
163 if f.UnmountFunc != nil {
164 err := f.UnmountFunc(absTarget)
165 if err != nil {
166 return err
167 }
168 }
169 klog.V(5).Infof("Fake mounter: unmounted %s from %s", mp.Device, absTarget)
170 // Don't copy it to newMountpoints
171 continue
172 }
173 newMountpoints = append(newMountpoints, MountPoint{Device: mp.Device, Path: mp.Path, Type: mp.Type})
174 }
175 f.MountPoints = newMountpoints
176 f.log = append(f.log, FakeAction{Action: FakeActionUnmount, Target: absTarget})
177 delete(f.MountCheckErrors, target)
178 return nil
179 }
180
181 // List returns all the in-memory mountpoints for FakeMounter
182 func (f *FakeMounter) List() ([]MountPoint, error) {
183 f.mutex.Lock()
184 defer f.mutex.Unlock()
185
186 return f.MountPoints, nil
187 }
188
189 // IsLikelyNotMountPoint determines whether a path is a mountpoint by checking
190 // if the absolute path to file is in the in-memory mountpoints
191 func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
192 f.mutex.Lock()
193 defer f.mutex.Unlock()
194
195 err := f.MountCheckErrors[file]
196 if err != nil {
197 return false, err
198 }
199
200 _, err = os.Stat(file)
201 if err != nil {
202 return true, err
203 }
204
205 // If file is a symlink, get its absolute path
206 absFile, err := filepath.EvalSymlinks(file)
207 if err != nil {
208 absFile = file
209 }
210
211 for _, mp := range f.MountPoints {
212 if mp.Path == absFile {
213 klog.V(5).Infof("isLikelyNotMountPoint for %s: mounted %s, false", file, mp.Path)
214 return false, nil
215 }
216 }
217 klog.V(5).Infof("isLikelyNotMountPoint for %s: true", file)
218 return true, nil
219 }
220
221 func (f *FakeMounter) CanSafelySkipMountPointCheck() bool {
222 return f.skipMountPointCheck
223 }
224
225 func (f *FakeMounter) IsMountPoint(file string) (bool, error) {
226 notMnt, err := f.IsLikelyNotMountPoint(file)
227 if err != nil {
228 return false, err
229 }
230 return !notMnt, nil
231 }
232
233 // GetMountRefs finds all mount references to the path, returns a
234 // list of paths.
235 func (f *FakeMounter) GetMountRefs(pathname string) ([]string, error) {
236 realpath, err := filepath.EvalSymlinks(pathname)
237 if err != nil {
238 // Ignore error in FakeMounter, because we actually didn't create files.
239 realpath = pathname
240 }
241 return getMountRefsByDev(f, realpath)
242 }