@@ -31,7 +31,7 @@ const actionREMOVED = "removed"
3131// FleetTestSuite represents the scenarios for Fleet-mode
3232type 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
5356func (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
112120func (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
165208func (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-
708710func (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+ }
0 commit comments