Skip to content

Commit

Permalink
Add test coverage for container stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jellonek committed Sep 12, 2018
1 parent b27a9b5 commit 22eedcb
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pkg/image/fake/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ func (s *FakeStore) FilesystemStats() (*types.FilesystemStats, error) {
UsedInodes: fakeUsedInodes,
}, nil
}

// BytesUsedBy implements BytesUsedBy method from Store interface.
func (s *FakeStore) BytesUsedBy(path string) (uint64, error) {
return fakeUsedBytes, nil
}
12 changes: 12 additions & 0 deletions pkg/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type Store interface {

// FilesystemStats returns disk space and inode usage info for this store.
FilesystemStats() (*types.FilesystemStats, error)

// BytesUsedBy returns disk usage of the file in this store.
BytesUsedBy(path string) (uint64, error)
}

// VirtualSizeFunc specifies a function that returns the virtual
Expand Down Expand Up @@ -603,3 +606,12 @@ func (s *FileStore) FilesystemStats() (*types.FilesystemStats, error) {
UsedInodes: occupiedInodes,
}, nil
}

// BytesUsedBy return disk usage of provided file as seen in store
func (s *FileStore) BytesUsedBy(path string) (uint64, error) {
fstat, err := os.Stat(path)
if err != nil {
return 0, err
}
return uint64(fstat.Size()), nil
}
5 changes: 5 additions & 0 deletions pkg/libvirttools/root_volumesource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (im *FakeImageManager) FilesystemStats() (*types.FilesystemStats, error) {
}, nil
}

func (im *FakeImageManager) BytesUsedBy(path string) (uint64, error) {
im.rec.Rec("BytesUsedBy", path)
return fakeImageVirtualSize, nil
}

func TestRootVolumeNaming(t *testing.T) {
v := rootVolume{
volumeBase{
Expand Down
7 changes: 3 additions & 4 deletions pkg/libvirttools/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package libvirttools

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -818,7 +817,7 @@ func (v *VirtualizationTool) VMStats(containerID string, name string) (*types.VM
return nil, err
}
vs := types.VMStats{
Timestamp: time.Now().UnixNano(),
Timestamp: v.clock.Now().UnixNano(),
ContainerID: containerID,
Name: name,
}
Expand Down Expand Up @@ -853,11 +852,11 @@ func (v *VirtualizationTool) VMStats(containerID string, name string) (*types.VM
return nil, fmt.Errorf("cannot locate root disk in domain definition")
}

fstat, err := os.Stat(rootDiskLocation)
rootDiskSize, err := v.ImageManager().BytesUsedBy(rootDiskLocation)
if err != nil {
return nil, err
}
vs.FsBytes = uint64(fstat.Size())
vs.FsBytes = rootDiskSize

return &vs, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/libvirttools/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type ImageManager interface {
GetImagePathAndVirtualSize(ref string) (string, uint64, error)
FilesystemStats() (*types.FilesystemStats, error)
BytesUsedBy(path string) (uint64, error)
}

type volumeOwner interface {
Expand Down
50 changes: 50 additions & 0 deletions pkg/manager/TestCRIPods.out.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,56 @@
name: container-for-testName_0
started_at: 1524648266720331175
state: 1
- name: 'enter: ContainerStats'
value:
container_id: 231700d5-c9a6-5a49-738d-99a954c51550
- name: 'leave: ContainerStats'
value:
stats:
attributes:
id: 231700d5-c9a6-5a49-738d-99a954c51550
metadata:
name: 231700d5-c9a6-5a49-738d-99a954c51550
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
- name: 'enter: ListContainerStats'
value: {}
- name: 'domain conn: ListDomains'
value:
- virtlet-231700d5-c9a6-container-for-testName_0
- name: 'leave: ListContainerStats'
value:
stats:
- attributes:
id: 231700d5-c9a6-5a49-738d-99a954c51550
metadata:
name: 231700d5-c9a6-5a49-738d-99a954c51550
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
- name: 'enter: PullImage'
value:
image:
Expand Down
117 changes: 117 additions & 0 deletions pkg/manager/TestContainerListStats.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
- name: 'enter: ListContainerStats'
value: {}
- name: 'leave: ListContainerStats'
value:
stats:
- attributes:
id: 231700d5-c9a6-5a49-738d-99a954c51550
metadata:
name: 231700d5-c9a6-5a49-738d-99a954c51550
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
- attributes:
id: 6b94d9a7-e22a-5d08-65ee-16b9b1e07ab0
metadata:
name: 6b94d9a7-e22a-5d08-65ee-16b9b1e07ab0
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
- name: 'enter: ListContainerStats'
value:
filter:
id: 231700d5-c9a6-5a49-738d-99a954c51550
- name: 'leave: ListContainerStats'
value:
stats:
- attributes:
id: 231700d5-c9a6-5a49-738d-99a954c51550
metadata:
name: 231700d5-c9a6-5a49-738d-99a954c51550
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
- name: 'enter: ListContainerStats'
value:
filter:
label_selector:
io.kubernetes.pod.name: testName_1
- name: 'leave: ListContainerStats'
value:
stats:
- attributes:
id: 6b94d9a7-e22a-5d08-65ee-16b9b1e07ab0
metadata:
name: 6b94d9a7-e22a-5d08-65ee-16b9b1e07ab0
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
- name: 'enter: ListContainerStats'
value:
filter:
pod_sandbox_id: 69eec606-0493-5825-73a4-c5e0c0236155
- name: 'leave: ListContainerStats'
value:
stats:
- attributes:
id: 231700d5-c9a6-5a49-738d-99a954c51550
metadata:
name: 231700d5-c9a6-5a49-738d-99a954c51550
cpu:
timestamp: 1524648266720331175
usage_core_nano_seconds: {}
memory:
timestamp: 1524648266720331175
working_set_bytes: {}
writable_layer:
fs_id:
mountpoint: /var/lib/virtlet
inodes_used:
value: 1
timestamp: 1524648266720331175
used_bytes:
value: 1073741824
41 changes: 41 additions & 0 deletions pkg/manager/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ func (tst *virtletCRITester) listContainers(filter *kubeapi.ContainerFilter) {
tst.invoke("ListContainers", &kubeapi.ListContainersRequest{Filter: filter}, true)
}

func (tst *virtletCRITester) listContainerStats(filter *kubeapi.ContainerStatsFilter) {
tst.invoke("ListContainerStats", &kubeapi.ListContainerStatsRequest{Filter: filter}, true)
}

func (tst *virtletCRITester) createContainer(sandbox *kubeapi.PodSandboxConfig, container *criapi.ContainerTestConfig, imageSpec *kubeapi.ImageSpec, mounts []*kubeapi.Mount) string {
req := createContainerRequest(sandbox, container, imageSpec, mounts)
resp, err := tst.invoke("CreateContainer", req, true)
Expand Down Expand Up @@ -397,6 +401,10 @@ func (tst *virtletCRITester) stopContainer(containerID string) {
tst.invoke("StopContainer", &kubeapi.StopContainerRequest{ContainerId: containerID}, true)
}

func (tst *virtletCRITester) containerStats(containerID string) {
tst.invoke("ContainerStats", &kubeapi.ContainerStatsRequest{ContainerId: containerID}, true)
}

func (tst *virtletCRITester) removeContainer(containerID string) {
tst.invoke("RemoveContainer", &kubeapi.RemoveContainerRequest{ContainerId: containerID}, true)
}
Expand Down Expand Up @@ -457,6 +465,8 @@ func TestCRIPods(t *testing.T) {
tst.containerStatus(containerId1)
tst.startContainer(containerId1)
tst.containerStatus(containerId1)
tst.containerStats(containerId1)
tst.listContainerStats(nil)

tst.pullImage(ubuntuImg())
tst.runPodSandbox(sandboxes[1])
Expand Down Expand Up @@ -626,6 +636,37 @@ func TestCRIContainerFilters(t *testing.T) {
tst.verify()
}

func TestContainerListStats(t *testing.T) {
tst := makeVirtletCRITester(t)
tst.rec.AddFilter("ListContainers")
tst.rec.AddFilter("ListContainerStats")
defer tst.teardown()

sandboxes := criapi.GetSandboxes(2)
containers := criapi.GetContainersConfig(sandboxes)
tst.pullImage(cirrosImg())
tst.runPodSandbox(sandboxes[0])
containerId1 := tst.createContainer(sandboxes[0], containers[0], cirrosImg(), nil)
tst.startContainer(containerId1)
tst.pullImage(ubuntuImg())
tst.runPodSandbox(sandboxes[1])
containerId2 := tst.createContainer(sandboxes[1], containers[1], ubuntuImg(), nil)
tst.startContainer(containerId2)

tst.listContainerStats(nil)
tst.listContainerStats(&kubeapi.ContainerStatsFilter{Id: containerId1})
tst.listContainerStats(&kubeapi.ContainerStatsFilter{
LabelSelector: map[string]string{
"io.kubernetes.pod.name": "testName_1",
},
})
tst.listContainerStats(&kubeapi.ContainerStatsFilter{
PodSandboxId: sandboxes[0].Metadata.Uid,
})

tst.verify()
}

func TestCRIAttachPortForward(t *testing.T) {
tst := makeVirtletCRITester(t)
tst.rec.AddFilter("Attach")
Expand Down

0 comments on commit 22eedcb

Please sign in to comment.