logtest

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Container(id string, rank int64, mods ...func(*logsv2.LogSource)) logsv2.LogSource

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

func NotDefault(s *logsv2.LogSource)

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

func WithInstance(v string) func(*logsv2.LogSource)

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

func New(t testing.TB, opts ...Option) *Harness

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

func (h *Harness) Add(s logsv2.LogSource)

Add reports a source that joined the catalog, and returns once the session handled it.

func (*Harness) Advance

func (h *Harness) Advance(d time.Duration)

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) Batches

func (h *Harness) Batches() []Batch

Batches returns the batches the session sent, in order.

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) Ended

func (h *Harness) Ended() (logsv2.Event, bool)

Ended returns the event that ended the session, if it came.

func (*Harness) Events

func (h *Harness) Events(t logsv2.EventType) []logsv2.Event

Events returns the events of a type that the session sent, in order.

func (*Harness) FlushLines

func (h *Harness) FlushLines(id string, n int) []string

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

func (h *Harness) LastSummary() (logsv2.Summary, bool)

LastSummary returns the counts of the last summary event, if there was one.

func (*Harness) Lines

func (h *Harness) Lines() []Line

Lines returns every line the session sent, in order, with its source.

func (*Harness) Remove

func (h *Harness) Remove(id string)

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) SessionID

func (h *Harness) SessionID() string

SessionID is the ID of the session, empty before Start.

func (*Harness) Snapshot

func (h *Harness) Snapshot() *logsv2.Session

Snapshot returns a snapshot of the session.

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

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) Summary

func (h *Harness) Summary() logsv2.Summary

Summary returns the counts of the session 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

func (h *Harness) Texts(id string) []string

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

func (h *Harness) Update(s logsv2.LogSource)

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

func (h *Harness) WaitClosed(id string)

WaitClosed waits until the SDK has closed the last stream of the source.

func (*Harness) WaitEnded

func (h *Harness) WaitEnded() logsv2.Event

WaitEnded waits for the event that ends the session, and returns it.

func (*Harness) WaitEvent

func (h *Harness) WaitEvent(t logsv2.EventType, n int) []logsv2.Event

WaitEvent waits until the session has sent n events of the type, and returns them.

func (*Harness) WaitLines

func (h *Harness) WaitLines(id string, n int) []string

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) WaitOpens

func (h *Harness) WaitOpens(id string, n int)

WaitOpens waits until the source has been opened n times.

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

func (h *Harness) WaitStreams(id string, n int)

WaitStreams waits until n streams of the source were opened, that is opened and not refused.

func (*Harness) WaitUntil

func (h *Harness) WaitUntil(what string, cond func() bool)

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 NewKind

func NewKind() *Kind

NewKind returns a kind with no script.

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

func (k *Kind) OpenOrder() []string

OpenOrder lists the source of every opening, in the order the openings came.

func (*Kind) Script

func (k *Kind) Script(id string) *Script

Script returns the script of the source with this id, made on first use.

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

func WithJitter(fn func(time.Duration) time.Duration) Option

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

func WithLimitCeiling(n int32) Option

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

func WithSources(sources ...logsv2.LogSource) Option

WithSources sets the sources the default resolver lists.

func WithTimeout

func WithTimeout(d time.Duration) Option

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

func NewResolver(t testing.TB, sources ...logsv2.LogSource) *Resolver

NewResolver returns a resolver that lists sources. t receives the failures of its helpers.

func (*Resolver) Add

func (r *Resolver) Add(s logsv2.LogSource)

Add reports a source that joined the catalog.

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

func (r *Resolver) Fail(err error)

Fail makes the calls that come after it return err; nil makes them succeed again.

func (*Resolver) Pending

func (r *Resolver) Pending() int

Pending counts the events that were sent and no session has taken yet.

func (*Resolver) Remove

func (r *Resolver) Remove(id string)

Remove reports that a source left the catalog.

func (*Resolver) Requests

func (r *Resolver) Requests() []logsv2.ResolveRequest

Requests returns the requests the resolver was called with, in order.

func (*Resolver) Resolve

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

func (r *Resolver) SetSources(sources ...logsv2.LogSource)

SetSources replaces the sources that later calls list; a session that exists is not affected.

func (*Resolver) Update

func (r *Resolver) Update(s logsv2.LogSource)

Update reports a change of the source with the same ID.

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

func (s *Script) Closed() bool

Closed reports whether the SDK closed the stream that was opened last. It is false for a source that has no stream.

func (*Script) End

func (s *Script) End()

End ends the stream after the lines written: io.EOF.

func (*Script) EndWith

func (s *Script) EndWith(reason string)

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

func (s *Script) EndWithError(err error)

EndWithError ends the stream after the lines written: err is what its Read returns, and nil is io.EOF.

func (*Script) EndedOnOpen

func (s *Script) EndedOnOpen(reason string)

EndedOnOpen makes every opening return a logsv2.EndedError that carries reason, until Ready.

func (*Script) Fail

func (s *Script) Fail(code, detail string)

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) FailWith

func (s *Script) FailWith(err error)

FailWith makes every opening return err, whatever it is, 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.

func (*Script) Ready

func (s *Script) Ready()

Ready makes the openings succeed again.

func (*Script) Release

func (s *Script) Release()

Release lets the held openings go on.

func (*Script) Streams

func (s *Script) Streams() int

Streams counts the streams that were opened for the source.

func (*Script) Write

func (s *Script) Write(lines ...string)

Write gives lines to the stream, each with its line break.

func (*Script) WriteRaw

func (s *Script) WriteRaw(b []byte)

WriteRaw gives bytes to the stream as they are: a line with no line break, say.

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.

Jump to

Keyboard shortcuts

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