108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
|
package azurerm
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestParseAzureResourceID(t *testing.T) {
|
||
|
testCases := []struct {
|
||
|
id string
|
||
|
expectedResourceID *ResourceID
|
||
|
expectError bool
|
||
|
}{
|
||
|
{
|
||
|
"random",
|
||
|
nil,
|
||
|
true,
|
||
|
},
|
||
|
{
|
||
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
nil,
|
||
|
true,
|
||
|
},
|
||
|
{
|
||
|
"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
nil,
|
||
|
true,
|
||
|
},
|
||
|
{
|
||
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1",
|
||
|
&ResourceID{
|
||
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
ResourceGroup: "testGroup1",
|
||
|
Provider: "",
|
||
|
Path: map[string]string{},
|
||
|
},
|
||
|
false,
|
||
|
},
|
||
|
{
|
||
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network",
|
||
|
&ResourceID{
|
||
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
ResourceGroup: "testGroup1",
|
||
|
Provider: "Microsoft.Network",
|
||
|
Path: map[string]string{},
|
||
|
},
|
||
|
false,
|
||
|
},
|
||
|
{
|
||
|
// Missing leading /
|
||
|
"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/",
|
||
|
nil,
|
||
|
true,
|
||
|
},
|
||
|
{
|
||
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1",
|
||
|
&ResourceID{
|
||
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
ResourceGroup: "testGroup1",
|
||
|
Provider: "Microsoft.Network",
|
||
|
Path: map[string]string{
|
||
|
"virtualNetworks": "virtualNetwork1",
|
||
|
},
|
||
|
},
|
||
|
false,
|
||
|
},
|
||
|
{
|
||
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1?api-version=2006-01-02-preview",
|
||
|
&ResourceID{
|
||
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
ResourceGroup: "testGroup1",
|
||
|
Provider: "Microsoft.Network",
|
||
|
Path: map[string]string{
|
||
|
"virtualNetworks": "virtualNetwork1",
|
||
|
},
|
||
|
},
|
||
|
false,
|
||
|
},
|
||
|
{
|
||
|
"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/subnets/publicInstances1?api-version=2006-01-02-preview",
|
||
|
&ResourceID{
|
||
|
SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
|
||
|
ResourceGroup: "testGroup1",
|
||
|
Provider: "Microsoft.Network",
|
||
|
Path: map[string]string{
|
||
|
"virtualNetworks": "virtualNetwork1",
|
||
|
"subnets": "publicInstances1",
|
||
|
},
|
||
|
},
|
||
|
false,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, test := range testCases {
|
||
|
parsed, err := parseAzureResourceID(test.id)
|
||
|
if test.expectError && err != nil {
|
||
|
continue
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Fatalf("Unexpected error: %s", err)
|
||
|
}
|
||
|
|
||
|
if !reflect.DeepEqual(test.expectedResourceID, parsed) {
|
||
|
t.Fatalf("Unexpected resource ID:\nExpected: %+v\nGot: %+v\n", test.expectedResourceID, parsed)
|
||
|
}
|
||
|
}
|
||
|
}
|