kubernetes/pkg/controller/gc/gc_controller_test.go

105 lines
2.6 KiB
Go
Raw Normal View History

2015-09-22 06:51:27 +08:00
/*
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 gc
import (
"sync"
"testing"
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
"k8s.io/kubernetes/pkg/controller"
2015-09-22 06:51:27 +08:00
"k8s.io/kubernetes/pkg/util/sets"
)
func TestGC(t *testing.T) {
2015-09-26 03:33:33 +08:00
type nameToPhase struct {
name string
phase api.PodPhase
}
2015-09-22 06:51:27 +08:00
testCases := []struct {
2015-09-26 03:33:33 +08:00
pods []nameToPhase
2015-09-22 06:51:27 +08:00
threshold int
deletedPodNames sets.String
}{
{
2015-09-26 03:33:33 +08:00
pods: []nameToPhase{
{name: "a", phase: api.PodFailed},
{name: "b", phase: api.PodSucceeded},
2015-09-22 06:51:27 +08:00
},
threshold: 0,
deletedPodNames: sets.NewString("a", "b"),
},
{
2015-09-26 03:33:33 +08:00
pods: []nameToPhase{
{name: "a", phase: api.PodFailed},
{name: "b", phase: api.PodSucceeded},
2015-09-22 06:51:27 +08:00
},
threshold: 1,
deletedPodNames: sets.NewString("a"),
},
{
2015-09-26 03:33:33 +08:00
pods: []nameToPhase{
{name: "a", phase: api.PodFailed},
{name: "b", phase: api.PodSucceeded},
2015-09-22 06:51:27 +08:00
},
threshold: 5,
deletedPodNames: sets.NewString(),
},
}
for i, test := range testCases {
client := testclient.NewSimpleFake()
gcc := New(client, controller.NoResyncPeriodFunc, test.threshold)
deletedPodNames := make([]string, 0)
var lock sync.Mutex
gcc.deletePod = func(_, name string) error {
lock.Lock()
defer lock.Unlock()
deletedPodNames = append(deletedPodNames, name)
return nil
}
2015-09-22 06:51:27 +08:00
creationTime := time.Unix(0, 0)
2015-09-26 03:33:33 +08:00
for _, pod := range test.pods {
2015-09-22 06:51:27 +08:00
creationTime = creationTime.Add(1 * time.Hour)
gcc.podStore.Store.Add(&api.Pod{
2015-09-26 03:33:33 +08:00
ObjectMeta: api.ObjectMeta{Name: pod.name, CreationTimestamp: unversioned.Time{creationTime}},
Status: api.PodStatus{Phase: pod.phase},
2015-09-22 06:51:27 +08:00
})
}
gcc.gc()
pass := true
for _, pod := range deletedPodNames {
2015-09-22 06:51:27 +08:00
if !test.deletedPodNames.Has(pod) {
pass = false
}
}
if len(deletedPodNames) != len(test.deletedPodNames) {
2015-09-22 06:51:27 +08:00
pass = false
}
if !pass {
t.Errorf("[%v]pod's deleted expected and actual did not match.\n\texpected: %v\n\tactual: %v", i, test.deletedPodNames, deletedPodNames)
2015-09-22 06:51:27 +08:00
}
}
}