Documentation
¶
Overview ¶
Package mockapi provides a mock Central API server for end-to-end testing. It returns fixture responses for each endpoint and tracks call counters that can be queried via the GET /test/assertions endpoint.
Index ¶
- Constants
- func GenerateSelfSignedTLSConfig() *tls.Config
- type AssertionCounters
- type Server
- func (s *Server) Assertions() AssertionCounters
- func (s *Server) BearerEnvelope() string
- func (s *Server) BroadcastSSE(envelope api.Envelope)
- func (s *Server) ExpectedBearerToken() string
- func (s *Server) GetState() api.NodeStateSnapshot
- func (s *Server) Handler() http.Handler
- func (s *Server) LastRequestBody(endpoint string) ([]byte, bool)
- func (s *Server) NSK() []byte
- func (s *Server) SetEndpointTTL(d time.Duration)
- func (s *Server) SetHeartbeatResponse(resp api.HeartbeatResponse)
- func (s *Server) SetState(state api.NodeStateSnapshot)
- func (s *Server) TLSHandler() http.Handler
Constants ¶
const DefaultKeepAliveInterval = 15 * time.Second
DefaultKeepAliveInterval is the default interval between SSE keep-alive comments.
Variables ¶
This section is empty.
Functions ¶
func GenerateSelfSignedTLSConfig ¶
GenerateSelfSignedTLSConfig creates a self-signed TLS configuration with an ECDSA key and X.509 certificate valid for "mock-api" and "localhost".
Types ¶
type AssertionCounters ¶
type AssertionCounters struct {
RegisterCount int64 `json:"registration_count"`
HeartbeatCount int64 `json:"heartbeat_count"`
StateCount int64 `json:"state_count"`
KeyRotateCount int64 `json:"key_rotate_count"`
CapabilitiesCount int64 `json:"capabilities_count"`
EndpointCount int64 `json:"endpoint_count"`
SecretsCount int64 `json:"secrets_count"`
SecretsRateLimitedCount int64 `json:"secrets_rate_limited_count"`
ReportPutCount int64 `json:"report_put_count"`
ReportDeleteCount int64 `json:"report_delete_count"`
ExecutionCallbackCount int64 `json:"execution_callback_count"`
ExecutionUploadCount int64 `json:"execution_upload_count"`
MetricsCount int64 `json:"metrics_count"`
LogsCount int64 `json:"logs_count"`
AuditCount int64 `json:"audit_count"`
SessionActivityCount int64 `json:"session_activity_count"`
IntegrityViolationCount int64 `json:"integrity_violation_count"`
InjectEventCount int64 `json:"inject_event_count"`
EventsRequestCount int64 `json:"events_request_count"`
LocalMetricsCount int64 `json:"local_metrics_count"`
LocalLogsCount int64 `json:"local_logs_count"`
LocalAuditCount int64 `json:"local_audit_count"`
// node that presents the wrong credential drives this above zero, which is
// exactly the defect the suite failed to catch before the gate existed.
UnauthorizedCount int64 `json:"unauthorized_count"`
}
AssertionCounters holds call counters for each tracked endpoint.
type Server ¶
type Server struct {
// KeepAliveInterval controls the SSE keep-alive comment interval.
// Defaults to DefaultKeepAliveInterval (15s). Set before calling Handler().
KeepAliveInterval time.Duration
// contains filtered or unexported fields
}
Server is a mock Central API server that returns fixture data.
func (*Server) Assertions ¶
func (s *Server) Assertions() AssertionCounters
Assertions returns a snapshot of the current call counters.
func (*Server) BearerEnvelope ¶ added in v0.4.0
BearerEnvelope returns the credential every authenticated route admits, in the exact form a client must present it. The e2e scripts fetch it from GET /test/bearer so the fixture keeps one source of truth for the node id and the nsk, and the `<env>` segment is deliberately not the agent's `plexd`: the gate gets its authority from the payload, not from the tag.
func (*Server) BroadcastSSE ¶
BroadcastSSE assigns the next stream sequence to the envelope, records it in the replay ring (dropping the oldest beyond eventRingCap), and fans it out to every connected client non-blocking. Recording happens even with zero clients and while descoped, so a later reconnect can replay from a cursor. A client whose channel is full is skipped to avoid blocking the broadcaster.
func (*Server) ExpectedBearerToken ¶
ExpectedBearerToken returns the expected bearer token (for testing).
func (*Server) GetState ¶
func (s *Server) GetState() api.NodeStateSnapshot
GetState returns the current state fixture with the peers, executions, and sessions slices normalized to non-nil so they always serialize as [] rather than null. The eight envelope blocks carry no omitempty, so all keys are emitted (null for unpopulated blocks).
The result is a SHALLOW copy: the policy, bridge, state, and reports pointers and the peers, executions, and sessions backing arrays alias the live fixture. Reading is safe — SetState replaces the whole struct under the mutex, so no caller observes a half-written fixture — but callers must NOT mutate through the returned pointers or slices, which would race the state handler serving a concurrent request. Change the fixture with SetState instead.
func (*Server) Handler ¶
Handler returns the http.Handler for use with httptest or a real listener. Every authenticated route is served behind the bearer-envelope gate, so the suite exercises the credential contract rather than assuming it.
func (*Server) LastRequestBody ¶
LastRequestBody returns the last captured request body for the given endpoint.
func (*Server) SetEndpointTTL ¶ added in v0.2.0
SetEndpointTTL updates the stale_after TTL applied by the endpoint handler.
func (*Server) SetHeartbeatResponse ¶
func (s *Server) SetHeartbeatResponse(resp api.HeartbeatResponse)
SetHeartbeatResponse updates the mutable heartbeat response fixture.
func (*Server) SetState ¶
func (s *Server) SetState(state api.NodeStateSnapshot)
SetState updates the mutable state fixture returned by GET /v1/nodes/{id}/state and seeds the execution state machine from the configured executions block, so an entry configured mid-flight resumes on the callback roster: a configured ack or started status seeds that state, while pending seeds nothing and the first legal callback for it stays ack. Seeding is skip-if-present — a re-posted snapshot must never regress an execution the node has already advanced. The two locks are never held at once: execMu is taken only after stateFixtureMu is released.
func (*Server) TLSHandler ¶
TLSHandler returns an http.Handler for the local endpoint TLS listener. It registers local endpoint routes plus test assertion/last-request routes.