From 87bfae0eb924b4e6b0ca8037257c8f257662ba67 Mon Sep 17 00:00:00 2001 From: Tim Bussmann <3524870+timbussmann@users.noreply.github.com> Date: Tue, 13 Aug 2024 22:21:48 +0200 Subject: [PATCH 1/2] Create build.yml --- .github/workflows/build.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..1fb5557 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,29 @@ +# This workflow will build a .NET project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net + +name: .NET + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + - name: Restore dependencies + 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 From 71ad323ccc974a79c0ceedc2e1a10b2887130e04 Mon Sep 17 00:00:00 2001 From: Tim Bussmann <3524870+timbussmann@users.noreply.github.com> Date: Sun, 1 Dec 2024 22:21:25 +0100 Subject: [PATCH 2/2] Add cosmosdb setup for github actions (#34) Co-authored-by: Tim Bussmann --- .github/workflows/build.yml | 24 +++++++++++++--- Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs | 28 +++++++++---------- Annoy-o-Bot.CosmosDB.Tests/CosmosFixture.cs | 5 +++- .../CosmosWrapperTests.cs | 24 ++++++++++------ 4 files changed, 53 insertions(+), 28 deletions(-) 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]