2017-10-24 09:59:44 +00:00
|
|
|
package watch_test
|
2014-08-20 22:22:22 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-24 09:59:44 +00:00
|
|
|
"encoding/json"
|
2017-10-24 10:48:05 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
2014-08-20 22:22:22 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2017-10-24 09:59:44 +00:00
|
|
|
"github.com/hashicorp/consul/watch"
|
2014-08-20 22:22:22 +00:00
|
|
|
)
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
var errBadContent = errors.New("bad content")
|
|
|
|
|
2014-08-20 22:22:22 +00:00
|
|
|
func TestKeyWatch(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"key", "key":"foo/bar/baz"}`)
|
2014-08-20 22:22:22 +00:00
|
|
|
invoke := 0
|
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
|
|
|
if invoke == 0 {
|
|
|
|
if raw == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v, ok := raw.(*consulapi.KVPair)
|
|
|
|
if !ok || v == nil || string(v.Value) != "test" {
|
|
|
|
t.Fatalf("Bad: %#v", raw)
|
|
|
|
}
|
|
|
|
invoke++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer plan.Stop()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
kv := a.Client().KV()
|
2014-08-20 22:22:22 +00:00
|
|
|
pair := &consulapi.KVPair{
|
|
|
|
Key: "foo/bar/baz",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
|
|
|
_, err := kv.Put(pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the query to run
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
plan.Stop()
|
|
|
|
|
|
|
|
// Delete the key
|
|
|
|
_, err = kv.Delete("foo/bar/baz", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
err := plan.Run(a.HTTPAddr())
|
2014-08-20 22:22:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoke == 0 {
|
|
|
|
t.Fatalf("bad: %v", invoke)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 03:21:14 +00:00
|
|
|
func TestKeyWatch_With_PrefixDelete(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-07-21 03:21:14 +00:00
|
|
|
plan := mustParse(t, `{"type":"key", "key":"foo/bar/baz"}`)
|
|
|
|
invoke := 0
|
|
|
|
deletedKeyWatchInvoked := 0
|
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
|
|
|
if raw == nil && deletedKeyWatchInvoked == 0 {
|
|
|
|
deletedKeyWatchInvoked++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if invoke == 0 {
|
|
|
|
v, ok := raw.(*consulapi.KVPair)
|
|
|
|
if !ok || v == nil || string(v.Value) != "test" {
|
|
|
|
t.Fatalf("Bad: %#v", raw)
|
|
|
|
}
|
|
|
|
invoke++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer plan.Stop()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
kv := a.Client().KV()
|
2017-07-21 03:21:14 +00:00
|
|
|
pair := &consulapi.KVPair{
|
|
|
|
Key: "foo/bar/baz",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
|
|
|
_, err := kv.Put(pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the query to run
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
|
|
|
// Delete the key
|
|
|
|
_, err = kv.DeleteTree("foo/bar", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plan.Stop()
|
|
|
|
}()
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
err := plan.Run(a.HTTPAddr())
|
2017-07-21 03:21:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if invoke != 1 {
|
|
|
|
t.Fatalf("expected watch plan to be invoked once but got %v", invoke)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deletedKeyWatchInvoked != 1 {
|
|
|
|
t.Fatalf("expected watch plan to be invoked once on delete but got %v", deletedKeyWatchInvoked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:22:22 +00:00
|
|
|
func TestKeyPrefixWatch(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"keyprefix", "prefix":"foo/"}`)
|
2014-08-20 22:22:22 +00:00
|
|
|
invoke := 0
|
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
|
|
|
if invoke == 0 {
|
|
|
|
if raw == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v, ok := raw.(consulapi.KVPairs)
|
2014-08-20 22:29:31 +00:00
|
|
|
if ok && v == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-20 22:22:22 +00:00
|
|
|
if !ok || v == nil || string(v[0].Key) != "foo/bar" {
|
|
|
|
t.Fatalf("Bad: %#v", raw)
|
|
|
|
}
|
|
|
|
invoke++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer plan.Stop()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
kv := a.Client().KV()
|
2014-08-20 22:22:22 +00:00
|
|
|
pair := &consulapi.KVPair{
|
|
|
|
Key: "foo/bar",
|
|
|
|
}
|
|
|
|
_, err := kv.Put(pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the query to run
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
plan.Stop()
|
|
|
|
|
|
|
|
// Delete the key
|
|
|
|
_, err = kv.Delete("foo/bar", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
err := plan.Run(a.HTTPAddr())
|
2014-08-20 22:22:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoke == 0 {
|
|
|
|
t.Fatalf("bad: %v", invoke)
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 22:29:31 +00:00
|
|
|
|
|
|
|
func TestServicesWatch(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-10-24 12:30:58 +00:00
|
|
|
invoke := make(chan error)
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"services"}`)
|
2014-08-20 22:29:31 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 12:30:58 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.(map[string][]string)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
if v["consul"] == nil {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
2014-08-20 22:29:31 +00:00
|
|
|
}
|
2017-10-24 12:30:58 +00:00
|
|
|
invoke <- nil
|
2014-08-20 22:29:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:30:58 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-20 22:29:31 +00:00
|
|
|
go func() {
|
2017-10-24 12:30:58 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
agent := a.Client().Agent()
|
2017-10-24 12:30:58 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 22:29:31 +00:00
|
|
|
reg := &consulapi.AgentServiceRegistration{
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
}
|
2017-10-24 12:30:58 +00:00
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-08-20 22:29:31 +00:00
|
|
|
}()
|
|
|
|
|
2017-10-24 12:30:58 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 22:29:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:30:58 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 22:29:31 +00:00
|
|
|
}
|
2014-08-20 22:33:13 +00:00
|
|
|
|
|
|
|
func TestNodesWatch(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
invoke := make(chan error)
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"nodes"}`)
|
2014-08-20 22:33:13 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 10:48:05 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.([]*consulapi.Node)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return // ignore
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
2017-10-24 10:48:05 +00:00
|
|
|
invoke <- nil
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-20 22:33:13 +00:00
|
|
|
go func() {
|
2017-10-24 10:48:05 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
catalog := a.Client().Catalog()
|
2017-10-24 10:48:05 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 22:33:13 +00:00
|
|
|
reg := &consulapi.CatalogRegistration{
|
|
|
|
Node: "foobar",
|
|
|
|
Address: "1.1.1.1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2017-10-24 10:48:05 +00:00
|
|
|
if _, err := catalog.Register(reg, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 22:33:13 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
2014-08-20 22:50:32 +00:00
|
|
|
|
|
|
|
func TestServiceWatch(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
invoke := make(chan error)
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"service", "service":"foo", "tag":"bar", "passingonly":true}`)
|
2014-08-20 22:50:32 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 12:23:57 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
2017-10-24 12:23:57 +00:00
|
|
|
v, ok := raw.([]*consulapi.ServiceEntry)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
if v[0].Service.ID != "foo" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
invoke <- nil
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
var wg sync.WaitGroup
|
2014-08-20 22:50:32 +00:00
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
agent := a.Client().Agent()
|
2017-10-24 12:23:57 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 22:50:32 +00:00
|
|
|
reg := &consulapi.AgentServiceRegistration{
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar"},
|
|
|
|
}
|
2017-10-24 12:23:57 +00:00
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2014-08-20 22:50:32 +00:00
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-08-20 22:50:32 +00:00
|
|
|
}()
|
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 22:50:32 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
2014-08-20 23:32:12 +00:00
|
|
|
|
|
|
|
func TestChecksWatch_State(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-10-24 12:39:26 +00:00
|
|
|
invoke := make(chan error)
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"checks", "state":"warning"}`)
|
2014-08-20 23:32:12 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 12:39:26 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
2017-10-24 12:39:26 +00:00
|
|
|
v, ok := raw.([]*consulapi.HealthCheck)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
if v[0].CheckID != "foobar" || v[0].Status != "warning" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
invoke <- nil
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:39:26 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-20 23:32:12 +00:00
|
|
|
go func() {
|
2017-10-24 12:39:26 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
catalog := a.Client().Catalog()
|
2017-10-24 12:39:26 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 23:32:12 +00:00
|
|
|
reg := &consulapi.CatalogRegistration{
|
|
|
|
Node: "foobar",
|
|
|
|
Address: "1.1.1.1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Check: &consulapi.AgentCheck{
|
|
|
|
Node: "foobar",
|
|
|
|
CheckID: "foobar",
|
|
|
|
Name: "foobar",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: consulapi.HealthWarning,
|
2014-08-20 23:32:12 +00:00
|
|
|
},
|
|
|
|
}
|
2017-10-24 12:39:26 +00:00
|
|
|
if _, err := catalog.Register(reg, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2014-08-20 23:32:12 +00:00
|
|
|
|
2017-10-24 12:39:26 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 12:39:26 +00:00
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 23:32:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:39:26 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChecksWatch_Service(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-10-24 12:55:04 +00:00
|
|
|
invoke := make(chan error)
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"checks", "service":"foobar"}`)
|
2014-08-20 23:32:12 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 12:55:04 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
2017-10-24 12:55:04 +00:00
|
|
|
v, ok := raw.([]*consulapi.HealthCheck)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
if v[0].CheckID != "foobar" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
invoke <- nil
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:55:04 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-20 23:32:12 +00:00
|
|
|
go func() {
|
2017-10-24 12:55:04 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
catalog := a.Client().Catalog()
|
2017-10-24 12:55:04 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 23:32:12 +00:00
|
|
|
reg := &consulapi.CatalogRegistration{
|
|
|
|
Node: "foobar",
|
|
|
|
Address: "1.1.1.1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Service: &consulapi.AgentService{
|
|
|
|
ID: "foobar",
|
|
|
|
Service: "foobar",
|
|
|
|
},
|
|
|
|
Check: &consulapi.AgentCheck{
|
|
|
|
Node: "foobar",
|
|
|
|
CheckID: "foobar",
|
|
|
|
Name: "foobar",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: consulapi.HealthPassing,
|
2014-08-20 23:32:12 +00:00
|
|
|
ServiceID: "foobar",
|
|
|
|
},
|
|
|
|
}
|
2017-10-24 12:55:04 +00:00
|
|
|
if _, err := catalog.Register(reg, nil); err != nil {
|
2014-08-20 23:32:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-24 12:55:04 +00:00
|
|
|
}()
|
2014-08-20 23:32:12 +00:00
|
|
|
|
2017-10-24 12:55:04 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 12:55:04 +00:00
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 23:32:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:55:04 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
2014-08-28 22:41:06 +00:00
|
|
|
|
|
|
|
func TestEventWatch(t *testing.T) {
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
2014-08-28 22:41:06 +00:00
|
|
|
plan := mustParse(t, `{"type":"event", "name": "foo"}`)
|
|
|
|
invoke := 0
|
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
|
|
|
if invoke == 0 {
|
|
|
|
if raw == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v, ok := raw.([]*consulapi.UserEvent)
|
|
|
|
if !ok || len(v) == 0 || string(v[len(v)-1].Name) != "foo" {
|
|
|
|
t.Fatalf("Bad: %#v", raw)
|
|
|
|
}
|
|
|
|
invoke++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer plan.Stop()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
event := a.Client().Event()
|
2014-08-28 22:41:06 +00:00
|
|
|
params := &consulapi.UserEvent{Name: "foo"}
|
|
|
|
if _, _, err := event.Fire(params, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
err := plan.Run(a.HTTPAddr())
|
2014-08-28 22:41:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoke == 0 {
|
|
|
|
t.Fatalf("bad: %v", invoke)
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 09:59:44 +00:00
|
|
|
|
|
|
|
func mustParse(t *testing.T, q string) *watch.Plan {
|
|
|
|
var params map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(q), ¶ms); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
plan, err := watch.Parse(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return plan
|
|
|
|
}
|