Skip to content

Commit

Permalink
Refactor kernel interface to remove mock and global var
Browse files Browse the repository at this point in the history
When a unit testing scheme was originally designed for the kernel
interface module in 2017 it was turned into a stateful struct to
accomodate a mock test environment that allowed the tests to call the
real functions and specify what exact commands they expected to be run.

This structure has not been maintained, new tests are not written in
that style and instead use separate testing functions. But the original
tests have not been modified much since that time. Leaving Rita with a
KI global variable that in production doesn't actually do anything all
to support mocking during tests.

This commit removes the mock structure and turns kernel interface into
just a grab bag of utility functions where tests functions are all split
out internally rather than relying on mock.

While we are touching all of these files it will also be renamed to
system_interface rather than kernel interface as we now interface with a
lot more than just linux kernel functions from this module.
  • Loading branch information
jkilpatr committed Oct 7, 2024
1 parent 85578ed commit 3864368
Show file tree
Hide file tree
Showing 81 changed files with 3,180 additions and 4,175 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions althea_kernel_interface/src/babel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::KernelInterface;
use crate::run_command;

impl dyn KernelInterface {
pub fn restart_babel(&self) {
let _res = self.run_command("/etc/init.d/babeld", &["restart"]);
}
pub fn restart_babel() {
let _res = run_command("/etc/init.d/babeld", &["restart"]);
}
14 changes: 6 additions & 8 deletions althea_kernel_interface/src/bridge_tools.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! Simple helper functions for brctl
use super::KernelInterface;
use crate::run_command;
use crate::KernelInterfaceError as Error;
use std::process::Output;

impl dyn KernelInterface {
pub fn add_if_to_bridge(&self, br: &str, iface: &str) -> Result<Output, Error> {
self.run_command("brctl", &["addif", br, iface])
}
pub fn add_if_to_bridge(br: &str, iface: &str) -> Result<Output, Error> {
run_command("brctl", &["addif", br, iface])
}

pub fn del_if_from_bridge(&self, br: &str, iface: &str) -> Result<Output, Error> {
self.run_command("brctl", &["delif", br, iface])
}
pub fn del_if_from_bridge(br: &str, iface: &str) -> Result<Output, Error> {
run_command("brctl", &["delif", br, iface])
}
25 changes: 11 additions & 14 deletions althea_kernel_interface/src/check_cron.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use super::KernelInterface;
use crate::KernelInterfaceError as Error;
use std::process::{Command, Stdio};

impl dyn KernelInterface {
/// Checks if the cron service is running and starts it if it's not
pub fn check_cron(&self) -> Result<(), Error> {
Command::new("/etc/init.d/cron")
.args(["enable"])
.stdout(Stdio::piped())
.output()?;
Command::new("/etc/init.d/cron")
.args(["start"])
.stdout(Stdio::piped())
.output()?;
/// Checks if the cron service is running and starts it if it's not
pub fn check_cron() -> Result<(), Error> {
Command::new("/etc/init.d/cron")
.args(["enable"])
.stdout(Stdio::piped())
.output()?;
Command::new("/etc/init.d/cron")
.args(["start"])
.stdout(Stdio::piped())
.output()?;

Ok(())
}
Ok(())
}
Loading

0 comments on commit 3864368

Please sign in to comment.