Testing Go code¶
NewTestClient returns a normal *contree.Client and its offline
*contree.MockTransport. Application functions can accept *contree.Client
directly. The transport never opens a network connection.
Mock a response¶
Application code adds context when it returns an error:
func permissions(ctx context.Context, client *contree.Client) (map[string]bool, error) {
me, err := client.WhoAmI(ctx)
if err != nil {
return nil, fmt.Errorf("get token permissions: %w", err)
}
return me.Permissions, nil
}
Use the same concrete client type in a test:
func TestPermissions(t *testing.T) {
client, mock, err := contree.NewTestClient()
if err != nil {
t.Fatalf("create test client: %v", err)
}
err = mock.Mock(contree.OpWhoAmI, contree.WhoAmIResponse{
Permissions: map[string]bool{"spawn": true},
OperationsStat: map[string]int64{},
})
if err != nil {
t.Fatalf("mock token permissions: %v", err)
}
allowed, err := permissions(context.Background(), client)
if err != nil {
t.Fatalf("load permissions: %v", err)
}
if !allowed["spawn"] {
t.Fatal("expected spawn permission")
}
if calls := mock.CallsFor("WhoAmI"); len(calls) != 1 {
t.Fatalf("WhoAmI calls = %d, want 1", len(calls))
}
}
Import context, fmt, testing, and the contree package in the test file.
%w preserves the underlying error for errors.Is and errors.As.
Mock takes a generated operation descriptor, such as contree.OpWhoAmI.
The descriptor fixes the result type, so a result for a different operation
fails to compile. Mock serializes model values when configured. The real client then decodes
the response, including required-field checks. Include required maps and slices
even when empty. Use MockHTTP to supply malformed responses deliberately.
Queues and errors¶
Each operation has an independent response queue. Repeated Mock calls append
responses; the last response repeats indefinitely. This supports polling:
for _, status := range []contree.OperationStatus{
contree.OperationStatusExecuting,
contree.OperationStatusSuccess,
} {
err := mock.Mock(contree.OpGetOperationStatus, contree.OperationResponse{
UUID: contree.Some(operationID),
Status: contree.Some(status),
})
if err != nil {
return fmt.Errorf("mock operation status %s: %w", status, err)
}
}
MockError converts typed HTTP errors into responses. Other errors become
transport failures wrapped in APIConnectionError:
err := mock.MockError("GetFile", &contree.NotFoundError{
APIStatusError: &contree.APIStatusError{
StatusCode: http.StatusNotFound,
Details: "file not found",
},
})
if err != nil {
return fmt.Errorf("mock missing file: %w", err)
}
NewTestClient disables retries by default, like the normal client. Pass
WithRetry to test retries against queued statuses or transport errors.
CallsFor records each HTTP attempt, including retries and unmocked requests.
A zero-value Operation[T] fails during mock configuration.
Unknown names passed to MockHTTP or MockError also fail. Calling an unconfigured
operation returns NotMockedError, wrapped in APIConnectionError.
Helpers and recorded requests¶
Configure descriptors, such as contree.OpSpawnInstance or contree.OpListFiles.
SpawnInstanceWithResponse shares the SpawnInstance queue and call log.
Helpers execute normally: SpawnInstance uses SpawnInstanceWithResponse, IterFiles uses
ListFiles, and EnsureFile can use both GetFile and UploadFile.
err := mock.Mock(contree.OpSpawnInstance, contree.InstanceSpawnResponse{
UUID: contree.Some("operation-1"),
})
if err != nil {
return fmt.Errorf("mock instance creation: %w", err)
}
response, err := client.SpawnInstance(ctx, "echo hello", "tag:ubuntu:latest",
contree.SpawnInstanceOptions{Shell: true})
if err != nil {
return fmt.Errorf("spawn greeting command: %w", err)
}
operationID, ok := response.UUID.Value()
if !ok || operationID == "" {
return fmt.Errorf("spawn response has no operation ID")
}
fmt.Println(operationID)
for _, call := range mock.CallsFor("SpawnInstance") {
fmt.Println(call.Method, call.Path, string(call.Body))
}
Operation.Name() returns the canonical name for CallsFor, MockError, and
MockHTTP. For example, use contree.OpWhoAmI.Name() instead of "WhoAmI".
Calls and CallsFor return independent snapshots. Each MockCall includes
the operation name, method, URL, decoded path parameters, query, headers, and
serialized request body. Queues and snapshots support concurrent use.
Streams and raw HTTP responses¶
Use []OperationEvent for SSE and []byte for byte streams.
Use struct{}{} for operations with empty responses, such as CancelOperation. MockStream can return a read error after the prepared data:
err := mock.MockStream(contree.OpIterOperationEvents, []contree.OperationEvent{
{
ID: 1,
Ts: time.Now(),
Type: contree.OperationEventTypeStdout,
Data: contree.NewEventDataStreamFromText("hello\n"),
},
}, io.ErrUnexpectedEOF)
if err != nil {
return fmt.Errorf("mock interrupted event stream: %w", err)
}
The client decodes the event before it receives the read error. To test
reconnection, queue another event response and configure GetOperationStatus
for the follower’s terminal-state probe.
MockHTTP accepts an explicit status, headers, raw bytes, and an optional body
read error:
err := mock.MockHTTP("InspectImageArchive", contree.MockResponse{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": {"application/x-tar"}},
Body: tarBytes,
})
if err != nil {
return fmt.Errorf("mock image archive: %w", err)
}
Each call receives a fresh body. InspectImageArchiveRaw shares the
InspectImageArchive queue; InspectImageDownloadStream shares the
InspectImageDownload queue. Raw and decoded variants exercise their normal
gzip behavior when Content-Encoding: gzip is set.
For factories that construct their own clients, create a zero-value
MockTransport and pass it through WithHTTPClient. The test-client constructor
accepts ordinary client options, but always installs its offline transport last.
Live integration tests¶
Run the Go scenarios from the repository root with Go 1.27 or later:
CONTREE_TOKEN=... CONTREE_URL=... make test-live-go
Set CONTREE_PROJECT when the endpoint requires a project header. These tests
upload files, start instances, cancel operations, and persist an image to check
PreserveCwd. Use a test endpoint and token.
Pytest runs the scenarios in go/tests/test_go_integration.py. The gorun fixture
writes each Go program and a go.mod into tmp_path, then runs go run ..
A local replace directive selects this checkout’s Go client. Credentials pass
through the environment. Each program has a deadline and cleans up its running
operations. Missing credentials or permissions appear as explicit pytest skips.
The shared GitHub Actions integration job runs Python, JavaScript, and Go in separate pytest steps with the same repository secrets and concurrency limit.