consul/command/agent/check_test.go

135 lines
3.0 KiB
Go
Raw Normal View History

2014-01-21 02:58:05 +00:00
package agent
import (
"github.com/hashicorp/consul/consul/structs"
"log"
"os"
"testing"
"time"
)
type MockNotify struct {
state map[string]string
updates map[string]int
output map[string]string
2014-01-21 02:58:05 +00:00
}
func (m *MockNotify) UpdateCheck(id, status, output string) {
2014-01-21 02:58:05 +00:00
m.state[id] = status
old := m.updates[id]
m.updates[id] = old + 1
m.output[id] = output
2014-01-21 02:58:05 +00:00
}
func expectStatus(t *testing.T, script, status string) {
mock := &MockNotify{
state: make(map[string]string),
updates: make(map[string]int),
output: make(map[string]string),
2014-01-21 02:58:05 +00:00
}
check := &CheckMonitor{
Notify: mock,
CheckID: "foo",
Script: script,
Interval: 25 * 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
if mock.updates["foo"] < 2 {
t.Fatalf("should have 2 updates %v", mock.updates)
}
if mock.state["foo"] != status {
t.Fatalf("should be %v %v", status, mock.state)
}
}
func TestCheckMonitor_Passing(t *testing.T) {
expectStatus(t, "exit 0", structs.HealthPassing)
}
func TestCheckMonitor_Warning(t *testing.T) {
expectStatus(t, "exit 1", structs.HealthWarning)
}
func TestCheckMonitor_Critical(t *testing.T) {
expectStatus(t, "exit 2", structs.HealthCritical)
}
func TestCheckMonitor_BadCmd(t *testing.T) {
expectStatus(t, "foobarbaz", structs.HealthCritical)
}
2014-01-21 03:12:40 +00:00
func TestCheckMonitor_LimitOutput(t *testing.T) {
mock := &MockNotify{
state: make(map[string]string),
updates: make(map[string]int),
output: make(map[string]string),
}
check := &CheckMonitor{
Notify: mock,
CheckID: "foo",
Script: "dd if=/dev/urandom bs=8192 count=10",
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
if len(mock.output["foo"]) > CheckBufSize+100 {
t.Fatalf("output size is too long")
}
}
2014-01-21 03:12:40 +00:00
func TestCheckTTL(t *testing.T) {
mock := &MockNotify{
state: make(map[string]string),
updates: make(map[string]int),
output: make(map[string]string),
2014-01-21 03:12:40 +00:00
}
check := &CheckTTL{
Notify: mock,
CheckID: "foo",
TTL: 20 * time.Millisecond,
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
check.Start()
defer check.Stop()
time.Sleep(10 * time.Millisecond)
2014-01-21 19:52:25 +00:00
check.SetStatus(structs.HealthPassing, "")
2014-01-21 03:12:40 +00:00
if mock.updates["foo"] != 1 {
t.Fatalf("should have 1 updates %v", mock.updates)
}
if mock.state["foo"] != structs.HealthPassing {
t.Fatalf("should be passing %v", mock.state)
}
// Ensure we don't fail early
time.Sleep(15 * time.Millisecond)
if mock.updates["foo"] != 1 {
t.Fatalf("should have 1 updates %v", mock.updates)
}
// Wait for the TTL to expire
time.Sleep(15 * time.Millisecond)
if mock.updates["foo"] != 2 {
t.Fatalf("should have 2 updates %v", mock.updates)
}
if mock.state["foo"] != structs.HealthCritical {
t.Fatalf("should be critical %v", mock.state)
}
}