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

Add unit tests for active_deadline.go #112373

Merged
merged 2 commits into from Nov 1, 2022
Merged
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
55 changes: 53 additions & 2 deletions pkg/kubelet/active_deadline_test.go
Expand Up @@ -17,13 +17,17 @@ limitations under the License.
package kubelet

import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/kubernetes/pkg/kubelet/status"
"k8s.io/utils/clock"
testingclock "k8s.io/utils/clock/testing"
)

Expand All @@ -42,9 +46,52 @@ func (m *mockPodStatusProvider) GetPodStatus(uid types.UID) (v1.PodStatus, bool)
return v1.PodStatus{}, false
}

func TestNewActiveDeadlineHandler(t *testing.T) {
pods := newTestPods(1)
podStatusProvider := &mockPodStatusProvider{pods: pods}
fakeRecorder := &record.FakeRecorder{}
fakeClock := testingclock.NewFakeClock(time.Now())

testCases := []struct {
podStatusProvider status.PodStatusProvider
recorder record.EventRecorder
clock clock.Clock
}{
{podStatusProvider, fakeRecorder, fakeClock},
{podStatusProvider, fakeRecorder, nil},
{podStatusProvider, nil, fakeClock},
{podStatusProvider, nil, nil},
{nil, fakeRecorder, fakeClock},
{nil, fakeRecorder, nil},
{nil, nil, fakeClock},
{nil, nil, nil},
}

for i, testCase := range testCases {
actual, err := newActiveDeadlineHandler(testCase.podStatusProvider, testCase.recorder, testCase.clock)

// 0th case is the only one expected to pass, and is kept for coverage and confidence check
if i == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to only be testing if we can construct an ActiveDeadlineHandler object... Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, there is actually only line 54 (which returns error) needs to be covered, but I think that unit test of function NewActiveDeadlineHeader won't be complete without no errors scenario, and I also saw such testing case in other tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth adding a comment about that. Something like 0th case is the only one expected to pass, and is kept for coverage and confidence check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected := &activeDeadlineHandler{
clock: fakeClock,
podStatusProvider: podStatusProvider,
recorder: fakeRecorder,
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("[%d] newActiveDeadlineHandler expected %#v, got %#v", i, expected, actual)
}
assert.NoError(t, err)

continue
}

assert.Error(t, err)
}
}

// TestActiveDeadlineHandler verifies the active deadline handler functions as expected.
func TestActiveDeadlineHandler(t *testing.T) {
pods := newTestPods(4)
pods := newTestPods(5)
fakeClock := testingclock.NewFakeClock(time.Now())
podStatusProvider := &mockPodStatusProvider{pods: pods}
fakeRecorder := &record.FakeRecorder{}
Expand All @@ -70,10 +117,14 @@ func TestActiveDeadlineHandler(t *testing.T) {
pods[2].Status.StartTime = &startTime
pods[2].Spec.ActiveDeadlineSeconds = nil

// this pod has no start time
pods[3].Status.StartTime = nil
pods[3].Spec.ActiveDeadlineSeconds = &notYetActiveDeadlineSeconds

testCases := []struct {
pod *v1.Pod
expected bool
}{{pods[0], true}, {pods[1], false}, {pods[2], false}, {pods[3], false}}
}{{pods[0], true}, {pods[1], false}, {pods[2], false}, {pods[3], false}, {pods[4], false}}

for i, testCase := range testCases {
if actual := handler.ShouldSync(testCase.pod); actual != testCase.expected {
Expand Down