Skip to content

Commit

Permalink
Fix test after enabled metrics on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Peng Yin committed Nov 15, 2017
1 parent 7154c59 commit c1827c5
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 112 deletions.
83 changes: 0 additions & 83 deletions agent/app/agent_windows_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion agent/config/config_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestConfigDefault(t *testing.T) {

assert.Equal(t, "npipe:////./pipe/docker_engine", cfg.DockerEndpoint, "Default docker endpoint set incorrectly")
assert.Equal(t, `C:\ProgramData\Amazon\ECS\data`, cfg.DataDir, "Default datadir set incorrectly")
assert.True(t, cfg.DisableMetrics, "Default disablemetrics set incorrectly")
assert.False(t, cfg.DisableMetrics, "Default disablemetrics set incorrectly")
assert.Equal(t, 10, len(cfg.ReservedPorts), "Default reserved ports set incorrectly")
assert.Equal(t, uint16(0), cfg.ReservedMemory, "Default reserved memory set incorrectly")
assert.Equal(t, 30*time.Second, cfg.DockerStopTimeout, "Default docker stop container timeout set incorrectly")
Expand Down
4 changes: 2 additions & 2 deletions agent/stats/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func TestContainerStatsCollection(t *testing.T) {
// deal with the docker.Stats.MemoryStats inner struct
jsonStat := fmt.Sprintf(`
{
"memory_stats": {"usage":%d},
"memory_stats": {"usage":%d, "privateworkingset":%d},
"cpu_stats":{
"cpu_usage":{
"percpu_usage":[%d],
"total_usage":%d
}
}
}`, stat.memBytes, stat.cpuTime, stat.cpuTime)
}`, stat.memBytes, stat.memBytes, stat.cpuTime, stat.cpuTime)
dockerStat := &docker.Stats{}
json.Unmarshal([]byte(jsonStat), dockerStat)
dockerStat.Read = stat.timestamp
Expand Down
24 changes: 3 additions & 21 deletions agent/stats/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,6 @@ func TestDockerStatsToContainerStatsCpuUsage(t *testing.T) {
}
}

func TestDockerStatsToContainerStatsZeroCoresGeneratesError(t *testing.T) {
// doing this with json makes me sad, but is the easiest way to deal with
// the inner structs
jsonStat := fmt.Sprintf(`
{
"cpu_stats":{
"cpu_usage":{
"total_usage":%d
}
}
}`, 100)
dockerStat := &docker.Stats{}
json.Unmarshal([]byte(jsonStat), dockerStat)
_, err := dockerStatsToContainerStats(dockerStat)
if err == nil {
t.Error("Expected error converting container stats with empty PercpuUsage")
}
}

func TestDockerStatsToContainerStatsMemUsage(t *testing.T) {
jsonStat := fmt.Sprintf(`
{
Expand All @@ -100,9 +81,10 @@ func TestDockerStatsToContainerStatsMemUsage(t *testing.T) {
"stats": {
"cache": %d,
"rss": %d
}
},
"privateworkingset": %d
}
}`, 1, 2, 3, 4, 100, 30, 100, 20, 10)
}`, 1, 2, 3, 4, 100, 30, 100, 20, 10, 10)
dockerStat := &docker.Stats{}
json.Unmarshal([]byte(jsonStat), dockerStat)
containerStats, err := dockerStatsToContainerStats(dockerStat)
Expand Down
6 changes: 3 additions & 3 deletions agent/stats/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
// dockerStatsToContainerStats returns a new object of the ContainerStats object from docker stats.
func dockerStatsToContainerStats(dockerStats *docker.Stats) (*ContainerStats, error) {
// The length of PercpuUsage represents the number of cores in an instance.
if len(dockerStats.CPUStats.CPUUsage.PercpuUsage) == 0 {
seelog.Debug("Invalid container statistics reported, invalid stats payload from docker")
return nil, fmt.Errorf("Invalid container statistics reported")
if len(dockerStats.CPUStats.CPUUsage.PercpuUsage) == 0 || numCores == uint64(0) {
seelog.Debug("Invalid container statistics reported, no cpu core usage reported")
return nil, fmt.Errorf("Invalid container statistics reported, no cpu core usage reported")
}

cpuUsage := dockerStats.CPUStats.CPUUsage.TotalUsage / numCores
Expand Down
42 changes: 42 additions & 0 deletions agent/stats/utils_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// +build !windows,!integration

// Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package stats

import (
"encoding/json"
"fmt"
"testing"

docker "github.com/fsouza/go-dockerclient"
)

func TestDockerStatsToContainerStatsZeroCoresGeneratesError(t *testing.T) {
// doing this with json makes me sad, but is the easiest way to deal with
// the inner structs
jsonStat := fmt.Sprintf(`
{
"cpu_stats":{
"cpu_usage":{
"total_usage":%d
}
}
}`, 100)
dockerStat := &docker.Stats{}
json.Unmarshal([]byte(jsonStat), dockerStat)
_, err := dockerStatsToContainerStats(dockerStat)
if err == nil {
t.Error("Expected error converting container stats with empty PercpuUsage")
}
}
4 changes: 2 additions & 2 deletions agent/stats/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
// dockerStatsToContainerStats returns a new object of the ContainerStats object from docker stats.
func dockerStatsToContainerStats(dockerStats *docker.Stats) (*ContainerStats, error) {
if numCores == uint64(0) {
seelog.Error("Invalid number of cpu cores")
return nil, fmt.Errorf("Invalid number of cpu cores")
seelog.Error("Invalid number of cpu cores acquired from the system")
return nil, fmt.Errorf("invalid number of cpu cores acquired from the system")
}

cpuUsage := dockerStats.CPUStats.CPUUsage.TotalUsage / numCores
Expand Down
41 changes: 41 additions & 0 deletions agent/stats/utils_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// +build windows,!integration

// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package stats

import (
"encoding/json"
"fmt"
"testing"

docker "github.com/fsouza/go-dockerclient"
)

func TestDockerStatsToContainerStatsZeroCoresGeneratesError(t *testing.T) {
numCores = uint64(0)
jsonStat := fmt.Sprintf(`
{
"cpu_stats":{
"cpu_usage":{
"total_usage":%d
}
}
}`, 100)
dockerStat := &docker.Stats{}
json.Unmarshal([]byte(jsonStat), dockerStat)
_, err := dockerStatsToContainerStats(dockerStat)
if err == nil {
t.Error("Expected error converting container stats with zero cpu cores")
}
}

0 comments on commit c1827c5

Please sign in to comment.