Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing telemetry polling test #1755

Merged
merged 1 commit into from
Dec 24, 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: 19 additions & 10 deletions agent/functional_tests/tests/functionaltests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,27 @@ func containerHealthWithStartPeriodTest(t *testing.T, taskDefinition string) {
containerHealthMetricsTest(t, taskDefinition, tdOverrides)
}

func telemetryTest(t *testing.T, taskDefinition string) {
// telemetry task requires 2GB of memory (for either linux or windows); requires a bit more to be stable
RequireMinimumMemory(t, 2200)

func calculateCpuLimits(cpuPercentage float64) (int, float64) {
cpuNum := runtime.NumCPU()
// Try to let the container use 25% cpu, but bound it within valid range
cpuShare := int(float64(cpuNum*cpuSharesPerCore) * 0.25)
cpuShare := int(float64(cpuNum*cpuSharesPerCore) * cpuPercentage)
if cpuShare < minimumCPUShares {
cpuShare = minimumCPUShares
} else if cpuShare > maximumCPUShares {
cpuShare = maximumCPUShares
}
expectedCPUPercentage := float64(cpuShare) / float64(cpuNum*cpuSharesPerCore)

return cpuShare, expectedCPUPercentage
}

func telemetryTest(t *testing.T, taskDefinition string) {
// telemetry task requires 2GB of memory (for either linux or windows); requires a bit more to be stable
RequireMinimumMemory(t, 2200)

// Try to let the container use 25% cpu, but bound it within valid range
cpuShare, expectedCPUPercentage := calculateCpuLimits(0.25)

// Try to use a new cluster for this test, ensure no other task metrics for this cluster
newClusterName := "ecstest-telemetry-" + uuid.New()
_, err := ECS.CreateCluster(&ecsapi.CreateClusterInput{
Expand Down Expand Up @@ -527,7 +534,7 @@ func telemetryTest(t *testing.T, taskDefinition string) {
metrics, err := VerifyMetrics(cwclient, params, false)
assert.NoError(t, err, "Task is running, verify metrics for CPU utilization failed")
// Also verify the cpu usage is around expectedCPUPercentage +/- 5%
assert.InDelta(t, expectedCPUPercentage * 100.0, *metrics.Average, 5)
assert.InDelta(t, expectedCPUPercentage*100.0, *metrics.Average, 5)

params.MetricName = aws.String("MemoryUtilization")
metrics, err = VerifyMetrics(cwclient, params, false)
Expand Down Expand Up @@ -557,6 +564,9 @@ func telemetryTest(t *testing.T, taskDefinition string) {
}

func telemetryTestWithStatsPolling(t *testing.T, taskDefinition string) {
// Try to let the container use 25% cpu, but bound it within valid range
cpuShare, expectedCPUPercentage := calculateCpuLimits(0.25)

// Try to use a new cluster for this test, ensure no other task metrics for this cluster
newClusterName := "ecstest-telemetry-polling-" + uuid.New()
_, err := ECS.CreateCluster(&ecsapi.CreateClusterInput{
Expand Down Expand Up @@ -593,11 +603,10 @@ func telemetryTestWithStatsPolling(t *testing.T, taskDefinition string) {
}

cwclient := cloudwatch.New(session.New(), aws.NewConfig().WithRegion(*ECS.Config.Region))
cpuNum := runtime.NumCPU()

tdOverrides := make(map[string]string)
// Set the container cpu percentage 25%
tdOverrides["$$$$CPUSHARE$$$$"] = strconv.Itoa(int(float64(cpuNum*cpuSharesPerCore) * 0.25))
tdOverrides["$$$$CPUSHARE$$$$"] = strconv.Itoa(cpuShare)

testTask, err := agent.StartTaskWithTaskDefinitionOverrides(t, taskDefinition, tdOverrides)
require.NoError(t, err, "Failed to start telemetry task")
Expand All @@ -611,8 +620,8 @@ func telemetryTestWithStatsPolling(t *testing.T, taskDefinition string) {
params.MetricName = aws.String("CPUUtilization")
metrics, err := VerifyMetrics(cwclient, params, false)
assert.NoError(t, err, "Task is running, verify metrics for CPU utilization failed")
// Also verify the cpu usage is around 25% +/- 5%
assert.InDelta(t, 25, *metrics.Average, 5)
// Also verify the cpu usage is around expectedCPUPercentage +/- 5%
assert.InDelta(t, expectedCPUPercentage*100.0, *metrics.Average, 5)

params.MetricName = aws.String("MemoryUtilization")
metrics, err = VerifyMetrics(cwclient, params, false)
Expand Down
30 changes: 11 additions & 19 deletions agent/functional_tests/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/docker/docker/pkg/system"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -602,19 +603,14 @@ func (task *TestTask) GetAttachmentInfo() ([]*ecs.KeyValuePair, error) {
func RequireDockerVersion(t *testing.T, selector string) {
ctx := context.TODO()
dockerClient, err := docker.NewClientWithOpts(docker.WithVersion(sdkclientfactory.GetDefaultVersion().String()))
if err != nil {
t.Fatalf("Could not get docker client to check version: %v", err)
}
require.NoError(t, err, "Could not get docker client to check version")

version, err := dockerClient.ServerVersion(ctx)
if err != nil {
t.Fatalf("Could not get docker version: %v", err)
}
dockerVersion := version.Version
require.NoError(t, err, "Could not get docker version")

dockerVersion := version.Version
match, err := utils.Version(dockerVersion).Matches(selector)
if err != nil {
t.Fatalf("Could not check docker version to match required: %v", err)
}
require.NoError(t, err, "Could not check docker version to match required")

if !match {
t.Skipf("Skipping test; requires %v, but version is %v", selector, dockerVersion)
Expand All @@ -623,9 +619,7 @@ func RequireDockerVersion(t *testing.T, selector string) {

func RequireMinimumMemory(t *testing.T, minimumMemoryInMegaBytes int) {
memInfo, err := system.ReadMemInfo()
if err != nil {
t.Fatalf("Could not check system memory info before checking minimum memory requirement: %v", err)
}
require.NoError(t, err, "Could not check system memory info before checking minimum memory requirement")

totalMemory := int(memInfo.MemTotal / bytePerMegabyte)
if totalMemory < minimumMemoryInMegaBytes {
Expand All @@ -636,13 +630,11 @@ func RequireMinimumMemory(t *testing.T, minimumMemoryInMegaBytes int) {
func RequireDockerAPIVersion(t *testing.T, selector string) {
ctx := context.TODO()
dockerClient, err := docker.NewClientWithOpts(docker.WithVersion(sdkclientfactory.GetDefaultVersion().String()))
if err != nil {
t.Fatalf("Could not get docker client to check version: %v", err)
}
require.NoError(t, err, "Could not get docker client to check version")

version, err := dockerClient.ServerVersion(ctx)
if err != nil {
t.Fatalf("Could not get docker version: %v", err)
}
require.NoError(t, err, "Could not get docker version")

apiVersion := version.APIVersion
// adding zero patch to use semver comparator
// TODO: Implement non-semver comparator
Expand Down