-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagger_test.go
More file actions
215 lines (188 loc) · 5.54 KB
/
tagger_test.go
File metadata and controls
215 lines (188 loc) · 5.54 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
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
package parallel
import (
"errors"
"testing"
)
// Test that an unconfigured tagger writer does not modify the data stream
func TestTaggerEmpty(t *testing.T) {
var buf testBufWriter
wtr := newTagger(&buf, []byte{})
exp := "Line 1\nLine 2\nLine 3\n"
b, e := wtr.Write([]byte(exp))
if b != len(exp) {
t.Error("Write len wrong. Got", b, "expected", len(exp))
}
if e != nil {
t.Error("Unexpected error", e)
}
act := buf.String()
if act != exp {
t.Error("Unexpected modification. \nExp", exp, "\nactual", act)
}
}
// Prepend with a whole line data
func TestTaggerSimple(t *testing.T) {
var buf testBufWriter
wtr := newTagger(&buf, []byte("host1: "))
before := "Line 1\nLine 2\n"
exp := "host1: Line 1\nhost1: Line 2\n"
b, e := wtr.Write([]byte(before))
if b != len(before) {
t.Error("Write len wrong. Got", b, "expected", len(before))
}
if e != nil {
t.Error("Unexpected error", e)
}
act := buf.String()
if act != exp {
t.Error("Unexpected modification. \nExp", exp, "\nactual", act)
}
}
// Test tagger prepend logic with no trailing newline
func TestTaggerNoTrailingNL(t *testing.T) {
var buf testBufWriter
wtr := newTagger(&buf, []byte("host1: "))
before := "Line 1\nXX"
exp := "host1: Line 1\nhost1: XX"
b, e := wtr.Write([]byte(before))
if b != len(before) {
t.Error("Write len wrong. Got", b, "expected", len(before))
}
if e != nil {
t.Error("Unexpected error", e)
}
act := buf.String()
if act != exp {
t.Error("Unexpected modification. \nExp", exp, "\nactual", act)
}
}
// Prepend with partial lines
func TestTaggerPartialWrites(t *testing.T) {
var buf testBufWriter
wtr := newTagger(&buf, []byte("host1: "))
before := []byte("Line 1\nLine2 \nLine 3\nLine 4\n")
exp := "host1: Line 1\nhost1: Line2 \nhost1: Line 3\nhost1: Line 4\n"
for _, one := range before {
b, e := wtr.Write([]byte{one})
if e != nil {
t.Fatal("Unexpected error", e)
}
if b != 1 {
t.Error("Expected a 1 byte write, not", b)
}
}
act := buf.String()
if act != exp {
t.Error("Unexpected modification. \nExp", exp, "\nactual", act)
}
}
// Zero-length writes
func TestTaggerWriteZero(t *testing.T) {
var buf testBufWriter
wtr := newTagger(&buf, []byte("host1: "))
before := []byte("")
exp := ""
b, e := wtr.Write(before)
if e != nil {
t.Fatal("Unexpected error", e)
}
if b != 0 {
t.Error("Expected a zero byte write, not", b)
}
act := buf.String()
if act != exp {
t.Error("Unexpected modification. \nExp", exp, "\nactual", act)
}
}
// Data is zero length
func TestTaggerIOErrorsW0(t *testing.T) {
buf := &testTruncateWriter{}
wtr := newTagger(buf, []byte(""))
b, err := wtr.Write([]byte{})
if b != 0 || err != nil {
t.Error("Expected zero and nil, not", b, err)
}
}
// Tag is zero length so return downstream write results
func TestTaggerIOErrorsW1(t *testing.T) {
buf := &testTruncateWriter{}
buf.append("Error on first write", -1, errors.New("W1 error"))
wtr := newTagger(buf, []byte(""))
b, err := wtr.Write([]byte{'a'})
if b != 1 || err == nil {
t.Error("Expected one and error, not", b)
}
}
// Error with tag pending on first line
func TestTaggerIOErrorsW2(t *testing.T) {
buf := &testTruncateWriter{}
wtr := newTagger(buf, []byte("W2Tag"))
buf.append("Error on first write", 0, errors.New("Tag Error"))
b, err := wtr.Write([]byte{'a', '\n', 'b', '\n'})
if b != 4 { // User bytes written is unaffected
t.Error("Expected 4 bytes written, not", b)
}
if err == nil || err.Error() != "Tag Error" { // But error should be present
t.Error("Expected Tag Error return, not", err)
}
}
// Error on writing of first line
func TestTaggerIOErrorsW3(t *testing.T) {
buf := &testTruncateWriter{}
wtr := newTagger(buf, []byte("W3Tag"))
buf.append("W1 is good", -1, nil)
buf.append("W3 is error", -1, errors.New("W3 error"))
data := []byte{'a', 'b', 'c', 'd', '\n', 'A', 'B', '\n'}
b, err := wtr.Write(data)
if b != len(data) {
t.Error("Expected W3 to return", len(data), "not", b)
}
if err == nil || err.Error() != "W3 error" {
t.Error("Expected 'W3 error', not", err)
}
}
// Error on writing NL between split lines
func TestTaggerIOErrorsW4(t *testing.T) {
buf := &testTruncateWriter{}
wtr := newTagger(buf, []byte("W4Tag"))
buf.append("W2Tag", -1, nil)
buf.append("W3Line 1 'abcd'", -1, nil)
buf.append("W4 NL fails", 0, errors.New("W4 NL failed"))
data := []byte{'a', 'b', 'c', 'd', '\n', 'A', 'B', '\n'}
b, err := wtr.Write(data)
if b != len(data)-1 { // Minus the NL
t.Error("Expected W4 to return", len(data)-1, "not", b)
}
if err == nil || err.Error() != "W4 NL failed" {
t.Error("Expected 'W4 NL failed', not", err)
}
}
// Error on writing tag on last line
func TestTaggerIOErrorsW5(t *testing.T) {
buf := &testTruncateWriter{}
wtr := newTagger(buf, []byte("W5Tag"))
buf.append("W5 Tag fails", 2, errors.New("W5 Tag failed"))
data := []byte{'a', 'b', 'c'} // Last line
b, err := wtr.Write(data)
if b != 3 { // Tag write failed, but data write succeeds
t.Error("Expected W5 to return", 3, "not", b)
}
if err == nil || err.Error() != "W5 Tag failed" {
t.Error("Expected 'W5 Tag failed', not", err)
}
}
// Error on writing data on last line
func TestTaggerIOErrorsW6(t *testing.T) {
buf := &testTruncateWriter{}
wtr := newTagger(buf, []byte("W5Tag"))
buf.append("W5 Tag ok", -1, nil)
buf.append("W6 LL fails", 2, errors.New("W6 LL failed"))
data := []byte{'a', 'b', 'c'} // Last line
b, err := wtr.Write(data)
if b != 2 { // Tag write ok, but data write failed
t.Error("Expected W6 to return", 2, "not", b)
}
if err == nil || err.Error() != "W6 LL failed" {
t.Error("Expected 'W6 LL failed', not", err)
}
}