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

Add cosmosdb setup for github actions #34

Merged
merged 14 commits into from
Dec 1, 2024
24 changes: 20 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ on:
branches: [ "master" ]
pull_request:
branches: [ "master" ]


concurrency:
group: ci-tests
cancel-in-progress: false

jobs:
build:

Expand All @@ -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/[email protected]
# with:
# creds: ${{ secrets.AZURE_CREDENTIALS }}
# - name: Setup Cosmos DB
# uses: Particular/[email protected]
# 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
28 changes: 14 additions & 14 deletions Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,15 +12,15 @@
namespace Annoy_o_Bot.AcceptanceTests;

[Collection("CosmosDB")]
public class AcceptanceTest
public class AcceptanceTest(CosmosFixture cosmosFixture) : IAsyncLifetime
{
protected const string SignatureKey = "mysecretkey";

protected ConfigurationBuilder configurationBuilder;

protected Container container;

public AcceptanceTest(CosmosFixture cosmosFixture)
public async Task InitializeAsync()
{
configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(new Dictionary<string, string>
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion Annoy-o-Bot.CosmosDB.Tests/CosmosFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
/*
Expand All @@ -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);
}
}
Expand Down
24 changes: 15 additions & 9 deletions Annoy-o-Bot.CosmosDB.Tests/CosmosWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

namespace Annoy_o_Bot.CosmosDB.Tests;

public class CosmosWrapperTests : IClassFixture<CosmosFixture>
public class CosmosWrapperTests(CosmosFixture cosmosFixture) : IClassFixture<CosmosFixture>, IAsyncLifetime
{
Container DocumentClient;

CosmosClientWrapper CosmosWrapper;

ReminderDefinition reminderDefinition = new()
Expand All @@ -20,23 +21,28 @@ public class CosmosWrapperTests : IClassFixture<CosmosFixture>
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]
Expand Down
Loading