This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 348
/
fake_os.go
254 lines (215 loc) · 6.62 KB
/
fake_os.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
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testing
import (
"os"
"sync"
containerdmount "github.com/containerd/containerd/mount"
osInterface "github.com/containerd/cri/pkg/os"
)
// CalledDetail is the struct contains called function name and arguments.
type CalledDetail struct {
// Name of the function called.
Name string
// Arguments of the function called.
Arguments []interface{}
}
// FakeOS mocks out certain OS calls to avoid perturbing the filesystem
// If a member of the form `*Fn` is set, that function will be called in place
// of the real call.
type FakeOS struct {
sync.Mutex
MkdirAllFn func(string, os.FileMode) error
RemoveAllFn func(string) error
StatFn func(string) (os.FileInfo, error)
ResolveSymbolicLinkFn func(string) (string, error)
FollowSymlinkInScopeFn func(string, string) (string, error)
CopyFileFn func(string, string, os.FileMode) error
WriteFileFn func(string, []byte, os.FileMode) error
MountFn func(source string, target string, fstype string, flags uintptr, data string) error
UnmountFn func(target string) error
LookupMountFn func(path string) (containerdmount.Info, error)
HostnameFn func() (string, error)
calls []CalledDetail
errors map[string]error
}
var _ osInterface.OS = &FakeOS{}
// getError get error for call
func (f *FakeOS) getError(op string) error {
f.Lock()
defer f.Unlock()
err, ok := f.errors[op]
if ok {
delete(f.errors, op)
return err
}
return nil
}
// InjectError inject error for call
func (f *FakeOS) InjectError(fn string, err error) {
f.Lock()
defer f.Unlock()
f.errors[fn] = err
}
// InjectErrors inject errors for calls
func (f *FakeOS) InjectErrors(errs map[string]error) {
f.Lock()
defer f.Unlock()
for fn, err := range errs {
f.errors[fn] = err
}
}
// ClearErrors clear errors for call
func (f *FakeOS) ClearErrors() {
f.Lock()
defer f.Unlock()
f.errors = make(map[string]error)
}
func (f *FakeOS) appendCalls(name string, args ...interface{}) {
f.Lock()
defer f.Unlock()
f.calls = append(f.calls, CalledDetail{Name: name, Arguments: args})
}
// GetCalls get detail of calls.
func (f *FakeOS) GetCalls() []CalledDetail {
f.Lock()
defer f.Unlock()
return append([]CalledDetail{}, f.calls...)
}
// NewFakeOS creates a FakeOS.
func NewFakeOS() *FakeOS {
return &FakeOS{
errors: make(map[string]error),
}
}
// MkdirAll is a fake call that invokes MkdirAllFn or just returns nil.
func (f *FakeOS) MkdirAll(path string, perm os.FileMode) error {
f.appendCalls("MkdirAll", path, perm)
if err := f.getError("MkdirAll"); err != nil {
return err
}
if f.MkdirAllFn != nil {
return f.MkdirAllFn(path, perm)
}
return nil
}
// RemoveAll is a fake call that invokes RemoveAllFn or just returns nil.
func (f *FakeOS) RemoveAll(path string) error {
f.appendCalls("RemoveAll", path)
if err := f.getError("RemoveAll"); err != nil {
return err
}
if f.RemoveAllFn != nil {
return f.RemoveAllFn(path)
}
return nil
}
// Stat is a fake call that invokes StatFn or just return nil.
func (f *FakeOS) Stat(name string) (os.FileInfo, error) {
f.appendCalls("Stat", name)
if err := f.getError("Stat"); err != nil {
return nil, err
}
if f.StatFn != nil {
return f.StatFn(name)
}
return nil, nil
}
// ResolveSymbolicLink is a fake call that invokes ResolveSymbolicLinkFn or returns its input
func (f *FakeOS) ResolveSymbolicLink(path string) (string, error) {
f.appendCalls("ResolveSymbolicLink", path)
if err := f.getError("ResolveSymbolicLink"); err != nil {
return "", err
}
if f.ResolveSymbolicLinkFn != nil {
return f.ResolveSymbolicLinkFn(path)
}
return path, nil
}
// FollowSymlinkInScope is a fake call that invokes FollowSymlinkInScope or returns its input
func (f *FakeOS) FollowSymlinkInScope(path, scope string) (string, error) {
f.appendCalls("FollowSymlinkInScope", path, scope)
if err := f.getError("FollowSymlinkInScope"); err != nil {
return "", err
}
if f.FollowSymlinkInScopeFn != nil {
return f.FollowSymlinkInScopeFn(path, scope)
}
return path, nil
}
// CopyFile is a fake call that invokes CopyFileFn or just return nil.
func (f *FakeOS) CopyFile(src, dest string, perm os.FileMode) error {
f.appendCalls("CopyFile", src, dest, perm)
if err := f.getError("CopyFile"); err != nil {
return err
}
if f.CopyFileFn != nil {
return f.CopyFileFn(src, dest, perm)
}
return nil
}
// WriteFile is a fake call that invokes WriteFileFn or just return nil.
func (f *FakeOS) WriteFile(filename string, data []byte, perm os.FileMode) error {
f.appendCalls("WriteFile", filename, data, perm)
if err := f.getError("WriteFile"); err != nil {
return err
}
if f.WriteFileFn != nil {
return f.WriteFileFn(filename, data, perm)
}
return nil
}
// Mount is a fake call that invokes MountFn or just return nil.
func (f *FakeOS) Mount(source string, target string, fstype string, flags uintptr, data string) error {
f.appendCalls("Mount", source, target, fstype, flags, data)
if err := f.getError("Mount"); err != nil {
return err
}
if f.MountFn != nil {
return f.MountFn(source, target, fstype, flags, data)
}
return nil
}
// Unmount is a fake call that invokes UnmountFn or just return nil.
func (f *FakeOS) Unmount(target string) error {
f.appendCalls("Unmount", target)
if err := f.getError("Unmount"); err != nil {
return err
}
if f.UnmountFn != nil {
return f.UnmountFn(target)
}
return nil
}
// LookupMount is a fake call that invokes LookupMountFn or just return nil.
func (f *FakeOS) LookupMount(path string) (containerdmount.Info, error) {
f.appendCalls("LookupMount", path)
if err := f.getError("LookupMount"); err != nil {
return containerdmount.Info{}, err
}
if f.LookupMountFn != nil {
return f.LookupMountFn(path)
}
return containerdmount.Info{}, nil
}
// Hostname is a fake call that invokes HostnameFn or just return nil.
func (f *FakeOS) Hostname() (string, error) {
f.appendCalls("Hostname")
if err := f.getError("Hostname"); err != nil {
return "", err
}
if f.HostnameFn != nil {
return f.HostnameFn()
}
return "", nil
}