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"
|
2017-06-06 23:11:56 +00:00
|
|
|
"io/ioutil"
|
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"
|
2017-06-06 23:11:56 +00:00
|
|
|
"reflect"
|
2016-03-03 01:58:01 +00:00
|
|
|
"strings"
|
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"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/mock"
|
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
|
|
|
)
|
|
|
|
|
2017-07-04 10:44:24 +00:00
|
|
|
func TestCheckMonitor(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
script, status string
|
|
|
|
}{
|
|
|
|
{"exit 0", "passing"},
|
|
|
|
{"exit 1", "warning"},
|
|
|
|
{"exit 2", "critical"},
|
|
|
|
{"foobarbaz", "critical"},
|
2014-01-21 02:58:05 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 10:44:24 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.status, func(t *testing.T) {
|
|
|
|
notif := mock.NewNotify()
|
|
|
|
check := &CheckMonitor{
|
|
|
|
Notify: notif,
|
|
|
|
CheckID: types.CheckID("foo"),
|
|
|
|
Script: tt.script,
|
|
|
|
Interval: 25 * 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
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) {
|
2017-07-04 10:44:24 +00:00
|
|
|
// t.Parallel() // timing test. no parallel
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2016-02-26 03:18:20 +00:00
|
|
|
check := &CheckMonitor{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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",
|
2017-07-04 10:44:24 +00:00
|
|
|
Interval: 50 * time.Millisecond,
|
|
|
|
Timeout: 25 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2016-02-26 03:18:20 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
2017-07-04 10:44:24 +00:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2016-02-26 03:18:20 +00:00
|
|
|
|
|
|
|
// Should have at least 2 updates
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Updates("foo") < 2 {
|
|
|
|
t.Fatalf("should have at least 2 updates %v", notif.UpdatesMap())
|
2016-02-26 03:18:20 +00:00
|
|
|
}
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.State("foo") != "critical" {
|
|
|
|
t.Fatalf("should be critical %v", notif.StateMap())
|
2016-02-26 03:18:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:39:11 +00:00
|
|
|
func TestCheckMonitor_RandomStagger(t *testing.T) {
|
2017-07-04 10:44:24 +00:00
|
|
|
// t.Parallel() // timing test. no parallel
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2014-12-18 03:39:11 +00:00
|
|
|
check := &CheckMonitor{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2014-12-18 03:39:11 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
2017-07-04 10:44:24 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2014-12-18 03:39:11 +00:00
|
|
|
|
|
|
|
// Should have at least 1 update
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Updates("foo") < 1 {
|
|
|
|
t.Fatalf("should have 1 or more updates %v", notif.UpdatesMap())
|
2014-12-18 03:39:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.State("foo") != api.HealthPassing {
|
|
|
|
t.Fatalf("should be %v %v", api.HealthPassing, notif.StateMap())
|
2014-12-18 03:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 22:28:56 +00:00
|
|
|
func TestCheckMonitor_LimitOutput(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2014-04-29 22:28:56 +00:00
|
|
|
check := &CheckMonitor{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2014-04-29 22:28:56 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Allow for extra bytes for the truncation message
|
2017-05-22 20:07:40 +00:00
|
|
|
if len(notif.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) {
|
2017-07-04 10:44:24 +00:00
|
|
|
// t.Parallel() // timing test. no parallel
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2014-01-21 03:12:40 +00:00
|
|
|
check := &CheckTTL{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("foo"),
|
2017-07-04 10:44:24 +00:00
|
|
|
TTL: 200 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
2017-07-04 10:44:24 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2017-04-19 23:00:11 +00:00
|
|
|
check.SetStatus(api.HealthPassing, "test-output")
|
2014-01-21 03:12:40 +00:00
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Updates("foo") != 1 {
|
|
|
|
t.Fatalf("should have 1 updates %v", notif.UpdatesMap())
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.State("foo") != api.HealthPassing {
|
|
|
|
t.Fatalf("should be passing %v", notif.StateMap())
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we don't fail early
|
2017-07-04 10:44:24 +00:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Updates("foo") != 1 {
|
|
|
|
t.Fatalf("should have 1 updates %v", notif.UpdatesMap())
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the TTL to expire
|
2017-07-04 10:44:24 +00:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2014-01-21 03:12:40 +00:00
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Updates("foo") != 2 {
|
|
|
|
t.Fatalf("should have 2 updates %v", notif.UpdatesMap())
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.State("foo") != api.HealthCritical {
|
|
|
|
t.Fatalf("should be critical %v", notif.StateMap())
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
2016-03-03 01:58:01 +00:00
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if !strings.Contains(notif.Output("foo"), "test-output") {
|
|
|
|
t.Fatalf("should have retained output %v", notif.OutputMap())
|
2016-03-03 01:58:01 +00:00
|
|
|
}
|
2014-01-21 03:12:40 +00:00
|
|
|
}
|
2015-01-13 00:09:42 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
func TestCheckHTTP(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-02-02 08:30:44 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
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},
|
2017-06-08 17:24:50 +00:00
|
|
|
{desc: "custom method POST", code: 200, header: http.Header{"Content-Length": []string{"0"}}, method: "POST", status: api.HealthPassing},
|
2017-06-06 23:11:56 +00:00
|
|
|
{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},
|
2017-06-29 23:26:08 +00:00
|
|
|
{desc: "host header", code: 200, header: http.Header{"Host": []string{"a"}}, status: api.HealthPassing},
|
2017-06-06 23:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-06-08 17:24:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-06-29 23:26:08 +00:00
|
|
|
|
|
|
|
// the Host header is in r.Host and not in the headers
|
|
|
|
host := expectedHeader.Get("Host")
|
|
|
|
if host != "" && host != r.Host {
|
|
|
|
w.WriteHeader(999)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expectedHeader.Del("Host")
|
|
|
|
|
2017-06-08 17:24:50 +00:00
|
|
|
if !reflect.DeepEqual(expectedHeader, r.Header) {
|
2017-06-06 23:11:56 +00:00
|
|
|
w.WriteHeader(999)
|
|
|
|
return
|
|
|
|
}
|
2017-06-08 17:24:50 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-02-02 08:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckHTTPTimeout(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-06-06 23:11:56 +00:00
|
|
|
timeout := 5 * time.Millisecond
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
|
|
|
time.Sleep(2 * timeout)
|
|
|
|
}))
|
2015-02-02 08:30:44 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2015-02-02 08:30:44 +00:00
|
|
|
check := &CheckHTTP{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
2016-06-06 20:19:31 +00:00
|
|
|
CheckID: types.CheckID("bar"),
|
2015-02-02 08:30:44 +00:00
|
|
|
HTTP: server.URL,
|
2017-06-06 23:11:56 +00:00
|
|
|
Timeout: timeout,
|
2015-02-02 08:30:44 +00:00
|
|
|
Interval: 10 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-02-02 08:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.Updates("bar"), 2; got < want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.State("bar"), api.HealthCritical; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-03-15 20:30:50 +00:00
|
|
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-03-15 20:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-11-03 20:17:30 +00:00
|
|
|
check := &CheckHTTP{
|
|
|
|
CheckID: "foo",
|
|
|
|
HTTP: "https://foo.bar/baz",
|
|
|
|
Interval: 10 * time.Second,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2016-11-03 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
if check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should default to false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 09:01:16 +00:00
|
|
|
func largeBodyHandler(code int) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-06-06 23:11:56 +00:00
|
|
|
// Body larger than 4k limit
|
|
|
|
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write(body)
|
2017-07-04 09:01:16 +00:00
|
|
|
})
|
2017-06-06 23:11:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 20:17:30 +00:00
|
|
|
func TestCheckHTTP_TLSSkipVerify_true_pass(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-07-04 09:01:16 +00:00
|
|
|
server := httptest.NewTLSServer(largeBodyHandler(200))
|
2016-11-03 20:17:30 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2016-11-03 20:17:30 +00:00
|
|
|
|
|
|
|
check := &CheckHTTP{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
2016-11-03 20:17:30 +00:00
|
|
|
CheckID: types.CheckID("skipverify_true"),
|
|
|
|
HTTP: server.URL,
|
2017-07-04 09:01:56 +00:00
|
|
|
Interval: 25 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2016-11-03 20:17:30 +00:00
|
|
|
TLSSkipVerify: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2016-11-05 04:55:55 +00:00
|
|
|
|
2017-07-04 09:01:56 +00:00
|
|
|
// give check some time to execute
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2016-11-03 20:17:30 +00:00
|
|
|
if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should be true")
|
|
|
|
}
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.State("skipverify_true"), api.HealthPassing; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-07-04 09:01:16 +00:00
|
|
|
server := httptest.NewTLSServer(largeBodyHandler(500))
|
2016-11-03 20:17:30 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2016-11-03 20:17:30 +00:00
|
|
|
|
|
|
|
check := &CheckHTTP{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
2016-11-03 20:17:30 +00:00
|
|
|
CheckID: types.CheckID("skipverify_true"),
|
|
|
|
HTTP: server.URL,
|
|
|
|
Interval: 5 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2016-11-03 20:17:30 +00:00
|
|
|
TLSSkipVerify: true,
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Fatalf("should be true")
|
|
|
|
}
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.State("skipverify_true"), api.HealthCritical; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-07-04 09:01:16 +00:00
|
|
|
server := httptest.NewTLSServer(largeBodyHandler(200))
|
2016-11-03 20:17:30 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2016-11-03 20:17:30 +00:00
|
|
|
|
|
|
|
check := &CheckHTTP{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
2016-11-03 20:17:30 +00:00
|
|
|
CheckID: types.CheckID("skipverify_false"),
|
|
|
|
HTTP: server.URL,
|
|
|
|
Interval: 100 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2016-11-03 20:17:30 +00:00
|
|
|
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-05-04 22:52:53 +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-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.State("skipverify_false"), api.HealthCritical; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
2016-11-05 04:55:55 +00:00
|
|
|
}
|
2017-05-22 20:07:40 +00:00
|
|
|
if !strings.Contains(notif.Output("skipverify_false"), "certificate signed by unknown authority") {
|
|
|
|
r.Fatalf("should fail with certificate error %v", notif.OutputMap())
|
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) {
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2015-07-23 11:45:08 +00:00
|
|
|
check := &CheckTCP{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-07-23 11:45:08 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.Updates("foo"), 2; got < want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
2016-10-31 16:59:20 +00:00
|
|
|
}
|
2017-05-22 20:07:40 +00:00
|
|
|
if got, want := notif.State("foo"), status; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-07-23 11:45:08 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-07-23 11:45:08 +00:00
|
|
|
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) {
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2015-10-26 19:59:40 +00:00
|
|
|
check := &CheckDocker{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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",
|
2017-07-04 10:44:24 +00:00
|
|
|
Interval: 25 * time.Millisecond,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-10-26 19:59:40 +00:00
|
|
|
dockerClient: dockerClient,
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
2017-07-04 10:44:24 +00:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2015-10-26 19:59:40 +00:00
|
|
|
|
|
|
|
// Should have at least 2 updates
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Updates("foo") < 2 {
|
|
|
|
t.Fatalf("should have 2 updates %v", notif.UpdatesMap())
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.State("foo") != status {
|
|
|
|
t.Fatalf("should be %v %v", status, notif.StateMap())
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
2015-10-27 01:06:55 +00:00
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
if notif.Output("foo") != output {
|
|
|
|
t.Fatalf("should be %v %v", output, notif.OutputMap())
|
2015-10-27 01:06:55 +00:00
|
|
|
}
|
2015-10-26 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCheckWhenExecReturnsSuccessExitCode(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
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-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
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-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
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-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
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-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
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-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-10-27 03:35:13 +00:00
|
|
|
os.Setenv("SHELL", "")
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2015-10-27 03:21:50 +00:00
|
|
|
check := &CheckDocker{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-10-27 03:21:50 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2015-10-27 03:21:50 +00:00
|
|
|
os.Setenv("SHELL", "/bin/bash")
|
|
|
|
check := &CheckDocker{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-10-27 03:21:50 +00:00
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 20:07:40 +00:00
|
|
|
notif := mock.NewNotify()
|
2015-10-27 01:06:55 +00:00
|
|
|
check := &CheckDocker{
|
2017-05-22 20:07:40 +00:00
|
|
|
Notify: notif,
|
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,
|
2017-06-06 23:11:56 +00:00
|
|
|
Logger: log.New(ioutil.Discard, UniqueID(), log.LstdFlags),
|
2015-10-27 01:06:55 +00:00
|
|
|
dockerClient: &fakeDockerClientWithLongOutput{},
|
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// Allow for extra bytes for the truncation message
|
2017-05-22 20:07:40 +00:00
|
|
|
if len(notif.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
|
|
|
}
|