File: logstash_formatter_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 (73 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download
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
package logrustash

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/url"
	"testing"

	"github.com/sirupsen/logrus"
)

func TestLogstashFormatter(t *testing.T) {
t.Skip("DM-Skipped")
	lf := LogstashFormatter{Type: "abc"}

	fields := logrus.Fields{
		"message": "def",
		"level":   "ijk",
		"type":    "lmn",
		"one":     1,
		"pi":      3.14,
		"bool":    true,
		"error":   &url.Error{Op: "Get", URL: "http://example.com", Err: fmt.Errorf("The error")},
	}

	entry := logrus.WithFields(fields)
	entry.Message = "msg"
	entry.Level = logrus.InfoLevel

	b, _ := lf.Format(entry)

	var data map[string]interface{}
	dec := json.NewDecoder(bytes.NewReader(b))
	dec.UseNumber()
	dec.Decode(&data)

	// base fields
	if data["@timestamp"] == "" {
		t.Error("expected @timestamp to be not empty")
	}
	tt := []struct {
		expected string
		key      string
	}{
		// base fields
		{"1", "@version"},
		{"abc", "type"},
		{"msg", "message"},
		{"info", "level"},
		{"Get http://example.com: The error", "error"},
		// substituted fields
		{"def", "fields.message"},
		{"ijk", "fields.level"},
		{"lmn", "fields.type"},
	}
	for _, te := range tt {
		if te.expected != data[te.key] {
			t.Errorf("expected data[%s] to be '%s' but got '%s'", te.key, te.expected, data[te.key])
		}
	}

	// formats
	if json.Number("1") != data["one"] {
		t.Errorf("expected one to be '%v' but got '%v'", json.Number("1"), data["one"])
	}
	if json.Number("3.14") != data["pi"] {
		t.Errorf("expected pi to be '%v' but got '%v'", json.Number("3.14"), data["pi"])
	}
	if true != data["bool"] {
		t.Errorf("expected bool to be '%v' but got '%v'", true, data["bool"])
	}
}