From aadf94623806731617fda2c8a8624028f157a154 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 11 Jul 2023 12:55:26 -0700 Subject: [PATCH] return count for number of tags and add a testcase --- pkg/tags/tags.go | 9 +++++++++ pkg/tags/tags_test.go | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/tags/tags.go b/pkg/tags/tags.go index 98ae17efa..7a84a6f34 100644 --- a/pkg/tags/tags.go +++ b/pkg/tags/tags.go @@ -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 { @@ -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() diff --git a/pkg/tags/tags_test.go b/pkg/tags/tags_test.go index 89b68cc17..129a10986 100644 --- a/pkg/tags/tags_test.go +++ b/pkg/tags/tags_test.go @@ -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, }, } @@ -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) + } } }) }