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
|
package clients
import (
"testing"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/omode"
)
func TestSessionSpecCommands(t *testing.T) {
t.Parallel()
tests := []struct {
name string
spec SessionSpec
want []string
wantErr bool
}{
{
name: "tail read commands",
spec: SessionSpec{
Mode: omode.TailClient,
Files: []string{"/var/log/app.log"},
Options: "plain=true",
Regex: "ERROR",
},
want: []string{"tail:plain=true /var/log/app.log regex:default,literal ERROR"},
},
{
name: "map client commands",
spec: SessionSpec{
Mode: omode.MapClient,
Files: []string{"/var/log/app.log"},
Options: "plain=true",
Query: "from STATS select count(*)",
Regex: ".",
},
want: []string{
"map:plain=true from STATS select count(*)",
"cat:plain=true /var/log/app.log regex:noop ",
},
},
{
name: "tail query with timeout",
spec: SessionSpec{
Mode: omode.TailClient,
Files: []string{"/var/log/app.log"},
Options: "plain=true",
Query: "from STATS select count(*)",
Regex: "WARN",
Timeout: 15,
},
want: []string{
"map:plain=true from STATS select count(*)",
"timeout 15 tail /var/log/app.log regex:default,literal WARN",
},
},
{
name: "health command",
spec: SessionSpec{
Mode: omode.HealthClient,
},
want: []string{"health"},
},
{
name: "invalid regex returns error",
spec: SessionSpec{
Mode: omode.GrepClient,
Files: []string{"/var/log/app.log"},
Regex: "[",
},
wantErr: true,
},
{
name: "query with unsupported mode returns error",
spec: SessionSpec{
Mode: omode.GrepClient,
Files: []string{"/var/log/app.log"},
Query: "from STATS select count(*)",
Regex: ".",
},
wantErr: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := tc.spec.Commands()
if tc.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("Commands() error = %v", err)
}
if len(got) != len(tc.want) {
t.Fatalf("unexpected command count: got %d want %d (%v)", len(got), len(tc.want), got)
}
for i := range tc.want {
if got[i] != tc.want[i] {
t.Fatalf("command %d mismatch: got %q want %q", i, got[i], tc.want[i])
}
}
})
}
}
func TestNewSessionSpecSplitsFiles(t *testing.T) {
t.Parallel()
spec := NewSessionSpec(config.Args{
Mode: omode.GrepClient,
What: " a.log , , b.log ",
RegexStr: "ERROR",
RegexInvert: true,
Timeout: 10,
})
if len(spec.Files) != 2 || spec.Files[0] != "a.log" || spec.Files[1] != "b.log" {
t.Fatalf("unexpected files: %#v", spec.Files)
}
if !spec.RegexInvert {
t.Fatalf("expected RegexInvert to be true")
}
if spec.Timeout != 10 {
t.Fatalf("unexpected timeout: %d", spec.Timeout)
}
}
func TestNewSessionSpecUsesPipeSentinelForServerlessStdin(t *testing.T) {
t.Parallel()
spec := NewSessionSpec(config.Args{
Mode: omode.GrepClient,
Serverless: true,
RegexStr: "ERROR",
})
if len(spec.Files) != 1 || spec.Files[0] != "-" {
t.Fatalf("unexpected files for serverless stdin: %#v", spec.Files)
}
}
|