2014-08-21 21:28:16 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifyWatchHandler(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-08-21 21:28:16 +00:00
|
|
|
if err := verifyWatchHandler(nil); err == nil {
|
|
|
|
t.Fatalf("should err")
|
|
|
|
}
|
|
|
|
if err := verifyWatchHandler(123); err == nil {
|
|
|
|
t.Fatalf("should err")
|
|
|
|
}
|
|
|
|
if err := verifyWatchHandler([]string{"foo"}); err == nil {
|
|
|
|
t.Fatalf("should err")
|
|
|
|
}
|
|
|
|
if err := verifyWatchHandler("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeWatchHandler(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-08-21 21:28:16 +00:00
|
|
|
defer os.Remove("handler_out")
|
|
|
|
defer os.Remove("handler_index_out")
|
|
|
|
script := "echo $CONSUL_INDEX >> handler_index_out && cat >> handler_out"
|
2016-10-04 15:36:41 +00:00
|
|
|
handler := makeWatchHandler(os.Stderr, script)
|
2014-08-21 21:28:16 +00:00
|
|
|
handler(100, []string{"foo", "bar", "baz"})
|
|
|
|
raw, err := ioutil.ReadFile("handler_out")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if string(raw) != "[\"foo\",\"bar\",\"baz\"]\n" {
|
|
|
|
t.Fatalf("bad: %s", raw)
|
|
|
|
}
|
|
|
|
raw, err = ioutil.ReadFile("handler_index_out")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if string(raw) != "100\n" {
|
|
|
|
t.Fatalf("bad: %s", raw)
|
|
|
|
}
|
|
|
|
}
|