File: logstash_test.go

package info (click to toggle)
golang-github-bshuster-repo-logrus-logstash-hook 0.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 116 kB
  • sloc: makefile: 2
file content (320 lines) | stat: -rw-r--r-- 8,220 bytes parent folder | download | duplicates (2)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package logrustash

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net"
	"reflect"
	"testing"
	"time"

	"github.com/sirupsen/logrus"
)

func TestLegostashHook(t *testing.T) {
	type Expct struct {
		appName          string
		hookOnlyPrefix   string
		alwaysSentFields logrus.Fields
	}
	tt := []struct {
		expected Expct
		initFunc func() (*Hook, error)
	}{
		{Expct{"bla", "", nil}, func() (*Hook, error) {
			return NewHook("udp", "localhost:9999", "bla")
		}},
		{Expct{"bzz", "", nil}, func() (*Hook, error) {
			udpConn, err := net.Dial("udp", "localhost:9999")
			if err != nil {
				return nil, err
			}
			return NewHookWithConn(udpConn, "bzz")
		}},
		{Expct{"blk", "", logrus.Fields{"id": 1}}, func() (*Hook, error) {
			return NewHookWithFields("udp", "localhost:9999", "blk", logrus.Fields{"id": 1})
		}},
		{Expct{"prefix", "-->", logrus.Fields{"id": 1}}, func() (*Hook, error) {
			return NewHookWithFieldsAndPrefix("udp", "localhost:9999", "prefix", logrus.Fields{"id": 1}, "-->")
		}},
		{Expct{"fieldsconn", "", logrus.Fields{"id": 5}}, func() (*Hook, error) {
			udpConn, err := net.Dial("udp", "localhost:9999")
			if err != nil {
				return nil, err
			}
			return NewHookWithFieldsAndConn(udpConn, "fieldsconn", logrus.Fields{"id": 5})
		}},
		{Expct{"zz", "~~>", logrus.Fields{"id": "bal"}}, func() (*Hook, error) {
			udpConn, err := net.Dial("udp", "localhost:9999")
			if err != nil {
				return nil, err
			}
			return NewHookWithFieldsAndConnAndPrefix(udpConn, "zz", logrus.Fields{"id": "bal"}, "~~>")
		}},
	}

	for _, te := range tt {
		h, err := te.initFunc()
		if err != nil {
			t.Error(err)
		}
		if h == nil {
			t.Error("expected hook to be not nil")
		}

		if h.conn == nil {
			t.Error("expected conn to be not nil")
		}
		if h.appName != te.expected.appName {
			t.Errorf("expected appName to be '%s' but got '%s'", te.expected.appName, h.appName)
		}
		if h.alwaysSentFields == nil {
			t.Error("expected alwaysSentFields to be not nil")
		}
		if te.expected.alwaysSentFields != nil && !reflect.DeepEqual(te.expected.alwaysSentFields, h.alwaysSentFields) {
			t.Errorf("expected alwaysSentFields to be '%v' but got '%v'", te.expected.alwaysSentFields, h.alwaysSentFields)
		}
		if h.hookOnlyPrefix != te.expected.hookOnlyPrefix {
			t.Error("expected hookOnlyPrefix to be an empty string")
		}
	}
}

func TestNewFiltering(t *testing.T) {
	type Expct struct {
		prefix  string
		appName string
	}
	tt := []struct {
		expected Expct
		initFunc func() *Hook
	}{
		{Expct{"", ""}, func() *Hook {
			return NewFilterHook()
		}},
		{Expct{"~~~>", ""}, func() *Hook {
			return NewFilterHookWithPrefix("~~~>")
		}},
	}

	for _, te := range tt {
		h := te.initFunc()
		if h.conn != nil {
			t.Error("expected conn to be nil")
		}
		if h.alwaysSentFields == nil {
			t.Error("expected alwaysSentFields to be not nil")
		}
		if h.hookOnlyPrefix != te.expected.prefix {
			t.Errorf("expected prefix to be '%s' but got '%s'", te.expected.prefix, h.hookOnlyPrefix)
		}
	}
}

func TestSettingAttributes(t *testing.T) {
	tt := []struct {
		setFunc   func(*Hook)
		expctFunc func(*Hook) error
	}{
		{func(h *Hook) {
			h.WithPrefix("mprefix1")
		}, func(h *Hook) error {
			if h.hookOnlyPrefix != "mprefix1" {
				return fmt.Errorf("expected hookOnlyPrefix to be '%s' but got '%s'", "mprefix1", h.hookOnlyPrefix)
			}
			return nil
		}},
		{func(h *Hook) {
			h.WithField("name", "muha")
		}, func(h *Hook) error {
			nField := logrus.Fields{"name": "muha"}
			if !reflect.DeepEqual(h.alwaysSentFields, nField) {
				return fmt.Errorf("expected hookOnlyPrefix to be '%s' but got '%s'", nField, h.hookOnlyPrefix)
			}
			return nil
		}},
		{func(h *Hook) {
			h.WithFields(logrus.Fields{"filename": "app.log", "owner": "mick"})
		}, func(h *Hook) error {
			nField := logrus.Fields{"name": "test-me!", "filename": "app.log", "owner": "mick"}
			if !reflect.DeepEqual(h.alwaysSentFields, nField) {
				return fmt.Errorf("expected hookOnlyPrefix to be '%s' but got '%s'", nField, h.hookOnlyPrefix)
			}
			return nil
		}},
	}

	for _, te := range tt {
		hook := NewFilterHook()
		hook.alwaysSentFields = logrus.Fields{"name": "test-me!"}
		te.setFunc(hook)
		if err := te.expctFunc(hook); err != nil {
			t.Error(err)
		}
	}
}

func TestFilterHookOnly(t *testing.T) {
	tt := []struct {
		entry    *logrus.Entry
		prefix   string
		expected logrus.Fields
	}{
		{&logrus.Entry{Data: logrus.Fields{"name": "slimshady"}}, "", logrus.Fields{"name": "slimshady"}},
		{&logrus.Entry{Data: logrus.Fields{"_name": "slimshady", "nick": "blabla"}}, "_", logrus.Fields{"nick": "blabla"}},
	}

	for _, te := range tt {
		hook := NewFilterHookWithPrefix(te.prefix)
		hook.filterHookOnly(te.entry)
		if !reflect.DeepEqual(te.entry.Data, te.expected) {
			t.Errorf("expected entry data to be '%v' but got '%v'", te.expected, te.entry.Data)
		}
	}
}

type AddrMock struct {
}

func (a AddrMock) Network() string {
	return ""
}

func (a AddrMock) String() string {
	return ""
}

type ConnMock struct {
	buff *bytes.Buffer
}

func (c ConnMock) Read(b []byte) (int, error) {
	return c.buff.Read(b)
}

func (c ConnMock) Write(b []byte) (int, error) {
	return c.buff.Write(b)
}

func (c ConnMock) Close() error {
	return nil
}

func (c ConnMock) LocalAddr() net.Addr {
	return AddrMock{}
}

func (c ConnMock) RemoteAddr() net.Addr {
	return AddrMock{}
}

func (c ConnMock) SetDeadline(t time.Time) error {
	return nil
}

func (c ConnMock) SetReadDeadline(t time.Time) error {
	return nil
}

func (c ConnMock) SetWriteDeadline(t time.Time) error {
	return nil
}

func TestFire(t *testing.T) {
	conn := ConnMock{buff: bytes.NewBufferString("")}
	hook := &Hook{
		conn:             conn,
		appName:          "fire_test",
		alwaysSentFields: logrus.Fields{"test-name": "fire-test", "->ignore": "haaa", "override": "no"},
		hookOnlyPrefix:   "->",
	}
	entry := &logrus.Entry{
		Message: "hello world!",
		Data:    logrus.Fields{"override": "yes"},
		Level:   logrus.DebugLevel,
	}
	if err := hook.Fire(entry); err != nil {
		t.Error(err)
	}
	var res map[string]string
	if err := json.NewDecoder(conn.buff).Decode(&res); err != nil {
		t.Error(err)
	}
	expected := map[string]string{
		"@timestamp": "0001-01-01T00:00:00Z",
		"@version":   "1",
		"ignore":     "haaa",
		"level":      "debug",
		"message":    "hello world!",
		"override":   "yes",
		"test-name":  "fire-test",
		"type":       "fire_test",
	}
	if !reflect.DeepEqual(expected, res) {
		t.Errorf("expected message to be '%v' but got '%v'", expected, res)
	}
}

func TestFireFilterHook(t *testing.T) {
	hook := &Hook{
		appName:          "fire_hook_test",
		alwaysSentFields: logrus.Fields{"test-name": "fire-test-hook", "_ignore": "haaa", "override": "no"},
		hookOnlyPrefix:   "_",
	}
	entry := &logrus.Entry{
		Message: "hello world!",
		Data:    logrus.Fields{"override": "yes"},
		Level:   logrus.DebugLevel,
	}
	if err := hook.Fire(entry); err != nil {
		t.Error(err)
	}
	expected := &logrus.Entry{
		Message: "hello world!",
		Data:    logrus.Fields{"test-name": "fire-test-hook", "override": "yes"},
		Level:   logrus.DebugLevel,
	}

	if !reflect.DeepEqual(expected, entry) {
		t.Errorf("expected message to be '%v' but got '%v'", expected, entry)
	}
}

func TestLevels(t *testing.T) {
	hook := &Hook{}
	expected := []logrus.Level{
		logrus.PanicLevel,
		logrus.FatalLevel,
		logrus.ErrorLevel,
		logrus.WarnLevel,
		logrus.InfoLevel,
		logrus.DebugLevel,
	}
	res := hook.Levels()
	if !reflect.DeepEqual(expected, res) {
		t.Errorf("expected levels to be '%v' but got '%v'", expected, res)
	}

}

func TestLogstashTimeStampFormat(t *testing.T) {
	conn := ConnMock{buff: bytes.NewBufferString("")}
	hook := &Hook{
		conn:       conn,
		TimeFormat: time.Kitchen,
	}
	fTime := time.Date(2009, time.November, 10, 3, 4, 0, 0, time.UTC)
	if err := hook.Fire(&logrus.Entry{Time: fTime}); err != nil {
		t.Errorf("expected fire to not return error: %s", err)
	}
	var res map[string]string
	if err := json.NewDecoder(conn.buff).Decode(&res); err != nil {
		t.Error(err)
	}
	if value, ok := res["@timestamp"]; !ok {
		t.Error("expected result to have '@timestamp'")
	} else if value != "3:04AM" {
		t.Errorf("expected time to be '%s' but got '%s'", "3:04AM", value)
	}
}