Skip to content

Commit

Permalink
Move utils function Uint16SliceToStringSlice to shared lib
Browse files Browse the repository at this point in the history
  • Loading branch information
timj-hh committed Oct 24, 2023
1 parent 06931a5 commit 66a4352
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 21 deletions.
5 changes: 3 additions & 2 deletions agent/api/ecsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/httpclient"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
commonutils "github.com/aws/amazon-ecs-agent/ecs-agent/utils"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -322,12 +323,12 @@ func (client *APIECSClient) getResources() ([]*ecs.Resource, error) {
portResource := ecs.Resource{
Name: utils.Strptr("PORTS"),
Type: utils.Strptr("STRINGSET"),
StringSetValue: utils.Uint16SliceToStringSlice(client.config.ReservedPorts),
StringSetValue: commonutils.Uint16SliceToStringSlice(client.config.ReservedPorts),
}
udpPortResource := ecs.Resource{
Name: utils.Strptr("PORTS_UDP"),
Type: utils.Strptr("STRINGSET"),
StringSetValue: utils.Uint16SliceToStringSlice(client.config.ReservedPortsUDP),
StringSetValue: commonutils.Uint16SliceToStringSlice(client.config.ReservedPortsUDP),
}

return []*ecs.Resource{&cpuResource, &memResource, &portResource, &udpPortResource}, nil
Expand Down
5 changes: 3 additions & 2 deletions agent/api/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/field"
nlappmesh "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/appmesh"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
commonutils "github.com/aws/amazon-ecs-agent/ecs-agent/utils"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime"

Expand Down Expand Up @@ -3649,12 +3650,12 @@ func (task *Task) ToHostResources() map[string]*ecs.Resource {
resources["PORTS_TCP"] = &ecs.Resource{
Name: utils.Strptr("PORTS_TCP"),
Type: utils.Strptr("STRINGSET"),
StringSetValue: utils.Uint16SliceToStringSlice(tcpPortSet),
StringSetValue: commonutils.Uint16SliceToStringSlice(tcpPortSet),
}
resources["PORTS_UDP"] = &ecs.Resource{
Name: utils.Strptr("PORTS_UDP"),
Type: utils.Strptr("STRINGSET"),
StringSetValue: utils.Uint16SliceToStringSlice(udpPortSet),
StringSetValue: commonutils.Uint16SliceToStringSlice(udpPortSet),
}

// GPU
Expand Down
3 changes: 2 additions & 1 deletion agent/api/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
mock_credentials "github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
commonutils "github.com/aws/amazon-ecs-agent/ecs-agent/utils"
"github.com/aws/aws-sdk-go/service/secretsmanager"

"github.com/aws/amazon-ecs-agent/agent/taskresource/asmsecret"
Expand Down Expand Up @@ -5178,7 +5179,7 @@ func TestToHostResources(t *testing.T) {
},
{
task: testTask4,
expectedResources: getTestTaskResourceMap(int64(1024), int64(512), utils.Uint16SliceToStringSlice(portsTCP), utils.Uint16SliceToStringSlice(portsUDP), []*string{}),
expectedResources: getTestTaskResourceMap(int64(1024), int64(512), commonutils.Uint16SliceToStringSlice(portsTCP), commonutils.Uint16SliceToStringSlice(portsUDP), []*string{}),
},
{
task: testTask5,
Expand Down
7 changes: 4 additions & 3 deletions agent/engine/host_resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/aws/amazon-ecs-agent/agent/utils"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
commonutils "github.com/aws/amazon-ecs-agent/ecs-agent/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -231,7 +232,7 @@ func TestConsumable(t *testing.T) {
}

for _, tc := range testCases {
resources := getTestTaskResourceMap(tc.cpu, tc.mem, utils.Uint16SliceToStringSlice(tc.ports), utils.Uint16SliceToStringSlice(tc.portsUdp), aws.StringSlice(tc.gpus))
resources := getTestTaskResourceMap(tc.cpu, tc.mem, commonutils.Uint16SliceToStringSlice(tc.ports), commonutils.Uint16SliceToStringSlice(tc.portsUdp), aws.StringSlice(tc.gpus))
canBeConsumed, err := h.consumable(resources)
assert.Equal(t, canBeConsumed, tc.canBeConsumed, "Error in checking if resources can be successfully consumed")
assert.Equal(t, err, nil, "Error in checking if resources can be successfully consumed, error returned from consumable")
Expand All @@ -244,7 +245,7 @@ func TestResourceHealthTrue(t *testing.T) {
gpuIDs := []string{"gpu1", "gpu2", "gpu3", "gpu4"}
h := getTestHostResourceManager(int64(2048), int64(2048), []*string{&hostResourcePort1}, []*string{&hostResourcePort2}, aws.StringSlice(gpuIDs))

resources := getTestTaskResourceMap(1024, 1024, utils.Uint16SliceToStringSlice([]uint16{22}), utils.Uint16SliceToStringSlice([]uint16{1000}), aws.StringSlice([]string{"gpu1", "gpu2"}))
resources := getTestTaskResourceMap(1024, 1024, commonutils.Uint16SliceToStringSlice([]uint16{22}), commonutils.Uint16SliceToStringSlice([]uint16{1000}), aws.StringSlice([]string{"gpu1", "gpu2"}))
err := h.checkResourcesHealth(resources)
assert.NoError(t, err, "Error in checking healthy resource map status")
}
Expand All @@ -256,7 +257,7 @@ func TestResourceHealthGPUFalse(t *testing.T) {
gpuIDs := []string{"gpu1", "gpu2", "gpu3", "gpu4"}
h := getTestHostResourceManager(int64(2048), int64(2048), []*string{&hostResourcePort1}, []*string{&hostResourcePort2}, aws.StringSlice(gpuIDs))

resources := getTestTaskResourceMap(1024, 1024, utils.Uint16SliceToStringSlice([]uint16{22}), utils.Uint16SliceToStringSlice([]uint16{1000}), aws.StringSlice([]string{"gpu1", "gpu5"}))
resources := getTestTaskResourceMap(1024, 1024, commonutils.Uint16SliceToStringSlice([]uint16{22}), commonutils.Uint16SliceToStringSlice([]uint16{1000}), aws.StringSlice([]string{"gpu1", "gpu5"}))
err := h.checkResourcesHealth(resources)
assert.Error(t, err, "Error in checking unhealthy resource map status")
}
Expand Down
11 changes: 0 additions & 11 deletions agent/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ func BoolPtr(b bool) *bool {
return &b
}

// Uint16SliceToStringSlice converts a slice of type uint16 to a slice of type
// *string. It uses strconv.Itoa on each element
func Uint16SliceToStringSlice(slice []uint16) []*string {
stringSlice := make([]*string, len(slice))
for i, el := range slice {
str := strconv.Itoa(int(el))
stringSlice[i] = &str
}
return stringSlice
}

func StrSliceEqual(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion ecs-agent/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// permissions and limitations under the License.
package utils

import "reflect"
import (
"reflect"
"strconv"
)

func ZeroOrNil(obj interface{}) bool {
value := reflect.ValueOf(obj)
Expand All @@ -35,3 +38,14 @@ func ZeroOrNil(obj interface{}) bool {
}
return false
}

// Uint16SliceToStringSlice converts a slice of type uint16 to a slice of type
// *string. It uses strconv.Itoa on each element
func Uint16SliceToStringSlice(slice []uint16) []*string {
stringSlice := make([]*string, len(slice))
for i, el := range slice {
str := strconv.Itoa(int(el))
stringSlice[i] = &str
}
return stringSlice
}
30 changes: 30 additions & 0 deletions ecs-agent/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package utils

import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -59,3 +60,32 @@ func TestZeroOrNil(t *testing.T) {
}

}

// TestUint16SliceToStringSlice tests the utils method Uint16SliceToStringSlice
// By taking in a slice of untyped 16 bit ints, asserting the util function
// returns the correct size of array, and asserts their equality.
// This is done by re-converting the string into a uint16.
func TestUint16SliceToStringSlice(t *testing.T) {
testCases := []struct {
param []uint16
expected int
name string
}{
{nil, 0, "Nil argument"},
{[]uint16{0, 1, 2, 3}, 4, "Basic set"},
{[]uint16{65535, 65535}, 2, "Max Value"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output := Uint16SliceToStringSlice(tc.param)
assert.Equal(t, tc.expected, len(output), tc.name)
for idx, num := range tc.param {
reconverted, err := strconv.Atoi(*output[idx])
assert.NoError(t, err)
assert.Equal(t, num, uint16(reconverted))
}

})
}
}

0 comments on commit 66a4352

Please sign in to comment.