• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Testing virtio-fs.
6 
7 use fixture::vm::Config;
8 use fixture::vm::TestVm;
9 
10 /// Tests file copy on virtiofs
11 ///
12 /// 1. Create `original.txt` on a temporal directory.
13 /// 2. Start a VM with a virtiofs device for the temporal directory.
14 /// 3. Copy `original.txt` to `new.txt` in the guest.
15 /// 4. Check that `new.txt` is created in the host.
16 #[test]
copy_file()17 fn copy_file() {
18     const ORIGINAL_FILE_NAME: &str = "original.txt";
19     const NEW_FILE_NAME: &str = "new.txt";
20     const TEST_DATA: &str = "virtiofs works!";
21 
22     let temp_dir = tempfile::tempdir().unwrap();
23     let orig_file = temp_dir.path().join(ORIGINAL_FILE_NAME);
24 
25     std::fs::write(orig_file, TEST_DATA).unwrap();
26 
27     let tag = "mtdtest";
28 
29     let config = Config::new().extra_args(vec![
30         "--shared-dir".to_string(),
31         format!(
32             "{}:{tag}:type=fs:cache=auto",
33             temp_dir.path().to_str().unwrap()
34         ),
35     ]);
36 
37     let mut vm = TestVm::new(config).unwrap();
38     // TODO(b/269137600): Split this into multiple lines instead of connecting commands with `&&`.
39     vm.exec_in_guest(&format!(
40         "mount -t virtiofs {tag} /mnt && cp /mnt/{} /mnt/{} && sync",
41         ORIGINAL_FILE_NAME, NEW_FILE_NAME,
42     ))
43     .unwrap();
44 
45     let new_file = temp_dir.path().join(NEW_FILE_NAME);
46     let contents = std::fs::read(new_file).unwrap();
47     assert_eq!(TEST_DATA.as_bytes(), &contents);
48 }
49