Skip to content

Commit

Permalink
return count for number of tags and add a testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Jul 11, 2023
1 parent e9dd1e0 commit aadf946
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pkg/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func (tags *tagSet) set(key, value string, failOnExist bool) error {
return nil
}

func (tags tagSet) count() int {
return len(tags.tagMap)
}

func (tags tagSet) toMap() map[string]string {
m := make(map[string]string, len(tags.tagMap))
for key, value := range tags.tagMap {
Expand Down Expand Up @@ -279,6 +283,11 @@ func (tags *Tags) Set(key, value string) error {
return tags.TagSet.set(key, value, false)
}

// Count - return number of tags accounted for
func (tags Tags) Count() int {
return tags.TagSet.count()
}

// ToMap returns copy of tags.
func (tags Tags) ToMap() map[string]string {
return tags.TagSet.toMap()
Expand Down
17 changes: 16 additions & 1 deletion pkg/tags/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,58 @@ func TestParseTags(t *testing.T) {
testCases := []struct {
tags string
expectedErr bool
count int
}{
{
"key1=value1&key2=value2",
false,
2,
},
{
"store+forever=false&factory=true",
false,
2,
},
{
" store forever =false&factory=true",
false,
2,
},
{
fmt.Sprintf("%0128d=%0256d", 1, 1),
false,
1,
},
// Failure cases - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#tag-restrictions
{
"key1=value1&key1=value2",
true,
0,
},
{
"key$=value1",
true,
0,
},
{
"key1=value$",
true,
0,
},
{
fmt.Sprintf("%0128d=%0257d", 1, 1),
true,
0,
},
{
fmt.Sprintf("%0129d=%0256d", 1, 1),
true,
0,
},
{
fmt.Sprintf("%0129d=%0257d", 1, 1),
true,
0,
},
}

Expand All @@ -81,7 +92,11 @@ func TestParseTags(t *testing.T) {
t.Error("Expected failure but found success")
}
if err == nil {
t.Logf("%s", tt)
if tt.Count() != testCase.count {
t.Errorf("Expected count %d, got %d", testCase.count, tt.Count())
} else {
t.Logf("%s", tt)
}
}
})
}
Expand Down

0 comments on commit aadf946

Please sign in to comment.