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
e7b42f8a
Commit
e7b42f8a
authored
May 07, 2018
by
Michael Taufen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve test coverage of Kubelet file utils
Improves from 30.9% to 77.8%.
parent
f4b24526
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
187 additions
and
2 deletions
+187
-2
files.go
pkg/kubelet/kubeletconfig/util/files/files.go
+4
-2
files_test.go
pkg/kubelet/kubeletconfig/util/files/files_test.go
+183
-0
No files found.
pkg/kubelet/kubeletconfig/util/files/files.go
View file @
e7b42f8a
...
@@ -67,6 +67,7 @@ func EnsureFile(fs utilfs.Filesystem, path string) error {
...
@@ -67,6 +67,7 @@ func EnsureFile(fs utilfs.Filesystem, path string) error {
}
}
// WriteTmpFile creates a temporary file at `path`, writes `data` into it, and fsyncs the file
// WriteTmpFile creates a temporary file at `path`, writes `data` into it, and fsyncs the file
// Expects the parent directory to exist.
func
WriteTmpFile
(
fs
utilfs
.
Filesystem
,
path
string
,
data
[]
byte
)
(
tmpPath
string
,
retErr
error
)
{
func
WriteTmpFile
(
fs
utilfs
.
Filesystem
,
path
string
,
data
[]
byte
)
(
tmpPath
string
,
retErr
error
)
{
dir
:=
filepath
.
Dir
(
path
)
dir
:=
filepath
.
Dir
(
path
)
prefix
:=
tmptag
+
filepath
.
Base
(
path
)
prefix
:=
tmptag
+
filepath
.
Base
(
path
)
...
@@ -103,7 +104,8 @@ func WriteTmpFile(fs utilfs.Filesystem, path string, data []byte) (tmpPath strin
...
@@ -103,7 +104,8 @@ func WriteTmpFile(fs utilfs.Filesystem, path string, data []byte) (tmpPath strin
}
}
// ReplaceFile replaces the contents of the file at `path` with `data` by writing to a tmp file in the same
// ReplaceFile replaces the contents of the file at `path` with `data` by writing to a tmp file in the same
// dir as `path` and renaming the tmp file over `path`. The file does not have to exist to use ReplaceFile.
// dir as `path` and renaming the tmp file over `path`. The file does not have to exist to use ReplaceFile,
// but the parent directory must exist.
// Note ReplaceFile calls fsync.
// Note ReplaceFile calls fsync.
func
ReplaceFile
(
fs
utilfs
.
Filesystem
,
path
string
,
data
[]
byte
)
error
{
func
ReplaceFile
(
fs
utilfs
.
Filesystem
,
path
string
,
data
[]
byte
)
error
{
// write data to a temporary file
// write data to a temporary file
...
@@ -121,7 +123,7 @@ func DirExists(fs utilfs.Filesystem, path string) (bool, error) {
...
@@ -121,7 +123,7 @@ func DirExists(fs utilfs.Filesystem, path string) (bool, error) {
if
info
.
IsDir
()
{
if
info
.
IsDir
()
{
return
true
,
nil
return
true
,
nil
}
}
return
false
,
fmt
.
Errorf
(
"expected dir at %q, but mode is
is
%q"
,
path
,
info
.
Mode
()
.
String
())
return
false
,
fmt
.
Errorf
(
"expected dir at %q, but mode is %q"
,
path
,
info
.
Mode
()
.
String
())
}
else
if
os
.
IsNotExist
(
err
)
{
}
else
if
os
.
IsNotExist
(
err
)
{
return
false
,
nil
return
false
,
nil
}
else
{
}
else
{
...
...
pkg/kubelet/kubeletconfig/util/files/files_test.go
View file @
e7b42f8a
...
@@ -209,6 +209,189 @@ func TestHelpers(t *testing.T) {
...
@@ -209,6 +209,189 @@ func TestHelpers(t *testing.T) {
}
}
}
}
func
TestFileExists
(
t
*
testing
.
T
)
{
fn
:=
func
(
fs
utilfs
.
Filesystem
,
dir
string
,
c
*
test
)
[]
error
{
ok
,
err
:=
FileExists
(
fs
,
filepath
.
Join
(
dir
,
"foo"
))
if
err
!=
nil
{
return
[]
error
{
err
}
}
if
!
ok
{
return
[]
error
{
fmt
.
Errorf
(
"does not exist (test)"
)}
}
return
nil
}
cases
:=
[]
test
{
{
fn
:
fn
,
desc
:
"file exists"
,
writes
:
[]
file
{{
name
:
"foo"
}},
},
{
fn
:
fn
,
desc
:
"file does not exist"
,
err
:
"does not exist (test)"
,
},
{
fn
:
fn
,
desc
:
"object has non-file mode"
,
writes
:
[]
file
{{
name
:
"foo"
,
mode
:
os
.
ModeDir
}},
err
:
"expected regular file"
,
},
}
for
_
,
c
:=
range
cases
{
t
.
Run
(
c
.
desc
,
func
(
t
*
testing
.
T
)
{
c
.
run
(
t
,
utilfs
.
DefaultFs
{})
})
}
}
func
TestEnsureFile
(
t
*
testing
.
T
)
{
fn
:=
func
(
fs
utilfs
.
Filesystem
,
dir
string
,
c
*
test
)
[]
error
{
var
errs
[]
error
for
_
,
f
:=
range
c
.
expects
{
if
err
:=
EnsureFile
(
fs
,
filepath
.
Join
(
dir
,
f
.
name
));
err
!=
nil
{
errs
=
append
(
errs
,
err
)
}
}
return
errs
}
cases
:=
[]
test
{
{
fn
:
fn
,
desc
:
"file exists"
,
writes
:
[]
file
{{
name
:
"foo"
}},
expects
:
[]
file
{{
name
:
"foo"
}},
},
{
fn
:
fn
,
desc
:
"file does not exist"
,
expects
:
[]
file
{{
name
:
"bar"
}},
},
{
fn
:
fn
,
desc
:
"neither parent nor file exists"
,
expects
:
[]
file
{{
name
:
"baz/quux"
}},
},
}
for
_
,
c
:=
range
cases
{
t
.
Run
(
c
.
desc
,
func
(
t
*
testing
.
T
)
{
c
.
run
(
t
,
utilfs
.
DefaultFs
{})
})
}
}
// Note: This transitively tests WriteTmpFile
func
TestReplaceFile
(
t
*
testing
.
T
)
{
fn
:=
func
(
fs
utilfs
.
Filesystem
,
dir
string
,
c
*
test
)
[]
error
{
var
errs
[]
error
for
_
,
f
:=
range
c
.
expects
{
if
err
:=
ReplaceFile
(
fs
,
filepath
.
Join
(
dir
,
f
.
name
),
[]
byte
(
f
.
data
));
err
!=
nil
{
errs
=
append
(
errs
,
err
)
}
}
return
errs
}
cases
:=
[]
test
{
{
fn
:
fn
,
desc
:
"file exists"
,
writes
:
[]
file
{{
name
:
"foo"
}},
expects
:
[]
file
{{
name
:
"foo"
,
data
:
"bar"
}},
},
{
fn
:
fn
,
desc
:
"file does not exist"
,
expects
:
[]
file
{{
name
:
"foo"
,
data
:
"bar"
}},
},
{
fn
:
func
(
fs
utilfs
.
Filesystem
,
dir
string
,
c
*
test
)
[]
error
{
if
err
:=
ReplaceFile
(
fs
,
filepath
.
Join
(
dir
,
"foo/bar"
),
[]
byte
(
""
));
err
!=
nil
{
return
[]
error
{
err
}
}
return
nil
},
desc
:
"neither parent nor file exists"
,
err
:
"no such file or directory"
,
},
}
for
_
,
c
:=
range
cases
{
t
.
Run
(
c
.
desc
,
func
(
t
*
testing
.
T
)
{
c
.
run
(
t
,
utilfs
.
DefaultFs
{})
})
}
}
func
TestDirExists
(
t
*
testing
.
T
)
{
fn
:=
func
(
fs
utilfs
.
Filesystem
,
dir
string
,
c
*
test
)
[]
error
{
ok
,
err
:=
DirExists
(
fs
,
filepath
.
Join
(
dir
,
"foo"
))
if
err
!=
nil
{
return
[]
error
{
err
}
}
if
!
ok
{
return
[]
error
{
fmt
.
Errorf
(
"does not exist (test)"
)}
}
return
nil
}
cases
:=
[]
test
{
{
fn
:
fn
,
desc
:
"dir exists"
,
writes
:
[]
file
{{
name
:
"foo"
,
mode
:
os
.
ModeDir
}},
},
{
fn
:
fn
,
desc
:
"dir does not exist"
,
err
:
"does not exist (test)"
,
},
{
fn
:
fn
,
desc
:
"object has non-dir mode"
,
writes
:
[]
file
{{
name
:
"foo"
}},
err
:
"expected dir"
,
},
}
for
_
,
c
:=
range
cases
{
t
.
Run
(
c
.
desc
,
func
(
t
*
testing
.
T
)
{
c
.
run
(
t
,
utilfs
.
DefaultFs
{})
})
}
}
func
TestEnsureDir
(
t
*
testing
.
T
)
{
fn
:=
func
(
fs
utilfs
.
Filesystem
,
dir
string
,
c
*
test
)
[]
error
{
var
errs
[]
error
for
_
,
f
:=
range
c
.
expects
{
if
err
:=
EnsureDir
(
fs
,
filepath
.
Join
(
dir
,
f
.
name
));
err
!=
nil
{
errs
=
append
(
errs
,
err
)
}
}
return
errs
}
cases
:=
[]
test
{
{
fn
:
fn
,
desc
:
"dir exists"
,
writes
:
[]
file
{{
name
:
"foo"
,
mode
:
os
.
ModeDir
}},
expects
:
[]
file
{{
name
:
"foo"
,
mode
:
os
.
ModeDir
}},
},
{
fn
:
fn
,
desc
:
"dir does not exist"
,
expects
:
[]
file
{{
name
:
"bar"
,
mode
:
os
.
ModeDir
}},
},
{
fn
:
fn
,
desc
:
"neither parent nor dir exists"
,
expects
:
[]
file
{{
name
:
"baz/quux"
,
mode
:
os
.
ModeDir
}},
},
}
for
_
,
c
:=
range
cases
{
t
.
Run
(
c
.
desc
,
func
(
t
*
testing
.
T
)
{
c
.
run
(
t
,
utilfs
.
DefaultFs
{})
})
}
}
func
TestWriteTempDir
(
t
*
testing
.
T
)
{
func
TestWriteTempDir
(
t
*
testing
.
T
)
{
// writing a tmp dir is covered by TestReplaceDir, but we additionally test filename validation here
// writing a tmp dir is covered by TestReplaceDir, but we additionally test filename validation here
c
:=
test
{
c
:=
test
{
...
...
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