Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
2cba4e37
Commit
2cba4e37
authored
Dec 27, 2018
by
Michelle Au
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit test for UnmountMountPoint
parent
99a3b4da
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
29 deletions
+87
-29
fake.go
pkg/util/mount/fake.go
+9
-1
mount_helper_test.go
pkg/util/mount/mount_helper_test.go
+78
-28
No files found.
pkg/util/mount/fake.go
View file @
2cba4e37
...
@@ -30,6 +30,8 @@ type FakeMounter struct {
...
@@ -30,6 +30,8 @@ type FakeMounter struct {
MountPoints
[]
MountPoint
MountPoints
[]
MountPoint
Log
[]
FakeAction
Log
[]
FakeAction
Filesystem
map
[
string
]
FileType
Filesystem
map
[
string
]
FileType
// Error to return for a path when calling IsLikelyNotMountPoint
MountCheckErrors
map
[
string
]
error
// Some tests run things in parallel, make sure the mounter does not produce
// Some tests run things in parallel, make sure the mounter does not produce
// any golang's DATA RACE warnings.
// any golang's DATA RACE warnings.
mutex
sync
.
Mutex
mutex
sync
.
Mutex
...
@@ -119,6 +121,7 @@ func (f *FakeMounter) Unmount(target string) error {
...
@@ -119,6 +121,7 @@ func (f *FakeMounter) Unmount(target string) error {
}
}
f
.
MountPoints
=
newMountpoints
f
.
MountPoints
=
newMountpoints
f
.
Log
=
append
(
f
.
Log
,
FakeAction
{
Action
:
FakeActionUnmount
,
Target
:
absTarget
})
f
.
Log
=
append
(
f
.
Log
,
FakeAction
{
Action
:
FakeActionUnmount
,
Target
:
absTarget
})
delete
(
f
.
MountCheckErrors
,
target
)
return
nil
return
nil
}
}
...
@@ -141,7 +144,12 @@ func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
...
@@ -141,7 +144,12 @@ func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
f
.
mutex
.
Lock
()
f
.
mutex
.
Lock
()
defer
f
.
mutex
.
Unlock
()
defer
f
.
mutex
.
Unlock
()
_
,
err
:=
os
.
Stat
(
file
)
err
:=
f
.
MountCheckErrors
[
file
]
if
err
!=
nil
{
return
false
,
err
}
_
,
err
=
os
.
Stat
(
file
)
if
err
!=
nil
{
if
err
!=
nil
{
return
true
,
err
return
true
,
err
}
}
...
...
pkg/util/mount/mount_helper_test.go
View file @
2cba4e37
...
@@ -17,47 +17,97 @@ limitations under the License.
...
@@ -17,47 +17,97 @@ limitations under the License.
package
mount
package
mount
import
(
import
(
"io/ioutil"
"os"
"os"
"path/filepath"
"syscall"
"testing"
"testing"
utiltesting
"k8s.io/client-go/util/testing"
)
)
func
TestDoUnmountMountPoint
(
t
*
testing
.
T
)
{
func
TestDoUnmountMountPoint
(
t
*
testing
.
T
)
{
const
testMount
=
"test-mount"
const
defaultPerm
=
0750
tmpDir1
,
err1
:=
utiltesting
.
MkTmpdir
(
"umount_test1"
)
tests
:=
map
[
string
]
struct
{
if
err1
!=
nil
{
t
.
Fatalf
(
"error creating temp dir: %v"
,
err1
)
}
defer
os
.
RemoveAll
(
tmpDir1
)
tmpDir2
,
err2
:=
utiltesting
.
MkTmpdir
(
"umount_test2"
)
if
err2
!=
nil
{
t
.
Fatalf
(
"error creating temp dir: %v"
,
err2
)
}
defer
os
.
RemoveAll
(
tmpDir2
)
// Second part: want no error
tests
:=
[]
struct
{
mountPath
string
corruptedMnt
bool
corruptedMnt
bool
// Function that prepares the directory structure for the test under
// the given base directory.
// Returns a fake MountPoint, a fake error for the mount point,
// and error if the prepare function encountered a fatal error.
prepare
func
(
base
string
)
(
MountPoint
,
error
,
error
)
expectErr
bool
}{
}{
{
"mount-ok"
:
{
mountPath
:
tmpDir1
,
prepare
:
func
(
base
string
)
(
MountPoint
,
error
,
error
)
{
path
:=
filepath
.
Join
(
base
,
testMount
)
if
err
:=
os
.
MkdirAll
(
path
,
defaultPerm
);
err
!=
nil
{
return
MountPoint
{},
nil
,
err
}
return
MountPoint
{
Device
:
"/dev/sdb"
,
Path
:
path
},
nil
,
nil
},
},
"mount-corrupted"
:
{
prepare
:
func
(
base
string
)
(
MountPoint
,
error
,
error
)
{
path
:=
filepath
.
Join
(
base
,
testMount
)
if
err
:=
os
.
MkdirAll
(
path
,
defaultPerm
);
err
!=
nil
{
return
MountPoint
{},
nil
,
err
}
return
MountPoint
{
Device
:
"/dev/sdb"
,
Path
:
path
},
os
.
NewSyscallError
(
"fake"
,
syscall
.
ESTALE
),
nil
},
corruptedMnt
:
true
,
corruptedMnt
:
true
,
},
},
{
"mount-err-not-corrupted"
:
{
mountPath
:
tmpDir2
,
prepare
:
func
(
base
string
)
(
MountPoint
,
error
,
error
)
{
corruptedMnt
:
false
,
path
:=
filepath
.
Join
(
base
,
testMount
)
if
err
:=
os
.
MkdirAll
(
path
,
defaultPerm
);
err
!=
nil
{
return
MountPoint
{},
nil
,
err
}
return
MountPoint
{
Device
:
"/dev/sdb"
,
Path
:
path
},
os
.
NewSyscallError
(
"fake"
,
syscall
.
ETIMEDOUT
),
nil
},
expectErr
:
true
,
},
},
}
}
fake
:=
&
FakeMounter
{}
for
name
,
tt
:=
range
tests
{
t
.
Run
(
name
,
func
(
t
*
testing
.
T
)
{
tmpDir
,
err
:=
ioutil
.
TempDir
(
""
,
"unmount-mount-point-test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to create tmpdir: %v"
,
err
)
}
defer
os
.
RemoveAll
(
tmpDir
)
if
tt
.
prepare
==
nil
{
t
.
Fatalf
(
"prepare function required"
)
}
mountPoint
,
mountError
,
err
:=
tt
.
prepare
(
tmpDir
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to prepare test: %v"
,
err
)
}
fake
:=
&
FakeMounter
{
MountPoints
:
[]
MountPoint
{
mountPoint
},
MountCheckErrors
:
map
[
string
]
error
{
mountPoint
.
Path
:
mountError
},
}
for
_
,
tt
:=
range
tests
{
err
=
doUnmountMountPoint
(
mountPoint
.
Path
,
fake
,
true
,
tt
.
corruptedMnt
)
err
:=
doUnmountMountPoint
(
tt
.
mountPath
,
fake
,
false
,
tt
.
corruptedMnt
)
if
tt
.
expectErr
{
if
err
!=
nil
{
if
err
==
nil
{
t
.
Errorf
(
"err Expected nil, but got: %v"
,
err
)
t
.
Errorf
(
"test %s failed, expected error, got none"
,
name
)
}
}
if
err
:=
validateDirExists
(
mountPoint
.
Path
);
err
!=
nil
{
t
.
Errorf
(
"test %s failed, mount path doesn't exist: %v"
,
name
,
err
)
}
}
if
!
tt
.
expectErr
{
if
err
!=
nil
{
t
.
Errorf
(
"test %s failed: %v"
,
name
,
err
)
}
if
err
:=
validateDirNotExists
(
mountPoint
.
Path
);
err
!=
nil
{
t
.
Errorf
(
"test %s failed, mount path still exists: %v"
,
name
,
err
)
}
}
})
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment