From 4c37d0ad30e1ed6067d164f40841414722c7c2db Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Fri, 14 Jul 2023 15:45:09 -0700 Subject: [PATCH 1/2] Add some unit tests for config parsers where coverage was missing before: % go test -cover -tags unit ./config/. ok github.com/aws/amazon-ecs-agent/agent/config 0.460s coverage: 91.0% of statements after: % go test -cover -tags unit ./config/. ok github.com/aws/amazon-ecs-agent/agent/config 0.480s coverage: 92.6% of statements --- agent/config/parse_test.go | 116 +++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 agent/config/parse_test.go diff --git a/agent/config/parse_test.go b/agent/config/parse_test.go new file mode 100644 index 00000000000..4860e84b39b --- /dev/null +++ b/agent/config/parse_test.go @@ -0,0 +1,116 @@ +//go:build unit +// +build unit + +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseReservedPorts(t *testing.T) { + envVar := "ECS_RESERVED_PORTS" + // unset value + v := parseReservedPorts(envVar) + assert.Empty(t, v) + // invalid value + t.Setenv(envVar, "1,2,3") + v = parseReservedPorts(envVar) + assert.Empty(t, v) + // valid value + t.Setenv(envVar, "[1,2,3]") + v = parseReservedPorts(envVar) + assert.Equal(t, []uint16{1, 2, 3}, v) + // number too large + t.Setenv(envVar, "[1,2,3,99999999999]") + v = parseReservedPorts(envVar) + assert.Equal(t, []uint16{1, 2, 3, 0}, v) +} + +func TestParseVolumePluginCapabilities(t *testing.T) { + // unset value + t.Setenv("ECS_VOLUME_PLUGIN_CAPABILITIES", "") + v := parseVolumePluginCapabilities() + assert.Empty(t, v) + // invalid value + t.Setenv("ECS_VOLUME_PLUGIN_CAPABILITIES", "1,2,3") + v = parseVolumePluginCapabilities() + assert.Empty(t, v) + // valid value + t.Setenv("ECS_VOLUME_PLUGIN_CAPABILITIES", "[1,2,3]") + v = parseVolumePluginCapabilities() + assert.Equal(t, []string{"", "", ""}, v) + // valid values + t.Setenv("ECS_VOLUME_PLUGIN_CAPABILITIES", `["cap1","cap2"]`) + v = parseVolumePluginCapabilities() + assert.Equal(t, []string{"cap1", "cap2"}, v) +} + +func TestParseNumImagesToDeletePerCycle(t *testing.T) { + // unset value + t.Setenv("ECS_NUM_IMAGES_DELETE_PER_CYCLE", "") + v := parseNumImagesToDeletePerCycle() + assert.Zero(t, v) + // invalid value + t.Setenv("ECS_NUM_IMAGES_DELETE_PER_CYCLE", "1") + v = parseNumImagesToDeletePerCycle() + assert.Equal(t, 1, v) + // valid value + t.Setenv("ECS_NUM_IMAGES_DELETE_PER_CYCLE", "2000000") + v = parseNumImagesToDeletePerCycle() + assert.Equal(t, 2000000, v) + // valid values + t.Setenv("ECS_NUM_IMAGES_DELETE_PER_CYCLE", "foobar") + v = parseNumImagesToDeletePerCycle() + assert.Zero(t, v) +} + +func TestParseNumNonECSContainersToDeletePerCycle(t *testing.T) { + // unset value + t.Setenv("NONECS_NUM_CONTAINERS_DELETE_PER_CYCLE", "") + v := parseNumNonECSContainersToDeletePerCycle() + assert.Zero(t, v) + // invalid value + t.Setenv("NONECS_NUM_CONTAINERS_DELETE_PER_CYCLE", "1") + v = parseNumNonECSContainersToDeletePerCycle() + assert.Equal(t, 1, v) + // valid value + t.Setenv("NONECS_NUM_CONTAINERS_DELETE_PER_CYCLE", "2000000") + v = parseNumNonECSContainersToDeletePerCycle() + assert.Equal(t, 2000000, v) + // valid values + t.Setenv("NONECS_NUM_CONTAINERS_DELETE_PER_CYCLE", "foobar") + v = parseNumNonECSContainersToDeletePerCycle() + assert.Zero(t, v) +} + +func TestParseBooleanDefaultFalseConfig(t *testing.T) { + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_FALSE", "") + v := parseBooleanDefaultFalseConfig("ECS_PARSE_BOOLEAN_DEFAULT_FALSE") + assert.False(t, v.Enabled()) + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_FALSE", "true") + v = parseBooleanDefaultFalseConfig("ECS_PARSE_BOOLEAN_DEFAULT_FALSE") + assert.True(t, v.Enabled()) + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_FALSE", "false") + v = parseBooleanDefaultFalseConfig("ECS_PARSE_BOOLEAN_DEFAULT_FALSE") + assert.False(t, v.Enabled()) + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_FALSE", "foobar") + v = parseBooleanDefaultFalseConfig("ECS_PARSE_BOOLEAN_DEFAULT_FALSE") + assert.False(t, v.Enabled()) +} + +func TestParseBooleanDefaultTrueConfig(t *testing.T) { + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_TRUE", "") + v := parseBooleanDefaultTrueConfig("ECS_PARSE_BOOLEAN_DEFAULT_TRUE") + assert.True(t, v.Enabled()) + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_TRUE", "true") + v = parseBooleanDefaultTrueConfig("ECS_PARSE_BOOLEAN_DEFAULT_TRUE") + assert.True(t, v.Enabled()) + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_TRUE", "false") + v = parseBooleanDefaultTrueConfig("ECS_PARSE_BOOLEAN_DEFAULT_TRUE") + assert.False(t, v.Enabled()) + t.Setenv("ECS_PARSE_BOOLEAN_DEFAULT_TRUE", "foobar") + v = parseBooleanDefaultTrueConfig("ECS_PARSE_BOOLEAN_DEFAULT_TRUE") + assert.True(t, v.Enabled()) +} From c8b3847777b1715f38571ec83e794d78ae0d639d Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 18 Jul 2023 10:34:48 -0700 Subject: [PATCH 2/2] Add unit test tag to all unit test files --- agent/api/task/task_attachment_handler_test.go | 3 +++ agent/app/healthcheck_test.go | 3 +++ agent/doctor/docker_runtime_healthcheck_test.go | 3 +++ agent/engine/common_test.go | 3 +++ agent/engine/daemonmanager/daemon_manager_linux_test.go | 3 +++ agent/engine/execcmd/agent_version_test.go | 3 +++ agent/engine/execcmd/manager_test.go | 3 +++ agent/handlers/agentapi/taskprotection/factory_test.go | 3 +++ agent/stats/common_test.go | 3 +++ agent/stats/reporter/reporter_test.go | 3 +++ agent/taskresource/credentialspec/credentialspecstatus_test.go | 3 +++ agent/taskresource/volume/dockervolume_efs_test.go | 3 +++ agent/taskresource/volume/mountoptions_test.go | 3 +++ agent/utils/ephemeral_ports_test.go | 3 +++ 14 files changed, 42 insertions(+) diff --git a/agent/api/task/task_attachment_handler_test.go b/agent/api/task/task_attachment_handler_test.go index 137a58f7c15..15e9e8c8bed 100644 --- a/agent/api/task/task_attachment_handler_test.go +++ b/agent/api/task/task_attachment_handler_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/app/healthcheck_test.go b/agent/app/healthcheck_test.go index 7000bcd96c1..446b06ca0c3 100644 --- a/agent/app/healthcheck_test.go +++ b/agent/app/healthcheck_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/doctor/docker_runtime_healthcheck_test.go b/agent/doctor/docker_runtime_healthcheck_test.go index 633c187ee8e..b4ad49e5b9c 100644 --- a/agent/doctor/docker_runtime_healthcheck_test.go +++ b/agent/doctor/docker_runtime_healthcheck_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + package doctor import ( diff --git a/agent/engine/common_test.go b/agent/engine/common_test.go index ca0c5a16d6b..2bff7f017ed 100644 --- a/agent/engine/common_test.go +++ b/agent/engine/common_test.go @@ -1,3 +1,6 @@ +//go:build unit || integration || sudo +// +build unit integration sudo + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/engine/daemonmanager/daemon_manager_linux_test.go b/agent/engine/daemonmanager/daemon_manager_linux_test.go index e2307dd6dad..b920570d207 100644 --- a/agent/engine/daemonmanager/daemon_manager_linux_test.go +++ b/agent/engine/daemonmanager/daemon_manager_linux_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/engine/execcmd/agent_version_test.go b/agent/engine/execcmd/agent_version_test.go index b65b6ee5fce..18314656bbd 100644 --- a/agent/engine/execcmd/agent_version_test.go +++ b/agent/engine/execcmd/agent_version_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/engine/execcmd/manager_test.go b/agent/engine/execcmd/manager_test.go index 3d6c81f2b38..a7e04ab07c4 100644 --- a/agent/engine/execcmd/manager_test.go +++ b/agent/engine/execcmd/manager_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/handlers/agentapi/taskprotection/factory_test.go b/agent/handlers/agentapi/taskprotection/factory_test.go index 379d385b1b0..122622729d7 100644 --- a/agent/handlers/agentapi/taskprotection/factory_test.go +++ b/agent/handlers/agentapi/taskprotection/factory_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/stats/common_test.go b/agent/stats/common_test.go index 0c5fc33a1c1..1805ca5d996 100644 --- a/agent/stats/common_test.go +++ b/agent/stats/common_test.go @@ -1,3 +1,6 @@ +//go:build unit || sudo || integration +// +build unit sudo integration + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/stats/reporter/reporter_test.go b/agent/stats/reporter/reporter_test.go index f7088adf45f..96119f84945 100644 --- a/agent/stats/reporter/reporter_test.go +++ b/agent/stats/reporter/reporter_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/taskresource/credentialspec/credentialspecstatus_test.go b/agent/taskresource/credentialspec/credentialspecstatus_test.go index 7c01877ee42..66d13f8819a 100644 --- a/agent/taskresource/credentialspec/credentialspecstatus_test.go +++ b/agent/taskresource/credentialspec/credentialspecstatus_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/taskresource/volume/dockervolume_efs_test.go b/agent/taskresource/volume/dockervolume_efs_test.go index 984288610d5..d25607c235c 100644 --- a/agent/taskresource/volume/dockervolume_efs_test.go +++ b/agent/taskresource/volume/dockervolume_efs_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/taskresource/volume/mountoptions_test.go b/agent/taskresource/volume/mountoptions_test.go index 34a538cae05..ebe31a99eb9 100644 --- a/agent/taskresource/volume/mountoptions_test.go +++ b/agent/taskresource/volume/mountoptions_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may diff --git a/agent/utils/ephemeral_ports_test.go b/agent/utils/ephemeral_ports_test.go index 4972d2744a8..00ce729cefa 100644 --- a/agent/utils/ephemeral_ports_test.go +++ b/agent/utils/ephemeral_ports_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + package utils // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.