yao/tai/sandbox/local.go
Max 43d4ace13c Add Tai SDK tests and update Makefile for Tai integration
- Introduce new Tai SDK tests in the GitHub workflows, requiring a Tai container with Docker socket mount for execution.
- Update the Makefile to include a dedicated target for running Tai SDK tests, enhancing test coverage for the Tai integration.
- Modify the Go module dependencies to include the pierrec/lz4 package, ensuring compatibility with the new tests.
- Adjust test folder selection logic in the Makefile to exclude additional directories, streamlining the testing process.
2026-03-03 20:49:34 +08:00

81 lines
2.1 KiB
Go

package sandbox
import (
"context"
"fmt"
"runtime"
"time"
"github.com/docker/docker/client"
)
type local struct {
core dockerCore
}
// NewLocal creates a Sandbox backed by a direct Docker daemon connection.
// addr can be "unix:///var/run/docker.sock", "tcp://host:port", or "" for platform default.
func NewLocal(addr string) (Sandbox, error) {
opts := []client.Opt{client.WithAPIVersionNegotiation()}
if addr != "" {
opts = append(opts, client.WithHost(addr))
} else {
opts = append(opts, client.FromEnv)
}
cli, err := client.NewClientWithOpts(opts...)
if err != nil {
return nil, fmt.Errorf("docker client: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := cli.Ping(ctx); err != nil {
cli.Close()
return nil, fmt.Errorf("docker ping: %w", err)
}
return &local{core: dockerCore{cli: cli}}, nil
}
func (l *local) Create(ctx context.Context, opts CreateOptions) (string, error) {
return l.core.create(ctx, opts, opts.VNC && needsPortMapping())
}
func (l *local) Start(ctx context.Context, id string) error {
return l.core.start(ctx, id)
}
func (l *local) Stop(ctx context.Context, id string, timeout time.Duration) error {
return l.core.stop(ctx, id, int(timeout.Seconds()))
}
func (l *local) Remove(ctx context.Context, id string, force bool) error {
return l.core.remove(ctx, id, force)
}
func (l *local) Exec(ctx context.Context, id string, cmd []string, opts ExecOptions) (*ExecResult, error) {
return l.core.exec(ctx, id, cmd, opts)
}
func (l *local) Inspect(ctx context.Context, id string) (*ContainerInfo, error) {
return l.core.inspect(ctx, id)
}
func (l *local) List(ctx context.Context, opts ListOptions) ([]ContainerInfo, error) {
return l.core.list(ctx, opts)
}
func (l *local) Close() error {
return l.core.cli.Close()
}
// needsPortMapping returns true on platforms where container IPs are not
// directly reachable (macOS Docker Desktop, Windows).
func needsPortMapping() bool {
return runtime.GOOS == "darwin" || runtime.GOOS == "windows"
}
func portStr(p int) string {
if p == 0 {
return ""
}
return fmt.Sprintf("%d", p)
}