Skip to content

Commit 1fd8921

Browse files
committed
Move mount/fake.go to mount/fake_mount.go
This patch moves fake.go to mount_fake.go, and follows to principle of always returning a discrete type rather than an Interface. All callers of "FakeMounter" are changed to instead use "NewFakeMounter()". The FakeMounter "Log" struct member is changed to not be exported, and instead only access through a new "GetLog()" method.
1 parent 47dc1d6 commit 1fd8921

37 files changed

+161
-185
lines changed

pkg/kubelet/cm/container_manager_linux_test.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
)
3232

3333
func fakeContainerMgrMountInt() mount.Interface {
34-
return &mount.FakeMounter{
35-
MountPoints: []mount.MountPoint{
34+
return mount.NewFakeMounter(
35+
[]mount.MountPoint{
3636
{
3737
Device: "cgroup",
3838
Type: "cgroup",
@@ -53,8 +53,7 @@ func fakeContainerMgrMountInt() mount.Interface {
5353
Type: "cgroup",
5454
Opts: []string{"rw", "relatime", "memory"},
5555
},
56-
},
57-
}
56+
})
5857
}
5958

6059
func TestCgroupMountValidationSuccess(t *testing.T) {
@@ -64,8 +63,8 @@ func TestCgroupMountValidationSuccess(t *testing.T) {
6463
}
6564

6665
func TestCgroupMountValidationMemoryMissing(t *testing.T) {
67-
mountInt := &mount.FakeMounter{
68-
MountPoints: []mount.MountPoint{
66+
mountInt := mount.NewFakeMounter(
67+
[]mount.MountPoint{
6968
{
7069
Device: "cgroup",
7170
Type: "cgroup",
@@ -81,15 +80,14 @@ func TestCgroupMountValidationMemoryMissing(t *testing.T) {
8180
Type: "cgroup",
8281
Opts: []string{"rw", "relatime", "cpuacct"},
8382
},
84-
},
85-
}
83+
})
8684
_, err := validateSystemRequirements(mountInt)
8785
assert.Error(t, err)
8886
}
8987

9088
func TestCgroupMountValidationMultipleSubsystem(t *testing.T) {
91-
mountInt := &mount.FakeMounter{
92-
MountPoints: []mount.MountPoint{
89+
mountInt := mount.NewFakeMounter(
90+
[]mount.MountPoint{
9391
{
9492
Device: "cgroup",
9593
Type: "cgroup",
@@ -105,8 +103,7 @@ func TestCgroupMountValidationMultipleSubsystem(t *testing.T) {
105103
Type: "cgroup",
106104
Opts: []string{"rw", "relatime", "cpuacct"},
107105
},
108-
},
109-
}
106+
})
110107
_, err := validateSystemRequirements(mountInt)
111108
assert.Nil(t, err)
112109
}
@@ -118,8 +115,8 @@ func TestSoftRequirementsValidationSuccess(t *testing.T) {
118115
defer os.RemoveAll(tempDir)
119116
req.NoError(ioutil.WriteFile(path.Join(tempDir, "cpu.cfs_period_us"), []byte("0"), os.ModePerm))
120117
req.NoError(ioutil.WriteFile(path.Join(tempDir, "cpu.cfs_quota_us"), []byte("0"), os.ModePerm))
121-
mountInt := &mount.FakeMounter{
122-
MountPoints: []mount.MountPoint{
118+
mountInt := mount.NewFakeMounter(
119+
[]mount.MountPoint{
123120
{
124121
Device: "cgroup",
125122
Type: "cgroup",
@@ -136,8 +133,7 @@ func TestSoftRequirementsValidationSuccess(t *testing.T) {
136133
Type: "cgroup",
137134
Opts: []string{"rw", "relatime", "cpuacct", "memory"},
138135
},
139-
},
140-
}
136+
})
141137
f, err := validateSystemRequirements(mountInt)
142138
assert.NoError(t, err)
143139
assert.True(t, f.cpuHardcapping, "cpu hardcapping is expected to be enabled")

pkg/kubelet/kubelet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func newTestKubeletWithImageList(
161161
kubelet.kubeClient = fakeKubeClient
162162
kubelet.heartbeatClient = fakeKubeClient
163163
kubelet.os = &containertest.FakeOS{}
164-
kubelet.mounter = &mount.FakeMounter{}
164+
kubelet.mounter = mount.NewFakeMounter(nil)
165165
kubelet.hostutil = hostutil.NewFakeHostUtil(nil)
166166
kubelet.subpather = &subpath.FakeSubpath{}
167167

pkg/kubelet/runonce_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestRunOnce(t *testing.T) {
129129

130130
kb.evictionManager = evictionManager
131131
kb.admitHandlers.AddPodAdmitHandler(evictionAdmitHandler)
132-
kb.mounter = &mount.FakeMounter{}
132+
kb.mounter = mount.NewFakeMounter(nil)
133133
if err := kb.setupDataDirs(); err != nil {
134134
t.Errorf("Failed to init data dirs: %v", err)
135135
}

pkg/kubelet/volumemanager/reconciler/reconciler_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func Test_Run_Positive_DoNothing(t *testing.T) {
8484
asw,
8585
hasAddedPods,
8686
oex,
87-
&mount.FakeMounter{},
87+
mount.NewFakeMounter(nil),
8888
hostutil.NewFakeHostUtil(nil),
8989
volumePluginMgr,
9090
kubeletPodsDir)
@@ -128,7 +128,7 @@ func Test_Run_Positive_VolumeAttachAndMount(t *testing.T) {
128128
asw,
129129
hasAddedPods,
130130
oex,
131-
&mount.FakeMounter{},
131+
mount.NewFakeMounter(nil),
132132
hostutil.NewFakeHostUtil(nil),
133133
volumePluginMgr,
134134
kubeletPodsDir)
@@ -206,7 +206,7 @@ func Test_Run_Positive_VolumeMountControllerAttachEnabled(t *testing.T) {
206206
asw,
207207
hasAddedPods,
208208
oex,
209-
&mount.FakeMounter{},
209+
mount.NewFakeMounter(nil),
210210
hostutil.NewFakeHostUtil(nil),
211211
volumePluginMgr,
212212
kubeletPodsDir)
@@ -285,7 +285,7 @@ func Test_Run_Positive_VolumeAttachMountUnmountDetach(t *testing.T) {
285285
asw,
286286
hasAddedPods,
287287
oex,
288-
&mount.FakeMounter{},
288+
mount.NewFakeMounter(nil),
289289
hostutil.NewFakeHostUtil(nil),
290290
volumePluginMgr,
291291
kubeletPodsDir)
@@ -375,7 +375,7 @@ func Test_Run_Positive_VolumeUnmountControllerAttachEnabled(t *testing.T) {
375375
asw,
376376
hasAddedPods,
377377
oex,
378-
&mount.FakeMounter{},
378+
mount.NewFakeMounter(nil),
379379
hostutil.NewFakeHostUtil(nil),
380380
volumePluginMgr,
381381
kubeletPodsDir)
@@ -502,7 +502,7 @@ func Test_Run_Positive_VolumeAttachAndMap(t *testing.T) {
502502
asw,
503503
hasAddedPods,
504504
oex,
505-
&mount.FakeMounter{},
505+
mount.NewFakeMounter(nil),
506506
hostutil.NewFakeHostUtil(nil),
507507
volumePluginMgr,
508508
kubeletPodsDir)
@@ -608,7 +608,7 @@ func Test_Run_Positive_BlockVolumeMapControllerAttachEnabled(t *testing.T) {
608608
asw,
609609
hasAddedPods,
610610
oex,
611-
&mount.FakeMounter{},
611+
mount.NewFakeMounter(nil),
612612
hostutil.NewFakeHostUtil(nil),
613613
volumePluginMgr,
614614
kubeletPodsDir)
@@ -709,7 +709,7 @@ func Test_Run_Positive_BlockVolumeAttachMapUnmapDetach(t *testing.T) {
709709
asw,
710710
hasAddedPods,
711711
oex,
712-
&mount.FakeMounter{},
712+
mount.NewFakeMounter(nil),
713713
hostutil.NewFakeHostUtil(nil),
714714
volumePluginMgr,
715715
kubeletPodsDir)
@@ -823,7 +823,7 @@ func Test_Run_Positive_VolumeUnmapControllerAttachEnabled(t *testing.T) {
823823
asw,
824824
hasAddedPods,
825825
oex,
826-
&mount.FakeMounter{},
826+
mount.NewFakeMounter(nil),
827827
hostutil.NewFakeHostUtil(nil),
828828
volumePluginMgr,
829829
kubeletPodsDir)
@@ -1096,7 +1096,7 @@ func Test_Run_Positive_VolumeFSResizeControllerAttachEnabled(t *testing.T) {
10961096
asw,
10971097
hasAddedPods,
10981098
oex,
1099-
&mount.FakeMounter{},
1099+
mount.NewFakeMounter(nil),
11001100
hostutil.NewFakeHostUtil(nil),
11011101
volumePluginMgr,
11021102
kubeletPodsDir)
@@ -1278,7 +1278,7 @@ func Test_Run_Positive_VolumeMountControllerAttachEnabledRace(t *testing.T) {
12781278
asw,
12791279
hasAddedPods,
12801280
oex,
1281-
&mount.FakeMounter{},
1281+
mount.NewFakeMounter(nil),
12821282
hostutil.NewFakeHostUtil(nil),
12831283
volumePluginMgr,
12841284
kubeletPodsDir)

pkg/kubelet/volumemanager/volume_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func newTestVolumeManager(tmpDir string, podManager kubepod.Manager, kubeClient
302302
kubeClient,
303303
plugMgr,
304304
&containertest.FakeRuntime{},
305-
&mount.FakeMounter{},
305+
mount.NewFakeMounter(nil),
306306
hostutil.NewFakeHostUtil(nil),
307307
"",
308308
fakeRecorder,

pkg/util/mount/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ go_library(
55
srcs = [
66
"doc.go",
77
"exec.go",
8-
"fake.go",
8+
"fake_exec.go",
9+
"fake_mounter.go",
910
"mount.go",
1011
"mount_helper_common.go",
1112
"mount_helper_unix.go",

pkg/util/mount/exec.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,18 @@ package mount
1919
import "k8s.io/utils/exec"
2020

2121
// NewOSExec returns a new Exec interface implementation based on exec()
22-
func NewOSExec() Exec {
23-
return &osExec{}
22+
func NewOSExec() *OSExec {
23+
return &OSExec{}
2424
}
2525

26-
// Real implementation of Exec interface that uses simple utils.Exec
27-
type osExec struct{}
26+
// OSExec is an implementation of Exec interface that uses simple utils.Exec
27+
type OSExec struct{}
2828

29-
var _ Exec = &osExec{}
29+
var _ Exec = &OSExec{}
3030

31-
func (e *osExec) Run(cmd string, args ...string) ([]byte, error) {
31+
// Run exucutes the given cmd and arges and returns stdout and stderr as a
32+
// combined byte stream
33+
func (e *OSExec) Run(cmd string, args ...string) ([]byte, error) {
3234
exe := exec.New()
3335
return exe.Command(cmd, args...).CombinedOutput()
3436
}
35-
36-
// NewFakeExec returns a new FakeExec
37-
func NewFakeExec(run runHook) *FakeExec {
38-
return &FakeExec{runHook: run}
39-
}
40-
41-
// FakeExec for testing.
42-
type FakeExec struct {
43-
runHook runHook
44-
}
45-
type runHook func(cmd string, args ...string) ([]byte, error)
46-
47-
// Run executes the command using the optional runhook, if given
48-
func (f *FakeExec) Run(cmd string, args ...string) ([]byte, error) {
49-
if f.runHook != nil {
50-
return f.runHook(cmd, args...)
51-
}
52-
return nil, nil
53-
}

pkg/util/mount/fake_exec.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@ limitations under the License.
1616

1717
package mount
1818

19-
import "k8s.io/utils/exec"
20-
21-
// NewOSExec returns a new Exec interface implementation based on exec()
22-
func NewOSExec() Exec {
23-
return &osExec{}
24-
}
25-
26-
// Real implementation of Exec interface that uses simple utils.Exec
27-
type osExec struct{}
28-
29-
var _ Exec = &osExec{}
30-
31-
func (e *osExec) Run(cmd string, args ...string) ([]byte, error) {
32-
exe := exec.New()
33-
return exe.Command(cmd, args...).CombinedOutput()
34-
}
35-
3619
// NewFakeExec returns a new FakeExec
3720
func NewFakeExec(run runHook) *FakeExec {
3821
return &FakeExec{runHook: run}

pkg/util/mount/fake.go renamed to pkg/util/mount/fake_mounter.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
// FakeMounter implements mount.Interface for tests.
2828
type FakeMounter struct {
2929
MountPoints []MountPoint
30-
Log []FakeAction
30+
log []FakeAction
3131
// Error to return for a path when calling IsLikelyNotMountPoint
3232
MountCheckErrors map[string]error
3333
// Some tests run things in parallel, make sure the mounter does not produce
@@ -55,12 +55,26 @@ type FakeAction struct {
5555
FSType string // applies only to "mount" actions
5656
}
5757

58+
func NewFakeMounter(mps []MountPoint) *FakeMounter {
59+
return &FakeMounter{
60+
MountPoints: mps,
61+
}
62+
}
63+
5864
// ResetLog clears all the log entries in FakeMounter
5965
func (f *FakeMounter) ResetLog() {
6066
f.mutex.Lock()
6167
defer f.mutex.Unlock()
6268

63-
f.Log = []FakeAction{}
69+
f.log = []FakeAction{}
70+
}
71+
72+
// GetLog returns the slice of FakeActions taken by the mounter
73+
func (f *FakeMounter) GetLog() []FakeAction {
74+
f.mutex.Lock()
75+
defer f.mutex.Unlock()
76+
77+
return f.log
6478
}
6579

6680
// Mount records the mount event and updates the in-memory mount points for FakeMounter
@@ -102,7 +116,7 @@ func (f *FakeMounter) Mount(source string, target string, fstype string, options
102116
}
103117
f.MountPoints = append(f.MountPoints, MountPoint{Device: source, Path: absTarget, Type: fstype, Opts: opts})
104118
klog.V(5).Infof("Fake mounter: mounted %s to %s", source, absTarget)
105-
f.Log = append(f.Log, FakeAction{Action: FakeActionMount, Target: absTarget, Source: source, FSType: fstype})
119+
f.log = append(f.log, FakeAction{Action: FakeActionMount, Target: absTarget, Source: source, FSType: fstype})
106120
return nil
107121
}
108122

@@ -133,7 +147,7 @@ func (f *FakeMounter) Unmount(target string) error {
133147
newMountpoints = append(newMountpoints, MountPoint{Device: mp.Device, Path: mp.Path, Type: mp.Type})
134148
}
135149
f.MountPoints = newMountpoints
136-
f.Log = append(f.Log, FakeAction{Action: FakeActionUnmount, Target: absTarget})
150+
f.log = append(f.log, FakeAction{Action: FakeActionUnmount, Target: absTarget})
137151
delete(f.MountCheckErrors, target)
138152
return nil
139153
}

pkg/util/mount/mount_helper_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ func TestDoCleanupMountPoint(t *testing.T) {
9393
t.Fatalf("failed to prepare test: %v", err)
9494
}
9595

96-
fake := &FakeMounter{
97-
MountPoints: []MountPoint{mountPoint},
98-
MountCheckErrors: map[string]error{mountPoint.Path: mountError},
99-
}
96+
fake := NewFakeMounter(
97+
[]MountPoint{mountPoint},
98+
)
99+
fake.MountCheckErrors = map[string]error{mountPoint.Path: mountError}
100100

101101
err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt)
102102
if tt.expectErr {

0 commit comments

Comments
 (0)