@@ -2,8 +2,10 @@ use std::{fs, io, path::Path, time::Duration};
22
33use crate :: {
44 common:: {
5- agent_control:: start_agent_control_with_custom_config, base_paths:: TempBasePaths ,
6- retry:: retry, runtime:: tokio_runtime,
5+ agent_control:: { StartedAgentControl , start_agent_control_with_custom_config} ,
6+ base_paths:: TempBasePaths ,
7+ retry:: retry,
8+ runtime:: tokio_runtime,
79 } ,
810 on_host:: tools:: {
911 config:: { OnHostAgentControlConfigBuilder , create_file, create_local_config} ,
@@ -123,7 +125,7 @@ fn run_file_logging_scenario(
123125 initial_message : & str ,
124126 reload_file_logging : bool ,
125127 reload_message : & str ,
126- ) -> TempBasePaths {
128+ ) -> ( TempBasePaths , StartedAgentControl ) {
127129 let mut opamp_server = FakeServer :: start ( tokio_runtime ( ) . handle ( ) ) ;
128130
129131 let dirs = TempBasePaths :: default ( ) ;
@@ -154,13 +156,13 @@ fn run_file_logging_scenario(
154156 dirs. local_dir ( ) ,
155157 ) ;
156158
157- let _agent_control =
159+ let agent_control =
158160 start_agent_control_with_custom_config ( dirs. base_paths ( ) , AGENT_CONTROL_MODE_ON_HOST ) ;
159161
160162 let sub_agent_instance_id =
161163 get_instance_id ( & AgentID :: try_from ( agent_id) . unwrap ( ) , dirs. base_paths ( ) ) ;
162164
163- // Give some time for the echo output to be captured in the log files
165+ // Let the first run write its log before the reload can change/disable logging.
164166 std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
165167
166168 // Trigger a reload by sending a new remote config via OpAMP with updated values
@@ -169,10 +171,8 @@ fn run_file_logging_scenario(
169171 format ! ( "message: \" {reload_message}\" \n enable_file_logging: \" {reload_file_logging}\" \n " ) ,
170172 ) ;
171173
172- // Give some time for the echo output to be captured in the log files
173- std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
174-
175- dirs
174+ // AC stays alive via the returned handle; the caller's retry waits for the post-reload logs.
175+ ( dirs, agent_control)
176176}
177177
178178/// File logging enable/disable combinations with before and after reload checks
@@ -188,7 +188,8 @@ fn test_file_logging_reload(
188188) {
189189 let agent_id = format ! ( "file-logging-agent-{first_run_enabled}-{second_run_enabled}" ) ;
190190
191- let dirs = run_file_logging_scenario (
191+ // Keep the handle alive so AC keeps running during the wait below.
192+ let ( dirs, _agent_control) = run_file_logging_scenario (
192193 & agent_id,
193194 first_run_enabled,
194195 first_run_message,
@@ -197,24 +198,25 @@ fn test_file_logging_reload(
197198 ) ;
198199
199200 let log_dir_path = dirs. log_dir ( ) ;
200- let agent_logs_dir = log_dir_path. join ( & agent_id) ;
201- assert ! (
202- agent_logs_dir. exists( ) ,
203- "Log directory {agent_logs_dir:?} does not exist"
204- ) ;
205201
206202 let all_contents = retry ( 60 , Duration :: from_secs ( 1 ) , || {
207- Ok ( collect_stdout_logs ( & log_dir_path, & agent_id) ?)
203+ let contents = collect_stdout_logs ( & log_dir_path, & agent_id) ?;
204+ if first_run_enabled == contents. contains ( first_run_message)
205+ && second_run_enabled == contents. contains ( second_run_message)
206+ {
207+ Ok ( contents)
208+ } else {
209+ Err ( format ! ( "log state not settled yet. Log contents: {contents}" ) . into ( ) )
210+ }
208211 } ) ;
209212
210- // If the logs are enabled for the run the string must be found, same for disabled and not found
211213 assert ! (
212214 first_run_enabled == all_contents. contains( first_run_message) ,
213- "First run log not found (pre-reload). Log contents: {all_contents}"
215+ "First run log mismatch (pre-reload). Log contents: {all_contents}"
214216 ) ;
215217 assert ! (
216218 second_run_enabled == all_contents. contains( second_run_message) ,
217- "Second run log not found (post-reload). Log contents: {all_contents}"
219+ "Second run log mismatch (post-reload). Log contents: {all_contents}"
218220 ) ;
219221}
220222
@@ -226,7 +228,8 @@ fn onhost_supervisor_reloading_keeps_file_logging_disabled() {
226228 let unique_str_2 = "keeps_disabled_run2" ;
227229 let agent_id = "test-agent-logs-always-disabled" ;
228230
229- let dirs = run_file_logging_scenario ( agent_id, false , unique_str_1, false , unique_str_2) ;
231+ let ( dirs, _agent_control) =
232+ run_file_logging_scenario ( agent_id, false , unique_str_1, false , unique_str_2) ;
230233
231234 let log_dir_path = dirs. log_dir ( ) ;
232235 let agent_logs_dir = log_dir_path. join ( agent_id) ;
0 commit comments