Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
grpc :[RPC]Add the StatsContainer api for events cli
Browse files Browse the repository at this point in the history
Add `StatsContainer` API to display container events

Fixes: #221

Signed-off-by: c00416947 <caihaomin@huawei.com>
  • Loading branch information
jshachm committed Apr 28, 2018
1 parent bf6f367 commit 4db1116
Show file tree
Hide file tree
Showing 7 changed files with 4,530 additions and 1,076 deletions.
28 changes: 28 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,34 @@ func (a *agentGRPC) ListProcesses(ctx context.Context, req *pb.ListProcessesRequ
return resp, nil
}

func (a *agentGRPC) StatsContainer(ctx context.Context, req *pb.StatsContainerRequest) (*pb.StatsContainerResponse, error) {
c, err := a.sandbox.getContainer(req.ContainerId)
if err != nil {
return nil, err
}

stats, err := c.container.Stats()
if err != nil {
return nil, err
}

data, err := json.Marshal(stats.CgroupStats)
if err != nil {
return nil, err
}

var cgroupStats pb.CgroupStats
err = json.Unmarshal(data, &cgroupStats)
if err != nil {
return nil, err
}
resp := &pb.StatsContainerResponse{
CgroupStats: &cgroupStats,
}

return resp, nil
}

func (a *agentGRPC) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*gpb.Empty, error) {
ctr, err := a.sandbox.getContainer(req.ContainerId)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,31 @@ func TestListProcesses(t *testing.T) {
assert.NotNil(r)
assert.NotEmpty(r.ProcessList)
}

func TestStatsContainer(t *testing.T) {
containerID := "1"
assert := assert.New(t)
req := &pb.StatsContainerRequest{
ContainerId: containerID,
}

a := &agentGRPC{
sandbox: &sandbox{
containers: make(map[string]*container),
},
}

//getcontainer should failed
r, err := a.StatsContainer(context.TODO(), req)
assert.Error(err)

a.sandbox.containers[containerID] = &container{
container: &mockContainer{
id: containerID,
},
}

r, err = a.StatsContainer(context.TODO(), req)
assert.NoError(err)
assert.NotNil(r)
}
3 changes: 2 additions & 1 deletion mockcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type mockContainer struct {
id string
status libcontainer.Status
stats libcontainer.Stats
processes []int
}

Expand All @@ -40,7 +41,7 @@ func (m *mockContainer) Processes() ([]int, error) {
}

func (m *mockContainer) Stats() (*libcontainer.Stats, error) {
return nil, nil
return &m.stats, nil
}

func (m *mockContainer) Set(config configs.Config) error {
Expand Down
8 changes: 6 additions & 2 deletions mockcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main

import (
"github.com/opencontainers/runc/libcontainer/cgroups"
"syscall"
"testing"

Expand Down Expand Up @@ -58,10 +59,13 @@ func TestMockContainerProcesses(t *testing.T) {

func TestMockContainerStats(t *testing.T) {
assert := assert.New(t)
m := &mockContainer{}
expectedStats := &libcontainer.Stats{
CgroupStats: &cgroups.Stats{},
}
m := &mockContainer{stats: *expectedStats}
st, err := m.Stats()
assert.NoError(err)
assert.Nil(st)
assert.Equal(expectedStats, st)
}

func TestMockContainerSet(t *testing.T) {
Expand Down
Loading

0 comments on commit 4db1116

Please sign in to comment.