diff --git a/ecs-cli/modules/cli/cluster/cluster_app.go b/ecs-cli/modules/cli/cluster/cluster_app.go index 7029b0a09..f84a46b96 100644 --- a/ecs-cli/modules/cli/cluster/cluster_app.go +++ b/ecs-cli/modules/cli/cluster/cluster_app.go @@ -289,7 +289,7 @@ func createCluster(context *cli.Context, awsClients *AWSClients, commandConfig * tags := make([]*ecs.Tag, 0) if tagVal := context.String(flags.ResourceTagsFlag); tagVal != "" { - tags, err = utils.GetTags(tagVal, tags) + tags, err = utils.ParseTags(tagVal, tags) if err != nil { return err } @@ -409,7 +409,7 @@ func createEmptyCluster(context *cli.Context, ecsClient ecsclient.ECSClient, cfn tags := make([]*ecs.Tag, 0) var err error if tagVal := context.String(flags.ResourceTagsFlag); tagVal != "" { - tags, err = utils.GetTags(tagVal, tags) + tags, err = utils.ParseTags(tagVal, tags) if err != nil { return err } diff --git a/ecs-cli/modules/utils/utils.go b/ecs-cli/modules/utils/utils.go index 3b0c65615..8ad81f219 100644 --- a/ecs-cli/modules/utils/utils.go +++ b/ecs-cli/modules/utils/utils.go @@ -62,9 +62,9 @@ func EntityAlreadyExists(err error) bool { return false } -// GetTags parses AWS Resource tags from the flag value +// ParseTags parses AWS Resource tags from the flag value // users specify tags in this format: key1=value1,key2=value2,key3=value3 -func GetTags(flagValue string, tags []*ecs.Tag) ([]*ecs.Tag, error) { +func ParseTags(flagValue string, tags []*ecs.Tag) ([]*ecs.Tag, error) { keyValPairs := strings.Split(flagValue, ",") for _, kv := range keyValPairs { pair := strings.Split(kv, "=") diff --git a/ecs-cli/modules/utils/utils_test.go b/ecs-cli/modules/utils/utils_test.go index 3fc470535..0b903d516 100644 --- a/ecs-cli/modules/utils/utils_test.go +++ b/ecs-cli/modules/utils/utils_test.go @@ -18,6 +18,8 @@ import ( "os" "testing" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ecs" "github.com/stretchr/testify/assert" ) @@ -51,6 +53,54 @@ func TestGetHomeDirInWindows(t *testing.T) { assert.NoError(t, err, "Unexpected error getting home dir") } +func TestParseTags(t *testing.T) { + actualTags := make([]*ecs.Tag, 0) + expectedTags := []*ecs.Tag{ + &ecs.Tag{ + Key: aws.String("Pink"), + Value: aws.String("Floyd"), + }, + &ecs.Tag{ + Key: aws.String("Tame"), + Value: aws.String("Impala"), + }, + } + + var err error + actualTags, err = ParseTags("Pink=Floyd,Tame=Impala", actualTags) + assert.NoError(t, err, "Unexpected error calling ParseTags") + assert.ElementsMatch(t, actualTags, expectedTags, "Expected tags to match") + +} + +func TestParseTagsEmptyValue(t *testing.T) { + actualTags := make([]*ecs.Tag, 0) + expectedTags := []*ecs.Tag{ + &ecs.Tag{ + Key: aws.String("thecheese"), + Value: aws.String(""), + }, + &ecs.Tag{ + Key: aws.String("standsalone"), + Value: aws.String(""), + }, + } + + var err error + actualTags, err = ParseTags("thecheese=,standsalone=", actualTags) + assert.NoError(t, err, "Unexpected error calling ParseTags") + assert.ElementsMatch(t, actualTags, expectedTags, "Expected tags to match") + +} + +func TestParseTagInvalidFormat(t *testing.T) { + actualTags := make([]*ecs.Tag, 0) + + var err error + _, err = ParseTags("incorrectly=formatted,tags", actualTags) + assert.Error(t, err, "Expected error calling ParseTags") +} + func tempDir(t *testing.T) string { // Create a temprorary directory for the dummy ecs config tempDirName, err := ioutil.TempDir("", "test")