forked from elastic/e2e-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stand-alone.go
310 lines (257 loc) · 8.99 KB
/
stand-alone.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
// 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 main
import (
"context"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
elasticversion "github.com/elastic/e2e-testing/internal"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/installer"
"github.com/elastic/e2e-testing/internal/kibana"
"github.com/elastic/e2e-testing/internal/utils"
"github.com/elastic/e2e-testing/internal/elasticsearch"
log "github.com/sirupsen/logrus"
)
func (fts *FleetTestSuite) aStandaloneAgentIsDeployed(image string) error {
return fts.startStandAloneAgent(image, "", nil)
}
func (fts *FleetTestSuite) bootstrapFleetServerFromAStandaloneAgent(image string) error {
return fts.startStandAloneAgent(image, "", map[string]string{"fleetServerMode": "1"})
}
func (fts *FleetTestSuite) thereIsNewDataInTheIndexFromAgent() error {
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute * 2
minimumHitsCount := 20
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName).WithFlavour(fts.Image)
manifest, _ := fts.deployer.Inspect(fts.currentContext, agentService)
result, err := searchAgentData(fts.currentContext, manifest.Hostname, fts.RuntimeDependenciesStartDate, minimumHitsCount, maxTimeout)
if err != nil {
return err
}
log.Tracef("Search result: %v", result)
return elasticsearch.AssertHitsArePresent(result)
}
func (fts *FleetTestSuite) theDockerContainerIsStopped(serviceName string) error {
agentService := deploy.NewServiceRequest(serviceName)
err := fts.deployer.Stop(fts.currentContext, agentService)
if err != nil {
return err
}
fts.AgentStoppedDate = time.Now().UTC()
return nil
}
func (fts *FleetTestSuite) thereIsNoNewDataInTheIndexAfterAgentShutsDown() error {
maxTimeout := time.Duration(30) * time.Second
minimumHitsCount := 1
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.deployer.Inspect(fts.currentContext, agentService)
result, err := searchAgentData(fts.currentContext, manifest.Hostname, fts.AgentStoppedDate, minimumHitsCount, maxTimeout)
if err != nil {
if strings.Contains(err.Error(), "type:index_not_found_exception") {
return err
}
log.WithFields(log.Fields{
"error": err,
}).Info("No documents were found for the Agent in the index after it stopped")
return nil
}
return elasticsearch.AssertHitsAreNotPresent(result)
}
func (fts *FleetTestSuite) startStandAloneAgent(image string, flavour string, env map[string]string) error {
fts.StandAlone = true
log.Trace("Deploying an agent to Fleet")
dockerImageTag := common.BeatVersion
common.ProfileEnv["elasticAgentDockerNamespace"] = deploy.GetDockerNamespaceEnvVar("beats")
common.ProfileEnv["elasticAgentDockerImageSuffix"] = ""
if image != "default" {
common.ProfileEnv["elasticAgentDockerImageSuffix"] = "-" + image
}
useCISnapshots := elasticversion.GithubCommitSha1 != ""
if useCISnapshots || elasticversion.BeatsLocalPath != "" {
// load the docker images that were already:
// a. downloaded from the GCP bucket
// b. fetched from the local beats binaries
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
dockerInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, "docker")
dockerInstaller.Preinstall(fts.currentContext)
arch := utils.GetArchitecture()
dockerImageTag += "-" + arch
}
// Grab a new enrollment key for new agent
enrollmentKey, err := fts.kibanaClient.CreateEnrollmentAPIKey(fts.currentContext, fts.Policy)
if err != nil {
return err
}
fts.CurrentToken = enrollmentKey.APIKey
fts.CurrentTokenID = enrollmentKey.ID
cfg, err := kibana.NewFleetConfig(fts.CurrentToken)
if err != nil {
return err
}
// See https://github.com/elastic/beats/blob/4accfa8/x-pack/elastic-agent/pkg/agent/cmd/container.go#L73-L85
// to understand the environment variables used by the elastic-agent to automatically
// enroll the new agent container in Fleet
common.ProfileEnv["fleetInsecure"] = "1"
common.ProfileEnv["fleetUrl"] = cfg.FleetServerURL()
common.ProfileEnv["fleetEnroll"] = "1"
common.ProfileEnv["fleetEnrollmentToken"] = cfg.EnrollmentToken
common.ProfileEnv["fleetServerPort"] = "8221" // fixed port to avoid collitions with the stack's fleet-server
common.ProfileEnv["elasticAgentTag"] = dockerImageTag
for k, v := range env {
common.ProfileEnv[k] = v
}
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName).WithFlavour(flavour)
err = fts.deployer.Add(fts.currentContext, deploy.NewServiceRequest(common.FleetProfileName), []deploy.ServiceRequest{agentService}, common.ProfileEnv)
if err != nil {
log.Error("Could not deploy the elastic-agent")
return err
}
fts.Image = image
manifest, _ := fts.deployer.Inspect(fts.currentContext, agentService)
err = fts.installTestTools(manifest.Name)
if err != nil {
return err
}
return nil
}
func (fts *FleetTestSuite) thePolicyShowsTheDatasourceAdded(packageName string) error {
log.WithFields(log.Fields{
"policyID": fts.Policy.ID,
"package": packageName,
}).Trace("Checking if the policy shows the package added")
maxTimeout := time.Minute
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
configurationIsPresentFn := func() error {
packagePolicy, err := fts.kibanaClient.GetIntegrationFromAgentPolicy(fts.currentContext, packageName, fts.Policy)
if err != nil {
log.WithFields(log.Fields{
"packagePolicy": packagePolicy,
"policy": fts.Policy,
"retry": retryCount,
"error": err,
}).Warn("The integration was not found in the policy")
retryCount++
return err
}
retryCount++
return err
}
err := backoff.Retry(configurationIsPresentFn, exp)
if err != nil {
return err
}
return nil
}
// installTestTools we need the container name because we use the Docker Client instead of Docker Compose
// we are going to install those tools we use in the test framework for checking
// and verifications
func (fts *FleetTestSuite) installTestTools(containerName string) error {
if fts.Image != "ubi8" {
return nil
}
cmd := []string{"microdnf", "install", "procps-ng"}
log.WithFields(log.Fields{
"command": cmd,
"containerName": containerName,
}).Trace("Installing test tools ")
_, err := deploy.ExecCommandIntoContainer(fts.currentContext, containerName, "root", cmd)
if err != nil {
log.WithFields(log.Fields{
"command": cmd,
"containerName": containerName,
"error": err,
}).Error("Could not install test tools using the Docker client")
return err
}
log.WithFields(log.Fields{
"command": cmd,
"containerName": containerName,
}).Debug("Test tools installed")
return nil
}
func searchAgentData(ctx context.Context, hostname string, startDate time.Time, minimumHitsCount int, maxTimeout time.Duration) (elasticsearch.SearchResult, error) {
timezone := "America/New_York"
esQuery := map[string]interface{}{
"version": true,
"size": 500,
"docvalue_fields": []map[string]interface{}{
{
"field": "@timestamp",
"format": "date_time",
},
{
"field": "system.process.cpu.start_time",
"format": "date_time",
},
{
"field": "system.service.state_since",
"format": "date_time",
},
},
"_source": map[string]interface{}{
"excludes": []map[string]interface{}{},
},
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{},
"filter": []map[string]interface{}{
{
"bool": map[string]interface{}{
"filter": []map[string]interface{}{
{
"bool": map[string]interface{}{
"should": []map[string]interface{}{
{
"match_phrase": map[string]interface{}{
"host.name": hostname,
},
},
},
"minimum_should_match": 1,
},
},
{
"bool": map[string]interface{}{
"should": []map[string]interface{}{
{
"range": map[string]interface{}{
"@timestamp": map[string]interface{}{
"gte": startDate,
"time_zone": timezone,
},
},
},
},
"minimum_should_match": 1,
},
},
},
},
},
{
"range": map[string]interface{}{
"@timestamp": map[string]interface{}{
"gte": startDate,
"format": "strict_date_optional_time",
},
},
},
},
"should": []map[string]interface{}{},
"must_not": []map[string]interface{}{},
},
},
}
indexName := "logs-elastic_agent-default"
result, err := elasticsearch.WaitForNumberOfHits(ctx, indexName, esQuery, minimumHitsCount, maxTimeout)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn(elasticsearch.WaitForIndices())
}
return result, err
}