diff --git a/KubeOS-Rust/agent/src/rpc/agent_impl.rs b/KubeOS-Rust/agent/src/rpc/agent_impl.rs index 5f3a32590ee5bae65be56e5dcc399a724e8c3986..ab826413559cab6ed3240633c78a8325b072ab38 100644 --- a/KubeOS-Rust/agent/src/rpc/agent_impl.rs +++ b/KubeOS-Rust/agent/src/rpc/agent_impl.rs @@ -57,7 +57,10 @@ impl Default for AgentImpl { impl AgentImpl { fn prepare_upgrade_impl(&self, req: UpgradeRequest) -> Result { - let _lock = self.mutex.lock().unwrap(); + let lock = self.mutex.try_lock(); + if lock.is_err() { + bail!("os-agent is processing another request"); + } debug!("Received an 'prepare upgrade' request: {:?}", req); info!("Start preparing for upgrading to version: {}", req.version); @@ -76,7 +79,10 @@ impl AgentImpl { } fn upgrade_impl(&self) -> Result { - let _lock = self.mutex.lock().unwrap(); + let lock = self.mutex.try_lock(); + if lock.is_err() { + bail!("os-agent is processing another request"); + } info!("Start to upgrade"); let command_executor = RealCommandExecutor {}; let (_, next_partition_info) = get_partition_info(&command_executor)?; @@ -91,7 +97,10 @@ impl AgentImpl { } fn configure_impl(&self, mut req: ConfigureRequest) -> Result { - let _lock = self.mutex.lock().unwrap(); + let lock = self.mutex.try_lock(); + if lock.is_err() { + bail!("os-agent is processing another request"); + } debug!("Received a 'configure' request: {:?}", req); info!("Start to configure"); let config_map = &*CONFIG_TEMPLATE; @@ -108,7 +117,10 @@ impl AgentImpl { } fn rollback_impl(&self) -> Result { - let _lock = self.mutex.lock().unwrap(); + let lock = self.mutex.try_lock(); + if lock.is_err() { + bail!("os-agent is processing another request"); + } info!("Start to rollback"); let command_executor = RealCommandExecutor {}; let (_, next_partition_info) = get_partition_info(&command_executor)?; @@ -172,6 +184,18 @@ mod test { }; let res = agent.configure(req); assert!(res.is_err()); + + // test lock + let _lock = agent.mutex.lock().unwrap(); + let req = ConfigureRequest { + configs: vec![Sysconfig { + model: "kernel.sysctl".to_string(), + config_path: "".to_string(), + contents: HashMap::new(), + }], + }; + let res = agent.configure(req); + assert!(res.is_err()); } #[test] diff --git a/KubeOS-Rust/manager/src/utils/partition.rs b/KubeOS-Rust/manager/src/utils/partition.rs index fcfa2d8b87ff7e4e8cf893114dedfc55f3132675..799b4b35d63bdcfbe06c0c620dca5e68d7f5b464 100644 --- a/KubeOS-Rust/manager/src/utils/partition.rs +++ b/KubeOS-Rust/manager/src/utils/partition.rs @@ -50,7 +50,7 @@ pub fn get_partition_info(executor: &T) -> Result<(Partition } } } - if cur_partition.device.is_empty() { + if cur_partition.menuentry.is_empty() { bail!("Failed to get partition info, lsblk output: {}", lsblk); } Ok((cur_partition, next_partition)) @@ -108,5 +108,10 @@ mod tests { mock.expect_run_command_with_output().times(1).returning(|_, _| Ok(command_output3.to_string())); let res = get_partition_info(&mock); assert!(res.is_err()); + + let command_output4 = "sda4 / ext4"; + mock.expect_run_command_with_output().times(1).returning(|_, _| Ok(command_output4.to_string())); + let res = get_partition_info(&mock); + assert!(res.is_err()); } }