testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BinaryPath = "../../dist/spinner"

BinaryPath is the path to the compiled test binary

Functions

func CleanupGCSPrefix

func CleanupGCSPrefix(t *testing.T, bucket, prefix string)

CleanupGCSPrefix removes all objects under a given prefix in a GCS bucket. Used to clean up state/log objects created during tests.

func CleanupTestSpinnerDirs

func CleanupTestSpinnerDirs() error

CleanupTestSpinnerDirs removes all test-related spinner data directories. Matches directories containing "-test-" in the name (e.g., spinner-dockerfile-test-hello-world). This is intended to be called from TestMain for global cleanup after all tests.

func ComputeConfigHash

func ComputeConfigHash(project, zone, bucket string) string

ComputeConfigHash generates an 8-character hash from bake configuration. This hash ensures that images created with different configs get different names.

func DockerContainerExists

func DockerContainerExists(t *testing.T, name string) bool

DockerContainerExists checks if a container exists (running or stopped)

func DockerContainerRunning

func DockerContainerRunning(t *testing.T, name string) bool

DockerContainerRunning checks if a container is currently running

func DockerImageExists

func DockerImageExists(t *testing.T, image string) bool

DockerImageExists checks if a Docker image exists

func EnsureDockerRunning

func EnsureDockerRunning(t *testing.T)

EnsureDockerRunning checks that Docker daemon is accessible

func ExecInContainer

func ExecInContainer(t *testing.T, containerName string, command ...string) string

ExecInContainer runs a command in a container and returns the output

func GCPImageExists

func GCPImageExists(t *testing.T, project, name string) bool

GCPImageExists checks if a GCP Compute Engine image exists.

func GCPInstanceExists

func GCPInstanceExists(t *testing.T, project, zone, name string) bool

GCPInstanceExists checks if a GCP VM instance exists using gcloud CLI. Returns true if the instance exists (any state), false if not found.

func GCPInstanceMetadata

func GCPInstanceMetadata(t *testing.T, project, zone, name, key string) (string, bool)

GCPInstanceMetadata returns the value of a specific metadata key for a GCP VM instance. Returns the value and true if found, empty string and false if not found.

func GCPInstanceStatus

func GCPInstanceStatus(t *testing.T, project, zone, name string) string

GCPInstanceStatus returns the status of a GCP VM instance (e.g., "RUNNING", "TERMINATED"). Returns empty string if the instance doesn't exist.

func GCSObjectExists

func GCSObjectExists(t *testing.T, bucket, object string) bool

GCSObjectExists checks whether a GCS object exists.

func GenerateSharedImageName

func GenerateSharedImageName(version int, project, zone, bucket string) string

GenerateSharedImageName creates a deterministic image name based on version and config. Format: test-shared-v{version}-{hash} Version allows manual invalidation on major changes, hash ensures config match.

func GenerateTestContainerName

func GenerateTestContainerName(t *testing.T) string

GenerateTestContainerName creates a unique Docker container name for testing

func GenerateTestImageName

func GenerateTestImageName(t *testing.T) string

GenerateTestImageName creates a unique GCP image name for testing. Format: spinner-test-<timestamp> (valid GCP image name).

func GenerateTestImageTag

func GenerateTestImageTag(t *testing.T) string

GenerateTestImageTag creates a unique Docker image tag for testing Returns just the tag part (without the "spinner:" prefix) Use this with the setup command's --name flag

func GetBinaryPath

func GetBinaryPath() string

GetBinaryPath returns the path to the compiled spinner binary. If TestMain has built a shared binary, it returns that path. Otherwise, it falls back to the default BinaryPath.

func GetImageLabels

func GetImageLabels(t *testing.T, project, imageName string) map[string]string

GetImageLabels retrieves labels from a GCP Compute Engine image using gcloud CLI. Returns empty map if image doesn't exist or has no labels.

func GetSharedGCPImage

func GetSharedGCPImage(t *testing.T) string

GetSharedGCPImage returns the shared test image name created in TestMain. This is the preferred method for GCP integration tests to avoid recreating images (which takes 35+ minutes each).

The shared image is created once before all tests run and deleted after all tests complete. Individual tests create separate VM instances from this shared image, ensuring test isolation.

func IsDockerAvailable

func IsDockerAvailable() bool

IsDockerAvailable returns true if the Docker daemon is accessible.

func OverrideSecretStoreWithRealToken

func OverrideSecretStoreWithRealToken(githubToken string) (cleanup func(), err error)

OverrideSecretStoreWithRealToken creates a temporary secret store using the provided real GITHUB_TOKEN and a placeholder CLAUDE_CODE_OAUTH_TOKEN, then overrides SPINNER_SECRET_STORE and SPINNER_SECRET_PASSPHRASE for the caller's process. This is intended for tests that need to authenticate against GitHub with a real token. The returned cleanup function restores the previous env values.

func ReadGCSObject

func ReadGCSObject(t *testing.T, bucket, object string) []byte

ReadGCSObject reads the full contents of a GCS object.

func RemoveDockerContainer

func RemoveDockerContainer(t *testing.T, name string)

RemoveDockerContainer removes a Docker container if it exists

func RemoveDockerImage

func RemoveDockerImage(t *testing.T, image string)

RemoveDockerImage removes a Docker image if it exists

func RemoveGCPImage

func RemoveGCPImage(t *testing.T, project, name string)

RemoveGCPImage deletes a GCP Compute Engine image if it exists.

func RemoveGCPInstance

func RemoveGCPInstance(t *testing.T, project, zone, name string)

RemoveGCPInstance deletes a GCP VM instance if it exists.

func RunCommand

func RunCommand(t *testing.T, args ...string) (stdout string, stderr string, exitCode int)

RunCommand executes the spinner CLI with given arguments and returns output

func RunCommandExpectError

func RunCommandExpectError(t *testing.T, args ...string) (stdout string, stderr string, exitCode int)

RunCommandExpectError runs a command and requires it to fail

func RunCommandExpectSuccess

func RunCommandExpectSuccess(t *testing.T, args ...string) (stdout string, stderr string)

RunCommandExpectSuccess runs a command and requires it to succeed

func RunCommandWithEnv

func RunCommandWithEnv(t *testing.T, env map[string]string, args ...string) (stdout string, stderr string, exitCode int)

RunCommandWithEnv executes the spinner CLI with custom environment variables

func RunContainerWithImage

func RunContainerWithImage(t *testing.T, imageName string) string

RunContainerWithImage starts a container with the given image and returns the container name

func RunGCPSpinCommand

func RunGCPSpinCommand(t *testing.T, args ...string) (instanceName string, stdout string, stderr string)

RunGCPSpinCommand executes the spin command with GCP backend and returns the instance name from output.

func RunSpinCommand

func RunSpinCommand(t *testing.T, args ...string) (containerName string, stdout string, stderr string)

RunSpinCommand executes the spin command and returns the container name from output

func SetSharedBinaryAccessor

func SetSharedBinaryAccessor(accessor func() string)

SetSharedBinaryAccessor allows the integration package to register its binary accessor.

func SetSharedImageAccessor

func SetSharedImageAccessor(accessor func() string)

SetSharedImageAccessor allows the integration package to register its image accessor. This is called from init() in the integration package.

func SetupTestImage

func SetupTestImage(t *testing.T, setupArgs ...string) (imageTag string, imageName string)

SetupTestImage creates a test image with the given setup args and sets up cleanup. The CLI binary is built once in TestMain and reused across all tests.

func SetupTestSecretStore

func SetupTestSecretStore() (cleanup func(), err error)

SetupTestSecretStore creates a temporary encrypted secret store populated with test tokens required by `spinner spin` (GITHUB_TOKEN, CLAUDE_CODE_OAUTH_TOKEN). It also writes an empty .spinner.json config file and sets SPINNER_CONFIG_FILE so that test subprocesses do not inherit the developer's ~/.spinner.json defaults (e.g. model=...) that would cause test assertions to fail.

Sets the following in the test process environment (inherited by subprocesses):

  • SPINNER_SECRET_STORE — path to the temp store file
  • SPINNER_SECRET_PASSPHRASE — passphrase for the temp store
  • SPINNER_CONFIG_FILE — path to an empty .spinner.json (overrides config search)

Returns a cleanup function that removes temp files and restores env vars.

func SkipIfDockerNotAvailable

func SkipIfDockerNotAvailable(t *testing.T)

SkipIfDockerNotAvailable skips the test if Docker is not available

func Sleep

func Sleep(t *testing.T, seconds int)

Sleep pauses execution for the specified duration (convenience wrapper for tests).

func StopGCPInstance

func StopGCPInstance(t *testing.T, project, zone, name string)

StopGCPInstance stops a running GCP VM instance.

func ValidateSharedImage

func ValidateSharedImage(t *testing.T, project, imageName, expectedHash string) bool

ValidateSharedImage checks if an existing image matches the expected configuration. Returns true if the image exists AND has a matching spinner-config-hash label.

func WaitForGCPInstanceStatus

func WaitForGCPInstanceStatus(t *testing.T, project, zone, instanceName, expectedStatus string, timeoutSec int)

WaitForGCPInstanceStatus polls VM status until it reaches the expected status or timeout.

func WaitForGCSStateStatus

func WaitForGCSStateStatus(t *testing.T, bucket, instanceName, expectedStatus string, timeoutSec int)

WaitForGCSStateStatus polls GCS state.json until it reaches the expected status or timeout.

func WriteFile

func WriteFile(t *testing.T, path string, content string) error

WriteFile writes content to a file at the given path

func WriteGCSObject

func WriteGCSObject(t *testing.T, bucket, object string, data []byte)

WriteGCSObject writes data to a GCS object (for test setup, e.g., mock log files).

Types

type GCPTestConfig

type GCPTestConfig struct {
	Project        string
	Zone           string
	Bucket         string
	ServiceAccount string
}

GCPTestConfig holds the resolved configuration for GCP integration tests.

func GCPTestConfigFromEnv

func GCPTestConfigFromEnv() *GCPTestConfig

GCPTestConfigFromEnv reads GCP test configuration from environment variables. Returns nil if any required variable is missing.

func SkipIfGCPNotAvailable

func SkipIfGCPNotAvailable(t *testing.T) *GCPTestConfig

SkipIfGCPNotAvailable skips the test if GCP credentials or configuration are not available. It checks:

  1. Not in short mode
  2. Required env vars are set (SPINNER_TEST_GCP_PROJECT, SPINNER_TEST_GCP_BUCKET)
  3. Application Default Credentials (ADC) are available

Jump to

Keyboard shortcuts

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