mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-01 02:38:12 +00:00
Use the assert/require package in kubelet unit tests
This reduce the lines of code and improve readability.
This commit is contained in:
@@ -20,6 +20,9 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
cadvisorapi "github.com/google/cadvisor/info/v1"
|
||||
cadvisorapiv2 "github.com/google/cadvisor/info/v2"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -194,11 +197,10 @@ func TestGetContainerInfo(t *testing.T) {
|
||||
fakeRuntime.PodList = tc.podList
|
||||
|
||||
stats, err := kubelet.GetContainerInfo(tc.requestedPodFullName, tc.requestedPodUid, tc.requestedContainerName, cadvisorReq)
|
||||
if err != tc.expectedError {
|
||||
t.Errorf("test '%s' failed: expected error %#v, got %#v", tc.name, tc.expectedError, err)
|
||||
}
|
||||
if tc.expectStats && stats == nil {
|
||||
t.Fatalf("test '%s' failed: stats should not be nil", tc.name)
|
||||
assert.Equal(t, tc.expectedError, err)
|
||||
|
||||
if tc.expectStats {
|
||||
require.NotNil(t, stats)
|
||||
}
|
||||
mockCadvisor.AssertExpectations(t)
|
||||
}
|
||||
@@ -219,9 +221,7 @@ func TestGetRawContainerInfoRoot(t *testing.T) {
|
||||
mockCadvisor.On("ContainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
|
||||
|
||||
_, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, false)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
mockCadvisor.AssertExpectations(t)
|
||||
}
|
||||
|
||||
@@ -247,12 +247,8 @@ func TestGetRawContainerInfoSubcontainers(t *testing.T) {
|
||||
mockCadvisor.On("SubcontainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
|
||||
|
||||
result, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, true)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if len(result) != 2 {
|
||||
t.Errorf("Expected 2 elements, received: %#v", result)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, result, 2)
|
||||
mockCadvisor.AssertExpectations(t)
|
||||
}
|
||||
|
||||
@@ -282,11 +278,7 @@ func TestHasDedicatedImageFs(t *testing.T) {
|
||||
mockCadvisor.On("ImagesFsInfo").Return(testCase.imageFsInfo, nil)
|
||||
mockCadvisor.On("RootFsInfo").Return(testCase.rootFsInfo, nil)
|
||||
actual, err := kubelet.HasDedicatedImageFs()
|
||||
if err != nil {
|
||||
t.Errorf("case: %s, unexpected error: %v", testName, err)
|
||||
}
|
||||
if actual != testCase.expected {
|
||||
t.Errorf("case: %s, expected: %v, actual: %v", testName, testCase.expected, actual)
|
||||
}
|
||||
assert.NoError(t, err, "test [%s]", testName)
|
||||
assert.Equal(t, testCase.expected, actual, "test [%s]", testName)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user