blob: 57b68edb9967e95750e28b4fd4cf8e575ce3c72c [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::{
19 VirtualMachineConfig::VirtualMachineConfig,
20 VirtualMachineRawConfig::VirtualMachineRawConfig,
21 },
22 binder::{ParcelFileDescriptor, ProcessState},
23};
24use anyhow::{Context, Error};
25use log::info;
26use std::{
27 fs::File,
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000028 io::{self, BufRead, BufReader},
29 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";
36
37/// Runs the vmbase_example VM as an unprotected VM via VirtualizationService.
38#[test]
39fn test_run_example_vm() -> Result<(), Error> {
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000040 android_logger::init_once(
41 android_logger::Config::default().with_tag("vmbase").with_min_level(log::Level::Debug),
42 );
43
44 // Redirect panic messages to logcat.
45 panic::set_hook(Box::new(|panic_info| {
46 log::error!("{}", panic_info);
47 }));
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000048
49 // We need to start the thread pool for Binder to work properly, especially link_to_death.
50 ProcessState::start_thread_pool();
51
52 let service = vmclient::connect().context("Failed to find VirtualizationService")?;
53
54 // Start example VM.
55 let bootloader = ParcelFileDescriptor::new(
56 File::open(VMBASE_EXAMPLE_PATH)
57 .with_context(|| format!("Failed to open VM image {}", VMBASE_EXAMPLE_PATH))?,
58 );
Seungjae Yoo62085c02022-08-12 04:44:52 +000059
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000060 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Seungjae Yoo62085c02022-08-12 04:44:52 +000061 name: String::from("VmBaseTest"),
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000062 kernel: None,
63 initrd: None,
64 params: None,
65 bootloader: Some(bootloader),
66 disks: vec![],
67 protectedVm: false,
68 memoryMib: 300,
69 numCpus: 1,
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000070 platformVersion: "~1.0".to_string(),
71 taskProfiles: vec![],
72 });
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000073 let console = android_log_fd()?;
74 let log = android_log_fd()?;
Alan Stokes0e82b502022-08-08 14:44:48 +010075 let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log), None)
Andrew Walbran94bbf2f2022-05-12 18:35:42 +000076 .context("Failed to create VM")?;
77 vm.start().context("Failed to start VM")?;
78 info!("Started example VM.");
79
80 // Wait for VM to finish, and check that it shut down cleanly.
81 let death_reason = vm.wait_for_death();
82 assert_eq!(death_reason, DeathReason::Shutdown);
83
84 Ok(())
85}
86
Pierre-Clément Tosi0d1aed02022-11-17 17:06:28 +000087fn android_log_fd() -> io::Result<File> {
88 let (reader_fd, writer_fd) = nix::unistd::pipe()?;
89
90 // SAFETY: These are new FDs with no previous owner.
91 let reader = unsafe { File::from_raw_fd(reader_fd) };
92 let writer = unsafe { File::from_raw_fd(writer_fd) };
93
94 thread::spawn(|| {
95 for line in BufReader::new(reader).lines() {
96 info!("{}", line.unwrap());
97 }
98 });
99 Ok(writer)
Andrew Walbran94bbf2f2022-05-12 18:35:42 +0000100}