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
- func DefaultRentalOptions() []hyperbolic.RentalOption
- func VirtualMachineOption(o hyperbolic.VirtualMachineOption) hyperbolic.RentalOption
- type Fault
- type Server
- func (s *Server) ActiveRentals() []hyperbolic.RentalWithStatus
- func (s *Server) AddSSHKey(publicKey, name string) hyperbolic.SSHKey
- func (s *Server) Client(tb testing.TB, opts ...hyperbolic.Option) *hyperbolic.Client
- func (s *Server) InjectFault(f Fault)
- func (s *Server) RentalSSHKeyIDs(rentalID string) []int
- func (s *Server) Requests(method, path string) int
- func (s *Server) SSHKeys() []hyperbolic.SSHKey
- func (s *Server) SetBalance(balanceCents, maxOverdraftCents int64)
- func (s *Server) SetMaxPageSize(n int)
- func (s *Server) SetRentalOptions(opts ...hyperbolic.RentalOption)
- func (s *Server) SetRentalStatus(rentalID string, status hyperbolic.RentalStatus) bool
- func (s *Server) TerminateExternally(rentalID string) bool
Constants ¶
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.
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.
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 ¶
func VirtualMachineOption(o hyperbolic.VirtualMachineOption) hyperbolic.RentalOption
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 ¶
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 ¶
InjectFault adds a fault. Faults are matched in the order they were added.
func (*Server) RentalSSHKeyIDs ¶
RentalSSHKeyIDs returns the ids of the SSH keys authorized on a rental.
func (*Server) Requests ¶
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 ¶
SetBalance sets the account balance and overdraft allowance, in cents.
func (*Server) SetMaxPageSize ¶
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 ¶
TerminateExternally ends a rental the way the platform does when the balance runs out or hardware fails: it disappears from the active list.