diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1fb5557..7d6fce4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,11 @@ on: branches: [ "master" ] pull_request: branches: [ "master" ] - + +concurrency: + group: ci-tests + cancel-in-progress: false + jobs: build: @@ -24,6 +28,18 @@ jobs: run: dotnet restore - name: Build run: dotnet build --no-restore -# Requires a cosmosdb instance to run acceptance tests -# - name: Test -# run: dotnet test --no-build --verbosity normal +# - name: Azure login +# uses: azure/login@v2.2.0 +# with: +# creds: ${{ secrets.AZURE_CREDENTIALS }} +# - name: Setup Cosmos DB +# uses: Particular/setup-cosmosdb-action@v1.0.0 +# with: +# connection-string-name: Annoy_O_Bot_ComsmosConnectionString +# azure-credentials: ${{ secrets.AZURE_CREDENTIALS }} +# tag: Annoy_O_Bot_Tests + - name: Test + env: + CosmosDBConnectionString: ${{ secrets.CosmosDBConnectionString }} + # current using MaxCpuCount 1 as both CosmosDB.Tests and AcceptanceTests use the same cosmos DB instance and tests fail otherwise + run: dotnet test --no-build --verbosity normal -maxcpucount:1 diff --git a/Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs b/Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs index 0d800bb..61cb0f3 100644 --- a/Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs +++ b/Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs @@ -1,5 +1,4 @@ -using System.Net; -using System.Security.Cryptography; +using System.Security.Cryptography; using System.Text; using Annoy_o_Bot.CosmosDB; using Annoy_o_Bot.GitHub; @@ -13,7 +12,7 @@ namespace Annoy_o_Bot.AcceptanceTests; [Collection("CosmosDB")] -public class AcceptanceTest +public class AcceptanceTest(CosmosFixture cosmosFixture) : IAsyncLifetime { protected const string SignatureKey = "mysecretkey"; @@ -21,7 +20,7 @@ public class AcceptanceTest protected Container container; - public AcceptanceTest(CosmosFixture cosmosFixture) + public async Task InitializeAsync() { configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(new Dictionary @@ -30,25 +29,26 @@ public AcceptanceTest(CosmosFixture cosmosFixture) }); container = cosmosFixture.CreateDocumentClient(); - SetupCollection().GetAwaiter().GetResult(); + await SetupCollection(); } - async Task SetupCollection() + public async Task DisposeAsync() { - var database = container.Database; try { - await database.GetContainer(CosmosClientWrapper.collectionId).DeleteContainerAsync(); + await container.DeleteContainerAsync(); } - catch (CosmosException e) + catch (Exception) { - if (e.StatusCode != HttpStatusCode.NotFound) - { - throw; - } + // ignored } + } - await database.CreateContainerAsync(CosmosClientWrapper.collectionId, "/id"); + async Task SetupCollection() + { + var database = container.Database; + await database.Client.CreateDatabaseIfNotExistsAsync(CosmosClientWrapper.dbName); + await database.CreateContainerIfNotExistsAsync(CosmosClientWrapper.collectionId, "/id"); } protected async Task CreateDueReminders(IGitHubApi gitHubApi) diff --git a/Annoy-o-Bot.CosmosDB.Tests/CosmosFixture.cs b/Annoy-o-Bot.CosmosDB.Tests/CosmosFixture.cs index 1975108..75360a6 100644 --- a/Annoy-o-Bot.CosmosDB.Tests/CosmosFixture.cs +++ b/Annoy-o-Bot.CosmosDB.Tests/CosmosFixture.cs @@ -7,6 +7,8 @@ namespace Annoy_o_Bot.AcceptanceTests; public class CosmosFixture { + const string EmulatorConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; + public Container CreateDocumentClient() { /* @@ -20,7 +22,8 @@ public Container CreateDocumentClient() Serializer = new WorkerCosmosSerializer() }; // CosmosDB emulator connection settings - var cosmosClient = new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", options); + var cosmosConnectinString = Environment.GetEnvironmentVariable("CosmosDBConnectionString") ?? EmulatorConnectionString; + var cosmosClient = new CosmosClient(cosmosConnectinString, options); return cosmosClient.GetContainer(CosmosClientWrapper.dbName, CosmosClientWrapper.collectionId); } } diff --git a/Annoy-o-Bot.CosmosDB.Tests/CosmosWrapperTests.cs b/Annoy-o-Bot.CosmosDB.Tests/CosmosWrapperTests.cs index 2c43630..de0d299 100644 --- a/Annoy-o-Bot.CosmosDB.Tests/CosmosWrapperTests.cs +++ b/Annoy-o-Bot.CosmosDB.Tests/CosmosWrapperTests.cs @@ -5,9 +5,10 @@ namespace Annoy_o_Bot.CosmosDB.Tests; -public class CosmosWrapperTests : IClassFixture +public class CosmosWrapperTests(CosmosFixture cosmosFixture) : IClassFixture, IAsyncLifetime { Container DocumentClient; + CosmosClientWrapper CosmosWrapper; ReminderDefinition reminderDefinition = new() @@ -20,23 +21,28 @@ public class CosmosWrapperTests : IClassFixture Title = "demo title" }; - public CosmosWrapperTests(CosmosFixture cosmosFixture) + public async Task InitializeAsync() { DocumentClient = cosmosFixture.CreateDocumentClient(); + CosmosWrapper = new CosmosClientWrapper(DocumentClient); + + await DocumentClient.Database.Client.CreateDatabaseIfNotExistsAsync(CosmosClientWrapper.dbName); try { - DocumentClient.DeleteContainerAsync().GetAwaiter().GetResult(); + await DocumentClient.DeleteContainerAsync(); } - catch (CosmosException e) + catch (CosmosException e) when (e.StatusCode == HttpStatusCode.NotFound) { - if (e.StatusCode != HttpStatusCode.NotFound) - { - throw; - } } - DocumentClient.Database.CreateContainerAsync(new ContainerProperties(CosmosClientWrapper.collectionId, "/id")).GetAwaiter().GetResult(); + await DocumentClient.Database.CreateContainerIfNotExistsAsync( + new ContainerProperties(CosmosClientWrapper.collectionId, "/id")); + } + + public Task DisposeAsync() + { + return Task.CompletedTask; } [Fact]