Repository navigation
Expand file tree
/
Copy pathsyslog_test.go
More file actions
166 lines (142 loc) · 3.7 KB
/
Copy pathsyslog_test.go
File metadata and controls
166 lines (142 loc) · 3.7 KB
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
package syslog
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
)
// mockParser implements syslog.Parser for testing option functions.
type mockParser struct {
listener ParserListener
bestEffort bool
maxMessageLength int
machineOpts []MachineOption
}
func (m *mockParser) Parse(r io.Reader) {}
func (m *mockParser) HasBestEffort() bool { return m.bestEffort }
func (m *mockParser) WithBestEffort() { m.bestEffort = true }
func (m *mockParser) WithListener(f ParserListener) {
m.listener = f
}
func (m *mockParser) WithMaxMessageLength(length int) {
m.maxMessageLength = length
}
func (m *mockParser) WithMachineOptions(opts ...MachineOption) {
m.machineOpts = opts
}
func TestWithListener(t *testing.T) {
called := false
listener := func(r *Result) { called = true }
opt := WithListener(listener)
p := &mockParser{}
opt(p)
assert.NotNil(t, p.listener)
p.listener(&Result{})
assert.True(t, called)
}
func TestWithMaxMessageLength(t *testing.T) {
opt := WithMaxMessageLength(2048)
p := &mockParser{}
opt(p)
assert.Equal(t, 2048, p.maxMessageLength)
}
func TestWithMachineOptions(t *testing.T) {
dummyOpt := func(m Machine) Machine { return m }
opt := WithMachineOptions(dummyOpt)
p := &mockParser{}
opt(p)
assert.Len(t, p.machineOpts, 1)
}
func TestWithBestEffort(t *testing.T) {
opt := WithBestEffort()
p := &mockParser{}
assert.False(t, p.bestEffort)
opt(p)
assert.True(t, p.bestEffort)
}
func TestFacilityLevelFallback(t *testing.T) {
// Facility value 24 is not in FacilityKeywords, triggering the fallback path
f := uint8(24)
b := &Base{Facility: &f}
result := b.FacilityLevel()
assert.NotNil(t, result)
// Value 24 is not in either map, so fallback returns empty string
assert.Equal(t, "", *result)
}
func TestFacilityLevelNil(t *testing.T) {
b := &Base{}
assert.Nil(t, b.FacilityLevel())
}
func TestFacilityLevelValid(t *testing.T) {
f := uint8(0)
b := &Base{Facility: &f}
result := b.FacilityLevel()
assert.NotNil(t, result)
assert.Equal(t, "kern", *result)
}
func TestFacilityMessageNil(t *testing.T) {
b := &Base{}
assert.Nil(t, b.FacilityMessage())
}
func TestFacilityMessageValid(t *testing.T) {
f := uint8(0)
b := &Base{Facility: &f}
result := b.FacilityMessage()
assert.NotNil(t, result)
assert.Equal(t, "kernel messages", *result)
}
func TestSeverityMessageNil(t *testing.T) {
b := &Base{}
assert.Nil(t, b.SeverityMessage())
}
func TestSeverityLevelNil(t *testing.T) {
b := &Base{}
assert.Nil(t, b.SeverityLevel())
}
func TestSeverityShortLevelNil(t *testing.T) {
b := &Base{}
assert.Nil(t, b.SeverityShortLevel())
}
func TestSeverityMessageValid(t *testing.T) {
s := uint8(0)
b := &Base{Severity: &s}
result := b.SeverityMessage()
assert.NotNil(t, result)
}
func TestSeverityLevelValid(t *testing.T) {
s := uint8(0)
b := &Base{Severity: &s}
result := b.SeverityLevel()
assert.NotNil(t, result)
}
func TestSeverityShortLevelValid(t *testing.T) {
s := uint8(0)
b := &Base{Severity: &s}
result := b.SeverityShortLevel()
assert.NotNil(t, result)
}
func TestValid(t *testing.T) {
p := uint8(1)
b := &Base{Priority: &p}
assert.True(t, b.Valid())
b2 := &Base{}
assert.False(t, b2.Valid())
}
func TestComputeFromPriority(t *testing.T) {
b := &Base{}
b.ComputeFromPriority(34)
assert.Equal(t, uint8(4), *b.Facility)
assert.Equal(t, uint8(2), *b.Severity)
assert.Equal(t, uint8(34), *b.Priority)
}
func TestComputeFromPriorityZero(t *testing.T) {
b := &Base{}
b.ComputeFromPriority(0)
assert.Equal(t, uint8(0), *b.Facility)
assert.Equal(t, uint8(0), *b.Severity)
}
func TestComputeFromPriorityMax(t *testing.T) {
b := &Base{}
b.ComputeFromPriority(191)
assert.Equal(t, uint8(23), *b.Facility)
assert.Equal(t, uint8(7), *b.Severity)
}