Skip to content

Commit 92c6f17

Browse files
author
Hakan Kaya
committed
feat(reporting): add JUnit XML export
1 parent c3529b9 commit 92c6f17

22 files changed

Lines changed: 749 additions & 39 deletions

File tree

‎Cargo.lock‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/vds-cli/Cargo.toml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ repository.workspace = true
88

99
[dependencies]
1010
tokio.workspace = true
11+
vds-core = { path = "../../crates/vds-core" }
1112
vds-protocol = { path = "../../crates/vds-protocol" }
13+
vds-scenario = { path = "../../crates/vds-scenario" }
14+
vds-server = { path = "../vds-server" }
1215

1316
[lints]
1417
workspace = true
15-

‎apps/vds-cli/src/main.rs‎

Lines changed: 185 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
1-
use std::{env, path::PathBuf, process::ExitCode};
1+
use std::{env, fs, path::PathBuf, process::ExitCode, sync::Arc};
22

33
use tokio::net::UnixStream;
4+
use vds_core::{clock::ManualClock, config::ServerConfig};
45
use vds_protocol::{
56
framing::{read_message, write_message},
67
v1::{
78
ClientRequest, ErrorCode, ServerResponse, SpiTransferRequest, client_request,
89
server_response,
910
},
1011
};
12+
use vds_scenario::{
13+
JUnitReportMetadata, RegistryRuntime, ResultStatus, ScenarioDocument, ScenarioExecutor,
14+
to_junit_xml,
15+
};
16+
17+
enum Arguments {
18+
SpiTransfer(SpiTransferArguments),
19+
ScenarioRun(ScenarioRunArguments),
20+
}
1121

12-
struct Arguments {
22+
struct SpiTransferArguments {
1323
socket: PathBuf,
1424
device_id: String,
1525
tx: Vec<u8>,
1626
}
1727

28+
struct ScenarioRunArguments {
29+
scenario: PathBuf,
30+
config: PathBuf,
31+
json_output: Option<PathBuf>,
32+
junit_output: Option<PathBuf>,
33+
run_id: String,
34+
revision: u64,
35+
}
36+
1837
impl Arguments {
1938
fn parse() -> Result<Self, String> {
2039
let mut args = env::args().skip(1);
2140
match args.next().as_deref() {
22-
Some("spi-transfer") => {}
23-
Some("--help" | "-h") | None => return Err(String::new()),
24-
Some(command) => return Err(format!("unknown command: {command}")),
41+
Some("spi-transfer") => Self::parse_spi_transfer(args).map(Self::SpiTransfer),
42+
Some("scenario") if args.next().as_deref() == Some("run") => {
43+
Self::parse_scenario_run(args).map(Self::ScenarioRun)
44+
}
45+
Some("--help" | "-h") | None => Err(String::new()),
46+
Some(command) => Err(format!("unknown command: {command}")),
2547
}
48+
}
2649

50+
fn parse_spi_transfer(
51+
mut args: impl Iterator<Item = String>,
52+
) -> Result<SpiTransferArguments, String> {
2753
let mut socket = None;
2854
let mut device_id = None;
2955
let mut tx = None;
@@ -36,12 +62,61 @@ impl Arguments {
3662
}
3763
}
3864

39-
Ok(Self {
65+
Ok(SpiTransferArguments {
4066
socket: socket.ok_or_else(|| "--socket is required".to_owned())?,
4167
device_id: device_id.ok_or_else(|| "--device is required".to_owned())?,
4268
tx: tx.ok_or_else(|| "--tx is required".to_owned())?,
4369
})
4470
}
71+
72+
fn parse_scenario_run(
73+
mut args: impl Iterator<Item = String>,
74+
) -> Result<ScenarioRunArguments, String> {
75+
let scenario = args
76+
.next()
77+
.map(PathBuf::from)
78+
.ok_or_else(|| "scenario path is required".to_owned())?;
79+
let mut config = PathBuf::from("config/vds-server.yaml");
80+
let mut json_output = None;
81+
let mut junit_output = None;
82+
let mut run_id = "cli".to_owned();
83+
let mut revision = 1_u64;
84+
while let Some(argument) = args.next() {
85+
match argument.as_str() {
86+
"--config" => config = PathBuf::from(required_value(&mut args, "--config")?),
87+
"--json-output" => {
88+
json_output = Some(PathBuf::from(required_value(&mut args, "--json-output")?));
89+
}
90+
"--junit-output" => {
91+
junit_output =
92+
Some(PathBuf::from(required_value(&mut args, "--junit-output")?));
93+
}
94+
"--run-id" => run_id = required_value(&mut args, "--run-id")?,
95+
"--revision" => {
96+
revision = required_value(&mut args, "--revision")?
97+
.parse()
98+
.map_err(|_| "--revision must be a positive integer".to_owned())?;
99+
if revision == 0 {
100+
return Err("--revision must be a positive integer".to_owned());
101+
}
102+
}
103+
unknown => return Err(format!("unknown argument: {unknown}")),
104+
}
105+
}
106+
Ok(ScenarioRunArguments {
107+
scenario,
108+
config,
109+
json_output,
110+
junit_output,
111+
run_id,
112+
revision,
113+
})
114+
}
115+
}
116+
117+
fn required_value(args: &mut impl Iterator<Item = String>, option: &str) -> Result<String, String> {
118+
args.next()
119+
.ok_or_else(|| format!("{option} requires a value"))
45120
}
46121

47122
#[tokio::main]
@@ -59,19 +134,29 @@ async fn main() -> ExitCode {
59134
}
60135
};
61136

62-
match transfer(arguments).await {
63-
Ok(rx) => {
64-
println!("RX: {}", format_hex(&rx));
65-
ExitCode::SUCCESS
66-
}
67-
Err(error) => {
68-
eprintln!("error: {error}");
69-
ExitCode::FAILURE
70-
}
137+
match arguments {
138+
Arguments::SpiTransfer(arguments) => match transfer(arguments).await {
139+
Ok(rx) => {
140+
println!("RX: {}", format_hex(&rx));
141+
ExitCode::SUCCESS
142+
}
143+
Err(error) => {
144+
eprintln!("error: {error}");
145+
ExitCode::FAILURE
146+
}
147+
},
148+
Arguments::ScenarioRun(arguments) => match run_scenario(&arguments) {
149+
Ok(ResultStatus::Passed) => ExitCode::SUCCESS,
150+
Ok(_) => ExitCode::FAILURE,
151+
Err(error) => {
152+
eprintln!("error: {error}");
153+
ExitCode::FAILURE
154+
}
155+
},
71156
}
72157
}
73158

74-
async fn transfer(arguments: Arguments) -> Result<Vec<u8>, String> {
159+
async fn transfer(arguments: SpiTransferArguments) -> Result<Vec<u8>, String> {
75160
let mut stream = UnixStream::connect(&arguments.socket)
76161
.await
77162
.map_err(|error| {
@@ -110,6 +195,45 @@ async fn transfer(arguments: Arguments) -> Result<Vec<u8>, String> {
110195
}
111196
}
112197

198+
fn run_scenario(arguments: &ScenarioRunArguments) -> Result<ResultStatus, String> {
199+
let yaml = fs::read_to_string(&arguments.scenario).map_err(|error| {
200+
format!(
201+
"failed to read scenario '{}': {error}",
202+
arguments.scenario.display()
203+
)
204+
})?;
205+
let document = ScenarioDocument::from_yaml(&yaml).map_err(|error| error.to_string())?;
206+
let config = ServerConfig::load(&arguments.config).map_err(|error| error.to_string())?;
207+
let clock = Arc::new(ManualClock::default());
208+
let registry = vds_server::load_registry_with_clock(&config, clock.clone())
209+
.map_err(|error| error.to_string())?;
210+
let runtime = RegistryRuntime::new(Arc::new(registry), clock);
211+
let result = ScenarioExecutor::new(runtime).run(&document);
212+
let json = format!(
213+
"{}\n",
214+
result.to_json_pretty().map_err(|error| error.to_string())?
215+
);
216+
if let Some(path) = &arguments.json_output {
217+
fs::write(path, &json)
218+
.map_err(|error| format!("failed to write JSON '{}': {error}", path.display()))?;
219+
}
220+
let junit = to_junit_xml(
221+
&result,
222+
JUnitReportMetadata {
223+
run_id: &arguments.run_id,
224+
scenario_revision: arguments.revision,
225+
},
226+
);
227+
if let Some(path) = &arguments.junit_output {
228+
fs::write(path, junit)
229+
.map_err(|error| format!("failed to write JUnit XML '{}': {error}", path.display()))?;
230+
}
231+
if arguments.json_output.is_none() && arguments.junit_output.is_none() {
232+
print!("{json}");
233+
}
234+
Ok(result.status)
235+
}
236+
113237
fn parse_hex(value: &str) -> Result<Vec<u8>, String> {
114238
let normalized = value
115239
.replace("0x", "")
@@ -147,12 +271,17 @@ fn format_hex(bytes: &[u8]) -> String {
147271
}
148272

149273
fn print_usage() {
150-
println!("vds-cli\n\nUSAGE:\n vds-cli spi-transfer --socket PATH --device ID --tx HEX");
274+
println!(
275+
"vds-cli\n\nUSAGE:\n vds-cli spi-transfer --socket PATH --device ID --tx HEX\n vds-cli scenario run SCENARIO [--config PATH] [--json-output PATH] [--junit-output PATH] [--run-id ID] [--revision N]"
276+
);
151277
}
152278

153279
#[cfg(test)]
154280
mod tests {
155-
use super::parse_hex;
281+
use std::{fs, path::PathBuf};
282+
283+
use super::{ScenarioRunArguments, parse_hex, run_scenario};
284+
use vds_scenario::ResultStatus;
156285

157286
#[test]
158287
fn parses_compact_and_separated_hex() {
@@ -166,4 +295,42 @@ mod tests {
166295
vec![0xef, 0x40, 0x18]
167296
);
168297
}
298+
299+
#[test]
300+
fn scenario_command_writes_json_and_junit_files() {
301+
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
302+
let directory =
303+
std::env::temp_dir().join(format!("vds4e-cli-junit-{}", std::process::id()));
304+
fs::create_dir_all(&directory).unwrap();
305+
let json = directory.join("result.json");
306+
let junit = directory.join("result.xml");
307+
let config = directory.join("vds-server.yaml");
308+
fs::write(
309+
&config,
310+
format!(
311+
"schema_version: 1\nserver: {{ control_address: '127.0.0.1:0' }}\ndata_plane: {{ unix_socket: /tmp/vds4e-cli-test.sock }}\nobservability: {{ log_level: info }}\ndevice_models: ['{}']\n",
312+
root.join("device-models/examples/spi-flash.yaml").display()
313+
),
314+
)
315+
.unwrap();
316+
let status = run_scenario(&ScenarioRunArguments {
317+
scenario: root.join("scenarios/examples/delayed-write-with-timeout.yaml"),
318+
config,
319+
json_output: Some(json.clone()),
320+
junit_output: Some(junit.clone()),
321+
run_id: "cli-test".to_owned(),
322+
revision: 3,
323+
})
324+
.unwrap();
325+
assert_eq!(status, ResultStatus::Passed);
326+
assert!(
327+
fs::read_to_string(json)
328+
.unwrap()
329+
.contains("\"status\": \"passed\"")
330+
);
331+
let xml = fs::read_to_string(junit).unwrap();
332+
assert!(xml.contains("name=\"vds4e.run_id\" value=\"cli-test\""));
333+
assert!(xml.contains("name=\"vds4e.scenario_revision\" value=\"3\""));
334+
fs::remove_dir_all(directory).unwrap();
335+
}
169336
}

0 commit comments

Comments
 (0)