testutil

package
v11.1.4-modfix Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultContextTimeout = time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type SignedInUser

type SignedInUser struct{}

func (SignedInUser) NewAnonymous

func (SignedInUser) NewAnonymous() (*user.SignedInUser, error)

func (SignedInUser) NewEditor

func (SignedInUser) NewEditor() (*user.SignedInUser, error)

func (SignedInUser) NewEmpty

func (SignedInUser) NewEmpty() (*user.SignedInUser, error)

func (SignedInUser) NewGrafanaAdmin

func (SignedInUser) NewGrafanaAdmin() (*user.SignedInUser, error)

func (SignedInUser) NewServiceAccount

func (SignedInUser) NewServiceAccount() (*user.SignedInUser, error)

func (SignedInUser) NewViewer

func (SignedInUser) NewViewer() (*user.SignedInUser, error)

type T

type T interface {
	Helper()
	Cleanup(func())
	Deadline() (time.Time, bool)
	Errorf(format string, args ...any)
	FailNow()
}

T provides a clean way to test the utilities of this package.

type TestContext

type TestContext interface {
	context.Context

	// Cancel cancels the context. The `Err` method and the `context.Cause`
	// function will return context.Canceled.
	Cancel()

	// CancelCause cancels the current context with the given cause. The `Err`
	// method will return context.Canceled and the `context.Cause` function will
	// return the given error.
	CancelCause(err error)

	// WithUser returns a derived user with the given user associated. To derive
	// a context without an associated user, pass a nil value.
	WithUser(*user.SignedInUser) TestContext
}

TestContext is a context.Context that can be canceled with or without a cause. This is only relevant for testing purposes.

func NewDefaultTestContext

func NewDefaultTestContext(t T) TestContext

NewDefaultTestContext calls NewTestContext with the provided `t` and a timeout of DefaultContextTimeout. This should work fine for most unit tests.

func NewTestContext

func NewTestContext(t T, deadline time.Time) TestContext

NewTestCtx returns a new TestContext with the following features:

  1. Provides a `deadline` argument which is especially useful in integration tests.
  2. It honours the `-timeout` flag of `go test` if it is given, so it will actually timeout at the earliest deadline (either the one resulting from the command line or the one resulting from the `deadline` argument).
  3. By default it has an empty user (i.e. the user at the UI login), so most of the code paths to be tested will not need any special setup, unless you need to test permissions. In that case, you can use any of the test users from the SignedInUser struct (all of them come from real payloads).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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