Skip to content

Commit

Permalink
Add more unit tests (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZichengMa committed Dec 18, 2023
1 parent 619d993 commit 3f1f6c0
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/unit_tests/kubernetes_api_objects/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ pub fn test_default(){
assert_eq!(container.into_kube(), deps_hack::k8s_openapi::api::core::v1::Container::default());
}

#[test]
#[verifier(external)]
pub fn test_set_args(){
let mut container = Container::default();
container.set_args(vec![new_strlit("args").to_string()]);
assert_eq!(vec!["args".to_string()], container.into_kube().args.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_set_security_context(){
let mut container = Container::default();
let kube_security_context = deps_hack::k8s_openapi::api::core::v1::SecurityContext {
run_as_user: Some(1000),
run_as_group: Some(1000),
privileged: Some(true),
..Default::default()
};
let security_context = SecurityContext::from_kube(kube_security_context.clone());

container.set_security_context(security_context);
assert_eq!(kube_security_context, container.into_kube().security_context.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_clone(){
Expand Down
39 changes: 39 additions & 0 deletions src/unit_tests/kubernetes_api_objects/container_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,45 @@ pub fn test_set_name() {
assert_eq!("name".to_string(), container_port.into_kube().name.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_name() {
let mut container_port = ContainerPort::default();
let temp = container_port.name();
if !temp.is_none() {
panic!("name should be none");
}
container_port.set_name(new_strlit("name").to_string());
assert_eq!("name".to_string(), container_port.name().unwrap().into_rust_string());
}

#[test]
#[verifier(external)]
pub fn test_container_port() {
let mut container_port = ContainerPort::default();
container_port.set_container_port(8080);
assert_eq!(8080, container_port.container_port());
}

#[test]
#[verifier(external)]
pub fn test_protocol() {
let container_port = ContainerPort::default();
let temp = container_port.protocol();
if !temp.is_none() {
panic!("protocol should be none");
}
let container_port = ContainerPort::from_kube(deps_hack::k8s_openapi::api::core::v1::ContainerPort {
container_port: 8080,
host_ip: Some("host_ip".to_string()),
host_port: Some(8080),
name: Some("name".to_string()),
protocol: Some("protocol".to_string()),
..Default::default()
});
assert_eq!("protocol".to_string(), container_port.protocol().unwrap().into_rust_string());
}

#[test]
#[verifier(external)]
pub fn test_kube() {
Expand Down
28 changes: 28 additions & 0 deletions src/unit_tests/kubernetes_api_objects/local_object_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::exec::affinity::*;
use crate::kubernetes_api_objects::exec::container::*;
use crate::kubernetes_api_objects::exec::object_meta::*;
use crate::kubernetes_api_objects::exec::pod::*;
use crate::kubernetes_api_objects::exec::resource::*;
use crate::kubernetes_api_objects::exec::toleration::*;
use crate::kubernetes_api_objects::exec::volume::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for LocalObjectReference
#[test]
#[verifier(external)]
pub fn test_kube() {
let kube_local_object_reference = deps_hack::k8s_openapi::api::core::v1::LocalObjectReference {
name: Some("name".to_string()),
};

let local_object_reference = LocalObjectReference::from_kube(kube_local_object_reference.clone());

assert_eq!(local_object_reference.into_kube(),
kube_local_object_reference);
}
}
2 changes: 2 additions & 0 deletions src/unit_tests/kubernetes_api_objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod key_to_path;
pub mod label_selector;
pub mod lifecycle;
pub mod lifecycle_handler;
pub mod local_object_reference;
pub mod object_field_selector;
pub mod object_meta;
pub mod owner_reference;
Expand All @@ -45,6 +46,7 @@ pub mod role_ref;
pub mod secret;
pub mod secret_projection;
pub mod secret_volume_source;
pub mod security_context;
pub mod service;
pub mod service_account;
pub mod service_port;
Expand Down
21 changes: 21 additions & 0 deletions src/unit_tests/kubernetes_api_objects/pod_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,27 @@ pub fn test_set_host_network() {
}


#[test]
#[verifier(external)]
pub fn test_set_image_pull_secrets(){
let mut pod_spec = PodSpec::default();
let kube_local_object_reference = deps_hack::k8s_openapi::api::core::v1::LocalObjectReference {
name: Some("name".to_string()),
};
let local_object_reference = LocalObjectReference::from_kube(kube_local_object_reference.clone());
pod_spec.set_image_pull_secrets(vec![local_object_reference]);

assert_eq!(vec![kube_local_object_reference], pod_spec.into_kube().image_pull_secrets.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_set_termination_grace_period_seconds(){
let mut pod_spec = PodSpec::default();
pod_spec.set_termination_grace_period_seconds(1);
assert_eq!(1, pod_spec.into_kube().termination_grace_period_seconds.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_kube() {
Expand Down
26 changes: 26 additions & 0 deletions src/unit_tests/kubernetes_api_objects/security_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: MIT
use crate::kubernetes_api_objects::exec::container::*;
use crate::kubernetes_api_objects::exec::object_meta::*;
use crate::kubernetes_api_objects::exec::resource::*;
use crate::kubernetes_api_objects::exec::volume::*;
use crate::vstd_ext::string_map::*;
use vstd::prelude::*;
use vstd::string::*;

verus! {
// Tests for SecurityContext
#[test]
#[verifier(external)]
pub fn test_kube(){
let kube_security_context = deps_hack::k8s_openapi::api::core::v1::SecurityContext {
privileged: Some(true),
..Default::default()
};

let security_context = SecurityContext::from_kube(kube_security_context.clone());

assert_eq!(security_context.into_kube(),
kube_security_context);
}
}
8 changes: 8 additions & 0 deletions src/unit_tests/kubernetes_api_objects/service_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ pub fn test_set_app_protocol() {
assert_eq!("protocol".to_string(), service_port.into_kube().app_protocol.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_set_protocaol() {
let mut service_port = ServicePort::default();
service_port.set_protocol(new_strlit("protocol").to_string());
assert_eq!("protocol".to_string(), service_port.into_kube().protocol.unwrap());
}

#[test]
#[verifier(external)]
pub fn test_kube() {
Expand Down
15 changes: 6 additions & 9 deletions tools/count-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ def camel_to_snake(name):
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

def main():
api_directory_path = 'src/kubernetes_api_objects' # Update this path if needed
api_directory_path = 'src/kubernetes_api_objects/exec' # Update this path if needed
test_directory_path = 'src/unit_tests/kubernetes_api_objects'
flag = True
for filename in sorted(os.listdir(api_directory_path)): # loop through all files in api_directory_path
if filename.endswith('.rs'):
file_path = os.path.join(api_directory_path, filename)
Expand All @@ -82,19 +83,15 @@ def main():
count_external_body, count_external_pub_fn, count_external_fn = count_functions(file_path)
total_count = count_external_body + count_external_pub_fn + count_external_fn


if total_count != test_count:
flag = False
print(f"{filename}: Total Count = {total_count}")
print("Test Count = ", test_count)
print("ERROR: Test Count != Total Count")
print(struct_count)
print("-------------------------------------------")
elif total_count == 0:
print(f"{filename}: Total Count = {total_count}")
print("-------------------------------------------")
else:
print(f"{filename}: Total Count = {total_count}")
print("Test Count = ", test_count)
print("Test coverage:", round(test_count / total_count * 100, 2), "%")
print("-------------------------------------------")
if flag:
print("All functions are covered!")
if __name__ == "__main__":
main()

0 comments on commit 3f1f6c0

Please sign in to comment.