-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathaction_handler_test.go
More file actions
216 lines (190 loc) · 5.02 KB
/
Copy pathaction_handler_test.go
File metadata and controls
216 lines (190 loc) · 5.02 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
216
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package beater
import (
"context"
"runtime"
"testing"
"time"
"github.com/gofrs/uuid/v5"
"github.com/google/go-cmp/cmp"
"github.com/elastic/beats/v7/x-pack/osquerybeat/internal/ecs"
"github.com/elastic/beats/v7/x-pack/osquerybeat/internal/osqdcli"
"github.com/elastic/elastic-agent-libs/logp"
)
type mockExecutor struct {
result []map[string]interface{}
err error
receivedSql string
}
func (e *mockExecutor) Query(ctx context.Context, sql string, to time.Duration) ([]map[string]interface{}, error) {
e.receivedSql = sql
return e.result, e.err
}
type mockPublisher struct {
index string
idValue string
idFieldKey string
responseID string
spaceID string
packID string
packName string
queryName string
meta map[string]interface{}
hits []map[string]interface{}
ecsm ecs.Mapping
reqData interface{}
profile map[string]interface{}
}
func (p *mockPublisher) Publish(index, idValue, idFieldKey, responseID, spaceID, packID, packName, queryName string, meta map[string]interface{}, hits []map[string]interface{}, ecsm ecs.Mapping, reqData interface{}) {
p.index = index
p.idValue = idValue
p.idFieldKey = idFieldKey
p.responseID = responseID
p.spaceID = spaceID
p.packID = packID
p.packName = packName
p.queryName = queryName
p.meta = meta
p.hits = hits
p.ecsm = ecsm
p.reqData = reqData
}
func (p *mockPublisher) PublishQueryProfile(index, queryName, actionID, responseID string, profile map[string]interface{}, reqData interface{}) {
p.profile = profile
}
func TestActionHandlerExecute(t *testing.T) {
validLogger := logp.NewLogger("action_test")
inputType := osqueryInputType
ctx := context.Background()
actionID := uuid.Must(uuid.NewV4()).String()
actionSQL := "select * from uptime"
nonMatchingPlatform := "windows"
if runtime.GOOS == "windows" {
nonMatchingPlatform = "linux"
}
request := map[string]interface{}{
"id": actionID,
"data": map[string]interface{}{
"query": actionSQL,
},
}
tests := []struct {
Name string
QueryExecutor queryExecutor
Publisher actionQueryPublisher
Request map[string]interface{}
Err error
Skipped bool
}{
{
Name: "no executor",
Request: request,
Err: ErrNoQueryExecutor,
},
{
Name: "no publisher",
QueryExecutor: &mockExecutor{},
Request: request,
Err: ErrNoPublisher,
},
{
Name: "valid",
QueryExecutor: &mockExecutor{},
Publisher: &mockPublisher{},
Request: request,
},
{
Name: "skips non matching platform",
QueryExecutor: &mockExecutor{},
Publisher: &mockPublisher{},
Request: map[string]interface{}{
"id": actionID,
"data": map[string]interface{}{
"query": actionSQL,
"platform": nonMatchingPlatform,
},
},
Skipped: true,
},
{
Name: "executor error",
QueryExecutor: &mockExecutor{err: osqdcli.ErrClientClosed},
Publisher: &mockPublisher{},
Request: request,
Err: osqdcli.ErrClientClosed,
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
ac := &actionHandler{
log: validLogger,
inputType: inputType,
queryExec: tc.QueryExecutor,
publisher: tc.Publisher,
}
diff := cmp.Diff(inputType, ac.Name())
if diff != "" {
t.Fatal(diff)
}
res, err := ac.Execute(ctx, tc.Request)
// The err here is only needed to comply with Action interface, should always be nil
if err != nil {
t.Fatal("Unexpected error:", err)
}
if res == nil {
t.Fatal("Unexpected result: nil")
}
errVal, ok := res["error"]
if tc.Err == nil {
if ok {
t.Fatal("Unexpected error:", errVal)
} else if tc.Skipped {
diff := cmp.Diff("", tc.QueryExecutor.(*mockExecutor).receivedSql)
if diff != "" {
t.Error(diff)
}
diff = cmp.Diff("", tc.Publisher.(*mockPublisher).idValue)
if diff != "" {
t.Error(diff)
}
diff = cmp.Diff(0, res["count"])
if diff != "" {
t.Error(diff)
}
} else {
diff := cmp.Diff(tc.QueryExecutor.(*mockExecutor).receivedSql, actionSQL)
if diff != "" {
t.Error(diff)
}
diff = cmp.Diff(actionID, tc.Publisher.(*mockPublisher).idValue)
if diff != "" {
t.Error(diff)
}
diff = cmp.Diff("action_id", tc.Publisher.(*mockPublisher).idFieldKey)
if diff != "" {
t.Error(diff)
}
diff = cmp.Diff("", tc.Publisher.(*mockPublisher).responseID)
if diff != "" {
t.Error(diff)
}
}
} else {
if ok {
errMsg, ok := errVal.(string)
if !ok {
t.Fatal("error message is not a string")
}
diff := cmp.Diff(tc.Err.Error(), errMsg)
if diff != "" {
t.Fatal(diff)
}
} else {
t.Fatal("Unexpected error, got none in the result")
}
}
})
}
}