mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
766 lines
20 KiB
766 lines
20 KiB
11 years ago
|
package agent
|
||
|
|
||
|
import (
|
||
9 years ago
|
"bytes"
|
||
9 years ago
|
"errors"
|
||
10 years ago
|
"fmt"
|
||
8 years ago
|
"io/ioutil"
|
||
11 years ago
|
"log"
|
||
9 years ago
|
"net"
|
||
10 years ago
|
"net/http"
|
||
|
"net/http/httptest"
|
||
11 years ago
|
"os"
|
||
9 years ago
|
"os/exec"
|
||
8 years ago
|
"reflect"
|
||
9 years ago
|
"strings"
|
||
11 years ago
|
"testing"
|
||
|
"time"
|
||
10 years ago
|
|
||
9 years ago
|
docker "github.com/fsouza/go-dockerclient"
|
||
8 years ago
|
"github.com/hashicorp/consul/agent/mock"
|
||
8 years ago
|
"github.com/hashicorp/consul/api"
|
||
8 years ago
|
"github.com/hashicorp/consul/testutil/retry"
|
||
9 years ago
|
"github.com/hashicorp/consul/types"
|
||
11 years ago
|
)
|
||
|
|
||
|
func expectStatus(t *testing.T, script, status string) {
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
11 years ago
|
check := &CheckMonitor{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
11 years ago
|
Script: script,
|
||
11 years ago
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
11 years ago
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
if got, want := notif.Updates("foo"), 2; got < want {
|
||
8 years ago
|
r.Fatalf("got %d updates want at least %d", got, want)
|
||
10 years ago
|
}
|
||
8 years ago
|
if got, want := notif.State("foo"), status; got != want {
|
||
8 years ago
|
r.Fatalf("got state %q want %q", got, want)
|
||
10 years ago
|
}
|
||
8 years ago
|
})
|
||
11 years ago
|
}
|
||
|
|
||
|
func TestCheckMonitor_Passing(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectStatus(t, "exit 0", api.HealthPassing)
|
||
11 years ago
|
}
|
||
|
|
||
|
func TestCheckMonitor_Warning(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectStatus(t, "exit 1", api.HealthWarning)
|
||
11 years ago
|
}
|
||
|
|
||
|
func TestCheckMonitor_Critical(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectStatus(t, "exit 2", api.HealthCritical)
|
||
11 years ago
|
}
|
||
|
|
||
|
func TestCheckMonitor_BadCmd(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectStatus(t, "foobarbaz", api.HealthCritical)
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
func TestCheckMonitor_Timeout(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
9 years ago
|
check := &CheckMonitor{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
9 years ago
|
Script: "sleep 1 && exit 0",
|
||
|
Interval: 10 * time.Millisecond,
|
||
|
Timeout: 5 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
9 years ago
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
8 years ago
|
time.Sleep(150 * time.Millisecond)
|
||
9 years ago
|
|
||
|
// Should have at least 2 updates
|
||
8 years ago
|
if notif.Updates("foo") < 2 {
|
||
|
t.Fatalf("should have at least 2 updates %v", notif.UpdatesMap())
|
||
9 years ago
|
}
|
||
8 years ago
|
if notif.State("foo") != "critical" {
|
||
|
t.Fatalf("should be critical %v", notif.StateMap())
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func TestCheckMonitor_RandomStagger(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
10 years ago
|
check := &CheckMonitor{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
10 years ago
|
Script: "exit 0",
|
||
10 years ago
|
Interval: 25 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
10 years ago
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
10 years ago
|
time.Sleep(50 * time.Millisecond)
|
||
10 years ago
|
|
||
|
// Should have at least 1 update
|
||
8 years ago
|
if notif.Updates("foo") < 1 {
|
||
|
t.Fatalf("should have 1 or more updates %v", notif.UpdatesMap())
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
if notif.State("foo") != api.HealthPassing {
|
||
|
t.Fatalf("should be %v %v", api.HealthPassing, notif.StateMap())
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
func TestCheckMonitor_LimitOutput(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
11 years ago
|
check := &CheckMonitor{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
11 years ago
|
Script: "od -N 81920 /dev/urandom",
|
||
11 years ago
|
Interval: 25 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
11 years ago
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
time.Sleep(50 * time.Millisecond)
|
||
|
|
||
|
// Allow for extra bytes for the truncation message
|
||
8 years ago
|
if len(notif.Output("foo")) > CheckBufSize+100 {
|
||
11 years ago
|
t.Fatalf("output size is too long")
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
func TestCheckTTL(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
11 years ago
|
check := &CheckTTL{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
11 years ago
|
TTL: 100 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
11 years ago
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
11 years ago
|
time.Sleep(50 * time.Millisecond)
|
||
8 years ago
|
check.SetStatus(api.HealthPassing, "test-output")
|
||
11 years ago
|
|
||
8 years ago
|
if notif.Updates("foo") != 1 {
|
||
|
t.Fatalf("should have 1 updates %v", notif.UpdatesMap())
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
if notif.State("foo") != api.HealthPassing {
|
||
|
t.Fatalf("should be passing %v", notif.StateMap())
|
||
11 years ago
|
}
|
||
|
|
||
|
// Ensure we don't fail early
|
||
11 years ago
|
time.Sleep(75 * time.Millisecond)
|
||
8 years ago
|
if notif.Updates("foo") != 1 {
|
||
|
t.Fatalf("should have 1 updates %v", notif.UpdatesMap())
|
||
11 years ago
|
}
|
||
|
|
||
|
// Wait for the TTL to expire
|
||
11 years ago
|
time.Sleep(75 * time.Millisecond)
|
||
11 years ago
|
|
||
8 years ago
|
if notif.Updates("foo") != 2 {
|
||
|
t.Fatalf("should have 2 updates %v", notif.UpdatesMap())
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
if notif.State("foo") != api.HealthCritical {
|
||
|
t.Fatalf("should be critical %v", notif.StateMap())
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if !strings.Contains(notif.Output("foo"), "test-output") {
|
||
|
t.Fatalf("should have retained output %v", notif.OutputMap())
|
||
9 years ago
|
}
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
func TestCheckHTTP(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
10 years ago
|
|
||
8 years ago
|
tests := []struct {
|
||
|
desc string
|
||
|
code int
|
||
|
method string
|
||
|
header http.Header
|
||
|
status string
|
||
|
}{
|
||
|
// passing
|
||
|
{code: 200, status: api.HealthPassing},
|
||
|
{code: 201, status: api.HealthPassing},
|
||
|
{code: 250, status: api.HealthPassing},
|
||
|
{code: 299, status: api.HealthPassing},
|
||
|
|
||
|
// warning
|
||
|
{code: 429, status: api.HealthWarning},
|
||
|
|
||
|
// critical
|
||
|
{code: 150, status: api.HealthCritical},
|
||
|
{code: 199, status: api.HealthCritical},
|
||
|
{code: 300, status: api.HealthCritical},
|
||
|
{code: 400, status: api.HealthCritical},
|
||
|
{code: 500, status: api.HealthCritical},
|
||
|
|
||
|
// custom method
|
||
|
{desc: "custom method GET", code: 200, method: "GET", status: api.HealthPassing},
|
||
8 years ago
|
{desc: "custom method POST", code: 200, header: http.Header{"Content-Length": []string{"0"}}, method: "POST", status: api.HealthPassing},
|
||
8 years ago
|
{desc: "custom method abc", code: 200, method: "abc", status: api.HealthPassing},
|
||
|
|
||
|
// custom header
|
||
|
{desc: "custom header", code: 200, header: http.Header{"A": []string{"b", "c"}}, status: api.HealthPassing},
|
||
|
}
|
||
|
|
||
|
for _, tt := range tests {
|
||
|
desc := tt.desc
|
||
|
if desc == "" {
|
||
|
desc = fmt.Sprintf("code %d -> status %s", tt.code, tt.status)
|
||
|
}
|
||
|
t.Run(desc, func(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
if tt.method != "" && tt.method != r.Method {
|
||
|
w.WriteHeader(999)
|
||
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
expectedHeader := http.Header{
|
||
|
"Accept": []string{"text/plain, text/*, */*"},
|
||
|
"Accept-Encoding": []string{"gzip"},
|
||
|
"Connection": []string{"close"},
|
||
|
"User-Agent": []string{"Consul Health Check"},
|
||
|
}
|
||
|
for k, v := range tt.header {
|
||
|
expectedHeader[k] = v
|
||
|
}
|
||
|
if !reflect.DeepEqual(expectedHeader, r.Header) {
|
||
8 years ago
|
w.WriteHeader(999)
|
||
|
return
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// Body larger than 4k limit
|
||
|
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
|
||
|
w.WriteHeader(tt.code)
|
||
|
w.Write(body)
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
notif := mock.NewNotify()
|
||
|
check := &CheckHTTP{
|
||
|
Notify: notif,
|
||
|
CheckID: types.CheckID("foo"),
|
||
|
HTTP: server.URL,
|
||
|
Method: tt.method,
|
||
|
Header: tt.header,
|
||
|
Interval: 10 * time.Millisecond,
|
||
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
retry.Run(t, func(r *retry.R) {
|
||
|
if got, want := notif.Updates("foo"), 2; got < want {
|
||
|
r.Fatalf("got %d updates want at least %d", got, want)
|
||
|
}
|
||
|
if got, want := notif.State("foo"), tt.status; got != want {
|
||
|
r.Fatalf("got state %q want %q", got, want)
|
||
|
}
|
||
|
// Allow slightly more data than CheckBufSize, for the header
|
||
|
if n := len(notif.Output("foo")); n > (CheckBufSize + 256) {
|
||
|
r.Fatalf("output too long: %d (%d-byte limit)", n, CheckBufSize)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
func TestCheckHTTPTimeout(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
timeout := 5 * time.Millisecond
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||
|
time.Sleep(2 * timeout)
|
||
|
}))
|
||
10 years ago
|
defer server.Close()
|
||
|
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
10 years ago
|
check := &CheckHTTP{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("bar"),
|
||
10 years ago
|
HTTP: server.URL,
|
||
8 years ago
|
Timeout: timeout,
|
||
10 years ago
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
10 years ago
|
}
|
||
|
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
if got, want := notif.Updates("bar"), 2; got < want {
|
||
8 years ago
|
r.Fatalf("got %d updates want at least %d", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
if got, want := notif.State("bar"), api.HealthCritical; got != want {
|
||
8 years ago
|
r.Fatalf("got state %q want %q", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
})
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
func TestCheckHTTP_disablesKeepAlives(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
10 years ago
|
check := &CheckHTTP{
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
10 years ago
|
HTTP: "http://foo.bar/baz",
|
||
|
Interval: 10 * time.Second,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
10 years ago
|
}
|
||
|
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
if !check.httpClient.Transport.(*http.Transport).DisableKeepAlives {
|
||
|
t.Fatalf("should have disabled keepalives")
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func TestCheckHTTP_TLSSkipVerify_defaultFalse(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
check := &CheckHTTP{
|
||
|
CheckID: "foo",
|
||
|
HTTP: "https://foo.bar/baz",
|
||
|
Interval: 10 * time.Second,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
8 years ago
|
}
|
||
|
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
if check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
||
|
t.Fatalf("should default to false")
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func mockTLSHTTPServer(code int) *httptest.Server {
|
||
|
return httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// Body larger than 4k limit
|
||
|
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
|
||
|
w.WriteHeader(code)
|
||
|
w.Write(body)
|
||
|
}))
|
||
|
}
|
||
|
|
||
8 years ago
|
func TestCheckHTTP_TLSSkipVerify_true_pass(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
server := mockTLSHTTPServer(200)
|
||
|
defer server.Close()
|
||
|
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
8 years ago
|
|
||
|
check := &CheckHTTP{
|
||
8 years ago
|
Notify: notif,
|
||
8 years ago
|
CheckID: types.CheckID("skipverify_true"),
|
||
|
HTTP: server.URL,
|
||
|
Interval: 5 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
8 years ago
|
TLSSkipVerify: true,
|
||
|
}
|
||
|
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
8 years ago
|
|
||
8 years ago
|
if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
||
|
t.Fatalf("should be true")
|
||
|
}
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
if got, want := notif.State("skipverify_true"), api.HealthPassing; got != want {
|
||
8 years ago
|
r.Fatalf("got state %q want %q", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
})
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestCheckHTTP_TLSSkipVerify_true_fail(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
server := mockTLSHTTPServer(500)
|
||
|
defer server.Close()
|
||
|
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
8 years ago
|
|
||
|
check := &CheckHTTP{
|
||
8 years ago
|
Notify: notif,
|
||
8 years ago
|
CheckID: types.CheckID("skipverify_true"),
|
||
|
HTTP: server.URL,
|
||
|
Interval: 5 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
8 years ago
|
TLSSkipVerify: true,
|
||
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
||
|
t.Fatalf("should be true")
|
||
|
}
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
if got, want := notif.State("skipverify_true"), api.HealthCritical; got != want {
|
||
8 years ago
|
r.Fatalf("got state %q want %q", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
})
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestCheckHTTP_TLSSkipVerify_false(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
server := mockTLSHTTPServer(200)
|
||
|
defer server.Close()
|
||
|
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
8 years ago
|
|
||
|
check := &CheckHTTP{
|
||
8 years ago
|
Notify: notif,
|
||
8 years ago
|
CheckID: types.CheckID("skipverify_false"),
|
||
|
HTTP: server.URL,
|
||
|
Interval: 100 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
8 years ago
|
TLSSkipVerify: false,
|
||
|
}
|
||
|
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
8 years ago
|
|
||
8 years ago
|
if check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
||
|
t.Fatalf("should be false")
|
||
|
}
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
// This should fail due to an invalid SSL cert
|
||
8 years ago
|
if got, want := notif.State("skipverify_false"), api.HealthCritical; got != want {
|
||
8 years ago
|
r.Fatalf("got state %q want %q", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
if !strings.Contains(notif.Output("skipverify_false"), "certificate signed by unknown authority") {
|
||
|
r.Fatalf("should fail with certificate error %v", notif.OutputMap())
|
||
8 years ago
|
}
|
||
8 years ago
|
})
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
func mockTCPServer(network string) net.Listener {
|
||
|
var (
|
||
|
addr string
|
||
|
)
|
||
|
|
||
|
if network == `tcp6` {
|
||
|
addr = `[::1]:0`
|
||
|
} else {
|
||
|
addr = `127.0.0.1:0`
|
||
|
}
|
||
|
|
||
|
listener, err := net.Listen(network, addr)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
return listener
|
||
|
}
|
||
|
|
||
|
func expectTCPStatus(t *testing.T, tcp string, status string) {
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
9 years ago
|
check := &CheckTCP{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
9 years ago
|
TCP: tcp,
|
||
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
9 years ago
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
8 years ago
|
retry.Run(t, func(r *retry.R) {
|
||
8 years ago
|
if got, want := notif.Updates("foo"), 2; got < want {
|
||
8 years ago
|
r.Fatalf("got %d updates want at least %d", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
if got, want := notif.State("foo"), status; got != want {
|
||
8 years ago
|
r.Fatalf("got state %q want %q", got, want)
|
||
8 years ago
|
}
|
||
8 years ago
|
})
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestCheckTCPCritical(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
9 years ago
|
var (
|
||
|
tcpServer net.Listener
|
||
|
)
|
||
|
|
||
|
tcpServer = mockTCPServer(`tcp`)
|
||
8 years ago
|
expectTCPStatus(t, `127.0.0.1:0`, api.HealthCritical)
|
||
9 years ago
|
tcpServer.Close()
|
||
|
}
|
||
|
|
||
|
func TestCheckTCPPassing(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
9 years ago
|
var (
|
||
|
tcpServer net.Listener
|
||
|
)
|
||
|
|
||
|
tcpServer = mockTCPServer(`tcp`)
|
||
8 years ago
|
expectTCPStatus(t, tcpServer.Addr().String(), api.HealthPassing)
|
||
9 years ago
|
tcpServer.Close()
|
||
|
|
||
|
tcpServer = mockTCPServer(`tcp6`)
|
||
8 years ago
|
expectTCPStatus(t, tcpServer.Addr().String(), api.HealthPassing)
|
||
9 years ago
|
tcpServer.Close()
|
||
|
}
|
||
9 years ago
|
|
||
|
// A fake docker client to test happy path scenario
|
||
|
type fakeDockerClientWithNoErrors struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithNoErrors) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return &docker.Exec{ID: "123"}, nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithNoErrors) StartExec(id string, opts docker.StartExecOptions) error {
|
||
9 years ago
|
fmt.Fprint(opts.OutputStream, "output")
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithNoErrors) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return &docker.ExecInspect{
|
||
|
ID: "123",
|
||
|
ExitCode: 0,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// A fake docker client to test truncation of output
|
||
|
type fakeDockerClientWithLongOutput struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithLongOutput) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return &docker.Exec{ID: "123"}, nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithLongOutput) StartExec(id string, opts docker.StartExecOptions) error {
|
||
|
b, _ := exec.Command("od", "-N", "81920", "/dev/urandom").Output()
|
||
|
fmt.Fprint(opts.OutputStream, string(b))
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithLongOutput) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return &docker.ExecInspect{
|
||
|
ID: "123",
|
||
|
ExitCode: 0,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// A fake docker client to test non-zero exit codes from exec invocation
|
||
|
type fakeDockerClientWithExecNonZeroExitCode struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecNonZeroExitCode) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return &docker.Exec{ID: "123"}, nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecNonZeroExitCode) StartExec(id string, opts docker.StartExecOptions) error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecNonZeroExitCode) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return &docker.ExecInspect{
|
||
|
ID: "123",
|
||
|
ExitCode: 127,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// A fake docker client to test exit code which result into Warning
|
||
|
type fakeDockerClientWithExecExitCodeOne struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecExitCodeOne) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return &docker.Exec{ID: "123"}, nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecExitCodeOne) StartExec(id string, opts docker.StartExecOptions) error {
|
||
9 years ago
|
fmt.Fprint(opts.OutputStream, "output")
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecExitCodeOne) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return &docker.ExecInspect{
|
||
|
ID: "123",
|
||
|
ExitCode: 1,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// A fake docker client to simulate create exec failing
|
||
|
type fakeDockerClientWithCreateExecFailure struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithCreateExecFailure) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return nil, errors.New("Exec Creation Failed")
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithCreateExecFailure) StartExec(id string, opts docker.StartExecOptions) error {
|
||
|
return errors.New("Exec doesn't exist")
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithCreateExecFailure) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return nil, errors.New("Exec doesn't exist")
|
||
|
}
|
||
|
|
||
|
// A fake docker client to simulate start exec failing
|
||
|
type fakeDockerClientWithStartExecFailure struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithStartExecFailure) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return &docker.Exec{ID: "123"}, nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithStartExecFailure) StartExec(id string, opts docker.StartExecOptions) error {
|
||
9 years ago
|
return errors.New("Couldn't Start Exec")
|
||
9 years ago
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithStartExecFailure) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return nil, errors.New("Exec doesn't exist")
|
||
|
}
|
||
|
|
||
|
// A fake docker client to test exec info query failures
|
||
|
type fakeDockerClientWithExecInfoErrors struct {
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecInfoErrors) CreateExec(opts docker.CreateExecOptions) (*docker.Exec, error) {
|
||
|
return &docker.Exec{ID: "123"}, nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecInfoErrors) StartExec(id string, opts docker.StartExecOptions) error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *fakeDockerClientWithExecInfoErrors) InspectExec(id string) (*docker.ExecInspect, error) {
|
||
|
return nil, errors.New("Unable to query exec info")
|
||
|
}
|
||
|
|
||
9 years ago
|
func expectDockerCheckStatus(t *testing.T, dockerClient DockerClient, status string, output string) {
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
9 years ago
|
check := &CheckDocker{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
9 years ago
|
Script: "/health.sh",
|
||
9 years ago
|
DockerContainerID: "54432bad1fc7",
|
||
9 years ago
|
Shell: "/bin/sh",
|
||
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
9 years ago
|
dockerClient: dockerClient,
|
||
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
time.Sleep(50 * time.Millisecond)
|
||
|
|
||
|
// Should have at least 2 updates
|
||
8 years ago
|
if notif.Updates("foo") < 2 {
|
||
|
t.Fatalf("should have 2 updates %v", notif.UpdatesMap())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if notif.State("foo") != status {
|
||
|
t.Fatalf("should be %v %v", status, notif.StateMap())
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if notif.Output("foo") != output {
|
||
|
t.Fatalf("should be %v %v", output, notif.OutputMap())
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestDockerCheckWhenExecReturnsSuccessExitCode(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectDockerCheckStatus(t, &fakeDockerClientWithNoErrors{}, api.HealthPassing, "output")
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestDockerCheckWhenExecCreationFails(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectDockerCheckStatus(t, &fakeDockerClientWithCreateExecFailure{}, api.HealthCritical, "Unable to create Exec, error: Exec Creation Failed")
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestDockerCheckWhenExitCodeIsNonZero(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectDockerCheckStatus(t, &fakeDockerClientWithExecNonZeroExitCode{}, api.HealthCritical, "")
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func TestDockerCheckWhenExitCodeIsone(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectDockerCheckStatus(t, &fakeDockerClientWithExecExitCodeOne{}, api.HealthWarning, "output")
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func TestDockerCheckWhenExecStartFails(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectDockerCheckStatus(t, &fakeDockerClientWithStartExecFailure{}, api.HealthCritical, "Unable to start Exec: Couldn't Start Exec")
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestDockerCheckWhenExecInfoFails(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
expectDockerCheckStatus(t, &fakeDockerClientWithExecInfoErrors{}, api.HealthCritical, "Unable to inspect Exec: Unable to query exec info")
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func TestDockerCheckDefaultToSh(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
9 years ago
|
os.Setenv("SHELL", "")
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
9 years ago
|
check := &CheckDocker{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
9 years ago
|
Script: "/health.sh",
|
||
9 years ago
|
DockerContainerID: "54432bad1fc7",
|
||
9 years ago
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
9 years ago
|
dockerClient: &fakeDockerClientWithNoErrors{},
|
||
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
time.Sleep(50 * time.Millisecond)
|
||
|
if check.Shell != "/bin/sh" {
|
||
|
t.Fatalf("Shell should be: %v , actual: %v", "/bin/sh", check.Shell)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestDockerCheckUseShellFromEnv(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
9 years ago
|
os.Setenv("SHELL", "/bin/bash")
|
||
|
check := &CheckDocker{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
9 years ago
|
Script: "/health.sh",
|
||
9 years ago
|
DockerContainerID: "54432bad1fc7",
|
||
9 years ago
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
9 years ago
|
dockerClient: &fakeDockerClientWithNoErrors{},
|
||
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
time.Sleep(50 * time.Millisecond)
|
||
|
if check.Shell != "/bin/bash" {
|
||
|
t.Fatalf("Shell should be: %v , actual: %v", "/bin/bash", check.Shell)
|
||
|
}
|
||
|
os.Setenv("SHELL", "")
|
||
|
}
|
||
|
|
||
9 years ago
|
func TestDockerCheckTruncateOutput(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
notif := mock.NewNotify()
|
||
9 years ago
|
check := &CheckDocker{
|
||
8 years ago
|
Notify: notif,
|
||
9 years ago
|
CheckID: types.CheckID("foo"),
|
||
9 years ago
|
Script: "/health.sh",
|
||
9 years ago
|
DockerContainerID: "54432bad1fc7",
|
||
9 years ago
|
Shell: "/bin/sh",
|
||
|
Interval: 10 * time.Millisecond,
|
||
8 years ago
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
||
9 years ago
|
dockerClient: &fakeDockerClientWithLongOutput{},
|
||
|
}
|
||
|
check.Start()
|
||
|
defer check.Stop()
|
||
|
|
||
|
time.Sleep(50 * time.Millisecond)
|
||
|
|
||
|
// Allow for extra bytes for the truncation message
|
||
8 years ago
|
if len(notif.Output("foo")) > CheckBufSize+100 {
|
||
9 years ago
|
t.Fatalf("output size is too long")
|
||
|
}
|
||
9 years ago
|
}
|