mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-23 12:31:07 +01:00
e614367cbf
### Describe Your Changes
Related issue: #7199
This is the initial version of the integration tests for cluster. See
`README.md` for details.
Currently cluster only, but it can also be used for vm-single if needed.
The code has been added to the apptest package that resides in the root
directory of the VM codebase. This is done to exclude the integration
tests from regular testing build targets because:
- Most of the test variants do not apply to integration testing (such as
pure or race).
- The integtation tests may also be slow because each test must wait for
2 seconds so vmstorage flushes pending content). It may be okay when
there are a few tests but when there is a 100 of them running tests will
require much more time which will affect the developer wait time and CI
workflows.
- Finally, the integration tests may be flaky especially short term.
An alternative approach would be placing apptest under app package and
exclude apptest from packages under test, but that is not trivial.
The integration tests rely on retrieving some application runtime info
from the application logs, namely the application's host:port. Therefore
some changes to lib/httpserver/httpserver.go were necessary, such as
reporting the effective host:port instead the one from the flag.
### Checklist
The following checks are **mandatory**:
- [x] My change adheres [VictoriaMetrics contributing
guidelines](https://docs.victoriametrics.com/contributing/).
---------
Signed-off-by: Artem Fetishev <rtm@victoriametrics.com>
(cherry picked from commit d7b3589dbd
)
102 lines
3.0 KiB
Go
102 lines
3.0 KiB
Go
package apptest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
// Vmselect holds the state of a vmselect app and provides vmselect-specific
|
|
// functions.
|
|
type Vmselect struct {
|
|
*app
|
|
*ServesMetrics
|
|
|
|
httpListenAddr string
|
|
clusternativeListenAddr string
|
|
cli *Client
|
|
}
|
|
|
|
// MustStartVmselect is a test helper function that starts an instance of
|
|
// vmselect and fails the test if the app fails to start.
|
|
func MustStartVmselect(t *testing.T, instance string, flags []string, cli *Client) *Vmselect {
|
|
t.Helper()
|
|
|
|
app, err := StartVmselect(instance, flags, cli)
|
|
if err != nil {
|
|
t.Fatalf("Could not start %s: %v", instance, err)
|
|
}
|
|
|
|
return app
|
|
}
|
|
|
|
// 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 StartVmselect(instance string, flags []string, cli *Client) (*Vmselect, error) {
|
|
app, stderrExtracts, err := startApp(instance, "../../bin/vmselect", flags, &appOptions{
|
|
defaultFlags: map[string]string{
|
|
"-httpListenAddr": "127.0.0.1:0",
|
|
"-clusternativeListenAddr": "127.0.0.1:0",
|
|
},
|
|
extractREs: []*regexp.Regexp{
|
|
httpListenAddrRE,
|
|
vmselectAddrRE,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Vmselect{
|
|
app: app,
|
|
ServesMetrics: &ServesMetrics{
|
|
metricsURL: fmt.Sprintf("http://%s/metrics", stderrExtracts[0]),
|
|
cli: cli,
|
|
},
|
|
httpListenAddr: stderrExtracts[0],
|
|
clusternativeListenAddr: stderrExtracts[1],
|
|
cli: cli,
|
|
}, nil
|
|
}
|
|
|
|
// ClusternativeListenAddr returns the address at which the vmselect process is
|
|
// listening for connections from other vmselect apps.
|
|
func (app *Vmselect) ClusternativeListenAddr() string {
|
|
return app.clusternativeListenAddr
|
|
}
|
|
|
|
// PrometheusAPIV1SeriesResponse is an inmemory representation of the
|
|
// /prometheus/api/v1/series response.
|
|
type PrometheusAPIV1SeriesResponse struct {
|
|
Status string
|
|
IsPartial bool
|
|
Data []map[string]string
|
|
}
|
|
|
|
// 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 (app *Vmselect) PrometheusAPIV1Series(t *testing.T, tenant, matchQuery string) *PrometheusAPIV1SeriesResponse {
|
|
t.Helper()
|
|
|
|
seriesURL := fmt.Sprintf("http://%s/select/%s/prometheus/api/v1/series", app.httpListenAddr, tenant)
|
|
values := url.Values{}
|
|
values.Add("match[]", matchQuery)
|
|
jsonRes := app.cli.PostForm(t, seriesURL, values, http.StatusOK)
|
|
var res PrometheusAPIV1SeriesResponse
|
|
if err := json.Unmarshal([]byte(jsonRes), &res); err != nil {
|
|
t.Fatalf("could not unmarshal /api/v1/series response: %v", err)
|
|
}
|
|
return &res
|
|
}
|
|
|
|
// String returns the string representation of the vmselect app state.
|
|
func (app *Vmselect) String() string {
|
|
return fmt.Sprintf("{app: %s httpListenAddr: %q}", app.app, app.httpListenAddr)
|
|
}
|