Commit 8b3130b1 authored by Yu-Ju Hong's avatar Yu-Ju Hong

Merge pull request #7980 from yifan-gu/fix_kubelet_tests

kubelet: Fix racy kubelet tests.
parents 14055d6e 85b45309
/*
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) {}
...@@ -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 *podWorkers podWorkers PodWorkers
resyncInterval time.Duration resyncInterval time.Duration
sourcesReady SourcesReadyFn sourcesReady SourcesReadyFn
......
...@@ -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 {
......
...@@ -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)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment