testutils

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2020 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CharSetAlphaNum is the alphanumeric character set for use with
	// RandStringFromCharSet
	CharSetAlphaNum = "abcdefghijklmnopqrstuvwxyz012346789"

	// CharSetAlpha is the alphabetical character set for use with
	// RandStringFromCharSet
	CharSetAlpha = "abcdefghijklmnopqrstuvwxyz"
)
View Source
const (
	StateCancelled = "cancelled"
	StateHalted    = "halted"
)
View Source
const TestEnvVar = "TRITON_TEST"

Variables

View Source
var DefaultMockClient = NewMockClient(MockClientInput{})

DefaultMockClient uses NewMockClient to construct a mocked out client.Client

View Source
var DefaultMockTransport = &MockTransport{
	responders: make(map[string]Responder),
}

DefaultMockTransport allows users to easily and globally alter the default RoundTripper for all http requests.

View Source
var NoResponderFound = errors.New("no responder found")

NoResponderFound is returned when no responders are found for a given HTTP method and URL.

Functions

func AccTest

func AccTest(t *testing.T, c TestCase)

func ActivateClient

func ActivateClient(failNoResponder bool)

Activate replaces the `Transport` on the `http.Client` with our `DefaultMockTransport`.

func DeactivateClient

func DeactivateClient()

Deactivate replaces our `DefaultMockTransport` with the `http.DefaultTransport`.

func NewMockClient

func NewMockClient(input MockClientInput) *client.Client

NewMockClient returns a new client.Client that includes our DefaultMockTransport which allows us to attach custom HTTP client request responders.

func RandPrefixString

func RandPrefixString(prefix string, strlen int) string

RandPrefixString generates a random alphanumeric string of the length specified with the given prefix

func RandString

func RandString(strlen int) string

RandString generates a random alphanumeric string of the length specified

func RandStringFromCharSet

func RandStringFromCharSet(strlen int, charSet string) string

RandStringFromCharSet generates a random string by selecting characters from the charset provided

func RegisterResponder

func RegisterResponder(method, url string, responder Responder)

RegisterResponder adds a responder to the `DefaultMockTransport` responder table.

Types

type AssertFunc

type AssertFunc func(TritonStateBag) error

type MockClientInput

type MockClientInput struct {
	AccountName string
}

type MockTransport

type MockTransport struct {
	FailNoResponder bool
	// contains filtered or unexported fields
}

MockTransport implements http.RoundTripper, which fulfills single http requests issued by an http.Client. This implementation doesn't actually make the call, instead defering to the registered list of responders.

func (*MockTransport) Clear

func (m *MockTransport) Clear()

Clear clears out all the mock responders that have been set on a particular MockTransport object. This comes in especially handy when utilizing the global DefaultMockTRansport and is utilized by the DeactivateClient func.

func (*MockTransport) RegisterResponder

func (m *MockTransport) RegisterResponder(method, url string, responder Responder)

RegisterResponder adds a new responder, associated with a given HTTP method and URL. When a request comes in that matches, the responder will be called and the response returned to the client.

func (*MockTransport) RoundTrip

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

RoundTrip is required to implement http.MockTransport. Instead of fulfilling the given request, the internal list of responders is consulted to handle the request. If no responder is found an error is returned, which is the equivalent of a network error.

type Responder

type Responder func(*http.Request) (*http.Response, error)

Responders are callbacks that receive http requests and return a mocked response.

type Step

type Step interface {
	Run(TritonStateBag) StepAction

	Cleanup(TritonStateBag)
}

type StepAPICall

type StepAPICall struct {
	StateBagKey string
	ErrorKey    string
	CallFunc    func(client interface{}) (interface{}, error)
	CleanupFunc func(client interface{}, callState interface{})
}

func (*StepAPICall) Cleanup

func (s *StepAPICall) Cleanup(state TritonStateBag)

func (*StepAPICall) Run

func (s *StepAPICall) Run(state TritonStateBag) StepAction

type StepAction

type StepAction uint
const (
	Continue StepAction = iota
	Halt
)

type StepAssert

type StepAssert struct {
	StateBagKey string
	Assertions  seq.Map
}

func (*StepAssert) Cleanup

func (s *StepAssert) Cleanup(state TritonStateBag)

func (*StepAssert) Run

func (s *StepAssert) Run(state TritonStateBag) StepAction

type StepAssertFunc

type StepAssertFunc struct {
	AssertFunc AssertFunc
}

func (*StepAssertFunc) Cleanup

func (s *StepAssertFunc) Cleanup(state TritonStateBag)

func (*StepAssertFunc) Run

type StepAssertSet

type StepAssertSet struct {
	StateBagKey string
	Keys        []string
}

func (*StepAssertSet) Cleanup

func (s *StepAssertSet) Cleanup(state TritonStateBag)

func (*StepAssertSet) Run

func (s *StepAssertSet) Run(state TritonStateBag) StepAction

type StepAssertTritonError

type StepAssertTritonError struct {
	ErrorKey string
	Code     string
}

func (*StepAssertTritonError) Cleanup

func (s *StepAssertTritonError) Cleanup(state TritonStateBag)

func (*StepAssertTritonError) Run

type StepClient

type StepClient struct {
	StateBagKey string
	ErrorKey    string
	CallFunc    func(config *triton.ClientConfig) (interface{}, error)
	CleanupFunc func(client interface{}, callState interface{})
}

func (*StepClient) Cleanup

func (s *StepClient) Cleanup(state TritonStateBag)

func (*StepClient) Run

func (s *StepClient) Run(state TritonStateBag) StepAction

type TestCase

type TestCase struct {
	Steps []Step
	State TritonStateBag
}

type TritonStateBag

type TritonStateBag interface {
	Get(string) interface{}
	GetOk(string) (interface{}, bool)
	Put(string, interface{})
	Remove(string)

	Config() *triton.ClientConfig

	Client() interface{}
	PutClient(interface{})

	AppendError(error)
	ErrorsOrNil() []error
}

Jump to

Keyboard shortcuts

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