diff --git a/Directory.Packages.props b/Directory.Packages.props index aef0ebe..56e0d4c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -40,6 +40,7 @@ + diff --git a/infra/modules/aci-mongodb.bicep b/infra/modules/aci-mongodb.bicep deleted file mode 100644 index fb6c954..0000000 --- a/infra/modules/aci-mongodb.bicep +++ /dev/null @@ -1,74 +0,0 @@ -targetScope = 'resourceGroup' - -@secure() -@description('The password for the administrator') -param administratorPassword string - -@description('The username for the administrator') -param administratorUsername string = 'tester' - -@description('The image URI to use.') -param image string = 'mongo' - -@minLength(1) -@description('Primary location for all resources') -param location string = resourceGroup().location - -@description('The port # to expose in the Docker image') -param port int = 27017 - -@description('The name of the Mongo Server to create.') -param serverName string - -@description('The list of tags to apply to all resources.') -param tags object = {} - -/*********************************************************************************/ - -resource containerGroup 'Microsoft.ContainerInstance/containerGroups@2023-05-01' = { - name: toLower(serverName) - location: location - tags: tags - properties: { - containers: [ - { - name: toLower(serverName) - properties: { - image: image - environmentVariables: [ - { name: 'MONGO_INITDB_ROOT_USERNAME', value: administratorUsername } - { name: 'MONGO_INITDB_ROOT_PASSWORD', secureValue: administratorPassword } - ] - ports: [ - { - port: port - protocol: 'TCP' - } - ] - resources: { - requests: { - cpu: 2 - memoryInGB: 2 - } - } - } - } - ] - osType: 'Linux' - restartPolicy: 'Always' - ipAddress: { - type: 'Public' - ports: [ - { - port: port - protocol: 'TCP' - } - ] - } - } -} - -/*********************************************************************************/ - -#disable-next-line outputs-should-not-contain-secrets -output MONGO_CONNECTIONSTRING string = 'mongodb://${administratorUsername}:${administratorPassword}@${containerGroup.properties.ipAddress.ip}:27017/' diff --git a/infra/resources.bicep b/infra/resources.bicep index 6457e00..a43638e 100644 --- a/infra/resources.bicep +++ b/infra/resources.bicep @@ -88,17 +88,6 @@ module mongodb './modules/cosmos-mongodb.bicep' = { } } -module mongoaci './modules/aci-mongodb.bicep' = { - name: 'mongoaci-deployment-${resourceToken}' - params: { - location: location - tags: tags - serverName: mongoaciServerName - administratorPassword: sqlAdminPassword - - } -} - module app_service './modules/appservice.bicep' = { name: 'appsvc-deployment-${resourceToken}' params: { @@ -121,5 +110,4 @@ module app_service './modules/appservice.bicep' = { output AZSQL_CONNECTIONSTRING string = azuresql.outputs.AZSQL_CONNECTIONSTRING output COSMOS_CONNECTIONSTRING string = cosmos.outputs.COSMOS_CONNECTIONSTRING output MONGO_CONNECTIONSTRING string = mongodb.outputs.MONGO_CONNECTIONSTRING -output MONGOACI_CONNECTIONSTRING string = mongoaci.outputs.MONGO_CONNECTIONSTRING output SERVICE_ENDPOINT string = app_service.outputs.SERVICE_ENDPOINT diff --git a/tests/CommunityToolkit.Datasync.Server.MongoDB.Test/MongoDBRepository_Tests.cs b/tests/CommunityToolkit.Datasync.Server.MongoDB.Test/MongoDBRepository_Tests.cs index 6bb294b..c417d61 100644 --- a/tests/CommunityToolkit.Datasync.Server.MongoDB.Test/MongoDBRepository_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.MongoDB.Test/MongoDBRepository_Tests.cs @@ -4,6 +4,7 @@ using CommunityToolkit.Datasync.TestCommon; using CommunityToolkit.Datasync.TestCommon.Databases; +using CommunityToolkit.Datasync.TestCommon.Fixtures; using Microsoft.EntityFrameworkCore; using MongoDB.Bson; using MongoDB.Driver; @@ -13,7 +14,7 @@ namespace CommunityToolkit.Datasync.Server.MongoDB.Test; [ExcludeFromCodeCoverage] -public class MongoDBRepository_Tests(ITestOutputHelper output) : RepositoryTests(), IAsyncLifetime +public class MongoDBRepository_Tests(MongoDatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests(), IClassFixture, IAsyncLifetime { #region Setup private readonly Random random = new(); @@ -21,11 +22,8 @@ public class MongoDBRepository_Tests(ITestOutputHelper output) : RepositoryTests public async Task InitializeAsync() { - if (!string.IsNullOrEmpty(ConnectionStrings.MongoCommunity)) - { - Context = await MongoDBContext.CreateContextAsync(ConnectionStrings.MongoCommunity, output); - this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync(); - } + Context = await MongoDBContext.CreateContextAsync(fixture.ConnectionString, output); + this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync(); } public async Task DisposeAsync() @@ -38,7 +36,7 @@ public async Task DisposeAsync() public MongoDBContext Context { get; set; } - protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.CosmosDb); + protected override bool CanRunLiveTests() => true; protected override async Task GetEntityAsync(string id) => await Context.Movies.Find(Builders.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync(); diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Live/MongoDB_Controller_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Live/MongoDB_Controller_Tests.cs index 69d9b09..52d8a3a 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Live/MongoDB_Controller_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Live/MongoDB_Controller_Tests.cs @@ -5,6 +5,7 @@ using CommunityToolkit.Datasync.Server.MongoDB; using CommunityToolkit.Datasync.Server.Test.Helpers; using CommunityToolkit.Datasync.TestCommon.Databases; +using CommunityToolkit.Datasync.TestCommon.Fixtures; using MongoDB.Bson; using MongoDB.Driver; using Xunit.Abstractions; @@ -13,7 +14,7 @@ namespace CommunityToolkit.Datasync.Server.Test.Live; [ExcludeFromCodeCoverage] [Collection("LiveTestsCollection")] -public class MongoDB_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests(), IAsyncLifetime +public class MongoDB_Controller_Tests(MongoDatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests(), IClassFixture, IAsyncLifetime { #region Setup private readonly Random random = new(); @@ -21,16 +22,8 @@ public class MongoDB_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper public async Task InitializeAsync() { - if (!string.IsNullOrEmpty(ConnectionStrings.MongoCommunity)) - { - // Note: we don't clear entities on every run to speed up the test runs. This can only be done because - // the tests are read-only (associated with the query and get capabilities). If the test being run writes - // to the database then change clearEntities to true. - output.WriteLine($"MongoCommunityIsInitialized = {fixture.MongoCommunityIsInitialized}"); - Context = await MongoDBContext.CreateContextAsync(ConnectionStrings.MongoCommunity, output, clearEntities: !fixture.MongoCommunityIsInitialized); - this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync(); - fixture.MongoCommunityIsInitialized = true; - } + Context = await MongoDBContext.CreateContextAsync(fixture.ConnectionString, output); + this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync(); } public async Task DisposeAsync() @@ -45,7 +38,7 @@ public async Task DisposeAsync() protected override string DriverName { get; } = "MongoDB"; - protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.MongoCommunity); + protected override bool CanRunLiveTests() => true; protected override async Task GetEntityAsync(string id) => await Context.Movies.Find(Builders.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync(); diff --git a/tests/CommunityToolkit.Datasync.TestCommon/CommunityToolkit.Datasync.TestCommon.csproj b/tests/CommunityToolkit.Datasync.TestCommon/CommunityToolkit.Datasync.TestCommon.csproj index 89d1186..cf2bdbc 100644 --- a/tests/CommunityToolkit.Datasync.TestCommon/CommunityToolkit.Datasync.TestCommon.csproj +++ b/tests/CommunityToolkit.Datasync.TestCommon/CommunityToolkit.Datasync.TestCommon.csproj @@ -20,6 +20,7 @@ + diff --git a/tests/CommunityToolkit.Datasync.TestCommon/Databases/ConnectionStrings.cs b/tests/CommunityToolkit.Datasync.TestCommon/Databases/ConnectionStrings.cs index 4c0c324..16899e0 100644 --- a/tests/CommunityToolkit.Datasync.TestCommon/Databases/ConnectionStrings.cs +++ b/tests/CommunityToolkit.Datasync.TestCommon/Databases/ConnectionStrings.cs @@ -8,7 +8,6 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases; public static class ConnectionStrings { public static readonly string CosmosDb = Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING"); - public static readonly string MongoCommunity = Environment.GetEnvironmentVariable("MONGOACI_CONNECTION_STRING"); public static readonly string CosmosMongo = Environment.GetEnvironmentVariable("MONGO_CONNECTION_STRING"); public static readonly string Service = Environment.GetEnvironmentVariable("SERVICE_ENDPOINT"); public static readonly bool EnableLogging = (Environment.GetEnvironmentVariable("ENABLE_SQL_LOGGING") ?? "false") == "true"; diff --git a/tests/CommunityToolkit.Datasync.TestCommon/Fixtures/MongoDatabaseFixture.cs b/tests/CommunityToolkit.Datasync.TestCommon/Fixtures/MongoDatabaseFixture.cs new file mode 100644 index 0000000..a20e936 --- /dev/null +++ b/tests/CommunityToolkit.Datasync.TestCommon/Fixtures/MongoDatabaseFixture.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Testcontainers.MongoDb; +using Testcontainers.MsSql; +using Xunit; + +namespace CommunityToolkit.Datasync.TestCommon.Fixtures; + +[ExcludeFromCodeCoverage] +public class MongoDatabaseFixture : IAsyncLifetime +{ + private readonly MongoDbContainer _container; + + public MongoDatabaseFixture() + { + this._container = new MongoDbBuilder() + .WithImage("mongo:latest") + .Build(); + } + + /// + public async Task DisposeAsync() + { + if (this._container is not null) + { + await this._container.DisposeAsync(); + } + } + + /// + public async Task InitializeAsync() + { + await this._container.StartAsync(); + ConnectionString = this._container.GetConnectionString(); + } + + /// + /// The connection string for the MySQL database. + /// + public string ConnectionString { get; private set; } = string.Empty; +}