Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
using System.Collections.Generic;
using Microsoft.Azure.Management.ApiManagement;
using Microsoft.Azure.Management.ApiManagement.Models;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Network;
using Xunit;
using Microsoft.Azure.Management.EventHub;
using Microsoft.Azure.Management.ManagedServiceIdentity;
using ApiManagementManagement.Tests.Helpers;

namespace ApiManagement.Tests
{
Expand All @@ -33,6 +34,9 @@ public class ApiManagementTestBase : TestBase
private const string TestBackupUserMsiClientId = "TestBackupUserMsiClientId";
private const string TestBackupUserMsiResourceId = "TestBackupUserMsiResourceId";

private static string[] CanaryRegions = new[] { "eastus2euap", "centraluseuap" };
private const string EnvironmentKey = "Environment";

public string location { get; set; }
public string subscriptionId { get; set; }
public ApiManagementClient client { get; set; }
Expand All @@ -51,9 +55,11 @@ public class ApiManagementTestBase : TestBase
public string testBackupStorageAccountName { get; internal set; }
public string testBackupUserMsiClientId { get; internal set; }
public string testBackupUserMsiId { get; internal set; }
public string envName { get; internal set; }

public ApiManagementTestBase(MockContext context)
{

this.client = context.GetServiceClient<ApiManagementClient>();
this.resourcesClient = context.GetServiceClient<ResourceManagementClient>();
this.storageClient = context.GetServiceClient<StorageManagementClient>();
Expand All @@ -68,6 +74,8 @@ private void Initialize()
{
var testEnv = TestEnvironmentFactory.GetTestEnvironment();

tags = new Dictionary<string, string> { { "DateCreated", $"{DateTime.UtcNow.ToString()}" }, { "apiversion", $"{client.ApiVersion}" } };

if (HttpMockServer.Mode == HttpRecorderMode.Record)
{
if (!testEnv.ConnectionString.KeyValuePairs.TryGetValue(ServiceNameKey, out string apimServiceName))
Expand All @@ -91,13 +99,14 @@ private void Initialize()
if (!testEnv.ConnectionString.KeyValuePairs.TryGetValue(ResourceGroupNameKey, out string resourceGroupName))
{
rgName = TestUtilities.GenerateName("sdktestrg");
resourcesClient.ResourceGroups.CreateOrUpdate(rgName, new ResourceGroup { Location = this.location });
}
else
{
this.rgName = resourceGroupName;
}

TryCreateResourceGroupIfNoExists(this.rgName);

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(TestCertificateKey, out string base64EncodedCertificate))
{
this.base64EncodedTestCertificateData = base64EncodedCertificate;
Expand Down Expand Up @@ -134,6 +143,12 @@ private void Initialize()
HttpMockServer.Variables[TestBackupUserMsiResourceId] = backupUserMsiId;
}

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(EnvironmentKey, out string envName))
{
this.envName = envName;
HttpMockServer.Variables[EnvironmentKey] = envName;
}

this.subscriptionId = testEnv.SubscriptionId;
HttpMockServer.Variables[SubIdKey] = subscriptionId;
HttpMockServer.Variables[ServiceNameKey] = this.serviceName;
Expand Down Expand Up @@ -168,18 +183,22 @@ private void Initialize()
this.testBackupStorageAccountName = testBackupStorageAccount;
}
HttpMockServer.Variables.TryGetValue(TestBackupUserMsiClientId, out string backupUserMsiClientId);
if(!string.IsNullOrEmpty(backupUserMsiClientId))
if (!string.IsNullOrEmpty(backupUserMsiClientId))
{
this.testBackupUserMsiClientId = backupUserMsiClientId;
}
HttpMockServer.Variables.TryGetValue(TestBackupUserMsiResourceId, out string backupUserMsiId);
if (!string.IsNullOrEmpty(backupUserMsiId))
{
{
this.testBackupUserMsiId = backupUserMsiId;
}
if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(EnvironmentKey, out string envName))
{
this.envName = envName;
HttpMockServer.Variables[EnvironmentKey] = envName;
}
}

tags = new Dictionary<string, string> { { "tag1", "value1" }, { "tag2", "value2" }, { "tag3", "value3" } };

serviceProperties = new ApiManagementServiceResource
{
Expand All @@ -193,7 +212,7 @@ private void Initialize()
PublisherName = "autorestsdk",
Tags = tags,
Identity = new ApiManagementServiceIdentity("SystemAssigned")
};
};
}

public void TryCreateApiManagementService()
Expand All @@ -207,6 +226,14 @@ public void TryCreateApiManagementService()
Assert.Equal(this.serviceName, service.Name);
}

private void TryCreateResourceGroupIfNoExists(string rgName)
{
var rg = resourcesClient.ResourceGroups.CheckExistence(rgName);
if (!rg)
{
resourcesClient.ResourceGroups.CreateOrUpdate(rgName, new ResourceGroup { Location = this.location, Tags = tags });
}
}

public string GetOpenIdMetadataEndpointUrl()
{
Expand All @@ -227,6 +254,29 @@ public string GetLocation(string regionIn = "US")
).First().Locations.Where(l => l.Contains(regionIn)).FirstOrDefault();
}

public string GetAdditionLocation(string masterLocation, string regionIn = "US")
{
if (CanaryRegions
.Any(d => string.Equals(masterLocation.ToLowerAndRemoveWhiteSpaces(), d)))
{
return CanaryRegions.FirstOrDefault(d => !string.Equals(d, masterLocation.ToLowerAndRemoveWhiteSpaces()));
}

var provider = this.resourcesClient.Providers.Get("Microsoft.ApiManagement");
return provider.ResourceTypes.Where(
(resType) =>
{
if (resType.ResourceType == "service")
return true;
else
return false;
}
).First().Locations
.Where(l => !string.Equals(l.ToLowerAndRemoveWhiteSpaces(), masterLocation.ToLowerAndRemoveWhiteSpaces()))
.Where(l => l.Contains(regionIn))
.FirstOrDefault();
}

public static byte[] RandomBytes(int length)
{
if (HttpMockServer.Mode == HttpRecorderMode.Record)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public partial class ApiManagementServiceTests
[Trait("owner", "sasolank")]
public void CreateMultiRegionService()
{
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
using (MockContext context = MockContext.Start(this.GetType()))
{
var testBase = new ApiManagementTestBase(context);

var additionalLocation = new AdditionalLocation()
{
Location = testBase.GetLocation("Europe"),
Location = testBase.GetAdditionLocation(testBase.location, "Europe"),
Sku = new ApiManagementServiceSkuProperties(SkuType.Premium, capacity: 1)
};

Expand Down Expand Up @@ -97,5 +97,5 @@ public void CreateMultiRegionService()
});
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class ApiManagementServiceTests
[Trait("owner", "jikang")]
public void GetOutboundNetworkDependencies()
{
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");

using (MockContext context = MockContext.Start(this.GetType()))
{
Expand All @@ -32,7 +32,7 @@ public void GetOutboundNetworkDependencies()
var outboundDependencies = testBase.client.OutboundNetworkDependenciesEndpoints.ListByService(testBase.rgName, testBase.serviceName);

Assert.NotNull(outboundDependencies);
Assert.Equal(10, outboundDependencies.Value.Count);
Assert.Equal(11, outboundDependencies.Value.Count);
Assert.NotNull(outboundDependencies.Value.Single(d => d.Category.Equals("Azure SMTP")));
Assert.NotNull(outboundDependencies.Value.Single(d => d.Category.Equals("Azure Storage")));
Assert.NotNull(outboundDependencies.Value.Single(d => d.Category.Equals("Azure Active Directory")));
Expand Down
Loading