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"
|
|
|
|
|
2018-09-04 11:31:51 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
2018-04-26 13:01:20 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
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"
|
2018-04-05 16:15:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-08-20 22:22:22 +00:00
|
|
|
)
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
var errBadContent = errors.New("bad content")
|
2017-10-24 14:28:20 +00:00
|
|
|
var errTimeout = errors.New("timeout")
|
|
|
|
|
|
|
|
var timeout = 5 * time.Second
|
|
|
|
|
|
|
|
func makeInvokeCh() chan error {
|
|
|
|
ch := make(chan error)
|
|
|
|
time.AfterFunc(timeout, func() { ch <- errTimeout })
|
|
|
|
return ch
|
|
|
|
}
|
2017-10-24 10:48:05 +00:00
|
|
|
|
2014-08-20 22:22:22 +00:00
|
|
|
func TestKeyWatch(t *testing.T) {
|
2017-10-24 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"key", "key":"foo/bar/baz"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 13:02:25 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.(*consulapi.KVPair)
|
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
if string(v.Value) != "test" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
2017-10-24 13:02:25 +00:00
|
|
|
invoke <- nil
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 13:02:25 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-20 22:22:22 +00:00
|
|
|
go func() {
|
2017-10-24 13:02:25 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
kv := a.Client().KV()
|
2017-10-24 13:02:25 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 22:22:22 +00:00
|
|
|
pair := &consulapi.KVPair{
|
|
|
|
Key: "foo/bar/baz",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
2017-10-24 13:02:25 +00:00
|
|
|
if _, err := kv.Put(pair, nil); err != nil {
|
2014-08-20 22:22:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-24 13:02:25 +00:00
|
|
|
}()
|
2014-08-20 22:22:22 +00:00
|
|
|
|
2017-10-24 13:02:25 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
2014-08-20 22:22:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 13:02:25 +00:00
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 22:22:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:02:25 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 03:21:14 +00:00
|
|
|
func TestKeyWatch_With_PrefixDelete(t *testing.T) {
|
2017-10-24 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2017-07-21 03:21:14 +00:00
|
|
|
plan := mustParse(t, `{"type":"key", "key":"foo/bar/baz"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 13:07:44 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
2017-07-21 03:21:14 +00:00
|
|
|
}
|
2017-10-24 13:07:44 +00:00
|
|
|
v, ok := raw.(*consulapi.KVPair)
|
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
if string(v.Value) != "test" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
2017-07-21 03:21:14 +00:00
|
|
|
}
|
2017-10-24 13:07:44 +00:00
|
|
|
invoke <- nil
|
2017-07-21 03:21:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 13:07:44 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2017-07-21 03:21:14 +00:00
|
|
|
go func() {
|
2017-10-24 13:07:44 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
kv := a.Client().KV()
|
2017-10-24 13:07:44 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2017-07-21 03:21:14 +00:00
|
|
|
pair := &consulapi.KVPair{
|
|
|
|
Key: "foo/bar/baz",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
2017-10-24 13:07:44 +00:00
|
|
|
if _, err := kv.Put(pair, nil); err != nil {
|
2017-07-21 03:21:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-24 13:07:44 +00:00
|
|
|
}()
|
2017-07-21 03:21:14 +00:00
|
|
|
|
2017-10-24 13:07:44 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
2017-07-21 03:21:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 13:07:44 +00:00
|
|
|
if err := <-invoke; err != nil {
|
2017-07-21 03:21:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:07:44 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2017-07-21 03:21:14 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 22:22:22 +00:00
|
|
|
func TestKeyPrefixWatch(t *testing.T) {
|
2017-10-24 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"keyprefix", "prefix":"foo/"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 13:12:08 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.(consulapi.KVPairs)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
2017-10-24 13:12:08 +00:00
|
|
|
if string(v[0].Key) != "foo/bar" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
invoke <- nil
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 13:12:08 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-20 22:22:22 +00:00
|
|
|
go func() {
|
2017-10-24 13:12:08 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
kv := a.Client().KV()
|
2017-10-24 13:12:08 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2014-08-20 22:22:22 +00:00
|
|
|
pair := &consulapi.KVPair{
|
|
|
|
Key: "foo/bar",
|
|
|
|
}
|
2017-10-24 13:12:08 +00:00
|
|
|
if _, err := kv.Put(pair, nil); err != nil {
|
2014-08-20 22:22:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-24 13:12:08 +00:00
|
|
|
}()
|
2014-08-20 22:22:22 +00:00
|
|
|
|
2017-10-24 13:12:08 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := plan.Run(a.HTTPAddr()); err != nil {
|
2014-08-20 22:22:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-24 13:12:08 +00:00
|
|
|
if err := <-invoke; err != nil {
|
2014-08-20 22:22:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:12:08 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
2014-08-20 22:29:31 +00:00
|
|
|
|
|
|
|
func TestServicesWatch(t *testing.T) {
|
2017-10-24 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"services"}`)
|
2018-04-25 19:41:26 +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 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"nodes"}`)
|
2018-04-25 19:41:26 +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 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"service", "service":"foo", "tag":"bar", "passingonly":true}`)
|
2018-04-25 19:41:26 +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 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"checks", "state":"warning"}`)
|
2018-04-25 19:41:26 +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 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-21 18:38:44 +00:00
|
|
|
plan := mustParse(t, `{"type":"checks", "service":"foobar"}`)
|
2018-04-25 19:41:26 +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 13:24:57 +00:00
|
|
|
t.Parallel()
|
2017-10-24 09:59:44 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2017-10-24 14:28:20 +00:00
|
|
|
invoke := makeInvokeCh()
|
2014-08-28 22:41:06 +00:00
|
|
|
plan := mustParse(t, `{"type":"event", "name": "foo"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2017-10-24 13:19:13 +00:00
|
|
|
if raw == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v, ok := raw.([]*consulapi.UserEvent)
|
|
|
|
if !ok || len(v) == 0 {
|
|
|
|
return // ignore
|
2014-08-28 22:41:06 +00:00
|
|
|
}
|
2017-10-24 13:19:13 +00:00
|
|
|
if string(v[len(v)-1].Name) != "foo" {
|
|
|
|
invoke <- errBadContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
invoke <- nil
|
2014-08-28 22:41:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 13:19:13 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2014-08-28 22:41:06 +00:00
|
|
|
go func() {
|
2017-10-24 13:19:13 +00:00
|
|
|
defer wg.Done()
|
2017-10-24 09:59:44 +00:00
|
|
|
event := a.Client().Event()
|
2017-10-24 13:19:13 +00:00
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
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 13:19:13 +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-28 22:41:06 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:19:13 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2014-08-28 22:41:06 +00:00
|
|
|
}
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2018-04-05 16:15:43 +00:00
|
|
|
func TestConnectRootsWatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-04-26 13:01:20 +00:00
|
|
|
// NewTestAgent will bootstrap a new CA
|
|
|
|
a := agent.NewTestAgent(t.Name(), "")
|
2018-04-05 16:15:43 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-04 11:31:51 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
var originalCAID string
|
2018-04-05 16:15:43 +00:00
|
|
|
invoke := makeInvokeCh()
|
|
|
|
plan := mustParse(t, `{"type":"connect_roots"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2018-04-05 16:15:43 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.(*consulapi.CARootList)
|
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
2018-04-26 13:01:20 +00:00
|
|
|
// Only 1 CA is the bootstrapped state (i.e. first response). Ignore this
|
|
|
|
// state and wait for the new CA to show up too.
|
|
|
|
if len(v.Roots) == 1 {
|
|
|
|
originalCAID = v.ActiveRootID
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NotEmpty(t, originalCAID)
|
|
|
|
assert.NotEqual(t, originalCAID, v.ActiveRootID)
|
2018-04-05 16:15:43 +00:00
|
|
|
invoke <- nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2018-04-26 13:01:20 +00:00
|
|
|
// Set a new CA
|
|
|
|
connect.TestCAConfigSet(t, a, nil)
|
2018-04-05 16:15:43 +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 {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectLeafWatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-04-26 13:01:20 +00:00
|
|
|
// NewTestAgent will bootstrap a new CA
|
2018-04-05 16:15:43 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
// Register a web service to get certs for
|
|
|
|
{
|
|
|
|
agent := a.Client().Agent()
|
|
|
|
reg := consulapi.AgentServiceRegistration{
|
|
|
|
ID: "web",
|
|
|
|
Name: "web",
|
|
|
|
Port: 9090,
|
|
|
|
}
|
|
|
|
err := agent.ServiceRegister(®)
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
var lastCert *consulapi.LeafCert
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
//invoke := makeInvokeCh()
|
|
|
|
invoke := make(chan error)
|
2018-05-19 07:11:51 +00:00
|
|
|
plan := mustParse(t, `{"type":"connect_leaf", "service":"web"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
2018-04-05 16:15:43 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.(*consulapi.LeafCert)
|
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
2018-04-26 13:01:20 +00:00
|
|
|
if lastCert == nil {
|
|
|
|
// Initial fetch, just store the cert and return
|
|
|
|
lastCert = v
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO(banks): right now the root rotation actually causes Serial numbers
|
|
|
|
// to reset so these end up all being the same. That needs fixing but it's
|
|
|
|
// a bigger task than I want to bite off for this PR.
|
|
|
|
//assert.NotEqual(t, lastCert.SerialNumber, v.SerialNumber)
|
|
|
|
assert.NotEqual(t, lastCert.CertPEM, v.CertPEM)
|
|
|
|
assert.NotEqual(t, lastCert.PrivateKeyPEM, v.PrivateKeyPEM)
|
|
|
|
assert.NotEqual(t, lastCert.ModifyIndex, v.ModifyIndex)
|
2018-04-05 16:15:43 +00:00
|
|
|
invoke <- nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2018-04-26 13:01:20 +00:00
|
|
|
// Change the CA to trigger a leaf change
|
|
|
|
connect.TestCAConfigSet(t, a, nil)
|
2018-04-05 16:15:43 +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 {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectProxyConfigWatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-06-14 13:07:33 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), `
|
|
|
|
connect {
|
|
|
|
enabled = true
|
|
|
|
proxy {
|
|
|
|
allow_managed_api_registration = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
2018-04-05 16:15:43 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-04 11:32:59 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
// Register a local agent service with a managed proxy
|
|
|
|
reg := &consulapi.AgentServiceRegistration{
|
|
|
|
Name: "web",
|
|
|
|
Port: 8080,
|
|
|
|
Connect: &consulapi.AgentServiceConnect{
|
|
|
|
Proxy: &consulapi.AgentServiceConnectProxy{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := a.Client()
|
|
|
|
agent := client.Agent()
|
|
|
|
err := agent.ServiceRegister(reg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
invoke := makeInvokeCh()
|
|
|
|
plan := mustParse(t, `{"type":"connect_proxy_config", "proxy_service_id":"web-proxy"}`)
|
2018-04-25 19:41:26 +00:00
|
|
|
plan.HybridHandler = func(blockParamVal watch.BlockingParamVal, raw interface{}) {
|
2018-04-05 16:15:43 +00:00
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.(*consulapi.ConnectProxyConfig)
|
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
invoke <- nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
|
|
|
// Change the proxy's config
|
|
|
|
reg.Connect.Proxy.Config["foo"] = "buzz"
|
|
|
|
reg.Connect.Proxy.Config["baz"] = "qux"
|
|
|
|
err := agent.ServiceRegister(reg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2017-10-24 09:59:44 +00:00
|
|
|
func mustParse(t *testing.T, q string) *watch.Plan {
|
2018-04-26 13:01:20 +00:00
|
|
|
t.Helper()
|
2017-10-24 09:59:44 +00:00
|
|
|
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
|
|
|
|
}
|