Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for container and configmap #325

Merged
merged 3 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/unit_tests/kubernetes_api_objects/config_map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::api_resource::*;
use crate::kubernetes_api_objects::config_map::*;
use crate::kubernetes_api_objects::object_meta::*;
use crate::kubernetes_api_objects::resource::*;
Expand Down Expand Up @@ -30,4 +31,50 @@ pub fn test_set_data(){
assert_eq!(data.into_rust_map(), config_map.into_kube().data.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_default(){
let config_map = ConfigMap::default();
assert_eq!(config_map.into_kube(), deps_hack::k8s_openapi::api::core::v1::ConfigMap::default());
}

#[test]
#[verifier(external)]
pub fn test_clone(){
let mut config_map = ConfigMap::default();
let mut data = StringMap::new();
data.insert(new_strlit("key").to_string(), new_strlit("value").to_string());
config_map.set_data(data.clone());
let config_map_clone = config_map.clone();
assert_eq!(config_map.into_kube(), config_map_clone.into_kube());
}

#[test]
#[verifier(external)]
pub fn test_metadata(){
let mut object_meta = ObjectMeta::default();
object_meta.set_name(new_strlit("name").to_string());

let mut config_map = ConfigMap::default();
config_map.set_metadata(object_meta.clone());
assert_eq!(object_meta.into_kube(), config_map.metadata().into_kube());
}

#[test]
#[verifier(external)]
pub fn test_data(){
let mut config_map = ConfigMap::default();
let mut data = StringMap::new();
data.insert(new_strlit("key").to_string(), new_strlit("value").to_string());
config_map.set_data(data.clone());
assert_eq!(data.into_rust_map(), config_map.data().unwrap().into_rust_map());
}

#[test]
#[verifier(external)]
pub fn test_api_resource(){
let api_resource = ConfigMap::api_resource();
assert_eq!(api_resource.into_kube().kind, "ConfigMap");
}

}
32 changes: 32 additions & 0 deletions src/unit_tests/kubernetes_api_objects/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,36 @@ pub fn test_set_env() {
container.set_env(vec![env_var.clone()]);
assert_eq!(vec![env_var.into_kube()], container.into_kube().env.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_default(){
let container = Container::default();
assert_eq!(container.into_kube(), deps_hack::k8s_openapi::api::core::v1::Container::default());
}

#[test]
#[verifier(external)]
pub fn test_clone(){
let mut container = Container::default();
container.set_image(new_strlit("image").to_string());
let container_clone = container.clone();
assert_eq!(container.into_kube(), container_clone.into_kube());
}

#[test]
#[verifier(external)]
pub fn test_overwrite_resources(){
let mut container = Container::default();
let mut resources = ResourceRequirements::default();
let mut requests = StringMap::new();
requests.insert(new_strlit("cpu").to_string(), new_strlit("100m").to_string());
resources.set_requests(requests);
container. overwrite_resources(Some(resources.clone()));
assert_eq!(resources.into_kube(), container.into_kube().resources.unwrap());
let mut container_2 = Container::default();
let resources_2 = None;
container_2.overwrite_resources(resources_2);
assert_eq!(None, container_2.into_kube().resources);
}
}
57 changes: 57 additions & 0 deletions src/unit_tests/kubernetes_api_objects/env_var.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::container::*;
use crate::kubernetes_api_objects::object_meta::*;
use crate::kubernetes_api_objects::resource::*;
use crate::kubernetes_api_objects::volume::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for env var
#[test]
#[verifier(external)]
pub fn test_default(){
let env = EnvVar::default();
assert_eq!(env.into_kube(), deps_hack::k8s_openapi::api::core::v1::EnvVar::default());
}

#[test]
#[verifier(external)]
pub fn test_clone(){
let mut env = EnvVar::default();
env.set_name(new_strlit("name").to_string());
env.overwrite_value(Some(new_strlit("value").to_string()));
let env_clone = env.clone();
assert_eq!(env.into_kube(), env_clone.into_kube());
}

#[test]
#[verifier(external)]
pub fn test_set_name(){
let mut env_var = EnvVar::default();
env_var.set_name(new_strlit("name").to_string());
assert_eq!("name".to_string(), env_var.into_kube().name);
}

#[test]
#[verifier(external)]
pub fn test_overwrite_value(){
let mut env_var = EnvVar::default();
env_var.overwrite_value(Some(new_strlit("value").to_string()));
assert_eq!("value".to_string(), env_var.into_kube().value.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_overwrite_value_from(){
let mut env_var = EnvVar::default();
let mut env_var_source = EnvVarSource::default();
let mut object_field_selector = ObjectFieldSelector::default();
object_field_selector.set_field_path(new_strlit("field_path").to_string());
env_var_source.set_field_ref(object_field_selector);
env_var.overwrite_value_from(Some(env_var_source.clone()));
assert_eq!(env_var_source.into_kube(), env_var.into_kube().value_from.unwrap());
}
}
42 changes: 42 additions & 0 deletions src/unit_tests/kubernetes_api_objects/env_var_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::container::*;
use crate::kubernetes_api_objects::object_meta::*;
use crate::kubernetes_api_objects::resource::*;
use crate::kubernetes_api_objects::volume::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for EnvVarSource
#[test]
#[verifier(external)]
pub fn test_set_field_ref() {
let mut env_var_source = EnvVarSource::default();
let mut object_field_selector = ObjectFieldSelector::default();
object_field_selector.set_field_path(new_strlit("field_path").to_string());
let mut object_field_selector_2 = ObjectFieldSelector::default();
object_field_selector_2.set_field_path(new_strlit("field_path").to_string());
env_var_source.set_field_ref(object_field_selector);
assert_eq!(object_field_selector_2.into_kube(), env_var_source.into_kube().field_ref.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_default(){
let env_var_source = EnvVarSource::default();
assert_eq!(env_var_source.into_kube(), deps_hack::k8s_openapi::api::core::v1::EnvVarSource::default());
}

#[test]
#[verifier(external)]
pub fn test_clone(){
let mut env_var_source = EnvVarSource::default();
let mut object_field_selector = ObjectFieldSelector::default();
object_field_selector.set_field_path(new_strlit("field_path").to_string());
env_var_source.set_field_ref(object_field_selector);
let env_var_source_clone = env_var_source.clone();
assert_eq!(env_var_source.into_kube(), env_var_source_clone.into_kube());
}
}
42 changes: 42 additions & 0 deletions src/unit_tests/kubernetes_api_objects/execaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::container::*;
use crate::kubernetes_api_objects::object_meta::*;
use crate::kubernetes_api_objects::resource::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for life cycle
#[test]
#[verifier(external)]
pub fn test_set_command() {
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
assert_eq!(
vec!["command".to_string()],
exec_action.into_kube().command.unwrap()
);
}

#[test]
#[verifier(external)]
pub fn test_default() {
let exec_action = ExecAction::default();
assert_eq!(
exec_action.into_kube(),
deps_hack::k8s_openapi::api::core::v1::ExecAction::default()
);
}

#[test]
#[verifier(external)]
pub fn test_clone() {
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
let exec_action_clone = exec_action.clone();
assert_eq!(exec_action.into_kube(), exec_action_clone.into_kube());
}

}
33 changes: 15 additions & 18 deletions src/unit_tests/kubernetes_api_objects/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,35 @@ verus! {
// Tests for life cycle
#[test]
#[verifier(external)]
pub fn test_set_command() {
pub fn test_set_pre_stop() {
let mut lifecycle = Lifecycle::default();
let mut handler = LifecycleHandler::default();
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
handler.set_exec(exec_action);

lifecycle.set_pre_stop(handler.clone());
assert_eq!(
vec!["command".to_string()],
exec_action.into_kube().command.unwrap()
handler.into_kube(),
lifecycle.into_kube().pre_stop.unwrap()
);
}

#[test]
#[verifier(external)]
pub fn test_set_exec() {
let mut handler = LifecycleHandler::default();
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
handler.set_exec(exec_action.clone());
assert_eq!(exec_action.into_kube(), handler.into_kube().exec.unwrap());
pub fn test_default(){
let lifecycle = Lifecycle::default();
assert_eq!(lifecycle.into_kube(), deps_hack::k8s_openapi::api::core::v1::Lifecycle::default());
}

#[test]
#[verifier(external)]
pub fn test_set_pre_stop() {
let mut lifecycle = Lifecycle::default();
pub fn test_clone(){
let mut handler = LifecycleHandler::default();
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
handler.set_exec(exec_action);

lifecycle.set_pre_stop(handler.clone());
assert_eq!(
handler.into_kube(),
lifecycle.into_kube().pre_stop.unwrap()
);
handler.set_exec(exec_action.clone());
let handler_clone = handler.clone();
assert_eq!(handler.into_kube(), handler_clone.into_kube());
}
}
39 changes: 39 additions & 0 deletions src/unit_tests/kubernetes_api_objects/lifecycle_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::container::*;
use crate::kubernetes_api_objects::object_meta::*;
use crate::kubernetes_api_objects::resource::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for life cycle handler
#[test]
#[verifier(external)]
pub fn test_set_exec() {
let mut handler = LifecycleHandler::default();
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
handler.set_exec(exec_action.clone());
assert_eq!(exec_action.into_kube(), handler.into_kube().exec.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_default(){
let handler = LifecycleHandler::default();
assert_eq!(handler.into_kube(), deps_hack::k8s_openapi::api::core::v1::LifecycleHandler::default());
}

#[test]
#[verifier(external)]
pub fn test_clone(){
let mut handler = LifecycleHandler::default();
let mut exec_action = ExecAction::default();
exec_action.set_command(vec![new_strlit("command").to_string()]);
handler.set_exec(exec_action.clone());
let handler_clone = handler.clone();
assert_eq!(handler.into_kube(), handler_clone.into_kube());
}
}
7 changes: 7 additions & 0 deletions src/unit_tests/kubernetes_api_objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
// SPDX-License-Identifier: MIT
pub mod config_map;
pub mod container;
pub mod env_var;
pub mod env_var_source;
pub mod execaction;
pub mod lifecycle;
pub mod lifecycle_handler;
pub mod object_field_selector;
pub mod object_meta;
pub mod probe;
pub mod resource_requirements;
pub mod tcp_socket_action;
pub mod volume_mount;
35 changes: 35 additions & 0 deletions src/unit_tests/kubernetes_api_objects/object_field_selector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::object_meta::*;
use crate::kubernetes_api_objects::resource::*;
use crate::kubernetes_api_objects::volume::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for ObjectFieldSelector
#[test]
#[verifier(external)]
pub fn test_set_field_path() {
let mut object_field_selector = ObjectFieldSelector::default();
object_field_selector.set_field_path(new_strlit("field_path").to_string());
assert_eq!("field_path".to_string(), object_field_selector.into_kube().field_path);
}

#[test]
#[verifier(external)]
pub fn test_set_api_version() {
let mut object_field_selector = ObjectFieldSelector::default();
object_field_selector.set_api_version(new_strlit("api_version").to_string());
assert_eq!("api_version".to_string(), object_field_selector.into_kube().api_version.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_default() {
let object_field_selector = ObjectFieldSelector::default();
assert_eq!(object_field_selector.into_kube(), deps_hack::k8s_openapi::api::core::v1::ObjectFieldSelector::default());
}

}
Loading