forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_test.go
172 lines (145 loc) · 4.39 KB
/
logging_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// nolint: goconst // string duplication is for test readability.
package runtime
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/open-policy-agent/opa/logging"
"github.com/open-policy-agent/opa/logging/test"
)
func TestValidateGzipHeader(t *testing.T) {
httpHeader := http.Header{}
httpHeader.Add("Accept", "*/*")
if result, expected := gzipAccepted(httpHeader), false; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
httpHeader.Add("Accept-Encoding", "gzip")
if result, expected := gzipAccepted(httpHeader), true; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
httpHeader.Set("Accept-Encoding", "gzip, deflate, br")
if result, expected := gzipAccepted(httpHeader), true; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
httpHeader.Set("Accept-Encoding", "br;q=1.0, gzip;q=0.8, *;q=0.1")
if result, expected := gzipAccepted(httpHeader), true; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}
func TestValidatePprofUrl(t *testing.T) {
req := http.Request{}
req.URL = &url.URL{Path: "/metrics"}
if result, expected := isPprofEndpoint(&req), false; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
req.URL = &url.URL{Path: "/debug/pprof/"}
if result, expected := isPprofEndpoint(&req), true; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}
func TestValidateMetricsUrl(t *testing.T) {
req := http.Request{}
req.URL = &url.URL{Path: "/metrics"}
if result, expected := isMetricsEndpoint(&req), true; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
req.URL = &url.URL{Path: "/debug/pprof/"}
if result, expected := isMetricsEndpoint(&req), false; result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}
func TestRequestLogging(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
logger := test.New()
logger.SetLevel(logging.Debug)
shutdownSeconds := 1
params := NewParams()
params.Addrs = &[]string{":0"}
params.Logger = logger
params.PprofEnabled = true
params.GracefulShutdownPeriod = shutdownSeconds // arbitrary, must be non-zero
rt, err := NewRuntime(ctx, params)
if err != nil {
t.Fatal(err)
}
initChannel := rt.Manager.ServerInitializedChannel()
go func() {
if err := rt.Serve(ctx); err != nil {
t.Error(err)
}
}()
<-initChannel
tests := []struct {
path string
acceptEncoding string
expected string
}{
{
"/metrics", "gzip", "[compressed payload]",
},
{
"/metrics", "*/*", "HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.", // rest omitted
},
{ // accept-encoding does not matter for "our" handlers -- they don't compress
"/v1/data", "gzip", "{\"result\":{}}",
},
{ // accept-encoding does not matter for pprof: it's always protobuf
"/debug/pprof/cmdline", "*/*", "[binary payload]",
},
}
// execute all the requests
for _, tc := range tests {
rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", tc.path, nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Accept-Encoding", tc.acceptEncoding)
rt.server.Handler.ServeHTTP(rec, req)
if exp, act := http.StatusOK, rec.Result().StatusCode; exp != act {
t.Errorf("GET %s: expected HTTP %d, got %d", tc.path, exp, act)
}
}
cancel()
// check the logs
ents := logger.Entries()
for j, tc := range tests {
i := uint64(j + 1)
found := false
for _, ent := range entriesForReq(ents, i) {
if ent.Message == "Sent response." {
act := ent.Fields["resp_body"].(string)
if !strings.Contains(act, tc.expected) {
t.Errorf("expected %q in resp_body field, got %q", tc.expected, act)
}
found = true
}
}
if !found {
t.Errorf("Expected \"Sent response.\" log for request %d (path %s)", j, tc.path)
}
}
if t.Failed() {
t.Logf("logs: %v", ents)
}
}
func entriesForReq(ents []test.LogEntry, n uint64) []test.LogEntry {
var ret []test.LogEntry
for _, e := range ents {
if r, ok := e.Fields["req_id"]; ok {
if i, ok := r.(uint64); ok {
if i == n {
ret = append(ret, e)
}
}
}
}
return ret
}