-
Notifications
You must be signed in to change notification settings - Fork 33
/
logs_test.go
219 lines (191 loc) · 5.78 KB
/
logs_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
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
package processlog_test
import (
"bytes"
"context"
"fmt"
"github.com/Flaque/filet"
"github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/core/processlog"
"io"
"os"
"strings"
"testing"
"time"
)
// TestWritePrintFunc tests the writePrintFunc function.
func TestWritePrintFunc(t *testing.T) {
// Create a new pipe for stdout and write some data to it.
stdOutR, stdOutW := io.Pipe()
go func() {
defer func(stdOutW *io.PipeWriter) {
err := stdOutW.Close()
assert.Nil(t, err)
}(stdOutW)
_, err := stdOutW.Write([]byte("stdout data"))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}()
// Create a new pipe for stderr and write some data to it.
stdErrR, stdErrW := io.Pipe()
go func() {
defer func(stdErrW *io.PipeWriter) {
err := stdErrW.Close()
assert.Nil(t, err)
}(stdErrW)
_, err := stdErrW.Write([]byte("stderr data"))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}()
// Create a new buffer for the print function.
var buf bytes.Buffer
// Create a new context with a timeout.
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// Call the writePrintFunc function.
processlog.WritePrintFunc(ctx, func(s []byte) {
buf.WriteString(fmt.Sprintf("%s ", s))
}, time.Second, stdOutR, stdErrR)
// Check that the buffer contains the expected data.
actual := strings.Split(strings.TrimSuffix(buf.String(), " "), " ")
expected := strings.Split("stderr datastdout data", " ")
if len(actual) != len(expected) {
t.Errorf("expected %q, but got %q", expected, buf.String())
}
}
type bufCloser struct {
*bytes.Buffer
}
// Mock.
func (b bufCloser) Close() error {
return nil
}
// TestPipeSingleOutput tests the pipeSingleOutput function.
func TestPipeSingleOutput(t *testing.T) {
// Create a new temporary file.
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer func() {
_ = os.Remove(tmpfile.Name())
}()
// Create a new buffer with some data.
data := "test data"
buf := bufCloser{bytes.NewBufferString(data)}
// Call the pipeSingleOutput function.
err = processlog.PipeSingleOutput(tmpfile, buf)
// Check that the function returned no error.
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Check that the file contains the expected data.
fileData, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if string(fileData) != data {
t.Errorf("expected %q, but got %q", data, string(fileData))
}
}
// TestPipeCombinedOutput tests the pipeCombinedOutput function.
func TestPipeCombinedOutput(t *testing.T) {
// Create a new temporary file.
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
return
}
}(tmpfile.Name())
// Create some mock data.
data1 := "test data 1"
data2 := "test data 2"
data3 := "test data 3"
buf1 := bufCloser{bytes.NewBufferString(data1)}
buf2 := bufCloser{bytes.NewBufferString(data2)}
buf3 := bufCloser{bytes.NewBufferString(data3)}
// Call the pipeCombinedOutput function with the mock data.
err = processlog.PipeCombinedOutput(context.Background(), tmpfile, nil, buf1, buf2, buf3)
// Check that the function returned no error.
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Check that the file contains the expected data.
fileData, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
expectedData := data1 + "\n" + data2 + "\n" + data3 + "\n"
if len(strings.Split(expectedData, "\n")) != len(strings.Split(string(fileData), "\n")) {
t.Errorf("expected %q, but got %q", expectedData, string(fileData))
}
}
// TestStartLogs tests the StartLogs function.
func TestStartLogs(t *testing.T) {
// Create a new temporary directory.
tmpdir, err := os.MkdirTemp("", "example")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer func(path string) {
_ = os.RemoveAll(path)
}(tmpdir)
// Create some mock data.
data1 := "test data 1"
data2 := "test data 2"
data3 := "test data 3"
buf1 := bytes.NewBufferString(data1)
buf2 := bytes.NewBufferString(data2)
acc := bytes.NewBufferString(data3)
// Set up the arguments for the StartLogs function.
logDir := filet.TmpDir(t, tmpdir)
logFileName := "test"
logFrequency := 1 * time.Second
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
args := []processlog.StdStreamLogArgsOption{
processlog.WithStdOut(bufCloser{buf1}),
processlog.WithStdErr(bufCloser{buf2}),
processlog.WithLogDir(logDir),
processlog.WithLogFileName(logFileName),
processlog.WithLogFrequency(logFrequency),
processlog.WithAccumulator(acc),
processlog.WithCtx(ctx),
}
// Call the StartLogs function.
res, err := processlog.StartLogs(args...)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Wait for the log files to be written.
time.Sleep(2 * time.Second)
// Check that the separate stdout file contains the expected data.
stdOutData, err := os.ReadFile(res.StdOutFile())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if string(stdOutData) != data1 {
t.Errorf("expected %q, but got %q", data1+"\n", string(stdOutData))
}
// Check that the separate stderr file contains the expected data.
stdErrData, err := os.ReadFile(res.StdErrFile())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if string(stdErrData) != data2 {
t.Errorf("expected %q, but got %q", data2+"\n", string(stdErrData))
}
// Check that the combined log file contains the expected data.
combinedData, err := os.ReadFile(res.CombinedFile())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !(len(string(combinedData)) >= len(stdErrData)+len(stdOutData)) {
t.Error("failed to combine")
}
}