Documentation
¶
Overview ¶
Package logtest helps the authors of a log plugin, contract v2, test it against the real manager of the SDK without a process, a network or a real clock.
It has three parts, which a plugin author may use together or apart:
- Resolver is a programmable logsv2.SourceResolver: it lists the sources it was given, and then reports Added, Updated and Removed events when the test says so, and closes its event channel on demand.
- Kind is a programmable source kind: each source has a Script that says which lines its stream gives, when it blocks, how it ends and how it fails.
- Harness builds a logsv2.Manager on a fake clock around a Resolver and a Kind (or around the author's own resources and kinds), opens one session, and records what the session sends: batches with their source tables resolved to source ids, events and summaries. Its wait helpers fail the test with a message that says what was awaited and what the session looked like, and never hang.
Time in a harness is a fake clock that the test moves (Harness.Advance), so nothing that a test asserts depends on how fast the machine is. A line waits logsv2.BatchDelay of that clock for its batch: a test that wants the line moves the clock (Harness.FlushLines).
Index ¶
- Constants
- func Container(id string, rank int64, mods ...func(*logsv2.LogSource)) logsv2.LogSource
- func NotDefault(s *logsv2.LogSource)
- func StateName(s logsv2.SourceState) string
- func WithInstance(v string) func(*logsv2.LogSource)
- type Batch
- type Harness
- func (h *Harness) Add(s logsv2.LogSource)
- func (h *Harness) Advance(d time.Duration)
- func (h *Harness) Batches() []Batch
- func (h *Harness) Close()
- func (h *Harness) CloseEvents()
- func (h *Harness) Ended() (logsv2.Event, bool)
- func (h *Harness) Events(t logsv2.EventType) []logsv2.Event
- func (h *Harness) FlushLines(id string, n int) []string
- func (h *Harness) LastState(id string) (logsv2.SourceInfo, bool)
- func (h *Harness) LastSummary() (logsv2.Summary, bool)
- func (h *Harness) Lines() []Line
- func (h *Harness) Remove(id string)
- func (h *Harness) Send(ev logsv2.SourceEvent)
- func (h *Harness) SessionID() string
- func (h *Harness) Snapshot() *logsv2.Session
- func (h *Harness) Source(id string) logsv2.SourceInfo
- func (h *Harness) Sources(req logsv2.ListSourcesRequest) *logsv2.SourcePage
- func (h *Harness) Start(opts ...SessionOption) *logsv2.Session
- func (h *Harness) State(id string) logsv2.SourceState
- func (h *Harness) Summary() logsv2.Summary
- func (h *Harness) Sync()
- func (h *Harness) Texts(id string) []string
- func (h *Harness) TryStart(opts ...SessionOption) (*logsv2.Session, error)
- func (h *Harness) Update(s logsv2.LogSource)
- func (h *Harness) UpdateSelection(sel logsv2.SourceSelection, limit int32) logsv2.SelectionResult
- func (h *Harness) WaitClosed(id string)
- func (h *Harness) WaitEnded() logsv2.Event
- func (h *Harness) WaitEvent(t logsv2.EventType, n int) []logsv2.Event
- func (h *Harness) WaitLines(id string, n int) []string
- func (h *Harness) WaitOpens(id string, n int)
- func (h *Harness) WaitState(id string, want logsv2.SourceState)
- func (h *Harness) WaitStreams(id string, n int)
- func (h *Harness) WaitUntil(what string, cond func() bool)
- type Kind
- type Line
- type Option
- func WithJitter(fn func(time.Duration) time.Duration) Option
- func WithKind(key string, k logsv2.SourceKind) Option
- func WithLimitCeiling(n int32) Option
- func WithResource(key string, r logsv2.LogResource) Option
- func WithSources(sources ...logsv2.LogSource) Option
- func WithTimeout(d time.Duration) Option
- type Resolver
- func (r *Resolver) Add(s logsv2.LogSource)
- func (r *Resolver) CloseEvents()
- func (r *Resolver) Fail(err error)
- func (r *Resolver) Pending() int
- func (r *Resolver) Remove(id string)
- func (r *Resolver) Requests() []logsv2.ResolveRequest
- func (r *Resolver) Resolve(pctx *types.PluginContext, req logsv2.ResolveRequest) (*logsv2.ResolveResult, error)
- func (r *Resolver) Send(ev logsv2.SourceEvent)
- func (r *Resolver) SetSources(sources ...logsv2.LogSource)
- func (r *Resolver) Update(s logsv2.LogSource)
- type Script
- func (s *Script) Block()
- func (s *Script) Closed() bool
- func (s *Script) End()
- func (s *Script) EndWith(reason string)
- func (s *Script) EndWithError(err error)
- func (s *Script) EndedOnOpen(reason string)
- func (s *Script) Fail(code, detail string)
- func (s *Script) FailWith(err error)
- func (s *Script) NotReady()
- func (s *Script) Opens() []logsv2.OpenRequest
- func (s *Script) Ready()
- func (s *Script) Release()
- func (s *Script) Streams() int
- func (s *Script) Write(lines ...string)
- func (s *Script) WriteRaw(b []byte)
- type SessionOption
- func OnResource(key, id string) SessionOption
- func WithFollow(f bool) SessionOption
- func WithLimit(n int32) SessionOption
- func WithPrevious() SessionOption
- func WithReplayMarkers() SessionOption
- func WithSelection(s logsv2.SourceSelection) SessionOption
- func WithSince(at time.Time) SessionOption
- func WithTail(budget int64) SessionOption
Constants ¶
const ( // WorkloadResource is the key of the resource a harness registers by default. WorkloadResource = "workload" // ContainerKind is the key of the source kind a harness registers by default, and the kind of // the sources that Container builds. ContainerKind = "container" )
Variables ¶
This section is empty.
Functions ¶
func Container ¶
Container builds a source of the default kind: one container whose id, label and pod are id. It is in the default rule and has an instance, so it can be opened. rank orders the sources that compete for slots; the mods change what the helper made.
func NotDefault ¶
NotDefault takes a source out of the default rule.
func StateName ¶
func StateName(s logsv2.SourceState) string
StateName names a source state: "waiting", "streaming" and so on.
func WithInstance ¶
WithInstance gives a source an instance; an empty one cannot be opened yet.
Types ¶
type Batch ¶
type Batch struct {
Entries []logsv2.SourceEntry
Lines []Line
}
Batch is a batch a session sent: the table entries it introduced, and its lines.
type Harness ¶
type Harness struct {
// Clock is the clock of the manager. Nothing but Advance and FlushLines moves it.
Clock *timeutil.FakeClock
// Manager is the manager under test.
Manager *logsv2.Manager
// Resolver lists the sources of the default resource.
Resolver *Resolver
// Kind opens the sources of the default kind.
Kind *Kind
// contains filtered or unexported fields
}
Harness runs the real logsv2.Manager on a fake clock, with one session at a time, and records what the session sends. It is built by New and closed by the cleanup of the test.
By default the manager serves the resource WorkloadResource, whose sources come from Resolver and whose kind ContainerKind is Kind. WithResource and WithKind register the plugin author's own code beside them, or instead of them.
func New ¶
New builds a harness around a manager that has no session yet and has its stream open: the output of every session goes to the harness's records. The cleanup of the test closes the manager.
func (*Harness) Add ¶
Add reports a source that joined the catalog, and returns once the session handled it.
func (*Harness) Advance ¶
Advance moves the fake clock by d, once the scheduler has armed its timers, and returns once the scheduler has handled the timers that this made due.
func (*Harness) Close ¶
func (h *Harness) Close()
Close closes the session, as the host does when its view goes away.
func (*Harness) CloseEvents ¶
func (h *Harness) CloseEvents()
CloseEvents closes the event channel of the default resolver, after the events sent before it, and returns once the session handled it.
func (*Harness) FlushLines ¶
FlushLines moves the fake clock in steps of logsv2.BatchDelay until the source has delivered n lines, system lines included. The clock moves as far as that takes: use WaitLines for a source whose lines are due without time passing.
func (*Harness) LastState ¶
func (h *Harness) LastState(id string) (logsv2.SourceInfo, bool)
LastState returns the last state event of a source, if there was one. A source that is only available, and was never selected, has none: use State for the state it has now.
func (*Harness) LastSummary ¶
LastSummary returns the counts of the last summary event, if there was one.
func (*Harness) Remove ¶
Remove reports that a source left the catalog, and returns once the session handled it.
func (*Harness) Send ¶
func (h *Harness) Send(ev logsv2.SourceEvent)
Send reports a change of the catalog through the default resolver and returns once the session has taken and handled it.
func (*Harness) Source ¶
func (h *Harness) Source(id string) logsv2.SourceInfo
Source returns what the session knows of a source now, whatever its state; it fails the test for a source the catalog does not hold.
func (*Harness) Sources ¶
func (h *Harness) Sources(req logsv2.ListSourcesRequest) *logsv2.SourcePage
Sources pages the catalog of the session.
func (*Harness) Start ¶
func (h *Harness) Start(opts ...SessionOption) *logsv2.Session
Start opens a following session on the default resource with the default selection, unless the options say otherwise, and fails the test if it cannot. A harness has one session.
func (*Harness) State ¶
func (h *Harness) State(id string) logsv2.SourceState
State returns the state a source has now.
func (*Harness) Sync ¶
func (h *Harness) Sync()
Sync returns once the scheduler of the session has handled everything handed to it before, and has armed its timers for what it knows. It does nothing for a session that was closed.
func (*Harness) Texts ¶
Texts returns the text of the lines of one source, system lines ("ended: ...") included.
func (*Harness) TryStart ¶
func (h *Harness) TryStart(opts ...SessionOption) (*logsv2.Session, error)
TryStart is Start for a test of the creation itself: it returns the error. A creation that fails leaves the harness without a session.
func (*Harness) Update ¶
Update reports a change of a source, and returns once the session handled it.
func (*Harness) UpdateSelection ¶
func (h *Harness) UpdateSelection(sel logsv2.SourceSelection, limit int32) logsv2.SelectionResult
UpdateSelection replaces the selection of the session, and its limit unless it is zero.
func (*Harness) WaitClosed ¶
WaitClosed waits until the SDK has closed the last stream of the source.
func (*Harness) WaitEvent ¶
WaitEvent waits until the session has sent n events of the type, and returns them.
func (*Harness) WaitLines ¶
WaitLines waits until the source has delivered n lines, system lines included, and returns their text. It does not move the clock: a line waits logsv2.BatchDelay of it for its batch, so a test that wants a line that is not due uses FlushLines.
func (*Harness) WaitState ¶
func (h *Harness) WaitState(id string, want logsv2.SourceState)
WaitState waits until a source has told the state in an event.
func (*Harness) WaitStreams ¶
WaitStreams waits until n streams of the source were opened, that is opened and not refused.
func (*Harness) WaitUntil ¶
WaitUntil returns once cond holds, and fails the test, saying what was awaited and how the session looks, if it does not hold within ten seconds of real time. cond reads the harness: Lines, Texts, State, Summary and the rest. It runs often and on the goroutine of the test.
type Kind ¶
type Kind struct {
// contains filtered or unexported fields
}
Kind is a programmable logsv2.SourceKind. Each source it opens has a Script, found by the id of the source, that says what the stream of the source does. A source that nobody scripted opens a stream that gives no line and stays open.
A Kind is safe for concurrent use.
func (*Kind) Open ¶
func (k *Kind) Open(pctx *types.PluginContext, req logsv2.OpenRequest) (io.ReadCloser, error)
Open is the logsv2.OpenFunc of the kind.
func (*Kind) OpenOrder ¶
OpenOrder lists the source of every opening, in the order the openings came.
func (*Kind) SourceKind ¶
func (k *Kind) SourceKind() logsv2.SourceKind
SourceKind returns the logsv2.SourceKind to register under the kind's key.
type Line ¶
type Line struct {
Source logsv2.SourceRef
// Text is the content of the line, system lines included (see Origin).
Text string
TimestampMicros int64
Level logsv2.LogLevel
Origin logsv2.LogLineOrigin
Replay bool
// Batch numbers the batch that carried the line, from 1.
Batch int
}
Line is a line a session sent, with its source resolved from the table of its batch.
type Option ¶
type Option func(*config)
Option configures New.
func WithJitter ¶
WithJitter spreads the delay of a reconnect. The default leaves delays as they are, so that a test knows when a retry is due.
func WithKind ¶
func WithKind(key string, k logsv2.SourceKind) Option
WithKind registers a source kind of the author's, or replaces the default one when the key is ContainerKind.
func WithLimitCeiling ¶
WithLimitCeiling sets the highest limit the default resource accepts; zero is the SDK's ceiling.
func WithResource ¶
func WithResource(key string, r logsv2.LogResource) Option
WithResource registers a resource of the author's, or replaces the default one when the key is WorkloadResource.
func WithSources ¶
WithSources sets the sources the default resolver lists.
func WithTimeout ¶
WithTimeout sets how long, in real time, a wait helper gives something that has to happen before it fails the test; the default is ten seconds. Nothing a test asserts depends on it.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver is a programmable logsv2.SourceResolver. A call lists the sources the resolver holds; when the call watches, Add, Update, Remove and Send report changes of the catalog to the session, and CloseEvents closes its event channel. The event channel also closes when the session ends, as the contract of logsv2.ResolveResult asks.
A Resolver is safe for concurrent use.
func NewResolver ¶
NewResolver returns a resolver that lists sources. t receives the failures of its helpers.
func (*Resolver) CloseEvents ¶
func (r *Resolver) CloseEvents()
CloseEvents closes the event channel of every watching session, after the events that were sent before it. The session goes on without a watch.
func (*Resolver) Fail ¶
Fail makes the calls that come after it return err; nil makes them succeed again.
func (*Resolver) Requests ¶
func (r *Resolver) Requests() []logsv2.ResolveRequest
Requests returns the requests the resolver was called with, in order.
func (*Resolver) Resolve ¶
func (r *Resolver) Resolve(pctx *types.PluginContext, req logsv2.ResolveRequest) (*logsv2.ResolveResult, error)
Resolve is the logsv2.SourceResolver of the resolver.
func (*Resolver) Send ¶
func (r *Resolver) Send(ev logsv2.SourceEvent)
Send queues a change of the catalog for every session that watches, and returns at once; the session takes it in order. It fails the test when no session watches, as one that does not follow, or one that has ended, takes no event.
func (*Resolver) SetSources ¶
SetSources replaces the sources that later calls list; a session that exists is not affected.
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script is what the stream of one source does. Write, End, EndWith and EndWithError act on the stream that is open; when none is, they are kept for the next opening, which starts with the lines written and ends where it was told to. FailWith, Fail, NotReady and EndedOnOpen make every opening after them return an error, until Ready. Block holds every opening until Release.
A Script is safe for concurrent use.
func (*Script) Block ¶
func (s *Script) Block()
Block holds the openings that come after it, and the ones that are held already, until Release. An opening that is held returns the error of its context when the session ends.
func (*Script) Closed ¶
Closed reports whether the SDK closed the stream that was opened last. It is false for a source that has no stream.
func (*Script) EndWith ¶
EndWith ends the stream after the lines written with a logsv2.EndedError that carries reason: the source has no stream to give and will have none.
func (*Script) EndWithError ¶
EndWithError ends the stream after the lines written: err is what its Read returns, and nil is io.EOF.
func (*Script) EndedOnOpen ¶
EndedOnOpen makes every opening return a logsv2.EndedError that carries reason, until Ready.
func (*Script) Fail ¶
Fail makes every opening return an error of the class code (NOT_FOUND, say, which fails the source for good; a class the SDK does not know as permanent is retried), until Ready.
func (*Script) NotReady ¶
func (s *Script) NotReady()
NotReady makes every opening return logsv2.ErrSourceNotReady, until Ready.
func (*Script) Opens ¶
func (s *Script) Opens() []logsv2.OpenRequest
Opens returns the requests of the openings, in order.
type SessionOption ¶
type SessionOption func(*logsv2.CreateSessionOptions)
SessionOption configures Start.
func OnResource ¶
func OnResource(key, id string) SessionOption
OnResource opens the session on another resource than the default one.
func WithFollow ¶
func WithFollow(f bool) SessionOption
WithFollow says whether the session follows: keeps its streams open and watches the catalog. The default is true.
func WithLimit ¶
func WithLimit(n int32) SessionOption
WithLimit sets how many sources stream at once; zero is the SDK's default.
func WithPrevious ¶
func WithPrevious() SessionOption
WithPrevious asks for the previous instance of every source.
func WithReplayMarkers ¶
func WithReplayMarkers() SessionOption
WithReplayMarkers asks for replay markers.
func WithSelection ¶
func WithSelection(s logsv2.SourceSelection) SessionOption
WithSelection sets the selection the session starts with; the default is logsv2.DefaultSelection.
func WithSince ¶
func WithSince(at time.Time) SessionOption
WithSince asks for the lines since a time.
func WithTail ¶
func WithTail(budget int64) SessionOption
WithTail sets the tail budget of the session.