-
Notifications
You must be signed in to change notification settings - Fork 176
feat(cli): add plugin invocation protocol #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
QMalcolm
wants to merge
3
commits into
main
Choose a base branch
from
qmalcolm--feat-plugin-io-protocol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+377
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,83 @@ | ||
| package plugin | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "os/exec" | ||
| ) | ||
|
|
||
| // Request is the JSON envelope sent to a plugin on stdin. | ||
| type Request struct { | ||
| Files map[string]string `json:"files"` | ||
| } | ||
|
|
||
| // Response is the JSON envelope received from a plugin on stdout. | ||
| type Response struct { | ||
| Files map[string]string `json:"files"` | ||
| Issues []Issue `json:"issues,omitempty"` | ||
| } | ||
|
|
||
| // Issue is a single diagnostic emitted by a plugin during conversion. | ||
| // Severity is one of "error", "warning", or "info". | ||
| // Path is optional; it is omitted for issues not tied to a specific location. | ||
| type Issue struct { | ||
| Severity string `json:"severity"` | ||
| Message string `json:"message"` | ||
| Path string `json:"path,omitempty"` | ||
| } | ||
|
|
||
| // Invoke runs a plugin subprocess, pipes req as JSON to its stdin, | ||
| // and decodes its stdout as a JSON Response. | ||
| // | ||
| // pluginDir is used as the working directory for the subprocess so that | ||
| // relative paths in the invoke array resolve correctly. | ||
| // invoke is the command and its arguments (invoke[0] is the executable). | ||
| // pluginStderr receives the plugin's stderr verbatim; pass io.Discard to suppress. | ||
| // | ||
| // Hard errors: failed to marshal request, process start failure, context | ||
| // deadline exceeded, non-zero exit code, or invalid JSON in the response. | ||
| // A non-empty Issues slice in the response is NOT itself a Go error — the | ||
| // caller is responsible for inspecting severities and setting the exit code. | ||
| func Invoke(ctx context.Context, pluginDir string, invoke []string, req Request, pluginStderr io.Writer) (*Response, error) { | ||
| if len(invoke) == 0 { | ||
| return nil, fmt.Errorf("plugin invoke command is empty") | ||
| } | ||
|
|
||
| // Normalise nil to empty map so plugins always receive {"files":{}} not {"files":null}. | ||
| if req.Files == nil { | ||
| req.Files = map[string]string{} | ||
| } | ||
|
|
||
| reqJSON, err := json.Marshal(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal plugin request: %w", err) | ||
| } | ||
|
|
||
| var stdout bytes.Buffer | ||
| cmd := exec.CommandContext(ctx, invoke[0], invoke[1:]...) | ||
| cmd.Dir = pluginDir | ||
| cmd.Stdin = bytes.NewReader(reqJSON) | ||
| // Use an explicit buffer rather than cmd.Output() — cmd.Output() is | ||
| // incompatible with a pre-assigned cmd.Stderr writer. | ||
| cmd.Stdout = &stdout | ||
| cmd.Stderr = pluginStderr | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| // ctx.Err() is the authoritative signal for timeout/cancellation. | ||
| // exec.CommandContext kills the process and returns "signal: killed", | ||
| // not context.DeadlineExceeded, so we must check ctx.Err() explicitly. | ||
| if ctx.Err() != nil { | ||
| return nil, fmt.Errorf("plugin timed out: %w", ctx.Err()) | ||
| } | ||
| return nil, fmt.Errorf("plugin process failed: %w", err) | ||
| } | ||
|
|
||
| var resp Response | ||
| if err := json.Unmarshal(stdout.Bytes(), &resp); err != nil { | ||
| return nil, fmt.Errorf("invalid JSON response from plugin: %w", err) | ||
| } | ||
| return &resp, nil | ||
| } | ||
This file contains hidden or 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,294 @@ | ||
| package plugin_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/apache/ossie/cli/internal/plugin" | ||
| ) | ||
|
|
||
| // TestMain handles two roles: | ||
| // 1. When GO_TEST_PLUGIN=1 it acts as a fake plugin subprocess, reads stdin, | ||
| // and writes a canned response based on GO_TEST_PLUGIN_MODE. | ||
| // 2. Otherwise it runs the normal test suite. | ||
| func TestMain(m *testing.M) { | ||
| if os.Getenv("GO_TEST_PLUGIN") == "1" { | ||
| runFakePlugin() | ||
| // runFakePlugin calls os.Exit; this line is never reached. | ||
| } | ||
| os.Exit(m.Run()) | ||
| } | ||
|
|
||
| // runFakePlugin implements a minimal plugin subprocess used by invoke tests. | ||
| // It reads all of stdin (the JSON request), then dispatches on GO_TEST_PLUGIN_MODE. | ||
| func runFakePlugin() { | ||
| stdinBytes, err := io.ReadAll(os.Stdin) | ||
| if err != nil { | ||
| fmt.Fprintln(os.Stderr, "fake plugin: failed to read stdin:", err) | ||
| os.Exit(2) | ||
| } | ||
|
|
||
| switch mode := os.Getenv("GO_TEST_PLUGIN_MODE"); mode { | ||
| case "success": | ||
| json.NewEncoder(os.Stdout).Encode(map[string]any{ | ||
| "files": map[string]string{"output.yaml": "converted content"}, | ||
| }) | ||
|
|
||
| case "warning_issue": | ||
| json.NewEncoder(os.Stdout).Encode(map[string]any{ | ||
| "files": map[string]string{"output.yaml": "converted content"}, | ||
| "issues": []map[string]string{ | ||
| {"severity": "warning", "message": "some warning", "path": "input.yaml"}, | ||
| }, | ||
| }) | ||
|
|
||
| case "error_issue": | ||
| json.NewEncoder(os.Stdout).Encode(map[string]any{ | ||
| "files": map[string]string{}, | ||
| "issues": []map[string]string{ | ||
| {"severity": "error", "message": "conversion failed"}, | ||
| }, | ||
| }) | ||
|
|
||
| case "invalid_json": | ||
| fmt.Fprint(os.Stdout, "not json") | ||
|
|
||
| case "stderr_output": | ||
| fmt.Fprint(os.Stderr, "stderr from plugin") | ||
| json.NewEncoder(os.Stdout).Encode(map[string]any{ | ||
| "files": map[string]string{"output.yaml": "ok"}, | ||
| }) | ||
|
|
||
| case "nonzero_exit": | ||
| os.Exit(1) | ||
|
|
||
| case "timeout": | ||
| time.Sleep(30 * time.Second) | ||
|
|
||
| case "echo_request": | ||
| // Echo the raw stdin bytes back in files["received_request"] so the | ||
| // test can assert the plugin received the correct request JSON. | ||
| json.NewEncoder(os.Stdout).Encode(map[string]any{ | ||
| "files": map[string]string{"received_request": string(stdinBytes)}, | ||
| }) | ||
|
|
||
| default: | ||
| fmt.Fprintln(os.Stderr, "fake plugin: unknown mode:", mode) | ||
| os.Exit(2) | ||
| } | ||
|
|
||
| os.Exit(0) | ||
| } | ||
|
|
||
| // fakePluginInvoke sets up env vars for the fake plugin and returns the | ||
| // invoke slice. The test binary re-invokes itself with -test.run=^$ so that | ||
| // no tests run in the child — TestMain sees GO_TEST_PLUGIN=1 and exits early. | ||
| func fakePluginInvoke(t *testing.T, mode string) []string { | ||
| t.Helper() | ||
| t.Setenv("GO_TEST_PLUGIN", "1") | ||
| t.Setenv("GO_TEST_PLUGIN_MODE", mode) | ||
| return []string{os.Args[0], "-test.run=^$"} | ||
| } | ||
|
|
||
| func TestInvoke_success(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "success") | ||
| ctx := context.Background() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{Files: map[string]string{}}, io.Discard) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if resp == nil { | ||
| t.Fatal("expected non-nil response") | ||
| } | ||
| if _, ok := resp.Files["output.yaml"]; !ok { | ||
| t.Errorf("expected output.yaml in response files, got: %v", resp.Files) | ||
| } | ||
| if len(resp.Issues) != 0 { | ||
| t.Errorf("expected no issues, got %d", len(resp.Issues)) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_warningIssue(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "warning_issue") | ||
| ctx := context.Background() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if len(resp.Files) != 1 { | ||
| t.Errorf("expected 1 output file, got %d", len(resp.Files)) | ||
| } | ||
| if len(resp.Issues) != 1 { | ||
| t.Fatalf("expected 1 issue, got %d", len(resp.Issues)) | ||
| } | ||
| if resp.Issues[0].Severity != "warning" { | ||
| t.Errorf("issue severity: got %q, want %q", resp.Issues[0].Severity, "warning") | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_errorIssue(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "error_issue") | ||
| ctx := context.Background() | ||
|
|
||
| // An error-severity issue is NOT a Go error — Invoke must return nil err. | ||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected Go error: %v", err) | ||
| } | ||
| if len(resp.Issues) != 1 { | ||
| t.Fatalf("expected 1 issue, got %d", len(resp.Issues)) | ||
| } | ||
| if resp.Issues[0].Severity != "error" { | ||
| t.Errorf("issue severity: got %q, want %q", resp.Issues[0].Severity, "error") | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_invalidResponseJSON(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "invalid_json") | ||
| ctx := context.Background() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
|
|
||
| if err == nil { | ||
| t.Fatal("expected error for invalid JSON response, got nil") | ||
| } | ||
| if resp != nil { | ||
| t.Errorf("expected nil response, got %+v", resp) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_nonZeroExit(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "nonzero_exit") | ||
| ctx := context.Background() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
|
|
||
| if err == nil { | ||
| t.Fatal("expected error for non-zero exit, got nil") | ||
| } | ||
| if resp != nil { | ||
| t.Errorf("expected nil response, got %+v", resp) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_stderrForwarded(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "stderr_output") | ||
| ctx := context.Background() | ||
| var buf strings.Builder | ||
|
|
||
| _, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, &buf) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !strings.Contains(buf.String(), "stderr from plugin") { | ||
| t.Errorf("expected stderr content in writer, got: %q", buf.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_stderrSuppressed(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "stderr_output") | ||
| ctx := context.Background() | ||
|
|
||
| // Passing io.Discard must not panic and must return a valid response. | ||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if resp == nil { | ||
| t.Fatal("expected non-nil response") | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_timeout(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "timeout") | ||
| ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
|
|
||
| if err == nil { | ||
| t.Fatal("expected timeout error, got nil") | ||
| } | ||
| if resp != nil { | ||
| t.Errorf("expected nil response on timeout, got %+v", resp) | ||
| } | ||
| if !errors.Is(err, context.DeadlineExceeded) { | ||
| t.Errorf("expected DeadlineExceeded in error chain, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_requestPayloadReachesPlugin(t *testing.T) { | ||
| invoke := fakePluginInvoke(t, "echo_request") | ||
| ctx := context.Background() | ||
|
|
||
| req := plugin.Request{ | ||
| Files: map[string]string{ | ||
| "core/orders.yaml": "version: 2\nmodels:\n - name: orders", | ||
| }, | ||
| } | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, req, io.Discard) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| raw, ok := resp.Files["received_request"] | ||
| if !ok { | ||
| t.Fatal("expected received_request key in response files") | ||
| } | ||
|
|
||
| // Decode the echoed request and compare — avoids brittle JSON key-ordering checks. | ||
| var echoed plugin.Request | ||
| if err := json.Unmarshal([]byte(raw), &echoed); err != nil { | ||
| t.Fatalf("could not decode echoed request: %v", err) | ||
| } | ||
| want := req.Files["core/orders.yaml"] | ||
| if echoed.Files["core/orders.yaml"] != want { | ||
| t.Errorf("echoed file content:\ngot: %q\nwant: %q", echoed.Files["core/orders.yaml"], want) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_nilFilesNormalisedToEmptyObject(t *testing.T) { | ||
| // A Request with nil Files must marshal as {"files":{}} not {"files":null}. | ||
| // Plugins receiving null instead of an object may crash on iteration. | ||
| invoke := fakePluginInvoke(t, "echo_request") | ||
| ctx := context.Background() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), invoke, plugin.Request{}, io.Discard) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| raw := resp.Files["received_request"] | ||
|
|
||
| // The echoed bytes must contain `"files":{}` not `"files":null`. | ||
| if !strings.Contains(raw, `"files":{}`) { | ||
| t.Errorf("expected files to be serialised as {}, got: %s", raw) | ||
| } | ||
| } | ||
|
|
||
| func TestInvoke_emptyInvokeReturnsError(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| resp, err := plugin.Invoke(ctx, t.TempDir(), []string{}, plugin.Request{}, io.Discard) | ||
|
|
||
| if err == nil { | ||
| t.Fatal("expected error for empty invoke slice, got nil") | ||
| } | ||
| if resp != nil { | ||
| t.Errorf("expected nil response, got %+v", resp) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this fail nicely if pluginDir is not accessible ? Would be good to add error handling for this case.