|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package azure |
| 7 | + |
| 8 | +import "testing" |
| 9 | + |
| 10 | +func TestParseAzureID(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + input string |
| 14 | + expectedString string |
| 15 | + expectErr bool |
| 16 | + expected azureID |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "Valid VM", |
| 20 | + input: "/subscriptions/1234-abcd/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/n01", |
| 21 | + expectedString: "/subscriptions/1234-abcd/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/n01", |
| 22 | + expected: azureID{ |
| 23 | + subscription: "1234-abcd", |
| 24 | + resourceGroup: "my-rg", |
| 25 | + provider: "Microsoft.Compute", |
| 26 | + resourceType: "virtualMachines", |
| 27 | + resourceName: "n01", |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "Valid NIC", |
| 32 | + input: "/subscriptions/1234-abcd/resourceGroups/test-rg/providers/Microsoft.Network/networkInterfaces/test-nic01", |
| 33 | + expectedString: "/subscriptions/1234-abcd/resourceGroups/test-rg/providers/Microsoft.Network/networkInterfaces/test-nic01", |
| 34 | + expected: azureID{ |
| 35 | + subscription: "1234-abcd", |
| 36 | + resourceGroup: "test-rg", |
| 37 | + provider: "Microsoft.Network", |
| 38 | + resourceType: "networkInterfaces", |
| 39 | + resourceName: "test-nic01", |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + // Since child resources are not implemented, all we are checking here is |
| 44 | + // that the resourceName gets selected out properly |
| 45 | + name: "Valid with child resource", |
| 46 | + input: "/subscriptions/1111/resourceGroups/rg1/providers/Microsoft.Network/loadBalancers/lb01/frontendIPConfigurations/ipconfig1", |
| 47 | + expectedString: "/subscriptions/1111/resourceGroups/rg1/providers/Microsoft.Network/loadBalancers/lb01", |
| 48 | + expected: azureID{ |
| 49 | + subscription: "1111", |
| 50 | + resourceGroup: "rg1", |
| 51 | + provider: "Microsoft.Network", |
| 52 | + resourceType: "loadBalancers", |
| 53 | + resourceName: "lb01", |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Invalid - missing fields", |
| 58 | + input: "/subscriptions/abcd/resourceGroups//providers/Microsoft.Compute/virtualMachines/vm1", |
| 59 | + expectErr: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Invalid - not an Azure ID", |
| 63 | + input: "/this/is/not/an/azure/id", |
| 64 | + expectErr: true, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.name, func(t *testing.T) { |
| 70 | + actual, err := parseAzureID(tt.input) |
| 71 | + if tt.expectErr { |
| 72 | + if err == nil { |
| 73 | + t.Errorf("expected error but got none") |
| 74 | + } |
| 75 | + return |
| 76 | + } |
| 77 | + if err != nil { |
| 78 | + t.Errorf("unexpected error: %v", err) |
| 79 | + return |
| 80 | + } |
| 81 | + // Compare fields |
| 82 | + if actual.subscription != tt.expected.subscription { |
| 83 | + t.Errorf("subscription: actual %q, expected %q", actual.subscription, tt.expected.subscription) |
| 84 | + } |
| 85 | + if actual.resourceGroup != tt.expected.resourceGroup { |
| 86 | + t.Errorf("resourceGroup: actual %q, expected %q", actual.resourceGroup, tt.expected.resourceGroup) |
| 87 | + } |
| 88 | + if actual.provider != tt.expected.provider { |
| 89 | + t.Errorf("provider: actual %q, expected %q", actual.provider, tt.expected.provider) |
| 90 | + } |
| 91 | + if actual.resourceType != tt.expected.resourceType { |
| 92 | + t.Errorf("resourceType: actual %q, expected %q", actual.resourceType, tt.expected.resourceType) |
| 93 | + } |
| 94 | + if actual.resourceName != tt.expected.resourceName { |
| 95 | + t.Errorf("resourceName: actual %q, expected %q", actual.resourceName, tt.expected.resourceName) |
| 96 | + } |
| 97 | + // Ensure String() reconstructs the same input |
| 98 | + if actual.String() != tt.expectedString { |
| 99 | + t.Errorf("String(): actual %q, expected %q", actual.String(), tt.expectedString) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments