2022-11-01 19:03:23 +00:00
|
|
|
package assert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2023-01-20 22:02:44 +00:00
|
|
|
libservice "github.com/hashicorp/consul/test/integration/consul-container/libs/service"
|
2022-11-01 19:03:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-04 20:28:15 +00:00
|
|
|
defaultHTTPTimeout = 100 * time.Second
|
2022-11-01 19:03:23 +00:00
|
|
|
defaultHTTPWait = defaultWait
|
|
|
|
)
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
// CatalogServiceExists verifies the service name exists in the Consul catalog
|
|
|
|
func CatalogServiceExists(t *testing.T, c *api.Client, svc string) {
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
services, _, err := c.Catalog().Service(svc, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatal("error reading peering data")
|
|
|
|
}
|
|
|
|
if len(services) == 0 {
|
|
|
|
r.Fatal("did not find catalog entry for ", svc)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// HTTPServiceEchoes verifies that a post to the given ip/port combination returns the data
|
2023-01-20 22:02:44 +00:00
|
|
|
// in the response body. Optional path can be provided to differentiate requests.
|
|
|
|
func HTTPServiceEchoes(t *testing.T, ip string, port int, path string) {
|
2023-01-11 21:34:27 +00:00
|
|
|
const phrase = "hello"
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
failer := func() *retry.Timer {
|
|
|
|
return &retry.Timer{Timeout: defaultHTTPTimeout, Wait: defaultHTTPWait}
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
client := cleanhttp.DefaultClient()
|
2022-11-01 19:03:23 +00:00
|
|
|
url := fmt.Sprintf("http://%s:%d", ip, port)
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
if path != "" {
|
|
|
|
url += "/" + path
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
retry.RunWith(failer(), t, func(r *retry.R) {
|
|
|
|
t.Logf("making call to %s", url)
|
|
|
|
reader := strings.NewReader(phrase)
|
|
|
|
res, err := client.Post(url, "text/plain", reader)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatal("could not make call to service ", url)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatal("could not read response body ", url)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(string(body), phrase) {
|
|
|
|
r.Fatal("received an incorrect response ", body)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
// ServiceLogContains returns true if the service container has the target string in its logs
|
|
|
|
func ServiceLogContains(t *testing.T, service libservice.Service, target string) bool {
|
|
|
|
logs, err := service.GetLogs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return strings.Contains(logs, target)
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
2023-02-01 15:48:54 +00:00
|
|
|
|
|
|
|
// AssertContainerState validates service container status
|
|
|
|
func AssertContainerState(t *testing.T, service libservice.Service, state string) {
|
|
|
|
containerStatus, err := service.GetStatus()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, containerStatus, state, fmt.Sprintf("Expected: %s. Got %s", containerStatus, state))
|
|
|
|
}
|