Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .github/workflows/compat-plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Compatibility suite (V4 plugins)

# The V4 plugin features are not in the pinned suite submodule yet, so this
# checks out the suite branch that adds them and runs the V4.Plugins tests in
# PactNet.Compatibility.Tests against it. It runs on PRs to any base, unlike ci.yml.
on:
push:
branches:
- master
pull_request:
workflow_dispatch:
inputs:
suite-repository:
description: pact-compatibility-suite repository with features/V4/plugins
default: YOU54F/pact-compatibility-suite
suite-ref:
description: Ref of that repository to run
default: feat/plugin-features

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
PACT_DO_NOT_TRACK: 'true'
SUITE_REPOSITORY: ${{ inputs.suite-repository || 'YOU54F/pact-compatibility-suite' }}
SUITE_REF: ${{ inputs.suite-ref || 'feat/plugin-features' }}

jobs:
plugins:
name: V4 plugins (Linux)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
submodules: true

- name: Checkout plugin suite
uses: actions/checkout@v7
with:
repository: ${{ env.SUITE_REPOSITORY }}
ref: ${{ env.SUITE_REF }}
path: pact-compatibility-suite-plugins

# PactNet.Compatibility.Tests targets net10.0.
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v6
with:
dotnet-version: "10.0.x"

- name: Cache FFI dependencies
id: cache
uses: actions/cache@v6
with:
key: cache-ffi-${{ hashFiles('build/download-native-libs.sh') }}
enableCrossOsArchive: true
path: |
build/linux
build/macos
build/windows

- name: Pull interop dependencies
run: bash -c "build/download-native-libs.sh"
if: ${{ steps.cache.outputs.cache-hit != 'true' }}

- name: Install the Pact CLI
uses: pact-foundation/pact-cli@ce8e65692ad3ea9d11244749ae24c8c49225d95f # main

- name: Install plugins
run: |
# matt is not in the plugin repository index, so it is installed from its release.
pact plugin install https://github.com/mefellows/pact-matt-plugin/releases/tag/v0.1.1 -y
pact plugin install csv --version 0.0.6 -y
pact plugin install avro --version 0.1.3 -y
pact plugin install protobuf --version 0.8.0 -y

# PACT_COMPAT_SUITE is read at build time too, to include the plugin feature files.
- name: Run plugin features
run: dotnet test tests/PactNet.Compatibility.Tests --filter "FullyQualifiedName~.V4.Plugins." --verbosity normal
env:
PACT_COMPAT_SUITE: ${{ github.workspace }}/pact-compatibility-suite-plugins
81 changes: 81 additions & 0 deletions tests/PactNet.Compatibility.Tests/Hooks/PluginHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using PactNet.Compatibility.Tests.Support;
using Reqnroll;

namespace PactNet.Compatibility.Tests.Hooks
{
/// <summary>
/// V4 plugin scenarios: serialised (plugins are shared per-process in pact_ffi), torn down before the ratchet,
/// and each one's DSL/FFI route recorded to plugin-routes.txt
/// </summary>
[Binding]
public sealed class PluginHooks
{
private static readonly SemaphoreSlim Gate = new(1, 1);

[BeforeScenario("plugin")]
public static async Task BeforePlugin(PluginWorld plugin, CompatWorld world, ScenarioContext scenario)
{
await Gate.WaitAsync();
plugin.Active = true;

// @wip plugin scenarios are reported as unsupported (skipped) by the ratchet
if (scenario.ScenarioInfo.CombinedTags.Contains("wip"))
{
world.Run("@wip", () => throw new NotSupportedException("@wip"));
}
}

[AfterScenario("plugin", Order = 10)]
public static async Task AfterPlugin(PluginWorld plugin)
{
try
{
await plugin.ShutdownAsync();
}
finally
{
Gate.Release();
}
}

[AfterScenario("plugin", Order = 50)]
public static void RecordRoute(PluginWorld plugin, CompatWorld world, FeatureContext feature, ScenarioContext scenario)
{
bool failed = world.Failure != null || world.Unsupported != null || scenario.TestError != null ||
scenario.ScenarioExecutionStatus != ScenarioExecutionStatus.OK;
string route = failed ? "fail" : plugin.Ops.Any(o => o.StartsWith("ffi:", StringComparison.Ordinal)) ? "FFI" : "DSL";
string ops = string.Join(",", plugin.Ops.Distinct());
PluginWorld.Routes[(feature.FeatureInfo.Title, scenario.ScenarioInfo.Title)] = (route, ops);
}

[AfterTestRun]
public static void WriteRoutes()
{
if (PluginWorld.Routes.IsEmpty)
{
return;
}

var text = new StringBuilder();
foreach (var feature in PluginWorld.Routes.GroupBy(r => r.Key.Feature).OrderBy(g => g.Key))
{
text.AppendLine($"{feature.Key}: DSL={feature.Count(r => r.Value.Route == "DSL")} " +
$"FFI={feature.Count(r => r.Value.Route == "FFI")} fail={feature.Count(r => r.Value.Route == "fail")}");
}

text.AppendLine();
foreach (var r in PluginWorld.Routes.OrderBy(r => r.Key.Feature).ThenBy(r => r.Key.Scenario))
{
text.AppendLine($"{r.Value.Route}\t{r.Key.Feature}: {r.Key.Scenario}\t[{r.Value.Ops}]");
}

File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "plugin-routes.txt"), text.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@
</AssemblyAttribute>
</ItemGroup>

<!-- V4 plugin features (pact-compatibility-suite feat/plugin-features): only when PACT_COMPAT_SUITE points at a checkout that has them -->
<ItemGroup Condition="'$(PACT_COMPAT_SUITE)' != '' And Exists('$(PACT_COMPAT_SUITE)/features/V4/plugins')">
<ReqnrollFeatureFiles Include="$(PACT_COMPAT_SUITE)/features/V4/plugins/*.feature">
<Link>features/V4/plugins/%(Filename)%(Extension)</Link>
</ReqnrollFeatureFiles>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>PluginSuiteDir</_Parameter1>
<_Parameter2>$(PACT_COMPAT_SUITE)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Reqnroll.xUnit" Version="3.3.4" />
Expand Down
71 changes: 65 additions & 6 deletions tests/PactNet.Compatibility.Tests/Steps/HttpConsumerSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ public sealed class HttpConsumerSteps
{ "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth" };

private readonly CompatWorld world;
private readonly PluginWorld plugin;
private readonly PluginSteps pluginSteps;

public HttpConsumerSteps(CompatWorld world)
public HttpConsumerSteps(CompatWorld world, PluginWorld plugin, PluginSteps pluginSteps)
{
this.world = world;
this.plugin = plugin;
this.pluginSteps = pluginSteps;
}

[Given(@"^the following HTTP interactions have been defined:$")]
Expand Down Expand Up @@ -57,7 +61,18 @@ public Task RequestMadeWithChanges(int no, Table changes) => this.world.RunAsync
});

[Then(@"^a (\d+) (?:success|error) response is returned$")]
public void ResponseReturned(int status) => this.world.Run("response status", () =>
public void ResponseReturned(int status)
{
if (this.plugin.Active)
{
this.pluginSteps.ResponseStatus(status);
return;
}

this.ResponseReturnedV1(status);
}

private void ResponseReturnedV1(int status) => this.world.Run("response status", () =>
{
HttpResponse response = this.RequireResponse();
CompatWorld.Check(response.Status == status,
Expand All @@ -80,10 +95,32 @@ public void ContentType(string contentType) => this.world.Run("response content
});

[When(@"^the pact test is done$")]
public Task PactTestDone() => this.world.RunAsync("pact test done", () => this.world.RequireSession().FinishAsync());
public Task PactTestDone()
{
if (this.plugin.Active)
{
this.pluginSteps.PactTestDone();
return Task.CompletedTask;
}

return this.PactTestDoneV1();
}

private Task PactTestDoneV1() => this.world.RunAsync("pact test done", () => this.world.RequireSession().FinishAsync());

[Then(@"^the mock server status will be OK$")]
public void StatusOk() => this.world.Run("status OK", () =>
public void StatusOk()
{
if (this.plugin.Active)
{
this.pluginSteps.StatusOk();
return;
}

this.StatusOkV1();
}

private void StatusOkV1() => this.world.Run("status OK", () =>
{
MockServerSession session = this.RequireDone();
CompatWorld.Check(session.Ok, $"Expected the mock server status to be OK, but verification failed: {session.VerifyError?.Message}\n{session.FinalMismatches?.ToJsonString()}");
Expand All @@ -96,7 +133,18 @@ public void StatusNotOk() => this.world.Run("status NOT OK", () =>
});

[Then(@"^the mock server will write out a Pact file for the interactions? when done$")]
public void PactWritten() => this.world.Run("pact file written", () =>
public void PactWritten()
{
if (this.plugin.Active)
{
this.pluginSteps.PactWritten();
return;
}

this.PactWrittenV1();
}

private void PactWrittenV1() => this.world.Run("pact file written", () =>
{
MockServerSession session = this.RequireDone();
CompatWorld.Check(File.Exists(session.PactFile), $"Expected a pact file at {session.PactFile}");
Expand Down Expand Up @@ -214,7 +262,18 @@ public void UnexpectedRequestForPath(string method, string path) => this.world.R
});

[Then(@"^the mock server status will be mismatches$")]
public void StatusMismatches() => this.world.Run("request mismatches", () =>
public void StatusMismatches()
{
if (this.plugin.Active)
{
this.pluginSteps.StatusMismatches();
return;
}

this.StatusMismatchesV1();
}

private void StatusMismatchesV1() => this.world.Run("request mismatches", () =>
{
MockServerSession session = this.RequireDone();
CompatWorld.Check(!session.Ok && session.FinalMismatches.Any(m => (string)m?["type"] == "request-mismatch"),
Expand Down
Loading
Loading