Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 77a2c55

Browse files
authored
Unify fleet and stand-alone suites (#1112)
* fix agent uninstall * unify fleet and stand alone suites * move things around a bit more * fixe bad merge * simplify some things
1 parent 77eaee8 commit 77a2c55

5 files changed

Lines changed: 172 additions & 230 deletions

File tree

e2e/_suites/fleet/features/apm_integration.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Scenarios for APM
66
Scenario Outline: Deploying a <image> stand-alone agent with fleet server mode
77
Given a "<image>" stand-alone agent is deployed with fleet server mode
88
And the stand-alone agent is listed in Fleet as "online"
9-
When the "Elastic APM" integration is added to the policy
10-
Then the "Elastic APM" datasource is shown in the policy
9+
When the "Elastic APM" integration is added in the policy
10+
Then the "Elastic APM" datasource is shown in the policy as added
1111
And the "apm-server" process is in the "started" state on the host
1212

1313

e2e/_suites/fleet/fleet.go

Lines changed: 85 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const actionREMOVED = "removed"
3131
// FleetTestSuite represents the scenarios for Fleet-mode
3232
type FleetTestSuite struct {
3333
// integrations
34-
Cleanup bool
34+
StandAlone bool
3535
CurrentToken string // current enrollment token
3636
CurrentTokenID string // current enrollment tokenID
3737
ElasticAgentStopped bool // will be used to signal when the agent process can be called again in the tear-down stage
@@ -47,32 +47,39 @@ type FleetTestSuite struct {
4747
kibanaClient *kibana.Client
4848
// fleet server
4949
FleetServerHostname string // hostname of the fleet server. If empty, it means the agent is the first one, bootstrapping fleet server
50+
// date controls for queries
51+
AgentStoppedDate time.Time
52+
RuntimeDependenciesStartDate time.Time
5053
}
5154

5255
// afterScenario destroys the state created by a scenario
5356
func (fts *FleetTestSuite) afterScenario() {
54-
serviceManager := compose.NewServiceManager()
5557

56-
agentInstaller := fts.getInstaller()
58+
serviceName := common.ElasticAgentServiceName
59+
serviceManager := compose.NewServiceManager()
5760

58-
serviceName := fts.getServiceName(agentInstaller)
61+
if !fts.StandAlone {
62+
agentInstaller := fts.getInstaller()
63+
serviceName = fts.getServiceName(agentInstaller)
5964

60-
if log.IsLevelEnabled(log.DebugLevel) {
61-
err := agentInstaller.PrintLogsFn(fts.Hostname)
62-
if err != nil {
63-
log.WithFields(log.Fields{
64-
"containerName": fts.Hostname,
65-
"error": err,
66-
}).Warn("Could not get agent logs in the container")
65+
if log.IsLevelEnabled(log.DebugLevel) {
66+
err := agentInstaller.PrintLogsFn(fts.Hostname)
67+
if err != nil {
68+
log.WithFields(log.Fields{
69+
"containerName": fts.Hostname,
70+
"error": err,
71+
}).Warn("Could not get agent logs in the container")
72+
}
6773
}
68-
}
69-
70-
// only call it when the elastic-agent is present
71-
if !fts.ElasticAgentStopped {
72-
err := agentInstaller.UninstallFn()
73-
if err != nil {
74-
log.Warnf("Could not uninstall the agent after the scenario: %v", err)
74+
// only call it when the elastic-agent is present
75+
if !fts.ElasticAgentStopped {
76+
err := agentInstaller.UninstallFn()
77+
if err != nil {
78+
log.Warnf("Could not uninstall the agent after the scenario: %v", err)
79+
}
7580
}
81+
} else if log.IsLevelEnabled(log.DebugLevel) {
82+
_ = fts.getContainerLogs()
7683
}
7784

7885
err := fts.unenrollHostname()
@@ -106,11 +113,12 @@ func (fts *FleetTestSuite) afterScenario() {
106113
fts.Image = ""
107114
fts.Hostname = ""
108115
fts.FleetServerHostname = ""
116+
fts.StandAlone = false
109117
}
110118

111119
// beforeScenario creates the state needed by a scenario
112120
func (fts *FleetTestSuite) beforeScenario() {
113-
fts.Cleanup = false
121+
fts.StandAlone = false
114122
fts.ElasticAgentStopped = false
115123

116124
fts.Version = common.AgentVersion
@@ -160,6 +168,41 @@ func (fts *FleetTestSuite) contributeSteps(s *godog.ScenarioContext) {
160168
s.Step(`^the policy response will be shown in the Security App$`, fts.thePolicyResponseWillBeShownInTheSecurityApp)
161169
s.Step(`^the policy is updated to have "([^"]*)" in "([^"]*)" mode$`, fts.thePolicyIsUpdatedToHaveMode)
162170
s.Step(`^the policy will reflect the change in the Security App$`, fts.thePolicyWillReflectTheChangeInTheSecurityApp)
171+
172+
// stand-alone only steps
173+
s.Step(`^a "([^"]*)" stand-alone agent is deployed$`, fts.aStandaloneAgentIsDeployed)
174+
s.Step(`^a "([^"]*)" stand-alone agent is deployed with fleet server mode$`, fts.bootstrapFleetServerFromAStandaloneAgent)
175+
s.Step(`^a "([^"]*)" stand-alone agent is deployed with fleet server mode on cloud$`, fts.aStandaloneAgentIsDeployedWithFleetServerModeOnCloud)
176+
s.Step(`^there is new data in the index from agent$`, fts.thereIsNewDataInTheIndexFromAgent)
177+
s.Step(`^the "([^"]*)" docker container is stopped$`, fts.theDockerContainerIsStopped)
178+
s.Step(`^there is no new data in the index after agent shuts down$`, fts.thereIsNoNewDataInTheIndexAfterAgentShutsDown)
179+
s.Step(`^the stand-alone agent is listed in Fleet as "([^"]*)"$`, fts.theStandaloneAgentIsListedInFleetWithStatus)
180+
}
181+
182+
func (fts *FleetTestSuite) theStandaloneAgentIsListedInFleetWithStatus(desiredStatus string) error {
183+
waitForAgents := func() error {
184+
agents, err := fts.kibanaClient.ListAgents()
185+
if err != nil {
186+
return err
187+
}
188+
189+
if len(agents) == 0 {
190+
return errors.New("No agents found")
191+
}
192+
193+
agentZero := agents[0]
194+
hostname := agentZero.LocalMetadata.Host.HostName
195+
196+
return theAgentIsListedInFleetWithStatus(desiredStatus, hostname)
197+
}
198+
maxTimeout := time.Duration(common.TimeoutFactor) * time.Minute * 2
199+
exp := common.GetExponentialBackOff(maxTimeout)
200+
201+
err := backoff.Retry(waitForAgents, exp)
202+
if err != nil {
203+
return err
204+
}
205+
return nil
163206
}
164207

165208
func (fts *FleetTestSuite) anStaleAgentIsDeployedToFleetWithInstaller(image, version, installerType string) error {
@@ -310,7 +353,6 @@ func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndFleetServer(i
310353
var fleetConfig *kibana.FleetConfig
311354
fleetConfig, err = deployAgentToFleet(agentInstaller, containerName, fts.CurrentToken, fts.FleetServerHostname)
312355

313-
fts.Cleanup = true
314356
if err != nil {
315357
return err
316358
}
@@ -665,46 +707,6 @@ func (fts *FleetTestSuite) theEnrollmentTokenIsRevoked() error {
665707
return nil
666708
}
667709

668-
func (fts *FleetTestSuite) thePolicyShowsTheDatasourceAdded(packageName string) error {
669-
return thePolicyShowsTheDatasourceAdded(fts.kibanaClient, fts.FleetServerPolicy, packageName)
670-
}
671-
672-
func thePolicyShowsTheDatasourceAdded(client *kibana.Client, policy kibana.Policy, packageName string) error {
673-
log.WithFields(log.Fields{
674-
"policyID": policy.ID,
675-
"package": packageName,
676-
}).Trace("Checking if the policy shows the package added")
677-
678-
maxTimeout := time.Minute
679-
retryCount := 1
680-
681-
exp := common.GetExponentialBackOff(maxTimeout)
682-
683-
configurationIsPresentFn := func() error {
684-
packagePolicy, err := client.GetIntegrationFromAgentPolicy(packageName, policy)
685-
if err != nil {
686-
log.WithFields(log.Fields{
687-
"packagePolicy": packagePolicy,
688-
"policy": policy,
689-
"retry": retryCount,
690-
"error": err,
691-
}).Warn("The integration was not found in the policy")
692-
retryCount++
693-
return err
694-
}
695-
696-
retryCount++
697-
return err
698-
}
699-
700-
err := backoff.Retry(configurationIsPresentFn, exp)
701-
if err != nil {
702-
return err
703-
}
704-
705-
return nil
706-
}
707-
708710
func (fts *FleetTestSuite) theIntegrationIsOperatedInThePolicy(packageName string, action string) error {
709711
return theIntegrationIsOperatedInThePolicy(fts.kibanaClient, fts.FleetServerPolicy, packageName, action)
710712
}
@@ -1227,3 +1229,26 @@ func inputs(integration string) []kibana.Input {
12271229
}
12281230
return []kibana.Input{}
12291231
}
1232+
1233+
func (fts *FleetTestSuite) getContainerLogs() error {
1234+
serviceManager := compose.NewServiceManager()
1235+
1236+
profile := common.FleetProfileName
1237+
serviceName := common.ElasticAgentServiceName
1238+
1239+
composes := []string{
1240+
profile, // profile name
1241+
serviceName, // agent service
1242+
}
1243+
err := serviceManager.RunCommand(profile, composes, []string{"logs", serviceName}, common.ProfileEnv)
1244+
if err != nil {
1245+
log.WithFields(log.Fields{
1246+
"error": err,
1247+
"service": serviceName,
1248+
}).Error("Could not retrieve Elastic Agent logs")
1249+
1250+
return err
1251+
}
1252+
1253+
return nil
1254+
}

e2e/_suites/fleet/ingest_manager_test.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,37 +89,23 @@ func setUpSuite() {
8989
kibanaClient: kibanaClient,
9090
Installers: map[string]installer.ElasticAgentInstaller{}, // do not pre-initialise the map
9191
},
92-
StandAlone: &StandAloneTestSuite{
93-
kibanaClient: kibanaClient,
94-
},
9592
}
9693
}
9794

9895
func InitializeIngestManagerTestScenario(ctx *godog.ScenarioContext) {
9996
ctx.BeforeScenario(func(*messages.Pickle) {
10097
log.Trace("Before Fleet scenario")
101-
102-
imts.StandAlone.Cleanup = false
103-
10498
imts.Fleet.beforeScenario()
10599
})
106100

107101
ctx.AfterScenario(func(*messages.Pickle, error) {
108102
log.Trace("After Fleet scenario")
109-
110-
if imts.StandAlone.Cleanup {
111-
imts.StandAlone.afterScenario()
112-
}
113-
114-
if imts.Fleet.Cleanup {
115-
imts.Fleet.afterScenario()
116-
}
103+
imts.Fleet.afterScenario()
117104
})
118105

119106
ctx.Step(`^the "([^"]*)" process is in the "([^"]*)" state on the host$`, imts.processStateOnTheHost)
120107

121108
imts.Fleet.contributeSteps(ctx)
122-
imts.StandAlone.contributeSteps(ctx)
123109
}
124110

125111
func InitializeIngestManagerTestSuite(ctx *godog.TestSuiteContext) {
@@ -181,7 +167,7 @@ func InitializeIngestManagerTestSuite(ctx *godog.TestSuiteContext) {
181167

182168
imts.Fleet.setup()
183169

184-
imts.StandAlone.RuntimeDependenciesStartDate = time.Now().UTC()
170+
imts.Fleet.RuntimeDependenciesStartDate = time.Now().UTC()
185171
})
186172

187173
ctx.AfterSuite(func() {

0 commit comments

Comments
 (0)