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
9ca4fb40
Commit
9ca4fb40
authored
May 26, 2015
by
Saad Ali
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8844 from yujuhong/kubelet_unittest
Kubelet: clean up unit tests
parents
2c92915f
9a71fb93
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
17 deletions
+119
-17
fake_runtime.go
pkg/kubelet/container/fake_runtime.go
+115
-13
runtime_cache_test.go
pkg/kubelet/container/runtime_cache_test.go
+4
-4
manager_test.go
pkg/kubelet/dockertools/manager_test.go
+0
-0
kubelet_test.go
pkg/kubelet/kubelet_test.go
+0
-0
No files found.
pkg/kubelet/container/fake_runtime.go
View file @
9ca4fb40
...
@@ -18,6 +18,7 @@ package container
...
@@ -18,6 +18,7 @@ package container
import
(
import
(
"fmt"
"fmt"
"io"
"reflect"
"reflect"
"sync"
"sync"
"time"
"time"
...
@@ -30,17 +31,39 @@ import (
...
@@ -30,17 +31,39 @@ import (
type
FakeRuntime
struct
{
type
FakeRuntime
struct
{
sync
.
Mutex
sync
.
Mutex
CalledFunctions
[]
string
CalledFunctions
[]
string
Pod
l
ist
[]
*
Pod
Pod
L
ist
[]
*
Pod
ContainerList
[]
*
Container
ContainerList
[]
*
Container
ImageList
[]
Image
PodStatus
api
.
PodStatus
PodStatus
api
.
PodStatus
StartedPods
[]
string
StartedPods
[]
string
KilledPods
[]
string
KilledPods
[]
string
StartedContainers
[]
string
StartedContainers
[]
string
KilledContainers
[]
string
KilledContainers
[]
string
VersionInfo
map
[
string
]
string
VersionInfo
string
Err
error
Err
error
}
}
// FakeRuntime should implement Runtime.
var
_
Runtime
=
&
FakeRuntime
{}
type
FakeVersion
struct
{
Version
string
}
func
(
fv
*
FakeVersion
)
String
()
string
{
return
fv
.
Version
}
func
(
fv
*
FakeVersion
)
Compare
(
other
string
)
(
int
,
error
)
{
result
:=
0
if
fv
.
Version
>
other
{
result
=
1
}
else
if
fv
.
Version
<
other
{
result
=
-
1
}
return
result
,
nil
}
type
FakeRuntimeCache
struct
{
type
FakeRuntimeCache
struct
{
getter
podsGetter
getter
podsGetter
}
}
...
@@ -63,14 +86,14 @@ func (f *FakeRuntime) ClearCalls() {
...
@@ -63,14 +86,14 @@ func (f *FakeRuntime) ClearCalls() {
defer
f
.
Unlock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
[]
string
{}
f
.
CalledFunctions
=
[]
string
{}
f
.
Pod
l
ist
=
[]
*
Pod
{}
f
.
Pod
L
ist
=
[]
*
Pod
{}
f
.
ContainerList
=
[]
*
Container
{}
f
.
ContainerList
=
[]
*
Container
{}
f
.
PodStatus
=
api
.
PodStatus
{}
f
.
PodStatus
=
api
.
PodStatus
{}
f
.
StartedPods
=
[]
string
{}
f
.
StartedPods
=
[]
string
{}
f
.
KilledPods
=
[]
string
{}
f
.
KilledPods
=
[]
string
{}
f
.
StartedContainers
=
[]
string
{}
f
.
StartedContainers
=
[]
string
{}
f
.
KilledContainers
=
[]
string
{}
f
.
KilledContainers
=
[]
string
{}
f
.
VersionInfo
=
map
[
string
]
string
{}
f
.
VersionInfo
=
""
f
.
Err
=
nil
f
.
Err
=
nil
}
}
...
@@ -112,12 +135,12 @@ func (f *FakeRuntime) AssertKilledContainers(containers []string) error {
...
@@ -112,12 +135,12 @@ func (f *FakeRuntime) AssertKilledContainers(containers []string) error {
return
f
.
assertList
(
containers
,
f
.
KilledContainers
)
return
f
.
assertList
(
containers
,
f
.
KilledContainers
)
}
}
func
(
f
*
FakeRuntime
)
Version
()
(
map
[
string
]
string
,
error
)
{
func
(
f
*
FakeRuntime
)
Version
()
(
Version
,
error
)
{
f
.
Lock
()
f
.
Lock
()
defer
f
.
Unlock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"Version"
)
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"Version"
)
return
f
.
VersionInfo
,
f
.
Err
return
&
FakeVersion
{
Version
:
f
.
VersionInfo
}
,
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
GetPods
(
all
bool
)
([]
*
Pod
,
error
)
{
func
(
f
*
FakeRuntime
)
GetPods
(
all
bool
)
([]
*
Pod
,
error
)
{
...
@@ -125,10 +148,10 @@ func (f *FakeRuntime) GetPods(all bool) ([]*Pod, error) {
...
@@ -125,10 +148,10 @@ func (f *FakeRuntime) GetPods(all bool) ([]*Pod, error) {
defer
f
.
Unlock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetPods"
)
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetPods"
)
return
f
.
Pod
l
ist
,
f
.
Err
return
f
.
Pod
L
ist
,
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
SyncPod
(
pod
*
api
.
Pod
,
_
Pod
,
_
api
.
PodStatus
)
error
{
func
(
f
*
FakeRuntime
)
SyncPod
(
pod
*
api
.
Pod
,
_
Pod
,
_
api
.
PodStatus
,
_
[]
api
.
Secret
)
error
{
f
.
Lock
()
f
.
Lock
()
defer
f
.
Unlock
()
defer
f
.
Unlock
()
...
@@ -140,13 +163,13 @@ func (f *FakeRuntime) SyncPod(pod *api.Pod, _ Pod, _ api.PodStatus) error {
...
@@ -140,13 +163,13 @@ func (f *FakeRuntime) SyncPod(pod *api.Pod, _ Pod, _ api.PodStatus) error {
return
f
.
Err
return
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
KillPod
(
pod
*
api
.
Pod
)
error
{
func
(
f
*
FakeRuntime
)
KillPod
(
pod
Pod
)
error
{
f
.
Lock
()
f
.
Lock
()
defer
f
.
Unlock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"KillPod"
)
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"KillPod"
)
f
.
KilledPods
=
append
(
f
.
KilledPods
,
string
(
pod
.
U
ID
))
f
.
KilledPods
=
append
(
f
.
KilledPods
,
string
(
pod
.
ID
))
for
_
,
c
:=
range
pod
.
Spec
.
Containers
{
for
_
,
c
:=
range
pod
.
Containers
{
f
.
KilledContainers
=
append
(
f
.
KilledContainers
,
c
.
Name
)
f
.
KilledContainers
=
append
(
f
.
KilledContainers
,
c
.
Name
)
}
}
return
f
.
Err
return
f
.
Err
...
@@ -186,12 +209,13 @@ func (f *FakeRuntime) KillContainerInPod(container api.Container, pod *api.Pod)
...
@@ -186,12 +209,13 @@ func (f *FakeRuntime) KillContainerInPod(container api.Container, pod *api.Pod)
return
f
.
Err
return
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
GetPodStatus
(
pod
*
Pod
)
(
api
.
PodStatus
,
error
)
{
func
(
f
*
FakeRuntime
)
GetPodStatus
(
*
api
.
Pod
)
(
*
api
.
PodStatus
,
error
)
{
f
.
Lock
()
f
.
Lock
()
defer
f
.
Unlock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetPodStatus"
)
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetPodStatus"
)
return
f
.
PodStatus
,
f
.
Err
status
:=
f
.
PodStatus
return
&
status
,
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
GetContainers
(
all
bool
)
([]
*
Container
,
error
)
{
func
(
f
*
FakeRuntime
)
GetContainers
(
all
bool
)
([]
*
Container
,
error
)
{
...
@@ -201,3 +225,81 @@ func (f *FakeRuntime) GetContainers(all bool) ([]*Container, error) {
...
@@ -201,3 +225,81 @@ func (f *FakeRuntime) GetContainers(all bool) ([]*Container, error) {
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetContainers"
)
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetContainers"
)
return
f
.
ContainerList
,
f
.
Err
return
f
.
ContainerList
,
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
ExecInContainer
(
containerID
string
,
cmd
[]
string
,
stdin
io
.
Reader
,
stdout
,
stderr
io
.
WriteCloser
,
tty
bool
)
error
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"ExecInContainer"
)
return
f
.
Err
}
func
(
f
*
FakeRuntime
)
RunInContainer
(
containerID
string
,
cmd
[]
string
)
([]
byte
,
error
)
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"RunInContainer"
)
return
[]
byte
{},
f
.
Err
}
func
(
f
*
FakeRuntime
)
GetContainerLogs
(
pod
*
api
.
Pod
,
containerID
,
tail
string
,
follow
bool
,
stdout
,
stderr
io
.
Writer
)
(
err
error
)
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"GetContainerLogs"
)
return
f
.
Err
}
func
(
f
*
FakeRuntime
)
PullImage
(
image
ImageSpec
,
pullSecrets
[]
api
.
Secret
)
error
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"PullImage"
)
return
f
.
Err
}
func
(
f
*
FakeRuntime
)
IsImagePresent
(
image
ImageSpec
)
(
bool
,
error
)
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"IsImagePresent"
)
for
_
,
i
:=
range
f
.
ImageList
{
if
i
.
ID
==
image
.
Image
{
return
true
,
f
.
Err
}
}
return
false
,
f
.
Err
}
func
(
f
*
FakeRuntime
)
ListImages
()
([]
Image
,
error
)
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"ListImages"
)
return
f
.
ImageList
,
f
.
Err
}
func
(
f
*
FakeRuntime
)
RemoveImage
(
image
ImageSpec
)
error
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"RemoveImage"
)
index
:=
0
for
i
:=
range
f
.
ImageList
{
if
f
.
ImageList
[
i
]
.
ID
==
image
.
Image
{
index
=
i
break
}
}
f
.
ImageList
=
append
(
f
.
ImageList
[
:
index
],
f
.
ImageList
[
index
+
1
:
]
...
)
return
f
.
Err
}
func
(
f
*
FakeRuntime
)
PortForward
(
pod
*
Pod
,
port
uint16
,
stream
io
.
ReadWriteCloser
)
error
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
CalledFunctions
=
append
(
f
.
CalledFunctions
,
"PortForward"
)
return
f
.
Err
}
pkg/kubelet/container/runtime_cache_test.go
View file @
9ca4fb40
...
@@ -48,7 +48,7 @@ func newTestRuntimeCache(getter podsGetter) *testRuntimeCache {
...
@@ -48,7 +48,7 @@ func newTestRuntimeCache(getter podsGetter) *testRuntimeCache {
func
TestGetPods
(
t
*
testing
.
T
)
{
func
TestGetPods
(
t
*
testing
.
T
)
{
runtime
:=
&
FakeRuntime
{}
runtime
:=
&
FakeRuntime
{}
expected
:=
[]
*
Pod
{{
ID
:
"1111"
},
{
ID
:
"2222"
},
{
ID
:
"3333"
}}
expected
:=
[]
*
Pod
{{
ID
:
"1111"
},
{
ID
:
"2222"
},
{
ID
:
"3333"
}}
runtime
.
Pod
l
ist
=
expected
runtime
.
Pod
L
ist
=
expected
cache
:=
newTestRuntimeCache
(
runtime
)
cache
:=
newTestRuntimeCache
(
runtime
)
actual
,
err
:=
cache
.
GetPods
()
actual
,
err
:=
cache
.
GetPods
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -65,12 +65,12 @@ func TestForceUpdateIfOlder(t *testing.T) {
...
@@ -65,12 +65,12 @@ func TestForceUpdateIfOlder(t *testing.T) {
// Cache old pods.
// Cache old pods.
oldpods
:=
[]
*
Pod
{{
ID
:
"1111"
}}
oldpods
:=
[]
*
Pod
{{
ID
:
"1111"
}}
runtime
.
Pod
l
ist
=
oldpods
runtime
.
Pod
L
ist
=
oldpods
cache
.
updateCacheWithLock
()
cache
.
updateCacheWithLock
()
// Update the runtime to new pods.
// Update the runtime to new pods.
newpods
:=
[]
*
Pod
{{
ID
:
"1111"
},
{
ID
:
"2222"
},
{
ID
:
"3333"
}}
newpods
:=
[]
*
Pod
{{
ID
:
"1111"
},
{
ID
:
"2222"
},
{
ID
:
"3333"
}}
runtime
.
Pod
l
ist
=
newpods
runtime
.
Pod
L
ist
=
newpods
// An older timestamp should not force an update.
// An older timestamp should not force an update.
cache
.
ForceUpdateIfOlder
(
time
.
Now
()
.
Add
(
-
20
*
time
.
Minute
))
cache
.
ForceUpdateIfOlder
(
time
.
Now
()
.
Add
(
-
20
*
time
.
Minute
))
...
@@ -100,7 +100,7 @@ func TestUpdatePodsOnlyIfNewer(t *testing.T) {
...
@@ -100,7 +100,7 @@ func TestUpdatePodsOnlyIfNewer(t *testing.T) {
// Instruct runime to return a list of old pods.
// Instruct runime to return a list of old pods.
oldpods
:=
[]
*
Pod
{{
ID
:
"1111"
}}
oldpods
:=
[]
*
Pod
{{
ID
:
"1111"
}}
runtime
.
Pod
l
ist
=
oldpods
runtime
.
Pod
L
ist
=
oldpods
// Try to update the cache; the attempt should not succeed because the
// Try to update the cache; the attempt should not succeed because the
// cache timestamp is newer than the current time.
// cache timestamp is newer than the current time.
...
...
pkg/kubelet/dockertools/manager_test.go
View file @
9ca4fb40
This diff is collapsed.
Click to expand it.
pkg/kubelet/kubelet_test.go
View file @
9ca4fb40
This diff is collapsed.
Click to expand it.
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