c7cc62ab5a
* agent: consolidate http method not allowed checks This patch uses the error handling of the http handlers to handle HTTP method not allowed errors across all available endpoints. It also adds a test for testing whether the endpoints respond with the correct status code. * agent: do not panic on metrics tests * agent: drop other tests for MethodNotAllowed * agent: align /agent/join with reality /agent/join uses PUT instead of GET as documented. * agent: align /agent/check/{fail,warn,pass} with reality /agent/check/{fail,warn,pass} uses PUT instead of GET as documented. * fix some tests * Drop more tests for method not allowed * Align TestAgent_RegisterService_InvalidAddress with reality * Changes API client join to use PUT instead of GET. * Fixes agent endpoint verbs and removes obsolete tests. * Updates the change log.
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
)
|
|
|
|
func TestSnapshot(t *testing.T) {
|
|
t.Parallel()
|
|
var snap io.Reader
|
|
t.Run("", func(t *testing.T) {
|
|
a := NewTestAgent(t.Name(), "")
|
|
defer a.Shutdown()
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
req, _ := http.NewRequest("GET", "/v1/snapshot?token=root", body)
|
|
resp := httptest.NewRecorder()
|
|
if _, err := a.srv.Snapshot(resp, req); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
snap = resp.Body
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
if header == "" {
|
|
t.Fatalf("bad: %v", header)
|
|
}
|
|
header = resp.Header().Get("X-Consul-KnownLeader")
|
|
if header != "true" {
|
|
t.Fatalf("bad: %v", header)
|
|
}
|
|
header = resp.Header().Get("X-Consul-LastContact")
|
|
if header != "0" {
|
|
t.Fatalf("bad: %v", header)
|
|
}
|
|
})
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
a := NewTestAgent(t.Name(), "")
|
|
defer a.Shutdown()
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/snapshot?token=root", snap)
|
|
resp := httptest.NewRecorder()
|
|
if _, err := a.srv.Snapshot(resp, req); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSnapshot_Options(t *testing.T) {
|
|
t.Parallel()
|
|
for _, method := range []string{"GET", "PUT"} {
|
|
t.Run(method, func(t *testing.T) {
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
defer a.Shutdown()
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
req, _ := http.NewRequest(method, "/v1/snapshot?token=anonymous", body)
|
|
resp := httptest.NewRecorder()
|
|
_, err := a.srv.Snapshot(resp, req)
|
|
if !acl.IsErrPermissionDenied(err) {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run(method, func(t *testing.T) {
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
defer a.Shutdown()
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
req, _ := http.NewRequest(method, "/v1/snapshot?dc=nope", body)
|
|
resp := httptest.NewRecorder()
|
|
_, err := a.srv.Snapshot(resp, req)
|
|
if err == nil || !strings.Contains(err.Error(), "No path to datacenter") {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run(method, func(t *testing.T) {
|
|
a := NewTestAgent(t.Name(), TestACLConfig())
|
|
defer a.Shutdown()
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
req, _ := http.NewRequest(method, "/v1/snapshot?token=root&stale", body)
|
|
resp := httptest.NewRecorder()
|
|
_, err := a.srv.Snapshot(resp, req)
|
|
if method == "GET" {
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
} else {
|
|
if err == nil || !strings.Contains(err.Error(), "stale not allowed") {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|