- add tests for CheckHTTP with new timeout parameter && CheckType.Timeout parsing

This commit is contained in:
arnaud briche 2015-02-02 15:30:44 +07:00
parent de96094cb0
commit 49a9836be3
2 changed files with 83 additions and 0 deletions

View File

@ -260,3 +260,48 @@ func TestCheckHTTPWarning(t *testing.T) {
expectHTTPStatus(t, server.URL, "warning")
server.Close()
}
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{
state: make(map[string]string),
updates: make(map[string]int),
output: make(map[string]string),
}
check := &CheckHTTP{
Notify: mock,
CheckID: "bar",
HTTP: server.URL,
Timeout: 5 * time.Millisecond,
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["bar"] < 2 {
t.Fatalf("should have at least 2 updates %v", mock.updates)
}
if mock.state["bar"] != "critical" {
t.Fatalf("should be critical %v", mock.state)
}
}

View File

@ -682,6 +682,16 @@ func TestDecodeConfig_Services(t *testing.T) {
"interval": "30s",
"ttl": "60s"
}
},
{
"id": "es0",
"name": "elasticsearch",
port: "9200",
"check": {
"HTTP": "http://localhost:9200/_cluster/health",
"interval": "10s",
"timeout": "100ms"
}
}
]
}`
@ -730,6 +740,16 @@ func TestDecodeConfig_Services(t *testing.T) {
},
Port: 7000,
},
&ServiceDefinition{
Check: CheckType{
HTTP: "http://localhost:9200/_cluster_health",
Interval: 10 * time.Second,
Timeout: 100 * time.Millisecond,
},
ID: "es0",
Name: "elasticsearch",
Port: 9200,
},
},
}
@ -759,6 +779,14 @@ func TestDecodeConfig_Checks(t *testing.T) {
"script": "/bin/check_redis_tx",
"interval": "1m",
"service_id": "redis"
},
{
"id": "chk4",
"name": "service:elasticsearch:health",
"HTTP": "http://localhost:9200/_cluster/health",
"interval": "10s",
"timeout": "100ms"
"service_id": "elasticsearch"
}
]
}`
@ -795,6 +823,16 @@ func TestDecodeConfig_Checks(t *testing.T) {
Interval: time.Minute,
},
},
&CheckDefinition{
ID: "chk4",
Name: "service:elasticsearch:health",
ServiceID: "elasticsearch",
CheckType: CheckType{
HTTP: "http://localhost:9200/_cluster_health",
Interval: 10 * time.Second,
Timeout: 100 * time.Millisecond,
},
},
},
}