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
ed506328
Commit
ed506328
authored
Apr 05, 2018
by
Di Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for GetFileType
parent
9c7604fe
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
161 additions
and
1 deletion
+161
-1
BUILD
pkg/util/mount/BUILD
+1
-0
mount_linux_test.go
pkg/util/mount/mount_linux_test.go
+110
-1
mount_windows_test.go
pkg/util/mount/mount_windows_test.go
+50
-0
No files found.
pkg/util/mount/BUILD
View file @
ed506328
...
@@ -102,6 +102,7 @@ go_test(
...
@@ -102,6 +102,7 @@ go_test(
] + select({
] + select({
"@io_bazel_rules_go//go/platform:linux": [
"@io_bazel_rules_go//go/platform:linux": [
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/utils/exec:go_default_library",
],
],
"@io_bazel_rules_go//go/platform:windows": [
"@io_bazel_rules_go//go/platform:windows": [
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
...
...
pkg/util/mount/mount_linux_test.go
View file @
ed506328
...
@@ -25,10 +25,12 @@ import (
...
@@ -25,10 +25,12 @@ import (
"os"
"os"
"path/filepath"
"path/filepath"
"reflect"
"reflect"
"strconv"
"strings"
"syscall"
"syscall"
"testing"
"testing"
"
strconv
"
"
k8s.io/utils/exec
"
"github.com/golang/glog"
"github.com/golang/glog"
)
)
...
@@ -1680,3 +1682,110 @@ func TestFindExistingPrefix(t *testing.T) {
...
@@ -1680,3 +1682,110 @@ func TestFindExistingPrefix(t *testing.T) {
os
.
RemoveAll
(
base
)
os
.
RemoveAll
(
base
)
}
}
}
}
func
TestGetFileType
(
t
*
testing
.
T
)
{
mounter
:=
Mounter
{
"fake/path"
,
false
}
testCase
:=
[]
struct
{
name
string
expectedType
FileType
setUp
func
()
(
string
,
string
,
error
)
}{
{
"Directory Test"
,
FileTypeDirectory
,
func
()
(
string
,
string
,
error
)
{
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
"test-get-filetype-"
)
return
tempDir
,
tempDir
,
err
},
},
{
"File Test"
,
FileTypeFile
,
func
()
(
string
,
string
,
error
)
{
tempFile
,
err
:=
ioutil
.
TempFile
(
""
,
"test-get-filetype"
)
if
err
!=
nil
{
return
""
,
""
,
err
}
tempFile
.
Close
()
return
tempFile
.
Name
(),
tempFile
.
Name
(),
nil
},
},
{
"Socket Test"
,
FileTypeSocket
,
func
()
(
string
,
string
,
error
)
{
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
"test-get-filetype-"
)
if
err
!=
nil
{
return
""
,
""
,
err
}
tempSocketFile
,
err
:=
createSocketFile
(
tempDir
)
return
tempSocketFile
,
tempDir
,
err
},
},
{
"Block Device Test"
,
FileTypeBlockDev
,
func
()
(
string
,
string
,
error
)
{
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
"test-get-filetype-"
)
if
err
!=
nil
{
return
""
,
""
,
err
}
tempBlockFile
:=
filepath
.
Join
(
tempDir
,
"test_blk_dev"
)
outputBytes
,
err
:=
exec
.
New
()
.
Command
(
"mknod"
,
tempBlockFile
,
"b"
,
"89"
,
"1"
)
.
CombinedOutput
()
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"%v: %s "
,
err
,
outputBytes
)
}
return
tempBlockFile
,
tempDir
,
err
},
},
{
"Character Device Test"
,
FileTypeCharDev
,
func
()
(
string
,
string
,
error
)
{
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
"test-get-filetype-"
)
if
err
!=
nil
{
return
""
,
""
,
err
}
tempCharFile
:=
filepath
.
Join
(
tempDir
,
"test_char_dev"
)
outputBytes
,
err
:=
exec
.
New
()
.
Command
(
"mknod"
,
tempCharFile
,
"c"
,
"89"
,
"1"
)
.
CombinedOutput
()
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"%v: %s "
,
err
,
outputBytes
)
}
return
tempCharFile
,
tempDir
,
err
},
},
}
for
idx
,
tc
:=
range
testCase
{
path
,
cleanUpPath
,
err
:=
tc
.
setUp
()
if
err
!=
nil
{
// Locally passed, but upstream CI is not friendly to create such device files
// Leave "Operation not permitted" out, which can be covered in an e2e test
if
isOperationNotPermittedError
(
err
)
{
continue
}
t
.
Fatalf
(
"[%d-%s] unexpected error : %v"
,
idx
,
tc
.
name
,
err
)
}
if
len
(
cleanUpPath
)
>
0
{
defer
os
.
RemoveAll
(
cleanUpPath
)
}
fileType
,
err
:=
mounter
.
GetFileType
(
path
)
if
err
!=
nil
{
t
.
Fatalf
(
"[%d-%s] unexpected error : %v"
,
idx
,
tc
.
name
,
err
)
}
if
fileType
!=
tc
.
expectedType
{
t
.
Fatalf
(
"[%d-%s] expected %s, but got %s"
,
idx
,
tc
.
name
,
tc
.
expectedType
,
fileType
)
}
}
}
func
isOperationNotPermittedError
(
err
error
)
bool
{
if
strings
.
Contains
(
err
.
Error
(),
"Operation not permitted"
)
{
return
true
}
return
false
}
pkg/util/mount/mount_windows_test.go
View file @
ed506328
...
@@ -20,6 +20,7 @@ package mount
...
@@ -20,6 +20,7 @@ package mount
import
(
import
(
"fmt"
"fmt"
"io/ioutil"
"os"
"os"
"os/exec"
"os/exec"
"path/filepath"
"path/filepath"
...
@@ -551,3 +552,52 @@ func TestPathWithinBase(t *testing.T) {
...
@@ -551,3 +552,52 @@ func TestPathWithinBase(t *testing.T) {
test
.
fullPath
,
test
.
basePath
,
result
,
test
.
expectedResult
)
test
.
fullPath
,
test
.
basePath
,
result
,
test
.
expectedResult
)
}
}
}
}
func
TestGetFileType
(
t
*
testing
.
T
)
{
mounter
:=
New
(
"fake/path"
)
testCase
:=
[]
struct
{
name
string
expectedType
FileType
setUp
func
()
(
string
,
string
,
error
)
}{
{
"Directory Test"
,
FileTypeDirectory
,
func
()
(
string
,
string
,
error
)
{
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
"test-get-filetype-"
)
return
tempDir
,
tempDir
,
err
},
},
{
"File Test"
,
FileTypeFile
,
func
()
(
string
,
string
,
error
)
{
tempFile
,
err
:=
ioutil
.
TempFile
(
""
,
"test-get-filetype"
)
if
err
!=
nil
{
return
""
,
""
,
err
}
tempFile
.
Close
()
return
tempFile
.
Name
(),
tempFile
.
Name
(),
nil
},
},
}
for
idx
,
tc
:=
range
testCase
{
path
,
cleanUpPath
,
err
:=
tc
.
setUp
()
if
err
!=
nil
{
t
.
Fatalf
(
"[%d-%s] unexpected error : %v"
,
idx
,
tc
.
name
,
err
)
}
if
len
(
cleanUpPath
)
>
0
{
defer
os
.
RemoveAll
(
cleanUpPath
)
}
fileType
,
err
:=
mounter
.
GetFileType
(
path
)
if
err
!=
nil
{
t
.
Fatalf
(
"[%d-%s] unexpected error : %v"
,
idx
,
tc
.
name
,
err
)
}
if
fileType
!=
tc
.
expectedType
{
t
.
Fatalf
(
"[%d-%s] expected %s, but got %s"
,
idx
,
tc
.
name
,
tc
.
expectedType
,
fileType
)
}
}
}
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