Skip to content

Commit

Permalink
Add unit tests for HostRequirements
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Aug 16, 2023
1 parent 78fb67f commit 9a48cc9
Showing 1 changed file with 81 additions and 3 deletions.
84 changes: 81 additions & 3 deletions src/devcontainer/requirements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use serde::{Deserialize, Serialize};

/// Host hardware requirements.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct HostRequirements {
/// Number of required CPUs. minimum: 1
Expand All @@ -36,7 +36,8 @@ pub struct HostRequirements {
pub gpu: Option<GPUVar>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum GPUVar {
/// Indicates whether a GPU is required.
Boolean(bool),
Expand All @@ -46,7 +47,7 @@ pub enum GPUVar {
Config(GPUConfig),
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GPUConfig {
/// Number of required cores. minimum: 1
Expand All @@ -57,3 +58,80 @@ pub struct GPUConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub memory: Option<String>,
}

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

#[test]
fn test_host_requirements() {
use serde_json::json;

let json = json!({
"cpus": 1,
"memory": "1gb",
"storage": "1gb",
"gpu": {
"cores": 1,
"memory": "1gb"
}
});
let host_requirements: HostRequirements = serde_json::from_value(json).unwrap();
assert_eq!(
host_requirements,
HostRequirements {
cpus: Some(1),
memory: Some("1gb".to_string()),
storage: Some("1gb".to_string()),
gpu: Some(GPUVar::Config(GPUConfig {
cores: Some(1),
memory: Some("1gb".to_string()),
})),
}
);
}

#[test]
fn test_host_requirements_gpu_boolean() {
use serde_json::json;

let json = json!({
"cpus": 1,
"memory": "1gb",
"storage": "1gb",
"gpu": true
});
let host_requirements: HostRequirements = serde_json::from_value(json).unwrap();
assert_eq!(
host_requirements,
HostRequirements {
cpus: Some(1),
memory: Some("1gb".to_string()),
storage: Some("1gb".to_string()),
gpu: Some(GPUVar::Boolean(true)),
}
);
}

#[test]
fn test_host_requirements_gpu_string() {
use serde_json::json;

let json = json!({
"cpus": 1,
"memory": "1gb",
"storage": "1gb",
"gpu": "optional"
});
let host_requirements: HostRequirements = serde_json::from_value(json).unwrap();
assert_eq!(
host_requirements,
HostRequirements {
cpus: Some(1),
memory: Some("1gb".to_string()),
storage: Some("1gb".to_string()),
gpu: Some(GPUVar::String("optional".to_string())),
}
);
}
}

0 comments on commit 9a48cc9

Please sign in to comment.