apptest

package
v0.40.0-victorialogs Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

README

App Integration Tests

The apptest package contains the integration tests for the VictoriaMetrics applications (such as vmstorage, vminsert, and vmselect).

An integration test aims at verifying the behavior of an application as a whole, as apposed to a unit test that verifies the behavior of a building block of an application.

To achieve that an integration test starts an application in a separate process and then issues HTTP requets to it and verifies the responses, examines the metrics the app exposes and/or files it creates, etc.

Note that an object of testing may be not just a single app, but several apps working together. A good example is VictoriaMetrics cluster. An integration test may reproduce an arbitrary cluster configuration and verify how the components work together as a system.

The package provides a collection of helpers to start applications and make queries to them:

  • app.go - contains the generic code for staring an application and should not be used by integration tests directly.
  • {vmstorage,vminsert,etc}.go - build on top of app.go and provide the code for staring a specific application.
  • client.go - provides helper functions for sending HTTP requests to applications.

The integration tests themselves reside in *_test.go files. Apart from having the _test suffix, there are no strict rules of how to name a file, but the name should reflect the prevailing purpose of the tests located in that file. For example, sharding_test.go aims at testing data sharding.

Since integration tests start applications in a separate process, they require the application binary files to be built and put into the bin directory. The build rule used for running integration tests, make integration-test, accounts for that, it builds all application binaries before running the tests. But if you want to run the tests without make, i.e. by executing go test ./app/apptest, you will need to build the binaries first (for example, by executing make all).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is used for interacting with the apps over the network.

At the moment it only supports HTTP protocol but may be exptended to support RPCs, etc.

func NewClient

func NewClient() *Client

NewClient creates a new client.

func (*Client) CloseConnections

func (c *Client) CloseConnections()

CloseConnections closes client connections.

func (*Client) Get

func (c *Client) Get(t *testing.T, url string, wantStatusCode int) string

Get sends a HTTP GET request. Once the function receives a response, it checks whether the response status code matches the expected one and returns the response body to the caller.

func (*Client) Post

func (c *Client) Post(t *testing.T, url, contentType, data string, wantStatusCode int) string

Post sends a HTTP POST request. Once the function receives a response, it checks whether the response status code matches the expected one and returns the response body to the caller.

func (*Client) PostForm

func (c *Client) PostForm(t *testing.T, url string, data url.Values, wantStatusCode int) string

PostForm sends a HTTP POST request containing the POST-form data. Once the function receives a response, it checks whether the response status code matches the expected one and returns the response body to the caller.

type PrometheusAPIV1SeriesResponse

type PrometheusAPIV1SeriesResponse struct {
	Status    string
	IsPartial bool
	Data      []map[string]string
}

PrometheusAPIV1SeriesResponse is an inmemory representation of the /prometheus/api/v1/series response.

type ServesMetrics

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

ServesMetrics is used to retrive the app's metrics.

This type is expected to be embdded by the apps that serve metrics.

func (*ServesMetrics) GetIntMetric

func (app *ServesMetrics) GetIntMetric(t *testing.T, metricName string) int

GetIntMetric retrieves the value of a metric served by an app at /metrics URL. The value is then converted to int.

func (*ServesMetrics) GetMetric

func (app *ServesMetrics) GetMetric(t *testing.T, metricName string) float64

GetMetric retrieves the value of a metric served by an app at /metrics URL.

type TestCase

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

TestCase holds the state and defines clean-up procedure common for all test cases.

func NewTestCase

func NewTestCase(t *testing.T) *TestCase

NewTestCase creates a new test case.

func (*TestCase) Client

func (tc *TestCase) Client() *Client

Client returns an instance of the client that can be used for interacting with the app(s) under test.

func (*TestCase) Close

func (tc *TestCase) Close()

Close performs the test case clean up, such as closing all client connections and removing the -storageDataDir directory.

Note that the -storageDataDir is not removed in case of test case failure to allow for furher manual debugging.

func (*TestCase) Dir

func (tc *TestCase) Dir() string

Dir returns the directory name that should be used by as the -storageDataDir.

type Vminsert

type Vminsert struct {
	*ServesMetrics
	// contains filtered or unexported fields
}

Vminsert holds the state of a vminsert app and provides vminsert-specific functions.

func MustStartVminsert

func MustStartVminsert(t *testing.T, instance string, flags []string, cli *Client) *Vminsert

MustStartVminsert is a test helper function that starts an instance of vminsert and fails the test if the app fails to start.

func StartVminsert

func StartVminsert(instance string, flags []string, cli *Client) (*Vminsert, error)

StartVminsert starts an instance of vminsert with the given flags. It also sets the default flags and populates the app instance state with runtime values extracted from the application log (such as httpListenAddr)

func (*Vminsert) PrometheusAPIV1ImportPrometheus

func (app *Vminsert) PrometheusAPIV1ImportPrometheus(t *testing.T, tenant string, records []string)

PrometheusAPIV1ImportPrometheus is a test helper function that inserts a collection of records in Prometheus text exposition format for the given tenant by sending a HTTP POST request to /prometheus/api/v1/import/prometheus vminsert endpoint.

See https://docs.victoriametrics.com/url-examples/#apiv1importprometheus

func (Vminsert) Stop

func (app Vminsert) Stop()

stop sends the app process a SIGINT signal and waits until it terminates gracefully.

func (*Vminsert) String

func (app *Vminsert) String() string

String returns the string representation of the vminsert app state.

type Vmselect

type Vmselect struct {
	*ServesMetrics
	// contains filtered or unexported fields
}

Vmselect holds the state of a vmselect app and provides vmselect-specific functions.

func MustStartVmselect

func MustStartVmselect(t *testing.T, instance string, flags []string, cli *Client) *Vmselect

MustStartVmselect is a test helper function that starts an instance of vmselect and fails the test if the app fails to start.

func StartVmselect

func StartVmselect(instance string, flags []string, cli *Client) (*Vmselect, error)

StartVmselect starts an instance of vmselect with the given flags. It also sets the default flags and populates the app instance state with runtime values extracted from the application log (such as httpListenAddr)

func (*Vmselect) ClusternativeListenAddr

func (app *Vmselect) ClusternativeListenAddr() string

ClusternativeListenAddr returns the address at which the vmselect process is listening for connections from other vmselect apps.

func (*Vmselect) PrometheusAPIV1Series

func (app *Vmselect) PrometheusAPIV1Series(t *testing.T, tenant, matchQuery string) *PrometheusAPIV1SeriesResponse

PrometheusAPIV1Series sends a query to a /prometheus/api/v1/series endpoint and returns the list of time series that match the query.

See https://docs.victoriametrics.com/url-examples/#apiv1series

func (Vmselect) Stop

func (app Vmselect) Stop()

stop sends the app process a SIGINT signal and waits until it terminates gracefully.

func (*Vmselect) String

func (app *Vmselect) String() string

String returns the string representation of the vmselect app state.

type Vmstorage

type Vmstorage struct {
	*ServesMetrics
	// contains filtered or unexported fields
}

Vmstorage holds the state of a vmstorage app and provides vmstorage-specific functions.

func MustStartVmstorage

func MustStartVmstorage(t *testing.T, instance string, flags []string, cli *Client) *Vmstorage

MustStartVmstorage is a test helper function that starts an instance of vmstorage and fails the test if the app fails to start.

func StartVmstorage

func StartVmstorage(instance string, flags []string, cli *Client) (*Vmstorage, error)

StartVmstorage starts an instance of vmstorage with the given flags. It also sets the default flags and populates the app instance state with runtime values extracted from the application log (such as httpListenAddr)

func (Vmstorage) Stop

func (app Vmstorage) Stop()

stop sends the app process a SIGINT signal and waits until it terminates gracefully.

func (*Vmstorage) String

func (app *Vmstorage) String() string

String returns the string representation of the vmstorage app state.

func (*Vmstorage) VminsertAddr

func (app *Vmstorage) VminsertAddr() string

VminsertAddr returns the address at which the vmstorage process is listening for vminsert connections.

func (*Vmstorage) VmselectAddr

func (app *Vmstorage) VmselectAddr() string

VmselectAddr returns the address at which the vmstorage process is listening for vmselect connections.

Jump to

Keyboard shortcuts

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