2013-12-11 22:03:09 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-01-08 18:31:20 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-03-05 22:34:29 +00:00
|
|
|
"os"
|
2014-01-08 20:15:01 +00:00
|
|
|
"reflect"
|
2013-12-12 19:07:14 +00:00
|
|
|
"sort"
|
2013-12-11 22:03:09 +00:00
|
|
|
"testing"
|
2014-05-19 19:50:29 +00:00
|
|
|
"time"
|
2013-12-11 22:03:09 +00:00
|
|
|
)
|
|
|
|
|
2014-03-05 22:34:29 +00:00
|
|
|
func testStateStore() (*StateStore, error) {
|
|
|
|
return NewStateStore(os.Stderr)
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:03:09 +00:00
|
|
|
func TestEnsureNode(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-11 22:03:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-11 22:03:09 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, found, addr := store.GetNode("foo")
|
|
|
|
if idx != 3 || !found || addr != "127.0.0.1" {
|
|
|
|
t.Fatalf("Bad: %v %v %v", idx, found, addr)
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(4, structs.Node{"foo", "127.0.0.2"}); err != nil {
|
2013-12-11 22:03:09 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, found, addr = store.GetNode("foo")
|
|
|
|
if idx != 4 || !found || addr != "127.0.0.2" {
|
|
|
|
t.Fatalf("Bad: %v %v %v", idx, found, addr)
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNodes(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-11 22:27:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(40, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-11 22:27:27 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(41, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
2013-12-11 22:27:27 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, nodes := store.Nodes()
|
|
|
|
if idx != 41 {
|
|
|
|
t.Fatalf("idx: %v", idx)
|
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
if len(nodes) != 2 {
|
2013-12-11 22:03:09 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
if nodes[1].Node != "foo" && nodes[0].Node != "bar" {
|
2013-12-11 22:27:27 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 20:00:56 +00:00
|
|
|
func BenchmarkGetNodes(b *testing.B) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-02-01 20:00:56 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(100, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-02-01 20:00:56 +00:00
|
|
|
b.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(101, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
2014-02-01 20:00:56 +00:00
|
|
|
b.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
store.Nodes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:27:27 +00:00
|
|
|
func TestEnsureService(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-11 22:27:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(10, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(11, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5001}); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(13, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, services := store.NodeServices("foo")
|
|
|
|
if idx != 13 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2013-12-11 22:27:27 +00:00
|
|
|
|
2014-01-03 01:29:39 +00:00
|
|
|
entry, ok := services.Services["api"]
|
2013-12-11 22:27:27 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing api: %#v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if entry.Tags != nil || entry.Port != 5001 {
|
2013-12-11 22:27:27 +00:00
|
|
|
t.Fatalf("Bad entry: %#v", entry)
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:29:39 +00:00
|
|
|
entry, ok = services.Services["db"]
|
2013-12-11 22:27:27 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing db: %#v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(entry.Tags, "master") || entry.Port != 8000 {
|
2013-12-11 22:27:27 +00:00
|
|
|
t.Fatalf("Bad entry: %#v", entry)
|
|
|
|
}
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
func TestEnsureService_DuplicateNode(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-01-06 22:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(10, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(11, "foo", &structs.NodeService{"api1", "api", nil, 5000}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(12, "foo", &structs.NodeService{"api2", "api", nil, 5001}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(13, "foo", &structs.NodeService{"api3", "api", nil, 5002}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, services := store.NodeServices("foo")
|
|
|
|
if idx != 13 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
|
|
|
|
entry, ok := services.Services["api1"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing api: %#v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if entry.Tags != nil || entry.Port != 5000 {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("Bad entry: %#v", entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, ok = services.Services["api2"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing api: %#v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if entry.Tags != nil || entry.Port != 5001 {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("Bad entry: %#v", entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, ok = services.Services["api3"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing api: %#v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if entry.Tags != nil || entry.Port != 5002 {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("Bad entry: %#v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 23:34:10 +00:00
|
|
|
func TestDeleteNodeService(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(11, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-08 20:15:01 +00:00
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "api",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "api",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.DeleteNodeService(14, "foo", "api"); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, services := store.NodeServices("foo")
|
|
|
|
if idx != 14 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
_, ok := services.Services["api"]
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("has api: %#v", services)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks := store.NodeChecks("foo")
|
|
|
|
if idx != 14 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) != 0 {
|
|
|
|
t.Fatalf("has check: %#v", checks)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNodeService_One(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-01-06 22:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(11, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(13, "foo", &structs.NodeService{"api2", "api", nil, 5001}); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.DeleteNodeService(14, "foo", "api"); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, services := store.NodeServices("foo")
|
|
|
|
if idx != 14 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-03 01:29:39 +00:00
|
|
|
_, ok := services.Services["api"]
|
2013-12-11 23:34:10 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatalf("has api: %#v", services)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
_, ok = services.Services["api2"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("does not have api2: %#v", services)
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNode(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(20, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(21, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-01-08 20:15:01 +00:00
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "api",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(22, check); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.DeleteNode(23, "foo"); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, services := store.NodeServices("foo")
|
|
|
|
if idx != 23 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-03-05 23:03:12 +00:00
|
|
|
if services != nil {
|
|
|
|
t.Fatalf("has services: %#v", services)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks := store.NodeChecks("foo")
|
|
|
|
if idx != 23 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) > 0 {
|
|
|
|
t.Fatalf("has checks: %v", checks)
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, found, _ := store.GetNode("foo")
|
|
|
|
if idx != 23 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
if found {
|
|
|
|
t.Fatalf("found node")
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
|
|
|
func TestGetServices(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-12 19:07:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(30, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(31, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(32, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(33, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(34, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, services := store.Services()
|
|
|
|
if idx != 34 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
|
|
|
tags, ok := services["api"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing api: %#v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if len(tags) != 0 {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("Bad entry: %#v", tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, ok = services["db"]
|
|
|
|
sort.Strings(tags)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing db: %#v", services)
|
|
|
|
}
|
|
|
|
if len(tags) != 2 || tags[0] != "master" || tags[1] != "slave" {
|
|
|
|
t.Fatalf("Bad entry: %#v", tags)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
|
|
|
func TestServiceNodes(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-12 19:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(10, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(11, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(13, "bar", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
2014-01-01 02:31:17 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil {
|
2014-01-01 02:31:17 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(15, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(16, "bar", &structs.NodeService{"db2", "db", []string{"slave"}, 8001}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, nodes := store.ServiceNodes("db")
|
|
|
|
if idx != 16 {
|
|
|
|
t.Fatalf("bad: %v", 16)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
if len(nodes) != 3 {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
if nodes[0].ServiceID != "db" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(nodes[0].ServiceTags, "master") {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].ServicePort != 8000 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes[1].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Address != "127.0.0.2" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
if nodes[1].ServiceID != "db" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(nodes[1].ServiceTags, "slave") {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].ServicePort != 8000 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
|
|
|
|
if nodes[2].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[2].Address != "127.0.0.2" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[2].ServiceID != "db2" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(nodes[2].ServiceTags, "slave") {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[2].ServicePort != 8001 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceTagNodes(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-12 19:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(15, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(16, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(17, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(18, "foo", &structs.NodeService{"db2", "db", []string{"slave"}, 8001}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(19, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, nodes := store.ServiceTagNodes("db", "master")
|
|
|
|
if idx != 19 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(nodes[0].ServiceTags, "master") {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].ServicePort != 8000 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
|
2014-04-03 19:07:17 +00:00
|
|
|
func TestServiceTagNodes_MultipleTags(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(15, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.EnsureNode(16, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.EnsureService(17, "foo", &structs.NodeService{"db", "db", []string{"master", "v2"}, 8000}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.EnsureService(18, "foo", &structs.NodeService{"db2", "db", []string{"slave", "v2", "dev"}, 8001}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.EnsureService(19, "bar", &structs.NodeService{"db", "db", []string{"slave", "v2"}, 8000}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, nodes := store.ServiceTagNodes("db", "master")
|
|
|
|
if idx != 19 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if !strContains(nodes[0].ServiceTags, "master") {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].ServicePort != 8000 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, nodes = store.ServiceTagNodes("db", "v2")
|
|
|
|
if idx != 19 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(nodes) != 3 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, nodes = store.ServiceTagNodes("db", "dev")
|
|
|
|
if idx != 19 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if !strContains(nodes[0].ServiceTags, "dev") {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].ServicePort != 8001 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:14:08 +00:00
|
|
|
func TestStoreSnapshot(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2013-12-12 23:14:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(8, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(9, structs.Node{"bar", "127.0.0.2"}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(10, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(11, "foo", &structs.NodeService{"db2", "db", []string{"slave"}, 8001}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(12, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-01-08 20:19:11 +00:00
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
2014-01-08 20:19:11 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:31:31 +00:00
|
|
|
// Add some KVS entries
|
|
|
|
d := &structs.DirEntry{Key: "/web/a", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(14, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/b", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(15, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-05-15 22:21:34 +00:00
|
|
|
// Add some sessions
|
|
|
|
session := &structs.Session{Node: "foo"}
|
|
|
|
if err := store.SessionCreate(16, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
session = &structs.Session{Node: "bar"}
|
|
|
|
if err := store.SessionCreate(17, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d.Session = session.ID
|
|
|
|
if ok, err := store.KVSLock(18, d); err != nil || !ok {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:14:08 +00:00
|
|
|
// Take a snapshot
|
|
|
|
snap, err := store.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
defer snap.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// Check the last nodes
|
2014-05-15 22:21:34 +00:00
|
|
|
if idx := snap.LastIndex(); idx != 18 {
|
2014-02-05 02:33:15 +00:00
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:14:08 +00:00
|
|
|
// Check snapshot has old values
|
|
|
|
nodes := snap.Nodes()
|
2014-01-08 18:31:20 +00:00
|
|
|
if len(nodes) != 2 {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we get the service entries
|
|
|
|
services := snap.NodeServices("foo")
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(services.Services["db"].Tags, "master") {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(services.Services["db2"].Tags, "slave") {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
|
|
|
|
services = snap.NodeServices("bar")
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(services.Services["db"].Tags, "slave") {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
|
|
|
|
2014-01-08 20:19:11 +00:00
|
|
|
// Ensure we get the checks
|
|
|
|
checks := snap.NodeChecks("foo")
|
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:31:31 +00:00
|
|
|
// Check we have the entries
|
2014-04-01 18:55:25 +00:00
|
|
|
streamCh := make(chan interface{}, 64)
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
var ents []*structs.DirEntry
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
obj := <-streamCh
|
|
|
|
if obj == nil {
|
|
|
|
close(doneCh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ents = append(ents, obj.(*structs.DirEntry))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := snap.KVSDump(streamCh); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
<-doneCh
|
2014-03-31 20:31:31 +00:00
|
|
|
if len(ents) != 2 {
|
|
|
|
t.Fatalf("missing KVS entries!")
|
|
|
|
}
|
|
|
|
|
2014-05-15 22:21:34 +00:00
|
|
|
// Check there are 2 sessions
|
|
|
|
sessions, err := snap.SessionList()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(sessions) != 2 {
|
|
|
|
t.Fatalf("missing sessions")
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:14:08 +00:00
|
|
|
// Make some changes!
|
2014-05-15 22:21:34 +00:00
|
|
|
if err := store.EnsureService(19, "foo", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-15 22:21:34 +00:00
|
|
|
if err := store.EnsureService(20, "bar", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-15 22:21:34 +00:00
|
|
|
if err := store.EnsureNode(21, structs.Node{"baz", "127.0.0.3"}); err != nil {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-08 20:19:11 +00:00
|
|
|
checkAfter := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
ServiceID: "db",
|
|
|
|
}
|
2014-05-15 22:21:34 +00:00
|
|
|
if err := store.EnsureCheck(22, checkAfter); err != nil {
|
2014-05-15 18:51:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-01-08 20:19:11 +00:00
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
|
2014-05-15 22:21:34 +00:00
|
|
|
if err := store.KVSDelete(23, "/web/b"); err != nil {
|
2014-05-15 18:51:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-03-31 20:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 23:14:08 +00:00
|
|
|
// Check snapshot has old values
|
|
|
|
nodes = snap.Nodes()
|
2014-01-08 18:31:20 +00:00
|
|
|
if len(nodes) != 2 {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure old service entries
|
|
|
|
services = snap.NodeServices("foo")
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(services.Services["db"].Tags, "master") {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(services.Services["db2"].Tags, "slave") {
|
2014-01-06 22:18:38 +00:00
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
|
|
|
|
services = snap.NodeServices("bar")
|
2014-04-03 19:03:10 +00:00
|
|
|
if !strContains(services.Services["db"].Tags, "slave") {
|
2013-12-12 23:14:08 +00:00
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
2014-01-08 20:19:11 +00:00
|
|
|
|
|
|
|
checks = snap.NodeChecks("foo")
|
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
2014-03-31 20:31:31 +00:00
|
|
|
|
|
|
|
// Check we have the entries
|
2014-04-01 18:55:25 +00:00
|
|
|
streamCh = make(chan interface{}, 64)
|
|
|
|
doneCh = make(chan struct{})
|
|
|
|
ents = nil
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
obj := <-streamCh
|
|
|
|
if obj == nil {
|
|
|
|
close(doneCh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ents = append(ents, obj.(*structs.DirEntry))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := snap.KVSDump(streamCh); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
<-doneCh
|
2014-03-31 20:31:31 +00:00
|
|
|
if len(ents) != 2 {
|
|
|
|
t.Fatalf("missing KVS entries!")
|
|
|
|
}
|
2014-05-15 22:21:34 +00:00
|
|
|
|
|
|
|
// Check there are 2 sessions
|
|
|
|
sessions, err = snap.SessionList()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(sessions) != 2 {
|
|
|
|
t.Fatalf("missing sessions")
|
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
|
|
|
|
func TestEnsureCheck(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-01-08 20:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db1",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(3, check); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
check2 := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "memory",
|
|
|
|
Name: "memory utilization",
|
|
|
|
Status: structs.HealthWarning,
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(4, check2); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks := store.NodeChecks("foo")
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) != 2 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[1], check2) {
|
|
|
|
t.Fatalf("bad: %v", checks[1])
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks = store.ServiceChecks("db")
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks = store.ChecksInState(structs.HealthPassing)
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks = store.ChecksInState(structs.HealthWarning)
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check2) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
2014-05-21 19:45:12 +00:00
|
|
|
|
|
|
|
idx, checks = store.ChecksInState(structs.HealthAny)
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(checks) != 2 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[1], check2) {
|
|
|
|
t.Fatalf("bad: %v", checks[1])
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNodeCheck(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-01-08 20:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db1",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(3, check); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
check2 := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "memory",
|
|
|
|
Name: "memory utilization",
|
|
|
|
Status: structs.HealthWarning,
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(4, check2); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.DeleteNodeCheck(5, "foo", "db"); err != nil {
|
2014-01-08 20:15:01 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, checks := store.NodeChecks("foo")
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 20:15:01 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(checks[0], check2) {
|
|
|
|
t.Fatalf("bad: %v", checks[0])
|
|
|
|
}
|
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
|
|
|
|
func TestCheckServiceNodes(t *testing.T) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-01-08 22:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db1",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(3, check); err != nil {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
2014-01-15 21:14:28 +00:00
|
|
|
check = &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
2014-01-16 03:24:16 +00:00
|
|
|
CheckID: SerfCheckID,
|
|
|
|
Name: SerfCheckName,
|
2014-01-15 21:14:28 +00:00
|
|
|
Status: structs.HealthPassing,
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(4, check); err != nil {
|
2014-01-15 21:14:28 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, nodes := store.CheckServiceNodes("db")
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes[0].Node.Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
if nodes[0].Service.ID != "db1" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2014-01-15 21:14:28 +00:00
|
|
|
if len(nodes[0].Checks) != 2 {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2014-01-15 21:14:28 +00:00
|
|
|
if nodes[0].Checks[0].CheckID != "db" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2014-01-16 03:24:16 +00:00
|
|
|
if nodes[0].Checks[1].CheckID != SerfCheckID {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, nodes = store.CheckServiceTagNodes("db", "master")
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes[0].Node.Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
if nodes[0].Service.ID != "db1" {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2014-01-15 21:14:28 +00:00
|
|
|
if len(nodes[0].Checks) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
if nodes[0].Checks[0].CheckID != "db" {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
2014-01-16 03:24:16 +00:00
|
|
|
if nodes[0].Checks[1].CheckID != SerfCheckID {
|
2014-01-08 22:58:53 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
}
|
2014-02-01 20:00:56 +00:00
|
|
|
func BenchmarkCheckServiceNodes(t *testing.B) {
|
2014-03-05 22:34:29 +00:00
|
|
|
store, err := testStateStore()
|
2014-02-01 20:00:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
2014-02-01 20:00:56 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-03 19:03:10 +00:00
|
|
|
if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
2014-02-01 20:00:56 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db1",
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(3, check); err != nil {
|
2014-02-01 20:00:56 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check = &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: SerfCheckID,
|
|
|
|
Name: SerfCheckName,
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := store.EnsureCheck(4, check); err != nil {
|
2014-02-01 20:00:56 +00:00
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
|
|
|
store.CheckServiceNodes("db")
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 22:34:29 +00:00
|
|
|
|
|
|
|
func TestSS_Register_Deregister_Query(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
"statsite-box-stats",
|
|
|
|
"statsite-box-stats",
|
2014-04-03 19:03:10 +00:00
|
|
|
nil,
|
2014-03-05 22:34:29 +00:00
|
|
|
0}
|
|
|
|
if err := store.EnsureService(2, "foo", srv); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
srv = &structs.NodeService{
|
|
|
|
"statsite-share-stats",
|
|
|
|
"statsite-share-stats",
|
2014-04-03 19:03:10 +00:00
|
|
|
nil,
|
2014-03-05 22:34:29 +00:00
|
|
|
0}
|
|
|
|
if err := store.EnsureService(3, "foo", srv); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.DeleteNode(4, "foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, nodes := store.CheckServiceNodes("statsite-share-stats")
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(nodes) != 0 {
|
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 19:13:57 +00:00
|
|
|
|
2014-04-27 19:49:20 +00:00
|
|
|
func TestNodeInfo(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db1",
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(3, check); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check = &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: SerfCheckID,
|
|
|
|
Name: SerfCheckName,
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(4, check); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
2014-04-27 19:56:06 +00:00
|
|
|
idx, dump := store.NodeInfo("foo")
|
2014-04-27 19:49:20 +00:00
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
2014-04-27 19:56:06 +00:00
|
|
|
if len(dump) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", dump)
|
2014-04-27 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 19:56:06 +00:00
|
|
|
info := dump[0]
|
2014-04-27 19:49:20 +00:00
|
|
|
if info.Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
if info.Services[0].ID != "db1" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
if len(info.Checks) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
if info.Checks[0].CheckID != "db" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
if info.Checks[1].CheckID != SerfCheckID {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeDump(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"baz", "127.0.0.2"}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := store.EnsureService(4, "baz", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, dump := store.NodeDump()
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(dump) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", dump)
|
|
|
|
}
|
|
|
|
|
|
|
|
info := dump[0]
|
|
|
|
if info.Node != "baz" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
if info.Services[0].ID != "db1" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
info = dump[1]
|
|
|
|
if info.Node != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
if info.Services[0].ID != "db1" {
|
|
|
|
t.Fatalf("Bad: %v", info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 19:13:57 +00:00
|
|
|
func TestKVSSet_Get(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Should not exist
|
|
|
|
idx, d, err := store.KVSGet("/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if d != nil {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the entry
|
|
|
|
d = &structs.DirEntry{Key: "/foo", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1000, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should exist exist
|
|
|
|
idx, d, err = store.KVSGet("/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1000 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if d.CreateIndex != 1000 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.ModifyIndex != 1000 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Key != "/foo" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Flags != 42 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if string(d.Value) != "test" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the entry
|
|
|
|
d = &structs.DirEntry{Key: "/foo", Flags: 43, Value: []byte("zip")}
|
|
|
|
if err := store.KVSSet(1010, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should update
|
|
|
|
idx, d, err = store.KVSGet("/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1010 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if d.CreateIndex != 1000 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.ModifyIndex != 1010 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Key != "/foo" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Flags != 43 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if string(d.Value) != "zip" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 19:24:06 +00:00
|
|
|
|
|
|
|
func TestKVSDelete(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Create the entry
|
|
|
|
d := &structs.DirEntry{Key: "/foo", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1000, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the entry
|
|
|
|
if err := store.KVSDelete(1020, "/foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not exist
|
|
|
|
idx, d, err := store.KVSGet("/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1020 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if d != nil {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 19:37:11 +00:00
|
|
|
|
|
|
|
func TestKVSCheckAndSet(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// CAS should fail, no entry
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
ModifyIndex: 100,
|
|
|
|
Key: "/foo",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
|
|
|
ok, err := store.KVSCheckAndSet(1000, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("unexpected commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain on not-exist, should work
|
|
|
|
d.ModifyIndex = 0
|
|
|
|
ok, err = store.KVSCheckAndSet(1001, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain on not-exist, should fail
|
|
|
|
d.ModifyIndex = 0
|
|
|
|
ok, err = store.KVSCheckAndSet(1002, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("unexpected commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain on a wrong modify time
|
|
|
|
d.ModifyIndex = 1000
|
|
|
|
ok, err = store.KVSCheckAndSet(1003, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("unexpected commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain on a correct modify time
|
|
|
|
d.ModifyIndex = 1001
|
|
|
|
ok, err = store.KVSCheckAndSet(1004, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected commit")
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 20:12:35 +00:00
|
|
|
|
|
|
|
func TestKVS_List(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Should not exist
|
|
|
|
idx, ents, err := store.KVSList("/web")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(ents) != 0 {
|
|
|
|
t.Fatalf("bad: %v", ents)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the entries
|
|
|
|
d := &structs.DirEntry{Key: "/web/a", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1000, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/b", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1001, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/sub/c", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1002, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should list
|
|
|
|
idx, ents, err = store.KVSList("/web")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1002 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(ents) != 3 {
|
|
|
|
t.Fatalf("bad: %v", ents)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ents[0].Key != "/web/a" {
|
|
|
|
t.Fatalf("bad: %v", ents[0])
|
|
|
|
}
|
|
|
|
if ents[1].Key != "/web/b" {
|
|
|
|
t.Fatalf("bad: %v", ents[1])
|
|
|
|
}
|
|
|
|
if ents[2].Key != "/web/sub/c" {
|
|
|
|
t.Fatalf("bad: %v", ents[2])
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 20:20:08 +00:00
|
|
|
|
2014-04-28 23:26:31 +00:00
|
|
|
func TestKVS_ListKeys(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Should not exist
|
|
|
|
idx, keys, err := store.KVSListKeys("", "/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the entries
|
|
|
|
d := &structs.DirEntry{Key: "/web/a", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1000, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/b", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1001, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/sub/c", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1002, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should list
|
|
|
|
idx, keys, err = store.KVSListKeys("", "/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1002 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[0] != "/" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should list just web
|
|
|
|
idx, keys, err = store.KVSListKeys("/", "/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1002 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[0] != "/web/" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should list a, b, sub/
|
|
|
|
idx, keys, err = store.KVSListKeys("/web/", "/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1002 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(keys) != 3 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[0] != "/web/a" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[1] != "/web/b" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[2] != "/web/sub/" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should list c
|
|
|
|
idx, keys, err = store.KVSListKeys("/web/sub/", "/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1002 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[0] != "/web/sub/c" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
2014-04-28 23:42:32 +00:00
|
|
|
|
|
|
|
// Should list all
|
|
|
|
idx, keys, err = store.KVSListKeys("/web/", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1002 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(keys) != 3 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
if keys[2] != "/web/sub/c" {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
2014-04-28 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 20:20:08 +00:00
|
|
|
func TestKVSDeleteTree(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Should not exist
|
|
|
|
err = store.KVSDeleteTree(1000, "/web")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the entries
|
|
|
|
d := &structs.DirEntry{Key: "/web/a", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1000, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/b", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1001, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d = &structs.DirEntry{Key: "/web/sub/c", Flags: 42, Value: []byte("test")}
|
|
|
|
if err := store.KVSSet(1002, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke the web tree
|
|
|
|
err = store.KVSDeleteTree(1010, "/web")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing should list
|
|
|
|
idx, ents, err := store.KVSList("/web")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1010 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if len(ents) != 0 {
|
|
|
|
t.Fatalf("bad: %v", ents)
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 00:30:05 +00:00
|
|
|
|
|
|
|
func TestSessionCreate(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "bar",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Checks: []string{"bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.SessionCreate(1000, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.ID == "" {
|
|
|
|
t.Fatalf("bad: %v", session)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.CreateIndex != 1000 {
|
|
|
|
t.Fatalf("bad: %v", session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionCreate_Invalid(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// No node registered
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Checks: []string{"bar"},
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(1000, session); err.Error() != "Missing node registration" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check not registered
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(1000, session); err.Error() != "Missing check 'bar' registration" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unhealthy check
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "bar",
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
2014-05-15 18:51:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-05-15 00:30:05 +00:00
|
|
|
}
|
|
|
|
if err := store.SessionCreate(1000, session); err.Error() != "Check 'bar' is in critical state" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Lookups(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Create a session
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(1000, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup by ID
|
|
|
|
idx, s2, err := store.SessionGet(session.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1000 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s2, session) {
|
|
|
|
t.Fatalf("bad: %v", s2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create many sessions
|
|
|
|
ids := []string{session.ID}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(uint64(1000+i), session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ids = append(ids, session.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List all
|
|
|
|
idx, all, err := store.SessionList()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1009 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the ids
|
|
|
|
var out []string
|
|
|
|
for _, s := range all {
|
|
|
|
out = append(out, s.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(ids)
|
|
|
|
sort.Strings(out)
|
|
|
|
if !reflect.DeepEqual(ids, out) {
|
|
|
|
t.Fatalf("bad: %v %v", ids, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List by node
|
|
|
|
idx, nodes, err := store.NodeSessions("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if idx != 1009 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check again for the node list
|
|
|
|
out = nil
|
|
|
|
for _, s := range nodes {
|
|
|
|
out = append(out, s.ID)
|
|
|
|
}
|
|
|
|
sort.Strings(out)
|
|
|
|
if !reflect.DeepEqual(ids, out) {
|
|
|
|
t.Fatalf("bad: %v %v", ids, out)
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 18:51:31 +00:00
|
|
|
|
|
|
|
func TestSessionInvalidate_CriticalHealthCheck(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "bar",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Checks: []string{"bar"},
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(14, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate the check
|
|
|
|
check.Status = structs.HealthCritical
|
|
|
|
if err := store.EnsureCheck(15, check); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup by ID, should be nil
|
|
|
|
_, s2, err := store.SessionGet(session.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s2 != nil {
|
|
|
|
t.Fatalf("session should be invalidated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionInvalidate_DeleteHealthCheck(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "bar",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Checks: []string{"bar"},
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(14, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the check
|
|
|
|
if err := store.DeleteNodeCheck(15, "foo", "bar"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup by ID, should be nil
|
|
|
|
_, s2, err := store.SessionGet(session.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s2 != nil {
|
|
|
|
t.Fatalf("session should be invalidated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionInvalidate_DeleteNode(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(14, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the node
|
|
|
|
if err := store.DeleteNode(15, "foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup by ID, should be nil
|
|
|
|
_, s2, err := store.SessionGet(session.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s2 != nil {
|
|
|
|
t.Fatalf("session should be invalidated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionInvalidate_DeleteNodeService(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(11, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "api",
|
|
|
|
Name: "Can connect",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "api",
|
|
|
|
}
|
|
|
|
if err := store.EnsureCheck(13, check); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
session := &structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Checks: []string{"api"},
|
|
|
|
}
|
|
|
|
if err := store.SessionCreate(14, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should invalidate the session
|
|
|
|
if err := store.DeleteNodeService(15, "foo", "api"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup by ID, should be nil
|
|
|
|
_, s2, err := store.SessionGet(session.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s2 != nil {
|
|
|
|
t.Fatalf("session should be invalidated")
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 21:56:58 +00:00
|
|
|
|
|
|
|
func TestKVSLock(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
session := &structs.Session{Node: "foo"}
|
|
|
|
if err := store.SessionCreate(4, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock with a non-existing keys should work
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "/foo",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
Session: session.ID,
|
|
|
|
}
|
|
|
|
ok, err := store.KVSLock(5, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected fail")
|
|
|
|
}
|
|
|
|
if d.LockIndex != 1 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-locking should fail
|
|
|
|
ok, err = store.KVSLock(6, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("expected fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a normal key
|
|
|
|
k1 := &structs.DirEntry{
|
|
|
|
Key: "/bar",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("asdf"),
|
|
|
|
}
|
|
|
|
if err := store.KVSSet(7, k1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should acquire the lock
|
|
|
|
k1.Session = session.ID
|
|
|
|
ok, err = store.KVSLock(8, k1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-acquire should fail
|
|
|
|
ok, err = store.KVSLock(9, k1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("expected fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVSUnlock(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
session := &structs.Session{Node: "foo"}
|
|
|
|
if err := store.SessionCreate(4, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock with a non-existing keys should fail
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "/foo",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
Session: session.ID,
|
|
|
|
}
|
|
|
|
ok, err := store.KVSUnlock(5, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("expected fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock should work
|
|
|
|
d.Session = session.ID
|
|
|
|
if ok, _ := store.KVSLock(6, d); !ok {
|
|
|
|
t.Fatalf("expected lock")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock should work
|
|
|
|
d.Session = session.ID
|
|
|
|
ok, err = store.KVSUnlock(7, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-lock should work
|
|
|
|
d.Session = session.ID
|
|
|
|
if ok, err := store.KVSLock(8, d); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if !ok {
|
|
|
|
t.Fatalf("expected lock")
|
|
|
|
}
|
|
|
|
if d.LockIndex != 2 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 22:09:55 +00:00
|
|
|
|
|
|
|
func TestSessionInvalidate_KeyUnlock(t *testing.T) {
|
|
|
|
store, err := testStateStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
if err := store.EnsureNode(3, structs.Node{"foo", "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
2014-05-19 19:50:29 +00:00
|
|
|
session := &structs.Session{Node: "foo", LockDelay: 50 * time.Millisecond}
|
2014-05-15 22:09:55 +00:00
|
|
|
if err := store.SessionCreate(4, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock a key with the session
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "/foo",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
Session: session.ID,
|
|
|
|
}
|
|
|
|
ok, err := store.KVSLock(5, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the node
|
|
|
|
if err := store.DeleteNode(6, "foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key should be unlocked
|
|
|
|
idx, d2, err := store.KVSGet("/foo")
|
|
|
|
if idx != 6 {
|
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
if d2.LockIndex != 1 {
|
|
|
|
t.Fatalf("bad: %v", *d2)
|
|
|
|
}
|
|
|
|
if d2.Session != "" {
|
|
|
|
t.Fatalf("bad: %v", *d2)
|
|
|
|
}
|
2014-05-19 19:50:29 +00:00
|
|
|
|
|
|
|
// Key should have a lock delay
|
|
|
|
expires := store.KVSLockDelay("/foo")
|
|
|
|
if expires.Before(time.Now().Add(30 * time.Millisecond)) {
|
|
|
|
t.Fatalf("Bad: %v", expires)
|
|
|
|
}
|
2014-05-15 22:09:55 +00:00
|
|
|
}
|