github.com/mponton/terratest@v0.44.0/modules/azure/client_factory_test.go (about) 1 //go:build azure 2 // +build azure 3 4 // This file contains unit tests for the client factory implementation(s). 5 6 package azure 7 8 import ( 9 "os" 10 "testing" 11 12 autorest "github.com/Azure/go-autorest/autorest/azure" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 // Local consts for this file only 18 const govCloudEnvName = "AzureUSGovernmentCloud" 19 const publicCloudEnvName = "AzurePublicCloud" 20 const chinaCloudEnvName = "AzureChinaCloud" 21 const germanyCloudEnvName = "AzureGermanCloud" 22 23 func TestDefaultEnvIsPublicWhenNotSet(t *testing.T) { 24 // save any current env value and restore on exit 25 originalEnv := os.Getenv(AzureEnvironmentEnvName) 26 defer os.Setenv(AzureEnvironmentEnvName, originalEnv) 27 28 // Set env var to missing value 29 os.Setenv(AzureEnvironmentEnvName, "") 30 31 // get the default 32 env := getDefaultEnvironmentName() 33 34 // Make sure it's public cloud 35 assert.Equal(t, autorest.PublicCloud.Name, env) 36 } 37 38 func TestDefaultEnvSetToGov(t *testing.T) { 39 // save any current env value and restore on exit 40 originalEnv := os.Getenv(AzureEnvironmentEnvName) 41 defer os.Setenv(AzureEnvironmentEnvName, originalEnv) 42 43 // Set env var to gov 44 os.Setenv(AzureEnvironmentEnvName, govCloudEnvName) 45 46 // get the default 47 env := getDefaultEnvironmentName() 48 49 // Make sure it's public cloud 50 assert.Equal(t, autorest.USGovernmentCloud.Name, env) 51 } 52 53 func TestSubscriptionClientBaseURISetCorrectly(t *testing.T) { 54 var cases = []struct { 55 CaseName string 56 EnvironmentName string 57 ExpectedBaseURI string 58 }{ 59 {"GovCloud/SubscriptionClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 60 {"PublicCloud/SubscriptionClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 61 {"ChinaCloud/SubscriptionClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 62 {"GermanCloud/SubscriptionClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 63 } 64 65 // save any current env value and restore on exit 66 currentEnv := os.Getenv(AzureEnvironmentEnvName) 67 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 68 69 for _, tt := range cases { 70 // The following is necessary to make sure testCase's values don't 71 // get updated due to concurrency within the scope of t.Run(..) below 72 tt := tt 73 t.Run(tt.CaseName, func(t *testing.T) { 74 // Override env setting 75 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 76 77 // Get a VM client 78 client, err := CreateSubscriptionsClientE() 79 require.NoError(t, err) 80 81 // Check for correct ARM URI 82 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 83 }) 84 } 85 } 86 87 // snippet-tag-start::client_factory_example.UnitTest 88 89 func TestVMClientBaseURISetCorrectly(t *testing.T) { 90 var cases = []struct { 91 CaseName string 92 EnvironmentName string 93 ExpectedBaseURI string 94 }{ 95 {"GovCloud/VMClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 96 {"PublicCloud/VMClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 97 {"ChinaCloud/VMClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 98 {"GermanCloud/VMClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 99 } 100 101 // save any current env value and restore on exit 102 currentEnv := os.Getenv(AzureEnvironmentEnvName) 103 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 104 105 for _, tt := range cases { 106 // The following is necessary to make sure testCase's values don't 107 // get updated due to concurrency within the scope of t.Run(..) below 108 tt := tt 109 t.Run(tt.CaseName, func(t *testing.T) { 110 // Override env setting 111 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 112 113 // Get a VM client 114 client, err := CreateVirtualMachinesClientE("") 115 require.NoError(t, err) 116 117 // Check for correct ARM URI 118 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 119 }) 120 } 121 } 122 123 // snippet-tag-end::client_factory_example.UnitTest 124 125 func TestManagedClustersClientBaseURISetCorrectly(t *testing.T) { 126 var cases = []struct { 127 CaseName string 128 EnvironmentName string 129 ExpectedBaseURI string 130 }{ 131 {"GovCloud/ManagedClustersClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 132 {"PublicCloud/ManagedClustersClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 133 {"ChinaCloud/ManagedClustersClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 134 {"GermanCloud/ManagedClustersClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 135 } 136 137 // save any current env value and restore on exit 138 currentEnv := os.Getenv(AzureEnvironmentEnvName) 139 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 140 141 for _, tt := range cases { 142 // The following is necessary to make sure testCase's values don't 143 // get updated due to concurrency within the scope of t.Run(..) below 144 tt := tt 145 t.Run(tt.CaseName, func(t *testing.T) { 146 // Override env setting 147 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 148 149 // Get a VM client 150 client, err := CreateManagedClustersClientE("") 151 require.NoError(t, err) 152 153 // Check for correct ARM URI 154 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 155 }) 156 } 157 } 158 159 func TestCosmosDBAccountClientBaseURISetCorrectly(t *testing.T) { 160 var cases = []struct { 161 CaseName string 162 EnvironmentName string 163 ExpectedBaseURI string 164 }{ 165 {"GovCloud/CosmosDBAccountClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 166 {"PublicCloud/CosmosDBAccountClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 167 {"ChinaCloud/CosmosDBAccountClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 168 {"GermanCloud/CosmosDBAccountClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 169 } 170 171 // save any current env value and restore on exit 172 currentEnv := os.Getenv(AzureEnvironmentEnvName) 173 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 174 175 for _, tt := range cases { 176 // The following is necessary to make sure testCase's values don't 177 // get updated due to concurrency within the scope of t.Run(..) below 178 tt := tt 179 t.Run(tt.CaseName, func(t *testing.T) { 180 // Override env setting 181 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 182 183 // Get a VM client 184 client, err := CreateCosmosDBAccountClientE("") 185 require.NoError(t, err) 186 187 // Check for correct ARM URI 188 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 189 }) 190 } 191 } 192 193 func TestCosmosDBSQLClientBaseURISetCorrectly(t *testing.T) { 194 var cases = []struct { 195 CaseName string 196 EnvironmentName string 197 ExpectedBaseURI string 198 }{ 199 {"GovCloud/CosmosDBAccountClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 200 {"PublicCloud/CosmosDBAccountClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 201 {"ChinaCloud/CosmosDBAccountClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 202 {"GermanCloud/CosmosDBAccountClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 203 } 204 205 // save any current env value and restore on exit 206 currentEnv := os.Getenv(AzureEnvironmentEnvName) 207 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 208 209 for _, tt := range cases { 210 // The following is necessary to make sure testCase's values don't 211 // get updated due to concurrency within the scope of t.Run(..) below 212 tt := tt 213 t.Run(tt.CaseName, func(t *testing.T) { 214 // Override env setting 215 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 216 217 // Get a VM client 218 client, err := CreateCosmosDBSQLClientE("") 219 require.NoError(t, err) 220 221 // Check for correct ARM URI 222 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 223 }) 224 } 225 } 226 func TestPublicIPAddressesClientBaseURISetCorrectly(t *testing.T) { 227 var cases = []struct { 228 CaseName string 229 EnvironmentName string 230 ExpectedBaseURI string 231 }{ 232 {"GovCloud/CosmosDBAccountClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 233 {"PublicCloud/CosmosDBAccountClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 234 {"ChinaCloud/CosmosDBAccountClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 235 {"GermanCloud/CosmosDBAccountClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 236 } 237 238 // save any current env value and restore on exit 239 currentEnv := os.Getenv(AzureEnvironmentEnvName) 240 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 241 242 for _, tt := range cases { 243 // The following is necessary to make sure testCase's values don't 244 // get updated due to concurrency within the scope of t.Run(..) below 245 tt := tt 246 t.Run(tt.CaseName, func(t *testing.T) { 247 // Override env setting 248 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 249 250 // Get a VM client 251 client, err := CreatePublicIPAddressesClientE("") 252 require.NoError(t, err) 253 254 // Check for correct ARM URI 255 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 256 }) 257 } 258 } 259 func TestLoadBalancerClientBaseURISetCorrectly(t *testing.T) { 260 var cases = []struct { 261 CaseName string 262 EnvironmentName string 263 ExpectedBaseURI string 264 }{ 265 {"GovCloud/CosmosDBAccountClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 266 {"PublicCloud/CosmosDBAccountClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 267 {"ChinaCloud/CosmosDBAccountClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 268 {"GermanCloud/CosmosDBAccountClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 269 } 270 271 // save any current env value and restore on exit 272 currentEnv := os.Getenv(AzureEnvironmentEnvName) 273 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 274 275 for _, tt := range cases { 276 // The following is necessary to make sure testCase's values don't 277 // get updated due to concurrency within the scope of t.Run(..) below 278 tt := tt 279 t.Run(tt.CaseName, func(t *testing.T) { 280 // Override env setting 281 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 282 283 // Get a VM client 284 client, err := CreateLoadBalancerClientE("") 285 require.NoError(t, err) 286 287 // Check for correct ARM URI 288 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 289 }) 290 } 291 } 292 293 func TestFrontDoorClientBaseURISetCorrectly(t *testing.T) { 294 var cases = []struct { 295 CaseName string 296 EnvironmentName string 297 ExpectedBaseURI string 298 }{ 299 {"GovCloud/FrontDoorClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 300 {"PublicCloud/FrontDoorClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 301 {"ChinaCloud/FrontDoorClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 302 {"GermanCloud/FrontDoorClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 303 } 304 305 // save any current env value and restore on exit 306 currentEnv := os.Getenv(AzureEnvironmentEnvName) 307 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 308 309 for _, tt := range cases { 310 // The following is necessary to make sure testCase's values don't 311 // get updated due to concurrency within the scope of t.Run(..) below 312 tt := tt 313 t.Run(tt.CaseName, func(t *testing.T) { 314 // Override env setting 315 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 316 317 // Get a Front Door client 318 client, err := CreateFrontDoorClientE("") 319 require.NoError(t, err) 320 321 // Check for correct ARM URI 322 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 323 }) 324 } 325 } 326 327 func TestFrontDoorFrontendEndpointClientBaseURISetCorrectly(t *testing.T) { 328 var cases = []struct { 329 CaseName string 330 EnvironmentName string 331 ExpectedBaseURI string 332 }{ 333 {"GovCloud/FrontDoorClient", govCloudEnvName, autorest.USGovernmentCloud.ResourceManagerEndpoint}, 334 {"PublicCloud/FrontDoorClient", publicCloudEnvName, autorest.PublicCloud.ResourceManagerEndpoint}, 335 {"ChinaCloud/FrontDoorClient", chinaCloudEnvName, autorest.ChinaCloud.ResourceManagerEndpoint}, 336 {"GermanCloud/FrontDoorClient", germanyCloudEnvName, autorest.GermanCloud.ResourceManagerEndpoint}, 337 } 338 339 // save any current env value and restore on exit 340 currentEnv := os.Getenv(AzureEnvironmentEnvName) 341 defer os.Setenv(AzureEnvironmentEnvName, currentEnv) 342 343 for _, tt := range cases { 344 // The following is necessary to make sure testCase's values don't 345 // get updated due to concurrency within the scope of t.Run(..) below 346 tt := tt 347 t.Run(tt.CaseName, func(t *testing.T) { 348 // Override env setting 349 os.Setenv(AzureEnvironmentEnvName, tt.EnvironmentName) 350 351 // Get a AFD frontend endpoint client 352 client, err := CreateFrontDoorFrontendEndpointClientE("") 353 require.NoError(t, err) 354 355 // Check for correct ARM URI 356 assert.Equal(t, tt.ExpectedBaseURI, client.BaseURI) 357 }) 358 } 359 }