2017-03-23 20:26:05 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
// copied from testutil to break circular dependency
|
|
|
|
const (
|
|
|
|
HealthAny = "any"
|
|
|
|
HealthPassing = "passing"
|
|
|
|
HealthWarning = "warning"
|
|
|
|
HealthCritical = "critical"
|
|
|
|
HealthMaint = "maintenance"
|
|
|
|
)
|
|
|
|
|
2017-03-23 20:26:05 +00:00
|
|
|
// JoinLAN is used to join local datacenters together.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) JoinLAN(t testing.TB, addr string) {
|
2017-09-26 06:11:19 +00:00
|
|
|
resp := s.put(t, "/v1/agent/join/"+addr, nil)
|
2017-03-23 20:26:05 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinWAN is used to join remote datacenters together.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) JoinWAN(t testing.TB, addr string) {
|
2017-09-26 06:11:19 +00:00
|
|
|
resp := s.put(t, "/v1/agent/join/"+addr+"?wan=1", nil)
|
2017-03-23 20:26:05 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKV sets an individual key in the K/V store.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) SetKV(t testing.TB, key string, val []byte) {
|
2017-03-23 20:26:05 +00:00
|
|
|
resp := s.put(t, "/v1/kv/"+key, bytes.NewBuffer(val))
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKVString sets an individual key in the K/V store, but accepts a string
|
|
|
|
// instead of []byte.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) SetKVString(t testing.TB, key string, val string) {
|
2017-03-23 20:26:05 +00:00
|
|
|
resp := s.put(t, "/v1/kv/"+key, bytes.NewBufferString(val))
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKV retrieves a single key and returns its value
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) GetKV(t testing.TB, key string) []byte {
|
2017-03-23 20:26:05 +00:00
|
|
|
resp := s.get(t, "/v1/kv/"+key)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2017-03-23 20:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*TestKVResponse
|
|
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal: %s", err)
|
|
|
|
}
|
|
|
|
if len(result) < 1 {
|
|
|
|
t.Fatalf("key does not exist: %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := base64.StdEncoding.DecodeString(result[0].Value)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to base64 decode: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKVString retrieves a value from the store, but returns as a string instead
|
|
|
|
// of []byte.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) GetKVString(t testing.TB, key string) string {
|
2017-03-23 20:26:05 +00:00
|
|
|
return string(s.GetKV(t, key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PopulateKV fills the Consul KV with data from a generic map.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) PopulateKV(t testing.TB, data map[string][]byte) {
|
2017-03-23 20:26:05 +00:00
|
|
|
for k, v := range data {
|
|
|
|
s.SetKV(t, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListKV returns a list of keys present in the KV store. This will list all
|
|
|
|
// keys under the given prefix recursively and return them as a slice.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) ListKV(t testing.TB, prefix string) []string {
|
2017-03-23 20:26:05 +00:00
|
|
|
resp := s.get(t, "/v1/kv/"+prefix+"?keys")
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2017-03-23 20:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal: %s", err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddService adds a new service to the Consul instance. It also
|
|
|
|
// automatically adds a health check with the given status, which
|
|
|
|
// can be one of "passing", "warning", or "critical".
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) AddService(t testing.TB, name, status string, tags []string) {
|
2016-09-30 06:26:12 +00:00
|
|
|
s.AddAddressableService(t, name, status, "", 0, tags) // set empty address and 0 as port for non-accessible service
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddressableService adds a new service to the Consul instance by
|
|
|
|
// passing "address" and "port". It is helpful when you need to prepare a fakeService
|
|
|
|
// that maybe accessed with in target source code.
|
|
|
|
// It also automatically adds a health check with the given status, which
|
|
|
|
// can be one of "passing", "warning", or "critical", just like `AddService` does.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) AddAddressableService(t testing.TB, name, status, address string, port int, tags []string) {
|
2017-03-23 20:26:05 +00:00
|
|
|
svc := &TestService{
|
2016-09-30 06:26:12 +00:00
|
|
|
Name: name,
|
|
|
|
Tags: tags,
|
|
|
|
Address: address,
|
|
|
|
Port: port,
|
2017-03-23 20:26:05 +00:00
|
|
|
}
|
|
|
|
payload, err := s.encodePayload(svc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s.put(t, "/v1/agent/service/register", payload)
|
|
|
|
|
|
|
|
chkName := "service:" + name
|
|
|
|
chk := &TestCheck{
|
|
|
|
Name: chkName,
|
|
|
|
ServiceID: name,
|
|
|
|
TTL: "10m",
|
|
|
|
}
|
|
|
|
payload, err = s.encodePayload(chk)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s.put(t, "/v1/agent/check/register", payload)
|
|
|
|
|
|
|
|
switch status {
|
2017-04-19 23:00:11 +00:00
|
|
|
case HealthPassing:
|
2017-03-23 20:26:05 +00:00
|
|
|
s.put(t, "/v1/agent/check/pass/"+chkName, nil)
|
2017-04-19 23:00:11 +00:00
|
|
|
case HealthWarning:
|
2017-03-23 20:26:05 +00:00
|
|
|
s.put(t, "/v1/agent/check/warn/"+chkName, nil)
|
2017-04-19 23:00:11 +00:00
|
|
|
case HealthCritical:
|
2017-03-23 20:26:05 +00:00
|
|
|
s.put(t, "/v1/agent/check/fail/"+chkName, nil)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unrecognized status: %s", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCheck adds a check to the Consul instance. If the serviceID is
|
|
|
|
// left empty (""), then the check will be associated with the node.
|
|
|
|
// The check status may be "passing", "warning", or "critical".
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) AddCheck(t testing.TB, name, serviceID, status string) {
|
2017-03-23 20:26:05 +00:00
|
|
|
chk := &TestCheck{
|
|
|
|
ID: name,
|
|
|
|
Name: name,
|
|
|
|
TTL: "10m",
|
|
|
|
}
|
|
|
|
if serviceID != "" {
|
|
|
|
chk.ServiceID = serviceID
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := s.encodePayload(chk)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s.put(t, "/v1/agent/check/register", payload)
|
|
|
|
|
|
|
|
switch status {
|
2017-04-19 23:00:11 +00:00
|
|
|
case HealthPassing:
|
2017-03-23 20:26:05 +00:00
|
|
|
s.put(t, "/v1/agent/check/pass/"+name, nil)
|
2017-04-19 23:00:11 +00:00
|
|
|
case HealthWarning:
|
2017-03-23 20:26:05 +00:00
|
|
|
s.put(t, "/v1/agent/check/warn/"+name, nil)
|
2017-04-19 23:00:11 +00:00
|
|
|
case HealthCritical:
|
2017-03-23 20:26:05 +00:00
|
|
|
s.put(t, "/v1/agent/check/fail/"+name, nil)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unrecognized status: %s", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// put performs a new HTTP PUT request.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) put(t testing.TB, path string, body io.Reader) *http.Response {
|
2017-03-23 20:26:05 +00:00
|
|
|
req, err := http.NewRequest("PUT", s.url(path), body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create PUT request: %s", err)
|
|
|
|
}
|
2017-04-21 00:02:42 +00:00
|
|
|
resp, err := s.HTTPClient.Do(req)
|
2017-03-23 20:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to make PUT request: %s", err)
|
|
|
|
}
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
t.Fatalf("not OK PUT: %s", err)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// get performs a new HTTP GET request.
|
2021-03-16 22:05:39 +00:00
|
|
|
func (s *TestServer) get(t testing.TB, path string) *http.Response {
|
2017-04-21 00:02:42 +00:00
|
|
|
resp, err := s.HTTPClient.Get(s.url(path))
|
2017-03-23 20:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create GET request: %s", err)
|
|
|
|
}
|
|
|
|
if err := s.requireOK(resp); err != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
t.Fatalf("not OK GET: %s", err)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodePayload returns a new io.Reader wrapping the encoded contents
|
|
|
|
// of the payload, suitable for passing directly to a new request.
|
|
|
|
func (s *TestServer) encodePayload(payload interface{}) (io.Reader, error) {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&encoded)
|
|
|
|
if err := enc.Encode(payload); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to encode payload")
|
|
|
|
}
|
|
|
|
return &encoded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// url is a helper function which takes a relative URL and
|
|
|
|
// makes it into a proper URL against the local Consul server.
|
|
|
|
func (s *TestServer) url(path string) string {
|
|
|
|
if s == nil {
|
|
|
|
log.Fatal("s is nil")
|
|
|
|
}
|
|
|
|
if s.Config == nil {
|
|
|
|
log.Fatal("s.Config is nil")
|
|
|
|
}
|
|
|
|
if s.Config.Ports == nil {
|
|
|
|
log.Fatal("s.Config.Ports is nil")
|
|
|
|
}
|
|
|
|
if s.Config.Ports.HTTP == 0 {
|
|
|
|
log.Fatal("s.Config.Ports.HTTP is 0")
|
|
|
|
}
|
|
|
|
if path == "" {
|
|
|
|
log.Fatal("path is empty")
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("http://127.0.0.1:%d%s", s.Config.Ports.HTTP, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// requireOK checks the HTTP response code and ensures it is acceptable.
|
|
|
|
func (s *TestServer) requireOK(resp *http.Response) error {
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Bad status code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|