Skip to content

Commit

Permalink
Add unit test for stdin input to c8y-remote-access-plugin
Browse files Browse the repository at this point in the history
Signed-off-by: James Rhodes <[email protected]>
  • Loading branch information
jarhodes314 committed May 14, 2024
1 parent d46f8f4 commit 9c1bafe
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions plugins/c8y_remote_access_plugin/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use miette::ensure;
use miette::miette;
use miette::Context;
use serde::Deserialize;
use std::io::stdin;
use std::io::BufRead;
use std::path::PathBuf;
use tedge_config::TEdgeConfigLocation;
Expand Down Expand Up @@ -45,6 +46,8 @@ pub struct C8yRemoteAccessPluginOpt {
connect_string: Option<String>,

#[arg(long)]
/// Specifies that this remote access command is a child process,
/// taking the SmartREST input as an argument
// Use "-" to read the value from stdin.
child: Option<String>,
}
Expand Down Expand Up @@ -80,7 +83,8 @@ impl TryFrom<C8yRemoteAccessPluginOpt> for Command {
C8yRemoteAccessPluginOpt {
child: Some(message),
..
} => RemoteAccessConnect::deserialize_smartrest(&message).map(Command::Connect),
} => RemoteAccessConnect::deserialize_smartrest(&message, stdin().lock())
.map(Command::Connect),
_ => Err(miette!(
"Expected one argument to the remote access plugin process"
)),
Expand All @@ -89,13 +93,12 @@ impl TryFrom<C8yRemoteAccessPluginOpt> for Command {
}

impl RemoteAccessConnect {
fn deserialize_smartrest(message: &str) -> miette::Result<Self> {
fn deserialize_smartrest(message: &str, mut stdin: impl BufRead) -> miette::Result<Self> {
// Read value from stdin
let message = if message.eq("-") {
let mut line = String::new();
let stdin = std::io::stdin();
stdin.lock().read_line(&mut line).unwrap();
line.to_string()
stdin.read_line(&mut line).unwrap();
line
} else {
message.to_string()
};
Expand All @@ -120,6 +123,7 @@ impl RemoteAccessConnect {

#[cfg(test)]
mod tests {
use std::io::Cursor;
use std::iter;

use super::*;
Expand Down Expand Up @@ -179,7 +183,7 @@ mod tests {
};

assert_eq!(
RemoteAccessConnect::deserialize_smartrest(input).unwrap(),
RemoteAccessConnect::deserialize_smartrest(input, Cursor::new("")).unwrap(),
expected
);
}
Expand All @@ -188,15 +192,31 @@ mod tests {
fn rejects_input_if_it_is_not_a_530_message() {
let input = "71,abcdef";

RemoteAccessConnect::deserialize_smartrest(input).unwrap_err();
RemoteAccessConnect::deserialize_smartrest(input, Cursor::new("")).unwrap_err();
}

#[test]
fn generates_the_target_address_by_combining_the_specified_host_and_port() {
let input = "530,jrh-rc-test0,127.0.0.1,22,cd8fc847-f4f2-4712-8dd7-31496aef0a7d";

let command = RemoteAccessConnect::deserialize_smartrest(input).unwrap();
let command = RemoteAccessConnect::deserialize_smartrest(input, Cursor::new("")).unwrap();

assert_eq!(command.target_address(), "127.0.0.1:22");
}

#[test]
fn parses_command_from_a_530_message_via_stdin() {
let input = "530,jrh-rc-test0,127.0.0.1,22,cd8fc847-f4f2-4712-8dd7-31496aef0a7d";
let expected = RemoteAccessConnect {
device_id: "jrh-rc-test0".into(),
host: "127.0.0.1".into(),
port: 22,
key: "cd8fc847-f4f2-4712-8dd7-31496aef0a7d".into(),
};

assert_eq!(
RemoteAccessConnect::deserialize_smartrest("-", Cursor::new(input)).unwrap(),
expected
);
}
}

0 comments on commit 9c1bafe

Please sign in to comment.