testutil

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package testutil provides testing utilities for the Dash0 CLI.

Index

Constants

View Source
const (
	// Dashboards fixtures
	FixtureDashboardsListSuccess   = "dashboards/list_success.json"
	FixtureDashboardsListEmpty     = "dashboards/list_empty.json"
	FixtureDashboardsGetSuccess    = "dashboards/get_success.json"
	FixtureDashboardsImportSuccess = "dashboards/import_success.json"
	FixtureDashboardsNotFound      = "dashboards/error_not_found.json"
	FixtureDashboardsUnauthorized  = "dashboards/error_unauthorized.json"

	// Check rules fixtures
	FixtureCheckRulesListSuccess   = "checkrules/list_success.json"
	FixtureCheckRulesListEmpty     = "checkrules/list_empty.json"
	FixtureCheckRulesGetSuccess    = "checkrules/get_success.json"
	FixtureCheckRulesImportSuccess = "checkrules/import_success.json"
	FixtureCheckRulesNotFound      = "checkrules/error_not_found.json"

	// Views fixtures
	FixtureViewsListSuccess   = "views/list_success.json"
	FixtureViewsListEmpty     = "views/list_empty.json"
	FixtureViewsGetSuccess    = "views/get_success.json"
	FixtureViewsImportSuccess = "views/import_success.json"
	FixtureViewsNotFound      = "views/error_not_found.json"

	// Logs fixtures
	FixtureLogsQuerySuccess = "logs/query_success.json"
	FixtureLogsQueryEmpty   = "logs/query_empty.json"

	// Spans fixtures
	FixtureSpansQuerySuccess = "spans/query_success.json"
	FixtureSpansQueryEmpty   = "spans/query_empty.json"

	// Traces fixtures
	FixtureTracesGetSuccess   = "traces/get_success.json"
	FixtureTracesGetWithLinks = "traces/get_with_links.json"

	// Members fixtures
	FixtureMembersListSuccess  = "members/list_success.json"
	FixtureMembersListEmpty    = "members/list_empty.json"
	FixtureMembersUnauthorized = "members/error_unauthorized.json"

	// Synthetic checks fixtures
	FixtureSyntheticChecksListSuccess   = "syntheticchecks/list_success.json"
	FixtureSyntheticChecksListEmpty     = "syntheticchecks/list_empty.json"
	FixtureSyntheticChecksGetSuccess    = "syntheticchecks/get_success.json"
	FixtureSyntheticChecksImportSuccess = "syntheticchecks/import_success.json"
	FixtureSyntheticChecksNotFound      = "syntheticchecks/error_not_found.json"

	// Notification channels fixtures
	FixtureNotificationChannelsListSuccess   = "notificationchannels/list_success.json"
	FixtureNotificationChannelsListEmpty     = "notificationchannels/list_empty.json"
	FixtureNotificationChannelsGetSuccess    = "notificationchannels/get_success.json"
	FixtureNotificationChannelsCreateSuccess = "notificationchannels/create_success.json"
	FixtureNotificationChannelsNotFound      = "notificationchannels/error_not_found.json"
	FixtureNotificationChannelsUnauthorized  = "notificationchannels/error_unauthorized.json"

	// Recording rules fixtures
	FixtureRecordingRulesListSuccess   = "recordingrules/list_success.json"
	FixtureRecordingRulesListEmpty     = "recordingrules/list_empty.json"
	FixtureRecordingRulesGetSuccess    = "recordingrules/get_success.json"
	FixtureRecordingRulesCreateSuccess = "recordingrules/create_success.json"
	FixtureRecordingRulesNotFound      = "recordingrules/error_not_found.json"

	// Teams fixtures
	FixtureTeamsListSuccess   = "teams/list_success.json"
	FixtureTeamsListEmpty     = "teams/list_empty.json"
	FixtureTeamsGetSuccess    = "teams/get_success.json"
	FixtureTeamsCreateSuccess = "teams/create_success.json"
	FixtureTeamsNotFound      = "teams/error_not_found.json"
	FixtureTeamsUnauthorized  = "teams/error_unauthorized.json"

	// Spam filters fixtures
	FixtureSpamFiltersListSuccess   = "spamfilters/list_success.json"
	FixtureSpamFiltersListEmpty     = "spamfilters/list_empty.json"
	FixtureSpamFiltersGetSuccess    = "spamfilters/get_success.json"
	FixtureSpamFiltersCreateSuccess = "spamfilters/create_success.json"
	FixtureSpamFiltersNotFound      = "spamfilters/error_not_found.json"
)

Fixture paths relative to the fixtures directory.

Variables

This section is empty.

Functions

func BuildGitScenario added in v1.17.0

func BuildGitScenario(t *testing.T, name string) (repoDir, ref string)

BuildGitScenario reads the named git-scenario fixture (internal/testutil/fixtures/git-scenarios/<name>.yml) and replays its commit history into a fresh repository under t.TempDir(). It returns the repository directory and the ref the scenario wants tests to pass as --since.

This is the one thing every test tier (unit, integration, e2e) calls — no tier re-derives or hand-rolls its own git repo setup, so all three build the exact same repo history per scenario. Building the repo fresh from a declarative description (rather than unpacking a checked-in binary artifact) keeps each scenario's history readable and diffable in review, with nothing to regenerate when a scenario's shape changes.

func CaptureStderr added in v1.10.0

func CaptureStderr(t *testing.T, fn func()) string

CaptureStderr captures stderr during the execution of the given function. It returns the captured output as a string.

func CaptureStdout

func CaptureStdout(t *testing.T, fn func()) string

CaptureStdout captures stdout during the execution of the given function. It returns the captured output as a string.

func FixturesDir

func FixturesDir() string

FixturesDir returns the absolute path to the fixtures directory.

func GitScenariosDir added in v1.17.0

func GitScenariosDir() string

GitScenariosDir returns the absolute path to the git-scenarios fixtures directory.

func RequireAuthHeader

func RequireAuthHeader(r *http.Request) error

RequireAuthHeader is a validator that checks for the presence of an Authorization header.

func RequireHeaders added in v1.5.0

func RequireHeaders(r *http.Request) error

RequireHeaders is a validator that checks for both the Authorization and User-Agent headers.

func RequireUserAgentHeader added in v1.5.0

func RequireUserAgentHeader(r *http.Request) error

RequireUserAgentHeader is a validator that checks that the User-Agent header starts with "dash0-cli/".

func SetupTestEnv

func SetupTestEnv(t *testing.T)

SetupTestEnv sets environment variables for testing and returns a cleanup function.

Types

type GitChange added in v1.17.0

type GitChange struct {
	Op   GitChangeOp `yaml:"op"`
	Name string      `yaml:"name"`
	// Content is the file's new full content. Required for "add" and
	// "modify"; must be empty for "delete".
	Content string `yaml:"content,omitempty"`
}

GitChange is one file-level change within a commit. Op is explicit rather than inferred (e.g. from the same Name reappearing with different Content in a later commit) so a fixture's history reads correctly on its own, without cross-referencing earlier commits, and so BuildGitScenario can catch an inconsistent fixture (e.g. "add" on a file that already exists) at build time.

type GitChangeOp added in v1.17.0

type GitChangeOp string

GitChangeOp names what a GitChange does to a file.

const (
	GitChangeAdd    GitChangeOp = "add"
	GitChangeModify GitChangeOp = "modify"
	GitChangeDelete GitChangeOp = "delete"
)

type GitCommit added in v1.17.0

type GitCommit struct {
	// Label optionally names this commit so SinceRef or a later commit's
	// ResetTo can refer back to it.
	Label string `yaml:"label,omitempty"`
	// ResetTo, if set, hard-resets the working tree to the labeled commit
	// before applying this commit's Changes -- simulating a force-push/
	// history rewrite that leaves earlier commits orphaned (still
	// resolvable by SHA, but no longer an ancestor of HEAD).
	ResetTo string      `yaml:"resetTo,omitempty"`
	Message string      `yaml:"message"`
	Changes []GitChange `yaml:"changes,omitempty"`
}

GitCommit describes one commit: an ordered list of file changes applied to the working tree, then committed.

type GitRepo added in v1.17.0

type GitRepo struct {
	Commits []GitCommit `yaml:"commits"`
}

GitRepo is an ordered list of commits to apply to a fresh repository.

type GitRepoFixture added in v1.17.0

type GitRepoFixture struct {
	Kind string             `yaml:"kind"`
	Spec GitRepoFixtureSpec `yaml:"spec"`
}

GitRepoFixture declaratively describes a git repository's history for a `--since` test scenario. See internal/testutil/fixtures/git-scenarios/ for examples, and BuildGitScenario for how one is turned into a real repo.

type GitRepoFixtureSpec added in v1.17.0

type GitRepoFixtureSpec struct {
	// SinceRef is the --since value this scenario is designed to be tested
	// with: either the Label of one of Repo.Commits, or a literal ref value
	// (e.g. git's all-zeros sentinel for a "first push" scenario) used
	// as-is when it doesn't match any commit's label.
	SinceRef string  `yaml:"sinceRef"`
	Repo     GitRepo `yaml:"repo"`
}

GitRepoFixtureSpec is a GitRepoFixture's body.

type MockResponse

type MockResponse struct {
	// StatusCode is the HTTP status code to return.
	StatusCode int
	// BodyFile is the path to a fixture file containing the response body.
	// If empty, Body is used instead.
	BodyFile string
	// Body is the response body to return if BodyFile is not set.
	Body interface{}
	// Validator is an optional function to validate the incoming request.
	// If it returns an error, the mock server returns a 400 Bad Request.
	Validator func(r *http.Request) error
}

MockResponse defines how the mock server should respond to a request.

type MockServer

type MockServer struct {
	*httptest.Server
	// contains filtered or unexported fields
}

MockServer is a test HTTP server that serves responses from fixtures.

func NewMockServer

func NewMockServer(t *testing.T, fixturesDir string) *MockServer

NewMockServer creates a new mock server for testing. The fixturesDir parameter specifies the base directory for fixture files.

func (*MockServer) LastRequest

func (m *MockServer) LastRequest() *RecordedRequest

LastRequest returns the most recent request, or nil if no requests were made.

func (*MockServer) On

func (m *MockServer) On(method, path string, resp MockResponse) *MockServer

On registers a mock response for a specific method and path.

func (*MockServer) OnDefault

func (m *MockServer) OnDefault(handler http.HandlerFunc) *MockServer

OnDefault sets a default handler for unmatched requests.

func (*MockServer) OnPattern

func (m *MockServer) OnPattern(method string, pattern *regexp.Regexp, resp MockResponse) *MockServer

OnPattern registers a mock response for requests matching a regex pattern.

func (*MockServer) RequestBodies added in v1.16.5

func (m *MockServer) RequestBodies(t *testing.T, method, path string) []map[string]any

RequestBodies returns the decoded JSON bodies of all recorded requests matching method and path, in the order they were received. Use this instead of LastRequest when a command issues more than one request to the same route and the test needs to inspect all of them, not just the last.

func (*MockServer) Requests

func (m *MockServer) Requests() []RecordedRequest

Requests returns all recorded requests.

func (*MockServer) Reset

func (m *MockServer) Reset()

Reset clears all recorded requests.

func (*MockServer) WithCheckRulesCreate

func (m *MockServer) WithCheckRulesCreate(fixture string) *MockServer

WithCheckRulesCreate sets up the mock server to accept check rule creation.

func (*MockServer) WithCheckRulesDelete

func (m *MockServer) WithCheckRulesDelete() *MockServer

WithCheckRulesDelete sets up the mock server to accept check rule deletion.

func (*MockServer) WithCheckRulesGet

func (m *MockServer) WithCheckRulesGet(fixture string) *MockServer

WithCheckRulesGet sets up the mock server to return a check rule by ID.

func (*MockServer) WithCheckRulesList

func (m *MockServer) WithCheckRulesList(fixture string) *MockServer

WithCheckRulesList sets up the mock server to return a list of check rules.

func (*MockServer) WithCheckRulesUpdate added in v1.7.1

func (m *MockServer) WithCheckRulesUpdate(fixture string) *MockServer

WithCheckRulesUpdate sets up the mock server to accept check rule updates.

func (*MockServer) WithDashboardsCreate

func (m *MockServer) WithDashboardsCreate(fixture string) *MockServer

WithDashboardsCreate sets up the mock server to accept dashboard creation.

func (*MockServer) WithDashboardsDelete

func (m *MockServer) WithDashboardsDelete() *MockServer

WithDashboardsDelete sets up the mock server to accept dashboard deletion.

func (*MockServer) WithDashboardsGet

func (m *MockServer) WithDashboardsGet(fixture string) *MockServer

WithDashboardsGet sets up the mock server to return a dashboard by ID.

func (*MockServer) WithDashboardsList

func (m *MockServer) WithDashboardsList(fixture string) *MockServer

WithDashboardsList sets up the mock server to return a list of dashboards.

func (*MockServer) WithDashboardsUpdate

func (m *MockServer) WithDashboardsUpdate(fixture string) *MockServer

WithDashboardsUpdate sets up the mock server to accept dashboard updates.

func (*MockServer) WithNotFound

func (m *MockServer) WithNotFound(fixture string) *MockServer

WithNotFound sets up any unmatched route to return 404.

func (*MockServer) WithRecordingRulesCreate added in v1.11.0

func (m *MockServer) WithRecordingRulesCreate(fixture string) *MockServer

WithRecordingRulesCreate sets up the mock server to accept recording rule creation.

func (*MockServer) WithRecordingRulesDelete added in v1.11.0

func (m *MockServer) WithRecordingRulesDelete() *MockServer

WithRecordingRulesDelete sets up the mock server to accept recording rule deletion.

func (*MockServer) WithRecordingRulesGet added in v1.11.0

func (m *MockServer) WithRecordingRulesGet(fixture string) *MockServer

WithRecordingRulesGet sets up the mock server to return a recording rule by ID.

func (*MockServer) WithRecordingRulesList added in v1.11.0

func (m *MockServer) WithRecordingRulesList(fixture string) *MockServer

WithRecordingRulesList sets up the mock server to return a list of recording rules.

func (*MockServer) WithRecordingRulesUpdate added in v1.11.0

func (m *MockServer) WithRecordingRulesUpdate(fixture string) *MockServer

WithRecordingRulesUpdate sets up the mock server to accept recording rule updates.

func (*MockServer) WithSyntheticChecksCreate

func (m *MockServer) WithSyntheticChecksCreate(fixture string) *MockServer

WithSyntheticChecksCreate sets up the mock server to accept synthetic check creation.

func (*MockServer) WithSyntheticChecksDelete

func (m *MockServer) WithSyntheticChecksDelete() *MockServer

WithSyntheticChecksDelete sets up the mock server to accept synthetic check deletion.

func (*MockServer) WithSyntheticChecksGet

func (m *MockServer) WithSyntheticChecksGet(fixture string) *MockServer

WithSyntheticChecksGet sets up the mock server to return a synthetic check by ID.

func (*MockServer) WithSyntheticChecksList

func (m *MockServer) WithSyntheticChecksList(fixture string) *MockServer

WithSyntheticChecksList sets up the mock server to return a list of synthetic checks.

func (*MockServer) WithSyntheticChecksUpdate added in v1.7.1

func (m *MockServer) WithSyntheticChecksUpdate(fixture string) *MockServer

WithSyntheticChecksUpdate sets up the mock server to accept synthetic check updates.

func (*MockServer) WithUnauthorized

func (m *MockServer) WithUnauthorized(fixture string) *MockServer

WithUnauthorized sets up the mock server to return 401 Unauthorized.

func (*MockServer) WithViewsCreate

func (m *MockServer) WithViewsCreate(fixture string) *MockServer

WithViewsCreate sets up the mock server to accept view creation.

func (*MockServer) WithViewsDelete

func (m *MockServer) WithViewsDelete() *MockServer

WithViewsDelete sets up the mock server to accept view deletion.

func (*MockServer) WithViewsGet

func (m *MockServer) WithViewsGet(fixture string) *MockServer

WithViewsGet sets up the mock server to return a view by ID.

func (*MockServer) WithViewsList

func (m *MockServer) WithViewsList(fixture string) *MockServer

WithViewsList sets up the mock server to return a list of views.

func (*MockServer) WithViewsUpdate added in v1.7.1

func (m *MockServer) WithViewsUpdate(fixture string) *MockServer

WithViewsUpdate sets up the mock server to accept view updates.

type RecordedRequest

type RecordedRequest struct {
	Method string
	Path   string
	Query  string
	Body   []byte
	Header http.Header
}

RecordedRequest stores information about a received request.

Jump to

Keyboard shortcuts

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