From bbb5f4696a695a2f39be3895a18931e58dbc494b Mon Sep 17 00:00:00 2001 From: arnaud briche Date: Mon, 2 Feb 2015 15:30:44 +0700 Subject: [PATCH] - add tests for CheckHTTP with new timeout parameter && CheckType.Timeout parsing --- command/agent/check_test.go | 45 ++++++++++++++++++++++++++++++++++++ command/agent/config_test.go | 38 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/command/agent/check_test.go b/command/agent/check_test.go index 46746d35a3..b55d5a6580 100644 --- a/command/agent/check_test.go +++ b/command/agent/check_test.go @@ -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) + } +} diff --git a/command/agent/config_test.go b/command/agent/config_test.go index ee436dc7d0..3bc0f35370 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -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, + }, + }, }, }