2013-12-23 21:52:10 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2013-12-24 00:20:51 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-05-21 19:31:22 +00:00
|
|
|
"fmt"
|
2013-12-24 00:20:51 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-01-16 17:58:37 +00:00
|
|
|
"net"
|
2014-02-05 22:47:42 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-04-17 18:38:14 +00:00
|
|
|
"os"
|
2014-04-23 19:57:06 +00:00
|
|
|
"path/filepath"
|
2015-01-16 17:58:37 +00:00
|
|
|
"runtime"
|
2014-04-21 20:11:05 +00:00
|
|
|
"strconv"
|
2013-12-23 21:52:10 +00:00
|
|
|
"testing"
|
2014-02-05 22:47:42 +00:00
|
|
|
"time"
|
2014-09-02 19:47:40 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2013-12-23 21:52:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
|
2015-01-16 17:58:37 +00:00
|
|
|
return makeHTTPServerWithConfig(t, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPServer) {
|
2013-12-23 21:52:10 +00:00
|
|
|
conf := nextConfig()
|
2015-01-16 17:58:37 +00:00
|
|
|
if cb != nil {
|
|
|
|
cb(conf)
|
|
|
|
}
|
|
|
|
|
2013-12-23 21:52:10 +00:00
|
|
|
dir, agent := makeAgent(t, conf)
|
2014-04-23 19:57:06 +00:00
|
|
|
uiDir := filepath.Join(dir, "ui")
|
2014-04-23 20:10:18 +00:00
|
|
|
if err := os.Mkdir(uiDir, 755); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-11-19 21:29:15 +00:00
|
|
|
conf.UiDir = uiDir
|
2015-02-05 02:45:08 +00:00
|
|
|
servers, err := NewHTTPServers(agent, conf, nil, agent.logOutput)
|
2013-12-23 21:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-11-19 19:51:25 +00:00
|
|
|
if len(servers) == 0 {
|
|
|
|
t.Fatalf(fmt.Sprintf("Failed to make HTTP server"))
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
return dir, servers[0]
|
2013-12-23 21:52:10 +00:00
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
|
|
|
func encodeReq(obj interface{}) io.ReadCloser {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.Encode(obj)
|
|
|
|
return ioutil.NopCloser(buf)
|
|
|
|
}
|
2014-02-05 22:47:42 +00:00
|
|
|
|
2015-01-16 17:58:37 +00:00
|
|
|
func TestHTTPServer_UnixSocket(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
tempDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
socket := filepath.Join(tempDir, "test.sock")
|
|
|
|
|
|
|
|
dir, srv := makeHTTPServerWithConfig(t, func(c *Config) {
|
|
|
|
c.Addresses.HTTP = "unix://" + socket
|
2015-01-20 22:32:15 +00:00
|
|
|
|
|
|
|
// Only testing mode, since uid/gid might not be settable
|
|
|
|
// from test environment.
|
2015-01-21 04:30:59 +00:00
|
|
|
c.UnixSockets = UnixSocketConfig{}
|
|
|
|
c.UnixSockets.Perms = "0777"
|
2015-01-16 17:58:37 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
// Ensure the socket was created
|
|
|
|
if _, err := os.Stat(socket); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-20 22:32:15 +00:00
|
|
|
// Ensure the mode was set properly
|
|
|
|
fi, err := os.Stat(socket)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if fi.Mode().String() != "Srwxrwxrwx" {
|
|
|
|
t.Fatalf("bad permissions: %s", fi.Mode())
|
|
|
|
}
|
|
|
|
|
2015-01-16 17:58:37 +00:00
|
|
|
// Ensure we can get a response from the socket.
|
|
|
|
path, _ := unixSocketAddr(srv.agent.config.Addresses.HTTP)
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: func(_, _ string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", path)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// This URL doesn't look like it makes sense, but the scheme (http://) and
|
|
|
|
// the host (127.0.0.1) are required by the HTTP client library. In reality
|
|
|
|
// this will just use the custom dialer and talk to the socket.
|
|
|
|
resp, err := client.Get("http://127.0.0.1/v1/agent/self")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if body, err := ioutil.ReadAll(resp.Body); err != nil || len(body) == 0 {
|
|
|
|
t.Fatalf("bad: %s %v", body, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 20:39:15 +00:00
|
|
|
func TestHTTPServer_UnixSocket_FileExists(t *testing.T) {
|
2015-01-16 18:37:13 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
tempDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
socket := filepath.Join(tempDir, "test.sock")
|
|
|
|
|
|
|
|
// Create a regular file at the socket path
|
|
|
|
if err := ioutil.WriteFile(socket, []byte("hello world"), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(socket)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !fi.Mode().IsRegular() {
|
|
|
|
t.Fatalf("not a regular file: %s", socket)
|
|
|
|
}
|
|
|
|
|
2015-01-16 20:39:15 +00:00
|
|
|
conf := nextConfig()
|
|
|
|
conf.Addresses.HTTP = "unix://" + socket
|
|
|
|
|
|
|
|
dir, agent := makeAgent(t, conf)
|
2015-01-16 18:37:13 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2015-01-16 20:39:15 +00:00
|
|
|
// Try to start the server with the same path anyways.
|
2015-02-05 02:45:08 +00:00
|
|
|
if _, err := NewHTTPServers(agent, conf, nil, agent.logOutput); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the file was replaced by the socket
|
|
|
|
fi, err = os.Stat(socket)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if fi.Mode()&os.ModeSocket == 0 {
|
|
|
|
t.Fatalf("expected socket to replace file")
|
2015-01-16 18:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:47:42 +00:00
|
|
|
func TestSetIndex(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
setIndex(resp, 1000)
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
|
|
if header != "1000" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
2014-10-14 00:53:54 +00:00
|
|
|
setIndex(resp, 2000)
|
|
|
|
if v := resp.Header()["X-Consul-Index"]; len(v) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2014-02-05 22:47:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 20:19:18 +00:00
|
|
|
func TestSetKnownLeader(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
setKnownLeader(resp, true)
|
|
|
|
header := resp.Header().Get("X-Consul-KnownLeader")
|
|
|
|
if header != "true" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
setKnownLeader(resp, false)
|
|
|
|
header = resp.Header().Get("X-Consul-KnownLeader")
|
|
|
|
if header != "false" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLastContact(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
setLastContact(resp, 123456*time.Microsecond)
|
|
|
|
header := resp.Header().Get("X-Consul-LastContact")
|
|
|
|
if header != "123" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetMeta(t *testing.T) {
|
|
|
|
meta := structs.QueryMeta{
|
|
|
|
Index: 1000,
|
|
|
|
KnownLeader: true,
|
|
|
|
LastContact: 123456 * time.Microsecond,
|
|
|
|
}
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
setMeta(resp, &meta)
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
|
|
if header != "1000" {
|
|
|
|
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 != "123" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 04:53:19 +00:00
|
|
|
func TestHTTPAPIResponseHeaders(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
srv.agent.config.HTTPAPIResponseHeaders = map[string]string{
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"X-XSS-Protection": "1; mode=block",
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
|
|
|
|
srv.wrap(handler)(resp, req)
|
|
|
|
|
|
|
|
origin := resp.Header().Get("Access-Control-Allow-Origin")
|
|
|
|
if origin != "*" {
|
|
|
|
t.Fatalf("bad Access-Control-Allow-Origin: expected %q, got %q", "*", origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
xss := resp.Header().Get("X-XSS-Protection")
|
|
|
|
if xss != "1; mode=block" {
|
|
|
|
t.Fatalf("bad X-XSS-Protection header: expected %q, got %q", "1; mode=block", xss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 18:38:14 +00:00
|
|
|
func TestContentTypeIsJSON(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// stub out a DirEntry so that it will be encoded as JSON
|
|
|
|
return &structs.DirEntry{Key: "key"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/key", nil)
|
|
|
|
srv.wrap(handler)(resp, req)
|
|
|
|
|
|
|
|
contentType := resp.Header().Get("Content-Type")
|
|
|
|
|
|
|
|
if contentType != "application/json" {
|
|
|
|
t.Fatalf("Content-Type header was not 'application/json'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 22:27:10 +00:00
|
|
|
func TestPrettyPrint(t *testing.T) {
|
2015-01-02 08:14:44 +00:00
|
|
|
testPrettyPrint("pretty=1", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrettyPrintBare(t *testing.T) {
|
|
|
|
testPrettyPrint("pretty", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPrettyPrint(pretty string, t *testing.T) {
|
2015-01-01 22:27:10 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
|
|
|
r := &structs.DirEntry{Key: "key"}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-01-02 08:14:44 +00:00
|
|
|
urlStr := "/v1/kv/key?" + pretty
|
|
|
|
req, _ := http.NewRequest("GET", urlStr, nil)
|
2015-01-01 22:27:10 +00:00
|
|
|
srv.wrap(handler)(resp, req)
|
|
|
|
|
|
|
|
expected, _ := json.MarshalIndent(r, "", " ")
|
|
|
|
actual, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, actual) {
|
|
|
|
t.Fatalf("bad: %q", string(actual))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:47:42 +00:00
|
|
|
func TestParseWait(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
2014-04-21 20:11:05 +00:00
|
|
|
var b structs.QueryOptions
|
2014-02-05 22:47:42 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?wait=60s&index=1000", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d := parseWait(resp, req, &b); d {
|
|
|
|
t.Fatalf("unexpected done")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.MinQueryIndex != 1000 {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
if b.MaxQueryTime != 60*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseWait_InvalidTime(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
2014-04-21 20:11:05 +00:00
|
|
|
var b structs.QueryOptions
|
2014-02-05 22:47:42 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?wait=60foo&index=1000", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d := parseWait(resp, req, &b); !d {
|
|
|
|
t.Fatalf("expected done")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("bad code: %v", resp.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseWait_InvalidIndex(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
2014-04-21 20:11:05 +00:00
|
|
|
var b structs.QueryOptions
|
2014-02-05 22:47:42 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?wait=60s&index=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d := parseWait(resp, req, &b); !d {
|
|
|
|
t.Fatalf("expected done")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("bad code: %v", resp.Code)
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 20:11:05 +00:00
|
|
|
|
2014-04-21 20:19:18 +00:00
|
|
|
func TestParseConsistency(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
var b structs.QueryOptions
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?stale", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d := parseConsistency(resp, req, &b); d {
|
|
|
|
t.Fatalf("unexpected done")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.AllowStale {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
if b.RequireConsistent {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
b = structs.QueryOptions{}
|
|
|
|
req, err = http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?consistent", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d := parseConsistency(resp, req, &b); d {
|
|
|
|
t.Fatalf("unexpected done")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.AllowStale {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
if !b.RequireConsistent {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConsistency_Invalid(t *testing.T) {
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
var b structs.QueryOptions
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?stale&consistent", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d := parseConsistency(resp, req, &b); !d {
|
|
|
|
t.Fatalf("expected done")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Code != 400 {
|
|
|
|
t.Fatalf("bad code: %v", resp.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:38:01 +00:00
|
|
|
// Test ACL token is resolved in correct order
|
|
|
|
func TestACLResolution(t *testing.T) {
|
|
|
|
var token string
|
|
|
|
// Request without token
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request with explicit token
|
|
|
|
reqToken, err := http.NewRequest("GET",
|
|
|
|
"/v1/catalog/nodes?token=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
// Check when no token is set
|
|
|
|
srv.agent.config.ACLToken = ""
|
|
|
|
srv.parseToken(req, &token)
|
|
|
|
if token != "" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check when ACLToken set
|
|
|
|
srv.agent.config.ACLToken = "agent"
|
|
|
|
srv.parseToken(req, &token)
|
|
|
|
if token != "agent" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check when AtlasACLToken set, wrong server
|
|
|
|
srv.agent.config.AtlasACLToken = "atlas"
|
|
|
|
srv.parseToken(req, &token)
|
|
|
|
if token != "agent" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check when AtlasACLToken set, correct server
|
|
|
|
srv.addr = scadaHTTPAddr
|
|
|
|
srv.parseToken(req, &token)
|
|
|
|
if token != "atlas" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check when AtlasACLToken not, correct server
|
|
|
|
srv.agent.config.AtlasACLToken = ""
|
|
|
|
srv.parseToken(req, &token)
|
|
|
|
if token != "agent" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit token has highest precedence
|
|
|
|
srv.parseToken(reqToken, &token)
|
|
|
|
if token != "foo" {
|
|
|
|
t.Fatalf("bad: %s", token)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
// assertIndex tests that X-Consul-Index is set and non-zero
|
|
|
|
func assertIndex(t *testing.T, resp *httptest.ResponseRecorder) {
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
|
|
if header == "" || header == "0" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:31:22 +00:00
|
|
|
// checkIndex is like assertIndex but returns an error
|
|
|
|
func checkIndex(resp *httptest.ResponseRecorder) error {
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
|
|
if header == "" || header == "0" {
|
|
|
|
return fmt.Errorf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
// getIndex parses X-Consul-Index
|
|
|
|
func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 {
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
|
|
if header == "" {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
val, err := strconv.Atoi(header)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: %v", header)
|
|
|
|
}
|
|
|
|
return uint64(val)
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
|
|
|
|
func httpTest(t *testing.T, f func(srv *HTTPServer)) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
|
|
|
|
f(srv)
|
|
|
|
}
|