mirror of https://github.com/status-im/consul.git
- add tests for CheckHTTP with new timeout parameter && CheckType.Timeout parsing
This commit is contained in:
parent
7f91782478
commit
bbb5f4696a
|
@ -260,3 +260,48 @@ func TestCheckHTTPWarning(t *testing.T) {
|
||||||
expectHTTPStatus(t, server.URL, "warning")
|
expectHTTPStatus(t, server.URL, "warning")
|
||||||
server.Close()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -682,6 +682,16 @@ func TestDecodeConfig_Services(t *testing.T) {
|
||||||
"interval": "30s",
|
"interval": "30s",
|
||||||
"ttl": "60s"
|
"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,
|
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",
|
"script": "/bin/check_redis_tx",
|
||||||
"interval": "1m",
|
"interval": "1m",
|
||||||
"service_id": "redis"
|
"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,
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue