forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve_test.go
116 lines (100 loc) · 3.02 KB
/
serve_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package goss
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"
"github.com/aelsabbahy/goss/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestServe(t *testing.T) {
t.Parallel()
tests := map[string]struct {
outputFormat string
specFile string
expectedHTTPStatus int
expectedContentType string
}{
"passing-json": {
outputFormat: "json",
specFile: filepath.Join("testdata", "passing.goss.yaml"),
expectedHTTPStatus: http.StatusOK,
expectedContentType: "application/json",
},
"failing-json": {
outputFormat: "json",
specFile: filepath.Join("testdata", "failing.goss.yaml"),
expectedHTTPStatus: http.StatusServiceUnavailable,
expectedContentType: "application/json",
},
"failing-default-output": {
outputFormat: "rspecish",
specFile: filepath.Join("testdata", "failing.goss.yaml"),
expectedHTTPStatus: http.StatusServiceUnavailable,
expectedContentType: "",
},
}
for testName := range tests {
tc := tests[testName]
t.Run(testName, func(t *testing.T) {
var logOutput bytes.Buffer
log.SetOutput(&logOutput)
config, err := util.NewConfig(util.WithSpecFile(tc.specFile), util.WithOutputFormat(tc.outputFormat))
require.NoError(t, err)
t.Logf("Config: %v", config)
hh, err := newHealthHandler(config)
require.NoError(t, err)
req, err := http.NewRequest("GET", config.Endpoint, nil)
if err != nil {
require.NoError(t, err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(hh.ServeHTTP)
handler.ServeHTTP(rr, req)
t.Logf("testName %q log output:\n%s", testName, logOutput.String())
assert.Equal(t, tc.expectedHTTPStatus, rr.Code)
if tc.expectedContentType != "" {
assert.Equal(t, []string{tc.expectedContentType}, rr.HeaderMap["Content-Type"])
}
})
}
}
func TestServeCache(t *testing.T) {
var logOutput bytes.Buffer
log.SetOutput(&logOutput)
const cache = time.Duration(time.Millisecond * 100)
config, err := util.NewConfig(
util.WithSpecFile(filepath.Join("testdata", "passing.goss.yaml")),
util.WithCache(cache),
)
require.NoError(t, err)
t.Logf("Config: %v", config)
hh, err := newHealthHandler(config)
require.NoError(t, err)
req, err := http.NewRequest("GET", config.Endpoint, nil)
if err != nil {
require.NoError(t, err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(hh.ServeHTTP)
t.Run("fresh cache", func(t *testing.T) {
handler.ServeHTTP(rr, req)
assert.Contains(t, logOutput.String(), "Stale cache")
logOutput.Reset()
})
t.Run("immediately re-request, cache should be warm", func(t *testing.T) {
handler.ServeHTTP(rr, req)
assert.NotContains(t, logOutput.String(), "Stale cache")
logOutput.Reset()
})
t.Run("allow cache to expire, cache should be cold", func(t *testing.T) {
time.Sleep(cache + 5*time.Millisecond)
handler.ServeHTTP(rr, req)
assert.Contains(t, logOutput.String(), "Stale cache")
logOutput.Reset()
})
}