Skip to content

Commit bcd591f

Browse files
author
Peng Yin
committed
Fix test after enabled metrics on windows
1 parent cfd4cfe commit bcd591f

8 files changed

+94
-89
lines changed

agent/app/agent_windows_test.go

-60
Original file line numberDiff line numberDiff line change
@@ -21,74 +21,14 @@ import (
2121
"testing"
2222
"time"
2323

24-
app_mocks "github.com/aws/amazon-ecs-agent/agent/app/mocks"
2524
"github.com/aws/amazon-ecs-agent/agent/engine"
26-
"github.com/aws/amazon-ecs-agent/agent/eventstream"
2725
"github.com/aws/amazon-ecs-agent/agent/sighandlers"
28-
"github.com/aws/amazon-ecs-agent/agent/statemanager"
2926
statemanager_mocks "github.com/aws/amazon-ecs-agent/agent/statemanager/mocks"
30-
"github.com/aws/aws-sdk-go/aws/credentials"
3127
"github.com/golang/mock/gomock"
3228
"github.com/stretchr/testify/assert"
3329
"golang.org/x/sys/windows/svc"
3430
)
3531

36-
// TestDoStartHappyPath tests the doStart method for windows. This method should
37-
// go away when we support metrics for windows containers
38-
func TestDoStartHappyPath(t *testing.T) {
39-
ctrl, credentialsManager, state, imageManager, client,
40-
dockerClient, _, _ := setup(t)
41-
defer ctrl.Finish()
42-
43-
mockCredentialsProvider := app_mocks.NewMockProvider(ctrl)
44-
45-
var discoverEndpointsInvoked sync.WaitGroup
46-
discoverEndpointsInvoked.Add(1)
47-
containerChangeEvents := make(chan engine.DockerContainerChangeEvent)
48-
49-
// These calls are expected to happen, but cannot be ordered as they are
50-
// invoked via go routines, which will lead to occasional test failues
51-
dockerClient.EXPECT().Version().AnyTimes()
52-
imageManager.EXPECT().StartImageCleanupProcess(gomock.Any()).MaxTimes(1)
53-
mockCredentialsProvider.EXPECT().IsExpired().Return(false).AnyTimes()
54-
client.EXPECT().DiscoverPollEndpoint(gomock.Any()).Do(func(x interface{}) {
55-
// Ensures that the test waits until acs session has bee started
56-
discoverEndpointsInvoked.Done()
57-
}).Return("poll-endpoint", nil)
58-
client.EXPECT().DiscoverPollEndpoint(gomock.Any()).Return("acs-endpoint", nil).AnyTimes()
59-
60-
gomock.InOrder(
61-
mockCredentialsProvider.EXPECT().Retrieve().Return(credentials.Value{}, nil),
62-
dockerClient.EXPECT().SupportedVersions().Return(nil),
63-
dockerClient.EXPECT().KnownVersions().Return(nil),
64-
client.EXPECT().RegisterContainerInstance(gomock.Any(), gomock.Any()).Return("arn", nil),
65-
imageManager.EXPECT().SetSaver(gomock.Any()),
66-
dockerClient.EXPECT().ContainerEvents(gomock.Any()).Return(containerChangeEvents, nil),
67-
state.EXPECT().AllImageStates().Return(nil),
68-
state.EXPECT().AllTasks().Return(nil),
69-
)
70-
71-
cfg := getTestConfig()
72-
ctx, cancel := context.WithCancel(context.TODO())
73-
// Cancel the context to cancel async routines
74-
defer cancel()
75-
agent := &ecsAgent{
76-
ctx: ctx,
77-
cfg: &cfg,
78-
credentialProvider: credentials.NewCredentials(mockCredentialsProvider),
79-
dockerClient: dockerClient,
80-
terminationHandler: func(saver statemanager.Saver, taskEngine engine.TaskEngine) {},
81-
}
82-
83-
go agent.doStart(eventstream.NewEventStream("events", ctx),
84-
credentialsManager, state, imageManager, client)
85-
86-
// Wait for both DiscoverPollEndpointInput and DiscoverTelemetryEndpoint to be
87-
// invoked. These are used as proxies to indicate that acs and tcs handlers'
88-
// NewSession call has been invoked
89-
discoverEndpointsInvoked.Wait()
90-
}
91-
9232
type mockAgent struct {
9333
startFunc func() int
9434
terminationHandler sighandlers.TerminationHandler

agent/config/config_windows_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestConfigDefault(t *testing.T) {
3434

3535
assert.Equal(t, "npipe:////./pipe/docker_engine", cfg.DockerEndpoint, "Default docker endpoint set incorrectly")
3636
assert.Equal(t, `C:\ProgramData\Amazon\ECS\data`, cfg.DataDir, "Default datadir set incorrectly")
37-
assert.True(t, cfg.DisableMetrics, "Default disablemetrics set incorrectly")
37+
assert.False(t, cfg.DisableMetrics, "Default disablemetrics set incorrectly")
3838
assert.Equal(t, 10, len(cfg.ReservedPorts), "Default reserved ports set incorrectly")
3939
assert.Equal(t, uint16(0), cfg.ReservedMemory, "Default reserved memory set incorrectly")
4040
assert.Equal(t, 30*time.Second, cfg.DockerStopTimeout, "Default docker stop container timeout set incorrectly")

agent/stats/container_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ func TestContainerStatsCollection(t *testing.T) {
6161
// deal with the docker.Stats.MemoryStats inner struct
6262
jsonStat := fmt.Sprintf(`
6363
{
64-
"memory_stats": {"usage":%d},
64+
"memory_stats": {"usage":%d, "privateworkingset":%d},
6565
"cpu_stats":{
6666
"cpu_usage":{
6767
"percpu_usage":[%d],
6868
"total_usage":%d
6969
}
7070
}
71-
}`, stat.memBytes, stat.cpuTime, stat.cpuTime)
71+
}`, stat.memBytes, stat.memBytes, stat.cpuTime, stat.cpuTime)
7272
dockerStat := &docker.Stats{}
7373
json.Unmarshal([]byte(jsonStat), dockerStat)
7474
dockerStat.Read = stat.timestamp

agent/stats/utils_test.go

+3-21
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,6 @@ func TestDockerStatsToContainerStatsCpuUsage(t *testing.T) {
6666
}
6767
}
6868

69-
func TestDockerStatsToContainerStatsZeroCoresGeneratesError(t *testing.T) {
70-
// doing this with json makes me sad, but is the easiest way to deal with
71-
// the inner structs
72-
jsonStat := fmt.Sprintf(`
73-
{
74-
"cpu_stats":{
75-
"cpu_usage":{
76-
"total_usage":%d
77-
}
78-
}
79-
}`, 100)
80-
dockerStat := &docker.Stats{}
81-
json.Unmarshal([]byte(jsonStat), dockerStat)
82-
_, err := dockerStatsToContainerStats(dockerStat)
83-
if err == nil {
84-
t.Error("Expected error converting container stats with empty PercpuUsage")
85-
}
86-
}
87-
8869
func TestDockerStatsToContainerStatsMemUsage(t *testing.T) {
8970
jsonStat := fmt.Sprintf(`
9071
{
@@ -100,9 +81,10 @@ func TestDockerStatsToContainerStatsMemUsage(t *testing.T) {
10081
"stats": {
10182
"cache": %d,
10283
"rss": %d
103-
}
84+
},
85+
"privateworkingset": %d
10486
}
105-
}`, 1, 2, 3, 4, 100, 30, 100, 20, 10)
87+
}`, 1, 2, 3, 4, 100, 30, 100, 20, 10, 10)
10688
dockerStat := &docker.Stats{}
10789
json.Unmarshal([]byte(jsonStat), dockerStat)
10890
containerStats, err := dockerStatsToContainerStats(dockerStat)

agent/stats/utils_unix.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
// dockerStatsToContainerStats returns a new object of the ContainerStats object from docker stats.
2525
func dockerStatsToContainerStats(dockerStats *docker.Stats) (*ContainerStats, error) {
2626
// The length of PercpuUsage represents the number of cores in an instance.
27-
if len(dockerStats.CPUStats.CPUUsage.PercpuUsage) == 0 {
28-
seelog.Debug("Invalid container statistics reported, invalid stats payload from docker")
29-
return nil, fmt.Errorf("Invalid container statistics reported")
27+
if len(dockerStats.CPUStats.CPUUsage.PercpuUsage) == 0 || numCores == uint64(0) {
28+
seelog.Debug("Invalid container statistics reported, no cpu core usage reported")
29+
return nil, fmt.Errorf("Invalid container statistics reported, no cpu core usage reported")
3030
}
3131

3232
cpuUsage := dockerStats.CPUStats.CPUUsage.TotalUsage / numCores

agent/stats/utils_unix_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// +build !windows,!integration
2+
3+
// Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
// not use this file except in compliance with the License. A copy of the
7+
// License is located at
8+
//
9+
// http://aws.amazon.com/apache2.0/
10+
//
11+
// or in the "license" file accompanying this file. This file is distributed
12+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
// express or implied. See the License for the specific language governing
14+
// permissions and limitations under the License.
15+
package stats
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
"testing"
21+
22+
docker "github.com/fsouza/go-dockerclient"
23+
)
24+
25+
func TestDockerStatsToContainerStatsZeroCoresGeneratesError(t *testing.T) {
26+
// doing this with json makes me sad, but is the easiest way to deal with
27+
// the inner structs
28+
jsonStat := fmt.Sprintf(`
29+
{
30+
"cpu_stats":{
31+
"cpu_usage":{
32+
"total_usage":%d
33+
}
34+
}
35+
}`, 100)
36+
dockerStat := &docker.Stats{}
37+
json.Unmarshal([]byte(jsonStat), dockerStat)
38+
_, err := dockerStatsToContainerStats(dockerStat)
39+
if err == nil {
40+
t.Error("Expected error converting container stats with empty PercpuUsage")
41+
}
42+
}

agent/stats/utils_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
// dockerStatsToContainerStats returns a new object of the ContainerStats object from docker stats.
2525
func dockerStatsToContainerStats(dockerStats *docker.Stats) (*ContainerStats, error) {
2626
if numCores == uint64(0) {
27-
seelog.Error("Invalid number of cpu cores")
28-
return nil, fmt.Errorf("Invalid number of cpu cores")
27+
seelog.Error("Invalid number of cpu cores acquired from the system")
28+
return nil, fmt.Errorf("invalid number of cpu cores acquired from the system")
2929
}
3030

3131
cpuUsage := dockerStats.CPUStats.CPUUsage.TotalUsage / numCores

agent/stats/utils_windows_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// +build windows,!integration
2+
3+
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
// not use this file except in compliance with the License. A copy of the
7+
// License is located at
8+
//
9+
// http://aws.amazon.com/apache2.0/
10+
//
11+
// or in the "license" file accompanying this file. This file is distributed
12+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
// express or implied. See the License for the specific language governing
14+
// permissions and limitations under the License.
15+
package stats
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
"testing"
21+
22+
docker "github.com/fsouza/go-dockerclient"
23+
)
24+
25+
func TestDockerStatsToContainerStatsZeroCoresGeneratesError(t *testing.T) {
26+
numCores = uint64(0)
27+
jsonStat := fmt.Sprintf(`
28+
{
29+
"cpu_stats":{
30+
"cpu_usage":{
31+
"total_usage":%d
32+
}
33+
}
34+
}`, 100)
35+
dockerStat := &docker.Stats{}
36+
json.Unmarshal([]byte(jsonStat), dockerStat)
37+
_, err := dockerStatsToContainerStats(dockerStat)
38+
if err == nil {
39+
t.Error("Expected error converting container stats with zero cpu cores")
40+
}
41+
}

0 commit comments

Comments
 (0)