Documentation
¶
Overview ¶
Package cloudrig is a local emulator for Google Cloud APIs.
Start runs it as a server; MustStart runs it in-process inside a Go test:
func TestUpload(t *testing.T) {
t.Parallel()
emu := cloudrig.MustStart(t)
// ... point a GCP client at emu.BaseURL()
}
MustStart is the reason the project exists: no container, no daemon, and one isolated instance per test.
Index ¶
- Constants
- type Emulator
- func (e *Emulator) BaseURL() string
- func (e *Emulator) Clock() clock.Clock
- func (e *Emulator) CloudRun() *cloudrun.Registry
- func (e *Emulator) Endpoint() string
- func (e *Emulator) FakeClock(t testing.TB) *clock.FakeClock
- func (e *Emulator) Faults() *faults.Set
- func (e *Emulator) Fork(t testing.TB) *Emulator
- func (e *Emulator) FunctionURL(name string) string
- func (e *Emulator) Functions() *functions.Registry
- func (e *Emulator) Reset(ctx context.Context, project string) error
- func (e *Emulator) Shutdown(ctx context.Context) error
- func (e *Emulator) SyncEvents()
- func (e *Emulator) SyncScheduler()
- func (e *Emulator) SyncTasks()
- type Options
Constants ¶
const DefaultAddr = ":4599"
DefaultAddr binds every interface so the binary is reachable from a container. MustStart ignores it and takes a random free loopback port.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Emulator ¶
type Emulator struct {
// contains filtered or unexported fields
}
Emulator is a running instance.
func MustStart ¶
MustStart runs the emulator in-process for one test, on a random free port with a FakeClock and t.Cleanup registered. Every call is fully isolated.
func Start ¶
Start runs the emulator on a real listener; the caller owns shutdown. ctx bounds startup only and does not stop the server.
func (*Emulator) BaseURL ¶
BaseURL is the endpoint as a URL: what STORAGE_EMULATOR_HOST and the SDK endpoint overrides want.
func (*Emulator) FakeClock ¶
FakeClock returns the clock as a *clock.FakeClock so a test can Advance it, failing t if the emulator is running a real one.
func (*Emulator) Faults ¶
Faults is the live fault-injection rule set.
A rule armed here fails matching requests before they reach any service, so a test can prove its own retry and error handling rather than hoping the path is exercised.
func (*Emulator) Fork ¶
Fork returns a second emulator carrying a copy of this one's state.
Metadata is copied; object payloads are shared, because they are content-addressed and immutable — the same bytes under the same name. So a fork of a hundred gigabytes of objects costs the size of the metadata.
What is copied is state, not processes: deployed functions, armed faults and the clock do not travel. The fork gets its own port, its own event bus and its own fault set, and starts with no functions deployed.
func (*Emulator) FunctionURL ¶
FunctionURL is where the named function is served, or "" if it is not deployed. It returns the short form, valid for the default project and location; the prefixed form is also served.
func (*Emulator) Functions ¶
Functions is the registry backing this emulator, so a test can deploy into a running instance rather than only at Start.
func (*Emulator) SyncEvents ¶
func (e *Emulator) SyncEvents()
SyncEvents waits for every event published so far to reach its subscribers.
Delivery is asynchronous so a write never waits on a trigger. A test that uploads an object and then asserts a function ran needs this, or it would have to poll.
func (*Emulator) SyncScheduler ¶
func (e *Emulator) SyncScheduler()
SyncScheduler waits for every fired Cloud Scheduler HTTP job to finish, the same way SyncTasks does for tasks.
type Options ¶
type Options struct {
// Addr is the listen address. Empty means DefaultAddr for Start, a random
// free loopback port for MustStart.
Addr string
// Clock is the only source of time. Empty means real for Start, fake for
// MustStart: a test that cannot control time will eventually sleep.
Clock clock.Clock
// Version is reported by /_emu/health. Empty means "dev".
Version string
// Runner is "auto", "subprocess" or "none". "auto" resolves to
// "subprocess" when Functions is non-empty, else "none".
Runner string
// Functions are built and launched at Start, and stopped at Shutdown.
Functions []functions.Function
// FunctionLog receives function output. Nil discards it; use fn logs.
FunctionLog io.Writer
// EventLog receives the emulator's own messages, such as a watched
// function redeploying. Nil discards them.
EventLog io.Writer
// DataDir persists Cloud Storage across restarts: metadata snapshots and
// object content live under it. Empty keeps everything in memory and a
// temp directory, which is what a test wants — state surviving a test is a
// bug, not a feature.
DataDir string
}
Options configures an Emulator. The zero value is valid.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
cloudrig
command
Command cloudrig runs the emulator as a server: flags, environment and signals only.
|
Command cloudrig runs the emulator as a server: flags, environment and signals only. |
|
core
|
|
|
clock
Package clock is the only place permitted to read wall-clock time.
|
Package clock is the only place permitted to read wall-clock time. |
|
events
Package events is the in-process bus services use to reach each other.
|
Package events is the in-process bus services use to reach each other. |
|
faults
Package faults injects failures into the emulator's responses.
|
Package faults injects failures into the emulator's responses. |
|
gerr
Package gerr is cloudrig's canonical error type: a code, an explicit HTTP status, and the reason string clients branch on.
|
Package gerr is cloudrig's canonical error type: a code, an explicit HTTP status, and the reason string clients branch on. |
|
logring
Package logring keeps the last lines a child process wrote.
|
Package logring keeps the last lines a child process wrote. |
|
resource
Package resource encodes GCP resources as store keys.
|
Package resource encodes GCP resources as store keys. |
|
tmp
Package tmp puts every temporary directory the emulator makes under one process-owned root.
|
Package tmp puts every temporary directory the emulator makes under one process-owned root. |
|
examples
|
|
|
hello
Package hello is a sample HTTP Cloud Function.
|
Package hello is a sample HTTP Cloud Function. |
|
pubsub
command
Command pubsub-demo exercises the emulator's Pub/Sub the way an application would: nothing here names cloudrig, and the only configuration is PUBSUB_EMULATOR_HOST.
|
Command pubsub-demo exercises the emulator's Pub/Sub the way an application would: nothing here names cloudrig, and the only configuration is PUBSUB_EMULATOR_HOST. |
|
Package functions builds and runs Go Cloud Functions as child processes.
|
Package functions builds and runs Go Cloud Functions as child processes. |
|
services
|
|
|
cloudfunctions
Package cloudfunctions serves the Cloud Functions v1 REST API, so real gcloud and the GCP SDKs can drive the emulator.
|
Package cloudfunctions serves the Cloud Functions v1 REST API, so real gcloud and the GCP SDKs can drive the emulator. |
|
cloudlogging
Package cloudlogging is the Cloud Logging emulation.
|
Package cloudlogging is the Cloud Logging emulation. |
|
cloudrun
Package cloudrun runs Cloud Run services locally.
|
Package cloudrun runs Cloud Run services locally. |
|
cloudscheduler
Package cloudscheduler is the Cloud Scheduler emulation.
|
Package cloudscheduler is the Cloud Scheduler emulation. |
|
cloudtasks
Package cloudtasks is the Cloud Tasks emulation.
|
Package cloudtasks is the Cloud Tasks emulation. |
|
firestore
Package firestore is the Cloud Firestore emulation.
|
Package firestore is the Cloud Firestore emulation. |
|
gke
Package gke emulates the GKE cluster admin API, backed by a real local Kubernetes cluster rather than a stub.
|
Package gke emulates the GKE cluster admin API, backed by a real local Kubernetes cluster rather than a stub. |
|
pubsub
Package pubsub is the Cloud Pub/Sub emulation.
|
Package pubsub is the Cloud Pub/Sub emulation. |
|
secretmanager
Package secretmanager is the Secret Manager emulation.
|
Package secretmanager is the Secret Manager emulation. |
|
serviceusage
Package serviceusage tracks which APIs a project has enabled.
|
Package serviceusage tracks which APIs a project has enabled. |
|
storage
Package storage is the Cloud Storage semantics layer: buckets, objects, generations and preconditions.
|
Package storage is the Cloud Storage semantics layer: buckets, objects, generations and preconditions. |
|
Package store is cloudrig's metadata layer: a versioned compare-and-swap key/value map.
|
Package store is cloudrig's metadata layer: a versioned compare-and-swap key/value map. |
|
blob
Package blob stores object payloads as content-addressed files.
|
Package blob stores object payloads as content-addressed files. |
|
Package transport is cloudrig's front door: one handler on one port, serving REST over HTTP/1.1 and gRPC over cleartext HTTP/2 at once.
|
Package transport is cloudrig's front door: one handler on one port, serving REST over HTTP/1.1 and gRPC over cleartext HTTP/2 at once. |