Skip to content

Commit

Permalink
refactor: Satisfy linter in zonesToRegion (#2735)
Browse files Browse the repository at this point in the history
* Satisfy linter in `zonesToRegion`

* Ensure consistent region ordering

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
e-sumin and mergify[bot] authored Mar 9, 2024
1 parent 2b86def commit 0e6831c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
17 changes: 7 additions & 10 deletions pkg/kube/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,16 @@ func zoneToRegion(zone string) string {
}

func zonesToRegions(zone string) []string {
reg := map[string]struct{}{}
// TODO: gocritic rule below suggests to use regexp.MustCompile but it
// panics if regex cannot be compiled. We should add proper test before
// enabling this below so that no change to this regex results in a panic
r, _ := regexp.Compile("-?[a-z]$") //nolint:gocritic
reg := map[string]bool{}
var regions []string
r := regexp.MustCompile("-?[a-z]$")
for _, z := range strings.Split(zone, RegionZoneSeparator) {
zone = r.ReplaceAllString(z, "")
reg[zone] = struct{}{}
if _, ok := reg[zone]; !ok {
reg[zone] = true
regions = append(regions, zone)
}
}

var regions []string
for k := range reg {
regions = append(regions, k)
}
return regions
}
25 changes: 2 additions & 23 deletions pkg/kube/volume/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (s *TestVolSuite) fakeUnstructuredSnasphotWSize(vsName, namespace, size str
}

func (s *TestVolSuite) TestZoneToRegion(c *C) {
for _, tc := range []struct {
for idx, tc := range []struct {
zone string
expectedRegion []string
}{
Expand Down Expand Up @@ -237,27 +237,6 @@ func (s *TestVolSuite) TestZoneToRegion(c *C) {
},
} {
reg := zonesToRegions(tc.zone)
c.Assert(slicesEqual(reg, tc.expectedRegion), Equals, true)
}
}

// slicesEqual compares two unordered slices and returns true if
// both of them have same elements
func slicesEqual(one, two []string) bool {
if len(one) != len(two) {
return false
}

for _, o := range one {
var found bool
for _, t := range two {
if o == t {
found = true
}
}
if !found {
return false
}
c.Assert(reg, DeepEquals, tc.expectedRegion, Commentf("Case #%d", idx))
}
return true
}

0 comments on commit 0e6831c

Please sign in to comment.