This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhealthcheck_test.go
102 lines (79 loc) · 2.88 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Spin up a sequins instance and ensure that the healthcheck returns StatusOK
// (200) as expected.
func TestGoodHealthCheck(t *testing.T) {
config := defaultConfig()
dir, err := ioutil.TempDir("", "sequins_test")
if err != nil {
t.Fatalf("can't create temp dir. %q", err)
}
config.LocalStore = dir
config.Source = "test_databases/healthy"
s := localSetup("test_databases/healthy", config)
err = s.init()
require.NoError(t, err, "sequins should initialize without problems")
// Because the version status is set asynchronously, we need to wait a
// few seconds so that it will report its true status as opposed to
// the default (AVAILABLE).
time.Sleep(time.Duration(1) * time.Second)
r := httptest.NewRequest("GET", "http://localhost/healthz", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
// Spin up a sequins instance with a healthy database and an unhealthy database
// and test that the healthcheck returns (200) as expected.
func TestPartialHealthCheck(t *testing.T) {
config := defaultConfig()
dir, err := ioutil.TempDir("", "sequins_test")
if err != nil {
t.Fatalf("can't create temp dir. %q", err)
}
config.LocalStore = dir
config.Source = "test_databases/partially_healthy"
s := localSetup("test_databases/partially_healthy", config)
err = s.init()
require.NoError(t, err, "sequins should initialize without problems")
// Because the version status is set asynchronously, we need to wait a
// few seconds so that it will report its true status as opposed to
// the default (AVAILABLE).
time.Sleep(time.Duration(1) * time.Second)
r := httptest.NewRequest("GET", "http://localhost/healthz", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
// Spin up a sequins instance with a bad database and test that the healthcheck
// returns StatusNotFound (404) as expected.
func TestBadHealthCheck(t *testing.T) {
config := defaultConfig()
dir, err := ioutil.TempDir("", "sequins_test_bad")
if err != nil {
t.Fatalf("can't create temp dir. %q", err)
}
config.LocalStore = dir
config.Source = "test_databases/unhealthy"
s := localSetup("test_databases/unhealthy", config)
err = s.init()
require.NoError(t, err, "sequins should initialize without problems")
// Because the version status is set asynchronously, we need to wait a
// few seconds so that it will report its true status as opposed to
// the default (AVAILABLE).
time.Sleep(time.Duration(1) * time.Second)
r := httptest.NewRequest("GET", "http://localhost/healthz", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
resp := w.Result()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}