Skip to content

Commit 75bae63

Browse files
committed
test(vmm): Add test for consistency of static CPU templates
Adds a test that verify consistency of static CPU templates between hardcoded ones and JSON ones in order not to forget to update one or the other. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 397c2bb commit 75bae63

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/vmm/src/cpu_config/aarch64/static_cpu_templates/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ impl std::fmt::Display for StaticCpuTemplate {
4040
}
4141
}
4242
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
use crate::cpu_config::test_utils::get_json_template;
48+
49+
#[test]
50+
fn verify_consistency_with_json_templates() {
51+
let static_templates = [(v1n1::v1n1(), "aarch64_v1n1.json")];
52+
53+
for (hardcoded_template, filename) in static_templates {
54+
let json_template = get_json_template(filename);
55+
assert_eq!(hardcoded_template, json_template);
56+
}
57+
}
58+
}

src/vmm/src/cpu_config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ pub mod x86_64;
1313
/// Module containing type implementations needed for aarch64 (ARM) CPU configuration
1414
#[cfg(target_arch = "aarch64")]
1515
pub mod aarch64;
16+
17+
#[cfg(test)]
18+
pub mod test_utils;

src/vmm/src/cpu_config/test_utils.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::path::PathBuf;
5+
6+
use crate::cpu_config::templates::CustomCpuTemplate;
7+
8+
// Get a static CPU template stored as a JSON file.
9+
pub fn get_json_template(filename: &str) -> CustomCpuTemplate {
10+
let json_path = [
11+
env!("CARGO_MANIFEST_DIR"),
12+
"../../resources/tests/static_cpu_templates",
13+
filename,
14+
]
15+
.iter()
16+
.collect::<PathBuf>();
17+
18+
serde_json::from_str(&std::fs::read_to_string(json_path).unwrap()).unwrap()
19+
}

src/vmm/src/cpu_config/x86_64/static_cpu_templates/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,25 @@ impl StaticCpuTemplate {
5050
self == &StaticCpuTemplate::None
5151
}
5252
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
use crate::cpu_config::test_utils::get_json_template;
58+
59+
#[test]
60+
fn verify_consistency_with_json_templates() {
61+
let static_templates = [
62+
(c3::c3(), "c3.json"),
63+
(t2::t2(), "t2.json"),
64+
(t2s::t2s(), "t2s.json"),
65+
(t2cl::t2cl(), "t2cl.json"),
66+
(t2a::t2a(), "t2a.json"),
67+
];
68+
69+
for (hardcoded_template, filename) in static_templates {
70+
let json_template = get_json_template(filename);
71+
assert_eq!(hardcoded_template, json_template);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)