github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/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  			// Missing "resourceGroups".
    16  			"/subscriptions/00000000-0000-0000-0000-000000000000//myResourceGroup/",
    17  			nil,
    18  			true,
    19  		},
    20  		{
    21  			// Empty resource group ID.
    22  			"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//",
    23  			nil,
    24  			true,
    25  		},
    26  		{
    27  			"random",
    28  			nil,
    29  			true,
    30  		},
    31  		{
    32  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    33  			nil,
    34  			true,
    35  		},
    36  		{
    37  			"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    38  			nil,
    39  			true,
    40  		},
    41  		{
    42  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1",
    43  			&ResourceID{
    44  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    45  				ResourceGroup:  "testGroup1",
    46  				Provider:       "",
    47  				Path:           map[string]string{},
    48  			},
    49  			false,
    50  		},
    51  		{
    52  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network",
    53  			&ResourceID{
    54  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    55  				ResourceGroup:  "testGroup1",
    56  				Provider:       "Microsoft.Network",
    57  				Path:           map[string]string{},
    58  			},
    59  			false,
    60  		},
    61  		{
    62  			// Missing leading /
    63  			"subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/",
    64  			nil,
    65  			true,
    66  		},
    67  		{
    68  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1",
    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?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  				},
    88  			},
    89  			false,
    90  		},
    91  		{
    92  			"/subscriptions/6d74bdd2-9f84-11e5-9bd9-7831c1c4c038/resourceGroups/testGroup1/providers/Microsoft.Network/virtualNetworks/virtualNetwork1/subnets/publicInstances1?api-version=2006-01-02-preview",
    93  			&ResourceID{
    94  				SubscriptionID: "6d74bdd2-9f84-11e5-9bd9-7831c1c4c038",
    95  				ResourceGroup:  "testGroup1",
    96  				Provider:       "Microsoft.Network",
    97  				Path: map[string]string{
    98  					"virtualNetworks": "virtualNetwork1",
    99  					"subnets":         "publicInstances1",
   100  				},
   101  			},
   102  			false,
   103  		},
   104  		{
   105  			"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourcegroups/acceptanceTestResourceGroup1/providers/Microsoft.Cdn/profiles/acceptanceTestCdnProfile1",
   106  			&ResourceID{
   107  				SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
   108  				ResourceGroup:  "acceptanceTestResourceGroup1",
   109  				Provider:       "Microsoft.Cdn",
   110  				Path: map[string]string{
   111  					"profiles": "acceptanceTestCdnProfile1",
   112  				},
   113  			},
   114  			false,
   115  		},
   116  		{
   117  			"/subscriptions/34ca515c-4629-458e-bf7c-738d77e0d0ea/resourceGroups/testGroup1/providers/Microsoft.ServiceBus/namespaces/testNamespace1/topics/testTopic1/subscriptions/testSubscription1",
   118  			&ResourceID{
   119  				SubscriptionID: "34ca515c-4629-458e-bf7c-738d77e0d0ea",
   120  				ResourceGroup:  "testGroup1",
   121  				Provider:       "Microsoft.ServiceBus",
   122  				Path: map[string]string{
   123  					"namespaces":    "testNamespace1",
   124  					"topics":        "testTopic1",
   125  					"subscriptions": "testSubscription1",
   126  				},
   127  			},
   128  			false,
   129  		},
   130  	}
   131  
   132  	for _, test := range testCases {
   133  		parsed, err := parseAzureResourceID(test.id)
   134  		if test.expectError && err != nil {
   135  			continue
   136  		}
   137  		if err != nil {
   138  			t.Fatalf("Unexpected error: %s", err)
   139  		}
   140  
   141  		if !reflect.DeepEqual(test.expectedResourceID, parsed) {
   142  			t.Fatalf("Unexpected resource ID:\nExpected: %+v\nGot:      %+v\n", test.expectedResourceID, parsed)
   143  		}
   144  	}
   145  }
   146  
   147  func TestComposeAzureResourceID(t *testing.T) {
   148  	testCases := []struct {
   149  		resourceID  *ResourceID
   150  		expectedID  string
   151  		expectError bool
   152  	}{
   153  		{
   154  			&ResourceID{
   155  				SubscriptionID: "00000000-0000-0000-0000-000000000000",
   156  				ResourceGroup:  "testGroup1",
   157  				Provider:       "foo.bar",
   158  				Path: map[string]string{
   159  					"k1": "v1",
   160  					"k2": "v2",
   161  					"k3": "v3",
   162  				},
   163  			},
   164  			"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1/providers/foo.bar/k1/v1/k2/v2/k3/v3",
   165  			false,
   166  		},
   167  		{
   168  			&ResourceID{
   169  				SubscriptionID: "00000000-0000-0000-0000-000000000000",
   170  				ResourceGroup:  "testGroup1",
   171  			},
   172  			"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1",
   173  			false,
   174  		},
   175  		{
   176  			// If Provider is specified, there must be at least one element in Path.
   177  			&ResourceID{
   178  				SubscriptionID: "00000000-0000-0000-0000-000000000000",
   179  				ResourceGroup:  "testGroup1",
   180  				Provider:       "foo.bar",
   181  			},
   182  			"",
   183  			true,
   184  		},
   185  		{
   186  			// One of the keys in Path is an empty string.
   187  			&ResourceID{
   188  				SubscriptionID: "00000000-0000-0000-0000-000000000000",
   189  				ResourceGroup:  "testGroup1",
   190  				Provider:       "foo.bar",
   191  				Path: map[string]string{
   192  					"k2": "v2",
   193  					"":   "v1",
   194  				},
   195  			},
   196  			"",
   197  			true,
   198  		},
   199  		{
   200  			// One of the values in Path is an empty string.
   201  			&ResourceID{
   202  				SubscriptionID: "00000000-0000-0000-0000-000000000000",
   203  				ResourceGroup:  "testGroup1",
   204  				Provider:       "foo.bar",
   205  				Path: map[string]string{
   206  					"k1": "v1",
   207  					"k2": "",
   208  				},
   209  			},
   210  			"",
   211  			true,
   212  		},
   213  	}
   214  
   215  	for _, test := range testCases {
   216  		idString, err := composeAzureResourceID(test.resourceID)
   217  
   218  		if test.expectError && err != nil {
   219  			continue
   220  		}
   221  
   222  		if err != nil {
   223  			t.Fatalf("Unexpected error: %s", err)
   224  		}
   225  
   226  		if test.expectedID != idString {
   227  			t.Fatalf("Unexpected resource ID string:\nExpected: %s\nGot:      %s\n", test.expectedID, idString)
   228  		}
   229  	}
   230  }