Skip to content

Commit

Permalink
Refactored utils.GetTags() => utils.ParseTags(); added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PettitWesley committed Feb 22, 2019
1 parent 8c3dc3d commit 212cc47
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ecs-cli/modules/cli/cluster/cluster_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions ecs-cli/modules/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "=")
Expand Down
50 changes: 50 additions & 0 deletions ecs-cli/modules/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 212cc47

Please sign in to comment.