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
9a70273e
Commit
9a70273e
authored
Oct 23, 2017
by
chentao1596
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding unit tests to methods of file's util
parent
6f06408e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
161 additions
and
0 deletions
+161
-0
BUILD
pkg/util/file/BUILD
+12
-0
file_test.go
pkg/util/file/file_test.go
+149
-0
No files found.
pkg/util/file/BUILD
View file @
9a70273e
...
...
@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
...
...
@@ -23,3 +24,14 @@ filegroup(
srcs = [":package-srcs"],
tags = ["automanaged"],
)
go_test(
name = "go_default_test",
srcs = ["file_test.go"],
importpath = "k8s.io/kubernetes/pkg/util/file",
library = ":go_default_library",
deps = [
"//vendor/github.com/spf13/afero:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
],
)
pkg/util/file/file_test.go
0 → 100644
View file @
9a70273e
/*
Copyright 2017 The Kubernetes 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
file
import
(
"os"
"path/filepath"
"sort"
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
func
RecoverEnv
(
wd
,
tmpDir
string
)
{
os
.
Chdir
(
wd
)
os
.
RemoveAll
(
tmpDir
)
}
func
TestFileUtils
(
t
*
testing
.
T
)
{
fs
:=
&
afero
.
Afero
{
Fs
:
afero
.
NewOsFs
()}
// Create tmp dir
tmpDir
,
err
:=
fs
.
TempDir
(
os
.
TempDir
(),
"util_file_test_"
)
if
err
!=
nil
{
t
.
Fatal
(
"Failed to test: failed to create temp dir."
)
}
// create tmp file
tmpFile
,
err
:=
fs
.
TempFile
(
tmpDir
,
"test_file_exists_"
)
if
err
!=
nil
{
t
.
Fatal
(
"Failed to test: failed to create temp file."
)
}
// create tmp sym link
tmpSymlinkName
:=
filepath
.
Join
(
tmpDir
,
"test_file_exists_sym_link"
)
err
=
os
.
Symlink
(
tmpFile
.
Name
(),
tmpSymlinkName
)
if
err
!=
nil
{
t
.
Fatal
(
"Failed to test: failed to create sym link."
)
}
// create tmp sub dir
tmpSubDir
,
err
:=
fs
.
TempDir
(
tmpDir
,
"sub_"
)
if
err
!=
nil
{
t
.
Fatal
(
"Failed to test: failed to create temp sub dir."
)
}
// record the current dir
currentDir
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
t
.
Fatal
(
"Failed to test: failed to get current dir."
)
}
// change the work dir to temp dir
err
=
os
.
Chdir
(
tmpDir
)
if
err
!=
nil
{
t
.
Fatal
(
"Failed to test: failed to change work dir."
)
}
// recover test enviroment
defer
RecoverEnv
(
currentDir
,
tmpDir
)
t
.
Run
(
"TestFileExists"
,
func
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
fileName
string
expectedError
bool
expectedValue
bool
}{
{
"file_not_exists"
,
filepath
.
Join
(
tmpDir
,
"file_not_exist_case"
),
false
,
false
},
{
"file_exists"
,
tmpFile
.
Name
(),
false
,
true
},
}
for
_
,
test
:=
range
tests
{
realValued
,
realError
:=
FileExists
(
test
.
fileName
)
if
test
.
expectedError
{
assert
.
Errorf
(
t
,
realError
,
"Failed to test with '%s': %s"
,
test
.
fileName
,
test
.
name
)
}
else
{
assert
.
EqualValuesf
(
t
,
test
.
expectedValue
,
realValued
,
"Failed to test with '%s': %s"
,
test
.
fileName
,
test
.
name
)
}
}
})
t
.
Run
(
"TestFileOrSymlinkExists"
,
func
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
fileName
string
expectedError
bool
expectedValue
bool
}{
{
"file_not_exists"
,
filepath
.
Join
(
tmpDir
,
"file_not_exist_case"
),
false
,
false
},
{
"file_exists"
,
tmpFile
.
Name
(),
false
,
true
},
{
"symlink_exists"
,
tmpSymlinkName
,
false
,
true
},
}
for
_
,
test
:=
range
tests
{
realValued
,
realError
:=
FileOrSymlinkExists
(
test
.
fileName
)
if
test
.
expectedError
{
assert
.
Errorf
(
t
,
realError
,
"Failed to test with '%s': %s"
,
test
.
fileName
,
test
.
name
)
}
else
{
assert
.
EqualValuesf
(
t
,
test
.
expectedValue
,
realValued
,
"Failed to test with '%s': %s"
,
test
.
fileName
,
test
.
name
)
}
}
})
t
.
Run
(
"TestReadDirNoStat"
,
func
(
t
*
testing
.
T
)
{
_
,
tmpFileSimpleName
:=
filepath
.
Split
(
tmpFile
.
Name
())
_
,
tmpSymlinkSimpleName
:=
filepath
.
Split
(
tmpSymlinkName
)
_
,
tmpSubDirSimpleName
:=
filepath
.
Split
(
tmpSubDir
)
tests
:=
[]
struct
{
name
string
dirName
string
expectedError
bool
expectedValue
[]
string
}{
{
"dir_not_exists"
,
filepath
.
Join
(
tmpDir
,
"file_not_exist_case"
),
true
,
[]
string
{}},
{
"dir_is_empty"
,
""
,
false
,
[]
string
{
tmpFileSimpleName
,
tmpSymlinkSimpleName
,
tmpSubDirSimpleName
}},
{
"dir_exists"
,
tmpDir
,
false
,
[]
string
{
tmpFileSimpleName
,
tmpSymlinkSimpleName
,
tmpSubDirSimpleName
}},
}
for
_
,
test
:=
range
tests
{
realValued
,
realError
:=
ReadDirNoStat
(
test
.
dirName
)
// execute sort action before compare
sort
.
Strings
(
realValued
)
sort
.
Strings
(
test
.
expectedValue
)
if
test
.
expectedError
{
assert
.
Errorf
(
t
,
realError
,
"Failed to test with '%s': %s"
,
test
.
dirName
,
test
.
name
)
}
else
{
assert
.
EqualValuesf
(
t
,
test
.
expectedValue
,
realValued
,
"Failed to test with '%s': %s"
,
test
.
dirName
,
test
.
name
)
}
}
})
}
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