-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
93 lines (75 loc) · 2.08 KB
/
main_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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"os"
"github.com/Sirupsen/logrus"
)
var (
server *httptest.Server
reader io.Reader
)
type FakeCheck struct {
Check
}
func NewFakeCheck() *FakeCheck {
return &FakeCheck{
Check{
name: "FakeCheck",
description: "FakeCheck",
currentStatus: true,
},
}
}
func (c *FakeCheck) eval() bool {
fmt.Println("Evaluating check", c.name)
return true
}
func TestCheckPoller(t *testing.T) {
checkSlice = append(checkSlice, NewFakeCheck())
go checkPoller(checkSlice, 2)
}
func TestHTTP(t *testing.T) {
// Borrowing example from https://elithrar.github.io/article/testing-http-handlers-go/
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/health-check", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(checkState)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `Everything OK`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
func TestParseConfig(t *testing.T) {
os.Setenv("LOG_LEVEL", "DEBUG")
os.Setenv("POLL_INTERVAL", "25")
cfg := parseConfig()
if cfg.logLevel == logrus.DebugLevel {
t.Logf("Found expected value for logLevel")
} else {
t.Errorf("Error: Did not find expected value for logLevel. Expected logrus.DebugLevel")
}
if cfg.pollInterval == 25 {
t.Logf("Found expected value for pollInterval")
} else {
t.Errorf("Did not find expected value for pollInverval")
}
}