|
|
|
@ -1,8 +1,11 @@
|
|
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
@ -160,3 +163,94 @@ func TestCheckTTL(t *testing.T) {
|
|
|
|
|
t.Fatalf("should be critical %v", mock.state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mockHTTPServer(responseCode int) *httptest.Server {
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(responseCode)
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return httptest.NewServer(mux)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func expectHTTPStatus(t *testing.T, url string, status string) {
|
|
|
|
|
mock := &MockNotify{
|
|
|
|
|
state: make(map[string]string),
|
|
|
|
|
updates: make(map[string]int),
|
|
|
|
|
output: make(map[string]string),
|
|
|
|
|
}
|
|
|
|
|
check := &CheckHTTP{
|
|
|
|
|
Notify: mock,
|
|
|
|
|
CheckID: "foo",
|
|
|
|
|
HTTP: url,
|
|
|
|
|
Interval: 10 * 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 TestCheckHTTPCritical(t *testing.T) {
|
|
|
|
|
// var server *httptest.Server
|
|
|
|
|
|
|
|
|
|
server := mockHTTPServer(150)
|
|
|
|
|
fmt.Println(server.URL)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "critical")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
// 2xx - 1
|
|
|
|
|
server = mockHTTPServer(199)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "critical")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
// 2xx + 1
|
|
|
|
|
server = mockHTTPServer(300)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "critical")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
server = mockHTTPServer(400)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "critical")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
server = mockHTTPServer(500)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "critical")
|
|
|
|
|
server.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCheckHTTPPassing(t *testing.T) {
|
|
|
|
|
var server *httptest.Server
|
|
|
|
|
|
|
|
|
|
server = mockHTTPServer(200)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "passing")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
server = mockHTTPServer(201)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "passing")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
server = mockHTTPServer(250)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "passing")
|
|
|
|
|
server.Close()
|
|
|
|
|
|
|
|
|
|
server = mockHTTPServer(299)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "passing")
|
|
|
|
|
server.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCheckHTTPWarning(t *testing.T) {
|
|
|
|
|
server := mockHTTPServer(429)
|
|
|
|
|
expectHTTPStatus(t, server.URL, "warning")
|
|
|
|
|
server.Close()
|
|
|
|
|
}
|
|
|
|
|