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

grpc :Add the StatsContainer api for events cli #222

Merged
merged 1 commit into from
May 10, 2018
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
29 changes: 29 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,35 @@ func (a *agentGRPC) UpdateContainer(ctx context.Context, req *pb.UpdateContainer
return emptyResp, c.container.Set(config)
}

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
30 changes: 30 additions & 0 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,33 @@ func TestUpdateContainer(t *testing.T) {
assert.NoError(err)
assert.Equal(emptyResp, r)
}

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)
assert.Nil(r)

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 Down Expand Up @@ -46,7 +47,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