Skip to content

Commit

Permalink
Merge pull request #1521 from andyzhangx/fix-zone-check-empty-location
Browse files Browse the repository at this point in the history
fix: zone format check when cloud config is empty
  • Loading branch information
andyzhangx authored Sep 17, 2022
2 parents 16ce4b7 + 16e4a82 commit 217a81b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/azureutils/azure_disk_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ func IsAzureStackCloud(cloud string, disableAzureStackCloud bool) bool {

// IsValidAvailabilityZone returns true if the zone is in format of <region>-<zone-id>.
func IsValidAvailabilityZone(zone, region string) bool {
if region == "" {
index := strings.Index(zone, "-")
return index > 0 && index < len(zone)-1
}
return strings.HasPrefix(zone, fmt.Sprintf("%s-", region))
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/azureutils/azure_disk_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,20 +908,24 @@ func TestIsARMResourceID(t *testing.T) {
}

func TestIsAvailabilityZone(t *testing.T) {
region := "eastus"
tests := []struct {
desc string
zone string
region string
expected bool
}{
{"empty string should return false", "", false},
{"wrong farmat should return false", "123", false},
{"wrong location should return false", "chinanorth-1", false},
{"correct zone should return true", "eastus-1", true},
{"empty string should return false", "", "eastus", false},
{"wrong farmat should return false", "123", "eastus", false},
{"wrong location should return false", "chinanorth-1", "eastus", false},
{"correct zone should return true", "eastus-1", "eastus", true},
{"empty location should return true", "eastus-1", "", true},
{"empty location with fault domain should return false", "1", "", false},
{"empty location with wrong format should return false", "-1", "", false},
{"empty location with wrong format should return false", "eastus-", "", false},
}

for _, test := range tests {
actual := IsValidAvailabilityZone(test.zone, region)
actual := IsValidAvailabilityZone(test.zone, test.region)
if actual != test.expected {
t.Errorf("test [%q] get unexpected result: %v != %v", test.desc, actual, test.expected)
}
Expand Down

0 comments on commit 217a81b

Please sign in to comment.