active_deadline_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "testing"
  16. "time"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. "k8s.io/kubernetes/pkg/client/record"
  20. "k8s.io/kubernetes/pkg/types"
  21. "k8s.io/kubernetes/pkg/util/clock"
  22. )
  23. // mockPodStatusProvider returns the status on the specified pod
  24. type mockPodStatusProvider struct {
  25. pods []*api.Pod
  26. }
  27. // GetPodStatus returns the status on the associated pod with matching uid (if found)
  28. func (m *mockPodStatusProvider) GetPodStatus(uid types.UID) (api.PodStatus, bool) {
  29. for _, pod := range m.pods {
  30. if pod.UID == uid {
  31. return pod.Status, true
  32. }
  33. }
  34. return api.PodStatus{}, false
  35. }
  36. // TestActiveDeadlineHandler verifies the active deadline handler functions as expected.
  37. func TestActiveDeadlineHandler(t *testing.T) {
  38. pods := newTestPods(4)
  39. fakeClock := clock.NewFakeClock(time.Now())
  40. podStatusProvider := &mockPodStatusProvider{pods: pods}
  41. fakeRecorder := &record.FakeRecorder{}
  42. handler, err := newActiveDeadlineHandler(podStatusProvider, fakeRecorder, fakeClock)
  43. if err != nil {
  44. t.Fatalf("unexpected error: %v", err)
  45. }
  46. now := unversioned.Now()
  47. startTime := unversioned.NewTime(now.Time.Add(-1 * time.Minute))
  48. // this pod has exceeded its active deadline
  49. exceededActiveDeadlineSeconds := int64(30)
  50. pods[0].Status.StartTime = &startTime
  51. pods[0].Spec.ActiveDeadlineSeconds = &exceededActiveDeadlineSeconds
  52. // this pod has not exceeded its active deadline
  53. notYetActiveDeadlineSeconds := int64(120)
  54. pods[1].Status.StartTime = &startTime
  55. pods[1].Spec.ActiveDeadlineSeconds = &notYetActiveDeadlineSeconds
  56. // this pod has no deadline
  57. pods[2].Status.StartTime = &startTime
  58. pods[2].Spec.ActiveDeadlineSeconds = nil
  59. testCases := []struct {
  60. pod *api.Pod
  61. expected bool
  62. }{{pods[0], true}, {pods[1], false}, {pods[2], false}, {pods[3], false}}
  63. for i, testCase := range testCases {
  64. if actual := handler.ShouldSync(testCase.pod); actual != testCase.expected {
  65. t.Errorf("[%d] ShouldSync expected %#v, got %#v", i, testCase.expected, actual)
  66. }
  67. actual := handler.ShouldEvict(testCase.pod)
  68. if actual.Evict != testCase.expected {
  69. t.Errorf("[%d] ShouldEvict.Evict expected %#v, got %#v", i, testCase.expected, actual.Evict)
  70. }
  71. if testCase.expected {
  72. if actual.Reason != reason {
  73. t.Errorf("[%d] ShouldEvict.Reason expected %#v, got %#v", i, message, actual.Reason)
  74. }
  75. if actual.Message != message {
  76. t.Errorf("[%d] ShouldEvict.Message expected %#v, got %#v", i, message, actual.Message)
  77. }
  78. }
  79. }
  80. }