grouproll: run the document witnesses against the Go builder
TestRegionGroupDocumentGoBuilder builds a shim over grouproll.RemoteCommand and runs region_group_document_test.py with GROUPROLL_GO_REMOTE_COMMAND set, so every command the witnesses frame comes from the Go builder (spec §10.3). Without the variable the Python test is unchanged. The shim is listed as a core file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+139
−0
internal/grouproll/testdata/remotecommand/main.go+79 −0internal/provision/region_group_guards_test.go+25 −0region_ha_boundary_test.go+1 −0scripts/region_group_document_test.py+34 −0
Intent
diff --git a/internal/grouproll/testdata/remotecommand/main.go b/internal/grouproll/testdata/remotecommand/main.go| new file mode 100644 |
| index 000000000..d4ac40531 |
| --- /dev/null |
| +++ b/internal/grouproll/testdata/remotecommand/main.go |
| @@ -0,0 +1,79 @@ |
| +// Command remotecommand is the Go builder behind the operator document's |
| +// Python witnesses (scripts/region_group_document_test.py with |
| +// GROUPROLL_GO_REMOTE_COMMAND set): it reads one remote_command request, |
| +// {"verb", "sha256", "transaction", "permit", "nonce"} as JSON on stdin, |
| +// and writes grouproll.RemoteCommand's command to stdout. A refusal exits |
| +// 3 with its key on stderr. Test-only; built by |
| +// internal/provision's TestRegionGroupDocumentGoBuilder. |
| +package main |
| + |
| +import ( |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "os" |
| + |
| + "github.com/carlosframework/platform/internal/grouproll" |
| +) |
| + |
| +func main() { |
| + var req struct { |
| + Verb string `json:"verb"` |
| + SHA256 string `json:"sha256"` |
| + Transaction string `json:"transaction"` |
| + Permit map[string]any `json:"permit"` |
| + Nonce string `json:"nonce"` |
| + } |
| + dec := json.NewDecoder(os.Stdin) |
| + dec.UseNumber() |
| + if err := dec.Decode(&req); err != nil { |
| + fmt.Fprintln(os.Stderr, err) |
| + os.Exit(2) |
| + } |
| + permit, err := integers(req.Permit) |
| + if err != nil { |
| + fmt.Fprintln(os.Stderr, err) |
| + os.Exit(2) |
| + } |
| + args := grouproll.BoxArgs{SHA256: req.SHA256, Transaction: req.Transaction} |
| + if req.Permit != nil { |
| + args.Permit = permit.(map[string]any) |
| + } |
| + command, err := grouproll.RemoteCommand(req.Verb, args, req.Nonce) |
| + if err != nil { |
| + var r grouproll.Refusal |
| + if errors.As(err, &r) { |
| + fmt.Fprintln(os.Stderr, r.Key) |
| + os.Exit(3) |
| + } |
| + fmt.Fprintln(os.Stderr, err) |
| + os.Exit(2) |
| + } |
| + os.Stdout.WriteString(command) |
| +} |
| + |
| +// integers turns the decoder's numbers into int64, as Python's json.loads |
| +// gives ints; any other number is refused, as the permit encoder would. |
| +func integers(v any) (any, error) { |
| + switch x := v.(type) { |
| + case json.Number: |
| + return x.Int64() |
| + case map[string]any: |
| + for k, e := range x { |
| + n, err := integers(e) |
| + if err != nil { |
| + return nil, err |
| + } |
| + x[k] = n |
| + } |
| + case []any: |
| + for i, e := range x { |
| + n, err := integers(e) |
| + if err != nil { |
| + return nil, err |
| + } |
| + x[i] = n |
| + } |
| + } |
| + return v, nil |
| +} |
diff --git a/internal/provision/region_group_guards_test.go b/internal/provision/region_group_guards_test.go| index 52445cbdb..3d00e35d7 100644 |
| --- a/internal/provision/region_group_guards_test.go |
| +++ b/internal/provision/region_group_guards_test.go |
| @@ -228,3 +228,28 @@ func TestRegionGroupHeldPythonGuards(t *testing.T) { |
| } |
| t.Logf("%s", output) |
| } |
| + |
| +// TestRegionGroupDocumentGoBuilder runs the operator document's witnesses |
| +// (scripts/region_group_document_test.py) against `carlos system group`'s |
| +// command builder: a shim over grouproll.RemoteCommand |
| +// (internal/grouproll/testdata/remotecommand) stands in for the script's |
| +// remote_command, so every command the witnesses frame must match the |
| +// document's pattern as the script's do (group roll verb spec §10.3). |
| +func TestRegionGroupDocumentGoBuilder(t *testing.T) { |
| + python := groupGuardBinary(t, "python3") |
| + dir := t.TempDir() |
| + shim := filepath.Join(dir, "remotecommand") |
| + build := exec.Command("go", "build", "-o", shim, "./internal/grouproll/testdata/remotecommand") |
| + build.Dir = "../.." |
| + if output, err := build.CombinedOutput(); err != nil { |
| + t.Fatalf("building the Go builder's shim: %v\n%s", err, output) |
| + } |
| + cmd := exec.Command(python, "-B", "-m", "unittest", "-v", "scripts.region_group_document_test") |
| + cmd.Dir = "../.." |
| + cmd.Env = append(os.Environ(), "TMPDIR="+dir, "GROUPROLL_GO_REMOTE_COMMAND="+shim) |
| + output, err := cmd.CombinedOutput() |
| + if err != nil { |
| + t.Fatalf("the document witnesses against the Go builder: %v\n%s", err, output) |
| + } |
| + t.Logf("%s", output) |
| +} |
diff --git a/region_ha_boundary_test.go b/region_ha_boundary_test.go| index 9a7cf2a05..79007ee94 100644 |
| --- a/region_ha_boundary_test.go |
| +++ b/region_ha_boundary_test.go |
| @@ -58,6 +58,7 @@ var regionHACoreFiles = []string{ |
| "internal/grouproll/roll.go", |
| "internal/grouproll/depart.go", |
| "internal/grouproll/probe.go", |
| + "internal/grouproll/testdata/remotecommand/main.go", |
| "internal/release/systemlookup.go", |
| } |
diff --git a/scripts/region_group_document_test.py b/scripts/region_group_document_test.py| index 92da3ec7e..8cf4ea76e 100644 |
| --- a/scripts/region_group_document_test.py |
| +++ b/scripts/region_group_document_test.py |
| @@ -1,12 +1,16 @@ |
| """Offline acceptance and refusal witnesses for the approved session grammar.""" |
| import base64 |
| +import json |
| +import os |
| from pathlib import Path |
| import random |
| import re |
| import shlex |
| import string |
| +import subprocess |
| import unittest |
| +from unittest.mock import patch |
| from scripts import region_group_frames as frames |
| @@ -27,6 +31,36 @@ def pattern(sha256=None): |
| return re.compile("^(" + tool + "|" + harness + ")$") |
| +# With GROUPROLL_GO_REMOTE_COMMAND naming the Go builder's shim |
| +# (internal/grouproll/testdata/remotecommand), every command these witnesses |
| +# frame comes from `carlos system group`'s builder instead of the script's |
| +# (internal/provision TestRegionGroupDocumentGoBuilder). |
| +GO_REMOTE_COMMAND = os.environ.get("GROUPROLL_GO_REMOTE_COMMAND") |
| +_go_builder = None |
| + |
| + |
| +def go_remote_command(args, nonce): |
| + request = json.dumps(dict(args, nonce=nonce)).encode() |
| + done = subprocess.run([GO_REMOTE_COMMAND], input=request, capture_output=True, timeout=30) |
| + if done.returncode == 3: |
| + raise frames.roll.Refusal(done.stderr.decode().strip()) |
| + if done.returncode != 0: |
| + raise RuntimeError(done.stderr.decode()) |
| + return done.stdout.decode() |
| + |
| + |
| +def setUpModule(): |
| + global _go_builder |
| + if GO_REMOTE_COMMAND: |
| + _go_builder = patch.object(frames.roll.AWS, "remote_command", staticmethod(go_remote_command)) |
| + _go_builder.start() |
| + |
| + |
| +def tearDownModule(): |
| + if _go_builder is not None: |
| + _go_builder.stop() |
| + |
| + |
| class DocumentPatternTests(unittest.TestCase): |
| def setUp(self): |
| self.tool = pattern() |