Merge branch 'next' of github.com:xetorthio/play-with-docker into next

This commit is contained in:
Marcos Lilljedahl
2017-09-21 15:06:37 -03:00
15 changed files with 111 additions and 35 deletions

View File

@@ -32,6 +32,7 @@ func TestCheckSwarmPorts_RunWhenManager(t *testing.T) {
IP: "10.0.0.1",
Name: "aaaabbbb_node1",
SessionId: "aaaabbbbcccc",
Hostname: "node1",
}
info := dockerTypes.Info{
Swarm: swarm.Info{

View File

@@ -12,10 +12,12 @@ import (
dockerTypes "github.com/docker/docker/api/types"
units "github.com/docker/go-units"
lru "github.com/hashicorp/golang-lru"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/router"
"github.com/play-with-docker/play-with-docker/storage"
)
type InstanceStats struct {
@@ -28,6 +30,8 @@ type collectStats struct {
event event.EventApi
factory docker.FactoryApi
cli *http.Client
cache *lru.Cache
storage storage.StorageApi
}
var CollectStatsEvent event.EventType
@@ -71,7 +75,18 @@ func (t *collectStats) Run(ctx context.Context, instance *types.Instance) error
t.event.Emit(CollectStatsEvent, instance.SessionId, stats)
return nil
}
dockerClient, err := t.factory.GetForSession(instance.SessionId)
var session *types.Session
if sess, found := t.cache.Get(instance.SessionId); !found {
s, err := t.storage.SessionGet(instance.SessionId)
if err != nil {
return err
}
t.cache.Add(s.Id, s)
session = s
} else {
session = sess.(*types.Session)
}
dockerClient, err := t.factory.GetForSession(session)
if err != nil {
log.Println(err)
return err
@@ -119,7 +134,7 @@ func proxyHost(r *http.Request) (*url.URL, error) {
return u, nil
}
func NewCollectStats(e event.EventApi, f docker.FactoryApi) *collectStats {
func NewCollectStats(e event.EventApi, f docker.FactoryApi, s storage.StorageApi) *collectStats {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 1 * time.Second,
@@ -131,7 +146,8 @@ func NewCollectStats(e event.EventApi, f docker.FactoryApi) *collectStats {
cli := &http.Client{
Transport: transport,
}
return &collectStats{event: e, factory: f, cli: cli}
c, _ := lru.New(5000)
return &collectStats{event: e, factory: f, cli: cli, cache: c, storage: s}
}
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *dockerTypes.StatsJSON) float64 {

View File

@@ -11,6 +11,7 @@ import (
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@@ -19,8 +20,8 @@ type mockSessionProvider struct {
mock.Mock
}
func (m *mockSessionProvider) GetDocker(sessionId string) (docker.DockerApi, error) {
args := m.Called(sessionId)
func (m *mockSessionProvider) GetDocker(session *types.Session) (docker.DockerApi, error) {
args := m.Called(session)
return args.Get(0).(docker.DockerApi), args.Error(1)
}
@@ -34,8 +35,9 @@ func (nopCloser) Close() error { return nil }
func TestCollectStats_Name(t *testing.T) {
e := &event.Mock{}
f := &docker.FactoryMock{}
s := &storage.Mock{}
task := NewCollectStats(e, f)
task := NewCollectStats(e, f, s)
assert.Equal(t, "CollectStats", task.Name())
e.M.AssertExpectations(t)
@@ -46,6 +48,7 @@ func TestCollectStats_Run(t *testing.T) {
d := &docker.Mock{}
e := &event.Mock{}
f := &docker.FactoryMock{}
s := &storage.Mock{}
stats := dockerTypes.StatsJSON{}
b, _ := json.Marshal(stats)
@@ -53,13 +56,19 @@ func TestCollectStats_Run(t *testing.T) {
IP: "10.0.0.1",
Name: "aaaabbbb_node1",
SessionId: "aaaabbbbcccc",
Hostname: "node1",
}
f.On("GetForSession", i.SessionId).Return(d, nil)
sess := &types.Session{
Id: "aaaabbbbcccc",
}
s.On("SessionGet", i.SessionId).Return(sess, nil)
f.On("GetForSession", sess).Return(d, nil)
d.On("GetContainerStats", i.Name).Return(nopCloser{bytes.NewReader(b)}, nil)
e.M.On("Emit", CollectStatsEvent, "aaaabbbbcccc", []interface{}{InstanceStats{Instance: i.Name, Mem: "0.00% (0B / 0B)", Cpu: "0.00%"}}).Return()
task := NewCollectStats(e, f)
task := NewCollectStats(e, f, s)
ctx := context.Background()
err := task.Run(ctx, i)