-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_test.go
189 lines (175 loc) · 4.21 KB
/
client_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
package splunk
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"sync"
"testing"
"time"
)
func decodeBody(t *testing.T, r *http.Request) map[string]any {
m := map[string]any{}
err := json.NewDecoder(r.Body).Decode(&m)
if err != nil {
t.Error(err)
}
m["time"] = 0
return m
}
func TestSplunkLoggerRetry(t *testing.T) {
var internalErrorOnce sync.Once
ch := make(chan bool)
time.AfterFunc(time.Second*8, func() {
ch <- false
})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
internalErrorOnce.Do(func() {
// make sure the logger retries requests
w.WriteHeader(http.StatusInternalServerError)
})
if r.Header.Get("Authorization") != "Splunk token" {
t.Errorf("got %v, want Splunk token", r.Header.Get("Authorization"))
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("got %v, want application/json", r.Header.Get("Content-Type"))
}
m := decodeBody(t, r)
want := map[string]any{
"time": 0,
"host": "hostname",
"event": map[string]any{
"message": map[string]any{},
"ident": "source",
"host": "hostname",
},
}
if !reflect.DeepEqual(want, m) {
t.Errorf("got %v, want %v", m, want)
}
ch <- true
}))
sl := newSplunkLogger(context.Background(), srv.URL, "token", "source", "hostname", 0)
_, err := sl.event([]byte("{}\n"))
if err != nil {
t.Error(err)
}
defer sl.close()
if !<-ch {
t.Error("timeout")
}
}
func TestSplunkLoggerContext(t *testing.T) {
ch := make(chan bool)
time.AfterFunc(time.Second*10, func() {
ch <- false
})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Splunk token" {
t.Errorf("got %v, want Splunk token", r.Header.Get("Authorization"))
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("got %v, want application/json", r.Header.Get("Content-Type"))
}
m := decodeBody(t, r)
want := map[string]any{
"time": 0,
"host": "hostname",
"event": map[string]any{
"message": map[string]any{},
"ident": "source",
"host": "hostname",
},
}
if !reflect.DeepEqual(want, m) {
t.Errorf("got %v, want %v", m, want)
}
ch <- true
}))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()
sl := newSplunkLogger(ctx, srv.URL, "token", "source", "hostname", 0)
_, err := sl.event([]byte("{}\n"))
if err != nil {
t.Error(err)
}
if !<-ch {
t.Error("timeout")
}
}
func TestSplunkLoggerPayloads(t *testing.T) {
var url string
tests := []struct {
name string
f func() error
want map[string]any
}{
{
name: "empty",
f: func() error {
sl := newSplunkLogger(context.Background(), url, "token", "source", "hostname", 0)
defer sl.close()
_, err := sl.event([]byte("{}\n"))
if err != nil {
return err
}
return nil
},
want: map[string]any{
"time": 0,
"host": "hostname",
"event": map[string]any{
"message": map[string]any{},
"ident": "source",
"host": "hostname",
},
},
},
{
name: "json",
f: func() error {
sl := newSplunkLogger(context.Background(), url, "token", "source", "hostname", 0)
defer sl.close()
_, err := sl.event([]byte(`{"a": "b"}` + "\n"))
if err != nil {
return err
}
return nil
},
want: map[string]any{
"time": 0,
"host": "hostname",
"event": map[string]any{
"ident": "source",
"host": "hostname",
"message": map[string]any{
"a": "b",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Splunk token" {
t.Errorf("got %v, want Splunk token", r.Header.Get("Authorization"))
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("got %v, want application/json", r.Header.Get("Content-Type"))
}
m := decodeBody(t, r)
if !reflect.DeepEqual(tt.want, m) {
t.Errorf("got %+v, want %+v", m, tt.want)
}
}))
url = srv.URL
defer srv.Close()
err := tt.f()
if err != nil {
t.Error(err)
}
})
}
}