-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_test.go
118 lines (102 loc) · 3.74 KB
/
output_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
package jasper
import (
"context"
"path/filepath"
"strings"
"testing"
"github.com/tychoish/fun/assert"
"github.com/tychoish/fun/assert/check"
"github.com/tychoish/jasper/options"
"github.com/tychoish/jasper/testutil"
)
func TestGetInMemoryLogStream(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for procType, makeProc := range map[string]ProcessConstructor{
"Basic": NewBasicProcess,
"Blocking": NewBlockingProcess,
} {
t.Run(procType, func(t *testing.T) {
for testName, testCase := range map[string]func(ctx context.Context, t *testing.T, opts *options.Create, makeProc ProcessConstructor, output string){
"FailsWithNilProcess": func(ctx context.Context, t *testing.T, opts *options.Create, makeProc ProcessConstructor, output string) {
_, err := GetInMemoryLogStream(ctx, nil, 1)
check.Error(t, err)
},
"FailsWithInvalidCount": func(ctx context.Context, t *testing.T, opts *options.Create, makeProc ProcessConstructor, output string) {
proc, err := makeProc(ctx, opts)
assert.NotError(t, err)
_, err = proc.Wait(ctx)
assert.NotError(t, err)
_, err = GetInMemoryLogStream(ctx, proc, 0)
check.Error(t, err)
},
"FailsWithoutInMemoryLogger": func(ctx context.Context, t *testing.T, opts *options.Create, makeProc ProcessConstructor, output string) {
proc, err := makeProc(ctx, opts)
assert.NotError(t, err)
_, err = proc.Wait(ctx)
assert.NotError(t, err)
_, err = GetInMemoryLogStream(ctx, proc, 100)
check.Error(t, err)
},
"SucceedsWithInMemoryLogger": func(ctx context.Context, t *testing.T, opts *options.Create, makeProc ProcessConstructor, output string) {
loggerProducer := &options.InMemoryLoggerOptions{
InMemoryCap: 100,
Base: options.BaseOptions{
Format: options.LogFormatPlain,
},
}
config := &options.LoggerConfig{}
assert.NotError(t, config.Set(loggerProducer))
opts.Output.Loggers = []*options.LoggerConfig{config}
proc, err := makeProc(ctx, opts)
assert.NotError(t, err)
_, err = proc.Wait(ctx)
assert.NotError(t, err)
logs, err := GetInMemoryLogStream(ctx, proc, 100)
check.NotError(t, err)
check.Contains(t, logs, output)
},
"MultipleInMemoryLoggersReturnLogsFromOnlyOne": func(ctx context.Context, t *testing.T, opts *options.Create, makeProc ProcessConstructor, output string) {
config1 := &options.LoggerConfig{}
assert.NotError(t, config1.Set(&options.InMemoryLoggerOptions{
InMemoryCap: 100,
Base: options.BaseOptions{
Format: options.LogFormatPlain,
},
}))
config2 := &options.LoggerConfig{}
assert.NotError(t, config2.Set(&options.InMemoryLoggerOptions{
InMemoryCap: 100,
Base: options.BaseOptions{
Format: options.LogFormatPlain,
},
}))
opts.Output.Loggers = []*options.LoggerConfig{config1, config2}
proc, err := makeProc(ctx, opts)
assert.NotError(t, err)
_, err = proc.Wait(ctx)
assert.NotError(t, err)
logs, err := GetInMemoryLogStream(ctx, proc, 100)
check.NotError(t, err)
check.Contains(t, logs, output)
outputCount := 0
for _, log := range logs {
if strings.Contains(log, output) {
outputCount++
}
}
check.Equal(t, 1, outputCount)
},
// "": func(ctx context.Context, t *testing.T, opts *options.Create, output string) {},
} {
t.Run(testName, func(t *testing.T) {
tctx, tcancel := context.WithTimeout(ctx, testutil.ProcessTestTimeout)
defer tcancel()
output := t.Name() + " " + filepath.Join(procType, testName)
opts := &options.Create{Args: []string{"echo", output}}
testCase(tctx, t, opts, makeProc, output)
})
}
})
}
}