Documentation
¶
Overview ¶
Package dynamodbfake provides a tiny in-memory HTTP server that mimics the subset of the DynamoDB JSON-RPC surface our test suites need. It exists so every package that talks to DynamoDB (apikeys, cost, etc.) can share a single fake instead of each test file forking its own copy and drifting.
The server dispatches on the `X-Amz-Target` header (e.g. PutItem) and is intentionally NOT a full-fidelity fake — it returns canned shapes that are just accurate enough for the AWS SDK's marshalling layer to round-trip.
Typical usage:
fake := dynamodbfake.New(t)
dynamodbfake.UseFakeDynamo(t, fake.URL())
store, err := NewStore(StoreConfig{TableName: "x", Region: "us-west-2"})
...
// optional: fake.FailOnce("PutItem", errors.New("ConditionalCheckFailedException"))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractDDBString ¶
ExtractDDBString reads a top-level DynamoDB attribute encoded as `{"S": "value"}` and returns the underlying string, or "" if the attribute is missing or of a different shape.
func UseFakeDynamo ¶
UseFakeDynamo points the AWS SDK at the given URL via env vars and neutralises the SDK's IMDS / config-file lookups so CI runs don't accidentally hit real AWS metadata. All env-var changes are scoped to the test via t.Setenv.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an in-memory DynamoDB fake. Tests use a fresh Server per test via New(t); the t.Cleanup hook tears down the underlying httptest.Server when the test ends.
httptest.Server runs handlers on multiple goroutines (each accepted connection becomes its own goroutine). The tables map is now guarded by mu so a single test that issues concurrent SDK calls (e.g. a goroutined CreateKey loop) does not race the map under -race. Prior to this every code path went through the same goroutine in the test body, but adding the race detector to CI exposes the issue immediately.
func New ¶
New starts a fake DynamoDB server and registers a t.Cleanup hook that closes the underlying httptest.Server when the test finishes.
func (*Server) FailOnce ¶
FailOnce queues a one-shot error for the next request whose X-Amz-Target operation suffix matches op. After firing, the entry is removed; subsequent requests for the same op succeed normally.
The error string is wrapped in DynamoDB's standard `__type` envelope so the AWS SDK classifies it as a service error (e.g. "ResourceNotFoundException").
func (*Server) InjectItem ¶
InjectItem stores a raw DynamoDB-encoded item directly so tests can drive error branches (disabled / expired entries) without going through the PutItem path. Item values must already be in AWS-SDK attribute form (e.g. {"S": "value"}).