2023-08-11 13:12:13 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
package topoutil
|
2023-07-18 23:41:30 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-06 09:29:54 +00:00
|
|
|
"encoding/json"
|
2023-07-18 23:41:30 +00:00
|
|
|
"fmt"
|
2024-01-12 23:35:44 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-07-18 23:41:30 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2023-11-02 19:25:48 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-12-06 17:11:32 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2023-07-18 23:41:30 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
|
|
libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert"
|
|
|
|
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
|
2023-11-02 19:25:48 +00:00
|
|
|
"github.com/hashicorp/consul/testing/deployer/topology"
|
2024-03-06 09:29:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-07-18 23:41:30 +00:00
|
|
|
)
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
// Asserter is a utility to help in reducing boilerplate in invoking test
|
2023-07-18 23:41:30 +00:00
|
|
|
// assertions against consul-topology Sprawl components.
|
|
|
|
//
|
|
|
|
// The methods should largely take in *topology.Service instances in lieu of
|
|
|
|
// ip/ports if there is only one port that makes sense for the assertion (such
|
|
|
|
// as use of the envoy admin port 19000).
|
|
|
|
//
|
2023-11-10 19:22:06 +00:00
|
|
|
// If it's up to the test (like picking a destination) leave port as an argument
|
2023-07-18 23:41:30 +00:00
|
|
|
// but still take the service and use that to grab the local ip from the
|
|
|
|
// topology.Node.
|
2023-11-02 19:25:48 +00:00
|
|
|
type Asserter struct {
|
|
|
|
sp SprawlLite
|
2023-07-18 23:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// *sprawl.Sprawl satisfies this. We don't need anything else.
|
2023-11-02 19:25:48 +00:00
|
|
|
type SprawlLite interface {
|
2023-07-18 23:41:30 +00:00
|
|
|
HTTPClientForCluster(clusterName string) (*http.Client, error)
|
|
|
|
APIClientForNode(clusterName string, nid topology.NodeID, token string) (*api.Client, error)
|
2023-09-06 23:46:34 +00:00
|
|
|
APIClientForCluster(clusterName string, token string) (*api.Client, error)
|
2023-11-02 19:25:48 +00:00
|
|
|
ResourceServiceClientForCluster(clusterName string) pbresource.ResourceServiceClient
|
2023-07-18 23:41:30 +00:00
|
|
|
Topology() *topology.Topology
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
// NewAsserter creates a new assertion helper for the provided sprawl.
|
|
|
|
func NewAsserter(sp SprawlLite) *Asserter {
|
|
|
|
return &Asserter{
|
2023-07-18 23:41:30 +00:00
|
|
|
sp: sp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
func (a *Asserter) mustGetHTTPClient(t testutil.TestingTB, cluster string) *http.Client {
|
2023-07-18 23:41:30 +00:00
|
|
|
client, err := a.httpClientFor(cluster)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
func (a *Asserter) mustGetAPIClient(t testutil.TestingTB, cluster string) *api.Client {
|
2023-09-06 23:46:34 +00:00
|
|
|
clu := a.sp.Topology().Clusters[cluster]
|
|
|
|
cl, err := a.sp.APIClientForCluster(clu.Name, "")
|
2023-07-18 23:41:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpClientFor returns a pre-configured http.Client that proxies requests
|
|
|
|
// through the embedded squid instance in each LAN.
|
|
|
|
//
|
|
|
|
// Use this in methods below to magically pick the right proxied http client
|
|
|
|
// given the home of each node being checked.
|
2023-11-02 19:25:48 +00:00
|
|
|
func (a *Asserter) httpClientFor(cluster string) (*http.Client, error) {
|
2023-07-18 23:41:30 +00:00
|
|
|
client, err := a.sp.HTTPClientForCluster(cluster)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
// DestinationEndpointStatus validates that proxy was configured with provided clusterName in the healthStatus
|
2023-07-18 23:41:30 +00:00
|
|
|
//
|
|
|
|
// Exposes libassert.UpstreamEndpointStatus for use against a Sprawl.
|
|
|
|
//
|
|
|
|
// NOTE: this doesn't take a port b/c you always want to use the envoy admin port.
|
2023-11-10 19:22:06 +00:00
|
|
|
func (a *Asserter) DestinationEndpointStatus(
|
2023-07-18 23:41:30 +00:00
|
|
|
t *testing.T,
|
2023-11-10 19:22:06 +00:00
|
|
|
workload *topology.Workload,
|
2023-07-18 23:41:30 +00:00
|
|
|
clusterName string,
|
|
|
|
healthStatus string,
|
|
|
|
count int,
|
|
|
|
) {
|
|
|
|
t.Helper()
|
2023-11-10 19:22:06 +00:00
|
|
|
node := workload.Node
|
2023-07-18 23:41:30 +00:00
|
|
|
ip := node.LocalAddress()
|
2023-11-10 19:22:06 +00:00
|
|
|
port := workload.EnvoyAdminPort
|
2023-07-18 23:41:30 +00:00
|
|
|
addr := fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
|
|
|
|
client := a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
libassert.AssertUpstreamEndpointStatusWithClient(t, client, addr, clusterName, healthStatus, count)
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:55:50 +00:00
|
|
|
func (a *Asserter) getEnvoyClient(t *testing.T, workload *topology.Workload) (client *http.Client, addr string) {
|
|
|
|
node := workload.Node
|
|
|
|
ip := node.LocalAddress()
|
|
|
|
port := workload.EnvoyAdminPort
|
|
|
|
addr = fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
client = a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
return client, addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssertEnvoyRunningWithClient asserts that envoy is running by querying its stats page
|
|
|
|
func (a *Asserter) AssertEnvoyRunningWithClient(t *testing.T, workload *topology.Workload) {
|
|
|
|
t.Helper()
|
|
|
|
client, addr := a.getEnvoyClient(t, workload)
|
|
|
|
libassert.AssertEnvoyRunningWithClient(t, client, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssertEnvoyPresentsCertURIWithClient makes GET request to /certs endpoint and validates that
|
|
|
|
// two certificates URI is available in the response
|
|
|
|
func (a *Asserter) AssertEnvoyPresentsCertURIWithClient(t *testing.T, workload *topology.Workload) {
|
|
|
|
t.Helper()
|
|
|
|
client, addr := a.getEnvoyClient(t, workload)
|
|
|
|
libassert.AssertEnvoyPresentsCertURIWithClient(t, client, addr, workload.ID.Name)
|
|
|
|
}
|
|
|
|
|
2024-02-07 20:21:44 +00:00
|
|
|
func (a *Asserter) AssertEnvoyHTTPrbacFiltersContainIntentions(t *testing.T, workload *topology.Workload) {
|
|
|
|
t.Helper()
|
|
|
|
client, addr := a.getEnvoyClient(t, workload)
|
|
|
|
var (
|
|
|
|
dump string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
failer := func() *retry.Timer {
|
|
|
|
return &retry.Timer{Timeout: 30 * time.Second, Wait: 1 * time.Second}
|
|
|
|
}
|
|
|
|
|
|
|
|
retry.RunWith(failer(), t, func(r *retry.R) {
|
|
|
|
dump, _, err = libassert.GetEnvoyOutputWithClient(client, addr, "config_dump", map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
r.Fatal("could not fetch envoy configuration")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// the steps below validate that the json result from envoy config dump configured active listeners with rbac and http filters
|
|
|
|
filter := `.configs[2].dynamic_listeners[].active_state.listener | "\(.name) \( .filter_chains[].filters[] | select(.name == "envoy.filters.network.http_connection_manager") | .typed_config.http_filters | map(.name) | join(","))"`
|
|
|
|
errorStateFilter := `.configs[2].dynamic_listeners[].error_state"`
|
|
|
|
configErrorStates, _ := utils.JQFilter(dump, errorStateFilter)
|
|
|
|
require.Nil(t, configErrorStates, "there should not be any error states on listener configuration")
|
|
|
|
results, err := utils.JQFilter(dump, filter)
|
|
|
|
require.NoError(t, err, "could not parse envoy configuration")
|
|
|
|
var filteredResult []string
|
|
|
|
for _, result := range results {
|
|
|
|
parts := strings.Split(strings.ReplaceAll(result, `,`, " "), " ")
|
|
|
|
sanitizedResult := append(parts[:0], parts[1:]...)
|
|
|
|
filteredResult = append(filteredResult, sanitizedResult...)
|
|
|
|
}
|
|
|
|
require.Contains(t, filteredResult, "envoy.filters.http.rbac")
|
|
|
|
require.Contains(t, filteredResult, "envoy.filters.http.router")
|
|
|
|
require.Contains(t, dump, "intentions")
|
|
|
|
}
|
|
|
|
|
2023-07-18 23:41:30 +00:00
|
|
|
// HTTPServiceEchoes verifies that a post to the given ip/port combination
|
|
|
|
// returns the data in the response body. Optional path can be provided to
|
|
|
|
// differentiate requests.
|
|
|
|
//
|
|
|
|
// Exposes libassert.HTTPServiceEchoes for use against a Sprawl.
|
|
|
|
//
|
2023-11-10 19:22:06 +00:00
|
|
|
// NOTE: this takes a port b/c you may want to reach this via your choice of destination.
|
2023-11-02 19:25:48 +00:00
|
|
|
func (a *Asserter) HTTPServiceEchoes(
|
2023-07-18 23:41:30 +00:00
|
|
|
t *testing.T,
|
2023-11-10 19:22:06 +00:00
|
|
|
workload *topology.Workload,
|
2023-07-18 23:41:30 +00:00
|
|
|
port int,
|
|
|
|
path string,
|
|
|
|
) {
|
|
|
|
t.Helper()
|
|
|
|
require.True(t, port > 0)
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
node := workload.Node
|
2023-07-18 23:41:30 +00:00
|
|
|
ip := node.LocalAddress()
|
|
|
|
addr := fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
|
|
|
|
client := a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
libassert.HTTPServiceEchoesWithClient(t, client, addr, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPServiceEchoesResHeader verifies that a post to the given ip/port combination
|
|
|
|
// returns the data in the response body with expected response headers.
|
|
|
|
// Optional path can be provided to differentiate requests.
|
|
|
|
//
|
|
|
|
// Exposes libassert.HTTPServiceEchoes for use against a Sprawl.
|
|
|
|
//
|
2023-11-10 19:22:06 +00:00
|
|
|
// NOTE: this takes a port b/c you may want to reach this via your choice of destination.
|
2023-11-02 19:25:48 +00:00
|
|
|
func (a *Asserter) HTTPServiceEchoesResHeader(
|
2023-07-18 23:41:30 +00:00
|
|
|
t *testing.T,
|
2023-11-10 19:22:06 +00:00
|
|
|
workload *topology.Workload,
|
2023-07-18 23:41:30 +00:00
|
|
|
port int,
|
|
|
|
path string,
|
|
|
|
expectedResHeader map[string]string,
|
|
|
|
) {
|
|
|
|
t.Helper()
|
|
|
|
require.True(t, port > 0)
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
node := workload.Node
|
2023-07-18 23:41:30 +00:00
|
|
|
ip := node.LocalAddress()
|
|
|
|
addr := fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
|
|
|
|
client := a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
libassert.HTTPServiceEchoesResHeaderWithClient(t, client, addr, path, expectedResHeader)
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
func (a *Asserter) HTTPStatus(
|
2023-07-18 23:41:30 +00:00
|
|
|
t *testing.T,
|
2023-11-10 19:22:06 +00:00
|
|
|
workload *topology.Workload,
|
2023-07-18 23:41:30 +00:00
|
|
|
port int,
|
|
|
|
status int,
|
|
|
|
) {
|
|
|
|
t.Helper()
|
|
|
|
require.True(t, port > 0)
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
node := workload.Node
|
2023-07-18 23:41:30 +00:00
|
|
|
ip := node.LocalAddress()
|
|
|
|
addr := fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
|
|
|
|
client := a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
|
|
|
|
url := "http://" + addr
|
|
|
|
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: 30 * time.Second, Wait: 500 * time.Millisecond}, t, func(r *retry.R) {
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("could not make request to %q: %v", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != status {
|
|
|
|
r.Fatalf("expected status %d, got %d", status, resp.StatusCode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// asserts that the service sid in cluster and exported by peer localPeerName is passing health checks,
|
2023-11-10 19:22:06 +00:00
|
|
|
func (a *Asserter) HealthyWithPeer(t *testing.T, cluster string, sid topology.ID, peerName string) {
|
2023-07-18 23:41:30 +00:00
|
|
|
t.Helper()
|
|
|
|
cl := a.mustGetAPIClient(t, cluster)
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: time.Minute * 1, Wait: time.Millisecond * 500}, t, func(r *retry.R) {
|
|
|
|
svcs, _, err := cl.Health().Service(
|
|
|
|
sid.Name,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
utils.CompatQueryOpts(&api.QueryOptions{
|
|
|
|
Partition: sid.Partition,
|
|
|
|
Namespace: sid.Namespace,
|
|
|
|
Peer: peerName,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
require.NoError(r, err)
|
|
|
|
assert.GreaterOrEqual(r, len(svcs), 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
type testingT interface {
|
|
|
|
require.TestingT
|
|
|
|
Helper()
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
// does a fortio /fetch2 to the given fortio service, targetting the given destination. Returns
|
2023-07-18 23:41:30 +00:00
|
|
|
// the body, and response with response.Body already Closed.
|
|
|
|
//
|
|
|
|
// We treat 400, 503, and 504s as retryable errors
|
2023-11-10 19:22:06 +00:00
|
|
|
func (a *Asserter) fortioFetch2Destination(
|
2023-12-06 17:11:32 +00:00
|
|
|
t testutil.TestingTB,
|
2023-11-02 19:25:48 +00:00
|
|
|
client *http.Client,
|
|
|
|
addr string,
|
2023-11-10 19:22:06 +00:00
|
|
|
dest *topology.Destination,
|
2023-11-02 19:25:48 +00:00
|
|
|
path string,
|
|
|
|
) (body []byte, res *http.Response) {
|
2023-07-18 23:41:30 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2024-02-07 20:21:44 +00:00
|
|
|
err, res := getFortioFetch2DestinationResponse(t, client, addr, dest, path, nil)
|
2024-01-10 15:55:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
// not sure when these happen, suspect it's when the mesh gateway in the peer is not yet ready
|
|
|
|
require.NotEqual(t, http.StatusServiceUnavailable, res.StatusCode)
|
|
|
|
require.NotEqual(t, http.StatusGatewayTimeout, res.StatusCode)
|
|
|
|
// not sure when this happens, suspect it's when envoy hasn't configured the local destination yet
|
|
|
|
require.NotEqual(t, http.StatusBadRequest, res.StatusCode)
|
|
|
|
body, err = io.ReadAll(res.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return body, res
|
|
|
|
}
|
|
|
|
|
2024-02-07 20:21:44 +00:00
|
|
|
func getFortioFetch2DestinationResponse(t testutil.TestingTB, client *http.Client, addr string, dest *topology.Destination, path string, headers map[string]string) (error, *http.Response) {
|
2023-11-02 21:13:16 +00:00
|
|
|
var actualURL string
|
2023-11-10 19:22:06 +00:00
|
|
|
if dest.Implied {
|
2023-11-02 21:13:16 +00:00
|
|
|
actualURL = fmt.Sprintf("http://%s--%s--%s.virtual.consul:%d/%s",
|
2023-11-10 19:22:06 +00:00
|
|
|
dest.ID.Name,
|
|
|
|
dest.ID.Namespace,
|
|
|
|
dest.ID.Partition,
|
|
|
|
dest.VirtualPort,
|
2023-11-02 21:13:16 +00:00
|
|
|
path,
|
|
|
|
)
|
|
|
|
} else {
|
2023-11-10 19:22:06 +00:00
|
|
|
actualURL = fmt.Sprintf("http://localhost:%d/%s", dest.LocalPort, path)
|
2023-11-02 21:13:16 +00:00
|
|
|
}
|
2023-07-18 23:41:30 +00:00
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
url := fmt.Sprintf("http://%s/fortio/fetch2?url=%s", addr,
|
2023-11-02 21:13:16 +00:00
|
|
|
url.QueryEscape(actualURL),
|
2023-07-18 23:41:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, nil)
|
|
|
|
require.NoError(t, err)
|
2023-11-02 19:25:48 +00:00
|
|
|
|
2024-02-07 20:21:44 +00:00
|
|
|
for h, v := range headers {
|
|
|
|
req.Header.Set(h, v)
|
|
|
|
}
|
2024-01-10 15:55:50 +00:00
|
|
|
res, err := client.Do(req)
|
2023-11-02 19:25:48 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-10 15:55:50 +00:00
|
|
|
return err, res
|
2023-07-18 23:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// uses the /fortio/fetch2 endpoint to do a header echo check against an
|
2023-11-10 19:22:06 +00:00
|
|
|
// destination fortio
|
|
|
|
func (a *Asserter) FortioFetch2HeaderEcho(t *testing.T, fortioWrk *topology.Workload, dest *topology.Destination) {
|
2023-07-18 23:41:30 +00:00
|
|
|
const kPassphrase = "x-passphrase"
|
|
|
|
const passphrase = "hello"
|
|
|
|
path := (fmt.Sprintf("/?header=%s:%s", kPassphrase, passphrase))
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
var (
|
2023-11-10 19:22:06 +00:00
|
|
|
node = fortioWrk.Node
|
|
|
|
addr = fmt.Sprintf("%s:%d", node.LocalAddress(), fortioWrk.PortOrDefault(dest.PortName))
|
2023-11-02 19:25:48 +00:00
|
|
|
client = a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
)
|
|
|
|
|
2023-07-18 23:41:30 +00:00
|
|
|
retry.RunWith(&retry.Timer{Timeout: 60 * time.Second, Wait: time.Millisecond * 500}, t, func(r *retry.R) {
|
2023-11-10 19:22:06 +00:00
|
|
|
_, res := a.fortioFetch2Destination(r, client, addr, dest, path)
|
2023-11-02 19:25:48 +00:00
|
|
|
require.Equal(r, http.StatusOK, res.StatusCode)
|
2023-07-18 23:41:30 +00:00
|
|
|
v := res.Header.Get(kPassphrase)
|
2023-11-02 19:25:48 +00:00
|
|
|
require.Equal(r, passphrase, v)
|
2023-07-18 23:41:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// similar to libassert.AssertFortioName,
|
2023-11-10 19:22:06 +00:00
|
|
|
// uses the /fortio/fetch2 endpoint to hit the debug endpoint on the destination,
|
2023-07-18 23:41:30 +00:00
|
|
|
// and assert that the FORTIO_NAME == name
|
2023-11-02 21:13:16 +00:00
|
|
|
func (a *Asserter) FortioFetch2FortioName(
|
|
|
|
t *testing.T,
|
2023-11-10 19:22:06 +00:00
|
|
|
fortioWrk *topology.Workload,
|
|
|
|
dest *topology.Destination,
|
2023-11-02 21:13:16 +00:00
|
|
|
clusterName string,
|
2023-11-10 19:22:06 +00:00
|
|
|
sid topology.ID,
|
2023-11-02 21:13:16 +00:00
|
|
|
) {
|
2023-07-18 23:41:30 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
var (
|
2023-11-10 19:22:06 +00:00
|
|
|
node = fortioWrk.Node
|
|
|
|
addr = fmt.Sprintf("%s:%d", node.LocalAddress(), fortioWrk.PortOrDefault(dest.PortName))
|
2023-11-02 19:25:48 +00:00
|
|
|
client = a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
)
|
|
|
|
|
2023-07-18 23:41:30 +00:00
|
|
|
var fortioNameRE = regexp.MustCompile(("\nFORTIO_NAME=(.+)\n"))
|
|
|
|
path := "/debug?env=dump"
|
|
|
|
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: 60 * time.Second, Wait: time.Millisecond * 500}, t, func(r *retry.R) {
|
2023-11-10 19:22:06 +00:00
|
|
|
body, res := a.fortioFetch2Destination(r, client, addr, dest, path)
|
2023-11-02 21:13:16 +00:00
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
require.Equal(r, http.StatusOK, res.StatusCode)
|
2023-07-18 23:41:30 +00:00
|
|
|
|
|
|
|
// TODO: not sure we should retry these?
|
|
|
|
m := fortioNameRE.FindStringSubmatch(string(body))
|
|
|
|
require.GreaterOrEqual(r, len(m), 2)
|
|
|
|
// TODO: dedupe from NewFortioService
|
|
|
|
require.Equal(r, fmt.Sprintf("%s::%s", clusterName, sid.String()), m[1])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:55:50 +00:00
|
|
|
func (a *Asserter) FortioFetch2ServiceUnavailable(t *testing.T, fortioWrk *topology.Workload, dest *topology.Destination) {
|
|
|
|
const kPassphrase = "x-passphrase"
|
|
|
|
const passphrase = "hello"
|
|
|
|
path := (fmt.Sprintf("/?header=%s:%s", kPassphrase, passphrase))
|
2024-02-07 20:21:44 +00:00
|
|
|
a.FortioFetch2ServiceStatusCodes(t, fortioWrk, dest, path, nil, []int{http.StatusServiceUnavailable})
|
|
|
|
}
|
2024-01-10 15:55:50 +00:00
|
|
|
|
2024-02-07 20:21:44 +00:00
|
|
|
// FortioFetch2ServiceStatusCodes uses the /fortio/fetch2 endpoint to do a header echo check against a destination
|
|
|
|
// fortio and asserts that the returned status code matches the desired one(s)
|
|
|
|
func (a *Asserter) FortioFetch2ServiceStatusCodes(t *testing.T, fortioWrk *topology.Workload, dest *topology.Destination, path string, headers map[string]string, statuses []int) {
|
2024-01-10 15:55:50 +00:00
|
|
|
var (
|
|
|
|
node = fortioWrk.Node
|
|
|
|
addr = fmt.Sprintf("%s:%d", node.LocalAddress(), fortioWrk.PortOrDefault(dest.PortName))
|
|
|
|
client = a.mustGetHTTPClient(t, node.Cluster)
|
|
|
|
)
|
|
|
|
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: 60 * time.Second, Wait: time.Millisecond * 500}, t, func(r *retry.R) {
|
2024-02-07 20:21:44 +00:00
|
|
|
_, res := getFortioFetch2DestinationResponse(r, client, addr, dest, path, headers)
|
2024-01-10 15:55:50 +00:00
|
|
|
defer res.Body.Close()
|
2024-02-07 20:21:44 +00:00
|
|
|
require.Contains(r, statuses, res.StatusCode)
|
2024-01-10 15:55:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-18 23:41:30 +00:00
|
|
|
// CatalogServiceExists is the same as libassert.CatalogServiceExists, except that it uses
|
|
|
|
// a proxied API client
|
2023-11-02 19:25:48 +00:00
|
|
|
func (a *Asserter) CatalogServiceExists(t *testing.T, cluster string, svc string, opts *api.QueryOptions) {
|
2023-07-18 23:41:30 +00:00
|
|
|
t.Helper()
|
|
|
|
cl := a.mustGetAPIClient(t, cluster)
|
|
|
|
libassert.CatalogServiceExists(t, cl, svc, opts)
|
|
|
|
}
|
2024-01-10 15:55:50 +00:00
|
|
|
|
2024-01-22 23:35:06 +00:00
|
|
|
// HealthServiceEntries asserts the service has the expected number of instances and checks
|
|
|
|
func (a *Asserter) HealthServiceEntries(t *testing.T, cluster string, node *topology.Node, svc string, passingOnly bool, opts *api.QueryOptions, expectedInstance int, expectedChecks int) []*api.ServiceEntry {
|
2024-01-10 15:55:50 +00:00
|
|
|
t.Helper()
|
2024-01-22 23:35:06 +00:00
|
|
|
cl, err := a.sp.APIClientForNode(cluster, node.ID(), "")
|
|
|
|
require.NoError(t, err)
|
2024-01-12 23:35:44 +00:00
|
|
|
health := cl.Health()
|
|
|
|
|
|
|
|
var serviceEntries []*api.ServiceEntry
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: 60 * time.Second, Wait: time.Millisecond * 500}, t, func(r *retry.R) {
|
|
|
|
serviceEntries, _, err = health.Service(svc, "", passingOnly, opts)
|
|
|
|
require.NoError(r, err)
|
2024-01-22 23:35:06 +00:00
|
|
|
require.Equalf(r, expectedInstance, len(serviceEntries), "dc: %s, service: %s", cluster, serviceEntries[0].Service.Service)
|
|
|
|
require.Equalf(r, expectedChecks, len(serviceEntries[0].Checks), "dc: %s, service: %s", cluster, serviceEntries[0].Service.Service)
|
2024-01-12 23:35:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return serviceEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenExist asserts the token exists in the cluster and identical to the expected token
|
2024-01-22 23:35:06 +00:00
|
|
|
func (a *Asserter) TokenExist(t *testing.T, cluster string, node *topology.Node, expectedToken *api.ACLToken) {
|
2024-01-12 23:35:44 +00:00
|
|
|
t.Helper()
|
2024-01-22 23:35:06 +00:00
|
|
|
cl, err := a.sp.APIClientForNode(cluster, node.ID(), "")
|
|
|
|
require.NoError(t, err)
|
2024-01-12 23:35:44 +00:00
|
|
|
acl := cl.ACL()
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: 60 * time.Second, Wait: time.Millisecond * 500}, t, func(r *retry.R) {
|
|
|
|
retrievedToken, _, err := acl.TokenRead(expectedToken.AccessorID, &api.QueryOptions{})
|
2024-01-10 15:55:50 +00:00
|
|
|
require.NoError(r, err)
|
2024-01-12 23:35:44 +00:00
|
|
|
require.True(r, cmp.Equal(expectedToken, retrievedToken), "token %s", expectedToken.Description)
|
2024-01-10 15:55:50 +00:00
|
|
|
})
|
|
|
|
}
|
2024-03-06 09:29:54 +00:00
|
|
|
|
|
|
|
// AutopilotHealth asserts the autopilot health endpoint return expected state
|
|
|
|
func (a *Asserter) AutopilotHealth(t *testing.T, cluster *topology.Cluster, leaderName string, expectedHealthy bool) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
retry.RunWith(&retry.Timer{Timeout: 60 * time.Second, Wait: time.Millisecond * 10}, t, func(r *retry.R) {
|
|
|
|
var out *api.OperatorHealthReply
|
|
|
|
for _, node := range cluster.Nodes {
|
|
|
|
if node.Name == leaderName {
|
|
|
|
client, err := a.sp.APIClientForNode(cluster.Name, node.ID(), "")
|
|
|
|
if err != nil {
|
|
|
|
r.Log("err at node", node.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
operator := client.Operator()
|
|
|
|
out, err = operator.AutopilotServerHealth(&api.QueryOptions{})
|
|
|
|
if err != nil {
|
|
|
|
r.Log("err at node", node.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Got the Autopilot server health response, break the loop
|
|
|
|
r.Log("Got response at node", node.Name)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Log("out", out, "health", out.Healthy)
|
|
|
|
require.Equal(r, expectedHealthy, out.Healthy)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuditEntry struct {
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
EventType string `json:"event_type"`
|
|
|
|
Payload Payload `json:"payload"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Payload struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Timestamp time.Time `json:"timestamp"`
|
|
|
|
Auth Auth `json:"auth"`
|
|
|
|
Request Request `json:"request"`
|
|
|
|
Response Response `json:"response,omitempty"` // Response is not present in OperationStart log
|
|
|
|
Stage string `json:"stage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Auth struct {
|
|
|
|
AccessorID string `json:"accessor_id"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
CreateTime time.Time `json:"create_time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
Operation string `json:"operation"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
RemoteAddr string `json:"remote_addr"`
|
|
|
|
UserAgent string `json:"user_agent"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
QueryParams map[string]string `json:"query_params,omitempty"` // QueryParams might not be present in all cases
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Error string `json:"error,omitempty"` // Error field is optional,shows up only with the status is non 2xx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Asserter) ValidateHealthEndpointAuditLog(t *testing.T, filePath string) {
|
|
|
|
boolIsErrorJSON := false
|
|
|
|
jsonData, err := os.ReadFile(filePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Print details of each AuditEntry
|
|
|
|
var entry AuditEntry
|
|
|
|
events := strings.Split(strings.TrimSpace(string(jsonData)), "\n")
|
|
|
|
|
|
|
|
// function to validate the error object when the health endpoint is returning 429
|
|
|
|
isErrorJSONObject := func(errorString string) error {
|
|
|
|
// Attempt to unmarshal the error string into a map[string]interface{}
|
|
|
|
var m map[string]interface{}
|
|
|
|
err := json.Unmarshal([]byte(errorString), &m)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
if strings.Contains(event, "/v1/operator/autopilot/health") && strings.Contains(event, "OperationComplete") {
|
|
|
|
err = json.Unmarshal([]byte(event), &entry)
|
|
|
|
require.NoError(t, err, "Error unmarshalling JSON: %v\", err")
|
|
|
|
if entry.Payload.Response.Status == "429" {
|
|
|
|
boolIsErrorJSON = true
|
|
|
|
errResponse := entry.Payload.Response.Error
|
|
|
|
err = isErrorJSONObject(errResponse)
|
|
|
|
require.NoError(t, err, "Autopilot Health endpoint error response in the audit log is in unexpected format: %v\", err")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, boolIsErrorJSON, true, "Unable to verify audit log health endpoint error message")
|
|
|
|
}
|