framework

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package framework contains the low-level implementation of test harness infrastructure that can be reused for different kinds of tests.

The general model is:

1. The test harness communicates with a test service, which exposes a root endpoint for querying its status (GET) or creating some kind of entity within the test service (POST).

2. The test harness can expose any number of mock endpoints to receive requests from the test service.

3. There is a general notion of a test context which is similar to Go's *testing.T, allowing pieces of test logic to be associated with a test identifier and to accumulate success/failure results.

The domain-specific code that knows what is being tested is responsible for providing the parameters to send to the test service, the HTTP handlers for handling requests to mock endpoints, and a domain-specific test API on top of the test context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintFilterDescription

func PrintFilterDescription(harness *TestHarness, filters RegexFilters, allCapabilities []string)

func PrintResults

func PrintResults(results Results)

Types

type CapturedMessage

type CapturedMessage struct {
	Time    time.Time
	Message string
}

type CapturedOutput

type CapturedOutput []CapturedMessage

func (CapturedOutput) ToString

func (output CapturedOutput) ToString(prefix string) string

type CapturingLogger

type CapturingLogger struct {
	// contains filtered or unexported fields
}

func (*CapturingLogger) Output

func (l *CapturingLogger) Output() CapturedOutput

func (*CapturingLogger) Printf

func (l *CapturingLogger) Printf(message string, args ...interface{})

type ConsoleTestLogger

type ConsoleTestLogger struct {
	DebugOutputOnFailure bool
	DebugOutputOnSuccess bool
}

func (ConsoleTestLogger) TestError

func (c ConsoleTestLogger) TestError(id TestID, err error)

func (ConsoleTestLogger) TestFinished

func (c ConsoleTestLogger) TestFinished(id TestID, failed bool, debugOutput CapturedOutput)

func (ConsoleTestLogger) TestSkipped

func (c ConsoleTestLogger) TestSkipped(id TestID, reason string)

func (ConsoleTestLogger) TestStarted

func (c ConsoleTestLogger) TestStarted(id TestID)

type Context

type Context struct {
	// contains filtered or unexported fields
}

func (*Context) Debug

func (c *Context) Debug(message string, args ...interface{})

func (*Context) DebugLogger

func (c *Context) DebugLogger() Logger

func (*Context) Errorf

func (c *Context) Errorf(format string, args ...interface{})

func (*Context) FailNow

func (c *Context) FailNow()

func (*Context) ID

func (c *Context) ID() TestID

func (*Context) Run

func (c *Context) Run(name string, action func(*Context))

func (*Context) Skip

func (c *Context) Skip()

func (*Context) SkipWithReason

func (c *Context) SkipWithReason(reason string)

type Filter

type Filter func(TestID) bool

Filter is a function that can determine whether to run a specific test or not.

type IncomingRequestInfo

type IncomingRequestInfo struct {
	Headers http.Header
	Method  string
	Body    []byte
	Context context.Context
}

IncomingRequestInfo contains information about an HTTP request sent by the test service to one of the mock endpoints.

type Logger

type Logger interface {
	Printf(message string, args ...interface{})
}

func LoggerWithPrefix

func LoggerWithPrefix(baseLogger Logger, prefix string) Logger

func NullLogger

func NullLogger() Logger

type MessageSortingQueue added in v0.0.5

type MessageSortingQueue struct {
	C chan []byte
	// contains filtered or unexported fields
}

func NewMessageSortingQueue added in v0.0.5

func NewMessageSortingQueue(channelSize int) *MessageSortingQueue

func (*MessageSortingQueue) Accept added in v0.0.5

func (q *MessageSortingQueue) Accept(counter int, message []byte)

func (*MessageSortingQueue) Close added in v0.0.5

func (q *MessageSortingQueue) Close()

func (*MessageSortingQueue) Deferred added in v0.0.5

func (q *MessageSortingQueue) Deferred() [][]byte

type MockEndpoint

type MockEndpoint struct {
	// contains filtered or unexported fields
}

MockEndpoint represents an endpoint that can receive requests.

func (*MockEndpoint) ActiveConnection added in v0.0.5

func (e *MockEndpoint) ActiveConnection() *IncomingRequestInfo

func (*MockEndpoint) AwaitConnection

func (e *MockEndpoint) AwaitConnection(timeout time.Duration) (IncomingRequestInfo, error)

AwaitConnection waits for an incoming request to the endpoint.

func (*MockEndpoint) BaseURL

func (e *MockEndpoint) BaseURL() string

BaseURL returns the base path of the mock endpoint.

func (*MockEndpoint) Close

func (e *MockEndpoint) Close()

Close unregisters the endpoint. Any subsequent requests to it will receive 404 errors. It also cancels the Context for every active request to that endpoint.

type RegexFilters

type RegexFilters struct {
	MustMatch    RegexList
	MustNotMatch RegexList
}

func (RegexFilters) AsFilter

func (r RegexFilters) AsFilter(id TestID) bool

type RegexList

type RegexList []*regexp.Regexp

func (RegexList) AnyMatch

func (r RegexList) AnyMatch(s string) bool

func (RegexList) IsDefined

func (r RegexList) IsDefined() bool

func (*RegexList) Set

func (r *RegexList) Set(value string) error

Set is called by the command line parser

func (RegexList) String

func (r RegexList) String() string

type Results

type Results struct {
	Tests    []TestResult
	Failures []TestResult
}

func Run

func Run(
	filter func(TestID) bool,
	testLogger TestLogger,
	action func(*Context),
) Results

func (Results) OK

func (r Results) OK() bool

type TestFailure

type TestFailure struct {
	ID  TestID
	Err error
}

func (TestFailure) Error

func (f TestFailure) Error() string

type TestHarness

type TestHarness struct {
	// contains filtered or unexported fields
}

func NewTestHarness

func NewTestHarness(
	testServiceBaseURL string,
	testHarnessExternalHostname string,
	testHarnessPort int,
	statusQueryTimeout time.Duration,
	debugLogger Logger,
	startupOutput io.Writer,
) (*TestHarness, error)

NewTestHarness creates a TestHarness instance, and verifies that the test service is responding by querying its status resource. It also starts an HTTP listener on the specified port to receive callback requests.

func (*TestHarness) NewMockEndpoint

func (h *TestHarness) NewMockEndpoint(
	handler http.Handler,
	contextFn func(context.Context) context.Context,
	logger Logger,
) *MockEndpoint

NewMockEndpoint adds a new endpoint that can receive requests.

The specified handler will be called for all incoming requests to the endpoint's base URL or any subpath of it. For instance, if the generated base URL (as reported by MockEndpoint.BaseURL()) is http://localhost:8111/endpoints/3, then it can also receive requests to http://localhost:8111/endpoints/3/some/subpath.

When the handler is called, the test harness rewrites the request URL first so that the handler sees only the subpath. It also attaches a Context to the request whose Done channel will be closed if Close is called on the endpoint.

func (*TestHarness) NewTestServiceEntity

func (h *TestHarness) NewTestServiceEntity(
	entityParams interface{},
	description string,
	logger Logger,
) (*TestServiceEntity, error)

NewTestServiceEntity tells the test service to create a new instance of whatever kind of entity it manages, based on the parameters we provide. The test harness can interact with it via the returned TestServiceEntity. The entity is assumed to remain active inside the test service until we explicitly close it.

The format of entityParams is defined by the test harness; this low-level method simply calls json.Marshal to convert whatever it is to JSON.

func (*TestHarness) StopService added in v0.0.3

func (h *TestHarness) StopService() error

StopService tells the test service that it should exit.

func (*TestHarness) TestServiceHasCapability

func (h *TestHarness) TestServiceHasCapability(desired string) bool

func (*TestHarness) TestServiceInfo

func (h *TestHarness) TestServiceInfo() TestServiceInfo

type TestID

type TestID struct {
	Path []string
}

func (TestID) String

func (t TestID) String() string

type TestLogger

type TestLogger interface {
	TestStarted(id TestID)
	TestError(id TestID, err error)
	TestFinished(id TestID, failed bool, debugOutput CapturedOutput)
	TestSkipped(id TestID, reason string)
}

type TestResult

type TestResult struct {
	TestID  TestID
	Errors  []error
	Skipped bool
}

type TestServiceEntity

type TestServiceEntity struct {
	// contains filtered or unexported fields
}

TestServiceEntity represents some kind of entity that we have asked the test service to create, which the test harness will interact with.

func (*TestServiceEntity) Close

func (e *TestServiceEntity) Close() error

Close tells the test service to dispose of this entity.

func (*TestServiceEntity) SendCommand

func (e *TestServiceEntity) SendCommand(command string, additionalParams ...map[string]interface{}) error

SendCommand sends a command to the test service entity.

type TestServiceInfo

type TestServiceInfo struct {
	Description  string   `json:"description"`
	Capabilities []string `json:"capabilities"`
}

TestServiceInfo is status information returned by the test service from the initial status query.

Jump to

Keyboard shortcuts

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