From b2c25d400fad2437342c12d01d7e9aab8bc91fec Mon Sep 17 00:00:00 2001 From: Derek Su Date: Mon, 13 May 2024 03:46:21 +0000 Subject: [PATCH] misc: remove hyphens from UUID before truncating it Remove hyphens from UUID before truncating it in RandomID() Longhorn 8551 Signed-off-by: Derek Su --- utils/misc.go | 3 ++- utils/misc_test.go | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/utils/misc.go b/utils/misc.go index 8eeb4177..8485c679 100644 --- a/utils/misc.go +++ b/utils/misc.go @@ -6,6 +6,7 @@ import ( "path/filepath" "reflect" "runtime" + "strings" "github.com/google/uuid" "github.com/pkg/errors" @@ -67,7 +68,7 @@ func RandomID(randomIDLenth int) string { randomIDLenth = types.RandomIDDefaultLength } - uuid := UUID() + uuid := strings.Replace(UUID(), "-", "", -1) if len(uuid) > randomIDLenth { uuid = uuid[:randomIDLenth] diff --git a/utils/misc_test.go b/utils/misc_test.go index f425ba1e..d3841e82 100644 --- a/utils/misc_test.go +++ b/utils/misc_test.go @@ -1,6 +1,8 @@ package utils import ( + "strings" + . "gopkg.in/check.v1" "github.com/longhorn/go-common-libs/test" @@ -158,6 +160,7 @@ func (s *TestSuite) TestRandomID(c *C) { result := RandomID(testCase.idLength) c.Assert(len(result), Equals, testCase.expectedLength, Commentf(test.ErrResultFmt, testName)) + c.Assert(strings.Contains(result, "-"), Equals, false, Commentf(test.ErrResultFmt, testName)) } }