Skip to content

Commit

Permalink
Make mongodb connection string used for tests configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kmorkos committed Jul 25, 2024
1 parent 5ae28a6 commit d8bb4b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
8 changes: 2 additions & 6 deletions Tests/Realm.Tests/Sync/SyncTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand All @@ -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)
Expand Down
30 changes: 21 additions & 9 deletions Tools/DeployApps/BaasClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public class FunctionReturn

private readonly HttpClient _client = new();

private readonly string? _mongodbConnString;

private readonly string? _clusterName;

private readonly TextWriter _output;
Expand Down Expand Up @@ -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<BaasClient> Docker(Uri baseUri, string differentiator, TextWriter output)
public static async Task<BaasClient> 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
{
Expand All @@ -217,7 +225,7 @@ public static async Task<BaasClient> Docker(Uri baseUri, string differentiator,

public static async Task<BaasClient> 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,
Expand All @@ -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; }
Expand Down Expand Up @@ -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
{
Expand All @@ -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);
}

Expand Down Expand Up @@ -673,10 +683,12 @@ private async Task<string> CreateService(BaasApp app, string name, string type,

private async Task<string> 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
Expand Down

0 comments on commit d8bb4b5

Please sign in to comment.