2017-10-24 09:59:44 +00:00
|
|
|
package watch_test
|
2014-08-20 22:22:22 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-26 16:33:01 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2017-10-24 09:59:44 +00:00
|
|
|
"encoding/json"
|
2019-04-26 16:33:01 +00:00
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2017-10-24 10:48:05 +00:00
|
|
|
"sync"
|
2014-08-20 22:22:22 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/api/watch"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2018-04-05 16:15:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-08-20 22:22:22 +00:00
|
|
|
)
|
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
func makeClient(t *testing.T) (*api.Client, *testutil.TestServer) {
|
|
|
|
// Make client config
|
|
|
|
conf := api.DefaultConfig()
|
|
|
|
|
|
|
|
// Create server
|
|
|
|
server, err := testutil.NewTestServerConfigT(t, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
conf.Address = server.HTTPAddr
|
|
|
|
|
|
|
|
// Create client
|
|
|
|
client, err := api.NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
server.Stop()
|
|
|
|
// guaranteed to fail but will be a nice error message
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, server
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateConnectCA(t *testing.T, client *api.Client) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
connect := client.Connect()
|
|
|
|
cfg, _, err := connect.CAGetConfig(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, cfg.Config)
|
|
|
|
|
|
|
|
// update the cert
|
|
|
|
// Create the private key we'll use for this CA cert.
|
|
|
|
pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
bs, err := x509.MarshalECPrivateKey(pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
require.NoError(t, pem.Encode(&buf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: bs}))
|
|
|
|
cfg.Config["PrivateKey"] = buf.String()
|
|
|
|
|
|
|
|
bs, err = x509.MarshalPKIXPublicKey(pk.Public())
|
|
|
|
require.NoError(t, err)
|
|
|
|
kID := []byte(strings.Replace(fmt.Sprintf("% x", sha256.Sum256(bs)), " ", ":", -1))
|
|
|
|
|
|
|
|
// Create the CA cert
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: big.NewInt(42),
|
|
|
|
Subject: pkix.Name{CommonName: "CA Modified"},
|
|
|
|
URIs: []*url.URL{&url.URL{Scheme: "spiffe", Host: fmt.Sprintf("11111111-2222-3333-4444-555555555555.%s", "consul")}},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
KeyUsage: x509.KeyUsageCertSign |
|
|
|
|
x509.KeyUsageCRLSign |
|
|
|
|
x509.KeyUsageDigitalSignature,
|
|
|
|
IsCA: true,
|
|
|
|
NotAfter: time.Now().AddDate(10, 0, 0),
|
|
|
|
NotBefore: time.Now(),
|
|
|
|
AuthorityKeyId: kID,
|
|
|
|
SubjectKeyId: kID,
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err = x509.CreateCertificate(rand.Reader, &template, &template, pk.Public(), pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
require.NoError(t, pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs}))
|
|
|
|
cfg.Config["RootCert"] = buf.String()
|
|
|
|
|
|
|
|
_, err = connect.CASetConfig(cfg, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:22:22 +00:00
|
|
|
func TestKeyWatch(t *testing.T) {
|
2017-10-24 13:24:57 +00:00
|
|
|
t.Parallel()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []*api.KVPair
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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{}) {
|
2019-09-18 17:07:11 +00:00
|
|
|
var v *api.KVPair
|
|
|
|
if raw == nil { // nil is a valid return value
|
|
|
|
v = nil
|
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
if v, ok = raw.(*api.KVPair); !ok {
|
|
|
|
return // ignore
|
|
|
|
}
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
2017-10-24 13:02:25 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
|
|
|
kv := c.KV()
|
2019-04-26 16:33:01 +00:00
|
|
|
pair := &api.KVPair{
|
2014-08-20 22:22:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 13:02:25 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Nil(t, v)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Equal(t, "test", string(v.Value))
|
|
|
|
}
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []*api.KVPair
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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{}) {
|
2019-09-18 17:07:11 +00:00
|
|
|
var v *api.KVPair
|
|
|
|
if raw == nil { // nil is a valid return value
|
|
|
|
v = nil
|
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
if v, ok = raw.(*api.KVPair); !ok {
|
|
|
|
return // ignore
|
|
|
|
}
|
2017-07-21 03:21:14 +00:00
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
2017-10-24 13:07:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
|
|
|
{
|
|
|
|
kv := c.KV()
|
2019-04-26 16:33:01 +00:00
|
|
|
pair := &api.KVPair{
|
2017-07-21 03:21:14 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 13:07:44 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Nil(t, v)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Equal(t, "test", string(v.Value))
|
|
|
|
}
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []api.KVPairs
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.(api.KVPairs)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 13:12:08 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
2017-10-24 13:12:08 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
|
|
|
kv := c.KV()
|
2019-04-26 16:33:01 +00:00
|
|
|
pair := &api.KVPair{
|
2014-08-20 22:22:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 13:12:08 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 0)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "foo/bar", v[0].Key)
|
|
|
|
}
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []map[string][]string
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 12:30:58 +00:00
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
|
|
|
|
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
2019-04-26 16:33:01 +00:00
|
|
|
agent := c.Agent()
|
2017-10-24 12:30:58 +00:00
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
reg := &api.AgentServiceRegistration{
|
2014-08-20 22:29:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 12:30:58 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
_, ok := v["consul"]
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 2)
|
|
|
|
_, ok := v["consul"]
|
|
|
|
require.True(t, ok)
|
|
|
|
_, ok = v["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups [][]*api.Node
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.([]*api.Node)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 10:48:05 +00:00
|
|
|
return // ignore
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
|
|
|
|
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
2019-04-26 16:33:01 +00:00
|
|
|
catalog := c.Catalog()
|
2017-10-24 10:48:05 +00:00
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
reg := &api.CatalogRegistration{
|
2014-08-20 22:33:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 10:48:05 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
var originalNodeName string
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
originalNodeName = v[0].Node
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 2)
|
|
|
|
if v[0].Node == originalNodeName {
|
|
|
|
require.Equal(t, "foobar", v[1].Node)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, "foobar", v[0].Node)
|
|
|
|
}
|
|
|
|
}
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups [][]*api.ServiceEntry
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.([]*api.ServiceEntry)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 12:23:57 +00:00
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
|
|
|
|
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
2019-04-26 16:33:01 +00:00
|
|
|
agent := c.Agent()
|
2017-10-24 12:23:57 +00:00
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
reg := &api.AgentServiceRegistration{
|
2014-08-20 22:50:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 12:23:57 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 0)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "foo", v[0].Service.ID)
|
|
|
|
}
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
2014-08-20 23:32:12 +00:00
|
|
|
|
2019-04-29 19:28:01 +00:00
|
|
|
func TestServiceMultipleTagsWatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups [][]*api.ServiceEntry
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
2019-04-29 19:28:01 +00:00
|
|
|
plan := mustParse(t, `{"type":"service", "service":"foo", "tag":["bar","buzz"], "passingonly":true}`)
|
|
|
|
plan.Handler = func(idx uint64, raw interface{}) {
|
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
|
|
|
v, ok := raw.([]*api.ServiceEntry)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2019-04-29 19:28:01 +00:00
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
2019-04-29 19:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
|
|
|
|
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
2019-04-29 19:28:01 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
|
|
|
|
// we do not want to find this one.
|
|
|
|
reg := &api.AgentServiceRegistration{
|
|
|
|
ID: "foobarbiff",
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "biff"},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// we do not want to find this one.
|
|
|
|
reg = &api.AgentServiceRegistration{
|
|
|
|
ID: "foobuzzbiff",
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"buzz", "biff"},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// we want to find this one
|
|
|
|
reg = &api.AgentServiceRegistration{
|
|
|
|
ID: "foobarbuzzbiff",
|
|
|
|
Name: "foo",
|
|
|
|
Tags: []string{"bar", "buzz", "biff"},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2019-04-29 19:28:01 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 0)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
|
|
|
|
require.Equal(t, "foobarbuzzbiff", v[0].Service.ID)
|
|
|
|
require.ElementsMatch(t, []string{"bar", "buzz", "biff"}, v[0].Service.Tags)
|
|
|
|
}
|
2019-04-29 19:28:01 +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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups [][]*api.HealthCheck
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.([]*api.HealthCheck)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 12:39:26 +00:00
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
|
|
|
|
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
2019-04-26 16:33:01 +00:00
|
|
|
catalog := c.Catalog()
|
2017-10-24 12:39:26 +00:00
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
reg := &api.CatalogRegistration{
|
2014-08-20 23:32:12 +00:00
|
|
|
Node: "foobar",
|
|
|
|
Address: "1.1.1.1",
|
|
|
|
Datacenter: "dc1",
|
2019-04-26 16:33:01 +00:00
|
|
|
Check: &api.AgentCheck{
|
2014-08-20 23:32:12 +00:00
|
|
|
Node: "foobar",
|
|
|
|
CheckID: "foobar",
|
|
|
|
Name: "foobar",
|
2019-04-26 16:33:01 +00:00
|
|
|
Status: api.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
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 12:39:26 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 0)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "foobar", v[0].CheckID)
|
|
|
|
require.Equal(t, "warning", v[0].Status)
|
|
|
|
}
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChecksWatch_Service(t *testing.T) {
|
2017-10-24 13:24:57 +00:00
|
|
|
t.Parallel()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups [][]*api.HealthCheck
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.([]*api.HealthCheck)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 12:55:04 +00:00
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer plan.Stop()
|
|
|
|
|
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
2019-04-26 16:33:01 +00:00
|
|
|
catalog := c.Catalog()
|
2017-10-24 12:55:04 +00:00
|
|
|
|
2019-04-26 16:33:01 +00:00
|
|
|
reg := &api.CatalogRegistration{
|
2014-08-20 23:32:12 +00:00
|
|
|
Node: "foobar",
|
|
|
|
Address: "1.1.1.1",
|
|
|
|
Datacenter: "dc1",
|
2019-04-26 16:33:01 +00:00
|
|
|
Service: &api.AgentService{
|
2014-08-20 23:32:12 +00:00
|
|
|
ID: "foobar",
|
|
|
|
Service: "foobar",
|
|
|
|
},
|
2019-04-26 16:33:01 +00:00
|
|
|
Check: &api.AgentCheck{
|
2014-08-20 23:32:12 +00:00
|
|
|
Node: "foobar",
|
|
|
|
CheckID: "foobar",
|
|
|
|
Name: "foobar",
|
2019-04-26 16:33:01 +00:00
|
|
|
Status: api.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 12:55:04 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 0)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "foobar", v[0].CheckID)
|
|
|
|
}
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2017-10-24 09:59:44 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups [][]*api.UserEvent
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.([]*api.UserEvent)
|
2019-09-18 17:07:11 +00:00
|
|
|
if !ok {
|
2017-10-24 13:19:13 +00:00
|
|
|
return // ignore
|
2014-08-28 22:41:06 +00:00
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
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()
|
2019-09-18 17:07:11 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
2014-08-28 22:41:06 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-09-18 17:07:11 +00:00
|
|
|
defer plan.Stop()
|
2014-08-28 22:41:06 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
|
|
|
event := c.Event()
|
|
|
|
|
|
|
|
params := &api.UserEvent{Name: "foo"}
|
|
|
|
if _, _, err := event.Fire(params, nil); err != nil {
|
2017-10-24 13:19:13 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-08-28 22:41:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2017-10-24 13:19:13 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v, 0)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "foo", v[0].Name)
|
|
|
|
}
|
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()
|
2019-04-26 16:33:01 +00:00
|
|
|
// makeClient will bootstrap a CA
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []*api.CARootList
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
2018-04-05 16:15:43 +00:00
|
|
|
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
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.(*api.CARootList)
|
2019-07-17 15:33:38 +00:00
|
|
|
if !ok || v == nil || len(v.Roots) == 0 {
|
2018-04-05 16:15:43 +00:00
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
2018-04-05 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2019-04-26 16:33:01 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-09-18 17:07:11 +00:00
|
|
|
defer plan.Stop()
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
|
|
|
updateConnectCA(t, c)
|
|
|
|
|
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
var originalCAID string
|
|
|
|
{ // Only 1 CA is the bootstrapped state (i.e. first response).
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Len(t, v.Roots, 1)
|
|
|
|
originalCAID = v.ActiveRootID
|
|
|
|
require.NotEmpty(t, originalCAID)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // This is the new CA showing up.
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Len(t, v.Roots, 2)
|
|
|
|
require.NotEqual(t, originalCAID, v.ActiveRootID)
|
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectLeafWatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-04-26 16:33:01 +00:00
|
|
|
// makeClient will bootstrap a CA
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
// Register a web service to get certs for
|
|
|
|
{
|
2019-04-26 16:33:01 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
reg := api.AgentServiceRegistration{
|
2018-04-05 16:15:43 +00:00
|
|
|
ID: "web",
|
|
|
|
Name: "web",
|
|
|
|
Port: 9090,
|
|
|
|
}
|
|
|
|
err := agent.ServiceRegister(®)
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []*api.LeafCert
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
2018-04-05 16:15:43 +00:00
|
|
|
|
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
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.(*api.LeafCert)
|
2018-04-05 16:15:43 +00:00
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
2018-04-05 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2019-04-26 16:33:01 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-09-18 17:07:11 +00:00
|
|
|
defer plan.Stop()
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
updateConnectCA(t, c)
|
|
|
|
|
|
|
|
// Wait for second wakeup. Note due to the 30s random jitter for this endpoint
|
|
|
|
// this may take up to 30s.
|
|
|
|
<-notifyCh
|
|
|
|
t.Logf("leaf cert regen took %s with jitter", time.Since(start))
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
var lastCert *api.LeafCert
|
|
|
|
{ // Initial fetch, just store the cert and return
|
|
|
|
v := wakeups[0]
|
|
|
|
require.NotNil(t, v)
|
|
|
|
lastCert = v
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Rotation completed.
|
|
|
|
v := wakeups[1]
|
|
|
|
require.NotNil(t, v)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//require.NotEqual(t, lastCert.SerialNumber, v.SerialNumber)
|
|
|
|
require.NotEqual(t, lastCert.CertPEM, v.CertPEM)
|
|
|
|
require.NotEqual(t, lastCert.PrivateKeyPEM, v.PrivateKeyPEM)
|
|
|
|
require.NotEqual(t, lastCert.ModifyIndex, v.ModifyIndex)
|
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
func TestAgentServiceWatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-04-26 16:33:01 +00:00
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
2018-09-27 14:00:51 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
var (
|
|
|
|
wakeups []*api.AgentService
|
|
|
|
notifyCh = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// Register a local agent service
|
2019-04-26 16:33:01 +00:00
|
|
|
reg := &api.AgentServiceRegistration{
|
2018-09-27 14:00:51 +00:00
|
|
|
Name: "web",
|
|
|
|
Port: 8080,
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
client := c
|
2018-09-27 14:00:51 +00:00
|
|
|
agent := client.Agent()
|
|
|
|
err := agent.ServiceRegister(reg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
plan := mustParse(t, `{"type":"agent_service", "service_id":"web"}`)
|
|
|
|
plan.HybridHandler = func(blockParamVal watch.BlockingParamVal, raw interface{}) {
|
|
|
|
if raw == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
2019-04-26 16:33:01 +00:00
|
|
|
v, ok := raw.(*api.AgentService)
|
2018-09-27 14:00:51 +00:00
|
|
|
if !ok || v == nil {
|
|
|
|
return // ignore
|
|
|
|
}
|
2019-09-18 17:07:11 +00:00
|
|
|
wakeups = append(wakeups, v)
|
|
|
|
notifyCh <- struct{}{}
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2019-04-26 16:33:01 +00:00
|
|
|
if err := plan.Run(s.HTTPAddr); err != nil {
|
2018-09-27 14:00:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-09-18 17:07:11 +00:00
|
|
|
defer plan.Stop()
|
2018-09-27 14:00:51 +00:00
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for first wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
{
|
|
|
|
// Change the service definition
|
|
|
|
reg.Port = 9090
|
|
|
|
err := agent.ServiceRegister(reg)
|
|
|
|
require.NoError(t, err)
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 17:07:11 +00:00
|
|
|
// Wait for second wakeup.
|
|
|
|
<-notifyCh
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
plan.Stop()
|
|
|
|
wg.Wait()
|
2019-09-18 17:07:11 +00:00
|
|
|
|
|
|
|
require.Len(t, wakeups, 2)
|
|
|
|
|
|
|
|
{
|
|
|
|
v := wakeups[0]
|
|
|
|
require.Equal(t, 8080, v.Port)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v := wakeups[1]
|
|
|
|
require.Equal(t, 9090, v.Port)
|
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|