gc_controller_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package podgc
  14. import (
  15. "sync"
  16. "testing"
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  21. "k8s.io/kubernetes/pkg/controller"
  22. "k8s.io/kubernetes/pkg/util/sets"
  23. )
  24. func TestGC(t *testing.T) {
  25. type nameToPhase struct {
  26. name string
  27. phase api.PodPhase
  28. }
  29. testCases := []struct {
  30. pods []nameToPhase
  31. threshold int
  32. deletedPodNames sets.String
  33. }{
  34. {
  35. pods: []nameToPhase{
  36. {name: "a", phase: api.PodFailed},
  37. {name: "b", phase: api.PodSucceeded},
  38. },
  39. threshold: 0,
  40. deletedPodNames: sets.NewString("a", "b"),
  41. },
  42. {
  43. pods: []nameToPhase{
  44. {name: "a", phase: api.PodFailed},
  45. {name: "b", phase: api.PodSucceeded},
  46. },
  47. threshold: 1,
  48. deletedPodNames: sets.NewString("a"),
  49. },
  50. {
  51. pods: []nameToPhase{
  52. {name: "a", phase: api.PodFailed},
  53. {name: "b", phase: api.PodSucceeded},
  54. },
  55. threshold: 5,
  56. deletedPodNames: sets.NewString(),
  57. },
  58. }
  59. for i, test := range testCases {
  60. client := fake.NewSimpleClientset()
  61. gcc := New(client, controller.NoResyncPeriodFunc, test.threshold)
  62. deletedPodNames := make([]string, 0)
  63. var lock sync.Mutex
  64. gcc.deletePod = func(_, name string) error {
  65. lock.Lock()
  66. defer lock.Unlock()
  67. deletedPodNames = append(deletedPodNames, name)
  68. return nil
  69. }
  70. creationTime := time.Unix(0, 0)
  71. for _, pod := range test.pods {
  72. creationTime = creationTime.Add(1 * time.Hour)
  73. gcc.podStore.Indexer.Add(&api.Pod{
  74. ObjectMeta: api.ObjectMeta{Name: pod.name, CreationTimestamp: unversioned.Time{Time: creationTime}},
  75. Status: api.PodStatus{Phase: pod.phase},
  76. })
  77. }
  78. gcc.gc()
  79. pass := true
  80. for _, pod := range deletedPodNames {
  81. if !test.deletedPodNames.Has(pod) {
  82. pass = false
  83. }
  84. }
  85. if len(deletedPodNames) != len(test.deletedPodNames) {
  86. pass = false
  87. }
  88. if !pass {
  89. t.Errorf("[%v]pod's deleted expected and actual did not match.\n\texpected: %v\n\tactual: %v", i, test.deletedPodNames, deletedPodNames)
  90. }
  91. }
  92. }