dynamodbfake

package
v0.0.0-...-caa33fe Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 7 Imported by: 0

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

func ExtractDDBString(m map[string]any, k string) string

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

func UseFakeDynamo(t *testing.T, url string)

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

func New(t *testing.T) *Server

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

func (f *Server) FailOnce(op string, err error)

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

func (f *Server) InjectItem(table, pk string, item map[string]any)

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"}).

func (*Server) URL

func (f *Server) URL() string

URL returns the base URL of the fake server. Pass it to UseFakeDynamo (or set AWS_ENDPOINT_URL_DYNAMODB directly) so the AWS SDK targets the fake instead of real AWS.

Jump to

Keyboard shortcuts

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