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

update tests to reflect terminated containers should not be used in usage calculations #10

Open
wants to merge 1 commit into
base: release-1.14.7-lyft
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 52 additions & 20 deletions pkg/kubelet/stats/cri_stats_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const (
cName5 = "container5-name"
cName6 = "container6-name"
cName7 = "container7-name"
cName8 = "container8-name"
)

func TestCRIListPodStats(t *testing.T) {
Expand Down Expand Up @@ -112,10 +113,14 @@ func TestCRIListPodStats(t *testing.T) {
containerStats4 = makeFakeContainerStats(container4, imageFsMountpoint)
containerLogStats4 = makeFakeLogStats(4000)

// Running pod with a terminated container and a running container
sandbox3 = makeFakePodSandbox("sandbox3-name", "sandbox3-uid", "sandbox3-ns", false)
sandbox3Cgroup = "/" + cm.GetPodCgroupNameSuffix(types.UID(sandbox2.PodSandboxStatus.Metadata.Uid))
container5 = makeFakeContainer(sandbox3, cName5, 0, true)
containerStats5 = makeFakeContainerStats(container5, imageFsMountpoint)
containerLogStats5 = makeFakeLogStats(5000)
container8 = makeFakeContainer(sandbox3, cName8, 0, false)
containerStats8 = makeFakeContainerStats(container8, imageFsMountpoint)

// Terminated pod sandbox
sandbox4 = makeFakePodSandbox("sandbox1-name", "sandbox1-uid", "sandbox1-ns", true)
Expand Down Expand Up @@ -156,6 +161,7 @@ func TestCRIListPodStats(t *testing.T) {
sandbox2.PodSandboxStatus.Id: getTestContainerInfo(seedSandbox2, pName2, sandbox2.PodSandboxStatus.Metadata.Namespace, leaky.PodInfraContainerName),
sandbox2Cgroup: getTestContainerInfo(seedSandbox2, "", "", ""),
container4.ContainerStatus.Id: getTestContainerInfo(seedContainer3, pName2, sandbox2.PodSandboxStatus.Metadata.Namespace, cName3),
sandbox3Cgroup: getTestContainerInfo(seedSandbox3, "", "", ""),
}

options := cadvisorapiv2.RequestOptions{
Expand All @@ -173,10 +179,10 @@ func TestCRIListPodStats(t *testing.T) {
sandbox0, sandbox1, sandbox2, sandbox3, sandbox4, sandbox5,
})
fakeRuntimeService.SetFakeContainers([]*critest.FakeContainer{
container0, container1, container2, container3, container4, container5, container6, container7,
container0, container1, container2, container3, container4, container5, container6, container7, container8,
})
fakeRuntimeService.SetFakeContainerStats([]*runtimeapi.ContainerStats{
containerStats0, containerStats1, containerStats2, containerStats3, containerStats4, containerStats5, containerStats6, containerStats7,
containerStats0, containerStats1, containerStats2, containerStats3, containerStats4, containerStats5, containerStats6, containerStats7, containerStats8,
})

ephemeralVolumes := makeFakeVolumeStats([]string{"ephVolume1, ephVolumes2"})
Expand Down Expand Up @@ -299,14 +305,12 @@ func TestCRIListPodStats(t *testing.T) {
assert.Equal(sandbox3.CreatedAt, p3.StartTime.UnixNano())
assert.Equal(1, len(p3.Containers))

c5 := p3.Containers[0]
assert.Equal(cName5, c5.Name)
assert.Equal(container5.CreatedAt, c5.StartTime.UnixNano())
assert.NotNil(c5.CPU.Time)
assert.Zero(*c5.CPU.UsageCoreNanoSeconds)
assert.Zero(*c5.CPU.UsageNanoCores)
assert.NotNil(c5.Memory.Time)
assert.Zero(*c5.Memory.WorkingSetBytes)
c8 := p3.Containers[0]
assert.Equal(cName8, c8.Name)
assert.Equal(container8.CreatedAt, c8.StartTime.UnixNano())
assert.NotNil(c8.CPU.Time)
assert.NotNil(c8.Memory.Time)
checkCRIPodCPUAndMemoryStats(assert, p3, infos[sandbox3Cgroup].Stats[0])

mockCadvisor.AssertExpectations(t)
}
Expand Down Expand Up @@ -336,9 +340,13 @@ func TestCRIListPodCPUAndMemoryStats(t *testing.T) {
container4 = makeFakeContainer(sandbox2, cName3, 1, false)
containerStats4 = makeFakeContainerStats(container4, imageFsMountpoint)

// Running pod with a terminated container and a running container
sandbox3 = makeFakePodSandbox("sandbox3-name", "sandbox3-uid", "sandbox3-ns", false)
sandbox3Cgroup = "/" + cm.GetPodCgroupNameSuffix(types.UID(sandbox2.PodSandboxStatus.Metadata.Uid))
container5 = makeFakeContainer(sandbox3, cName5, 0, true)
containerStats5 = makeFakeContainerStats(container5, imageFsMountpoint)
container8 = makeFakeContainer(sandbox3, cName8, 0, false)
containerStats8 = makeFakeContainerStats(container8, imageFsMountpoint)

// Terminated pod sandbox
sandbox4 = makeFakePodSandbox("sandbox1-name", "sandbox1-uid", "sandbox1-ns", true)
Expand Down Expand Up @@ -373,6 +381,7 @@ func TestCRIListPodCPUAndMemoryStats(t *testing.T) {
sandbox2.PodSandboxStatus.Id: getTestContainerInfo(seedSandbox2, pName2, sandbox2.PodSandboxStatus.Metadata.Namespace, leaky.PodInfraContainerName),
sandbox2Cgroup: getTestContainerInfo(seedSandbox2, "", "", ""),
container4.ContainerStatus.Id: getTestContainerInfo(seedContainer3, pName2, sandbox2.PodSandboxStatus.Metadata.Namespace, cName3),
sandbox3Cgroup: getTestContainerInfo(seedSandbox3, "", "", ""),
}

options := cadvisorapiv2.RequestOptions{
Expand All @@ -387,10 +396,10 @@ func TestCRIListPodCPUAndMemoryStats(t *testing.T) {
sandbox0, sandbox1, sandbox2, sandbox3, sandbox4, sandbox5,
})
fakeRuntimeService.SetFakeContainers([]*critest.FakeContainer{
container0, container1, container2, container3, container4, container5, container6, container7,
container0, container1, container2, container3, container4, container5, container6, container7, container8,
})
fakeRuntimeService.SetFakeContainerStats([]*runtimeapi.ContainerStats{
containerStats0, containerStats1, containerStats2, containerStats3, containerStats4, containerStats5, containerStats6, containerStats7,
containerStats0, containerStats1, containerStats2, containerStats3, containerStats4, containerStats5, containerStats6, containerStats7, containerStats8,
})

ephemeralVolumes := makeFakeVolumeStats([]string{"ephVolume1, ephVolumes2"})
Expand Down Expand Up @@ -487,14 +496,12 @@ func TestCRIListPodCPUAndMemoryStats(t *testing.T) {
assert.Equal(sandbox3.CreatedAt, p3.StartTime.UnixNano())
assert.Equal(1, len(p3.Containers))

c5 := p3.Containers[0]
assert.Equal(cName5, c5.Name)
assert.Equal(container5.CreatedAt, c5.StartTime.UnixNano())
assert.NotNil(c5.CPU.Time)
assert.Zero(*c5.CPU.UsageCoreNanoSeconds)
assert.Zero(*c5.CPU.UsageNanoCores)
assert.NotNil(c5.Memory.Time)
assert.Zero(*c5.Memory.WorkingSetBytes)
c8 := p3.Containers[0]
assert.Equal(cName8, c8.Name)
assert.Equal(container8.CreatedAt, c8.StartTime.UnixNano())
assert.NotNil(c8.CPU.Time)
assert.NotNil(c8.Memory.Time)
checkCRIPodCPUAndMemoryStats(assert, p3, infos[sandbox3Cgroup].Stats[0])

mockCadvisor.AssertExpectations(t)
}
Expand Down Expand Up @@ -882,6 +889,31 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
},
expected: &value2,
},
{
desc: "should return nil if cpuacct is reset to 0 in a live container",
stats: &runtimeapi.ContainerStats{
Attributes: &runtimeapi.ContainerAttributes{
Id: "1",
},
Cpu: &runtimeapi.CpuUsage{
Timestamp: 2,
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
Value: 0,
},
},
},
cpuUsageCache: map[string]*cpuUsageRecord{
"1": {
stats: &runtimeapi.CpuUsage{
Timestamp: 1,
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
Value: 10000000000,
},
},
},
},
expected: nil,
},
}

for _, test := range tests {
Expand Down