mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
5.2 KiB
243 lines
5.2 KiB
11 years ago
|
package agent
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
11 years ago
|
"fmt"
|
||
11 years ago
|
"io"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
11 years ago
|
"reflect"
|
||
11 years ago
|
"testing"
|
||
9 years ago
|
|
||
8 years ago
|
"github.com/hashicorp/consul/agent/consul/structs"
|
||
8 years ago
|
"github.com/hashicorp/consul/api"
|
||
8 years ago
|
"github.com/hashicorp/consul/testutil"
|
||
9 years ago
|
"github.com/hashicorp/go-cleanhttp"
|
||
11 years ago
|
)
|
||
|
|
||
|
func TestUiIndex(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
9 years ago
|
// Make a test dir to serve UI files
|
||
8 years ago
|
uiDir := testutil.TempDir(t, "consul")
|
||
9 years ago
|
defer os.RemoveAll(uiDir)
|
||
|
|
||
|
// Make the server
|
||
8 years ago
|
cfg := TestConfig()
|
||
|
cfg.UIDir = uiDir
|
||
|
a := NewTestAgent(t.Name(), cfg)
|
||
8 years ago
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
|
// Create file
|
||
8 years ago
|
path := filepath.Join(a.Config.UIDir, "my-file")
|
||
11 years ago
|
if err := ioutil.WriteFile(path, []byte("test"), 777); err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
|
||
|
// Register node
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/ui/my-file", nil)
|
||
11 years ago
|
req.URL.Scheme = "http"
|
||
8 years ago
|
req.URL.Host = a.srv.Addr
|
||
11 years ago
|
|
||
|
// Make the request
|
||
9 years ago
|
client := cleanhttp.DefaultClient()
|
||
9 years ago
|
resp, err := client.Do(req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
// Verify the response
|
||
11 years ago
|
if resp.StatusCode != 200 {
|
||
11 years ago
|
t.Fatalf("bad: %v", resp)
|
||
|
}
|
||
|
|
||
|
// Verify the body
|
||
|
out := bytes.NewBuffer(nil)
|
||
|
io.Copy(out, resp.Body)
|
||
|
if string(out.Bytes()) != "test" {
|
||
|
t.Fatalf("bad: %s", out.Bytes())
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
func TestUiNodes(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
9 years ago
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
|
Node: "test",
|
||
|
Address: "127.0.0.1",
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/internal/ui/nodes/dc1", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.UINodes(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
|
||
9 years ago
|
// Should be 2 nodes, and all the empty lists should be non-nil
|
||
11 years ago
|
nodes := obj.(structs.NodeDump)
|
||
9 years ago
|
if len(nodes) != 2 ||
|
||
8 years ago
|
nodes[0].Node != a.Config.NodeName ||
|
||
9 years ago
|
nodes[0].Services == nil || len(nodes[0].Services) != 1 ||
|
||
|
nodes[0].Checks == nil || len(nodes[0].Checks) != 1 ||
|
||
|
nodes[1].Node != "test" ||
|
||
|
nodes[1].Services == nil || len(nodes[1].Services) != 0 ||
|
||
|
nodes[1].Checks == nil || len(nodes[1].Checks) != 0 {
|
||
11 years ago
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
func TestUiNodeInfo(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/internal/ui/node/%s", a.Config.NodeName), nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.UINodeInfo(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
assertIndex(t, resp)
|
||
|
|
||
|
// Should be 1 node for the server
|
||
|
node := obj.(*structs.NodeInfo)
|
||
8 years ago
|
if node.Node != a.Config.NodeName {
|
||
11 years ago
|
t.Fatalf("bad: %v", node)
|
||
|
}
|
||
9 years ago
|
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
|
Node: "test",
|
||
|
Address: "127.0.0.1",
|
||
|
}
|
||
|
|
||
|
var out struct{}
|
||
8 years ago
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
||
9 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/internal/ui/node/test", nil)
|
||
9 years ago
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, err = a.srv.UINodeInfo(resp, req)
|
||
9 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
assertIndex(t, resp)
|
||
|
|
||
|
// Should be non-nil empty lists for services and checks
|
||
|
node = obj.(*structs.NodeInfo)
|
||
|
if node.Node != "test" ||
|
||
|
node.Services == nil || len(node.Services) != 0 ||
|
||
|
node.Checks == nil || len(node.Checks) != 0 {
|
||
|
t.Fatalf("bad: %v", node)
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
func TestSummarizeServices(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
11 years ago
|
dump := structs.NodeDump{
|
||
|
&structs.NodeInfo{
|
||
|
Node: "foo",
|
||
|
Address: "127.0.0.1",
|
||
|
Services: []*structs.NodeService{
|
||
|
&structs.NodeService{
|
||
|
Service: "api",
|
||
|
},
|
||
|
&structs.NodeService{
|
||
|
Service: "web",
|
||
|
},
|
||
|
},
|
||
|
Checks: []*structs.HealthCheck{
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthPassing,
|
||
11 years ago
|
ServiceName: "",
|
||
|
},
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthPassing,
|
||
11 years ago
|
ServiceName: "web",
|
||
|
},
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthWarning,
|
||
11 years ago
|
ServiceName: "api",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
&structs.NodeInfo{
|
||
|
Node: "bar",
|
||
|
Address: "127.0.0.2",
|
||
|
Services: []*structs.NodeService{
|
||
|
&structs.NodeService{
|
||
|
Service: "web",
|
||
|
},
|
||
|
},
|
||
|
Checks: []*structs.HealthCheck{
|
||
|
&structs.HealthCheck{
|
||
8 years ago
|
Status: api.HealthCritical,
|
||
11 years ago
|
ServiceName: "web",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
&structs.NodeInfo{
|
||
|
Node: "zip",
|
||
|
Address: "127.0.0.3",
|
||
|
Services: []*structs.NodeService{
|
||
|
&structs.NodeService{
|
||
|
Service: "cache",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
summary := summarizeServices(dump)
|
||
|
if len(summary) != 3 {
|
||
|
t.Fatalf("bad: %v", summary)
|
||
|
}
|
||
|
|
||
|
expectAPI := &ServiceSummary{
|
||
|
Name: "api",
|
||
|
Nodes: []string{"foo"},
|
||
11 years ago
|
ChecksPassing: 1,
|
||
11 years ago
|
ChecksWarning: 1,
|
||
|
ChecksCritical: 0,
|
||
|
}
|
||
|
if !reflect.DeepEqual(summary[0], expectAPI) {
|
||
|
t.Fatalf("bad: %v", summary[0])
|
||
|
}
|
||
|
|
||
|
expectCache := &ServiceSummary{
|
||
|
Name: "cache",
|
||
|
Nodes: []string{"zip"},
|
||
|
ChecksPassing: 0,
|
||
|
ChecksWarning: 0,
|
||
|
ChecksCritical: 0,
|
||
|
}
|
||
|
if !reflect.DeepEqual(summary[1], expectCache) {
|
||
|
t.Fatalf("bad: %v", summary[1])
|
||
|
}
|
||
|
|
||
|
expectWeb := &ServiceSummary{
|
||
|
Name: "web",
|
||
|
Nodes: []string{"bar", "foo"},
|
||
11 years ago
|
ChecksPassing: 2,
|
||
11 years ago
|
ChecksWarning: 0,
|
||
|
ChecksCritical: 1,
|
||
|
}
|
||
|
if !reflect.DeepEqual(summary[2], expectWeb) {
|
||
|
t.Fatalf("bad: %v", summary[2])
|
||
|
}
|
||
|
}
|