Skip to content

Commit

Permalink
chore: add az env test config
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnmoreels committed Jan 31, 2025
1 parent d1c4139 commit db9c4c5
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Arcus.Testing.Tests.Integration.Configuration
{
/// <summary>
/// Represents the environment on Azure where a set of test resources are located.
/// </summary>
public class AzureEnvironment
{
/// <summary>
/// Initializes a new instance of the <see cref="AzureEnvironment" /> class.
/// </summary>
public AzureEnvironment(
string subscriptionId,
string resourceGroupName)
{
SubscriptionId = subscriptionId;
ResourceGroupName = resourceGroupName;
}

/// <summary>
/// Gets the subscription ID for the set of test resources.
/// </summary>
public string SubscriptionId { get; }

/// <summary>
/// Gets the resource group name for the set of test resources.
/// </summary>
public string ResourceGroupName { get; }
}

/// <summary>
/// Extensions on the <see cref="TestConfig"/> for more test-friendly interaction.
/// </summary>
public static partial class TestConfigExtensions
{
/// <summary>
/// Loads the <see cref="AzureEnvironment"/> from the current test <paramref name="config"/>.
/// </summary>
public static AzureEnvironment GetAzureEnvironment(this TestConfig config)
{
return new AzureEnvironment(
config["Arcus:SubscriptionId"],
config["Arcus:ResourceGroup:Name"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public ServicePrincipal(string tenantId, string clientId, string clientSecret)
public string TenantId { get; }

public string ClientId { get; }

public string ClientSecret { get; }
}

public static class TestConfigExtensions
public static partial class TestConfigExtensions
{
public static ServicePrincipal GetServicePrincipal(this TestConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using Arcus.Testing.Tests.Integration.Configuration;
using Azure.Core;
using Azure.ResourceManager.DataFactory;

// ReSharper disable once CheckNamespace
namespace Arcus.Testing
Expand All @@ -13,16 +15,14 @@ public class DataFactoryConfig
/// Initializes a new instance of the <see cref="DataFactoryConfig" /> class.
/// </summary>
public DataFactoryConfig(
string subscriptionId,
string resourceGroupName,
string resourceName)
string factoryName,
ResourceIdentifier factoryResourceId)
{
ArgumentException.ThrowIfNullOrWhiteSpace(subscriptionId);
ArgumentException.ThrowIfNullOrWhiteSpace(resourceGroupName);
ArgumentException.ThrowIfNullOrWhiteSpace(resourceName);
ArgumentException.ThrowIfNullOrWhiteSpace(factoryName);
ArgumentNullException.ThrowIfNull(factoryResourceId);

Name = resourceName;
ResourceId = ResourceIdentifier.Parse($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{resourceName}");
Name = factoryName;
ResourceId = factoryResourceId;
}

/// <summary>
Expand All @@ -37,7 +37,7 @@ public DataFactoryConfig(
}

/// <summary>
/// Extensions on the <see cref="TestConfig"/> for more easier access to the <see cref="DataFactoryConfig"/>.
/// Extensions on the <see cref="TestConfig"/> for easier access to the <see cref="DataFactoryConfig"/>.
/// </summary>
public static class TestConfigExtensions
{
Expand All @@ -46,10 +46,15 @@ public static class TestConfigExtensions
/// </summary>
public static DataFactoryConfig GetDataFactory(this TestConfig config)
{
AzureEnvironment env = config.GetAzureEnvironment();

string factoryName = config["Arcus:DataFactory:Name"];
ResourceIdentifier factoryResourceId =
DataFactoryResource.CreateResourceIdentifier(env.SubscriptionId, env.ResourceGroupName, factoryName);

return new DataFactoryConfig(
config["Arcus:SubscriptionId"],
config["Arcus:ResourceGroup:Name"],
config["Arcus:DataFactory:Name"]);
factoryName,
factoryResourceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Arcus.Testing.Tests.Integration.Configuration;
using Arcus.Testing.Tests.Integration.Storage.Configuration;
using Azure;
using Azure.Core;
Expand Down Expand Up @@ -50,15 +51,19 @@ private TemporaryDataFactoryDataFlow(DataFlowDataType dataType, TestConfig confi
_config = config;
_logger = logger;

var env = config.GetAzureEnvironment();
SubscriptionId = env.SubscriptionId;
ResourceGroupName = env.ResourceGroupName;

Name = RandomizeWith("dataFlow");
SourceName = RandomizeWith("sourceName");
SinkName = RandomizeWith("sinkName");
SinkDataSetName = RandomizeWith("sinkDataSet");
SourceDataSetName = RandomizeWith("sourceDataSet");
}

private string SubscriptionId => _config["Arcus:SubscriptionId"];
private string ResourceGroupName => _config["Arcus:ResourceGroup:Name"];
private string SubscriptionId { get; }
private string ResourceGroupName { get; }
private DataFactoryConfig DataFactory => _config.GetDataFactory();
private StorageAccount StorageAccount => _config.GetStorageAccount();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using Arcus.Testing.Tests.Integration.Configuration;
using Azure.Core;
using Azure.ResourceManager.CosmosDB;

// ReSharper disable once CheckNamespace
namespace Arcus.Testing
Expand All @@ -9,88 +11,71 @@ public class CosmosDbConfig
/// <summary>
/// Initializes a new instance of the <see cref="CosmosDbConfig" /> class.
/// </summary>
public CosmosDbConfig(ResourceIdentifier resourceId, MongoDbConfig mongoDb, NoSqlConfig noSql)
public CosmosDbConfig(ResourceIdentifier accountResourceId, string accountName, string databaseName)
{
Name = resourceId.Name;
Endpoint = new Uri("https://arcus-testing-cosmos.mongo.cosmos.azure.com/").ToString();
ResourceId = resourceId;
MongoDb = mongoDb;
NoSql = noSql;
}
ArgumentNullException.ThrowIfNull(accountResourceId);
ArgumentException.ThrowIfNullOrWhiteSpace(accountName);
ArgumentException.ThrowIfNullOrWhiteSpace(databaseName);

public string Name { get; }
public string Endpoint { get; }
public ResourceIdentifier ResourceId { get; }
public MongoDbConfig MongoDb { get; }
public NoSqlConfig NoSql { get; }
}
AccountName = accountName;
AccountEndpoint = new Uri($"https://{accountName}.documents.azure.com/");
AccountResourceId = accountResourceId;
DatabaseName = databaseName;
}

public class MongoDbConfig
{
/// <summary>
/// Initializes a new instance of the <see cref="MongoDbConfig" /> class.
/// Gets the name of the Cosmos DB account associated with the current test setup.
/// </summary>
public MongoDbConfig(ResourceIdentifier resourceId, string accountName, string databaseName)
{
ResourceId = resourceId;
Name = accountName;
DatabaseName = databaseName;
}
public string AccountName { get; }

public string Name { get; }
public ResourceIdentifier ResourceId { get; }
public string DatabaseName { get; }
}
/// <summary>
/// Gets the endpoint of the Cosmos DB account associated with the current test setup.
/// </summary>
public Uri AccountEndpoint { get; }

public class NoSqlConfig
{
/// <summary>
/// Initializes a new instance of the <see cref="NoSqlConfig" /> class.
/// Gets the resource ID of the Cosmos DB account associated with the current test setup.
/// </summary>
public NoSqlConfig(ResourceIdentifier resourceId, string accountName, string databaseName)
{
ResourceId = resourceId;
DatabaseName = databaseName;
Endpoint = new Uri($"https://{accountName}.documents.azure.com/").ToString();
}
public ResourceIdentifier AccountResourceId { get; }

public string Endpoint { get; }
public ResourceIdentifier ResourceId { get; }
/// <summary>
/// Gets the name of the default database that was created on the Cosmos DB account associated with the current test setup.
/// </summary>
public string DatabaseName { get; }
}

/// <summary>
/// Extensions on the <see cref="TestConfig"/> for more test-friendly interaction.
/// </summary>
public static class CosmosDbTestConfigExtensions
{
public static MongoDbConfig GetMongoDb(this TestConfig config)
/// <summary>
/// Loads the <see cref="CosmosDbConfig"/> specific for the Mongo storage.
/// </summary>
public static CosmosDbConfig GetMongoDb(this TestConfig config)
{
string subscriptionId = config["Arcus:SubscriptionId"];
string resourceGroupName = config["Arcus:ResourceGroup:Name"];
string cosmosDbName = config["Arcus:Cosmos:MongoDb:Name"];
var resourceId = ResourceIdentifier.Parse($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{cosmosDbName}");
string databaseName = config["Arcus:Cosmos:MongoDb:DatabaseName"];

return new MongoDbConfig(
resourceId,
cosmosDbName,
databaseName);
return config.GetCosmosDb(CosmosDbType.MongoDb);
}

public static NoSqlConfig GetNoSql(this TestConfig config)
/// <summary>
/// Loads the <see cref="CosmosDbConfig"/> specific for the NoSql storage.
/// </summary>
public static CosmosDbConfig GetNoSql(this TestConfig config)
{
ResourceIdentifier resourceId = CreateResourceId(config, config["Arcus:Cosmos:NoSql:Name"]);

return new NoSqlConfig(
resourceId,
config["Arcus:Cosmos:NoSql:Name"],
config["Arcus:Cosmos:NoSql:DatabaseName"]);
return config.GetCosmosDb(CosmosDbType.NoSql);
}

private static ResourceIdentifier CreateResourceId(TestConfig config, string accountName)
private enum CosmosDbType { NoSql, MongoDb }

private static CosmosDbConfig GetCosmosDb(this TestConfig config, CosmosDbType cosmosDbType)
{
string subscriptionId = config["Arcus:SubscriptionId"];
string resourceGroupName = config["Arcus:ResourceGroup:Name"];

return ResourceIdentifier.Parse($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{accountName}");
AzureEnvironment env = config.GetAzureEnvironment();

string cosmosDbName = config[$"Arcus:Cosmos:{cosmosDbType}:Name"];
string databaseName = config[$"Arcus:Cosmos:{cosmosDbType}:DatabaseName"];
ResourceIdentifier resourceId = CosmosDBAccountResource.CreateResourceIdentifier(env.SubscriptionId, env.ResourceGroupName, cosmosDbName);

return new CosmosDbConfig(resourceId, cosmosDbName, databaseName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ private static async Task<MongoClient> AuthenticateMongoDbClientAsync(TestConfig
var tokenProvider = new DefaultAzureCredential();
AccessToken accessToken = await tokenProvider.GetTokenAsync(new TokenRequestContext(scopes: new[] { scope }));

string subscriptionId = config["Arcus:SubscriptionId"];
string resourceGroupName = config["Arcus:ResourceGroup:Name"];
string cosmosDbName = config["Arcus:Cosmos:MongoDb:Name"];
AzureEnvironment env = config.GetAzureEnvironment();
string subscriptionId = env.SubscriptionId;
string resourceGroupName = env.ResourceGroupName;
string cosmosDbName = config.GetMongoDb().AccountName;

var listConnectionStringUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{cosmosDbName}/listConnectionStrings?api-version=2021-04-15";

Expand Down Expand Up @@ -201,7 +202,7 @@ public async Task ShouldStoreDocumentAsync<T>(string collectionName, BsonValue i
}

/// <summary>
/// Verifies that a document does not exists in the MongoDb collection.
/// Verifies that a document does not exist in the MongoDb collection.
/// </summary>
public async Task ShouldNotStoreDocumentAsync<T>(string collectionName, BsonValue id)
{
Expand Down
Loading

0 comments on commit db9c4c5

Please sign in to comment.