aboutsummaryrefslogtreecommitdiff
path: root/plugin/health/health_test.go
blob: c49a5d73ded48fe8bf8fe791ba9a132a127a9db9 (plain) (blame)
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
45
46
47
package health

import (
	"fmt"
	"io"
	"net/http"
	"testing"
	"time"
)

func TestHealth(t *testing.T) {
	h := &health{Addr: ":0"}

	if err := h.OnStartup(); err != nil {
		t.Fatalf("Unable to startup the health server: %v", err)
	}
	defer h.OnFinalShutdown()

	address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), "/health")

	response, err := http.Get(address)
	if err != nil {
		t.Fatalf("Unable to query %s: %v", address, err)
	}
	if response.StatusCode != http.StatusOK {
		t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
	}
	content, err := io.ReadAll(response.Body)
	if err != nil {
		t.Fatalf("Unable to get response body from %s: %v", address, err)
	}
	response.Body.Close()

	if string(content) != http.StatusText(http.StatusOK) {
		t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
	}
}

func TestHealthLameduck(t *testing.T) {
	h := &health{Addr: ":0", lameduck: 250 * time.Millisecond}

	if err := h.OnStartup(); err != nil {
		t.Fatalf("Unable to startup the health server: %v", err)
	}

	h.OnFinalShutdown()
}