Updating info function to use engine-api

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
This commit is contained in:
Nishant Totla 2016-03-08 16:29:58 -08:00
parent 9390d5f335
commit 6af37c4888
8 changed files with 48 additions and 51 deletions

View File

@ -47,7 +47,7 @@ func getInfo(c *context, w http.ResponseWriter, r *http.Request) {
ServerVersion: "swarm/" + version.VERSION,
OperatingSystem: runtime.GOOS,
Architecture: runtime.GOARCH,
NCPU: int(c.cluster.TotalCpus()),
NCPU: c.cluster.TotalCpus(),
MemTotal: c.cluster.TotalMemory(),
HTTPProxy: os.Getenv("http_proxy"),
HTTPSProxy: os.Getenv("https_proxy"),

View File

@ -78,7 +78,7 @@ type Cluster interface {
TotalMemory() int64
// Return the number of CPUs in the cluster
TotalCpus() int64
TotalCpus() int
// Register an event handler for cluster-wide events.
RegisterEventHandler(h EventHandler) error

View File

@ -100,7 +100,7 @@ type Engine struct {
IP string
Addr string
Name string
Cpus int64
Cpus int
Memory int64
Labels map[string]string
@ -408,7 +408,7 @@ func (e *Engine) CheckConnectionErr(err error) {
// Gather engine specs (CPU, memory, constraints, ...).
func (e *Engine) updateSpecs() error {
info, err := e.client.Info()
info, err := e.apiClient.Info()
e.CheckConnectionErr(err)
if err != nil {
return err
@ -665,7 +665,7 @@ func (e *Engine) updateContainer(c dockerclient.Container, containers map[string
container.Config = BuildContainerConfig(*info.Config)
// FIXME remove "duplicate" lines and move this to cluster/config.go
container.Config.CpuShares = container.Config.CpuShares * e.Cpus / 1024.0
container.Config.CpuShares = container.Config.CpuShares * int64(e.Cpus) / 1024.0
container.Config.HostConfig.CpuShares = container.Config.CpuShares
// Save the entire inspect back into the container.
@ -774,8 +774,8 @@ func (e *Engine) TotalMemory() int64 {
}
// TotalCpus returns the total cpus + overcommit
func (e *Engine) TotalCpus() int64 {
return e.Cpus + (e.Cpus * e.overcommitRatio / 100)
func (e *Engine) TotalCpus() int {
return e.Cpus + (e.Cpus * int(e.overcommitRatio) / 100)
}
// Create a new container

View File

@ -19,7 +19,7 @@ import (
)
var (
mockInfo = &dockerclient.Info{
mockInfo = types.Info{
ID: "id",
Name: "name",
NCPU: 10,
@ -115,7 +115,7 @@ func TestEngineConnectionFailure(t *testing.T) {
// Always fail.
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(&dockerclient.Info{}, errors.New("fail"))
apiClient.On("Info").Return(types.Info{}, errors.New("fail"))
// Connect() should fail
assert.Error(t, engine.ConnectWithClient(client, apiClient))
@ -127,13 +127,14 @@ func TestEngineConnectionFailure(t *testing.T) {
assert.False(t, engine.isConnected())
client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
func TestOutdatedEngine(t *testing.T) {
engine := NewEngine("test", 0, engOpts)
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(&dockerclient.Info{}, nil)
apiClient.On("Info").Return(types.Info{}, nil)
assert.Error(t, engine.ConnectWithClient(client, apiClient))
@ -143,6 +144,7 @@ func TestOutdatedEngine(t *testing.T) {
assert.False(t, engine.isConnected())
client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
func TestEngineCpusMemory(t *testing.T) {
@ -152,15 +154,14 @@ func TestEngineCpusMemory(t *testing.T) {
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil)
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil)
client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil)
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
apiClient.On("ServerVersion").Return(mockVersion, nil)
assert.NoError(t, engine.ConnectWithClient(client, apiClient))
assert.True(t, engine.isConnected())
assert.True(t, engine.IsHealthy())
@ -169,6 +170,7 @@ func TestEngineCpusMemory(t *testing.T) {
assert.Equal(t, engine.UsedMemory(), int64(0))
client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
func TestEngineSpecs(t *testing.T) {
@ -178,15 +180,14 @@ func TestEngineSpecs(t *testing.T) {
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil)
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil)
client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil)
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
apiClient.On("ServerVersion").Return(mockVersion, nil)
assert.NoError(t, engine.ConnectWithClient(client, apiClient))
assert.True(t, engine.isConnected())
assert.True(t, engine.IsHealthy())
@ -200,6 +201,7 @@ func TestEngineSpecs(t *testing.T) {
assert.Equal(t, engine.Labels["foo"], "bar")
client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
func TestEngineState(t *testing.T) {
@ -209,10 +211,9 @@ func TestEngineState(t *testing.T) {
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
// The client will return one container at first, then a second one will appear.
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "one"}}, nil).Once()
@ -245,6 +246,7 @@ func TestEngineState(t *testing.T) {
}
client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
func TestCreateContainer(t *testing.T) {
@ -261,15 +263,14 @@ func TestCreateContainer(t *testing.T) {
)
engine.setState(stateUnhealthy)
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once()
client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil)
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
assert.NoError(t, engine.ConnectWithClient(client, apiClient))
assert.True(t, engine.isConnected())
@ -344,17 +345,17 @@ func TestTotalMemory(t *testing.T) {
func TestTotalCpus(t *testing.T) {
engine := NewEngine("test", 0.05, engOpts)
engine.Cpus = 2
assert.Equal(t, engine.TotalCpus(), int64(2+2*5/100))
assert.Equal(t, engine.TotalCpus(), 2+2*5/100)
engine = NewEngine("test", 0, engOpts)
engine.Cpus = 2
assert.Equal(t, engine.TotalCpus(), int64(2))
assert.Equal(t, engine.TotalCpus(), 2)
}
func TestUsedCpus(t *testing.T) {
var (
containerNcpu = []int64{1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
hostNcpu = []int64{1, 2, 4, 8, 10, 12, 16, 20, 32, 36, 40, 48}
hostNcpu = []int{1, 2, 4, 8, 10, 12, 16, 20, 32, 36, 40, 48}
)
engine := NewEngine("test", 0, engOpts)
@ -364,11 +365,12 @@ func TestUsedCpus(t *testing.T) {
for _, hn := range hostNcpu {
for _, cn := range containerNcpu {
if cn <= hn {
if int(cn) <= hn {
mockInfo.NCPU = hn
cpuShares := int64(math.Ceil(float64(cn*1024) / float64(mockInfo.NCPU)))
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil).Once()
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "test"}}, nil).Once()
@ -376,10 +378,9 @@ func TestUsedCpus(t *testing.T) {
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
client.On("InspectContainer", "test").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: cpuShares}}, nil).Once()
apiClient.On("ServerVersion").Return(mockVersion, nil)
engine.ConnectWithClient(client, apiClient)
assert.Equal(t, engine.Cpus, mockInfo.NCPU)
assert.Equal(t, engine.UsedCpus(), cn)
}
}
@ -402,7 +403,8 @@ func TestContainerRemovedDuringRefresh(t *testing.T) {
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{container1, container2}, nil)
@ -411,8 +413,6 @@ func TestContainerRemovedDuringRefresh(t *testing.T) {
client.On("InspectContainer", "c1").Return(info1, errors.New("Not found"))
client.On("InspectContainer", "c2").Return(info2, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
assert.NoError(t, engine.ConnectWithClient(client, apiClient))
assert.Nil(t, engine.RefreshContainers(true))
@ -422,6 +422,7 @@ func TestContainerRemovedDuringRefresh(t *testing.T) {
assert.Equal(t, containers[0].Id, "c2")
client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
func TestDisconnect(t *testing.T) {
@ -430,12 +431,11 @@ func TestDisconnect(t *testing.T) {
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("StopAllMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
apiClient.On("ServerVersion").Return(mockVersion, nil)
// The client will return one container at first, then a second one will appear.
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "one"}}, nil).Once()
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once()

View File

@ -446,12 +446,12 @@ func (c *Cluster) TotalMemory() int64 {
}
// TotalCpus return the total memory of the cluster
func (c *Cluster) TotalCpus() int64 {
func (c *Cluster) TotalCpus() int {
c.RLock()
defer c.RUnlock()
var totalCpus int64
var totalCpus int
for _, s := range c.agents {
totalCpus += int64(sumScalarResourceValue(s.offers, "cpus"))
totalCpus += int(sumScalarResourceValue(s.offers, "cpus"))
}
return totalCpus
}

View File

@ -824,8 +824,8 @@ func (c *Cluster) TotalMemory() int64 {
}
// TotalCpus return the total memory of the cluster
func (c *Cluster) TotalCpus() int64 {
var totalCpus int64
func (c *Cluster) TotalCpus() int {
var totalCpus int
for _, engine := range c.engines {
totalCpus += engine.TotalCpus()
}

View File

@ -26,7 +26,7 @@ func (nopCloser) Close() error {
}
var (
mockInfo = &dockerclient.Info{
mockInfo = types.Info{
ID: "test-engine",
Name: "name",
NCPU: 10,
@ -135,15 +135,14 @@ func TestImportImage(t *testing.T) {
// create mock client
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil)
client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil)
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
// connect client
engine.ConnectWithClient(client, apiClient)
@ -187,15 +186,14 @@ func TestLoadImage(t *testing.T) {
// create mock client
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil)
client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil)
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
// connect client
engine.ConnectWithClient(client, apiClient)
@ -242,15 +240,14 @@ func TestTagImage(t *testing.T) {
// create mock client
client := mockclient.NewMockClient()
apiClient := engineapimock.NewMockClient()
client.On("Info").Return(mockInfo, nil)
apiClient.On("Info").Return(mockInfo, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
client.On("ListImages", mock.Anything).Return(images, nil)
client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil)
client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil)
apiClient.On("ServerVersion").Return(mockVersion, nil)
// connect client
engine.ConnectWithClient(client, apiClient)

View File

@ -37,7 +37,7 @@ func NewNode(e *cluster.Engine) *Node {
UsedMemory: e.UsedMemory(),
UsedCpus: e.UsedCpus(),
TotalMemory: e.TotalMemory(),
TotalCpus: e.TotalCpus(),
TotalCpus: int64(e.TotalCpus()),
HealthIndicator: e.HealthIndicator(),
}
}