testutils

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureStdout

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

CaptureStdout captures the standard output generated during the execution of f. It forces the slog logger to LevelDebug for the duration of the capture.

func CaptureStdoutWithLevel

func CaptureStdoutWithLevel(t *testing.T, level slog.Level, f func()) string

CaptureStdoutWithLevel captures the standard output generated during the execution of f, and sets the slog logger to the specified level for the duration of the capture.

func CreateTempYAMLFile

func CreateTempYAMLFile(t *testing.T, dir string, name string, content string) string

CreateTempYAMLFile creates a temporary .mt.yaml file with the given content. It automatically cleans up the file when the test finishes.

func NewMockDockerClient

func NewMockDockerClient(t *testing.T, roundTripFunc func(req *http.Request) (*http.Response, error)) *client.Client

func SetupMockExecutable

func SetupMockExecutable(t *testing.T, cmdName string, scriptContent string) (string, func())

SetupMockExecutable creates a mock executable shell script in a temporary bin directory, prepends that directory to the PATH environment variable, and returns the path to a log file where invocation arguments are recorded, along with a cleanup function.

cmdName is the name of the command to mock (e.g. "npx" or "docker"). scriptContent is the shell script body. If empty, a default script is used that logs arguments and exits with 0 (or 1 if MTRACE_TEST_FAIL env var is "true").

func StartMockJaegerServer

func StartMockJaegerServer(t *testing.T, handler func(traceID string) (any, int)) *httptest.Server

StartMockJaegerServer starts a mock HTTP server that simulates Jaeger trace search endpoint.

func StartMockNATSServer

func StartMockNATSServer(t *testing.T, enableJetstream bool) string

StartMockNATSServer starts an in-memory NATS server on a random port. If enableJetstream is true, JetStream is initialized using a temp directory. Returns the connection address (without the "nats://" scheme).

func StartMockNATSServerWithOptions

func StartMockNATSServerWithOptions(t *testing.T, opts *natsserver.Options) string

StartMockNATSServerWithOptions starts an in-memory NATS server with the given Options. Returns the connection address (without the "nats://" scheme).

func StartMockOpenObserveServer

func StartMockOpenObserveServer(t *testing.T, handler func(sqlQuery string) ([]map[string]any, int)) *httptest.Server

StartMockOpenObserveServer starts a mock HTTP server that simulates OpenObserve trace search endpoint. The handler receives the SQL query string and returns the list of hits and HTTP status code.

Types

type GRPCRequestRecord

type GRPCRequestRecord struct {
	Method   string
	Metadata metadata.MD
	Request  *dynamicpb.Message
}

GRPCRequestRecord records details of a gRPC request received by the mock server.

type GRPCTargetServer

type GRPCTargetServer struct {
	Address  string
	Server   *grpc.Server
	Requests []GRPCRequestRecord
}

GRPCTargetServer is a mock gRPC server.

func StartGRPCTargetServer

func StartGRPCTargetServer(t *testing.T, baseDir string, protoPath string, serviceName string, responseData map[string]any) *GRPCTargetServer

StartGRPCTargetServer starts a mock gRPC server on a local port. It uses the proto files to decode incoming requests dynamically.

type HTTPRequestRecord

type HTTPRequestRecord struct {
	Method  string
	URL     string
	Headers http.Header
	Body    string
}

HTTPRequestRecord represents a recorded request received by the mock target server.

type HTTPTargetServer

type HTTPTargetServer struct {
	Server   *httptest.Server
	Requests []HTTPRequestRecord
}

HTTPTargetServer represents a mock HTTP target server.

func StartHTTPTargetServer

func StartHTTPTargetServer(t *testing.T, responseBody string, statusCode int) *HTTPTargetServer

StartHTTPTargetServer starts a mock HTTP target server that records requests.

type MockIdGenerator

type MockIdGenerator struct {
	TraceID string
	SpanID  string
	Err     error
}

func (*MockIdGenerator) Generate

func (m *MockIdGenerator) Generate(length int) (string, error)

type MockRoundTripper

type MockRoundTripper struct {
	RoundTripFunc func(req *http.Request) (*http.Response, error)
}

func (*MockRoundTripper) RoundTrip

func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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