forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_test.go
228 lines (176 loc) · 5.61 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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.
package runtime
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"testing"
"github.com/sirupsen/logrus"
)
func TestDropInputParam(t *testing.T) {
// Without other params.
abc := `a.b.c:{"foo":[1,2,3,4]}`
abcEncoded := url.QueryEscape(abc)
uri, err := url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?input=%v`, abcEncoded))
if err != nil {
panic(err)
}
result := dropInputParam(uri)
expected := "/v1/data/foo/bar"
if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
// With other params.
def := `d.e.f:{"bar":{"baz":null}}`
defEncoded := url.QueryEscape(def)
uri, err = url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?input=%v&pretty=true&depth=1&input=%v`, abcEncoded, defEncoded))
if err != nil {
panic(err)
}
result = dropInputParam(uri)
expected = "/v1/data/foo/bar?depth=1&pretty=true"
if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}
func TestPrettyFormatterNoFields(t *testing.T) {
fmtr := prettyFormatter{}
e := logrus.NewEntry(logrus.StandardLogger())
e.Message = "test"
e.Level = logrus.InfoLevel
out, err := fmtr.Format(e)
if err != nil {
t.Fatalf("Unexpected error formatting log entry: %s", err.Error())
}
actualStr := string(out)
expectedLvl := strings.ToUpper(e.Level.String())
if !strings.Contains(actualStr, expectedLvl) {
t.Errorf("Expected log message to have level %s:\n%s", expectedLvl, actualStr)
}
if !strings.Contains(actualStr, "test") {
t.Errorf("Expected log message to have the entry message '%s':\n%s", "test", actualStr)
}
}
func TestPrettyFormatterBasicFields(t *testing.T) {
fmtr := prettyFormatter{}
e := logrus.WithFields(logrus.Fields{
"number": 5,
"string": "field_string",
"nil": nil,
"error": errors.New("field_error").Error(),
})
e.Message = "test"
e.Level = logrus.InfoLevel
out, err := fmtr.Format(e)
if err != nil {
t.Fatalf("Unexpected error formatting log entry: %s", err.Error())
}
actualStr := string(out)
expectedLvl := strings.ToUpper(e.Level.String())
if !strings.Contains(actualStr, expectedLvl) {
t.Errorf("Expected log message to have level %s:\n%s", expectedLvl, actualStr)
}
if !strings.Contains(actualStr, "test\n") {
t.Errorf("Expected log message to have the entry message '%s':\n%s", "test", actualStr)
}
if !strings.Contains(actualStr, "number = 5\n") {
t.Errorf("Expected to have the number field in message")
}
if !strings.Contains(actualStr, "string = \"field_string\"\n") {
t.Errorf("Expected to have the string field in message")
}
if !strings.Contains(actualStr, "nil = null\n") {
t.Errorf("Expected to have the nil field in message")
}
if !strings.Contains(actualStr, "error = \"field_error\"\n") {
t.Errorf("Expected to have the nil field in message")
}
expectedLines := 7 // one for the message, 4 fields (one line each), and two trailing \n
actualLines := len(strings.Split(actualStr, "\n"))
if actualLines != expectedLines {
t.Errorf("Expected %d lines in output, found %d\n Output: \n%s\n", expectedLines, actualLines, actualStr)
}
}
func TestPrettyFormatterMultilineStringFields(t *testing.T) {
fmtr := prettyFormatter{}
mlStr := `
package opa.examples
import data.servers
import data.networks
import data.ports
public_servers[server] {
server := servers[_]
server.ports[_] == ports[k].id
ports[k].networks[_] == networks[m].id
networks[m].public == true
}
`
e := logrus.WithFields(logrus.Fields{
"multi_line": mlStr,
})
e.Message = "test"
e.Level = logrus.InfoLevel
out, err := fmtr.Format(e)
if err != nil {
t.Fatalf("Unexpected error formatting log entry: %s", err.Error())
}
actualStr := string(out)
expectedLvl := strings.ToUpper(e.Level.String())
if !strings.Contains(actualStr, expectedLvl) {
t.Errorf("Expected log message to have level %s:\n%s", expectedLvl, actualStr)
}
if !strings.Contains(actualStr, "test") {
t.Errorf("Expected log message to have the entry message '%s':\n%s", "test", actualStr)
}
for _, line := range strings.Split(mlStr, "\n") {
// The lines will get prefixed with some padding but should always
// still have their real newlines, and not be encoded.
expectedStr := line + "\n"
if !strings.Contains(actualStr, expectedStr) {
t.Errorf("Expected to find line in message:\n\n%s\n\nactual:\n\n%s\n", expectedStr, actualStr)
}
}
}
func TestPrettyFormatterMultilineJSONFields(t *testing.T) {
fmtr := prettyFormatter{}
obj := map[string]interface{}{
"a": 123,
"b": nil,
"d": "abc",
"e": map[string]interface{}{
"test": []string{
"aa",
"bb",
"cc",
},
},
}
e := logrus.WithFields(logrus.Fields{
"json_string": obj,
})
e.Message = "test"
e.Level = logrus.InfoLevel
out, err := fmtr.Format(e)
if err != nil {
t.Fatalf("Unexpected error formatting log entry: %s", err.Error())
}
actualStr := string(out)
expectedLvl := strings.ToUpper(e.Level.String())
if !strings.Contains(actualStr, expectedLvl) {
t.Errorf("Expected log message to have level %s:\n%s", expectedLvl, actualStr)
}
if !strings.Contains(actualStr, "test") {
t.Errorf("Expected log message to have the entry message 'test':\n%s", actualStr)
}
expectedJSON, err := json.MarshalIndent(&obj, " ", " ")
if err != nil {
t.Fatalf("Unexpected error")
}
if !strings.Contains(actualStr, string(expectedJSON)) {
t.Errorf("Expected JSON to be formatted and included in message:\n\nExpected:\n%s\n\nActual:\n%s\n\n", string(expectedJSON), actualStr)
}
}