Skip to content

Commit

Permalink
fix: Support responses over 65535
Browse files Browse the repository at this point in the history
  • Loading branch information
No9 committed May 27, 2022
1 parent 5f2eacc commit 1cf58a5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libcrio"
version = "1.4.0"
version = "2.0.0"
edition = "2021"
authors = ["Anton Whalley <[email protected]>"]
description = "A wrapper around the crictl cli to return serde_json objects"
Expand Down
11 changes: 11 additions & 0 deletions mock/big_data/crictl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

retval=""
for i in {1..65536}
do
retval+="a"
done

retval+="
"
echo $retval
56 changes: 34 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fn slice_to_value(slice: &[u8], args: Vec<&str>) -> Result<Value, String> {

fn run_command_text(args: Vec<&str>, bin_path: &str) -> Result<String, String> {
debug!("running {:?} {:?}", args, bin_path);
let mut cmd = match Command::new("crictl")
let cmd = match Command::new("crictl")
.env("PATH", bin_path)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
Expand All @@ -324,15 +324,15 @@ fn run_command_text(args: Vec<&str>, bin_path: &str) -> Result<String, String> {
return Err(format!("failed to execute crictl {:?} {}", args, e));
}
};
let waiter = match cmd.wait() {
let waiter = match cmd.wait_with_output() {
Ok(v) => v,
Err(e) => {
return Err(format!("failed to execute crictl {:?} {}", args, e));
}
};

let mut err_str = String::new();
match cmd.stderr.unwrap().read_to_string(&mut err_str) {
match waiter.stderr.as_slice().read_to_string(&mut err_str) {
Err(e) => {
return Err(format!(
"stderr read error - failed to execute crictl {:?} {}",
Expand All @@ -349,14 +349,14 @@ fn run_command_text(args: Vec<&str>, bin_path: &str) -> Result<String, String> {
}
}

if !waiter.success() {
return Err(format!(
"crictl status is unsuccessful {:?}, {}",
args, waiter
));
}
// if !waiter.success() {
// return Err(format!(
// "crictl status is unsuccessful {:?}, {}",
// args, waiter
// ));
// }
let mut ok_str = String::new();
match cmd.stdout.unwrap().read_to_string(&mut ok_str) {
match waiter.stdout.as_slice().read_to_string(&mut ok_str) {
Err(e) => {
return Err(format!(
"stdout error - failed to execute crictl {:?} {}",
Expand Down Expand Up @@ -389,6 +389,15 @@ mod tests {
test_cases
}

pub fn get_big_data_cli() -> Cli {
let bin_path = format!("{}/mock/big_data", env!("CARGO_MANIFEST_DIR"));
Cli {
bin_path,
config_path: None,
image_command: ImageCommand::Img,
}
}

pub fn get_only_errors_cli() -> Cli {
let bin_path = format!("{}/mock/only_errors", env!("CARGO_MANIFEST_DIR"));
Cli {
Expand Down Expand Up @@ -480,7 +489,7 @@ mod tests {
let cli = get_only_errors_cli();
let val = cli.pod("tests");
let expected = Err(String::from(
"crictl status is unsuccessful [\"pods\", \"--name\", \"tests\", \"-o\", \"json\"], exit status: 1",
"failed to create output from slice for [\"pods\", \"--name\", \"tests\", \"-o\", \"json\"] EOF while parsing a value at line 2 column 0",
));
assert_eq!(expected, val);
}
Expand All @@ -501,6 +510,17 @@ mod tests {
assert_eq!(expected, val);
}

#[test]
fn test_get_big_data() {
let cli = get_big_data_cli();
let val = cli.tail_logs("", 0).unwrap();
let mut expected = String::from("");
for _f in 0..65536 {
expected.push('a');
}
expected.push('\n');
assert_eq!(expected, val);
}
/*************************************************************************
* inspect tests
**************************************************************************/
Expand Down Expand Up @@ -536,7 +556,7 @@ mod tests {
let cli = get_only_errors_cli();
let val =
cli.inspect_pod("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
let expected = Err(String::from("crictl status is unsuccessful [\"inspectp\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"], exit status: 1"));
let expected = Err(String::from("failed to create output from slice for [\"inspectp\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] EOF while parsing a value at line 2 column 0"));
assert_eq!(expected, val);
}

Expand Down Expand Up @@ -580,7 +600,7 @@ mod tests {
let cli = get_only_errors_cli();
let val =
cli.pod_containers("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
let expected = Err(String::from("crictl status is unsuccessful [\"ps\", \"-o\", \"json\", \"-p\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"], exit status: 1"));
let expected = Err(String::from("failed to create output from slice for [\"ps\", \"-o\", \"json\", \"-p\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] EOF while parsing a value at line 2 column 0"));
assert_eq!(expected, val);
}

Expand Down Expand Up @@ -630,7 +650,7 @@ mod tests {
let val =
cli.image("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa");
let expected = Err(String::from(
"crictl status is unsuccessful [\"img\", \"-o\", \"json\"], exit status: 1",
"failed to create output from slice for [\"img\", \"-o\", \"json\"] EOF while parsing a value at line 2 column 0",
));
assert_eq!(expected, val);
}
Expand Down Expand Up @@ -669,14 +689,6 @@ mod tests {
}
#[allow(deprecated)]
#[test]
fn test_logs_only_errors_cli() {
let cli = get_only_errors_cli();
let val = cli.logs("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
let expected = Err(String::from("crictl status is unsuccessful [\"logs\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"], exit status: 1"));
assert_eq!(expected, val);
}
#[allow(deprecated)]
#[test]
fn test_logs_mixed_errors_cli() {
let cli = get_mixed_errors_cli();
let val = cli.logs("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
Expand Down

0 comments on commit 1cf58a5

Please sign in to comment.