Skip to main content

flowey_lib_hvlite/
build_nextest_vmm_tests.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Build the cargo-nextest based VMM tests.
5
6use crate::common::CommonArch;
7use crate::common::CommonProfile;
8use crate::run_cargo_nextest_run::NextestProfile;
9use flowey::node::prelude::*;
10use flowey_lib_common::run_cargo_build::CargoBuildProfile;
11use flowey_lib_common::run_cargo_nextest_run::TestResults;
12use flowey_lib_common::run_cargo_nextest_run::build_params::TestPackages;
13use std::collections::BTreeMap;
14
15/// Type-safe wrapper around a built nextest archive containing VMM tests
16#[derive(Serialize, Deserialize)]
17pub struct NextestVmmTestsArchive {
18    #[serde(rename = "vmm_tests.tar.zst")]
19    pub archive_file: PathBuf,
20}
21
22impl Artifact for NextestVmmTestsArchive {}
23
24/// Build mode to use when building the nextest VMM tests
25#[derive(Serialize, Deserialize)]
26pub enum BuildNextestVmmTestsMode {
27    /// Build and immediate run VMM tests, side-stepping any intermediate
28    /// archiving steps.
29    ///
30    /// NOTE: The caller is responsible for setting `extra_env` and
31    /// `pre_run_deps` to ensure that all tests filtered by
32    /// `nextest_filter_expr` are able to run successfully.
33    ImmediatelyRun {
34        /// Nextest profile to use when running the source code
35        nextest_profile: NextestProfile,
36        /// Nextest test filter expression
37        nextest_filter_expr: Option<String>,
38        /// Additional env vars set when executing the tests.
39        extra_env: ReadVar<BTreeMap<String, String>>,
40        /// Wait for specified side-effects to resolve before building / running
41        /// any tests. (e.g: to allow for some ambient packages / dependencies
42        /// to get installed).
43        pre_run_deps: Vec<ReadVar<SideEffect>>,
44
45        results: WriteVar<TestResults>,
46    },
47    /// Build and archive the tests into a nextest archive file, which can then
48    /// be run via [`crate::test_nextest_vmm_tests_archive`].
49    Archive(WriteVar<NextestVmmTestsArchive>),
50}
51
52flowey_request! {
53    pub struct Request {
54        /// Build and run VMM tests for the specified target
55        pub target: target_lexicon::Triple,
56        /// Build and run VMM tests with the specified cargo profile
57        pub profile: CommonProfile,
58        /// Build mode to use when building the nextest VMM tests
59        pub build_mode: BuildNextestVmmTestsMode,
60    }
61}
62
63new_flow_node!(struct Node);
64
65impl FlowNode for Node {
66    type Request = Request;
67
68    fn imports(ctx: &mut ImportCtx<'_>) {
69        ctx.import::<crate::install_openvmm_rust_build_essential::Node>();
70        ctx.import::<crate::run_cargo_nextest_run::Node>();
71        ctx.import::<crate::git_checkout_openvmm_repo::Node>();
72        ctx.import::<crate::init_openvmm_magicpath_openhcl_sysroot::Node>();
73        ctx.import::<crate::init_cross_build::Node>();
74        ctx.import::<flowey_lib_common::run_cargo_nextest_archive::Node>();
75        ctx.import::<flowey_lib_common::install_dist_pkg::Node>();
76    }
77
78    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
79        let mut ambient_deps = vec![ctx.reqv(crate::install_openvmm_rust_build_essential::Request)];
80
81        // TODO: install build tools for other platforms
82        if matches!(
83            ctx.platform(),
84            FlowPlatform::Linux(FlowPlatformLinuxDistro::Ubuntu)
85        ) {
86            ambient_deps.push(ctx.reqv(|v| {
87                flowey_lib_common::install_dist_pkg::Request::Install {
88                    package_names: vec![
89                        "libssl-dev".into(),
90                        "pkg-config".into(),
91                        "build-essential".into(),
92                    ],
93                    done: v,
94                }
95            }));
96        }
97
98        let ambient_deps = ambient_deps;
99
100        for Request {
101            target,
102            profile,
103            build_mode,
104        } in requests
105        {
106            let mut ambient_deps = ambient_deps.clone();
107
108            let sysroot_arch = CommonArch::from_architecture(target.architecture)?;
109
110            // See comment in `crate::cargo_build` for why this is necessary.
111            //
112            // copied here since this node doesn't actually route through `cargo build`.
113            if matches!(target.environment, target_lexicon::Environment::Musl) {
114                ambient_deps.push(
115                    ctx.reqv(|v| crate::init_openvmm_magicpath_openhcl_sysroot::Request {
116                        arch: sysroot_arch,
117                        path: v,
118                    })
119                    .into_side_effect(),
120                );
121            }
122
123            let injected_env = ctx.reqv(|v| crate::init_cross_build::Request {
124                target: target.clone(),
125                injected_env: v,
126            });
127
128            let build_params =
129                flowey_lib_common::run_cargo_nextest_run::build_params::NextestBuildParams {
130                    packages: ReadVar::from_static(TestPackages::Crates {
131                        crates: vec!["vmm_tests".into()],
132                    }),
133                    features: Default::default(),
134                    no_default_features: false,
135                    target: target.clone(),
136                    profile: match profile {
137                        CommonProfile::Release => CargoBuildProfile::Release,
138                        CommonProfile::Debug => CargoBuildProfile::Debug,
139                    },
140                    extra_env: injected_env,
141                };
142
143            match build_mode {
144                BuildNextestVmmTestsMode::ImmediatelyRun {
145                    nextest_profile,
146                    nextest_filter_expr,
147                    extra_env,
148                    pre_run_deps,
149                    results,
150                } => {
151                    ambient_deps.extend(pre_run_deps);
152
153                    ctx.req(crate::run_cargo_nextest_run::Request {
154                        friendly_name: "vmm_tests".into(),
155                        run_kind:
156                            flowey_lib_common::run_cargo_nextest_run::NextestRunKind::BuildAndRun(
157                                build_params,
158                            ),
159                        nextest_profile,
160                        nextest_filter_expr,
161                        nextest_working_dir: None,
162                        nextest_config_file: None,
163                        run_ignored: false,
164                        extra_env: Some(extra_env),
165                        pre_run_deps: ambient_deps,
166                        results,
167                    })
168                }
169                BuildNextestVmmTestsMode::Archive(unit_tests_archive) => {
170                    let openvmm_repo_path =
171                        ctx.reqv(crate::git_checkout_openvmm_repo::req::GetRepoDir);
172
173                    let archive_file =
174                        ctx.reqv(|v| flowey_lib_common::run_cargo_nextest_archive::Request {
175                            friendly_label: "vmm_tests".into(),
176                            working_dir: openvmm_repo_path,
177                            build_params,
178                            pre_run_deps: ambient_deps,
179                            archive_file: v,
180                        });
181
182                    ctx.emit_minor_rust_step("report built vmm_tests", |ctx| {
183                        let archive_file = archive_file.claim(ctx);
184                        let unit_tests = unit_tests_archive.claim(ctx);
185                        |rt| {
186                            let archive_file = rt.read(archive_file);
187                            rt.write(unit_tests, &NextestVmmTestsArchive { archive_file });
188                        }
189                    });
190                }
191            }
192        }
193
194        Ok(())
195    }
196}