...

Source file src/k8s.io/kubernetes/pkg/kubelet/active_deadline_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kubelet
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/types"
    28  	"k8s.io/client-go/tools/record"
    29  	"k8s.io/kubernetes/pkg/kubelet/status"
    30  	"k8s.io/utils/clock"
    31  	testingclock "k8s.io/utils/clock/testing"
    32  )
    33  
    34  // mockPodStatusProvider returns the status on the specified pod
    35  type mockPodStatusProvider struct {
    36  	pods []*v1.Pod
    37  }
    38  
    39  // GetPodStatus returns the status on the associated pod with matching uid (if found)
    40  func (m *mockPodStatusProvider) GetPodStatus(uid types.UID) (v1.PodStatus, bool) {
    41  	for _, pod := range m.pods {
    42  		if pod.UID == uid {
    43  			return pod.Status, true
    44  		}
    45  	}
    46  	return v1.PodStatus{}, false
    47  }
    48  
    49  func TestNewActiveDeadlineHandler(t *testing.T) {
    50  	pods := newTestPods(1)
    51  	podStatusProvider := &mockPodStatusProvider{pods: pods}
    52  	fakeRecorder := &record.FakeRecorder{}
    53  	fakeClock := testingclock.NewFakeClock(time.Now())
    54  
    55  	testCases := []struct {
    56  		podStatusProvider status.PodStatusProvider
    57  		recorder          record.EventRecorder
    58  		clock             clock.Clock
    59  	}{
    60  		{podStatusProvider, fakeRecorder, fakeClock},
    61  		{podStatusProvider, fakeRecorder, nil},
    62  		{podStatusProvider, nil, fakeClock},
    63  		{podStatusProvider, nil, nil},
    64  		{nil, fakeRecorder, fakeClock},
    65  		{nil, fakeRecorder, nil},
    66  		{nil, nil, fakeClock},
    67  		{nil, nil, nil},
    68  	}
    69  
    70  	for i, testCase := range testCases {
    71  		actual, err := newActiveDeadlineHandler(testCase.podStatusProvider, testCase.recorder, testCase.clock)
    72  
    73  		// 0th case is the only one expected to pass, and is kept for coverage and confidence check
    74  		if i == 0 {
    75  			expected := &activeDeadlineHandler{
    76  				clock:             fakeClock,
    77  				podStatusProvider: podStatusProvider,
    78  				recorder:          fakeRecorder,
    79  			}
    80  			if !reflect.DeepEqual(actual, expected) {
    81  				t.Errorf("[%d] newActiveDeadlineHandler expected %#v, got %#v", i, expected, actual)
    82  			}
    83  			assert.NoError(t, err)
    84  
    85  			continue
    86  		}
    87  
    88  		assert.Error(t, err)
    89  	}
    90  }
    91  
    92  // TestActiveDeadlineHandler verifies the active deadline handler functions as expected.
    93  func TestActiveDeadlineHandler(t *testing.T) {
    94  	pods := newTestPods(5)
    95  	fakeClock := testingclock.NewFakeClock(time.Now())
    96  	podStatusProvider := &mockPodStatusProvider{pods: pods}
    97  	fakeRecorder := &record.FakeRecorder{}
    98  	handler, err := newActiveDeadlineHandler(podStatusProvider, fakeRecorder, fakeClock)
    99  	if err != nil {
   100  		t.Fatalf("unexpected error: %v", err)
   101  	}
   102  
   103  	now := metav1.Now()
   104  	startTime := metav1.NewTime(now.Time.Add(-1 * time.Minute))
   105  
   106  	// this pod has exceeded its active deadline
   107  	exceededActiveDeadlineSeconds := int64(30)
   108  	pods[0].Status.StartTime = &startTime
   109  	pods[0].Spec.ActiveDeadlineSeconds = &exceededActiveDeadlineSeconds
   110  
   111  	// this pod has not exceeded its active deadline
   112  	notYetActiveDeadlineSeconds := int64(120)
   113  	pods[1].Status.StartTime = &startTime
   114  	pods[1].Spec.ActiveDeadlineSeconds = &notYetActiveDeadlineSeconds
   115  
   116  	// this pod has no deadline
   117  	pods[2].Status.StartTime = &startTime
   118  	pods[2].Spec.ActiveDeadlineSeconds = nil
   119  
   120  	// this pod has no start time
   121  	pods[3].Status.StartTime = nil
   122  	pods[3].Spec.ActiveDeadlineSeconds = &notYetActiveDeadlineSeconds
   123  
   124  	testCases := []struct {
   125  		pod      *v1.Pod
   126  		expected bool
   127  	}{{pods[0], true}, {pods[1], false}, {pods[2], false}, {pods[3], false}, {pods[4], false}}
   128  
   129  	for i, testCase := range testCases {
   130  		if actual := handler.ShouldSync(testCase.pod); actual != testCase.expected {
   131  			t.Errorf("[%d] ShouldSync expected %#v, got %#v", i, testCase.expected, actual)
   132  		}
   133  		actual := handler.ShouldEvict(testCase.pod)
   134  		if actual.Evict != testCase.expected {
   135  			t.Errorf("[%d] ShouldEvict.Evict expected %#v, got %#v", i, testCase.expected, actual.Evict)
   136  		}
   137  		if testCase.expected {
   138  			if actual.Reason != reason {
   139  				t.Errorf("[%d] ShouldEvict.Reason expected %#v, got %#v", i, message, actual.Reason)
   140  			}
   141  			if actual.Message != message {
   142  				t.Errorf("[%d] ShouldEvict.Message expected %#v, got %#v", i, message, actual.Message)
   143  			}
   144  		}
   145  	}
   146  }
   147  

View as plain text