@@ -1007,20 +1007,12 @@ func (r *tester) prepareScenario(ctx context.Context, config *testConfig, stackC
10071007 return nil , fmt .Errorf ("failed to find the selected policy_template: %w" , err )
10081008 }
10091009
1010- policyCurrent , policyToEnroll , policyToTest , err := r .createKibanaPolicies (ctx , serviceStateData , stackConfig )
1010+ policyToEnrollOrCurrent , policyToTest , err := r .createOrGetKibanaPolicies (ctx , serviceStateData , stackConfig )
10111011 if err != nil {
10121012 return nil , fmt .Errorf ("failed to create kibana policies: %w" , err )
10131013 }
10141014
1015- // policyToEnroll is used in both independent agents and agents created by servicedeployer (custom or kubernetes agents)
1016- policy := policyToEnroll
1017- if r .runTearDown || r .runTestsOnly {
1018- // required in order to be able select the right agent in `checkEnrolledAgents` when
1019- // using independent agents or custom/kubernetes agents since policy data is set into `agentInfo` variable`
1020- policy = policyCurrent
1021- }
1022-
1023- agentDeployed , agentInfo , err := r .setupAgent (ctx , config , serviceStateData , policy )
1015+ agentDeployed , agentInfo , err := r .setupAgent (ctx , config , serviceStateData , policyToEnrollOrCurrent )
10241016 if err != nil {
10251017 return nil , err
10261018 }
@@ -1039,7 +1031,7 @@ func (r *tester) prepareScenario(ctx context.Context, config *testConfig, stackC
10391031 }
10401032 }
10411033
1042- service , svcInfo , err := r .setupService (ctx , config , serviceOptions , svcInfo , agentInfo , agentDeployed , policy , serviceStateData )
1034+ service , svcInfo , err := r .setupService (ctx , config , serviceOptions , svcInfo , agentInfo , agentDeployed , policyToEnrollOrCurrent , serviceStateData )
10431035 if err != nil && ! errors .Is (err , os .ErrNotExist ) {
10441036 return nil , err
10451037 }
@@ -1241,7 +1233,7 @@ func (r *tester) prepareScenario(ctx context.Context, config *testConfig, stackC
12411233 if r .runSetup {
12421234 opts := scenarioStateOpts {
12431235 origPolicy : & origPolicy ,
1244- enrollPolicy : policyToEnroll ,
1236+ enrollPolicy : policyToEnrollOrCurrent ,
12451237 currentPolicy : policyToTest ,
12461238 config : config ,
12471239 agent : * origAgent ,
@@ -1257,7 +1249,12 @@ func (r *tester) prepareScenario(ctx context.Context, config *testConfig, stackC
12571249 return & scenario , nil
12581250}
12591251
1260- func (r * tester ) createKibanaPolicies (ctx context.Context , serviceStateData ServiceState , stackConfig stack.Config ) (* kibana.Policy , * kibana.Policy , * kibana.Policy , error ) {
1252+ // createOrGetKibanaPolicies creates the Kibana policies required for testing.
1253+ // It creates two policies, one for enrolling the agent (policyToEnroll) and another one
1254+ // for testing purposes (policyToTest) where the package data stream is added.
1255+ // In case the tester is running with --teardown or --no-provision flags, then the policies
1256+ // are read from the service state file created in the setup stage.
1257+ func (r * tester ) createOrGetKibanaPolicies (ctx context.Context , serviceStateData ServiceState , stackConfig stack.Config ) (* kibana.Policy , * kibana.Policy , error ) {
12611258 // Configure package (single data stream) via Fleet APIs.
12621259 testTime := time .Now ().Format ("20060102T15:04:05Z" )
12631260 var policyToTest , policyCurrent , policyToEnroll * kibana.Policy
@@ -1282,7 +1279,7 @@ func (r *tester) createKibanaPolicies(ctx context.Context, serviceStateData Serv
12821279
12831280 policyToEnroll , err = r .kibanaClient .CreatePolicy (ctx , policyEnroll )
12841281 if err != nil {
1285- return nil , nil , nil , fmt .Errorf ("could not create test policy: %w" , err )
1282+ return nil , nil , fmt .Errorf ("could not create test policy: %w" , err )
12861283 }
12871284 }
12881285
@@ -1324,7 +1321,7 @@ func (r *tester) createKibanaPolicies(ctx context.Context, serviceStateData Serv
13241321 }
13251322 policyToTest , err = r .kibanaClient .CreatePolicy (ctx , policy )
13261323 if err != nil {
1327- return nil , nil , nil , fmt .Errorf ("could not create test policy: %w" , err )
1324+ return nil , nil , fmt .Errorf ("could not create test policy: %w" , err )
13281325 }
13291326 }
13301327
@@ -1342,7 +1339,14 @@ func (r *tester) createKibanaPolicies(ctx context.Context, serviceStateData Serv
13421339 return nil
13431340 }
13441341
1345- return policyCurrent , policyToEnroll , policyToTest , nil
1342+ if r .runTearDown || r .runTestsOnly {
1343+ // required to return "policyCurrent" policy in order to be able select the right agent in `checkEnrolledAgents` when
1344+ // using independent agents or custom/kubernetes agents since policy data is set into `agentInfo` variable`
1345+ return policyCurrent , policyToTest , nil
1346+ }
1347+
1348+ // policyToEnroll is used in both independent agents and agents created by servicedeployer (custom or kubernetes agents)
1349+ return policyToEnroll , policyToTest , nil
13461350}
13471351
13481352func (r * tester ) setupService (ctx context.Context , config * testConfig , serviceOptions servicedeployer.FactoryOptions , svcInfo servicedeployer.ServiceInfo , agentInfo agentdeployer.AgentInfo , agentDeployed agentdeployer.DeployedAgent , policy * kibana.Policy , state ServiceState ) (servicedeployer.DeployedService , servicedeployer.ServiceInfo , error ) {
0 commit comments