hyperbolictest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 14 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

Overview

Package hyperbolictest provides an in-memory fake of the Hyperbolic API for tests, with fault injection for the failures that matter most to callers: rate limiting, 5xx responses, timeouts, and failures that happen after the server has already applied a request.

The fake's routes, parameters and payloads are generated from the same OpenAPI spec as the client, so they cannot drift from it. It models the rental lifecycle (rental options, create, get, list, reboot, terminate), saved SSH keys (save, list, rename, delete, and attach to or detach from a rental), the balance and the active rental summary. Other operations answer 501 Not Implemented. Its behavior follows what was observed against the live API. When the two disagree, the live API wins and this package should be fixed.

A typical test injects a failure and checks what the caller does with it. Here a create fails after the server has already created the rental:

func TestCreateRetryIsSafe(t *testing.T) {
	srv := hyperbolictest.NewServer(t)
	client := srv.Client(t)
	srv.InjectFault(hyperbolictest.Fault{
		Method: http.MethodPost, Path: hyperbolictest.PathRentals,
		Status: http.StatusBadGateway, AfterCommit: true,
	})

	_, err := client.Rentals.Create(ctx, params)
	// err matches hyperbolic.ErrOutcomeUnknown, the create was sent
	// once, and srv.ActiveRentals() holds exactly one rental.
}

Index

Constants

View Source
const (
	PathRentalOptions = "/v2/on-demand/rental-options"
	// PathRentals creates rentals (POST).
	PathRentals = "/v2/on-demand/rentals"
	// PathActiveRentals lists active rentals, a page at a time (GET).
	PathActiveRentals = "/v2/on-demand/rentals/active"
	PathTerminate     = "/v2/on-demand/rentals/terminate"
	PathReboot        = "/v2/on-demand/rentals/reboot-instance"
	PathBalance       = "/v2/customer/balance"
	// PathActiveRentalSummary counts active rentals and sums their hourly
	// cost (GET).
	PathActiveRentalSummary = "/v2/on-demand/active-rental-summary"
	// PathSSHKeys lists (GET) and saves (POST) SSH keys.
	PathSSHKeys      = "/v2/ssh-keys"
	PathSSHKeyDelete = "/v2/ssh-keys/delete"
	PathAttachSSHKey = "/v2/on-demand/rentals/ssh-keys/attach"
	PathDetachSSHKey = "/v2/on-demand/rentals/ssh-keys/detach"
)

Paths served by the fake, exported so tests can target faults precisely.

View Source
const DefaultAPIKey = "sk_test_hyperbolictest" //nolint:gosec // a fixture for the in-memory fake, not a credential. gitleaks:allow

DefaultAPIKey is the key the fake server accepts unless changed.

View Source
const DefaultSSHPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEh5cGVyYm9saWN0ZXN0LWRlZmF1bHQta2V5LTAwMDA hyperbolictest"

DefaultSSHPublicKey is the SSH key a new Server's account has saved, so a rental created without naming keys gets it, as on the live API.

Variables

This section is empty.

Functions

func DefaultRentalOptions

func DefaultRentalOptions() []hyperbolic.RentalOption

DefaultRentalOptions is the set of rental options a new Server starts with.

func VirtualMachineOption

VirtualMachineOption returns a rental option for a virtual machine.

Types

type Fault

type Fault struct {
	// Method and Path select the requests the fault applies to. Path is
	// matched without the query.
	Method string
	Path   string
	// Status is the HTTP status to respond with. Zero means respond normally
	// (useful with Delay alone).
	Status int
	// Code and Message fill the error body.
	Code    string
	Message string
	// RetryAfter sets the Retry-After header.
	RetryAfter time.Duration
	// Delay waits before responding, to simulate a slow server or a timeout.
	Delay time.Duration
	// AfterCommit applies the request first and then fails, which is how a
	// create can fail while the rental is actually created.
	AfterCommit bool
	// Times is how many matching requests the fault applies to. Zero means 1.
	Times int
}

Fault makes matching requests fail or misbehave.

type Server

type Server struct {
	// URL is the base URL of the fake API.
	URL string
	// APIKey is the key the fake accepts.
	APIKey string
	// contains filtered or unexported fields
}

Server is a fake Hyperbolic API backed by memory.

func NewServer

func NewServer(tb testing.TB) *Server

NewServer starts a fake API with a small default set of rental options and a $1,000 balance. It is closed automatically when the test ends.

func (*Server) ActiveRentals

func (s *Server) ActiveRentals() []hyperbolic.RentalWithStatus

ActiveRentals returns the rentals that are not terminated, newest first.

func (*Server) AddSSHKey

func (s *Server) AddSSHKey(publicKey, name string) hyperbolic.SSHKey

AddSSHKey saves an SSH key on the account, as someone would in the dashboard, and returns it. Unlike the API, it does not reject a duplicate.

func (*Server) Client

func (s *Server) Client(tb testing.TB, opts ...hyperbolic.Option) *hyperbolic.Client

Client returns a client pointed at the fake, with fast retries.

func (*Server) InjectFault

func (s *Server) InjectFault(f Fault)

InjectFault adds a fault. Faults are matched in the order they were added.

func (*Server) RentalSSHKeyIDs

func (s *Server) RentalSSHKeyIDs(rentalID string) []int

RentalSSHKeyIDs returns the ids of the SSH keys authorized on a rental.

func (*Server) Requests

func (s *Server) Requests(method, path string) int

Requests returns how many requests the fake received for method and path (without the query), including ones answered with a fault.

func (*Server) SSHKeys

func (s *Server) SSHKeys() []hyperbolic.SSHKey

SSHKeys returns the account's saved SSH keys, oldest first.

func (*Server) SetBalance

func (s *Server) SetBalance(balanceCents, maxOverdraftCents int64)

SetBalance sets the account balance and overdraft allowance, in cents.

func (*Server) SetMaxPageSize

func (s *Server) SetMaxPageSize(n int)

SetMaxPageSize caps the page size the active-rental list serves, so tests can exercise pagination without creating a hundred rentals.

func (*Server) SetRentalOptions

func (s *Server) SetRentalOptions(opts ...hyperbolic.RentalOption)

SetRentalOptions replaces the rental options.

func (*Server) SetRentalStatus

func (s *Server) SetRentalStatus(rentalID string, status hyperbolic.RentalStatus) bool

SetRentalStatus moves a rental to status. Moving to Running fills in one instance per 8 GPUs, with SSH endpoints. It reports whether the rental exists.

func (*Server) TerminateExternally

func (s *Server) TerminateExternally(rentalID string) bool

TerminateExternally ends a rental the way the platform does when the balance runs out or hardware fails: it disappears from the active list.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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