Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNET-1168: Make mongodb connection string used for tests configurable #3654

Closed
wants to merge 1 commit into from
Closed
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
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);
}
Comment on lines -197 to -200
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, this client would always match the one initialized by ExtractBaasSettingsAsync so avoiding threading the connection string here, but lmk if there's some other workflow that relies on this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invoked when testing against a local baas docker image, so we should probably bring it back, even if most of the time people test against baasaas.


_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
Loading