mirror of https://github.com/hashicorp/consul
test and doc updates
parent
4304e97654
commit
c88cee6b63
|
@ -7047,10 +7047,11 @@ func TestLoad_FullConfig(t *testing.T) {
|
|||
UnixSocketMode: "E8sAwOv4",
|
||||
Watches: []map[string]interface{}{
|
||||
{
|
||||
"type": "key",
|
||||
"datacenter": "GyE6jpeW",
|
||||
"key": "j9lF1Tve",
|
||||
"handler": "90N7S4LN",
|
||||
"type": "key",
|
||||
"datacenter": "GyE6jpeW",
|
||||
"key": "j9lF1Tve",
|
||||
"handler": "90N7S4LN",
|
||||
"alldatacenters": true,
|
||||
},
|
||||
{
|
||||
"type": "keyprefix",
|
||||
|
|
|
@ -929,7 +929,8 @@
|
|||
"type": "key",
|
||||
"datacenter": "GyE6jpeW",
|
||||
"key": "j9lF1Tve",
|
||||
"handler": "90N7S4LN"
|
||||
"handler": "90N7S4LN",
|
||||
"alldatacenters": true
|
||||
},
|
||||
{
|
||||
"type": "keyprefix",
|
||||
|
|
|
@ -168,6 +168,7 @@ OUTER:
|
|||
}
|
||||
|
||||
for _, dc := range dcs {
|
||||
p.Datacenter = dc
|
||||
conf.Address = p.address
|
||||
conf.Datacenter = dc
|
||||
conf.Token = p.Token
|
||||
|
|
|
@ -4,12 +4,14 @@
|
|||
package watch
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
watchFuncFactory["noop"] = noopWatch
|
||||
watchFuncFactory["noopdcs"] = noopWatchAllDatacenters
|
||||
}
|
||||
|
||||
func noopWatch(params map[string]interface{}) (WatcherFunc, error) {
|
||||
|
@ -23,6 +25,20 @@ func noopWatch(params map[string]interface{}) (WatcherFunc, error) {
|
|||
return fn, nil
|
||||
}
|
||||
|
||||
func noopWatchAllDatacenters(params map[string]interface{}) (WatcherFunc, error) {
|
||||
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
||||
var idx WaitIndexVal
|
||||
if p.Datacenter == "" {
|
||||
idx = WaitIndexVal(0)
|
||||
}
|
||||
if i, ok := p.mapLastParamVal[p.Datacenter].(WaitIndexVal); ok {
|
||||
idx = i
|
||||
}
|
||||
return idx + 1, uint64(idx + 1), nil
|
||||
}
|
||||
return fn, nil
|
||||
}
|
||||
|
||||
func mustParse(t *testing.T, q string) *Plan {
|
||||
params := makeParams(t, q)
|
||||
plan, err := Parse(params)
|
||||
|
@ -79,6 +95,54 @@ func TestRun_Stop(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRun_Stop_AllDatacenters(t *testing.T) {
|
||||
t.Parallel()
|
||||
plan := mustParse(t, `{"type":"noopdcs","alldatacenters":true}`)
|
||||
|
||||
var expect uint64 = 1
|
||||
var expectVal = []uint64{1}
|
||||
doneCh := make(chan struct{})
|
||||
plan.Handler = func(idx uint64, val interface{}) {
|
||||
if idx != expect {
|
||||
t.Fatalf("Bad: %d %d", expect, idx)
|
||||
}
|
||||
if !(reflect.TypeOf(val).Kind() == reflect.Slice && val.([]interface{})[0] == expectVal[0]) {
|
||||
t.Fatalf("Bad: %d %d", expect, val)
|
||||
}
|
||||
if expect == 1 {
|
||||
close(doneCh)
|
||||
}
|
||||
expect++
|
||||
}
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
errCh <- plan.Run("127.0.0.1:8500")
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-doneCh:
|
||||
plan.Stop()
|
||||
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatalf("handler never ran")
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatalf("watcher didn't exit")
|
||||
}
|
||||
|
||||
if expect == 1 {
|
||||
t.Fatalf("Bad: %d", expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_Stop_Hybrid(t *testing.T) {
|
||||
t.Parallel()
|
||||
plan := mustParse(t, `{"type":"noop"}`)
|
||||
|
|
|
@ -29,7 +29,7 @@ func TestParseBasic(t *testing.T) {
|
|||
|
||||
func TestParse_exempt(t *testing.T) {
|
||||
t.Parallel()
|
||||
params := makeParams(t, `{"type":"key", "key":"foo", "handler": "foobar"}`)
|
||||
params := makeParams(t, `{"type":"key", "key":"foo", "handler": "foobar","alldatacenters":true}`)
|
||||
p, err := ParseExempt(params, []string{"handler"})
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
|
@ -37,6 +37,9 @@ func TestParse_exempt(t *testing.T) {
|
|||
if p.Type != "key" {
|
||||
t.Fatalf("Bad: %#v", p)
|
||||
}
|
||||
if !p.AllDatacenters {
|
||||
t.Fatalf("Bad: %#v", p)
|
||||
}
|
||||
ex := p.Exempt["handler"]
|
||||
if ex != "foobar" {
|
||||
t.Fatalf("bad: %v", ex)
|
||||
|
|
|
@ -77,11 +77,10 @@ func (f *HTTPFlags) ServerFlags() *flag.FlagSet {
|
|||
"allows for lower latency and higher throughput, but can result in "+
|
||||
"stale data. This option has no effect on non-read operations. The "+
|
||||
"default value is false.")
|
||||
fs.Var(&f.allDatacenters, "allDatacenters",
|
||||
"Specify if query to be made for all datacenters. The default value is false and "+
|
||||
"will default to the datacenter of the queried agent, if datacenter is "+
|
||||
"is not input. If datacenter is provided and allDatacenter is also set true, "+
|
||||
"allDatacenter takes precedence.")
|
||||
fs.Var(&f.allDatacenters, "alldatacenters",
|
||||
"Specify if query to be made on all datacenters. The default value is false."+
|
||||
"If datacenter is provided and alldatacenters is also set true, allDatacenters"+
|
||||
"takes precedence.")
|
||||
return fs
|
||||
}
|
||||
|
||||
|
|
|
@ -158,6 +158,8 @@ are a few global parameters that all watches support:
|
|||
- `token` - Can be provided to override the agent's default ACL token.
|
||||
- `args` - The handler subprocess and arguments to invoke when the data view updates.
|
||||
- `handler` - The handler shell command to invoke when the data view updates.
|
||||
- `alldatacenters` - Can be set to true, to make query on all datacenters. Default value is false.
|
||||
Value provided for `datacenter` is ignored if `allDatacenters` is set to true.
|
||||
|
||||
## Watch Types
|
||||
|
||||
|
|
Loading…
Reference in New Issue