Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fake clock in TestGetPodsToSync. #24959

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func NewMainKubelet(
}
klet.runtimeCache = runtimeCache
klet.reasonCache = NewReasonCache()
klet.workQueue = queue.NewBasicWorkQueue()
klet.workQueue = queue.NewBasicWorkQueue(klet.clock)
klet.podWorkers = newPodWorkers(klet.syncPod, recorder, klet.workQueue, klet.resyncInterval, backOffPeriod, klet.podCache)

klet.backOff = flowcontrol.NewBackOff(backOffPeriod, MaxContainerBackOff)
Expand Down
42 changes: 17 additions & 25 deletions pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func newTestKubelet(t *testing.T) *TestKubelet {
api.ResourceMemory: resource.MustParse(testReservationMemory),
},
}
kubelet.workQueue = queue.NewBasicWorkQueue()
kubelet.workQueue = queue.NewBasicWorkQueue(fakeClock)
// Relist period does not affect the tests.
kubelet.pleg = pleg.NewGenericPLEG(fakeRuntime, 100, time.Hour, nil, util.RealClock{})
kubelet.clock = fakeClock
Expand Down Expand Up @@ -4418,16 +4418,12 @@ func TestExtractBandwidthResources(t *testing.T) {
func TestGetPodsToSync(t *testing.T) {
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
clock := testKubelet.fakeClock
pods := newTestPods(5)
podUIDs := []types.UID{}
for _, pod := range pods {
podUIDs = append(podUIDs, pod.UID)
}

exceededActiveDeadlineSeconds := int64(30)
notYetActiveDeadlineSeconds := int64(120)
now := unversioned.Now()
startTime := unversioned.NewTime(now.Time.Add(-1 * time.Minute))
startTime := unversioned.NewTime(clock.Now())
pods[0].Status.StartTime = &startTime
pods[0].Spec.ActiveDeadlineSeconds = &exceededActiveDeadlineSeconds
pods[1].Status.StartTime = &startTime
Expand All @@ -4437,34 +4433,30 @@ func TestGetPodsToSync(t *testing.T) {

kubelet.podManager.SetPods(pods)
kubelet.workQueue.Enqueue(pods[2].UID, 0)
kubelet.workQueue.Enqueue(pods[3].UID, 0)
kubelet.workQueue.Enqueue(pods[4].UID, time.Hour)
kubelet.workQueue.Enqueue(pods[3].UID, 30*time.Second)
kubelet.workQueue.Enqueue(pods[4].UID, 2*time.Minute)

expectedPodsUID := []types.UID{pods[0].UID, pods[2].UID, pods[3].UID}
clock.Step(1 * time.Minute)

expectedPods := []*api.Pod{pods[0], pods[2], pods[3]}

podsToSync := kubelet.getPodsToSync()

if len(podsToSync) == len(expectedPodsUID) {
var rightNum int
for _, podUID := range expectedPodsUID {
for _, podToSync := range podsToSync {
if podToSync.UID == podUID {
rightNum++
if len(podsToSync) == len(expectedPods) {
for _, expect := range expectedPods {
var found bool
for _, got := range podsToSync {
if expect.UID == got.UID {
found = true
break
}
}
}
if rightNum != len(expectedPodsUID) {
// Just for report error
podsToSyncUID := []types.UID{}
for _, podToSync := range podsToSync {
podsToSyncUID = append(podsToSyncUID, podToSync.UID)
if !found {
t.Errorf("expected pod not found: %+v", expect)
}
t.Errorf("expected pods %v to sync, got %v", expectedPodsUID, podsToSyncUID)
}

} else {
t.Errorf("expected %d pods to sync, got %d", 3, len(podsToSync))
t.Errorf("expected %d pods to sync, got %d", len(expectedPods), len(podsToSync))
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/kubelet/pod_workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/kubelet/util/queue"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/util"
)

// fakePodWorkers runs sync pod function in serial, so we can have
Expand Down Expand Up @@ -82,7 +83,7 @@ func createPodWorkers() (*podWorkers, map[types.UID][]string) {
return nil
},
fakeRecorder,
queue.NewBasicWorkQueue(),
queue.NewBasicWorkQueue(&util.RealClock{}),
time.Second,
time.Second,
fakeCache,
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestFakePodWorkers(t *testing.T) {
kubeletForRealWorkers := &simpleFakeKubelet{}
kubeletForFakeWorkers := &simpleFakeKubelet{}

realPodWorkers := newPodWorkers(kubeletForRealWorkers.syncPodWithWaitGroup, fakeRecorder, queue.NewBasicWorkQueue(), time.Second, time.Second, fakeCache)
realPodWorkers := newPodWorkers(kubeletForRealWorkers.syncPodWithWaitGroup, fakeRecorder, queue.NewBasicWorkQueue(&util.RealClock{}), time.Second, time.Second, fakeCache)
fakePodWorkers := &fakePodWorkers{kubeletForFakeWorkers.syncPod, fakeCache, t}

tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/util/queue/work_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type basicWorkQueue struct {

var _ WorkQueue = &basicWorkQueue{}

func NewBasicWorkQueue() WorkQueue {
func NewBasicWorkQueue(clock util.Clock) WorkQueue {
queue := make(map[types.UID]time.Time)
return &basicWorkQueue{queue: queue, clock: util.RealClock{}}
return &basicWorkQueue{queue: queue, clock: clock}
}

func (q *basicWorkQueue) GetWork() []types.UID {
Expand Down