From d8bb4b54b627fe250b2e6e81495e376f1f6551a5 Mon Sep 17 00:00:00 2001 From: Kiro Morkos Date: Thu, 25 Jul 2024 12:23:49 -0400 Subject: [PATCH] Make mongodb connection string used for tests configurable --- Tests/Realm.Tests/Sync/SyncTestHelpers.cs | 8 ++---- Tools/DeployApps/BaasClient.cs | 30 ++++++++++++++++------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Tests/Realm.Tests/Sync/SyncTestHelpers.cs b/Tests/Realm.Tests/Sync/SyncTestHelpers.cs index 40a0546ca8..463b07b1ba 100644 --- a/Tests/Realm.Tests/Sync/SyncTestHelpers.cs +++ b/Tests/Realm.Tests/Sync/SyncTestHelpers.cs @@ -185,7 +185,7 @@ private static async Task CreateBaasAppsAsync() if (_baaSaasApiKey != null) { BaasUri = await BaasClient.GetOrDeployContainer(_baaSaasApiKey, differentiator, TestHelpers.Output); - _baasClient = await BaasClient.Docker(BaasUri, differentiator, TestHelpers.Output); + _baasClient = await BaasClient.Docker(BaasUri, differentiator, TestHelpers.Output, null); } else if (!string.IsNullOrEmpty(cluster) && !string.IsNullOrEmpty(apiKey) && @@ -194,12 +194,8 @@ private static async Task CreateBaasAppsAsync() { _baasClient = await BaasClient.Atlas(BaasUri!, differentiator, TestHelpers.Output, cluster, apiKey, privateApiKey, groupId); } - else - { - _baasClient = await BaasClient.Docker(BaasUri!, "local", TestHelpers.Output); - } - _apps = await _baasClient.GetOrCreateApps(); + _apps = await _baasClient!.GetOrCreateApps(); } public static Task SetRecoveryModeOnServer(string appConfigType, bool enabled) diff --git a/Tools/DeployApps/BaasClient.cs b/Tools/DeployApps/BaasClient.cs index 5559b819b3..25e9c0f9a4 100644 --- a/Tools/DeployApps/BaasClient.cs +++ b/Tools/DeployApps/BaasClient.cs @@ -150,6 +150,8 @@ public class FunctionReturn private readonly HttpClient _client = new(); + private readonly string? _mongodbConnString; + private readonly string? _clusterName; private readonly TextWriter _output; @@ -190,18 +192,24 @@ static BaasClient() BsonSerializer.RegisterSerializer(new ObjectSerializer(_ => true)); } - private BaasClient(Uri baseUri, string differentiator, TextWriter output, string? clusterName = null) + private BaasClient( + Uri baseUri, + string differentiator, + TextWriter output, + string? clusterName = null, + string? mongodbConnString = null) { _client.BaseAddress = new Uri(baseUri, "api/admin/v3.0/"); _client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json"); _clusterName = clusterName; + _mongodbConnString = mongodbConnString; Differentiator = differentiator; _output = output; } - public static async Task Docker(Uri baseUri, string differentiator, TextWriter output) + public static async Task Docker(Uri baseUri, string differentiator, TextWriter output, string? mongodbConnString) { - var result = new BaasClient(baseUri, differentiator, output); + var result = new BaasClient(baseUri, differentiator, output, mongodbConnString: mongodbConnString); await result.Authenticate("local-userpass", new { @@ -217,7 +225,7 @@ public static async Task Docker(Uri baseUri, string differentiator, public static async Task Atlas(Uri baseUri, string differentiator, TextWriter output, string clusterName, string apiKey, string privateApiKey, string groupId) { - var result = new BaasClient(baseUri, differentiator, output, clusterName); + var result = new BaasClient(baseUri, differentiator, output, clusterName: clusterName); await result.Authenticate("mongodb-cloud", new { username = apiKey, @@ -235,6 +243,8 @@ private class BaasArgs public string? BaasUrl { get; set; } + public string? MongodbConnectionString { get; set; } + public string? BaasCluster { get; set; } public string? BaasApiKey { get; set; } @@ -266,7 +276,7 @@ private class BaasArgs if (!string.IsNullOrEmpty(extracted.BaasaasApiKey)) { baseUri = await GetOrDeployContainer(extracted.BaasaasApiKey!, differentiator, output); - client = await Docker(baseUri, differentiator, output); + client = await Docker(baseUri, differentiator, output, extracted.MongodbConnectionString); } else { @@ -278,7 +288,7 @@ private class BaasArgs baseUri = new Uri(extracted.BaasUrl!); client = extracted.UseDocker - ? await Docker(baseUri, differentiator, output) + ? await Docker(baseUri, differentiator, output, extracted.MongodbConnectionString) : await Atlas(baseUri, differentiator, output, extracted.BaasCluster, extracted.BaasApiKey, extracted.BaasPrivateApiKey, extracted.BaasProjectId); } @@ -673,10 +683,12 @@ private async Task CreateService(BaasApp app, string name, string type, private async Task CreateMongodbService(BaasApp app, object syncConfig) { - var serviceName = _clusterName == null ? "mongodb" : "mongodb-atlas"; - object mongoConfig = _clusterName == null ? new { uri = "mongodb://localhost:26000" } : new { clusterName = _clusterName }; + var datasourceType = _clusterName == null ? "mongodb" : "mongodb-atlas"; + object mongoConfig = _clusterName == null + ? new { uri = _mongodbConnString ?? "mongodb://localhost:26000" } + : new { clusterName = _clusterName }; - var mongoServiceId = await CreateService(app, "BackingDB", serviceName, mongoConfig); + var mongoServiceId = await CreateService(app, "BackingDB", datasourceType, mongoConfig); // The cluster linking must be separated from enabling sync because Atlas // takes a few seconds to provision a user for BaaS, meaning enabling sync