-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathcontainer_cmd_test.go
496 lines (437 loc) · 15.2 KB
/
container_cmd_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
//go:build integration
package integration
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"text/template"
"time"
"github.com/gofrs/uuid/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/elastic/elastic-agent-libs/kibana"
"github.com/elastic/elastic-agent/pkg/core/process"
atesting "github.com/elastic/elastic-agent/pkg/testing"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools/fleettools"
)
func createPolicy(
t *testing.T,
ctx context.Context,
agentFixture *atesting.Fixture,
info *define.Info,
policyName string,
dataOutputID string) (string, string) {
createPolicyReq := kibana.AgentPolicy{
Name: policyName,
Namespace: info.Namespace,
Description: "test policy for agent enrollment",
MonitoringEnabled: []kibana.MonitoringEnabledOption{
kibana.MonitoringEnabledLogs,
kibana.MonitoringEnabledMetrics,
},
AgentFeatures: []map[string]interface{}{
{
"name": "test_enroll",
"enabled": true,
},
},
}
if dataOutputID != "" {
createPolicyReq.DataOutputID = dataOutputID
}
// Create policy
policy, err := info.KibanaClient.CreatePolicy(ctx, createPolicyReq)
if err != nil {
t.Fatalf("could not create Agent Policy: %s", err)
}
// Create enrollment API key
createEnrollmentAPIKeyReq := kibana.CreateEnrollmentAPIKeyRequest{
PolicyID: policy.ID,
}
t.Logf("Creating enrollment API key...")
enrollmentToken, err := info.KibanaClient.CreateEnrollmentAPIKey(ctx, createEnrollmentAPIKeyReq)
if err != nil {
t.Fatalf("unable to create enrolment API key: %s", err)
}
return policy.ID, enrollmentToken.APIKey
}
func prepareAgentCMD(
t *testing.T,
ctx context.Context,
agentFixture *atesting.Fixture,
args []string,
env []string) (*exec.Cmd, *strings.Builder) {
cmd, err := agentFixture.PrepareAgentCommand(ctx, args)
if err != nil {
t.Fatalf("could not prepare agent command: %s", err)
}
t.Cleanup(func() {
if cmd.Process != nil {
t.Log(">> cleaning up: killing the Elastic-Agent process")
if err := cmd.Process.Kill(); err != nil {
t.Fatalf("could not kill Elastic-Agent process: %s", err)
}
// Kill does not wait for the process to finish, so we wait here
state, err := cmd.Process.Wait()
if err != nil {
t.Errorf("Elastic-Agent exited with error after kill signal: %s", err)
t.Errorf("Elastic-Agent exited with status %d", state.ExitCode())
out, err := cmd.CombinedOutput()
if err == nil {
t.Log(string(out))
}
}
return
}
t.Log(">> cleaning up: no process to kill")
})
agentOutput := strings.Builder{}
cmd.Stderr = &agentOutput
cmd.Stdout = &agentOutput
cmd.Env = append(os.Environ(), env...)
return cmd, &agentOutput
}
func TestContainerCMD(t *testing.T) {
info := define.Require(t, define.Requirements{
Stack: &define.Stack{},
Local: false,
Sudo: true,
OS: []define.OS{
{Type: define.Linux},
},
Group: "container",
})
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
require.NoError(t, err)
// prepare must be called otherwise `agentFixture.WorkDir()` will be empty
// and it must be set so the `STATE_PATH` below gets a valid path.
err = agentFixture.Prepare(ctx)
require.NoError(t, err)
fleetURL, err := fleettools.DefaultURL(ctx, info.KibanaClient)
if err != nil {
t.Fatalf("could not get Fleet URL: %s", err)
}
_, enrollmentToken := createPolicy(
t,
ctx,
agentFixture,
info,
fmt.Sprintf("%s-%s", t.Name(), uuid.Must(uuid.NewV4()).String()),
"")
env := []string{
"FLEET_ENROLL=1",
"FLEET_URL=" + fleetURL,
"FLEET_ENROLLMENT_TOKEN=" + enrollmentToken,
// As the agent isn't built for a container, it's upgradable, triggering
// the start of the upgrade watcher. If `STATE_PATH` isn't set, the
// upgrade watcher will commence from a different path within the
// container, distinct from the current execution path.
"STATE_PATH=" + agentFixture.WorkDir(),
}
cmd, agentOutput := prepareAgentCMD(t, ctx, agentFixture, []string{"container"}, env)
t.Logf(">> running binary with: %v", cmd.Args)
if err := cmd.Start(); err != nil {
t.Fatalf("error running container cmd: %s", err)
}
require.Eventuallyf(t, func() bool {
// This will return errors until it connects to the agent,
// they're mostly noise because until the agent starts running
// we will get connection errors. If the test fails
// the agent logs will be present in the error message
// which should help to explain why the agent was not
// healthy.
err = agentFixture.IsHealthy(ctx, withEnv(env))
return err == nil
},
5*time.Minute, time.Second,
"Elastic-Agent did not report healthy. Agent status error: \"%v\", Agent logs\n%s",
err, agentOutput,
)
}
func TestContainerCMDWithAVeryLongStatePath(t *testing.T) {
info := define.Require(t, define.Requirements{
Stack: &define.Stack{},
Local: false,
Sudo: true,
OS: []define.OS{
{Type: define.Linux},
},
Group: "container",
})
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
fleetURL, err := fleettools.DefaultURL(ctx, info.KibanaClient)
if err != nil {
t.Fatalf("could not get Fleet URL: %s", err)
}
testCases := map[string]struct {
statePath string
expectedStatePath string
expectedSocketPath string
expectError bool
}{
"small path": { // Use the set path
statePath: filepath.Join(os.TempDir(), "foo", "bar"),
expectedStatePath: filepath.Join(os.TempDir(), "foo", "bar"),
expectedSocketPath: "/tmp/foo/bar/data/smp7BzlzcwgrLK4PUxpu7G1O5UwV4adr.sock",
},
"no path set": { // Use the default path
statePath: "",
expectedStatePath: "/usr/share/elastic-agent/state",
expectedSocketPath: "/usr/share/elastic-agent/state/data/Td8I7R-Zby36_zF_IOd9QVNlFblNEro3.sock",
},
"long path": { // Path too long to create a unix socket, it will use /tmp/elastic-agent
statePath: "/tmp/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
expectedStatePath: "/tmp/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
expectedSocketPath: "/tmp/elastic-agent/Xegnlbb8QDcqNLPzyf2l8PhVHjWvlQgZ.sock",
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
require.NoError(t, err)
_, enrollmentToken := createPolicy(
t,
ctx,
agentFixture,
info,
fmt.Sprintf("test-policy-enroll-%s", uuid.Must(uuid.NewV4()).String()),
"")
env := []string{
"FLEET_ENROLL=1",
"FLEET_URL=" + fleetURL,
"FLEET_ENROLLMENT_TOKEN=" + enrollmentToken,
"STATE_PATH=" + tc.statePath,
}
cmd, agentOutput := prepareAgentCMD(t, ctx, agentFixture, []string{"container"}, env)
t.Logf(">> running binary with: %v", cmd.Args)
if err := cmd.Start(); err != nil {
t.Fatalf("error running container cmd: %s", err)
}
require.Eventuallyf(t, func() bool {
// This will return errors until it connects to the agent,
// they're mostly noise because until the agent starts running
// we will get connection errors. If the test fails
// the agent logs will be present in the error message
// which should help to explain why the agent was not
// healthy.
err = agentFixture.IsHealthy(ctx, withEnv(env))
return err == nil
},
1*time.Minute, time.Second,
"Elastic-Agent did not report healthy. Agent status error: \"%v\", Agent logs\n%s",
err, agentOutput,
)
t.Cleanup(func() {
_ = os.RemoveAll(tc.expectedStatePath)
})
// Now that the Elastic-Agent is healthy, check that the control socket path
// is the expected one
if _, err := os.Stat(tc.expectedStatePath); err != nil {
t.Errorf("cannot stat expected state path ('%s'): %s", tc.expectedStatePath, err)
}
if _, err := os.Stat(tc.expectedSocketPath); err != nil {
t.Errorf("cannot stat expected socket path ('%s'): %s", tc.expectedSocketPath, err)
}
containerPaths := filepath.Join(tc.expectedStatePath, "container-paths.yml")
if _, err := os.Stat(tc.expectedSocketPath); err != nil {
t.Errorf("cannot stat expected container-paths.yml path ('%s'): %s", containerPaths, err)
}
if t.Failed() {
containerPathsContent, err := os.ReadFile(containerPaths)
if err != nil {
t.Fatalf("could not read container-paths.yml: %s", err)
}
t.Log("contents of 'container-paths-yml'")
t.Log(string(containerPathsContent))
}
})
}
}
func withEnv(env []string) process.CmdOption {
return func(c *exec.Cmd) error {
c.Env = append(os.Environ(), env...)
return nil
}
}
func TestContainerCMDEventToStderr(t *testing.T) {
info := define.Require(t, define.Requirements{
Stack: &define.Stack{},
Local: false,
Sudo: true,
OS: []define.OS{
{Type: define.Linux},
},
Group: "container",
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
require.NoError(t, err)
// We call agentFixture.Prepare to set the workdir
require.NoError(t, agentFixture.Prepare(ctx), "failed preparing agent fixture")
_, outputID := createMockESOutput(t, info, 0, 0, 100, 0)
policyID, enrollmentAPIKey := createPolicy(
t,
ctx,
agentFixture,
info,
fmt.Sprintf("%s-%s", t.Name(), uuid.Must(uuid.NewV4()).String()),
outputID)
fleetURL, err := fleettools.DefaultURL(ctx, info.KibanaClient)
if err != nil {
t.Fatalf("could not get Fleet URL: %s", err)
}
env := []string{
"FLEET_ENROLL=1",
"FLEET_URL=" + fleetURL,
"FLEET_ENROLLMENT_TOKEN=" + enrollmentAPIKey,
"STATE_PATH=" + agentFixture.WorkDir(),
// That is what we're interested in testing
"EVENTS_TO_STDERR=true",
}
cmd, agentOutput := prepareAgentCMD(t, ctx, agentFixture, []string{"container"}, env)
addLogIntegration(t, info, policyID, "/tmp/flog.log")
generateLogFile(t, "/tmp/flog.log", time.Second/2, 100)
t.Logf(">> running binary with: %v", cmd.Args)
if err := cmd.Start(); err != nil {
t.Fatalf("error running container cmd: %s", err)
}
assert.Eventuallyf(t, func() bool {
// This will return errors until it connects to the agent,
// they're mostly noise because until the agent starts running
// we will get connection errors. If the test fails
// the agent logs will be present in the error message
// which should help to explain why the agent was not
// healthy.
err := agentFixture.IsHealthy(ctx, withEnv(env))
return err == nil
},
2*time.Minute, time.Second,
"Elastic-Agent did not report healthy. Agent status error: \"%v\", Agent logs\n%s",
err, agentOutput,
)
assert.Eventually(t, func() bool {
agentOutputStr := agentOutput.String()
scanner := bufio.NewScanner(strings.NewReader(agentOutputStr))
for scanner.Scan() {
if strings.Contains(scanner.Text(), "Cannot index event") {
return true
}
}
return false
}, 3*time.Minute, 10*time.Second, "cannot find events on stderr")
}
// createMockESOutput creates an output configuration pointing to a mockES
// started in a random port and a cleanup function is registered to close
// the server at the end of the test.
// The server will respond with the passed error probabilities. If they add
// up to zero, all requests are a success.
func createMockESOutput(t *testing.T, info *define.Info, percentDuplicate, percentTooMany, percentNonIndex, percentTooLarge uint) (string, string) {
mockesURL := startMockES(t, percentDuplicate, percentTooMany, percentNonIndex, percentTooLarge)
createOutputBody := `
{
"id": "mock-es-%[1]s",
"name": "mock-es-%[1]s",
"type": "elasticsearch",
"is_default": false,
"hosts": [
"%s"
],
"preset": "latency"
}
`
// The API will return an error if the output ID/name contains an
// UUID substring, so we replace the '-' by '_' to keep the API happy.
outputUUID := strings.Replace(uuid.Must(uuid.NewV4()).String(), "-", "_", -1)
bodyStr := fmt.Sprintf(createOutputBody, outputUUID, mockesURL)
bodyReader := strings.NewReader(bodyStr)
// THE URL IS MISSING
status, result, err := info.KibanaClient.Request(http.MethodPost, "/api/fleet/outputs", nil, nil, bodyReader)
if err != nil {
t.Fatalf("could execute request to create output: %#v, status: %d, result:\n%s\nBody:\n%s", err, status, string(result), bodyStr)
}
if status != http.StatusOK {
t.Fatalf("creating output failed. Status code %d, response\n:%s", status, string(result))
}
outputResp := struct {
Item struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"is_default"`
Hosts []string `json:"hosts"`
Preset string `json:"preset"`
IsDefaultMonitoring bool `json:"is_default_monitoring"`
} `json:"item"`
}{}
if err := json.Unmarshal(result, &outputResp); err != nil {
t.Errorf("could not decode create output response: %s", err)
t.Logf("Response:\n%s", string(result))
}
return mockesURL, outputResp.Item.ID
}
func addLogIntegration(t *testing.T, info *define.Info, policyID, logFilePath string) {
agentPolicyBuilder := strings.Builder{}
tmpl, err := template.New(t.Name() + "custom-log-policy").Parse(policyJSON)
if err != nil {
t.Fatalf("cannot parse template: %s", err)
}
err = tmpl.Execute(&agentPolicyBuilder, policyVars{
Name: "Log-Input-" + t.Name() + "-" + time.Now().Format(time.RFC3339),
PolicyID: policyID,
LogFilePath: logFilePath,
Dataset: "logs",
Namespace: "default",
})
if err != nil {
t.Fatalf("could not render template: %s", err)
}
// We keep a copy of the policy for debugging prurposes
agentPolicy := agentPolicyBuilder.String()
// Call Kibana to create the policy.
// Docs: https://www.elastic.co/guide/en/fleet/current/fleet-api-docs.html#create-integration-policy-api
resp, err := info.KibanaClient.Connection.Send(
http.MethodPost,
"/api/fleet/package_policies",
nil,
nil,
bytes.NewBufferString(agentPolicy))
if err != nil {
t.Fatalf("could not execute request to Kibana/Fleet: %s", err)
}
if resp.StatusCode != http.StatusOK {
// On error dump the whole request response so we can easily spot
// what went wrong.
t.Errorf("received a non 200-OK when adding package to policy. "+
"Status code: %d", resp.StatusCode)
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
t.Fatalf("could not dump error response from Kibana: %s", err)
}
// Make debugging as easy as possible
t.Log("================================================================================")
t.Log("Kibana error response:")
t.Log(string(respDump))
t.Log("================================================================================")
t.Log("Rendered policy:")
t.Log(agentPolicy)
t.Log("================================================================================")
t.FailNow()
}
}