Skip to content

Commit

Permalink
feat: add tail option to log
Browse files Browse the repository at this point in the history
  • Loading branch information
No9 committed Jan 23, 2022
1 parent 05f2f04 commit 0212a35
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
11 changes: 11 additions & 0 deletions mock/long_logs/crictl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

export cmd=""$1
line_count=$(echo $2 | cut -d'=' -f 2)
if [ "$cmd" = "logs" ]
then
for (( i=1; i<=$line_count; i++))
do
echo "logging ${i}"
done
fi
57 changes: 56 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Cli {
}
}

/// Returns a JSON value containing the images related to a container
/// Returns a text value containing the logs related to a container
///
/// # Arguments
///
Expand All @@ -218,8 +218,10 @@ impl Cli {
/// bin_path,
/// ..Default::default()
/// };
/// #[allow(deprecated)]
/// let val = cli.logs("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa").unwrap();
/// ```
#[deprecated]
pub fn logs(&self, container_id: &str) -> Result<String, String> {
let log_output_args = match &self.config_path {
Some(s) => vec!["-c", s.as_str(), "logs", container_id],
Expand All @@ -228,6 +230,34 @@ impl Cli {
run_command_text(log_output_args, &self.bin_path)
}

/// Returns a text value containing the logs related to a container
///
/// # Arguments
///
/// * `container_id` - The container_id related to one of the containers obtained from `pod_containers`
///
/// * `line_count` - The number of lines to take from the end of the log.
///
/// # Examples
///
/// ```
/// use libcrio::Cli;
/// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
/// let cli = Cli {
/// bin_path,
/// ..Default::default()
/// };
/// let val = cli.tail_logs("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa", 500).unwrap();
/// ```
pub fn tail_logs(&self, container_id: &str, line_count: u32) -> Result<String, String> {
let tailoption = format!("--tail={}", line_count);
let log_output_args = match &self.config_path {
Some(s) => vec!["-c", s.as_str(), "logs", tailoption.as_str(), container_id],
None => vec!["logs", tailoption.as_str(), container_id],
};
run_command_text(log_output_args, &self.bin_path)
}

/// # Arguments
///
/// * `path` - The additional path to append to bin_path,
Expand Down Expand Up @@ -352,6 +382,15 @@ mod tests {
}
}

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

pub fn get_mixed_errors_cli() -> Cli {
let bin_path = format!("{}/mock/mixed_errors", env!("CARGO_MANIFEST_DIR"));
Cli {
Expand Down Expand Up @@ -602,6 +641,7 @@ mod tests {
/*************************************************************************
* log tests
**************************************************************************/
#[allow(deprecated)]
#[test]
fn test_logs() {
for cli in get_clis() {
Expand All @@ -611,13 +651,15 @@ mod tests {
assert_eq!(val, "A LOG\n".to_string())
}
}
#[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("failed to execute crictl [\"logs\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] "));
assert_eq!(expected, val);
}
#[allow(deprecated)]
#[test]
fn test_logs_mixed_errors_cli() {
let cli = get_mixed_errors_cli();
Expand All @@ -627,4 +669,17 @@ mod tests {
));
assert_eq!(expected, val);
}
#[test]
fn test_tail_logs() {
let cli = get_long_logs_cli();
let val = cli
.tail_logs(
"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6",
500,
)
.unwrap();
assert_eq!(val.lines().count(), 500);
assert!(val.ends_with("logging 500\n"));
assert!(!val.contains("logging 501"));
}
}

0 comments on commit 0212a35

Please sign in to comment.