2016-08-30 02:09:57 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-15 02:54:37 +00:00
|
|
|
"fmt"
|
2016-08-30 02:09:57 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-03-10 00:43:07 +00:00
|
|
|
"time"
|
2016-08-30 02:09:57 +00:00
|
|
|
|
2017-03-10 00:43:07 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-08-30 02:09:57 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2016-08-30 02:09:57 +00:00
|
|
|
)
|
|
|
|
|
2017-03-23 02:26:43 +00:00
|
|
|
func TestOperator_RaftConfiguration(t *testing.T) {
|
2016-08-30 02:09:57 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/raft/configuration", body)
|
2016-08-30 02:09:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.OperatorRaftConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
|
|
|
out, ok := obj.(structs.RaftConfigurationResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected: %T", obj)
|
|
|
|
}
|
2016-08-30 18:30:56 +00:00
|
|
|
if len(out.Servers) != 1 ||
|
|
|
|
!out.Servers[0].Leader ||
|
|
|
|
!out.Servers[0].Voter {
|
2016-08-30 02:09:57 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-23 02:26:43 +00:00
|
|
|
func TestOperator_RaftPeer(t *testing.T) {
|
2016-08-30 02:09:57 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", "/v1/operator/raft/peer?address=nope", body)
|
2016-08-30 02:09:57 +00:00
|
|
|
// If we get this error, it proves we sent the address all the
|
|
|
|
// way through.
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.OperatorRaftPeer(resp, req)
|
2016-08-30 02:09:57 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(),
|
|
|
|
"address \"nope\" was not found in the Raft configuration") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2017-03-30 01:09:41 +00:00
|
|
|
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", "/v1/operator/raft/peer?id=nope", body)
|
2017-03-30 01:09:41 +00:00
|
|
|
// If we get this error, it proves we sent the ID all the
|
|
|
|
// way through.
|
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.OperatorRaftPeer(resp, req)
|
2017-03-30 01:09:41 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(),
|
|
|
|
"id \"nope\" was not found in the Raft configuration") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2016-08-30 02:09:57 +00:00
|
|
|
}
|
2016-11-15 02:54:37 +00:00
|
|
|
|
|
|
|
func TestOperator_KeyringInstall(t *testing.T) {
|
|
|
|
oldKey := "H3/9gBxcKKRf45CaI2DlRg=="
|
|
|
|
newKey := "z90lFx3sZZLtTOkutXcwYg=="
|
|
|
|
configFunc := func(c *Config) {
|
|
|
|
c.EncryptKey = oldKey
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBufferString(fmt.Sprintf("{\"Key\":\"%s\"}", newKey))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("POST", "/v1/operator/keyring", body)
|
2016-11-15 02:54:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.OperatorKeyringEndpoint(resp, req)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:42:41 +00:00
|
|
|
listResponse, err := srv.agent.ListKeys("", 0)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-11-22 23:24:40 +00:00
|
|
|
if len(listResponse.Responses) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(listResponse.Responses))
|
|
|
|
}
|
2016-11-15 02:54:37 +00:00
|
|
|
|
|
|
|
for _, response := range listResponse.Responses {
|
|
|
|
count, ok := response.Keys[newKey]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad: %v", response.Keys)
|
|
|
|
}
|
|
|
|
if count != response.NumNodes {
|
|
|
|
t.Fatalf("bad: %d, %d", count, response.NumNodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, configFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_KeyringList(t *testing.T) {
|
|
|
|
key := "H3/9gBxcKKRf45CaI2DlRg=="
|
|
|
|
configFunc := func(c *Config) {
|
|
|
|
c.EncryptKey = key
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/keyring", nil)
|
2016-11-15 02:54:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2016-11-22 23:24:40 +00:00
|
|
|
r, err := srv.OperatorKeyringEndpoint(resp, req)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
responses, ok := r.([]*structs.KeyringResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("err: %v", !ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we get both a LAN and WAN response, and that they both only
|
|
|
|
// contain the original key
|
|
|
|
if len(responses) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(responses))
|
|
|
|
}
|
2016-11-22 23:24:40 +00:00
|
|
|
|
|
|
|
// WAN
|
|
|
|
if len(responses[0].Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %d", len(responses[0].Keys))
|
|
|
|
}
|
|
|
|
if !responses[0].WAN {
|
|
|
|
t.Fatalf("bad: %v", responses[0].WAN)
|
|
|
|
}
|
|
|
|
if _, ok := responses[0].Keys[key]; !ok {
|
|
|
|
t.Fatalf("bad: %v", ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LAN
|
|
|
|
if len(responses[1].Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %d", len(responses[1].Keys))
|
|
|
|
}
|
|
|
|
if responses[1].WAN {
|
|
|
|
t.Fatalf("bad: %v", responses[1].WAN)
|
|
|
|
}
|
|
|
|
if _, ok := responses[1].Keys[key]; !ok {
|
|
|
|
t.Fatalf("bad: %v", ok)
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
|
|
|
}, configFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_KeyringRemove(t *testing.T) {
|
|
|
|
key := "H3/9gBxcKKRf45CaI2DlRg=="
|
|
|
|
tempKey := "z90lFx3sZZLtTOkutXcwYg=="
|
|
|
|
configFunc := func(c *Config) {
|
|
|
|
c.EncryptKey = key
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
2017-02-02 02:42:41 +00:00
|
|
|
_, err := srv.agent.InstallKey(tempKey, "", 0)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the temp key is installed
|
2017-02-02 02:42:41 +00:00
|
|
|
list, err := srv.agent.ListKeys("", 0)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
responses := list.Responses
|
|
|
|
if len(responses) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(responses))
|
|
|
|
}
|
|
|
|
for _, response := range responses {
|
|
|
|
if len(response.Keys) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(response.Keys))
|
|
|
|
}
|
|
|
|
if _, ok := response.Keys[tempKey]; !ok {
|
|
|
|
t.Fatalf("bad: %v", ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body := bytes.NewBufferString(fmt.Sprintf("{\"Key\":\"%s\"}", tempKey))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", "/v1/operator/keyring", body)
|
2016-11-15 02:54:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
if _, err := srv.OperatorKeyringEndpoint(resp, req); err != nil {
|
2016-11-15 02:54:37 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the temp key has been removed
|
2017-02-02 02:42:41 +00:00
|
|
|
list, err = srv.agent.ListKeys("", 0)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
responses = list.Responses
|
|
|
|
if len(responses) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(responses))
|
|
|
|
}
|
|
|
|
for _, response := range responses {
|
|
|
|
if len(response.Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %d", len(response.Keys))
|
|
|
|
}
|
|
|
|
if _, ok := response.Keys[tempKey]; ok {
|
|
|
|
t.Fatalf("bad: %v", ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, configFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_KeyringUse(t *testing.T) {
|
|
|
|
oldKey := "H3/9gBxcKKRf45CaI2DlRg=="
|
|
|
|
newKey := "z90lFx3sZZLtTOkutXcwYg=="
|
|
|
|
configFunc := func(c *Config) {
|
|
|
|
c.EncryptKey = oldKey
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
2017-02-02 02:42:41 +00:00
|
|
|
if _, err := srv.agent.InstallKey(newKey, "", 0); err != nil {
|
2016-11-15 02:54:37 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body := bytes.NewBufferString(fmt.Sprintf("{\"Key\":\"%s\"}", newKey))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/keyring", body)
|
2016-11-15 02:54:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.OperatorKeyringEndpoint(resp, req)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:42:41 +00:00
|
|
|
if _, err := srv.agent.RemoveKey(oldKey, "", 0); err != nil {
|
2016-11-15 02:54:37 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure only the new key remains
|
2017-02-02 02:42:41 +00:00
|
|
|
list, err := srv.agent.ListKeys("", 0)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
responses := list.Responses
|
|
|
|
if len(responses) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(responses))
|
|
|
|
}
|
|
|
|
for _, response := range responses {
|
|
|
|
if len(response.Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %d", len(response.Keys))
|
|
|
|
}
|
|
|
|
if _, ok := response.Keys[newKey]; !ok {
|
|
|
|
t.Fatalf("bad: %v", ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, configFunc)
|
|
|
|
}
|
2017-02-02 02:42:41 +00:00
|
|
|
|
|
|
|
func TestOperator_Keyring_InvalidRelayFactor(t *testing.T) {
|
|
|
|
key := "H3/9gBxcKKRf45CaI2DlRg=="
|
|
|
|
configFunc := func(c *Config) {
|
|
|
|
c.EncryptKey = key
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
|
|
|
cases := map[string]string{
|
|
|
|
"999": "Relay factor must be in range",
|
|
|
|
"asdf": "Error parsing relay factor",
|
|
|
|
}
|
|
|
|
for relayFactor, errString := range cases {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/keyring?relay-factor="+relayFactor, nil)
|
2017-02-02 02:42:41 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
_, err := srv.OperatorKeyringEndpoint(resp, req)
|
2017-02-02 02:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
body := resp.Body.String()
|
|
|
|
if !strings.Contains(body, errString) {
|
|
|
|
t.Fatalf("bad: %v", body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, configFunc)
|
|
|
|
}
|
2017-02-24 04:32:13 +00:00
|
|
|
|
|
|
|
func TestOperator_AutopilotGetConfiguration(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/configuration", body)
|
2017-02-24 04:32:13 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.OperatorAutopilotConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
2017-03-10 00:43:07 +00:00
|
|
|
out, ok := obj.(api.AutopilotConfiguration)
|
2017-02-24 04:32:13 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected: %T", obj)
|
|
|
|
}
|
2017-02-28 22:23:14 +00:00
|
|
|
if !out.CleanupDeadServers {
|
2017-02-24 04:32:13 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_AutopilotSetConfiguration(t *testing.T) {
|
2017-02-24 21:08:49 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
2017-02-28 22:23:14 +00:00
|
|
|
body := bytes.NewBuffer([]byte(`{"CleanupDeadServers": false}`))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/autopilot/configuration", body)
|
2017-02-24 21:08:49 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
if _, err := srv.OperatorAutopilotConfiguration(resp, req); err != nil {
|
2017-02-24 21:08:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply structs.AutopilotConfig
|
|
|
|
if err := srv.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-02-28 22:23:14 +00:00
|
|
|
if reply.CleanupDeadServers {
|
2017-02-24 21:08:49 +00:00
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_AutopilotCASConfiguration(t *testing.T) {
|
2017-02-24 04:32:13 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
2017-02-28 22:23:14 +00:00
|
|
|
body := bytes.NewBuffer([]byte(`{"CleanupDeadServers": false}`))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/autopilot/configuration", body)
|
2017-02-24 04:32:13 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-09 11:38:05 +00:00
|
|
|
if _, err := srv.OperatorAutopilotConfiguration(resp, req); err != nil {
|
2017-02-24 04:32:13 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply structs.AutopilotConfig
|
|
|
|
if err := srv.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:23:14 +00:00
|
|
|
if reply.CleanupDeadServers {
|
2017-02-24 04:32:13 +00:00
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
2017-02-24 21:08:49 +00:00
|
|
|
|
|
|
|
// Create a CAS request, bad index
|
|
|
|
{
|
2017-02-28 22:23:14 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(`{"CleanupDeadServers": true}`))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/autopilot/configuration?cas=%d", reply.ModifyIndex-1), buf)
|
2017-02-24 21:08:49 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.OperatorAutopilotConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); res {
|
|
|
|
t.Fatalf("should NOT work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CAS request, good index
|
|
|
|
{
|
2017-02-28 22:23:14 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(`{"CleanupDeadServers": true}`))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/autopilot/configuration?cas=%d", reply.ModifyIndex), buf)
|
2017-02-24 21:08:49 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.OperatorAutopilotConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the update
|
|
|
|
if err := srv.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-02-28 22:23:14 +00:00
|
|
|
if !reply.CleanupDeadServers {
|
2017-02-24 21:08:49 +00:00
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
2017-02-24 04:32:13 +00:00
|
|
|
})
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
2017-03-01 22:04:40 +00:00
|
|
|
|
2017-03-23 02:26:43 +00:00
|
|
|
func TestOperator_ServerHealth(t *testing.T) {
|
2017-03-10 00:43:07 +00:00
|
|
|
cb := func(c *Config) {
|
|
|
|
c.RaftProtocol = 3
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
2017-03-01 22:04:40 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/health", body)
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-03-01 22:04:40 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.OperatorServerHealth(resp, req)
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad code: %d", resp.Code)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
2017-03-10 00:43:07 +00:00
|
|
|
out, ok := obj.(*api.OperatorHealthReply)
|
2017-03-01 22:04:40 +00:00
|
|
|
if !ok {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("unexpected: %T", obj)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
|
|
|
if len(out.Servers) != 1 ||
|
|
|
|
!out.Servers[0].Healthy ||
|
|
|
|
out.Servers[0].Name != srv.agent.config.NodeName ||
|
2017-03-10 00:43:07 +00:00
|
|
|
out.Servers[0].SerfStatus != "alive" ||
|
2017-03-01 22:04:40 +00:00
|
|
|
out.FailureTolerance != 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", out)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-03-10 00:43:07 +00:00
|
|
|
}, cb)
|
|
|
|
}
|
|
|
|
|
2017-03-23 02:26:43 +00:00
|
|
|
func TestOperator_ServerHealth_Unhealthy(t *testing.T) {
|
2017-03-10 00:43:07 +00:00
|
|
|
cb := func(c *Config) {
|
|
|
|
c.RaftProtocol = 3
|
2017-05-04 22:05:23 +00:00
|
|
|
|
|
|
|
threshold := time.Duration(-1)
|
2017-03-10 00:43:07 +00:00
|
|
|
c.Autopilot.LastContactThreshold = &threshold
|
|
|
|
}
|
|
|
|
httpTestWithConfig(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/health", body)
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-03-10 00:43:07 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.OperatorServerHealth(resp, req)
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
if resp.Code != 429 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad code: %d", resp.Code)
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
out, ok := obj.(*api.OperatorHealthReply)
|
|
|
|
if !ok {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("unexpected: %T", obj)
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
if len(out.Servers) != 1 ||
|
|
|
|
out.Healthy ||
|
|
|
|
out.Servers[0].Name != srv.agent.config.NodeName {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %#v", out.Servers)
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-03-10 00:43:07 +00:00
|
|
|
}, cb)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|