test

package
v0.0.0-...-5a776c5 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareImageTypes

func CompareImageTypes() cmp.Option

CompareImageType considers two image type objects equal if and only if the names of their distro/arch/imagetype are. The thinking is that the objects are static, and resolving by these three keys should always give equivalent objects. Whether we actually have object equality, is an implementation detail, so we don't want to rely on that.

func GenerateCIArtifactName

func GenerateCIArtifactName(prefix string) (string, error)

GenerateCIArtifactName generates a new identifier for CI artifacts which is based on environment variables specified by Jenkins note: in case of migration to sth else like Github Actions, change it to whatever variables GH Action provides

func Ignore

func Ignore(what string) cmp.Option

func IgnoreDates

func IgnoreDates() cmp.Option

func IgnoreUuids

func IgnoreUuids() cmp.Option

func SendHTTP

func SendHTTP(api http.Handler, external bool, method, path, body string) *http.Response

func SetUpTemporaryRepository

func SetUpTemporaryRepository() (string, error)

Create a temporary repository

func TearDownTemporaryRepository

func TearDownTemporaryRepository(dir string) error

Remove the temporary repository

func TestNonJsonRoute

func TestNonJsonRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedResponse string)

func TestRoute

func TestRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string)

func TestRouteWithReply

func TestRouteWithReply(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) (replyJSON []byte)

TestRouteWithReply tests the given API endpoint and if the test passes, it returns the raw JSON reply.

func TestTOMLRoute

func TestTOMLRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedTOML string, ignoreFields ...string)

Types

type APICall

type APICall struct {
	// http.Handler to run the call against
	Handler http.Handler

	// HTTP method, e.g. http.MethodPatch
	Method string

	// Request Path
	Path string

	// Request body. If nil, an empty body is sent
	RequestBody RequestBody

	// Request header. If nil, default header is sent
	Header http.Header

	// Request context. If nil, default context is used
	Context context.Context

	// Status that's expected to be received. If set to 0, the status is not checked
	ExpectedStatus int

	// Validator for the response body. If set to nil, the body is not validated
	ExpectedBody BodyValidator
}

APICall is a small function object for testing HTTP APIs

func (APICall) Do

func (a APICall) Do(t *testing.T) APICallResult

Do performs the request as defined in the APICall struct.

If any errors occur when doing the request, or any of the validators fail, t.FailNow() is called Note that HTTP error status is not checked if ExpectedStatus == 0

The result of the HTTP call is returned

type APICallResult

type APICallResult struct {
	// Full body as read from the server
	Body []byte

	// Status code returned from the server
	StatusCode int
}

APICallResult holds a parsed response for an APICall

type BodyValidator

type BodyValidator interface {
	// Validate returns nil if the body is valid. If the body isn't valid, a descriptive error is returned.
	Validate(body []byte) error
}

BodyValidator is an abstract interface for defining validators for response bodies

type JSONRequestBody

type JSONRequestBody string

JSONRequestBody is just a simple wrapper over plain string.

Body is just the string converted to a slice of bytes and content type is set to application/json

func (JSONRequestBody) Body

func (b JSONRequestBody) Body() []byte

func (JSONRequestBody) ContentType

func (b JSONRequestBody) ContentType() string

type JSONValidator

type JSONValidator struct {
	// Content is the expected json content of the body
	//
	// Note that the key order of maps is arbitrary
	Content string

	// IgnoreFields is a list of JSON keys that should be removed from both expected body and actual body
	IgnoreFields []string
}

JSONValidator is a simple validator for validating JSON responses

func (JSONValidator) Validate

func (b JSONValidator) Validate(body []byte) error

type RequestBody

type RequestBody interface {
	// Body returns the intended request body as a slice of bytes
	Body() []byte

	// ContentType returns value for Content-Type request header
	ContentType() string
}

RequestBody is an abstract interface for defining request bodies for APICall

Jump to

Keyboard shortcuts

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