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
8b3130b1
Commit
8b3130b1
authored
May 11, 2015
by
Yu-Ju Hong
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7980 from yifan-gu/fix_kubelet_tests
kubelet: Fix racy kubelet tests.
parents
14055d6e
85b45309
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
279 additions
and
1 deletion
+279
-1
fake_pod_workers.go
pkg/kubelet/fake_pod_workers.go
+45
-0
kubelet.go
pkg/kubelet/kubelet.go
+1
-1
kubelet_test.go
pkg/kubelet/kubelet_test.go
+0
-0
pod_workers.go
pkg/kubelet/pod_workers.go
+6
-0
pod_workers_test.go
pkg/kubelet/pod_workers_test.go
+227
-0
No files found.
pkg/kubelet/fake_pod_workers.go
0 → 100644
View file @
8b3130b1
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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
kubelet
import
(
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kubecontainer
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
)
// fakePodWorkers runs sync pod function in serial, so we can have
// deterministic behaviour in testing.
type
fakePodWorkers
struct
{
syncPodFn
syncPodFnType
runtimeCache
kubecontainer
.
RuntimeCache
t
*
testing
.
T
}
func
(
f
*
fakePodWorkers
)
UpdatePod
(
pod
*
api
.
Pod
,
mirrorPod
*
api
.
Pod
,
updateComplete
func
())
{
pods
,
err
:=
f
.
runtimeCache
.
GetPods
()
if
err
!=
nil
{
f
.
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
if
err
:=
f
.
syncPodFn
(
pod
,
mirrorPod
,
kubecontainer
.
Pods
(
pods
)
.
FindPodByID
(
pod
.
UID
));
err
!=
nil
{
f
.
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
}
func
(
f
*
fakePodWorkers
)
ForgetNonExistingPodWorkers
(
desiredPods
map
[
types
.
UID
]
empty
)
{}
pkg/kubelet/kubelet.go
View file @
8b3130b1
...
@@ -332,7 +332,7 @@ type Kubelet struct {
...
@@ -332,7 +332,7 @@ type Kubelet struct {
runtimeCache
kubecontainer
.
RuntimeCache
runtimeCache
kubecontainer
.
RuntimeCache
kubeClient
client
.
Interface
kubeClient
client
.
Interface
rootDirectory
string
rootDirectory
string
podWorkers
*
p
odWorkers
podWorkers
P
odWorkers
resyncInterval
time
.
Duration
resyncInterval
time
.
Duration
sourcesReady
SourcesReadyFn
sourcesReady
SourcesReadyFn
...
...
pkg/kubelet/kubelet_test.go
View file @
8b3130b1
This diff is collapsed.
Click to expand it.
pkg/kubelet/pod_workers.go
View file @
8b3130b1
...
@@ -28,6 +28,12 @@ import (
...
@@ -28,6 +28,12 @@ import (
"github.com/golang/glog"
"github.com/golang/glog"
)
)
// PodWorkers is an abstract interface for testability.
type
PodWorkers
interface
{
UpdatePod
(
pod
*
api
.
Pod
,
mirrorPod
*
api
.
Pod
,
updateComplete
func
())
ForgetNonExistingPodWorkers
(
desiredPods
map
[
types
.
UID
]
empty
)
}
type
syncPodFnType
func
(
*
api
.
Pod
,
*
api
.
Pod
,
kubecontainer
.
Pod
)
error
type
syncPodFnType
func
(
*
api
.
Pod
,
*
api
.
Pod
,
kubecontainer
.
Pod
)
error
type
podWorkers
struct
{
type
podWorkers
struct
{
...
...
pkg/kubelet/pod_workers_test.go
View file @
8b3130b1
...
@@ -17,6 +17,8 @@ limitations under the License.
...
@@ -17,6 +17,8 @@ limitations under the License.
package
kubelet
package
kubelet
import
(
import
(
"reflect"
"sort"
"sync"
"sync"
"testing"
"testing"
"time"
"time"
...
@@ -27,6 +29,7 @@ import (
...
@@ -27,6 +29,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/network"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/network"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/fsouza/go-dockerclient"
)
)
func
newPod
(
uid
,
name
string
)
*
api
.
Pod
{
func
newPod
(
uid
,
name
string
)
*
api
.
Pod
{
...
@@ -147,3 +150,227 @@ func TestForgetNonExistingPodWorkers(t *testing.T) {
...
@@ -147,3 +150,227 @@ func TestForgetNonExistingPodWorkers(t *testing.T) {
t
.
Errorf
(
"Incorrect number of open channels %v"
,
len
(
podWorkers
.
podUpdates
))
t
.
Errorf
(
"Incorrect number of open channels %v"
,
len
(
podWorkers
.
podUpdates
))
}
}
}
}
type
simpleFakeKubelet
struct
{
pod
*
api
.
Pod
mirrorPod
*
api
.
Pod
runningPod
kubecontainer
.
Pod
wg
sync
.
WaitGroup
}
func
(
kl
*
simpleFakeKubelet
)
syncPod
(
pod
*
api
.
Pod
,
mirrorPod
*
api
.
Pod
,
runningPod
kubecontainer
.
Pod
)
error
{
kl
.
pod
,
kl
.
mirrorPod
,
kl
.
runningPod
=
pod
,
mirrorPod
,
runningPod
return
nil
}
func
(
kl
*
simpleFakeKubelet
)
syncPodWithWaitGroup
(
pod
*
api
.
Pod
,
mirrorPod
*
api
.
Pod
,
runningPod
kubecontainer
.
Pod
)
error
{
kl
.
pod
,
kl
.
mirrorPod
,
kl
.
runningPod
=
pod
,
mirrorPod
,
runningPod
kl
.
wg
.
Done
()
return
nil
}
// byContainerName sort the containers in a running pod by their names.
type
byContainerName
kubecontainer
.
Pod
func
(
b
byContainerName
)
Len
()
int
{
return
len
(
b
.
Containers
)
}
func
(
b
byContainerName
)
Swap
(
i
,
j
int
)
{
b
.
Containers
[
i
],
b
.
Containers
[
j
]
=
b
.
Containers
[
j
],
b
.
Containers
[
i
]
}
func
(
b
byContainerName
)
Less
(
i
,
j
int
)
bool
{
return
b
.
Containers
[
i
]
.
Name
<
b
.
Containers
[
j
]
.
Name
}
// TestFakePodWorkers verifies that the fakePodWorkers behaves the same way as the real podWorkers
// for their invocation of the syncPodFn.
func
TestFakePodWorkers
(
t
*
testing
.
T
)
{
// Create components for pod workers.
fakeDocker
:=
&
dockertools
.
FakeDockerClient
{}
fakeRecorder
:=
&
record
.
FakeRecorder
{}
np
,
_
:=
network
.
InitNetworkPlugin
([]
network
.
NetworkPlugin
{},
""
,
network
.
NewFakeHost
(
nil
))
dockerManager
:=
dockertools
.
NewFakeDockerManager
(
fakeDocker
,
fakeRecorder
,
nil
,
nil
,
dockertools
.
PodInfraContainerImage
,
0
,
0
,
""
,
kubecontainer
.
FakeOS
{},
np
,
nil
,
nil
,
newKubeletRuntimeHooks
(
fakeRecorder
))
fakeRuntimeCache
:=
kubecontainer
.
NewFakeRuntimeCache
(
dockerManager
)
kubeletForRealWorkers
:=
&
simpleFakeKubelet
{}
kubeletForFakeWorkers
:=
&
simpleFakeKubelet
{}
realPodWorkers
:=
newPodWorkers
(
fakeRuntimeCache
,
kubeletForRealWorkers
.
syncPodWithWaitGroup
,
fakeRecorder
)
fakePodWorkers
:=
&
fakePodWorkers
{
kubeletForFakeWorkers
.
syncPod
,
fakeRuntimeCache
,
t
}
tests
:=
[]
struct
{
pod
*
api
.
Pod
mirrorPod
*
api
.
Pod
containerList
[]
docker
.
APIContainers
containersInRunningPod
int
}{
{
&
api
.
Pod
{},
&
api
.
Pod
{},
[]
docker
.
APIContainers
{},
0
,
},
{
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"12345678"
,
Name
:
"foo"
,
Namespace
:
"new"
,
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"fooContainer"
,
},
},
},
},
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"12345678"
,
Name
:
"fooMirror"
,
Namespace
:
"new"
,
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"fooContainerMirror"
,
},
},
},
},
[]
docker
.
APIContainers
{
{
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
Names
:
[]
string
{
"/k8s_bar.hash123_foo_new_12345678_0"
},
ID
:
"1234"
,
},
{
// pod infra container
Names
:
[]
string
{
"/k8s_POD.hash123_foo_new_12345678_0"
},
ID
:
"9876"
,
},
},
2
,
},
{
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"98765"
,
Name
:
"bar"
,
Namespace
:
"new"
,
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"fooContainer"
,
},
},
},
},
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"98765"
,
Name
:
"fooMirror"
,
Namespace
:
"new"
,
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"fooContainerMirror"
,
},
},
},
},
[]
docker
.
APIContainers
{
{
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
Names
:
[]
string
{
"/k8s_bar.hash123_bar_new_98765_0"
},
ID
:
"1234"
,
},
{
// pod infra container
Names
:
[]
string
{
"/k8s_POD.hash123_foo_new_12345678_0"
},
ID
:
"9876"
,
},
},
1
,
},
// Empty running pod.
{
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"98765"
,
Name
:
"baz"
,
Namespace
:
"new"
,
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"bazContainer"
,
},
},
},
},
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"98765"
,
Name
:
"bazMirror"
,
Namespace
:
"new"
,
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"bazContainerMirror"
,
},
},
},
},
[]
docker
.
APIContainers
{
{
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
Names
:
[]
string
{
"/k8s_bar.hash123_bar_new_12345678_0"
},
ID
:
"1234"
,
},
{
// pod infra container
Names
:
[]
string
{
"/k8s_POD.hash123_foo_new_12345678_0"
},
ID
:
"9876"
,
},
},
0
,
},
}
for
i
,
tt
:=
range
tests
{
kubeletForRealWorkers
.
wg
.
Add
(
1
)
fakeDocker
.
ContainerList
=
tt
.
containerList
realPodWorkers
.
UpdatePod
(
tt
.
pod
,
tt
.
mirrorPod
,
func
()
{})
fakePodWorkers
.
UpdatePod
(
tt
.
pod
,
tt
.
mirrorPod
,
func
()
{})
kubeletForRealWorkers
.
wg
.
Wait
()
if
!
reflect
.
DeepEqual
(
kubeletForRealWorkers
.
pod
,
kubeletForFakeWorkers
.
pod
)
{
t
.
Errorf
(
"%d: Expected: %#v, Actual: %#v"
,
i
,
kubeletForRealWorkers
.
pod
,
kubeletForFakeWorkers
.
pod
)
}
if
!
reflect
.
DeepEqual
(
kubeletForRealWorkers
.
mirrorPod
,
kubeletForFakeWorkers
.
mirrorPod
)
{
t
.
Errorf
(
"%d: Expected: %#v, Actual: %#v"
,
i
,
kubeletForRealWorkers
.
mirrorPod
,
kubeletForFakeWorkers
.
mirrorPod
)
}
if
tt
.
containersInRunningPod
!=
len
(
kubeletForFakeWorkers
.
runningPod
.
Containers
)
{
t
.
Errorf
(
"%d: Expected: %#v, Actual: %#v"
,
i
,
tt
.
containersInRunningPod
,
len
(
kubeletForFakeWorkers
.
runningPod
.
Containers
))
}
sort
.
Sort
(
byContainerName
(
kubeletForRealWorkers
.
runningPod
))
sort
.
Sort
(
byContainerName
(
kubeletForFakeWorkers
.
runningPod
))
if
!
reflect
.
DeepEqual
(
kubeletForRealWorkers
.
runningPod
,
kubeletForFakeWorkers
.
runningPod
)
{
t
.
Errorf
(
"%d: Expected: %#v, Actual: %#v"
,
i
,
kubeletForRealWorkers
.
runningPod
,
kubeletForFakeWorkers
.
runningPod
)
}
}
}
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