Skip to content

Commit

Permalink
Trim trailing '-' sign (#955)
Browse files Browse the repository at this point in the history
* trim trailing '-' sign

* additional test

* Update tags.go

Here's what I recommend.

* trimming

* trimming after replacement test

Co-authored-by: Aaron Alpar <55999015+aaron-kasten@users.noreply.github.com>
  • Loading branch information
bathina2 and aaron-kasten committed Apr 20, 2021
1 parent 897b8fc commit 5a2294c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/blockstorage/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func SanitizeValueForGCP(value string) string {
}
sanitizedVal = strings.ToLower(sanitizedVal)
sanitizedVal = re.ReplaceAllString(sanitizedVal, "_")
sanitizedVal = strings.TrimRight(sanitizedVal, "_-")
return sanitizedVal
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/blockstorage/tags/tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tags

import (
"testing"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type TagsSuite struct{}

var _ = Suite(&TagsSuite{})

func (s *TagsSuite) TestSanitizeValueForGCP(c *C) {
for _, tc := range []struct {
input string
output string
}{
{
input: "abcd",
output: "abcd",
},
{
input: "kasten__snapshot-wordpress-on-rbd-ceph-ns-2021-04-15t18-11-27z-abcd",
output: "kasten__snapshot-wordpress-on-rbd-ceph-ns-2021-04-15t18-11-27z",
},
{
input: "kasten__snapshot-wordpress-on-rbd-ceph-ns-",
output: "kasten__snapshot-wordpress-on-rbd-ceph-ns",
},
{
input: "kasten__snapshot-wordpress-on-rbd-ceph-ns---",
output: "kasten__snapshot-wordpress-on-rbd-ceph-ns",
},
{
input: "kasten__snapshot-wordpress! ?*()",
output: "kasten__snapshot-wordpress",
},
{
input: "kasten__snapshot-wordpress-on-rbd-ceph-ns-__",
output: "kasten__snapshot-wordpress-on-rbd-ceph-ns",
},
{
input: "kasten__snapshot-wordpress-on-rbd-ceph-ns__--",
output: "kasten__snapshot-wordpress-on-rbd-ceph-ns",
},
{
input: "ALLCAPS",
output: "allcaps",
},
} {
out := SanitizeValueForGCP(tc.input)
c.Assert(out, Equals, tc.output)
}
}

0 comments on commit 5a2294c

Please sign in to comment.