Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Fake

Package: openshell/v1/fake

The fake package provides an in-memory fake implementation of all SDK client interfaces for use in consumer test suites. It follows the client-go/kubernetes/fake pattern: in-memory stores, watch event broadcasting, and matching StatusError codes for equivalent error conditions (NotFound, AlreadyExists, Unavailable, Unimplemented).

Quick Start

import "github.com/rhuss/openshell-sdk-go/openshell/v1/fake"

func TestSandboxLifecycle(t *testing.T) {
    client := fake.NewClient()
    defer client.Close()

    ctx := context.Background()

    sb, err := client.Sandboxes().Create(ctx, "default", "my-sandbox", &v1.SandboxSpec{}, nil)
    require.NoError(t, err)
    assert.Equal(t, types.SandboxProvisioning, sb.Status.Phase)

    sb, err = client.Sandboxes().WaitReady(ctx, "default", "my-sandbox")
    require.NoError(t, err)
    assert.Equal(t, types.SandboxReady, sb.Status.Phase)

    require.NoError(t, client.Sandboxes().Delete(ctx, "default", "my-sandbox"))
}

Creating a Client

func NewClient(opts ...ClientOption) *Client

Returns a fake client implementing v1.ClientInterface with all sub-clients wired up. Default health result is healthy. Use options to customize initial state:

client := fake.NewClient(
    fake.WithHealthResult(&types.HealthResult{Healthy: false}),
    fake.WithCurrentUser(&types.CurrentUser{Subject: "test-user"}),
    fake.WithGatewayInfo(&types.GatewayInfo{Version: "1.0.0"}),
)

Pre-populating State

Seed objects directly into the fake stores for test setup:

client := fake.NewClient()

client.AddSandbox("default", &types.Sandbox{
    Name:   "pre-existing",
    Status: types.SandboxStatus{Phase: types.SandboxReady},
})

client.AddProvider("default", &types.Provider{
    Name: "my-provider",
    Spec: types.ProviderSpec{Type: "docker"},
})

client.AddWorkspace(&types.Workspace{Name: "staging"})
client.AddMember("staging", &types.WorkspaceMember{
    PrincipalSubject: "subject-123",
    Role:             types.WorkspaceRoleAdmin,
})

All Add* methods deep-copy their arguments; mutating the input after insertion does not affect the stored object.

Sub-Client Coverage

The fake client implements every interface in v1.ClientInterface:

AccessorInterfaceBehavior
Sandboxes()SandboxInterfaceFull CRUD, Watch, WaitReady
Providers()ProviderInterfaceFull CRUD, Ensure
Workspaces()WorkspaceInterfaceFull CRUD, Members
Health()HealthInterfaceConfigurable result
Inference()InferenceInterfaceRoute CRUD
Policy()PolicyInterfaceList, GetStatus (draft ops return Unimplemented)
Exec()ExecInterfaceReturns Unimplemented
Files()FileInterfaceReturns Unimplemented
Services()ServiceInterfaceReturns Unimplemented
SSH()SSHInterfaceInput validation, then Unimplemented
TCP()TCPInterfaceInput validation, then Unimplemented
Config()ConfigInterfaceReturns Unimplemented

ClientOption

ConstructorEffect
WithHealthResult(r)Set the health check return value
WithCurrentUser(u)Set the current user return value
WithGatewayInfo(i)Set the gateway info return value

Thread Safety

All operations are safe for concurrent use from multiple goroutines. Close is idempotent and causes all subsequent operations to return Unavailable.

See also: Testing Guide