Skip to content

Commit 8898240

Browse files
committed
Updates test modules to include all possible health check paths
Signed-off-by: djach7 <djachimo@redhat.com>
1 parent cd19ce8 commit 8898240

1 file changed

Lines changed: 133 additions & 115 deletions

File tree

src/lib/greenboot.rs

Lines changed: 133 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,16 @@ mod test {
207207
/// validate when the required folder is not found
208208
#[test]
209209
fn test_missing_required_folder() {
210-
let required_path = format!("{}/check/required.d", GREENBOOT_INSTALL_PATHS[1]);
211-
if Path::new(&required_path).exists() {
212-
fs::remove_dir_all(&required_path).unwrap();
210+
for path in GREENBOOT_INSTALL_PATHS {
211+
let required_path = format!("{}/check/required.d", path);
212+
if Path::new(&required_path).exists() {
213+
fs::remove_dir_all(&required_path).unwrap();
214+
}
215+
assert_eq!(
216+
run_diagnostics(vec![]).unwrap_err().to_string(),
217+
String::from("cannot find any required.d folder")
218+
);
213219
}
214-
assert_eq!(
215-
run_diagnostics(vec![]).unwrap_err().to_string(),
216-
String::from("cannot find any required.d folder")
217-
);
218220
}
219221

220222
#[test]
@@ -239,44 +241,51 @@ mod test {
239241
let _ = std::fs::remove_file(format!("{}/01_failing_binary", required_path));
240242
let _ = std::fs::remove_file(format!("{}/02_failing_binary", required_path));
241243

242-
let base_path = GREENBOOT_INSTALL_PATHS[1];
243-
244-
let counter_file = format!("{}/fail_counter.txt", base_path);
245-
let mut file = File::create(&counter_file).expect("Failed to create counter file");
246-
writeln!(file, "0").unwrap();
247-
248-
// Inject counter logic into the failing scripts
249-
for name in ["01_failing_script", "02_failing_script"] {
250-
let path = format!("{}/check/required.d/{}.sh", base_path, name);
251-
let mut script = File::create(&path).unwrap();
252-
writeln!(
253-
script,
254-
"#!/bin/bash\nCOUNTER_FILE=\"{}\"\ncount=$(cat $COUNTER_FILE)\necho $((count + 1)) >| $COUNTER_FILE\nexit 1",
255-
counter_file
256-
).unwrap();
257-
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
258-
}
259-
260-
let result = run_diagnostics(vec![]);
261-
log::debug!("Diagnostics result: {:?}", result);
262-
263-
assert!(result.is_err());
264-
assert_eq!(
265-
result.unwrap_err().to_string(),
266-
"required health-check failed, skipping remaining scripts"
267-
);
268-
269-
log::info!("Health check failed as expected.");
244+
for base_path in GREENBOOT_INSTALL_PATHS {
245+
let counter_file = format!("{}/fail_counter.txt", base_path);
246+
let mut file = File::create(&counter_file).expect("Failed to create counter file");
247+
writeln!(file, "0").unwrap();
248+
249+
// Inject counter logic into the failing scripts
250+
for name in ["01_failing_script", "02_failing_script"] {
251+
let path = format!("{}/check/required.d/{}.sh", base_path, name);
252+
let mut script = File::create(&path).unwrap();
253+
writeln!(
254+
script,
255+
"#!/bin/bash\nCOUNTER_FILE=\"{}\"\ncount=$(cat $COUNTER_FILE)\necho $((count + 1)) >| $COUNTER_FILE\nexit 1",
256+
counter_file
257+
).unwrap();
258+
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
259+
}
270260

271-
let fail_script_count = fs::read_to_string(counter_file)
272-
.unwrap()
273-
.trim()
274-
.parse::<u32>()
275-
.unwrap();
276-
assert_eq!(
277-
fail_script_count, 1,
278-
"Only one failing script should have executed"
279-
);
261+
let result = run_diagnostics(vec![]);
262+
log::debug!("Diagnostics result: {:?}", result);
263+
264+
assert!(result.is_err());
265+
assert_eq!(
266+
result.unwrap_err().to_string(),
267+
"required health-check failed, skipping remaining scripts"
268+
);
269+
270+
log::info!("Health check failed as expected.");
271+
272+
let fail_script_count = fs::read_to_string(counter_file)
273+
.unwrap()
274+
.trim()
275+
.parse::<u32>()
276+
.unwrap();
277+
assert_eq!(
278+
fail_script_count, 1,
279+
"Only one failing script should have executed"
280+
);
281+
282+
// Clean up the created scripts
283+
// Necessary as otherwise they will trip up other install paths
284+
for name in ["01_failing_script", "02_failing_script"] {
285+
fs::remove_file(format!("{}/check/required.d/{}.sh", base_path, name))
286+
.expect("Failed to remove script file");
287+
}
288+
}
280289

281290
tear_down().expect("teardown failed");
282291
}
@@ -306,9 +315,11 @@ mod test {
306315

307316
// Removing extra failing binaries because this can cause a
308317
// failure if not added to the skips or removed as done below.
309-
let required_path = format!("{}/check/required.d", GREENBOOT_INSTALL_PATHS[1]);
310-
let _ = std::fs::remove_file(format!("{}/01_failing_binary", required_path));
311-
let _ = std::fs::remove_file(format!("{}/02_failing_binary", required_path));
318+
for base_path in GREENBOOT_INSTALL_PATHS {
319+
let required_path = format!("{}/check/required.d", base_path);
320+
let _ = std::fs::remove_file(format!("{}/01_failing_binary", required_path));
321+
let _ = std::fs::remove_file(format!("{}/02_failing_binary", required_path));
322+
}
312323

313324
// Skip the disabled script in required.d ,since there are two
314325
// failing- scripts passing them both so that this test passes.
@@ -334,9 +345,11 @@ mod test {
334345

335346
// Removing extra failing scripts because this can cause a
336347
// failure if not added to the skips or removed as done below
337-
let required_path = format!("{}/check/required.d", GREENBOOT_INSTALL_PATHS[1]);
338-
let _ = std::fs::remove_file(format!("{}/01_failing_script.sh", required_path));
339-
let _ = std::fs::remove_file(format!("{}/02_failing_script.sh", required_path));
348+
for base_path in GREENBOOT_INSTALL_PATHS {
349+
let required_path = format!("{}/check/required.d", base_path);
350+
let _ = std::fs::remove_file(format!("{}/01_failing_script.sh", required_path));
351+
let _ = std::fs::remove_file(format!("{}/02_failing_script.sh", required_path));
352+
}
340353

341354
// Skip the disabled script in required.d ,since there are two
342355
// failing- scripts passing them both so that this test passes.
@@ -353,86 +366,91 @@ mod test {
353366
}
354367

355368
fn setup_folder_structure(passing: bool) -> Result<()> {
356-
let required_path = format!("{}/check/required.d", GREENBOOT_INSTALL_PATHS[1]);
357-
let wanted_path = format!("{}/check/wanted.d", GREENBOOT_INSTALL_PATHS[1]);
358-
let passing_test_scripts = "testing_assets/passing_script.sh";
359-
let failing_test_scripts = "testing_assets/failing_script.sh";
360-
let passing_test_binary = "testing_assets/passing_binary";
361-
let failing_test_binary = "testing_assets/failing_binary";
362-
363-
fs::create_dir_all(&required_path).expect("cannot create folder");
364-
fs::create_dir_all(&wanted_path).expect("cannot create folder");
365-
366-
// Create passing script in both required and wanted
367-
fs::copy(
368-
passing_test_scripts,
369-
format!("{}/passing_script.sh", &required_path),
370-
)
371-
.context("unable to copy passing script to required.d")?;
372-
373-
fs::copy(
374-
passing_test_scripts,
375-
format!("{}/passing_script.sh", &wanted_path),
376-
)
377-
.context("unable to copy passing script to wanted.d")?;
378-
379-
// Create passing binary in both required and wanted
380-
fs::copy(
381-
passing_test_binary,
382-
format!("{}/passing_binary", &required_path),
383-
)
384-
.context("unable to copy passing binary to required.d")?;
385-
386-
fs::copy(
387-
passing_test_binary,
388-
format!("{}/passing_binary", &wanted_path),
389-
)
390-
.context("unable to copy passing binary to wanted.d")?;
391-
392-
// Create failing script in wanted.d
393-
fs::copy(
394-
failing_test_scripts,
395-
format!("{}/failing_script.sh", &wanted_path),
396-
)
397-
.context("unable to copy failing script to wanted.d")?;
398-
399-
// Create failing binary in wanted.d
400-
fs::copy(
401-
failing_test_binary,
402-
format!("{}/failing_binary", &wanted_path),
403-
)
404-
.context("unable to copy failing binary to wanted.d")?;
405-
406-
if !passing {
407-
// Create multiple failing script in required.d for failure cases
369+
for install_path in GREENBOOT_INSTALL_PATHS {
370+
let required_path = format!("{}/check/required.d", install_path);
371+
let wanted_path = format!("{}/check/wanted.d", install_path);
372+
373+
let passing_test_scripts = "testing_assets/passing_script.sh";
374+
let failing_test_scripts = "testing_assets/failing_script.sh";
375+
let passing_test_binary = "testing_assets/passing_binary";
376+
let failing_test_binary = "testing_assets/failing_binary";
377+
378+
fs::create_dir_all(&required_path).expect("cannot create folder");
379+
fs::create_dir_all(&wanted_path).expect("cannot create folder");
380+
381+
// Create passing script in both required and wanted
408382
fs::copy(
409-
failing_test_scripts,
410-
format!("{}/01_failing_script.sh", &required_path),
383+
passing_test_scripts,
384+
format!("{}/passing_script.sh", &required_path),
411385
)
412-
.context("unable to copy failing script to required.d")?;
386+
.context("unable to copy passing script to required.d")?;
387+
413388
fs::copy(
414-
failing_test_scripts,
415-
format!("{}/02_failing_script.sh", &required_path),
389+
passing_test_scripts,
390+
format!("{}/passing_script.sh", &wanted_path),
416391
)
417-
.context("unable to copy another failing script to required.d")?;
392+
.context("unable to copy passing script to wanted.d")?;
418393

419-
// Create multiple failing binaries in required.d for failure cases
394+
// Create passing binary in both required and wanted
420395
fs::copy(
421-
failing_test_scripts,
422-
format!("{}/01_failing_binary", &required_path),
396+
passing_test_binary,
397+
format!("{}/passing_binary", &required_path),
423398
)
424-
.context("unable to copy failing binary to required.d")?;
399+
.context("unable to copy passing binary to required.d")?;
400+
401+
fs::copy(
402+
passing_test_binary,
403+
format!("{}/passing_binary", &wanted_path),
404+
)
405+
.context("unable to copy passing binary to wanted.d")?;
406+
407+
// Create failing script in wanted.d
425408
fs::copy(
426409
failing_test_scripts,
427-
format!("{}/02_failing_binary", &required_path),
410+
format!("{}/failing_script.sh", &wanted_path),
428411
)
429-
.context("unable to copy another failing binary to required.d")?;
412+
.context("unable to copy failing script to wanted.d")?;
413+
414+
// Create failing binary in wanted.d
415+
fs::copy(
416+
failing_test_binary,
417+
format!("{}/failing_binary", &wanted_path),
418+
)
419+
.context("unable to copy failing binary to wanted.d")?;
420+
421+
if !passing {
422+
// Create multiple failing script in required.d for failure cases
423+
fs::copy(
424+
failing_test_scripts,
425+
format!("{}/01_failing_script.sh", &required_path),
426+
)
427+
.context("unable to copy failing script to required.d")?;
428+
fs::copy(
429+
failing_test_scripts,
430+
format!("{}/02_failing_script.sh", &required_path),
431+
)
432+
.context("unable to copy another failing script to required.d")?;
433+
434+
// Create multiple failing binaries in required.d for failure cases
435+
fs::copy(
436+
failing_test_scripts,
437+
format!("{}/01_failing_binary", &required_path),
438+
)
439+
.context("unable to copy failing binary to required.d")?;
440+
fs::copy(
441+
failing_test_scripts,
442+
format!("{}/02_failing_binary", &required_path),
443+
)
444+
.context("unable to copy another failing binary to required.d")?;
445+
}
430446
}
431447
Ok(())
432448
}
433449

434450
fn tear_down() -> Result<()> {
435-
fs::remove_dir_all(GREENBOOT_INSTALL_PATHS[1]).expect("Unable to delete folder");
451+
for path in GREENBOOT_INSTALL_PATHS {
452+
fs::remove_dir_all(path).expect("Unable to delete folder");
453+
}
436454
Ok(())
437455
}
438456
}

0 commit comments

Comments
 (0)