Skip to content

Commit

Permalink
Merge pull request youki-dev#75 from tsturzl/main
Browse files Browse the repository at this point in the history
Improved testing, property testing, device tests
  • Loading branch information
utam0k authored Jun 10, 2021
2 parents 0212a88 + 4881e41 commit 32743ec
Show file tree
Hide file tree
Showing 12 changed files with 630 additions and 103 deletions.
219 changes: 141 additions & 78 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ once_cell = "1.6.0"
futures = { version = "0.3", features = ["thread-pool"] }
regex = "1.5"
oci_spec = { version = "0.1.0", path = "./oci_spec" }

[dev-dependencies]
oci_spec = { version = "0.1.0", path = "./oci_spec", features = ["proptests"] }
quickcheck = "1"
1 change: 1 addition & 0 deletions oci_spec/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
98 changes: 98 additions & 0 deletions oci_spec/Cargo.lock

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

6 changes: 5 additions & 1 deletion oci_spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ name = "oci_spec"
version = "0.1.0"
edition = "2018"

[features]
default = []
proptests = ["quickcheck"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
nix = "0.19.1"
anyhow = "1.0"
serde_json = "1.0"
caps = "0.5.1"

quickcheck = { version = "1", optional = true }
92 changes: 92 additions & 0 deletions oci_spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,95 @@ impl Spec {
Ok(spec)
}
}

#[cfg(feature = "proptests")]
use quickcheck::{Arbitrary, Gen};

#[cfg(feature = "proptests")]
fn some_none_generator_util<T: Arbitrary>(g: &mut Gen) -> Option<T> {
let choice = g.choose(&[true, false]).unwrap();
match choice {
false => None,
true => Some(T::arbitrary(g)),
}
}

#[cfg(feature = "proptests")]
impl Arbitrary for LinuxDeviceCgroup {
fn arbitrary(g: &mut Gen) -> LinuxDeviceCgroup {
let typ_choices = ["b", "c", "u", "p", "a"];

let typ_chosen = g.choose(&typ_choices).unwrap();

let typ = match typ_chosen.to_string().as_str() {
"b" => LinuxDeviceType::B,
"c" => LinuxDeviceType::C,
"u" => LinuxDeviceType::U,
"p" => LinuxDeviceType::P,
"a" => LinuxDeviceType::A,
_ => LinuxDeviceType::A,
};

let access_choices = ["rwm", "m"];
LinuxDeviceCgroup {
allow: bool::arbitrary(g),
typ,
major: some_none_generator_util::<i64>(g),
minor: some_none_generator_util::<i64>(g),
access: g.choose(&access_choices).unwrap().to_string(),
}
}
}

#[cfg(feature = "proptests")]
impl Arbitrary for LinuxMemory {
fn arbitrary(g: &mut Gen) -> LinuxMemory {
LinuxMemory {
kernel: some_none_generator_util::<i64>(g),
kernel_tcp: some_none_generator_util::<i64>(g),
limit: some_none_generator_util::<i64>(g),
reservation: some_none_generator_util::<i64>(g),
swap: some_none_generator_util::<i64>(g),
swappiness: some_none_generator_util::<u64>(g),
}
}
}

#[cfg(feature = "proptests")]
impl Arbitrary for LinuxHugepageLimit {
fn arbitrary(g: &mut Gen) -> LinuxHugepageLimit {
let unit_choice = ["KB", "MB", "GB"];
let unit = g.choose(&unit_choice).unwrap();
let page_size = u64::arbitrary(g).to_string() + unit;

LinuxHugepageLimit {
page_size,
limit: i64::arbitrary(g),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_linux_device_cgroup_to_string() {
let ldc = LinuxDeviceCgroup {
allow: true,
typ: LinuxDeviceType::A,
major: None,
minor: None,
access: "rwm".into(),
};
assert_eq!(ldc.to_string(), "a *:* rwm");
let ldc = LinuxDeviceCgroup {
allow: true,
typ: LinuxDeviceType::A,
major: Some(1),
minor: Some(9),
access: "rwm".into(),
};
assert_eq!(ldc.to_string(), "a 1:9 rwm");
}
}
Loading

0 comments on commit 32743ec

Please sign in to comment.