Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion server/container_stats_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package server

import (
"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/oci"
"golang.org/x/net/context"
pb "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

// ListContainerStats returns stats of all running containers.
func (s *Server) ListContainerStats(ctx context.Context, req *pb.ListContainerStatsRequest) (*pb.ListContainerStatsResponse, error) {
ctrList, err := s.ContainerServer.ListContainers()
ctrList, err := s.ContainerServer.ListContainers(
func(container *oci.Container) bool {
return container.StateNoLock().Status != oci.ContainerStateStopped
},
)
if err != nil {
return nil, err
}
Expand Down
61 changes: 61 additions & 0 deletions server/container_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server_test
import (
"context"

"github.com/cri-o/cri-o/internal/oci"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pb "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
Expand Down Expand Up @@ -31,3 +32,63 @@ var _ = t.Describe("ContainerStats", func() {
})
})
})

var _ = t.Describe("ContainerStatsList", func() {
// Prepare the sut
BeforeEach(func() {
beforeEach()
setupSUT()
})

AfterEach(afterEach)

t.Describe("ContainerStatsList", func() {
It("should succeed", func() {
// Given
addContainerAndSandbox()
// When
response, err := sut.ListContainerStats(context.Background(),
&pb.ListContainerStatsRequest{})

// Then
Expect(err).To(BeNil())
Expect(response).NotTo(BeNil())
Expect(len(response.Stats)).To(Equal(1))
})
It("should filter stopped container", func() {
// Given
state := oci.ContainerState{}
state.Status = oci.ContainerStateStopped
testContainer.SetState(&state)
addContainerAndSandbox()

// When
response, err := sut.ListContainerStats(context.Background(),
&pb.ListContainerStatsRequest{},
)

// Then
Expect(err).To(BeNil())
Expect(response).NotTo(BeNil())
Expect(len(response.Stats)).To(Equal(0))
})
It("should filter by id", func() {
// Given
addContainerAndSandbox()

// When
response, err := sut.ListContainerStats(context.Background(),
&pb.ListContainerStatsRequest{
Filter: &pb.ContainerStatsFilter{
Id: "invalid",
},
},
)

// Then
Expect(err).To(BeNil())
Expect(response).NotTo(BeNil())
Expect(len(response.Stats)).To(Equal(0))
})
})
})