github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resourceid_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestParseAzureResourceID(t *testing.T) {
     9  	testCases := []struct {
    10  		id                 string
    11  		expectedResourceID *ResourceID
    12  		expectError        bool
    13  	}{
    14  		{
    15  			"random",
    16  			nil,
    17  			true,
    18  		},
    19  		{
    20  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    21  			nil,
    22  			true,
    23  		},
    24  		{
    25  			"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    26  			nil,
    27  			true,
    28  		},
    29  		{
    30  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1",
    31  			&ResourceID{
    32  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    33  				ResourceGroup:  "testGroup1",
    34  				Provider:       "",
    35  				Path:           map[string]string{},
    36  			},
    37  			false,
    38  		},
    39  		{
    40  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network",
    41  			&ResourceID{
    42  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    43  				ResourceGroup:  "testGroup1",
    44  				Provider:       "Microsoft.Network",
    45  				Path:           map[string]string{},
    46  			},
    47  			false,
    48  		},
    49  		{
    50  			// Missing leading /
    51  			"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/",
    52  			nil,
    53  			true,
    54  		},
    55  		{
    56  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1",
    57  			&ResourceID{
    58  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    59  				ResourceGroup:  "testGroup1",
    60  				Provider:       "Microsoft.Network",
    61  				Path: map[string]string{
    62  					"virtualNetworks": "virtualNetwork1",
    63  				},
    64  			},
    65  			false,
    66  		},
    67  		{
    68  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1?api-version=2006-01-02-preview",
    69  			&ResourceID{
    70  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    71  				ResourceGroup:  "testGroup1",
    72  				Provider:       "Microsoft.Network",
    73  				Path: map[string]string{
    74  					"virtualNetworks": "virtualNetwork1",
    75  				},
    76  			},
    77  			false,
    78  		},
    79  		{
    80  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/subnets/publicInstances1?api-version=2006-01-02-preview",
    81  			&ResourceID{
    82  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    83  				ResourceGroup:  "testGroup1",
    84  				Provider:       "Microsoft.Network",
    85  				Path: map[string]string{
    86  					"virtualNetworks": "virtualNetwork1",
    87  					"subnets":         "publicInstances1",
    88  				},
    89  			},
    90  			false,
    91  		},
    92  		{
    93  			"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourcegroups/acceptanceTestResourceGroup1/providers/Microsoft.Cdn/profiles/acceptanceTestCdnProfile1",
    94  			&ResourceID{
    95  				SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
    96  				ResourceGroup:  "acceptanceTestResourceGroup1",
    97  				Provider:       "Microsoft.Cdn",
    98  				Path: map[string]string{
    99  					"profiles": "acceptanceTestCdnProfile1",
   100  				},
   101  			},
   102  			false,
   103  		},
   104  	}
   105  
   106  	for _, test := range testCases {
   107  		parsed, err := parseAzureResourceID(test.id)
   108  		if test.expectError && err != nil {
   109  			continue
   110  		}
   111  		if err != nil {
   112  			t.Fatalf("Unexpected error: %s", err)
   113  		}
   114  
   115  		if !reflect.DeepEqual(test.expectedResourceID, parsed) {
   116  			t.Fatalf("Unexpected resource ID:\nExpected: %+v\nGot:      %+v\n", test.expectedResourceID, parsed)
   117  		}
   118  	}
   119  }