mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Kubelet: switch cadvisor-related unit tests to use FakeRuntime
This commit is contained in:
parent
0bbba1c3d1
commit
ef5bef914f
@ -851,22 +851,15 @@ func TestGetContainerInfo(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
|
fakeRuntime := testKubelet.fakeRuntime
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
fakeDocker := testKubelet.fakeDocker
|
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
|
||||||
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
||||||
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, nil)
|
mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, nil)
|
||||||
|
fakeRuntime.PodList = []*kubecontainer.Pod{
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{
|
{ID: "12345678", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{{Name: "foo", ID: types.UID(containerID)}}},
|
||||||
{
|
|
||||||
ID: containerID,
|
|
||||||
// pod id: qux
|
|
||||||
// container id: foo
|
|
||||||
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", cadvisorReq)
|
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", cadvisorReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@ -884,17 +877,12 @@ func TestGetRawContainerInfoRoot(t *testing.T) {
|
|||||||
Name: containerPath,
|
Name: containerPath,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fakeDocker := dockertools.FakeDockerClient{}
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := &cadvisor.Mock{}
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
||||||
mockCadvisor.On("ContainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
|
mockCadvisor.On("ContainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
|
||||||
|
|
||||||
kubelet := Kubelet{
|
|
||||||
dockerClient: &fakeDocker,
|
|
||||||
cadvisor: mockCadvisor,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, false)
|
_, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@ -916,17 +904,12 @@ func TestGetRawContainerInfoSubcontainers(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fakeDocker := dockertools.FakeDockerClient{}
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := &cadvisor.Mock{}
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
||||||
mockCadvisor.On("SubcontainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
|
mockCadvisor.On("SubcontainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
|
||||||
|
|
||||||
kubelet := Kubelet{
|
|
||||||
dockerClient: &fakeDocker,
|
|
||||||
cadvisor: mockCadvisor,
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, true)
|
result, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@ -939,24 +922,17 @@ func TestGetRawContainerInfoSubcontainers(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetContainerInfoWhenCadvisorFailed(t *testing.T) {
|
func TestGetContainerInfoWhenCadvisorFailed(t *testing.T) {
|
||||||
containerID := "ab2cdf"
|
containerID := "ab2cdf"
|
||||||
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
testKubelet := newTestKubelet(t)
|
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
fakeDocker := testKubelet.fakeDocker
|
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
|
fakeRuntime := testKubelet.fakeRuntime
|
||||||
cadvisorApiFailure := fmt.Errorf("cAdvisor failure")
|
cadvisorApiFailure := fmt.Errorf("cAdvisor failure")
|
||||||
containerInfo := cadvisorApi.ContainerInfo{}
|
containerInfo := cadvisorApi.ContainerInfo{}
|
||||||
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
cadvisorReq := &cadvisorApi.ContainerInfoRequest{}
|
||||||
mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, cadvisorApiFailure)
|
mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, cadvisorApiFailure)
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{
|
fakeRuntime.PodList = []*kubecontainer.Pod{
|
||||||
{
|
{ID: "uuid", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{{Name: "foo", ID: types.UID(containerID)}}},
|
||||||
ID: containerID,
|
|
||||||
// pod id: qux
|
|
||||||
// container id: foo
|
|
||||||
Names: []string{"/k8s_foo_qux_ns_uuid_1234"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux_ns", "uuid", "foo", cadvisorReq)
|
stats, err := kubelet.GetContainerInfo("qux_ns", "uuid", "foo", cadvisorReq)
|
||||||
if stats != nil {
|
if stats != nil {
|
||||||
t.Errorf("non-nil stats on error")
|
t.Errorf("non-nil stats on error")
|
||||||
@ -972,11 +948,11 @@ func TestGetContainerInfoWhenCadvisorFailed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetContainerInfoOnNonExistContainer(t *testing.T) {
|
func TestGetContainerInfoOnNonExistContainer(t *testing.T) {
|
||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
fakeDocker := testKubelet.fakeDocker
|
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
fakeRuntime := testKubelet.fakeRuntime
|
||||||
|
fakeRuntime.PodList = []*kubecontainer.Pod{}
|
||||||
|
|
||||||
stats, _ := kubelet.GetContainerInfo("qux", "", "foo", nil)
|
stats, _ := kubelet.GetContainerInfo("qux", "", "foo", nil)
|
||||||
if stats != nil {
|
if stats != nil {
|
||||||
@ -985,13 +961,13 @@ func TestGetContainerInfoOnNonExistContainer(t *testing.T) {
|
|||||||
mockCadvisor.AssertExpectations(t)
|
mockCadvisor.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetContainerInfoWhenDockerToolsFailed(t *testing.T) {
|
func TestGetContainerInfoWhenContainerRuntimeFailed(t *testing.T) {
|
||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
fakeDocker := testKubelet.fakeDocker
|
fakeRuntime := testKubelet.fakeRuntime
|
||||||
expectedErr := fmt.Errorf("List containers error")
|
expectedErr := fmt.Errorf("List containers error")
|
||||||
fakeDocker.Errors["list"] = expectedErr
|
fakeRuntime.Err = expectedErr
|
||||||
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux", "", "foo", nil)
|
stats, err := kubelet.GetContainerInfo("qux", "", "foo", nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -1007,7 +983,7 @@ func TestGetContainerInfoWhenDockerToolsFailed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetContainerInfoWithNoContainers(t *testing.T) {
|
func TestGetContainerInfoWithNoContainers(t *testing.T) {
|
||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
|
|
||||||
@ -1025,15 +1001,12 @@ func TestGetContainerInfoWithNoContainers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
|
func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
|
||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubeletWithFakeRuntime(t)
|
||||||
|
fakeRuntime := testKubelet.fakeRuntime
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
fakeDocker := testKubelet.fakeDocker
|
fakeRuntime.PodList = []*kubecontainer.Pod{
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{
|
{ID: "12345678", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{{Name: "bar", ID: types.UID("fakeID")}}},
|
||||||
{
|
|
||||||
ID: "fakeId",
|
|
||||||
Names: []string{"/k8s_bar_qux_ns_1234_42"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
|
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user