2014-01-21 02:58:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-04-14 21:28:07 +00:00
|
|
|
"bytes"
|
2015-10-26 19:59:40 +00:00
|
|
|
"errors"
|
2015-01-13 00:09:42 +00:00
|
|
|
"fmt"
|
2014-01-21 02:58:05 +00:00
|
|
|
"log"
|
2015-07-23 11:45:08 +00:00
|
|
|
"net"
|
2015-01-13 00:09:42 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-01-21 02:58:05 +00:00
|
|
|
"os"
|
2015-10-27 01:06:55 +00:00
|
|
|
"os/exec"
|
2016-03-03 01:58:01 +00:00
|
|
|
"strings"
|
2016-12-07 16:26:43 +00:00
|
|
|
"sync"
|
2014-01-21 02:58:05 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-01-24 00:07:20 +00:00
|
|
|
|
2015-10-26 19:59:40 +00:00
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-01-21 02:58:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockNotify struct {
|
2016-06-06 20:19:31 +00:00
|
|
|
state map[types.CheckID]string
|
|
|
|
updates map[types.CheckID]int
|
|
|
|
output map[types.CheckID]string
|
2016-12-07 16:26:43 +00:00
|
|
|
|
|
|
|
// A guard to protect an access to the internal attributes
|
|
|
|
// of the notification mock in order to prevent panics
|
|
|
|
// raised by the race conditions detector.
|
|
|
|
mu sync.RWMutex
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:19:31 +00:00
|
|
|
func (m *MockNotify) UpdateCheck(id types.CheckID, status, output string) {
|
2016-12-07 16:26:43 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2014-01-21 02:58:05 +00:00
|
|
|
m.state[id] = status
|
|
|
|
old := m.updates[id]
|
|
|
|
m.updates[id] = old + 1
|
2014-04-29 22:28:56 +00:00
|
|
|
m.output[id] = output
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
// State returns the state of the specified health-check.
|
|
|
|
func (m *MockNotify) State(id types.CheckID) string {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.state[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates returns the count of updates of the specified health-check.
|
|
|
|
func (m *MockNotify) Updates(id types.CheckID) int {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.updates[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output returns an output string of the specified health-check.
|
|
|
|
func (m *MockNotify) Output(id types.CheckID) string {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.output[id]
|
|
|
|
}
|
|
|
|
|
2014-01-21 02:58:05 +00:00
|
|
|
func expectStatus(t *testing.T, script, status string) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
check := &CheckMonitor{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2014-01-21 02:58:05 +00:00
|
|
|
Script: script,
|
2014-05-09 01:41:10 +00:00
|
|
|
Interval: 10 * time.Millisecond,
|
2014-01-21 02:58:05 +00:00
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
|
|
|
if got, want := mock.Updates("foo"), 2; got < want {
|
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
2015-01-24 00:07:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := mock.State("foo"), status; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2015-01-24 00:07:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckMonitor_Passing(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectStatus(t, "exit 0", api.HealthPassing)
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckMonitor_Warning(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectStatus(t, "exit 1", api.HealthWarning)
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckMonitor_Critical(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectStatus(t, "exit 2", api.HealthCritical)
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckMonitor_BadCmd(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectStatus(t, "foobarbaz", api.HealthCritical)
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
2014-01-21 03:12:40 +00:00
|
|
|
|
2016-02-26 03:18:20 +00:00
|
|
|
func TestCheckMonitor_Timeout(t *testing.T) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2016-02-26 03:18:20 +00:00
|
|
|
}
|
|
|
|
check := &CheckMonitor{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2016-02-26 03:18:20 +00:00
|
|
|
Script: "sleep 1 && exit 0",
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Timeout: 5 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Should have at least 2 updates
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Updates("foo") < 2 {
|
2016-02-26 03:18:20 +00:00
|
|
|
t.Fatalf("should have at least 2 updates %v", mock.updates)
|
|
|
|
}
|
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.State("foo") != "critical" {
|
2016-02-26 03:18:20 +00:00
|
|
|
t.Fatalf("should be critical %v", mock.state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:39:11 +00:00
|
|
|
func TestCheckMonitor_RandomStagger(t *testing.T) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2014-12-18 03:39:11 +00:00
|
|
|
}
|
|
|
|
check := &CheckMonitor{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2014-12-18 03:39:11 +00:00
|
|
|
Script: "exit 0",
|
2014-12-18 14:00:51 +00:00
|
|
|
Interval: 25 * time.Millisecond,
|
2014-12-18 03:39:11 +00:00
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
2014-12-18 14:00:51 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2014-12-18 03:39:11 +00:00
|
|
|
|
|
|
|
// Should have at least 1 update
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Updates("foo") < 1 {
|
2014-12-18 14:00:51 +00:00
|
|
|
t.Fatalf("should have 1 or more updates %v", mock.updates)
|
2014-12-18 03:39:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
if mock.State("foo") != api.HealthPassing {
|
|
|
|
t.Fatalf("should be %v %v", api.HealthPassing, mock.state)
|
2014-12-18 03:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 22:28:56 +00:00
|
|
|
func TestCheckMonitor_LimitOutput(t *testing.T) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2014-04-29 22:28:56 +00:00
|
|
|
}
|
|
|
|
check := &CheckMonitor{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2014-05-26 20:13:56 +00:00
|
|
|
Script: "od -N 81920 /dev/urandom",
|
2014-04-29 22:28:56 +00:00
|
|
|
Interval: 25 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Allow for extra bytes for the truncation message
|
2016-12-07 16:26:43 +00:00
|
|
|
if len(mock.Output("foo")) > CheckBufSize+100 {
|
2014-04-29 22:28:56 +00:00
|
|
|
t.Fatalf("output size is too long")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 03:12:40 +00:00
|
|
|
func TestCheckTTL(t *testing.T) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
|
|
|
check := &CheckTTL{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2014-05-06 18:54:27 +00:00
|
|
|
TTL: 100 * time.Millisecond,
|
2014-01-21 03:12:40 +00:00
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
2014-05-06 18:54:27 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-04-19 23:00:11 +00:00
|
|
|
check.SetStatus(api.HealthPassing, "test-output")
|
2014-01-21 03:12:40 +00:00
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Updates("foo") != 1 {
|
2014-01-21 03:12:40 +00:00
|
|
|
t.Fatalf("should have 1 updates %v", mock.updates)
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
if mock.State("foo") != api.HealthPassing {
|
2014-01-21 03:12:40 +00:00
|
|
|
t.Fatalf("should be passing %v", mock.state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we don't fail early
|
2014-05-06 18:54:27 +00:00
|
|
|
time.Sleep(75 * time.Millisecond)
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Updates("foo") != 1 {
|
2014-01-21 03:12:40 +00:00
|
|
|
t.Fatalf("should have 1 updates %v", mock.updates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the TTL to expire
|
2014-05-06 18:54:27 +00:00
|
|
|
time.Sleep(75 * time.Millisecond)
|
2014-01-21 03:12:40 +00:00
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Updates("foo") != 2 {
|
2014-01-21 03:12:40 +00:00
|
|
|
t.Fatalf("should have 2 updates %v", mock.updates)
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
if mock.State("foo") != api.HealthCritical {
|
2014-01-21 03:12:40 +00:00
|
|
|
t.Fatalf("should be critical %v", mock.state)
|
|
|
|
}
|
2016-03-03 01:58:01 +00:00
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
if !strings.Contains(mock.Output("foo"), "test-output") {
|
2016-03-03 01:58:01 +00:00
|
|
|
t.Fatalf("should have retained output %v", mock.output)
|
|
|
|
}
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
2015-01-13 00:09:42 +00:00
|
|
|
|
|
|
|
func mockHTTPServer(responseCode int) *httptest.Server {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2016-04-14 21:28:07 +00:00
|
|
|
// Body larger than 4k limit
|
|
|
|
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
|
2015-01-13 00:09:42 +00:00
|
|
|
w.WriteHeader(responseCode)
|
2016-04-14 21:28:07 +00:00
|
|
|
w.Write(body)
|
2015-01-13 00:09:42 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
2016-11-03 20:17:30 +00:00
|
|
|
func mockTLSHTTPServer(responseCode int) *httptest.Server {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Body larger than 4k limit
|
|
|
|
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
|
|
|
|
w.WriteHeader(responseCode)
|
|
|
|
w.Write(body)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return httptest.NewTLSServer(mux)
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:09:42 +00:00
|
|
|
func expectHTTPStatus(t *testing.T, url string, status string) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-01-13 00:09:42 +00:00
|
|
|
}
|
|
|
|
check := &CheckHTTP{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-01-13 00:09:42 +00:00
|
|
|
HTTP: url,
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
|
|
|
if got, want := mock.Updates("foo"), 2; got < want {
|
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := mock.State("foo"), status; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
|
|
|
// Allow slightly more data than CheckBufSize, for the header
|
2016-12-07 16:26:43 +00:00
|
|
|
if n := len(mock.Output("foo")); n > (CheckBufSize + 256) {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("output too long: %d (%d-byte limit)", n, CheckBufSize)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-01-13 00:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTPCritical(t *testing.T) {
|
|
|
|
// var server *httptest.Server
|
|
|
|
|
|
|
|
server := mockHTTPServer(150)
|
|
|
|
fmt.Println(server.URL)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthCritical)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
// 2xx - 1
|
|
|
|
server = mockHTTPServer(199)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthCritical)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
// 2xx + 1
|
|
|
|
server = mockHTTPServer(300)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthCritical)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
server = mockHTTPServer(400)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthCritical)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
server = mockHTTPServer(500)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthCritical)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTPPassing(t *testing.T) {
|
|
|
|
var server *httptest.Server
|
|
|
|
|
|
|
|
server = mockHTTPServer(200)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthPassing)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
server = mockHTTPServer(201)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthPassing)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
server = mockHTTPServer(250)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthPassing)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
|
|
|
|
server = mockHTTPServer(299)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthPassing)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTPWarning(t *testing.T) {
|
|
|
|
server := mockHTTPServer(429)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectHTTPStatus(t, server.URL, api.HealthWarning)
|
2015-01-13 00:09:42 +00:00
|
|
|
server.Close()
|
|
|
|
}
|
2015-02-02 08:30:44 +00:00
|
|
|
|
|
|
|
func mockSlowHTTPServer(responseCode int, sleep time.Duration) *httptest.Server {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(sleep)
|
|
|
|
w.WriteHeader(responseCode)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTPTimeout(t *testing.T) {
|
|
|
|
server := mockSlowHTTPServer(200, 10*time.Millisecond)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-02-02 08:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check := &CheckHTTP{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("bar"),
|
2015-02-02 08:30:44 +00:00
|
|
|
HTTP: server.URL,
|
|
|
|
Timeout: 5 * time.Millisecond,
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
|
|
|
if got, want := mock.Updates("bar"), 2; got < want {
|
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := mock.State("bar"), api.HealthCritical; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-02-02 08:30:44 +00:00
|
|
|
}
|
2015-03-15 20:30:50 +00:00
|
|
|
|
|
|
|
func TestCheckHTTP_disablesKeepAlives(t *testing.T) {
|
|
|
|
check := &CheckHTTP{
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-03-15 20:30:50 +00:00
|
|
|
HTTP: "http://foo.bar/baz",
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
if !check.httpClient.Transport.(*http.Transport).DisableKeepAlives {
|
|
|
|
t.Fatalf("should have disabled keepalives")
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 11:45:08 +00:00
|
|
|
|
2016-11-03 20:17:30 +00:00
|
|
|
func TestCheckHTTP_TLSSkipVerify_defaultFalse(t *testing.T) {
|
|
|
|
check := &CheckHTTP{
|
|
|
|
CheckID: "foo",
|
|
|
|
HTTP: "https://foo.bar/baz",
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
if check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should default to false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTP_TLSSkipVerify_true_pass(t *testing.T) {
|
|
|
|
server := mockTLSHTTPServer(200)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
mock := &MockNotify{
|
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
check := &CheckHTTP{
|
|
|
|
Notify: mock,
|
|
|
|
CheckID: types.CheckID("skipverify_true"),
|
|
|
|
HTTP: server.URL,
|
|
|
|
Interval: 5 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
TLSSkipVerify: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2016-11-05 04:55:55 +00:00
|
|
|
|
2016-11-03 20:17:30 +00:00
|
|
|
if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should be true")
|
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
|
|
|
if got, want := mock.state["skipverify_true"], api.HealthPassing; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-11-05 04:55:55 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-11-03 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTP_TLSSkipVerify_true_fail(t *testing.T) {
|
|
|
|
server := mockTLSHTTPServer(500)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
mock := &MockNotify{
|
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
check := &CheckHTTP{
|
|
|
|
Notify: mock,
|
|
|
|
CheckID: types.CheckID("skipverify_true"),
|
|
|
|
HTTP: server.URL,
|
|
|
|
Interval: 5 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
TLSSkipVerify: true,
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should be true")
|
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
|
|
|
if got, want := mock.state["skipverify_true"], api.HealthCritical; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-11-05 04:55:55 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-11-03 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTP_TLSSkipVerify_false(t *testing.T) {
|
|
|
|
server := mockTLSHTTPServer(200)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
mock := &MockNotify{
|
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
check := &CheckHTTP{
|
|
|
|
Notify: mock,
|
|
|
|
CheckID: types.CheckID("skipverify_false"),
|
|
|
|
HTTP: server.URL,
|
|
|
|
Interval: 100 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
TLSSkipVerify: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2016-11-05 04:55:55 +00:00
|
|
|
|
2016-11-03 20:17:30 +00:00
|
|
|
if check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should be false")
|
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2016-11-05 04:55:55 +00:00
|
|
|
// This should fail due to an invalid SSL cert
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := mock.state["skipverify_false"], api.HealthCritical; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-11-05 04:55:55 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(mock.output["skipverify_false"], "certificate signed by unknown authority") {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("should fail with certificate error %v", mock.output)
|
2016-11-05 04:55:55 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-11-03 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 11:45:08 +00:00
|
|
|
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) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-07-23 11:45:08 +00:00
|
|
|
}
|
|
|
|
check := &CheckTCP{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-07-23 11:45:08 +00:00
|
|
|
TCP: tcp,
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
|
|
|
if got, want := mock.Updates("foo"), 2; got < want {
|
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := mock.State("foo"), status; got != want {
|
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-07-23 11:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckTCPCritical(t *testing.T) {
|
|
|
|
var (
|
|
|
|
tcpServer net.Listener
|
|
|
|
)
|
|
|
|
|
|
|
|
tcpServer = mockTCPServer(`tcp`)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectTCPStatus(t, `127.0.0.1:0`, api.HealthCritical)
|
2015-07-23 11:45:08 +00:00
|
|
|
tcpServer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckTCPPassing(t *testing.T) {
|
|
|
|
var (
|
|
|
|
tcpServer net.Listener
|
|
|
|
)
|
|
|
|
|
|
|
|
tcpServer = mockTCPServer(`tcp`)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectTCPStatus(t, tcpServer.Addr().String(), api.HealthPassing)
|
2015-07-23 11:45:08 +00:00
|
|
|
tcpServer.Close()
|
|
|
|
|
|
|
|
tcpServer = mockTCPServer(`tcp6`)
|
2017-04-19 23:00:11 +00:00
|
|
|
expectTCPStatus(t, tcpServer.Addr().String(), api.HealthPassing)
|
2015-07-23 11:45:08 +00:00
|
|
|
tcpServer.Close()
|
|
|
|
}
|
2015-10-26 19:59:40 +00:00
|
|
|
|
|
|
|
// 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 {
|
2015-10-27 01:06:55 +00:00
|
|
|
fmt.Fprint(opts.OutputStream, "output")
|
2015-10-26 19:59:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *fakeDockerClientWithNoErrors) InspectExec(id string) (*docker.ExecInspect, error) {
|
|
|
|
return &docker.ExecInspect{
|
|
|
|
ID: "123",
|
|
|
|
ExitCode: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-10-27 01:06:55 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:59:40 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:58:09 +00:00
|
|
|
// 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 {
|
2015-10-27 01:06:55 +00:00
|
|
|
fmt.Fprint(opts.OutputStream, "output")
|
2015-10-26 23:58:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *fakeDockerClientWithExecExitCodeOne) InspectExec(id string) (*docker.ExecInspect, error) {
|
|
|
|
return &docker.ExecInspect{
|
|
|
|
ID: "123",
|
|
|
|
ExitCode: 1,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:59:40 +00:00
|
|
|
// 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 {
|
2015-10-26 23:54:51 +00:00
|
|
|
return errors.New("Couldn't Start Exec")
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-10-27 01:20:02 +00:00
|
|
|
func expectDockerCheckStatus(t *testing.T, dockerClient DockerClient, status string, output string) {
|
2015-10-26 19:59:40 +00:00
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
check := &CheckDocker{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-10-26 19:59:40 +00:00
|
|
|
Script: "/health.sh",
|
2015-11-18 15:40:02 +00:00
|
|
|
DockerContainerID: "54432bad1fc7",
|
2015-10-26 19:59:40 +00:00
|
|
|
Shell: "/bin/sh",
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
dockerClient: dockerClient,
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Should have at least 2 updates
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Updates("foo") < 2 {
|
2015-10-26 19:59:40 +00:00
|
|
|
t.Fatalf("should have 2 updates %v", mock.updates)
|
|
|
|
}
|
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.State("foo") != status {
|
2015-10-26 19:59:40 +00:00
|
|
|
t.Fatalf("should be %v %v", status, mock.state)
|
|
|
|
}
|
2015-10-27 01:06:55 +00:00
|
|
|
|
2016-12-07 16:26:43 +00:00
|
|
|
if mock.Output("foo") != output {
|
2015-10-27 01:20:02 +00:00
|
|
|
t.Fatalf("should be %v %v", output, mock.output)
|
2015-10-27 01:06:55 +00:00
|
|
|
}
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCheckWhenExecReturnsSuccessExitCode(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectDockerCheckStatus(t, &fakeDockerClientWithNoErrors{}, api.HealthPassing, "output")
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCheckWhenExecCreationFails(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectDockerCheckStatus(t, &fakeDockerClientWithCreateExecFailure{}, api.HealthCritical, "Unable to create Exec, error: Exec Creation Failed")
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCheckWhenExitCodeIsNonZero(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectDockerCheckStatus(t, &fakeDockerClientWithExecNonZeroExitCode{}, api.HealthCritical, "")
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 23:58:09 +00:00
|
|
|
func TestDockerCheckWhenExitCodeIsone(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectDockerCheckStatus(t, &fakeDockerClientWithExecExitCodeOne{}, api.HealthWarning, "output")
|
2015-10-26 23:58:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 19:59:40 +00:00
|
|
|
func TestDockerCheckWhenExecStartFails(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectDockerCheckStatus(t, &fakeDockerClientWithStartExecFailure{}, api.HealthCritical, "Unable to start Exec: Couldn't Start Exec")
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCheckWhenExecInfoFails(t *testing.T) {
|
2017-04-19 23:00:11 +00:00
|
|
|
expectDockerCheckStatus(t, &fakeDockerClientWithExecInfoErrors{}, api.HealthCritical, "Unable to inspect Exec: Unable to query exec info")
|
2015-10-27 01:06:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 03:21:50 +00:00
|
|
|
func TestDockerCheckDefaultToSh(t *testing.T) {
|
2015-10-27 03:35:13 +00:00
|
|
|
os.Setenv("SHELL", "")
|
2015-10-27 03:21:50 +00:00
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-10-27 03:21:50 +00:00
|
|
|
}
|
|
|
|
check := &CheckDocker{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-10-27 03:21:50 +00:00
|
|
|
Script: "/health.sh",
|
2015-11-18 15:40:02 +00:00
|
|
|
DockerContainerID: "54432bad1fc7",
|
2015-10-27 03:21:50 +00:00
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
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) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-10-27 03:21:50 +00:00
|
|
|
}
|
|
|
|
os.Setenv("SHELL", "/bin/bash")
|
|
|
|
check := &CheckDocker{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-10-27 03:21:50 +00:00
|
|
|
Script: "/health.sh",
|
2015-11-18 15:40:02 +00:00
|
|
|
DockerContainerID: "54432bad1fc7",
|
2015-10-27 03:21:50 +00:00
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
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", "")
|
|
|
|
}
|
|
|
|
|
2015-10-27 01:06:55 +00:00
|
|
|
func TestDockerCheckTruncateOutput(t *testing.T) {
|
|
|
|
mock := &MockNotify{
|
2016-06-06 20:19:31 +00:00
|
|
|
state: make(map[types.CheckID]string),
|
|
|
|
updates: make(map[types.CheckID]int),
|
|
|
|
output: make(map[types.CheckID]string),
|
2015-10-27 01:06:55 +00:00
|
|
|
}
|
|
|
|
check := &CheckDocker{
|
|
|
|
Notify: mock,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2015-10-27 01:06:55 +00:00
|
|
|
Script: "/health.sh",
|
2015-11-18 15:40:02 +00:00
|
|
|
DockerContainerID: "54432bad1fc7",
|
2015-10-27 01:06:55 +00:00
|
|
|
Shell: "/bin/sh",
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
dockerClient: &fakeDockerClientWithLongOutput{},
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Allow for extra bytes for the truncation message
|
2016-12-07 16:26:43 +00:00
|
|
|
if len(mock.Output("foo")) > CheckBufSize+100 {
|
2015-10-27 01:06:55 +00:00
|
|
|
t.Fatalf("output size is too long")
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|