| 1 | // Package plugin supports extending a testscript run with external plugin |
| 2 | // binaries, and provides the contract and JSON-RPC bridge that those binaries |
| 3 | // implement. |
| 4 | // |
| 5 | // A plugin is an executable named "testscript-plugin-<name>" found on $PATH. |
| 6 | // It provides additional commands and services to a testscript run, and may |
| 7 | // set environment variables. The testscript host discovers, starts and queries |
| 8 | // a plugin lazily the first time a script runs the "plugin <name>" command; |
| 9 | // enable this by calling [Setup]. |
| 10 | // |
| 11 | // A plugin communicates with the host over JSON-RPC carried on its standard |
| 12 | // input (requests) and standard output (responses). It may write freely to its |
| 13 | // standard error for logging; the host captures this and reports it through the |
| 14 | // log of the test that is interacting with the plugin, rather than letting it |
| 15 | // interleave on the host's own standard error. A plugin binary's main is |
| 16 | // essentially: |
| 17 | // |
| 18 | // func main() { |
| 19 | // if err := plugin.Serve(myPlugin); err != nil { |
| 20 | // log.Fatal(err) |
| 21 | // } |
| 22 | // } |
| 23 | // |
| 24 | // where myPlugin implements [Interface]. |
| 25 | // |
| 26 | // IMPORTANT: a plugin must not write anything other than JSON-RPC frames to |
| 27 | // its standard output, or the protocol will be corrupted. Use standard error |
| 28 | // (or the log package, whose default output is standard error) for logging. |
| 29 | package plugin |
| 30 | |
| 31 | // Interface is implemented by a plugin. A single plugin process is shared by |
| 32 | // all tests using it, so all methods must be safe for concurrent use. |
| 33 | type Interface interface { |
| 34 | // Info returns static information about the plugin: the environment |
| 35 | // variables it requires and provides, and the commands it makes |
| 36 | // available to test scripts. |
| 37 | Info() PluginInfo |
| 38 | |
| 39 | // NewTestInstance creates a new instance of the plugin for a single |
| 40 | // running test. Multiple instances may exist concurrently. |
| 41 | NewTestInstance(TestParams) (TestInstance, error) |
| 42 | |
| 43 | // Close shuts the plugin down, releasing any global resources. |
| 44 | Close() |
| 45 | } |
| 46 | |
| 47 | // PluginInfo holds static information about a plugin. |
| 48 | type PluginInfo struct { |
| 49 | // RequiredEnv holds the set of environment variable names that the |
| 50 | // plugin needs to be passed when creating a test instance. Only these |
| 51 | // variables are sent to the plugin. |
| 52 | RequiredEnv map[string]bool |
| 53 | |
| 54 | // ResultEnv holds the set of environment variable names that the |
| 55 | // plugin is allowed to set in the test environment. The host rejects |
| 56 | // any attempt to set a variable not in this set. |
| 57 | ResultEnv map[string]bool |
| 58 | |
| 59 | // Cmds holds the set of commands provided by the plugin, keyed by |
| 60 | // command name. These commands become available to a test script after |
| 61 | // it runs the "plugin" command for this plugin. |
| 62 | Cmds map[string]CmdInfo |
| 63 | } |
| 64 | |
| 65 | // TestParams holds the parameters for creating a new test instance. |
| 66 | type TestParams struct { |
| 67 | // TestName is the name of the test that the instance is for. |
| 68 | TestName string |
| 69 | |
| 70 | // PluginParamsDir is the absolute path of the directory holding |
| 71 | // plugin-specific parameters for the test, or empty if none was given. |
| 72 | PluginParamsDir string |
| 73 | |
| 74 | // Env holds the values of the variables named in |
| 75 | // [PluginInfo.RequiredEnv], as resolved in the test environment. |
| 76 | Env map[string]string |
| 77 | } |
| 78 | |
| 79 | // CmdInfo holds static information about a command provided by a plugin. |
| 80 | type CmdInfo struct { |
| 81 | // RequiredEnv holds the set of environment variable names that the |
| 82 | // command needs passed to it. Only these variables are sent to the |
| 83 | // plugin when the command is run. |
| 84 | RequiredEnv map[string]bool |
| 85 | |
| 86 | // WritesOutput indicates whether the command sets |
| 87 | // standard error and standard output. |
| 88 | WritesOutput bool |
| 89 | } |
| 90 | |
| 91 | // TestInstance represents a running instance of a plugin for a single test. |
| 92 | // Its methods may be called concurrently with those of other instances. |
| 93 | type TestInstance interface { |
| 94 | // Env returns the environment variables that the instance wants to set |
| 95 | // in the test environment. Every key must be present in |
| 96 | // [PluginInfo.ResultEnv]. |
| 97 | Env() map[string]string |
| 98 | |
| 99 | // RunCmd runs one of the commands provided by the plugin. The returned |
| 100 | // error indicates a failure of the plugin itself (for example, the |
| 101 | // command could not be started); a failure of the command being run is |
| 102 | // reported via [CmdResult.Err]. |
| 103 | RunCmd(CmdParams) (CmdResult, error) |
| 104 | |
| 105 | // Close shuts the instance down, releasing any resources held for the |
| 106 | // test. |
| 107 | Close() |
| 108 | } |
| 109 | |
| 110 | // CmdParams holds the parameters for running a plugin command. |
| 111 | type CmdParams struct { |
| 112 | // Name is the name of the command being run. |
| 113 | Name string |
| 114 | |
| 115 | // Args holds the command's arguments (not including the command name). |
| 116 | Args []string |
| 117 | |
| 118 | // Dir is the absolute path of the working directory the command should |
| 119 | // run in. |
| 120 | Dir string |
| 121 | |
| 122 | // Env holds the values of the variables named in the command's |
| 123 | // [CmdInfo.RequiredEnv], as resolved in the test environment. |
| 124 | Env map[string]string |
| 125 | |
| 126 | // Stdin holds the standard input for the command. |
| 127 | Stdin []byte |
| 128 | } |
| 129 | |
| 130 | // CmdResult holds the result of running a plugin command. |
| 131 | type CmdResult struct { |
| 132 | // Stdout and Stderr hold the command's standard output and standard |
| 133 | // error respectively. |
| 134 | Stdout []byte |
| 135 | Stderr []byte |
| 136 | |
| 137 | // Env holds environment variables that the command wants to set in |
| 138 | // the test environment. Every key must be present in |
| 139 | // [PluginInfo.ResultEnv]. |
| 140 | Env map[string]string |
| 141 | |
| 142 | // Err, if non-empty, holds the error message from a failure of the |
| 143 | // command itself (for example a non-zero exit status). It is distinct |
| 144 | // from the error returned by [TestInstance.RunCmd], which indicates a |
| 145 | // failure of the plugin. |
| 146 | Err string |
| 147 | } |