blob: d58c8e6429294d7b8bfd2aa249a7ff8a8485ada1 [file] [log] [blame]
Andrew Walbran94bbf2f2022-05-12 18:35:42 +00001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Integration test for VM bootloader.
16
17use android_system_virtualizationservice::{
18 aidl::android::system::virtualizationservice::{
Andrew Walbranb713baa2022-12-07 14:34:49 +000019 DiskImage::DiskImage, VirtualMachineConfig::VirtualMachineConfig,
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000020 VirtualMachineRawConfig::VirtualMachineRawConfig,
21 },
22 binder::{ParcelFileDescriptor, ProcessState},
23};
24use anyhow::{Context, Error};
25use log::info;
26use std::{
27 fs::File,
Andrew Walbranb713baa2022-12-07 14:34:49 +000028 io::{self, BufRead, BufReader, Write},
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000029 os::unix::io::FromRawFd,
30 panic, thread,
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000031};
32use vmclient::{DeathReason, VmInstance};
33
34const VMBASE_EXAMPLE_PATH: &str =
35 "/data/local/tmp/vmbase_example.integration_test/arm64/vmbase_example.bin";
Andrew Walbranb713baa2022-12-07 14:34:49 +000036const TEST_DISK_IMAGE_PATH: &str = "/data/local/tmp/vmbase_example.integration_test/test_disk.img";
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000037
38/// Runs the vmbase_example VM as an unprotected VM via VirtualizationService.
39#[test]
40fn test_run_example_vm() -> Result<(), Error> {
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000041 android_logger::init_once(
42 android_logger::Config::default().with_tag("vmbase").with_min_level(log::Level::Debug),
43 );
44
45 // Redirect panic messages to logcat.
46 panic::set_hook(Box::new(|panic_info| {
47 log::error!("{}", panic_info);
48 }));
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000049
50 // We need to start the thread pool for Binder to work properly, especially link_to_death.
51 ProcessState::start_thread_pool();
52
53 let service = vmclient::connect().context("Failed to find VirtualizationService")?;
54
55 // Start example VM.
56 let bootloader = ParcelFileDescriptor::new(
57 File::open(VMBASE_EXAMPLE_PATH)
58 .with_context(|| format!("Failed to open VM image {}", VMBASE_EXAMPLE_PATH))?,
59 );
Seungjae Yoo62085c02022-08-12 04:44:52 +000060
Andrew Walbranb713baa2022-12-07 14:34:49 +000061 // Make file for test disk image.
62 let mut test_image = File::options()
63 .create(true)
64 .read(true)
65 .write(true)
66 .truncate(true)
67 .open(TEST_DISK_IMAGE_PATH)
68 .with_context(|| format!("Failed to open test disk image {}", TEST_DISK_IMAGE_PATH))?;
69 // Write 4 sectors worth of 4-byte numbers counting up.
70 for i in 0u32..512 {
71 test_image.write_all(&i.to_le_bytes())?;
72 }
73 let test_image = ParcelFileDescriptor::new(test_image);
74 let disk_image = DiskImage { image: Some(test_image), writable: false, partitions: vec![] };
75
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000076 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Seungjae Yoo62085c02022-08-12 04:44:52 +000077 name: String::from("VmBaseTest"),
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000078 kernel: None,
79 initrd: None,
80 params: None,
81 bootloader: Some(bootloader),
Andrew Walbranb713baa2022-12-07 14:34:49 +000082 disks: vec![disk_image],
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000083 protectedVm: false,
84 memoryMib: 300,
85 numCpus: 1,
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000086 platformVersion: "~1.0".to_string(),
87 taskProfiles: vec![],
88 });
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000089 let console = android_log_fd()?;
90 let log = android_log_fd()?;
Alan Stokes0e82b502022-08-08 14:44:48 +010091 let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log), None)
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000092 .context("Failed to create VM")?;
93 vm.start().context("Failed to start VM")?;
94 info!("Started example VM.");
95
96 // Wait for VM to finish, and check that it shut down cleanly.
97 let death_reason = vm.wait_for_death();
98 assert_eq!(death_reason, DeathReason::Shutdown);
99
100 Ok(())
101}
102
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +0000103fn android_log_fd() -> io::Result<File> {
104 let (reader_fd, writer_fd) = nix::unistd::pipe()?;
105
106 // SAFETY: These are new FDs with no previous owner.
107 let reader = unsafe { File::from_raw_fd(reader_fd) };
108 let writer = unsafe { File::from_raw_fd(writer_fd) };
109
110 thread::spawn(|| {
111 for line in BufReader::new(reader).lines() {
112 info!("{}", line.unwrap());
113 }
114 });
115 Ok(writer)
Andrew Walbran94bbf2f2022-05-12 18:35:42 +0000116}