|
1 | 1 | mod common;
|
2 | 2 |
|
3 |
| -use std::{fs::read_to_string, path::PathBuf}; |
| 3 | +use std::{ |
| 4 | + fs::{read_to_string, File}, |
| 5 | + path::PathBuf, |
| 6 | +}; |
4 | 7 |
|
5 | 8 | use byte_unit::{Byte, Unit};
|
6 |
| -use common::{build_hermit_bin, check_result, remove_file_if_exists}; |
| 9 | +use common::{build_hermit_bin, check_result, remove_file_if_exists, run_simple_vm}; |
7 | 10 | use uhyvelib::{
|
8 | 11 | params::{Output, Params},
|
9 | 12 | vm::UhyveVm,
|
@@ -67,3 +70,42 @@ fn uhyvefilemap_test() {
|
67 | 70 | check_result(&res);
|
68 | 71 | verify_file_equals(&output_path, "Hello, world!");
|
69 | 72 | }
|
| 73 | + |
| 74 | +#[test] |
| 75 | +fn remove_file_test() { |
| 76 | + env_logger::try_init().ok(); |
| 77 | + let output_path = PathBuf::from("/tmp/file_to_remove.txt"); |
| 78 | + remove_file_if_exists(&output_path); |
| 79 | + File::create(&output_path).unwrap(); |
| 80 | + |
| 81 | + let bin_path = build_hermit_bin("remove_file"); |
| 82 | + |
| 83 | + let params = Params { |
| 84 | + cpu_count: 2.try_into().unwrap(), |
| 85 | + memory_size: Byte::from_u64_with_unit(64, Unit::MiB) |
| 86 | + .unwrap() |
| 87 | + .try_into() |
| 88 | + .unwrap(), |
| 89 | + file_mapping: vec!["/tmp/file_to_remove.txt:/root/file_to_remove.txt".to_string()], |
| 90 | + output: Output::Buffer, |
| 91 | + stats: true, |
| 92 | + ..Default::default() |
| 93 | + }; |
| 94 | + |
| 95 | + assert!(output_path.exists()); |
| 96 | + |
| 97 | + let vm = UhyveVm::new(bin_path.clone(), params.clone()).unwrap(); |
| 98 | + let res = vm.run(None); |
| 99 | + check_result(&res); |
| 100 | + assert!(!output_path.exists()); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn remove_non_present_file_test() { |
| 105 | + // kernel tries to open a non-present file, so uhyve will reject the hypercall and the kernel |
| 106 | + // will panic. |
| 107 | + env_logger::try_init().ok(); |
| 108 | + let bin_path = build_hermit_bin("remove_file"); |
| 109 | + let res = run_simple_vm(bin_path); |
| 110 | + assert_ne!(res.code, 0); |
| 111 | +} |
0 commit comments