utils

package
v0.0.0-...-54693ff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultBuildTimeout = 5 * time.Minute
	DefaultCPUCount     = int32(2)
	DefaultMemoryMB     = int32(1024)
)
View Source
const MaxConcurrentSandboxes = 100

MaxConcurrentSandboxes gates parallel sandbox creation in tests. The integration test team is granted extra capacity in seed.go (base tier 20 + addon 200 = 220), so this can stay well below that ceiling.

Variables

This section is empty.

Functions

func AcquireSandboxSlot

func AcquireSandboxSlot(t *testing.T) func()

AcquireSandboxSlot blocks until a sandbox slot is available and returns an idempotent release function. The slot is also released automatically via t.Cleanup, so callers that tie the sandbox lifetime to the test can simply ignore the return value. Callers that tear down sandboxes inline (loops, sequential creates) can call the returned function early to free the slot sooner.

func AddUserToTeam

func AddUserToTeam(t *testing.T, db *setup.Database, teamID uuid.UUID, userID string)

func BuildSimpleTemplate

func BuildSimpleTemplate(tb testing.TB, name string, reqEditors ...api.RequestEditorFn) *api.TemplateRequestResponseV3

BuildSimpleTemplate builds a simple template with Ubuntu 22.04 base image

func BuildTemplate

BuildTemplate builds a template with the given options and waits for it to complete

func CreateAPIKey

func CreateAPIKey(t *testing.T, ctx context.Context, _ *api.ClientWithResponses, userID string, teamID uuid.UUID) string

func CreateDir

func CreateDir(tb testing.TB, sbx *api.Sandbox, path string)

func CreateTeam

func CreateTeam(t *testing.T, db *setup.Database, teamName string) uuid.UUID

func CreateTeamWithUser

func CreateTeamWithUser(
	t *testing.T,
	db *setup.Database,
	teamName, userID string,
) uuid.UUID

func CreateTextFile

func CreateTextFile(tb testing.TB, path string, content string) (*bytes.Buffer, string)

func CreateUser

func CreateUser(t *testing.T, db *setup.Database) uuid.UUID

func ExecCommand

func ExecCommand(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, command string, args ...string) error

func ExecCommandAsDefaultUserWithOutput

func ExecCommandAsDefaultUserWithOutput(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, command string, args ...string) (string, error)

ExecCommandAsDefaultUserWithOutput runs a command without sending a user header, so envd falls back to the default user set at /init time. This is the path that exercises the template's default user/workdir (it differs from passing an explicit "user").

func ExecCommandAsRoot

func ExecCommandAsRoot(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, command string, args ...string) error

func ExecCommandAsRootWithOutput

func ExecCommandAsRootWithOutput(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, command string, args ...string) (string, error)

func ExecCommandWithCwd

func ExecCommandWithCwd(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, cwd *string, command string, args ...string) error

func ExecCommandWithOptions

func ExecCommandWithOptions(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, cwd *string, user string, command string, args ...string) error

func ExecCommandWithOutput

func ExecCommandWithOutput(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, cwd *string, user string, command string, args ...string) (string, error)

func MatchesAPIKeyMask

func MatchesAPIKeyMask(apiKey string, prefix string, maskPrefix string, maskSuffix string) bool

func NewRequest

func NewRequest(sbx *api.Sandbox, url *url.URL, port int, extraHeaders *http.Header) *http.Request

func RequestTemplateWithoutBuild

func RequestTemplateWithoutBuild(tb testing.TB, name string, reqEditors ...api.RequestEditorFn) *api.TemplateRequestResponseV3

RequestTemplateWithoutBuild creates a template without starting (or waiting for) its build. The template row, a build in the "waiting" state, the alias, and the default tag assignment all exist as soon as the request returns, so the template can be listed, updated (e.g. visibility), and deleted.

Use this instead of BuildSimpleTemplate in tests that only exercise template metadata or authorization and never need a runnable build - a real build boots a Firecracker VM and takes minutes, while this is a single API call.

func SetupSandboxWithCleanup

func SetupSandboxWithCleanup(t *testing.T, c *api.ClientWithResponses, options ...SandboxOption) *api.Sandbox

SetupSandboxWithCleanup creates a new sandbox and returns its data. It acquires a semaphore slot to stay within the tier's concurrent instance limit; the slot is released after the sandbox is torn down in t.Cleanup.

func TeardownSandbox

func TeardownSandbox(t *testing.T, c *api.ClientWithResponses, sandboxID string)

TeardownSandbox kills the sandbox with the given ID

func UploadFile

func UploadFile(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, path string, content string)

func UploadFileAs

func UploadFileAs(tb testing.TB, ctx context.Context, sbx *api.Sandbox, envdClient *setup.EnvdClient, path, content, username string)

UploadFileAs uploads content to path as username, for the paths the default user cannot write.

func WaitForBuildCompletion

func WaitForBuildCompletion(
	tb testing.TB,
	ctx context.Context,
	templateID string,
	buildID string,
	name string,
	logHandler BuildLogHandler,
	enableDebug bool,
	reqEditors ...api.RequestEditorFn,
)

WaitForBuildCompletion waits for a template build to complete

func WaitForStatus

func WaitForStatus(tb testing.TB, client *http.Client, sbx *api.Sandbox, url *url.URL, port int, headers *http.Header, expectedStatus int) *http.Response

Types

type BuildLogHandler

type BuildLogHandler func(alias string, entry api.BuildLogEntry)

BuildLogHandler is a function that handles build log entries

func DefaultBuildLogHandler

func DefaultBuildLogHandler(tb testing.TB) BuildLogHandler

DefaultBuildLogHandler returns a default log handler that logs to the test logger

func NoOpBuildLogHandler

func NoOpBuildLogHandler() BuildLogHandler

NoOpBuildLogHandler returns a log handler that does nothing

type SandboxConfig

type SandboxConfig struct {
	// contains filtered or unexported fields
}

type SandboxOption

type SandboxOption func(config *SandboxConfig)

func WithAllowInternetAccess

func WithAllowInternetAccess(allow bool) SandboxOption

func WithAutoPause

func WithAutoPause(autoPause bool) SandboxOption

func WithAutoPauseMemory

func WithAutoPauseMemory(memory bool) SandboxOption

WithAutoPauseMemory controls the snapshot kind taken when the sandbox auto-pauses on timeout. false requests a filesystem-only auto-pause.

func WithAutoResume

func WithAutoResume(enabled bool) SandboxOption

func WithMetadata

func WithMetadata(metadata api.SandboxMetadata) SandboxOption

func WithNetwork

func WithNetwork(network *api.SandboxNetworkConfig) SandboxOption

func WithSecure

func WithSecure(secure bool) SandboxOption

func WithTemplateID

func WithTemplateID(templateID string) SandboxOption

func WithTimeout

func WithTimeout(timeout int32) SandboxOption

func WithoutAnyMetadata

func WithoutAnyMetadata() SandboxOption

type TemplateBuildOptions

type TemplateBuildOptions struct {
	Name        string
	Tags        *[]string
	CPUCount    *int32
	MemoryMB    *int32
	BuildData   api.TemplateBuildStartV2
	Timeout     time.Duration
	LogHandler  BuildLogHandler
	EnableDebug bool
	ReqEditors  []api.RequestEditorFn
}

TemplateBuildOptions contains options for building a template

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL