testutils

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddServices added in v0.1.3

func AddServices(t testing.TB, srv *testutil.TestServer, svcs []testutil.TestService)

Bulk add test data for seeding consul

func CheckDir added in v0.2.0

func CheckDir(t testing.TB, exists bool, dir string) []os.FileInfo

CheckDir checks whether a directory exists. If it exists, returns the file infos for further checking.

func CheckFile added in v0.2.0

func CheckFile(t testing.TB, exists bool, path, filename string) string

CheckFile checks whether a file exists or not. If it exists, returns the contents for further checking. If path parameter already includes filename, leave filename parameter as an empty string.

func CheckStateFile added in v0.1.3

func CheckStateFile(t *testing.T, consulAddr, taskname string)

CheckStateFile checks statefile in the default Terraform backend ConsulKV.

func ConsulServiceRegistered added in v0.6.0

func ConsulServiceRegistered(tb testing.TB, srv *testutil.TestServer, serviceID string) bool

ConsulServiceRegistered returns whether the Consul service with the given ID is registered or not.

func CopyFile added in v0.4.2

func CopyFile(t testing.TB, src, dst string)

CopyFile copies a file from src to dst.

func CopyFiles added in v0.4.2

func CopyFiles(t testing.TB, srcFiles []string, dst string)

CopyFiles copies a list of files to a destination (dst) directory

func DeleteKV added in v0.4.0

func DeleteKV(tb testing.TB, srv *testutil.TestServer, key string)

func DeregisterConsulService

func DeregisterConsulService(tb testing.TB, srv *testutil.TestServer, id string)

func FindFileMatches added in v0.4.2

func FindFileMatches(t testing.TB, rootDir, pattern string) []string

FindFileMatches walks a root directory and returns a list of all files that match a particular pattern string. E.g. If you want to find all files that end with .txt, pattern=*.txt

func FreePort added in v0.1.3

func FreePort(t testing.TB) int

FreePort finds the next free port incrementing upwards. Use for testing.

func ListConsulChecks added in v0.6.0

func ListConsulChecks(tb testing.TB, srv *testutil.TestServer) map[string]ConsulCheck

ListConsulChecks returns a list of checks from a Consul agent.

func ListConsulServices added in v0.6.0

func ListConsulServices(tb testing.TB, srv *testutil.TestServer, filter string) map[string]ConsulService

ListConsulServices returns a list of services registered with the Consul agent.

func MakeTempDir

func MakeTempDir(t testing.TB, tempDir string) func() error

MakeTempDir creates a directory in the current path for a test. Caller is responsible for managing the uniqueness of the directory name. Returns a function for the caller to delete the temporary directory.

func NewHttpClient added in v0.6.0

func NewHttpClient(t *testing.T, intercepts []*HttpIntercept) *http.Client

NewHttpClient returns an HTTP client that returns mocked responses to specific request paths based on the specified intercepts.

func NewTestConsulServer added in v0.1.1

func NewTestConsulServer(tb testing.TB, config TestConsulServerConfig) *testutil.TestServer

NewTestConsulServer starts a test Consul server as configured

func RegisterConsulService

func RegisterConsulService(tb testing.TB, srv *testutil.TestServer,
	s testutil.TestService, wait time.Duration)

RegisterConsulService regsiters a service to the Consul Catalog. The Consul sdk/testutil package currently does not support a method to register multiple service instances, distinguished by their IDs.

func RegisterConsulServiceHealth added in v0.4.0

func RegisterConsulServiceHealth(tb testing.TB, srv *testutil.TestServer,
	s testutil.TestService, wait time.Duration, health string)

RegisterConsulServiceHealth is similar to RegisterConsulService and also sets the health status of the service.

func RequestHTTP

func RequestHTTP(t testing.TB, method, url, body string) *http.Response

RequestHTTP makes an http request. The caller is responsible for closing the response.

func RequestHTTPS added in v0.6.0

func RequestHTTPS(t testing.TB, method, url, body string, conf TLSConfig) *http.Response

RequestHTTPS makes an https request using TLS. The caller is responsible for closing the response.

func RequestJSON added in v0.5.0

func RequestJSON(t testing.TB, method, url string, body interface{}) *http.Response

RequestJSON encodes the body to JSON and makes an HTTP request. The caller is responsible for closing the response.

func Setenv added in v0.3.0

func Setenv(envvar, value string) func()

Setenv sets an environment variable to a value. Returns a reset function to reset the environment variable back to the original state.

func ShowMeHealth added in v0.1.3

func ShowMeHealth(t testing.TB, srv *testutil.TestServer, svcName string)

Health status for all services

func ShowMeServices added in v0.1.3

func ShowMeServices(t testing.TB, srv *testutil.TestServer)

All registered services

func TestServices added in v0.1.3

func TestServices(n int) []testutil.TestService

Generate service TestService entries. Services with different IDs and Names.

func WaitForConsulServiceDeregistered added in v0.6.0

func WaitForConsulServiceDeregistered(tb testing.TB, srv *testutil.TestServer, serviceID string, wait time.Duration)

WaitForConsulServiceDeregistered polls Consul until the given service is deregistered or the timeout is reached.

func WaitForConsulServiceRegistered added in v0.6.0

func WaitForConsulServiceRegistered(tb testing.TB, srv *testutil.TestServer, serviceID string, wait time.Duration)

WaitForConsulServiceRegistered polls Consul until the given service is registered or the timeout is reached.

func WaitForHttpStatusChange added in v0.7.0

func WaitForHttpStatusChange(ctx context.Context, pollInterval time.Duration, method, url, body string, oldStatus, newStatus int) (*http.Response, error)

WaitForHttpStatusChange performs a blocking http call every pollInterval and returns the response whenever it encounters a StatusCode of newStatus. Any StatusCode other than oldStatus or newStatus will return an error immediately. It is the caller's responsibility to:

  • prevent an infinite loop by eventually cancelling the context
  • close the response

func WriteFile added in v0.2.0

func WriteFile(t testing.TB, path, content string)

WriteFile write a content to a file path.

Types

type ConsulCheck added in v0.6.0

type ConsulCheck struct {
	CheckID     string
	Name        string
	Status      string
	Notes       string
	Output      string
	ServiceID   string
	ServiceName string
	ServiceTags []string
	Type        string
}

ConsulCheck represents a Consul check with a subset of its attributes that are verified by the tests.

func WaitForConsulCheckStatus added in v0.6.0

func WaitForConsulCheckStatus(tb testing.TB, srv *testutil.TestServer, checkID, status string, wait time.Duration) (ConsulCheck, error)

WaitForConsulCheckStatus polls a Consul check until the check has the given status or the timeout is reached.

type ConsulService added in v0.6.0

type ConsulService struct {
	ID      string
	Service string
	Tags    []string
	Address string
	Port    int
}

ConsulService represents a Consul service with a subset of its attributes that are verified by the tests.

func GetConsulService added in v0.6.0

func GetConsulService(tb testing.TB, srv *testutil.TestServer, serviceID string) (ConsulService, error)

GetConsulService returns a service instance with the given ID from the Consul agent.

type HttpIntercept added in v0.6.0

type HttpIntercept struct {
	Path               string
	RequestTest        func(*testing.T, *http.Request)
	ResponseStatusCode int
	ResponseData       []byte
}

HttpIntercept represents a mapping of a path to the response that the HTTP client should return for requests to that path (including query parameters).

type TLSConfig added in v0.6.0

type TLSConfig struct {
	ClientCert     string
	ClientKey      string
	CACert         string
	CAFile         string
	VerifyIncoming bool
}

type TestConsulServerConfig added in v0.1.1

type TestConsulServerConfig struct {
	HTTPSRelPath string
	PortHTTP     int // random port will be generated if unset
	PortHTTPS    int // random port will be generated if unset
}

TestConsulServerConfig configures a test Consul server

type TestingTB

type TestingTB struct {
	sync.Mutex
	// contains filtered or unexported fields
}

TestingTB implements Consul's testutil.TestingTB

func (*TestingTB) Cleanup

func (t *TestingTB) Cleanup(f func())

Cleanup implements Consul's testutil.TestingTB's Cleanup()

func (*TestingTB) DoCleanup

func (t *TestingTB) DoCleanup()

DoCleanup implements Consul's testutil.TestingTB's DoCleanup()

func (*TestingTB) Failed

func (*TestingTB) Failed() bool

Failed implements Consul's testutil.TestingTB's Failed()

func (*TestingTB) Fatalf added in v0.7.0

func (*TestingTB) Fatalf(string, ...interface{})

Fatalf implements Consul's testutil.TestingTB's Fatalf()

func (*TestingTB) Logf

func (*TestingTB) Logf(string, ...interface{})

Logf implements Consul's testutil.TestingTB's Logf()

func (*TestingTB) Name

func (*TestingTB) Name() string

Name implements Consul's testutil.TestingTB's Name()

Jump to

Keyboard shortcuts

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