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

Add some unit tests for config parsers where coverage was missing #3809

Merged
merged 2 commits into from
Jul 20, 2023
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
3 changes: 3 additions & 0 deletions agent/api/task/task_attachment_handler_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/app/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
116 changes: 116 additions & 0 deletions agent/config/parse_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
3 changes: 3 additions & 0 deletions agent/doctor/docker_runtime_healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build unit
// +build unit

package doctor

import (
Expand Down
3 changes: 3 additions & 0 deletions agent/engine/common_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/engine/daemonmanager/daemon_manager_linux_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/engine/execcmd/agent_version_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/engine/execcmd/manager_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/handlers/agentapi/taskprotection/factory_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/stats/common_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/stats/reporter/reporter_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/taskresource/volume/dockervolume_efs_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/taskresource/volume/mountoptions_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/utils/ephemeral_ports_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build unit
// +build unit

package utils

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
Expand Down