From 182cae05d64a257cb02e065aae1049d5e4c73e42 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 7 Apr 2021 18:55:47 +0000 Subject: [PATCH] rust/client: Make status be a method In trying to use the API in ostree's tests, this feels like an obvious change. --- rust/rpmostree-client/src/lib.rs | 72 +++++++++++++++++--------------- rust/src/testutils.rs | 2 +- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/rust/rpmostree-client/src/lib.rs b/rust/rpmostree-client/src/lib.rs index 16f2458f..01f6ff9a 100644 --- a/rust/rpmostree-client/src/lib.rs +++ b/rust/rpmostree-client/src/lib.rs @@ -98,40 +98,44 @@ impl Deployment { } } -fn cli_cmd(c: &CliClient) -> Command { - let mut cmd = Command::new("rpm-ostree"); - cmd.env("RPMOSTREE_CLIENT_ID", c.agent_id.as_str()); - cmd -} - -/// Gather a snapshot of the system status. -pub fn query_status(c: &CliClient) -> Result { - // Retry on temporary activation failures, see - // https://github.com/coreos/rpm-ostree/issues/2531 - let pause = std::time::Duration::from_secs(1); - let max_retries = 10; - let mut retries = 0; - let cmd_res = loop { - retries += 1; - let res = cli_cmd(c) - .args(&["status", "--json"]) - .output() - .context("failed to spawn 'rpm-ostree status'")?; - - if res.status.success() || retries >= max_retries { - break res; - } - std::thread::sleep(pause); - }; - - if !cmd_res.status.success() { - return Err(format!( - "running 'rpm-ostree status' failed: {}", - String::from_utf8_lossy(&cmd_res.stderr) - ) - .into()); +impl CliClient { + /// Create an invocation of the client binary + fn cli_cmd(&self) -> Command { + let mut cmd = Command::new("rpm-ostree"); + cmd.env("RPMOSTREE_CLIENT_ID", self.agent_id.as_str()); + cmd } - Ok(serde_json::from_slice(&cmd_res.stdout) - .context("failed to parse 'rpm-ostree status' output")?) + /// Gather a snapshot of the system status. + pub fn query_status(&self) -> Result { + // Retry on temporary activation failures, see + // https://github.com/coreos/rpm-ostree/issues/2531 + let pause = std::time::Duration::from_secs(1); + let max_retries = 10; + let mut retries = 0; + let cmd_res = loop { + retries += 1; + let res = self + .cli_cmd() + .args(&["status", "--json"]) + .output() + .context("failed to spawn 'rpm-ostree status'")?; + + if res.status.success() || retries >= max_retries { + break res; + } + std::thread::sleep(pause); + }; + + if !cmd_res.status.success() { + return Err(format!( + "running 'rpm-ostree status' failed: {}", + String::from_utf8_lossy(&cmd_res.stderr) + ) + .into()); + } + + Ok(serde_json::from_slice(&cmd_res.stdout) + .context("failed to parse 'rpm-ostree status' output")?) + } } diff --git a/rust/src/testutils.rs b/rust/src/testutils.rs index 67b62293..51c5e3b6 100644 --- a/rust/src/testutils.rs +++ b/rust/src/testutils.rs @@ -227,7 +227,7 @@ fn update_os_tree(opts: &SyntheticUpgradeOpts) -> Result<()> { // to update the client bindings when adding new fields. fn validate_parse_status() -> Result<()> { let c = rpmostree_client::CliClient::new("tests"); - let s = rpmostree_client::query_status(&c).map_err(anyhow::Error::msg)?; + let s = c.query_status().map_err(anyhow::Error::msg)?; assert_ne!(s.deployments.len(), 0); Ok(()) }