diff --git a/agent/functional_tests/tests/functionaltests_test.go b/agent/functional_tests/tests/functionaltests_test.go index c22e25a43db..c1e893de2b6 100644 --- a/agent/functional_tests/tests/functionaltests_test.go +++ b/agent/functional_tests/tests/functionaltests_test.go @@ -453,13 +453,10 @@ 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 { @@ -467,6 +464,16 @@ func telemetryTest(t *testing.T, taskDefinition string) { } 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{ @@ -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) @@ -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{ @@ -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") @@ -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) diff --git a/agent/functional_tests/util/utils.go b/agent/functional_tests/util/utils.go index 31cff08699b..c162766c8e0 100644 --- a/agent/functional_tests/util/utils.go +++ b/agent/functional_tests/util/utils.go @@ -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 ( @@ -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) @@ -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 { @@ -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