- Implemented `MongoIndexModelTests` to verify index models for various stores. - Created `OpenApiMetadataFactory` with methods to generate OpenAPI metadata. - Added tests for `OpenApiMetadataFactory` to ensure expected defaults and URL overrides. - Introduced `ObserverSurfaceSecrets` and `WebhookSurfaceSecrets` for managing secrets. - Developed `RuntimeSurfaceFsClient` and `WebhookSurfaceFsClient` for manifest retrieval. - Added dependency injection tests for `SurfaceEnvironmentRegistration` in both Observer and Webhook contexts. - Implemented tests for secret resolution in `ObserverSurfaceSecretsTests` and `WebhookSurfaceSecretsTests`. - Created `EnsureLinkNotMergeCollectionsMigrationTests` to validate MongoDB migration logic. - Added project files for MongoDB tests and NuGet package mirroring.
71 lines
2.5 KiB
C#
71 lines
2.5 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using Mongo2Go;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using StellaOps.Concelier.Storage.Mongo.Migrations;
|
|
|
|
namespace StellaOps.Concelier.Storage.Mongo.Tests;
|
|
|
|
public sealed class EnsureLinkNotMergeCollectionsMigrationTests : IAsyncLifetime
|
|
{
|
|
private MongoDbRunner _runner = null!;
|
|
private IMongoDatabase _database = null!;
|
|
|
|
public Task InitializeAsync()
|
|
{
|
|
_runner = MongoDbRunner.Start(singleNodeReplSet: true);
|
|
var client = new MongoClient(_runner.ConnectionString);
|
|
_database = client.GetDatabase("lnm-migration-tests");
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task DisposeAsync()
|
|
{
|
|
_runner.Dispose();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreatesCollectionsAndIndexesIdempotently()
|
|
{
|
|
var migration = new EnsureLinkNotMergeCollectionsMigration();
|
|
|
|
await migration.ApplyAsync(_database, CancellationToken.None);
|
|
await migration.ApplyAsync(_database, CancellationToken.None); // idempotent second run
|
|
|
|
var collections = await _database.ListCollectionNames().ToListAsync();
|
|
collections.Should().Contain(new[]
|
|
{
|
|
MongoStorageDefaults.Collections.AdvisoryObservations,
|
|
MongoStorageDefaults.Collections.AdvisoryLinksets
|
|
});
|
|
|
|
var linksetIndexes = await _database
|
|
.GetCollection<BsonDocument>(MongoStorageDefaults.Collections.AdvisoryLinksets)
|
|
.Indexes.List()
|
|
.ToListAsync();
|
|
|
|
linksetIndexes.Should().ContainSingle(i => i["name"] == "linkset_tenant_advisory_source" && i["unique"].AsBoolean);
|
|
|
|
var obsIndexes = await _database
|
|
.GetCollection<BsonDocument>(MongoStorageDefaults.Collections.AdvisoryObservations)
|
|
.Indexes.List()
|
|
.ToListAsync();
|
|
|
|
obsIndexes.Should().Contain(i => i["name"] == "obs_prov_sourceArtifactSha_unique" && i["unique"].AsBoolean);
|
|
|
|
var linksetValidator = await GetValidatorAsync(MongoStorageDefaults.Collections.AdvisoryLinksets);
|
|
linksetValidator.Should().NotBeNull();
|
|
}
|
|
|
|
private async Task<BsonDocument?> GetValidatorAsync(string collection)
|
|
{
|
|
var filter = new BsonDocument("name", collection);
|
|
var cursor = await _database.ListCollectionsAsync(new ListCollectionsOptions { Filter = filter });
|
|
var doc = await cursor.FirstOrDefaultAsync();
|
|
return doc?"options"?"validator"?.AsBsonDocument;
|
|
}
|
|
}
|