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
220
221
222
223
|
package clients
import (
"context"
"errors"
"testing"
"time"
"github.com/mimecast/dtail/internal/clients/connectors"
"github.com/mimecast/dtail/internal/clients/handlers"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/omode"
sessionspec "github.com/mimecast/dtail/internal/session"
)
func TestParseInteractiveCommandForGrepReload(t *testing.T) {
current := config.Args{
Mode: omode.GrepClient,
What: "/var/log/app.log",
RegexStr: "ERROR",
}
command, err := parseInteractiveCommand(current,
`:reload --grep WARN --before 2 --after 3 --max 4 --invert --files "/tmp/other.log" --plain --quiet --timeout 7`)
if err != nil {
t.Fatalf("parseInteractiveCommand() error = %v", err)
}
if command.kind != "reload" {
t.Fatalf("command kind = %q, want reload", command.kind)
}
if command.next.What != "/tmp/other.log" {
t.Fatalf("files = %q, want /tmp/other.log", command.next.What)
}
if command.next.RegexStr != "WARN" {
t.Fatalf("regex = %q, want WARN", command.next.RegexStr)
}
if !command.next.RegexInvert || !command.next.Plain || !command.next.Quiet {
t.Fatalf("expected invert/plain/quiet flags to be set: %#v", command.next)
}
if command.next.LContext.BeforeContext != 2 || command.next.LContext.AfterContext != 3 || command.next.LContext.MaxCount != 4 {
t.Fatalf("unexpected context values: %#v", command.next.LContext)
}
if command.spec.Regex != "WARN" {
t.Fatalf("session spec regex = %q, want WARN", command.spec.Regex)
}
}
func TestParseInteractiveCommandForMapReloadDerivesRegex(t *testing.T) {
current := config.Args{
Mode: omode.MapClient,
What: "/var/log/app.log",
QueryStr: "select count(status) from stats group by status",
}
command, err := parseInteractiveCommand(current,
`:reload --query "select count(status) from warnings group by status" --files /tmp/new.log --plain --timeout 9`)
if err != nil {
t.Fatalf("parseInteractiveCommand() error = %v", err)
}
if command.kind != "reload" {
t.Fatalf("command kind = %q, want reload", command.kind)
}
if command.next.QueryStr != "select count(status) from warnings group by status" {
t.Fatalf("query = %q", command.next.QueryStr)
}
if command.spec.Regex != "\\|MAPREDUCE:WARNINGS\\|" {
t.Fatalf("session spec regex = %q, want WARNINGS table regex", command.spec.Regex)
}
}
func TestParseInteractiveCommandRejectsUnterminatedQuotes(t *testing.T) {
current := config.Args{
Mode: omode.MapClient,
QueryStr: "select count(status) from stats group by status",
}
if _, err := parseInteractiveCommand(current, `:reload --query "select count(status) from stats`); err == nil {
t.Fatalf("expected parseInteractiveCommand() to reject unterminated quoted input")
}
}
func TestApplyInteractiveReloadRejectsUnsupportedConnections(t *testing.T) {
client := &baseClient{
Args: config.Args{
Mode: omode.GrepClient,
What: "/var/log/app.log",
RegexStr: "ERROR",
},
sessionSpec: SessionSpec{
Mode: omode.GrepClient,
Files: []string{"/var/log/app.log"},
Regex: "ERROR",
},
connections: []connectors.Connector{
&interactiveReloadConnector{server: "srv1", supported: false},
},
}
err := client.applyInteractiveReload(config.Args{
Mode: omode.GrepClient,
What: "/tmp/next.log",
RegexStr: "WARN",
}, SessionSpec{
Mode: omode.GrepClient,
Files: []string{"/tmp/next.log"},
Regex: "WARN",
})
if !errors.Is(err, connectors.ErrSessionUnsupported) {
t.Fatalf("expected ErrSessionUnsupported, got %v", err)
}
if client.Args.What != "/var/log/app.log" || client.sessionSpec.Regex != "ERROR" {
t.Fatalf("client state changed on unsupported reload: args=%#v spec=%#v", client.Args, client.sessionSpec)
}
}
func TestApplyInteractiveReloadCommitsSharedState(t *testing.T) {
connA := &interactiveReloadConnector{server: "srv1", supported: true, generation: 4}
connB := &interactiveReloadConnector{server: "srv2", supported: true, generation: 4}
maker := &interactiveReloadMaker{}
client := &baseClient{
Args: config.Args{
Mode: omode.MapClient,
What: "/var/log/app.log",
QueryStr: "select count(status) from stats group by status",
},
sessionSpec: SessionSpec{
Mode: omode.MapClient,
Files: []string{"/var/log/app.log"},
Query: "select count(status) from stats group by status",
Regex: "\\|MAPREDUCE:STATS\\|",
},
connections: []connectors.Connector{connA, connB},
maker: maker,
}
nextArgs := config.Args{
Mode: omode.MapClient,
What: "/tmp/new.log",
QueryStr: "select count(status) from warnings group by status",
Plain: true,
Timeout: 5,
}
nextSpec := SessionSpec{
Mode: omode.MapClient,
Files: []string{"/tmp/new.log"},
Query: nextArgs.QueryStr,
Regex: "\\|MAPREDUCE:WARNINGS\\|",
Timeout: 5,
}
if err := client.applyInteractiveReload(nextArgs, nextSpec); err != nil {
t.Fatalf("applyInteractiveReload() error = %v", err)
}
if client.Args.What != "/tmp/new.log" || client.sessionSpec.Query != nextArgs.QueryStr {
t.Fatalf("client state not committed: args=%#v spec=%#v", client.Args, client.sessionSpec)
}
if len(maker.commits) != 1 {
t.Fatalf("expected one sessionCommitter call, got %d", len(maker.commits))
}
if maker.commits[0].generation != 4 || maker.commits[0].spec.Query != nextArgs.QueryStr {
t.Fatalf("unexpected commit payload: %#v", maker.commits[0])
}
if connA.appliedSpec.Query != nextArgs.QueryStr || connB.appliedSpec.Query != nextArgs.QueryStr {
t.Fatalf("connectors did not receive new session spec: %#v %#v", connA.appliedSpec, connB.appliedSpec)
}
}
type interactiveReloadConnector struct {
appliedSpec sessionspec.Spec
applyErr error
generation uint64
server string
supported bool
}
func (*interactiveReloadConnector) Start(context.Context, context.CancelFunc, chan struct{}, chan struct{}) {
}
func (c *interactiveReloadConnector) Server() string { return c.server }
func (*interactiveReloadConnector) Handler() handlers.Handler { return nil }
func (c *interactiveReloadConnector) SupportsQueryUpdates(time.Duration) bool { return c.supported }
func (c *interactiveReloadConnector) ApplySessionSpec(spec sessionspec.Spec, _ time.Duration) error {
if c.applyErr != nil {
return c.applyErr
}
c.appliedSpec = spec
return nil
}
func (c *interactiveReloadConnector) CommittedSession() (sessionspec.Spec, uint64, bool) {
if c.generation == 0 {
return sessionspec.Spec{}, 0, false
}
return c.appliedSpec, c.generation, true
}
type interactiveReloadMaker struct {
commits []interactiveReloadCommit
}
type interactiveReloadCommit struct {
generation uint64
spec SessionSpec
}
func (*interactiveReloadMaker) makeHandler(string) handlers.Handler { return nil }
func (*interactiveReloadMaker) makeCommands() []string { return nil }
func (m *interactiveReloadMaker) commitSessionSpec(spec SessionSpec, generation uint64) error {
m.commits = append(m.commits, interactiveReloadCommit{
generation: generation,
spec: spec,
})
return nil
}
|