Skip to content

Commit

Permalink
Fix the feature test and turn on in CI (#2060)
Browse files Browse the repository at this point in the history
* reworked the justfile to be explicit with path

Signed-off-by: yihuaf <yihuaf@unkies.org>

* fix the feature tests

Signed-off-by: yihuaf <yihuaf@unkies.org>

* add the musl test

Signed-off-by: yihuaf <yihuaf@unkies.org>

* moving all stub into a single dir

Signed-off-by: yihuaf <yihuaf@unkies.org>

---------

Signed-off-by: yihuaf <yihuaf@unkies.org>
  • Loading branch information
yihuaf authored Jun 19, 2023
1 parent fe0dba4 commit f4e7e30
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
for file in ${{ steps.filter.outputs.all_modified_files }}; do
echo "$file was changed"
done
check:
needs: [changes]
if: needs.changes.outputs.any_modified == 'true'
Expand Down Expand Up @@ -66,6 +67,8 @@ jobs:
run: |
export LD_LIBRARY_PATH=$HOME/.wasmedge/lib
cd ./crates && cargo test --all --all-features --no-fail-fast
- name: Run feature tests
run: just featuretest

coverage:
needs: [changes]
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = ["tests/rust-integration-tests/*", "crates/*", "tools/*"]

[profile.release]
Expand Down
15 changes: 0 additions & 15 deletions crates/justfile

This file was deleted.

19 changes: 10 additions & 9 deletions crates/libcgroups/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use oci_spec::runtime::{
LinuxDevice, LinuxDeviceBuilder, LinuxDeviceCgroup, LinuxDeviceCgroupBuilder, LinuxDeviceType,
};

#[cfg(feature = "systemd")]
use super::systemd;
#[cfg(feature = "v1")]
use super::v1;
#[cfg(feature = "v2")]
use super::v2;

use super::stats::Stats;
Expand Down Expand Up @@ -354,8 +351,10 @@ fn create_v1_cgroup_manager(
}

#[cfg(not(feature = "v1"))]
fn create_v1_cgroup_manager(_cgroup_path: PathBuf) -> Result<Box<dyn CgroupManager>> {
bail!("cgroup v1 feature is required, but was not enabled during compile time");
fn create_v1_cgroup_manager(
_cgroup_path: PathBuf,
) -> Result<v1::manager::Manager, v1::manager::V1ManagerError> {
Err(v1::manager::V1ManagerError::NotEnabled)
}

#[cfg(feature = "v2")]
Expand All @@ -367,8 +366,10 @@ fn create_v2_cgroup_manager(
}

#[cfg(not(feature = "v2"))]
fn create_v2_cgroup_manager(_cgroup_path: PathBuf) -> Result<Box<dyn CgroupManager>> {
bail!("cgroup v2 feature is required, but was not enabled during compile time");
fn create_v2_cgroup_manager(
_cgroup_path: PathBuf,
) -> Result<v2::manager::Manager, v2::manager::V2ManagerError> {
Err(v2::manager::V2ManagerError::NotEnabled)
}

#[cfg(feature = "systemd")]
Expand Down Expand Up @@ -400,8 +401,8 @@ fn create_systemd_cgroup_manager(
fn create_systemd_cgroup_manager(
_cgroup_path: PathBuf,
_container_name: &str,
) -> Result<Box<dyn CgroupManager>> {
bail!("systemd cgroup feature is required, but was not enabled during compile time");
) -> Result<systemd::manager::Manager, systemd::manager::SystemdManagerError> {
Err(systemd::manager::SystemdManagerError::NotEnabled)
}

pub fn get_all_pids(path: &Path) -> Result<Vec<Pid>, WrappedIoError> {
Expand Down
9 changes: 9 additions & 0 deletions crates/libcgroups/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ pub mod common;
pub mod stats;
#[cfg(feature = "systemd")]
pub mod systemd;
#[cfg(not(feature = "systemd"))]
#[path = "stub/systemd/mod.rs"]
pub mod systemd;
pub mod test_manager;
#[cfg(feature = "v1")]
pub mod v1;
#[cfg(not(feature = "v1"))]
#[path = "stub/v1/mod.rs"]
pub mod v1;
#[cfg(feature = "v2")]
pub mod v2;
#[cfg(not(feature = "v2"))]
#[path = "stub/v2/mod.rs"]
pub mod v2;
43 changes: 43 additions & 0 deletions crates/libcgroups/src/stub/systemd/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::common::{AnyCgroupManager, CgroupManager};

#[derive(thiserror::Error, Debug)]
pub enum SystemdManagerError {
#[error("systemd cgroup feature is required, but was not enabled during compile time")]
NotEnabled,
}

pub struct Manager {}

impl Manager {
pub fn any(self) -> AnyCgroupManager {
AnyCgroupManager::Systemd(self)
}
}

impl CgroupManager for Manager {
type Error = SystemdManagerError;

fn add_task(&self, _pid: nix::unistd::Pid) -> Result<(), Self::Error> {
Err(SystemdManagerError::NotEnabled)
}

fn apply(&self, _controller_opt: &crate::common::ControllerOpt) -> Result<(), Self::Error> {
Err(SystemdManagerError::NotEnabled)
}

fn remove(&self) -> Result<(), Self::Error> {
Err(SystemdManagerError::NotEnabled)
}

fn freeze(&self, _state: crate::common::FreezerState) -> Result<(), Self::Error> {
Err(SystemdManagerError::NotEnabled)
}

fn stats(&self) -> Result<crate::stats::Stats, Self::Error> {
Err(SystemdManagerError::NotEnabled)
}

fn get_all_pids(&self) -> Result<Vec<nix::unistd::Pid>, Self::Error> {
Err(SystemdManagerError::NotEnabled)
}
}
1 change: 1 addition & 0 deletions crates/libcgroups/src/stub/systemd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod manager;
43 changes: 43 additions & 0 deletions crates/libcgroups/src/stub/v1/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::common::{AnyCgroupManager, CgroupManager};

#[derive(thiserror::Error, Debug)]
pub enum V1ManagerError {
#[error("v1 cgroup feature is required, but was not enabled during compile time")]
NotEnabled,
}

pub struct Manager {}

impl Manager {
pub fn any(self) -> AnyCgroupManager {
crate::common::AnyCgroupManager::V1(self)
}
}

impl CgroupManager for Manager {
type Error = V1ManagerError;

fn add_task(&self, _pid: nix::unistd::Pid) -> Result<(), Self::Error> {
Err(V1ManagerError::NotEnabled)
}

fn apply(&self, _controller_opt: &crate::common::ControllerOpt) -> Result<(), Self::Error> {
Err(V1ManagerError::NotEnabled)
}

fn remove(&self) -> Result<(), Self::Error> {
Err(V1ManagerError::NotEnabled)
}

fn freeze(&self, _state: crate::common::FreezerState) -> Result<(), Self::Error> {
Err(V1ManagerError::NotEnabled)
}

fn stats(&self) -> Result<crate::stats::Stats, Self::Error> {
Err(V1ManagerError::NotEnabled)
}

fn get_all_pids(&self) -> Result<Vec<nix::unistd::Pid>, Self::Error> {
Err(V1ManagerError::NotEnabled)
}
}
1 change: 1 addition & 0 deletions crates/libcgroups/src/stub/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod manager;
43 changes: 43 additions & 0 deletions crates/libcgroups/src/stub/v2/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::common::{AnyCgroupManager, CgroupManager};

#[derive(thiserror::Error, Debug)]
pub enum V2ManagerError {
#[error("v2 cgroup feature is required, but was not enabled during compile time")]
NotEnabled,
}

pub struct Manager {}

impl Manager {
pub fn any(self) -> AnyCgroupManager {
crate::common::AnyCgroupManager::V2(self)
}
}

impl CgroupManager for Manager {
type Error = V2ManagerError;

fn add_task(&self, _pid: nix::unistd::Pid) -> Result<(), Self::Error> {
Err(V2ManagerError::NotEnabled)
}

fn apply(&self, _controller_opt: &crate::common::ControllerOpt) -> Result<(), Self::Error> {
Err(V2ManagerError::NotEnabled)
}

fn remove(&self) -> Result<(), Self::Error> {
Err(V2ManagerError::NotEnabled)
}

fn freeze(&self, _state: crate::common::FreezerState) -> Result<(), Self::Error> {
Err(V2ManagerError::NotEnabled)
}

fn stats(&self) -> Result<crate::stats::Stats, Self::Error> {
Err(V2ManagerError::NotEnabled)
}

fn get_all_pids(&self) -> Result<Vec<nix::unistd::Pid>, Self::Error> {
Err(V2ManagerError::NotEnabled)
}
}
1 change: 1 addition & 0 deletions crates/libcgroups/src/stub/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod manager;
1 change: 1 addition & 0 deletions crates/libcgroups/src/v1/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::v1::ControllerType;
pub struct Manager {
subsystems: HashMap<CtrlType, PathBuf>,
}

#[derive(thiserror::Error, Debug)]
pub enum V1ManagerError {
#[error("io error: {0}")]
Expand Down
3 changes: 1 addition & 2 deletions crates/libcontainer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ systemd = ["libcgroups/systemd", "v2"]
v2 = ["libcgroups/v2"]
v1 = ["libcgroups/v1"]
cgroupsv2_devices = ["libcgroups/cgroupsv2_devices"]
test_utils = ["dep:rand"]

[dependencies]
bitflags = "2.3.2"
Expand All @@ -42,11 +41,11 @@ regex = "1.8.4"
thiserror = "1.0.24"
tracing = { version = "0.1.37", features = ["attributes"]}
safe-path = "0.1.0"
rand = { version = "0.8.5", optional = true }

[dev-dependencies]
oci-spec = { version = "^0.6.0", features = ["proptests", "runtime"] }
quickcheck = "1"
serial_test = "2.0.0"
tempfile = "3"
anyhow = "1.0"
rand = { version = "0.8.5" }
1 change: 0 additions & 1 deletion crates/libcontainer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub mod rootless;
pub mod seccomp;
pub mod signal;
pub mod syscall;
#[cfg(feature = "test_utils")]
pub mod test_utils;
pub mod tty;
pub mod utils;
Expand Down
2 changes: 1 addition & 1 deletion crates/libcontainer/src/rootfs/symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ mod tests {
#[cfg(feature = "v1")]
use crate::syscall::linux::LinuxSyscall;
use crate::syscall::test::TestHelperSyscall;
use anyhow::{Context, Result};
#[cfg(feature = "v1")]
use anyhow::{Context, Result};
use nix::{
fcntl::{open, OFlag},
sys::stat::Mode,
Expand Down
8 changes: 5 additions & 3 deletions crates/libcontainer/src/rootless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,18 @@ fn write_id_mapping(
mod tests {
use std::fs;

use super::*;
use anyhow::Result;
use nix::unistd::getpid;
use oci_spec::runtime::{
LinuxBuilder, LinuxIdMappingBuilder, LinuxNamespaceBuilder, SpecBuilder,
};
use rand::Rng;
use serial_test::serial;

use crate::test_utils::gen_u32;

use super::*;
fn gen_u32() -> u32 {
rand::thread_rng().gen()
}

#[test]
fn test_validate_ok() -> Result<()> {
Expand Down
9 changes: 4 additions & 5 deletions crates/libcontainer/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use nix::sys::wait;
use rand::Rng;
use serde::{Deserialize, Serialize};

// Normally, error types are not implemented as serialize/deserialize, but to
Expand Down Expand Up @@ -84,11 +83,15 @@ where
let (mut sender, mut receiver) = crate::channel::channel::<ClosureResult>()?;
match unsafe { nix::unistd::fork().map_err(TestError::Fork)? } {
nix::unistd::ForkResult::Parent { child } => {
// Close unused senders
sender.close().map_err(TestError::Channel)?;
let res = receiver.recv().map_err(TestError::Channel)?;
wait::waitpid(child, None).map_err(TestError::Wait)?;
res.map_err(|err| TestError::Execution(Box::new(err)))?;
}
nix::unistd::ForkResult::Child => {
// Close unused receiver in the child
receiver.close().map_err(TestError::Channel)?;
let test_result = match std::panic::catch_unwind(cb) {
Ok(ret) => ret.map_err(|err| ErrorEnclosure::new(&err)),
Err(_) => Err(ErrorEnclosure::new(&TestError::Panic)),
Expand All @@ -104,10 +107,6 @@ where
Ok(())
}

pub fn gen_u32() -> u32 {
rand::thread_rng().gen()
}

#[cfg(test)]
mod tests {
use core::panic;
Expand Down
1 change: 0 additions & 1 deletion crates/youki/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ tracing-journald = "0.3.0"
serial_test = "2.0.0"
tempfile = "3"
scopeguard = "1.1.0"
libcontainer = {version = "0.0.5", path = "../libcontainer", features = ["test_utils"]}

[build-dependencies]
anyhow = "1.0.71"
Expand Down
Loading

0 comments on commit f4e7e30

Please sign in to comment.