utils

package
v8.0.0-rc6 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 13 Imported by: 0

Documentation

Overview

Package utils provides helpful utilities for easing the process of testing Traffic Ops API clients. The functions here don't depend on any particular API version, so they need not be copy/pasted along with the entire testing suites.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(t *testing.T, expected []string, alertsStrs []string)

Compare compares a set of expected alert messages to those actually received. It checks for the existence of each expected alert, not that they appear in any particular order. It also checks that no unexpected alert strings exist. Note that this isn't particularly efficient, which is fine because it's only meant to be used in testing.

func CreateV3Session

func CreateV3Session(t *testing.T, trafficOpsURL string, username string, password string, toReqTimeout int) *v3client.Session

CreateV3Session creates a session for client v4 using the passed in username and password.

func CreateV4Session

func CreateV4Session(t *testing.T, trafficOpsURL string, username string, password string, toReqTimeout int) *v4client.Session

CreateV4Session creates a session for client v4 using the passed in username and password.

func CreateV5Session

func CreateV5Session(t *testing.T, trafficOpsURL, username, password string, toReqTimeout int) *v5client.Session

CreateV5Session creates a session for client v5 using the passed in username and password.

func ErrorsToStrings

func ErrorsToStrings(errs []error) []string

ErrorsToStrings converts a slice of errors to a slice of their error messages.

func FindNeedle

func FindNeedle[T comparable](needle T, haystack []T) bool

FindNeedle searches a "haystack" slice of values for the the "needle" value, returning true if the value is in the haystack, false otherwise.

Types

type CkReqFunc

type CkReqFunc func(*testing.T, toclientlib.ReqInf, interface{}, tc.Alerts, error)

CkReqFunc defines the reusable signature for all other functions that perform checks. Common parameters that are checked include the request's info, response, alerts, and errors.

func CkRequest

func CkRequest(c ...CkReqFunc) []CkReqFunc

CkRequest wraps CkReqFunc functions, to be concise and reduce lexical tokens i.e. Instead of `[]CkReqFunc {` we can use `CkRequest(` in test case declarations.

func HasAlertLevel

func HasAlertLevel(expectedAlertLevel string) CkReqFunc

HasAlertLevel checks that the alert from the request matches the expected level.

func HasError

func HasError() CkReqFunc

HasError checks that an error was returned (i.e. not `nil`).

func HasStatus

func HasStatus(expectedStatus int) CkReqFunc

HasStatus checks that the status code from the request is as expected.

func NoError

func NoError() CkReqFunc

NoError checks that no error was returned (i.e. `nil`).

func ResponseHasLength

func ResponseHasLength(expected int) CkReqFunc

ResponseHasLength checks that the length of the response is as expected. Determines that response is a slice before checking the length of the reflected value.

func ResponseLengthGreaterOrEqual

func ResponseLengthGreaterOrEqual(expected int) CkReqFunc

ResponseLengthGreaterOrEqual checks that the response is greater or equal to the expected length. Determines that response is a slice before checking the length of the reflected value.

type TestCase

type TestCase[C clientSession, R requestOpts, B any] map[string]map[string]TestData[C, R, B]

type TestData

type TestData[C clientSession, R requestOpts, B any] struct {
	EndpointID    func() int
	ClientSession *C
	RequestOpts   R
	RequestBody   B
	Expectations  []CkReqFunc
}

TestData represents the data needed for testing the api endpoints.

type V3TestCase

type V3TestCase map[string]map[string]V3TestData

V3TestCase is the type of the V3TestData struct. Uses nested map to represent the method being tested and the test's description.

type V3TestCaseT

type V3TestCaseT[B any] map[string]map[string]V3TestDataT[B]

V3TestCaseT is the type of the V3TestDataT struct. Uses nested map to represent the method being tested and the test's description.

type V3TestData

type V3TestData struct {
	EndpointID     func() int
	ClientSession  *v3client.Session
	RequestParams  url.Values
	RequestHeaders http.Header
	RequestBody    map[string]interface{}
	PreReqFuncs    []func()
	Expectations   []CkReqFunc
}

V3TestData represents the data needed for testing the v3 api endpoints.

type V3TestDataT

type V3TestDataT[B any] struct {
	EndpointID     func() int
	ClientSession  *v3client.Session
	RequestParams  url.Values
	RequestHeaders http.Header
	RequestBody    B
	Expectations   []CkReqFunc
}

V3TestDataT represents the data needed for testing the v3 api endpoints.

type V4TestCase

type V4TestCase map[string]map[string]V4TestData

V4TestCase is the type of the V4TestData struct. Uses nested map to represent the method being tested and the test's description.

type V4TestData

type V4TestData struct {
	EndpointID    func() int
	ClientSession *v4client.Session
	RequestOpts   v4client.RequestOptions
	RequestBody   map[string]interface{}
	PreReqFuncs   []func()
	Expectations  []CkReqFunc
}

V4TestData represents the data needed for testing the v4 api endpoints.

type V5TestCase

type V5TestCase map[string]map[string]V5TestData

V5TestCase is a map of test names to maps of HTTP request method descriptions to V5TestData structures. Uses nested map to represent the method being tested and the test's description.

type V5TestData

type V5TestData struct {
	EndpointID    func() int
	ClientSession *v5client.Session
	RequestOpts   v5client.RequestOptions
	RequestBody   map[string]interface{}
	PreReqFuncs   []func()
	Expectations  []CkReqFunc
}

V5TestData represents the data needed for testing the v5 api endpoints.

Jump to

Keyboard shortcuts

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