2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2017-11-28 21:57:45 +00:00
|
|
|
"fmt"
|
2015-07-29 23:33:25 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-11-28 21:57:45 +00:00
|
|
|
"strings"
|
2015-07-29 23:33:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
|
|
|
|
2018-02-15 19:58:02 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-05-21 18:31:37 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2018-09-07 14:30:47 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2015-07-29 23:33:25 +00:00
|
|
|
)
|
|
|
|
|
2017-11-28 21:57:45 +00:00
|
|
|
func TestCoordinate_Disabled_Response(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:57:45 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, `
|
2017-11-28 21:57:45 +00:00
|
|
|
disable_coordinates = true
|
|
|
|
`)
|
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-11-28 21:57:45 +00:00
|
|
|
|
|
|
|
tests := []func(resp http.ResponseWriter, req *http.Request) (interface{}, error){
|
|
|
|
a.srv.CoordinateDatacenters,
|
|
|
|
a.srv.CoordinateNodes,
|
|
|
|
a.srv.CoordinateNode,
|
|
|
|
a.srv.CoordinateUpdate,
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/should/not/care", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := tt(resp, req)
|
2023-02-17 20:04:12 +00:00
|
|
|
if httpErr, ok := err.(HTTPError); ok {
|
|
|
|
if httpErr.StatusCode != 401 {
|
|
|
|
t.Fatalf("expected status 401 but got %d", httpErr.StatusCode)
|
2022-04-29 17:42:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected HTTP error but got %v", err)
|
2017-11-28 21:57:45 +00:00
|
|
|
}
|
2022-04-29 17:42:49 +00:00
|
|
|
|
2017-11-28 21:57:45 +00:00
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %#v", obj)
|
|
|
|
}
|
2022-01-31 16:17:35 +00:00
|
|
|
if !strings.Contains(err.Error(), "Coordinate support disabled") {
|
2017-11-28 21:57:45 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
func TestCoordinate_Datacenters(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/coordinate/datacenters", nil)
|
2015-07-29 23:33:25 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CoordinateDatacenters(resp, req)
|
2015-07-29 23:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:55:05 +00:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
t.Fatalf("bad: %v", resp.Code)
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
maps := obj.([]structs.DatacenterMap)
|
|
|
|
if len(maps) != 1 ||
|
|
|
|
maps[0].Datacenter != "dc1" ||
|
|
|
|
len(maps[0].Coordinates) != 1 ||
|
2017-05-21 07:11:09 +00:00
|
|
|
maps[0].Coordinates[0].Node != a.Config.NodeName {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("bad: %v", maps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoordinate_Nodes(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
// Make sure an empty list is non-nil.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2019-05-21 18:31:37 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
obj, err := a.srv.CoordinateNodes(resp, req)
|
|
|
|
if err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2019-05-21 18:31:37 +00:00
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
2019-08-29 14:55:05 +00:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
r.Fatalf("bad: %v", resp.Code)
|
|
|
|
}
|
|
|
|
|
2019-05-21 18:31:37 +00:00
|
|
|
// Check that coordinates are empty before registering a node
|
|
|
|
coordinates, ok := obj.(structs.Coordinates)
|
|
|
|
if !ok {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("expected: structs.Coordinates, received: %+v", obj)
|
2019-05-21 18:31:37 +00:00
|
|
|
}
|
2019-05-17 18:32:36 +00:00
|
|
|
|
2019-05-21 18:31:37 +00:00
|
|
|
if len(coordinates) != 0 {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("coordinates should be empty, received: %v", coordinates)
|
2019-05-21 18:31:37 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-15 05:05:37 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// Register the nodes.
|
|
|
|
nodes := []string{"foo", "bar"}
|
|
|
|
for _, node := range nodes {
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: node,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var reply struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Catalog.Register", &req, &reply); err != nil {
|
2015-10-23 22:19:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
// Send some coordinates for a few nodes, waiting a little while for the
|
|
|
|
// batch update to run.
|
|
|
|
arg1 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()),
|
|
|
|
}
|
|
|
|
var out struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Coordinate.Update", &arg1, &out); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg2 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()),
|
|
|
|
}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Coordinate.Update", &arg2, &out); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-03-22 01:23:11 +00:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2015-07-30 18:31:35 +00:00
|
|
|
// Query back and check the nodes are present and sorted correctly.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2019-05-21 18:31:37 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
obj, err := a.srv.CoordinateNodes(resp, req)
|
|
|
|
if err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2019-05-21 18:31:37 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
|
2019-08-29 14:55:05 +00:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
r.Fatalf("bad: %v", resp.Code)
|
|
|
|
}
|
|
|
|
|
2019-05-21 18:31:37 +00:00
|
|
|
coordinates, ok := obj.(structs.Coordinates)
|
|
|
|
if !ok {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("expected: structs.Coordinates, received: %+v", obj)
|
2019-05-21 18:31:37 +00:00
|
|
|
}
|
|
|
|
if len(coordinates) != 2 ||
|
|
|
|
coordinates[0].Node != "bar" ||
|
|
|
|
coordinates[1].Node != "foo" {
|
2020-01-27 13:00:33 +00:00
|
|
|
r.Fatalf("expected: bar, foo received: %v", coordinates)
|
2019-05-21 18:31:37 +00:00
|
|
|
}
|
|
|
|
})
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
2017-10-26 12:24:42 +00:00
|
|
|
|
|
|
|
func TestCoordinate_Node(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-26 12:24:42 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-10-26 12:24:42 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-26 12:24:42 +00:00
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
// Make sure we get a 404 with no coordinates.
|
2017-10-26 12:24:42 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/coordinate/node/foo?dc=dc1", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2020-05-14 18:41:20 +00:00
|
|
|
_, err := a.srv.CoordinateNode(resp, req)
|
2017-10-26 12:24:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-08-29 14:55:05 +00:00
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
if resp.Code != http.StatusNotFound {
|
|
|
|
t.Fatalf("bad: %v", resp.Code)
|
2017-10-26 12:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the nodes.
|
|
|
|
nodes := []string{"foo", "bar"}
|
|
|
|
for _, node := range nodes {
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: node,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var reply struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Catalog.Register", &req, &reply); err != nil {
|
2017-10-26 12:24:42 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send some coordinates for a few nodes, waiting a little while for the
|
|
|
|
// batch update to run.
|
|
|
|
arg1 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()),
|
|
|
|
}
|
|
|
|
var out struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Coordinate.Update", &arg1, &out); err != nil {
|
2017-10-26 12:24:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg2 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()),
|
|
|
|
}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Coordinate.Update", &arg2, &out); err != nil {
|
2017-10-26 12:24:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
|
2017-11-01 18:25:33 +00:00
|
|
|
// Query back and check the nodes are present.
|
2017-10-26 12:24:42 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/coordinate/node/foo?dc=dc1", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
2020-05-14 18:41:20 +00:00
|
|
|
obj, err := a.srv.CoordinateNode(resp, req)
|
2017-10-26 12:24:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:55:05 +00:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
t.Fatalf("bad: %v", resp.Code)
|
|
|
|
}
|
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
coordinates := obj.(structs.Coordinates)
|
2017-10-26 12:24:42 +00:00
|
|
|
if len(coordinates) != 1 ||
|
|
|
|
coordinates[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", coordinates)
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 03:12:54 +00:00
|
|
|
|
|
|
|
func TestCoordinate_Update(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-27 03:12:54 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-10-27 03:12:54 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-27 03:12:54 +00:00
|
|
|
|
|
|
|
// Register the node.
|
|
|
|
reg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var reply struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Catalog.Register", ®, &reply); err != nil {
|
2017-10-27 03:12:54 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the coordinates and wait for it to complete.
|
|
|
|
coord := coordinate.NewCoordinate(coordinate.DefaultConfig())
|
|
|
|
coord.Height = -5.0
|
|
|
|
body := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: coord,
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/coordinate/update", jsonReader(body))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.CoordinateUpdate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-08-29 14:55:05 +00:00
|
|
|
|
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
t.Fatalf("bad: %v", resp.Code)
|
|
|
|
}
|
|
|
|
|
2017-10-27 03:12:54 +00:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
|
|
|
|
// Query back and check the coordinates are present.
|
|
|
|
args := structs.NodeSpecificRequest{Node: "foo", Datacenter: "dc1"}
|
|
|
|
var coords structs.IndexedCoordinates
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Coordinate.Node", &args, &coords); err != nil {
|
2017-10-27 03:12:54 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
coordinates := coords.Coordinates
|
|
|
|
if len(coordinates) != 1 ||
|
|
|
|
coordinates[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", coordinates)
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 19:58:02 +00:00
|
|
|
|
|
|
|
func TestCoordinate_Update_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:58:02 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, TestACLConfig())
|
2018-02-15 19:58:02 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2018-02-15 19:58:02 +00:00
|
|
|
|
|
|
|
coord := coordinate.NewCoordinate(coordinate.DefaultConfig())
|
|
|
|
coord.Height = -5.0
|
|
|
|
body := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: coord,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no token", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/coordinate/update", jsonReader(body))
|
|
|
|
if _, err := a.srv.CoordinateUpdate(nil, req); !acl.IsErrPermissionDenied(err) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("valid token", func(t *testing.T) {
|
2023-01-24 16:21:41 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/coordinate/update", jsonReader(body))
|
|
|
|
req.Header.Add("X-Consul-Token", "root")
|
2018-02-15 19:58:02 +00:00
|
|
|
if _, err := a.srv.CoordinateUpdate(nil, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|