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
36 changes: 36 additions & 0 deletions .github/workflows/compatibility-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,39 @@ jobs:

- name: Run Behat
run: vendor/bin/behat compatibility-suite/pact-compatibility-suite/features/V4 --colors
v4-plugins:
# The plugin features are not in the pinned submodule yet, so this checks out the suite branch that adds
# them (YOU54F/pact-compatibility-suite#1). The repository variables PACT_COMPAT_SUITE_PLUGINS_REPOSITORY
# and PACT_COMPAT_SUITE_PLUGINS_REF override it.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
repository: ${{ vars.PACT_COMPAT_SUITE_PLUGINS_REPOSITORY || 'YOU54F/pact-compatibility-suite' }}
ref: ${{ vars.PACT_COMPAT_SUITE_PLUGINS_REF || 'feat/plugin-features' }}
path: pact-compatibility-suite-plugins

- uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2
with:
php-version: 8.2
extensions: sockets
coverage: none

- uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0

- 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

- name: Run Behat
run: vendor/bin/behat --suite=v4_plugins --colors
env:
PACT_COMPAT_SUITE: ${{ github.workspace }}/pact-compatibility-suite-plugins
1 change: 1 addition & 0 deletions behat.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
'compatibility-suite/suites/v4/sync-message/consumer.php',
'compatibility-suite/suites/v4/matching-rules.php',
'compatibility-suite/suites/v4/generators.php',
'compatibility-suite/suites/v4/plugins.php',
]);
1 change: 1 addition & 0 deletions compatibility-suite/pacts/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.json
plugins/
102 changes: 102 additions & 0 deletions compatibility-suite/public/plugins/grpc-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

// A minimal unary gRPC provider over HTTP/2 without TLS (h2c, prior knowledge). Every call is answered with the
// message in the file $argv[2], framed for gRPC, and a grpc-status 0 trailer. Request headers are not decoded.

$args = array_map(fn (mixed $arg): string => is_string($arg) ? $arg : '', (array) ($_SERVER['argv'] ?? []));
[, $port, $messageFile] = $args + ['', '', ''];
$message = (string) file_get_contents($messageFile);
$server = stream_socket_server("tcp://127.0.0.1:$port", $errno, $error);
if (!$server) {
fwrite(STDERR, "$error\n");
exit(1);
}

const PREFACE = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";

function frame(int $type, int $flags, int $stream, string $payload): string
{
return substr(pack('N', strlen($payload)), 1) . chr($type) . chr($flags) . pack('N', $stream) . $payload;
}

function literal(string $name, string $value): string
{
// Literal header field without indexing, new name, no Huffman coding (lengths < 127)
return "\x00" . chr(strlen($name)) . $name . chr(strlen($value)) . $value;
}

function u32(string $bytes): int
{
$value = unpack('N', $bytes);

return is_array($value) && is_int($value[1] ?? null) ? $value[1] : 0;
}

/** @param resource $conn */
function respond($conn, int $stream, string $message): void
{
$headers = "\x88" . literal('content-type', 'application/grpc'); // :status 200
$data = "\x00" . pack('N', strlen($message)) . $message;
fwrite($conn, frame(0x1, 0x4, $stream, $headers)
. frame(0x0, 0x0, $stream, $data)
. frame(0x1, 0x5, $stream, literal('grpc-status', '0')));
}

$clients = [];
$buffers = []; // unparsed bytes per connection
$prefaced = []; // whether the connection preface was read
while (true) {
$read = array_merge([$server], array_values($clients));
$write = $except = null;
if (false === stream_select($read, $write, $except, null)) {
break;
}
foreach ($read as $sock) {
if ($sock === $server) {
$conn = @stream_socket_accept($server, 0);
if ($conn) {
$id = (int) $conn;
$clients[$id] = $conn;
$buffers[$id] = '';
$prefaced[$id] = false;
fwrite($conn, frame(0x4, 0x0, 0, '')); // SETTINGS
}
continue;
}
$id = (int) $sock;
$chunk = fread($sock, 65536);
if ('' === $chunk || false === $chunk) {
fclose($sock);
unset($clients[$id], $buffers[$id], $prefaced[$id]);
continue;
}
$buf = ($buffers[$id] ?? '') . $chunk;
if (!($prefaced[$id] ?? false)) {
if (strlen($buf) < strlen(PREFACE)) {
$buffers[$id] = $buf;
continue;
}
$buf = substr($buf, strlen(PREFACE));
$prefaced[$id] = true;
}
while (strlen($buf) >= 9) {
$length = u32("\x00" . substr($buf, 0, 3));
if (strlen($buf) < 9 + $length) {
break;
}
$type = ord($buf[3]);
$flags = ord($buf[4]);
$stream = u32(substr($buf, 5, 4)) & 0x7FFFFFFF;
$payload = substr($buf, 9, $length);
$buf = substr($buf, 9 + $length);
if (0x4 === $type && !($flags & 0x1)) {
fwrite($sock, frame(0x4, 0x1, 0, '')); // SETTINGS ACK
} elseif (0x6 === $type && !($flags & 0x1)) {
fwrite($sock, frame(0x6, 0x1, 0, $payload)); // PING ACK
} elseif ((0x0 === $type || 0x1 === $type) && ($flags & 0x1) && $stream > 0) {
respond($sock, $stream, $message); // the request ended (END_STREAM)
}
}
$buffers[$id] = $buf;
}
}
26 changes: 26 additions & 0 deletions compatibility-suite/public/plugins/http-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// Router for `php -S`: a provider that returns a fixed body, or (when PROVIDER_DESCRIPTION is set) a message
// provider that answers the verifier's POST of {"description": ...} with the message and its metadata.

$body = (string) file_get_contents((string) getenv('PROVIDER_BODY_FILE'));
$contentType = (string) getenv('PROVIDER_CONTENT_TYPE');
$description = getenv('PROVIDER_DESCRIPTION');

if (false !== $description) {
$request = json_decode((string) file_get_contents('php://input'), true);
$asked = is_array($request) && is_string($request['description'] ?? null) ? $request['description'] : '';
if ($asked !== $description) {
http_response_code(404);
echo sprintf('no message "%s"', $asked);

return true;
}
header('pact-message-metadata: ' . base64_encode((string) json_encode(['contentType' => $contentType])));
}

http_response_code(200);
header('Content-Type: ' . $contentType);
echo $body;

return true;
21 changes: 21 additions & 0 deletions compatibility-suite/public/plugins/tcp-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// A line based TCP provider: answers $argv[2] to the line $argv[1], and "MATTerrorMATT" to anything else.

$args = array_map(fn (mixed $arg): string => is_string($arg) ? $arg : '', (array) ($_SERVER['argv'] ?? []));
[, $port, $line, $reply] = $args + ['', '', '', ''];
$server = stream_socket_server("tcp://127.0.0.1:$port", $errno, $error);
if (!$server) {
fwrite(STDERR, "$error\n");
exit(1);
}

while ($conn = @stream_socket_accept($server, -1)) {
while (false !== ($got = fgets($conn))) {
$answer = rtrim($got, "\r\n") === $line ? $reply : 'MATTerrorMATT';
if (false === @fwrite($conn, "$answer\n")) {
break;
}
}
fclose($conn);
}
33 changes: 33 additions & 0 deletions compatibility-suite/suites/v4/plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Behat\Config\Config;
use Behat\Config\Filter\TagFilter;
use Behat\Config\Profile;
use Behat\Config\Suite;
use PhpPactTest\CompatibilitySuite\Context\Shared\Hook\SetUpContext;
use PhpPactTest\CompatibilitySuite\Context\V4\PluginContext;

// The plugin features are not in the pinned compatibility-suite submodule yet, so they are read from a
// pact-compatibility-suite checkout named by PACT_COMPAT_SUITE. The suite is skipped when it is not set.
//
// To run them, install the plugins the scenarios pin and point PACT_COMPAT_SUITE at the checkout:
//
// pact plugin install matt --version 0.1.1
// pact plugin install csv --version 0.0.6
// pact plugin install avro --version 0.1.3
// pact plugin install protobuf --version 0.8.0
// PACT_COMPAT_SUITE=/path/to/pact-compatibility-suite vendor/bin/behat --suite=v4_plugins
//
// The providers are PHP subprocesses (php -S and stream servers) and the gRPC client needs curl with HTTP/2.
$root = getenv('PACT_COMPAT_SUITE');
if (!$root || !is_dir($root . '/features/V4/plugins')) {
return new Config();
}

return (new Config())
->withProfile((new Profile('default'))
->withSuite((new Suite('v4_plugins'))
->addContext(SetUpContext::class)
->addContext(PluginContext::class, [realpath($root)])
->withPaths(realpath($root) . '/features/V4/plugins')
->withFilter(new TagFilter('~@wip'))));
Loading
Loading