-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add go feature flag integration tests
- Loading branch information
Showing
14 changed files
with
783 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/CommunityToolkit.Aspire.GoFeatureFlag.Tests/AspireGoFeatureFlagClientExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Components.Common.Tests; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Hosting; | ||
using OpenFeature.Contrib.Providers.GOFeatureFlag; | ||
|
||
namespace CommunityToolkit.Aspire.GoFeatureFlag.Tests; | ||
|
||
public class AspireGoFeatureFlagClientExtensionsTest(GoFeatureFlagContainerFixture containerFixture) : IClassFixture<GoFeatureFlagContainerFixture> | ||
{ | ||
private const string DefaultConnectionName = "goff"; | ||
|
||
private string DefaultConnectionString => | ||
RequiresDockerAttribute.IsSupported ? containerFixture.GetConnectionString() : "http://localhost:27011"; | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
[RequiresDocker] | ||
public async Task AddGoFeatureFlagClient_HealthCheckShouldBeRegisteredWhenEnabled(bool useKeyed) | ||
{ | ||
var key = DefaultConnectionName; | ||
|
||
var builder = CreateBuilder(DefaultConnectionString); | ||
|
||
if (useKeyed) | ||
{ | ||
builder.AddKeyedGoFeatureFlagClient(key, settings => | ||
{ | ||
settings.DisableHealthChecks = false; | ||
}); | ||
} | ||
else | ||
{ | ||
builder.AddGoFeatureFlagClient(DefaultConnectionName, settings => | ||
{ | ||
settings.DisableHealthChecks = false; | ||
}); | ||
} | ||
|
||
using var host = builder.Build(); | ||
|
||
var healthCheckService = host.Services.GetRequiredService<HealthCheckService>(); | ||
|
||
var healthCheckReport = await healthCheckService.CheckHealthAsync(); | ||
|
||
var healthCheckName = useKeyed ? $"Goff_{key}" : "Goff"; | ||
|
||
Assert.Contains(healthCheckReport.Entries, x => x.Key == healthCheckName); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void AddGoFeatureFlagClient_HealthCheckShouldNotBeRegisteredWhenDisabled(bool useKeyed) | ||
{ | ||
var builder = CreateBuilder(DefaultConnectionString); | ||
|
||
if (useKeyed) | ||
{ | ||
builder.AddKeyedGoFeatureFlagClient(DefaultConnectionName, settings => | ||
{ | ||
settings.DisableHealthChecks = true; | ||
}); | ||
} | ||
else | ||
{ | ||
builder.AddGoFeatureFlagClient(DefaultConnectionName, settings => | ||
{ | ||
settings.DisableHealthChecks = true; | ||
}); | ||
} | ||
|
||
using var host = builder.Build(); | ||
|
||
var healthCheckService = host.Services.GetService<HealthCheckService>(); | ||
|
||
Assert.Null(healthCheckService); | ||
} | ||
|
||
[Fact] | ||
public void CanAddMultipleKeyedServices() | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>("ConnectionStrings:goff1", "http://localhost:19530"), | ||
new KeyValuePair<string, string?>("ConnectionStrings:goff2", "http://localhost:19531"), | ||
new KeyValuePair<string, string?>("ConnectionStrings:goff3", "http://localhost:19532"), | ||
]); | ||
|
||
builder.AddGoFeatureFlagClient("goff1"); | ||
builder.AddKeyedGoFeatureFlagClient("goff2"); | ||
builder.AddKeyedGoFeatureFlagClient("goff3"); | ||
|
||
using var host = builder.Build(); | ||
|
||
var client1 = host.Services.GetRequiredService<GoFeatureFlagProvider>(); | ||
var client2 = host.Services.GetRequiredKeyedService<GoFeatureFlagProvider>("goff2"); | ||
var client3 = host.Services.GetRequiredKeyedService<GoFeatureFlagProvider>("goff3"); | ||
|
||
Assert.NotSame(client1, client2); | ||
Assert.NotSame(client1, client3); | ||
Assert.NotSame(client2, client3); | ||
} | ||
|
||
[Fact] | ||
public void CanAddClientFromEncodedConnectionString() | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
|
||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>("ConnectionStrings:goff1", "Endpoint=http://localhost:19530"), | ||
new KeyValuePair<string, string?>("ConnectionStrings:goff2", "Endpoint=http://localhost:19531"), | ||
]); | ||
|
||
builder.AddGoFeatureFlagClient("goff1"); | ||
builder.AddKeyedGoFeatureFlagClient("goff2"); | ||
|
||
using var host = builder.Build(); | ||
|
||
var client1 = host.Services.GetRequiredService<GoFeatureFlagProvider>(); | ||
var client2 = host.Services.GetRequiredKeyedService<GoFeatureFlagProvider>("goff2"); | ||
|
||
Assert.NotSame(client1, client2); | ||
} | ||
|
||
private static HostApplicationBuilder CreateBuilder(string connectionString) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
|
||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>($"ConnectionStrings:{DefaultConnectionName}", connectionString) | ||
]); | ||
return builder; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...nityToolkit.Aspire.GoFeatureFlag.Tests/CommunityToolkit.Aspire.GoFeatureFlag.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Testcontainers" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\CommunityToolkit.Aspire.GoFeatureFlag\CommunityToolkit.Aspire.GoFeatureFlag.csproj" /> | ||
<ProjectReference Include="..\CommunityToolkit.Aspire.Testing\CommunityToolkit.Aspire.Testing.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="$(RepoRoot)src\CommunityToolkit.Aspire.Hosting.GoFeatureFlag\GoFeatureFlagContainerImageTags.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="goff\*.yaml"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
15 changes: 15 additions & 0 deletions
15
tests/CommunityToolkit.Aspire.GoFeatureFlag.Tests/ConfigurationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace CommunityToolkit.Aspire.GoFeatureFlag.Tests; | ||
|
||
public class ConfigurationTests | ||
{ | ||
[Fact] | ||
public void EndpointIsNullByDefault() => | ||
Assert.Null(new GoFeatureFlagClientSettings().Endpoint); | ||
|
||
[Fact] | ||
public void HealthChecksEnabledByDefault() => | ||
Assert.False(new GoFeatureFlagClientSettings().DisableHealthChecks); | ||
} |
115 changes: 115 additions & 0 deletions
115
tests/CommunityToolkit.Aspire.GoFeatureFlag.Tests/ConformanceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Components.Common.Tests; | ||
using Aspire.Components.ConformanceTests; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using OpenFeature.Contrib.Providers.GOFeatureFlag; | ||
using OpenFeature.Model; | ||
|
||
namespace CommunityToolkit.Aspire.GoFeatureFlag.Tests; | ||
|
||
public class ConformanceTests : ConformanceTests<GoFeatureFlagProvider, GoFeatureFlagClientSettings>, IClassFixture<GoFeatureFlagContainerFixture> | ||
{ | ||
private readonly GoFeatureFlagContainerFixture _containerFixture; | ||
|
||
protected override ServiceLifetime ServiceLifetime => ServiceLifetime.Singleton; | ||
|
||
protected override string ActivitySourceName => string.Empty; | ||
|
||
protected override string[] RequiredLogCategories => []; | ||
|
||
protected override bool CanConnectToServer => RequiresDockerAttribute.IsSupported; | ||
|
||
protected override bool SupportsKeyedRegistrations => true; | ||
|
||
public ConformanceTests(GoFeatureFlagContainerFixture containerFixture) | ||
{ | ||
_containerFixture = containerFixture; | ||
} | ||
|
||
protected override void PopulateConfiguration(ConfigurationManager configuration, string? key = null) | ||
{ | ||
var connectionString = RequiresDockerAttribute.IsSupported ? | ||
$"{_containerFixture.GetConnectionString()}" : | ||
"Endpoint=http://localhost:27017"; | ||
|
||
configuration.AddInMemoryCollection( | ||
[ | ||
new KeyValuePair<string, string?>(CreateConfigKey("Aspire:GoFeatureFlag:Client", key, "Endpoint"), GetConnectionStringKeyValue(connectionString,"Endpoint")), | ||
new KeyValuePair<string, string?>($"ConnectionStrings:{key}", $"{connectionString}") | ||
]); | ||
} | ||
|
||
internal static string GetConnectionStringKeyValue(string connectionString, string configKey) | ||
{ | ||
// from the connection string, extract the key value of the configKey | ||
var parts = connectionString.Split(';'); | ||
foreach (var part in parts) | ||
{ | ||
var keyValue = part.Split('='); | ||
if (keyValue.Length == 2 && keyValue[0].Equals(configKey, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return keyValue[1]; | ||
} | ||
} | ||
return string.Empty; | ||
} | ||
|
||
protected override void RegisterComponent(HostApplicationBuilder builder, Action<GoFeatureFlagClientSettings>? configure = null, string? key = null) | ||
{ | ||
if (key is null) | ||
{ | ||
builder.AddGoFeatureFlagClient("goff", configureSettings: configure); | ||
} | ||
else | ||
{ | ||
builder.AddKeyedGoFeatureFlagClient(key, configureSettings: configure); | ||
} | ||
} | ||
|
||
protected override string ValidJsonConfig => """ | ||
{ | ||
"Aspire": { | ||
"GoFeatureFlag": { | ||
"Client": { | ||
"Endpoint": "http://localhost:19530" | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
protected override (string json, string error)[] InvalidJsonToErrorMessage => new[] | ||
{ | ||
("""{"Aspire": { "GoFeatureFlag":{ "Client": { "Endpoint": 3 }}}}""", "Value is \"integer\" but should be \"string\""), | ||
("""{"Aspire": { "GoFeatureFlag":{ "Client": { "Endpoint": "hello" }}}}""", "Value does not match format \"uri\"") | ||
}; | ||
|
||
protected override void SetHealthCheck(GoFeatureFlagClientSettings options, bool enabled) | ||
{ | ||
options.DisableHealthChecks = !enabled; | ||
} | ||
|
||
protected override void SetMetrics(GoFeatureFlagClientSettings options, bool enabled) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void SetTracing(GoFeatureFlagClientSettings options, bool enabled) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void TriggerActivity(GoFeatureFlagProvider service) | ||
{ | ||
using var source = new CancellationTokenSource(100); | ||
|
||
var context = EvaluationContext.Builder() | ||
.Set("targetingKey", Guid.NewGuid().ToString()) | ||
.Set("anonymous", true) | ||
.Build(); | ||
service.InitializeAsync(context, source.Token).Wait(); | ||
} | ||
} |
Oops, something went wrong.