Skip to content

Commit 601f67c

Browse files
committed
fixup tests
Signed-off-by: Matej Hrica <[email protected]>
1 parent 2d4ddbb commit 601f67c

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

tests/test_cases/src/test_multiport_console.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ mod host {
3030
});
3131
}
3232

33-
fn add_console_port(
33+
fn test_port(
3434
ctx: u32,
3535
console_id: u32,
3636
name: &str,
37-
) -> anyhow::Result<(UnixStream, UnixStream)> {
37+
) -> anyhow::Result<()> {
3838
let (guest, host) = UnixStream::pair()?;
3939
let name_cstring = CString::new(name)?;
4040
unsafe {
@@ -46,7 +46,9 @@ mod host {
4646
guest.as_raw_fd()
4747
))?;
4848
}
49-
Ok((guest, host))
49+
mem::forget(guest);
50+
spawn_ping_pong_responder(host);
51+
Ok(())
5052
}
5153

5254
impl Test for TestMultiportConsole {
@@ -55,36 +57,22 @@ mod host {
5557
krun_call!(krun_set_log_level(KRUN_LOG_LEVEL_WARN))?;
5658
let ctx = krun_call_u32!(krun_create_ctx())?;
5759

58-
// Disable implicit console
5960
krun_call!(krun_disable_implicit_console(ctx))?;
6061

61-
// Add a default console with STDIN/STDOUT/STDERR
62+
// Add a default console (as with other tests this uses stdout for writing "OK")
6263
krun_call!(krun_add_virtio_console_default(
6364
ctx,
64-
std::io::stdin().as_raw_fd(),
65+
-1,
6566
std::io::stdout().as_raw_fd(),
66-
std::io::stderr().as_raw_fd()
67+
-1,
6768
))?;
6869

69-
// Create a multiport console
7070
let console_id = krun_call_u32!(krun_add_virtio_console_multiport(ctx))?;
7171

72-
// Add 3 inout ports with different names
73-
let (port1_guest, port1_host) =
74-
add_console_port(ctx, console_id, "test-port-alpha")?;
75-
let (port2_guest, port2_host) =
76-
add_console_port(ctx, console_id, "test-port-beta")?;
77-
let (port3_guest, port3_host) =
78-
add_console_port(ctx, console_id, "test-port-gamma")?;
72+
test_port(ctx, console_id, "test-port-alpha")?;
73+
test_port(ctx, console_id, "test-port-beta")?;
74+
test_port(ctx, console_id, "test-port-gamma")?;
7975

80-
spawn_ping_pong_responder(port1_host);
81-
spawn_ping_pong_responder(port2_host);
82-
spawn_ping_pong_responder(port3_host);
83-
84-
// Keep the guest-side streams open for the VM
85-
mem::forget(port1_guest);
86-
mem::forget(port2_guest);
87-
mem::forget(port3_guest);
8876

8977
krun_call!(krun_set_vm_config(ctx, 1, 1024))?;
9078
setup_fs_and_enter(ctx, test_setup)?;
@@ -116,13 +104,12 @@ mod guest {
116104
let mut response = String::new();
117105
reader.read_line(&mut response).unwrap();
118106

119-
let expected = message.replace("PING", "PONG").trim().to_string();
120-
assert_eq!(response.trim(), expected, "{}: wrong response", name);
107+
let expected = message.replace("PING", "PONG").to_string();
108+
assert_eq!(response, expected, "{}: wrong response", name);
121109
}
122110

123111
impl Test for TestMultiportConsole {
124112
fn in_guest(self: Box<Self>) {
125-
// List all virtio ports to find our named ports
126113
let ports_dir = "/sys/class/virtio-ports";
127114

128115
let mut port_map = std::collections::HashMap::new();
@@ -144,7 +131,10 @@ mod guest {
144131
}
145132
}
146133

147-
// Verify we have all three ports
134+
assert!(
135+
port_map.contains_key("krun-stdout"),
136+
"krun-stdout not found"
137+
);
148138
assert!(
149139
port_map.contains_key("test-port-alpha"),
150140
"test-port-alpha not found"
@@ -158,6 +148,9 @@ mod guest {
158148
"test-port-gamma not found"
159149
);
160150

151+
// We shouldn't have any more than configured here
152+
assert_eq!(port_map.len(), 4);
153+
161154
test_port(&port_map, "test-port-alpha", "PING-ALPHA\n");
162155
test_port(&port_map, "test-port-beta", "PING-BETA\n");
163156
test_port(&port_map, "test-port-gamma", "PING-GAMMA\n");

0 commit comments

Comments
 (0)