forked from SkySoft-ATM/gorillaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck_test.go
44 lines (34 loc) · 1.07 KB
/
healthcheck_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gorillaz
import (
"fmt"
"net/http"
"testing"
"github.com/gorilla/mux"
)
//TestHealth tests the creation of a prometheus endpoint and the given path
func TestHealth(t *testing.T) {
SetupLogger()
gaz := &Gaz{Router: mux.NewRouter(), isReady: new(int32)}
gaz.InitHealthcheck()
port, shutdown := setupServerHTTP(gaz.Router)
defer shutdown()
baseURL := fmt.Sprintf("http://localhost:%d", port)
okStatus := http.StatusOK
koStatus := http.StatusServiceUnavailable
check(t, "ready is unset", baseURL+"/ready", koStatus)
check(t, "live is set", baseURL+"/live", okStatus)
gaz.SetReady(true)
check(t, "ready is set", baseURL+"/ready", okStatus)
gaz.SetReady(false)
check(t, "ready is set to false", baseURL+"/ready", koStatus)
}
func check(t *testing.T, scenario, url string, expectedStatus int) {
resp, err := http.Get(url)
if err != nil {
t.Errorf("%s: cannot query %s, %+v", scenario, url, err)
}
defer resp.Body.Close()
if resp.StatusCode != expectedStatus {
t.Errorf("%s: expected status %d but got %d", scenario, expectedStatus, resp.StatusCode)
}
}