-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
todo: shutdown app cleanly
- Loading branch information
Showing
5 changed files
with
212 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Provider-Pact-Verification | ||
on: | ||
push: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
env: | ||
application_folder_consumer: smartbearcoin-payments-ui | ||
application_folder_provider_tests: smartbearcoin-payments-ui | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
- run: cd ${{ env.application_folder_consumer }} && npm ci | ||
- run: cd ${{ env.application_folder_consumer }} && npm test | ||
- run: cd ${{ env.application_folder_provider_tests }} && dotnet test | ||
env: | ||
PACT_URL: ../../../../smartbearcoin-payments-ui/pacts/SmartBearCoin-Payments-UI-SmartBearCoin-Payee-Provider.json |
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,90 @@ | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using PactNet.Infrastructure.Outputters; | ||
using PactNet.Output.Xunit; | ||
using PactNet.Verifier; | ||
using PactNet; | ||
using Xunit.Abstractions; | ||
|
||
namespace provider_azure_function_tests; | ||
|
||
public class ProviderApiTests : IDisposable | ||
{ | ||
private string _providerUri { get; } | ||
private ITestOutputHelper _outputHelper { get; } | ||
private System.Threading.Tasks.Task<TemporaryAzureFunctionsApplication> _app { get; } | ||
|
||
public ProviderApiTests(ITestOutputHelper output) | ||
{ | ||
_outputHelper = output; | ||
_providerUri = "http://localhost:7071/api"; | ||
_app = TemporaryAzureFunctionsApplication.StartNewAsync(new DirectoryInfo("../../../../provider_azure_function")); | ||
} | ||
|
||
[Fact] | ||
public void EnsureProviderApiHonoursPactWithConsumer() | ||
{ | ||
|
||
// Wait for the Azure Functions application to start | ||
_app.Wait(15000); | ||
|
||
// Arrange | ||
var config = new PactVerifierConfig | ||
{ | ||
|
||
// NOTE: We default to using a ConsoleOutput, | ||
// however xUnit 2 does not capture the console output, | ||
// so a custom outputter is required. | ||
Outputters = new List<IOutput> | ||
{ | ||
new XunitOutput(_outputHelper), | ||
new ConsoleOutput() | ||
}, | ||
|
||
// Output verbose verification logs to the test output | ||
LogLevel = PactLogLevel.Information, | ||
}; | ||
|
||
string providerName = "SmartBearCoin-Payee-Provider"; | ||
IPactVerifier pactVerifier = new PactVerifier(providerName, config); | ||
string pactUrl = Environment.GetEnvironmentVariable("PACT_URL"); | ||
|
||
pactVerifier.WithHttpEndpoint(new Uri(_providerUri)) | ||
.WithFileSource(new FileInfo(pactUrl)) | ||
.Verify(); | ||
|
||
// _app.Dispose(); | ||
|
||
} | ||
|
||
#region IDisposable Support | ||
|
||
private bool _disposed = false; // To detect redundant calls | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (disposing) | ||
{ | ||
Console.WriteLine("SAF IS A BOSS"); | ||
_app.Dispose(); | ||
} | ||
|
||
_disposed = true; | ||
} | ||
|
||
// This code added to correctly implement the disposable pattern. | ||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in Dispose(bool disposing) above. | ||
Dispose(true); | ||
|
||
GC.SuppressFinalize(this); | ||
} | ||
|
||
#endregion | ||
} |
67 changes: 67 additions & 0 deletions
67
provider_azure_function_tests/TemporaryAzureFunctionsApplication.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,67 @@ | ||
using System.Diagnostics; | ||
using Polly; | ||
using Polly.Retry; | ||
|
||
public class TemporaryAzureFunctionsApplication : IAsyncDisposable | ||
{ | ||
private readonly Process _application; | ||
private static readonly HttpClient HttpClient = new HttpClient(); | ||
|
||
private TemporaryAzureFunctionsApplication(Process application) | ||
{ | ||
_application = application; | ||
} | ||
|
||
public static async Task<TemporaryAzureFunctionsApplication> StartNewAsync(DirectoryInfo projectDirectory) | ||
{ | ||
int port = 7071; | ||
Process app = StartApplication(port, projectDirectory); | ||
await WaitUntilTriggerIsAvailableAsync($"http://localhost:{port}/"); | ||
|
||
return new TemporaryAzureFunctionsApplication(app); | ||
} | ||
|
||
private static Process StartApplication(int port, DirectoryInfo projectDirectory) | ||
{ | ||
var appInfo = new ProcessStartInfo("func", $"start --port {port}") | ||
{ | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
WorkingDirectory = projectDirectory.FullName | ||
}; | ||
|
||
var app = new Process { StartInfo = appInfo }; | ||
app.Start(); | ||
return app; | ||
} | ||
|
||
private static async Task WaitUntilTriggerIsAvailableAsync(string endpoint) | ||
{ | ||
AsyncRetryPolicy retryPolicy = | ||
Policy.Handle<Exception>() | ||
.WaitAndRetryForeverAsync(index => TimeSpan.FromMilliseconds(500)); | ||
|
||
PolicyResult<HttpResponseMessage> result = | ||
await Policy.TimeoutAsync(TimeSpan.FromSeconds(30)) | ||
.WrapAsync(retryPolicy) | ||
.ExecuteAndCaptureAsync(() => HttpClient.GetAsync(endpoint)); | ||
|
||
if (result.Outcome == OutcomeType.Failure) | ||
{ | ||
throw new InvalidOperationException( | ||
"The Azure Functions project doesn't seem to be running, " | ||
+ "please check any build or runtime errors that could occur during startup"); | ||
} | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
if (!_application.HasExited) | ||
{ | ||
_application.Kill(entireProcessTree: true); | ||
} | ||
|
||
_application.Dispose(); | ||
return ValueTask.CompletedTask; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
provider_azure_function_tests/provider_azure_function_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,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Polly" Version="8.5.0" /> | ||
|
||
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.6.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" /> | ||
<PackageReference Include="PactNet" Version="5.0.0" /> | ||
<PackageReference Include="PactNet.Output.Xunit" Version="1.0.0" /> | ||
|
||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\provider_azure_function\AzureFunctions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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