Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoshet committed Oct 16, 2024
1 parent 7531a8a commit 29f952c
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ colored = "2.1.0"
k8s-openapi = { version = "0.23.0", features = ["v1_29"] }
kube = { version = "0.95.0", features = ["runtime", "derive"] }
kube-custom-resources-rs = { version = "2024.6.1", features = ["snapshot_storage_k8s_io"] }
pretty_assertions = "1.4.1"
schemars = "0.8.21"
serde = "1.0.210"
serde_json = "1.0.128"
Expand Down
3 changes: 3 additions & 0 deletions src/k8s_ops/pvc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod persistent_volume_claims;
pub mod persistent_volume_claims_payload;

#[cfg(test)]
mod persistent_volume_claims_tests;
93 changes: 93 additions & 0 deletions src/k8s_ops/pvc/persistent_volume_claims_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#[cfg(test)]
mod tests {
use crate::k8s_ops::pvc::{
persistent_volume_claims::{get_pvcs_available, PVCOperator},
persistent_volume_claims_payload::PVCOperatorPayload,
};
use k8s_openapi::{
api::core::v1::{
PersistentVolumeClaim, PersistentVolumeClaimSpec, TypedLocalObjectReference,
TypedObjectReference, VolumeResourceRequirements,
},
apimachinery::pkg::api::resource::Quantity,
};
use kube::{api::ObjectMeta, Api, Client};
use pretty_assertions::assert_eq;
use std::collections::BTreeMap;

#[test]
fn test_construct_persistent_volume_claim_resource() {
let pvc_operator_payload = PVCOperatorPayload::new(
"test-pvc".to_string(),
"test-ns".to_string(),
"gp3".to_string(),
Some(vec!["ReadWriteOnce".to_string()]),
"test-vs".to_string(),
"1Gi".to_string(),
);

let pvc_operator = PVCOperator::new(pvc_operator_payload);

let pvc = pvc_operator.construct_persistent_volume_claim_resource();

let mut labels = BTreeMap::new();
labels.insert(
"pvc-snapshotter/volume-snapshot-name".to_string(),
"test-pvc".to_string(),
);

let expected_pvc = PersistentVolumeClaim {
metadata: ObjectMeta {
name: Some("test-pvc".to_string()),
namespace: Some("test-ns".to_string()),
labels: Some(labels),
..Default::default()
},
spec: Some(PersistentVolumeClaimSpec {
access_modes: Some(vec!["ReadWriteOnce".to_string()]),
storage_class_name: Some("gp3".to_string()),
data_source: Some(TypedLocalObjectReference {
name: "test-vs".to_string(),
kind: "VolumeSnapshot".to_string(),
api_group: Some("snapshot.storage.k8s.io".to_string()),
}),
data_source_ref: Some(TypedObjectReference {
name: "test-vs".to_string(),
kind: "VolumeSnapshot".to_string(),
api_group: Some("snapshot.storage.k8s.io".to_string()),
namespace: Some("test-ns".to_string()),
}),
volume_mode: Some("Filesystem".to_string()),
volume_name: Default::default(),
resources: Some(VolumeResourceRequirements {
requests: Some(BTreeMap::from([(
"storage".to_string(),
Quantity("1Gi".to_string()),
)])),
..Default::default()
}),
selector: Default::default(),
volume_attributes_class_name: Default::default(),
}),
..Default::default()
};

assert_eq!(pvc, expected_pvc);
}

#[tokio::test]
async fn test_get_pvcs_available() {
let client = Client::try_default().await.unwrap();
let api: Api<PersistentVolumeClaim> = Api::namespaced(client, "test-ns");
let pvcs = get_pvcs_available(&api).await.unwrap();
assert!(pvcs.is_empty());
}

#[tokio::test]
async fn test_check_if_pvc_exists() {
let client = Client::try_default().await.unwrap();
let api: Api<PersistentVolumeClaim> = Api::namespaced(client, "test-ns");
let pvc = api.get("test-pvc").await;
assert!(pvc.is_err());
}
}
3 changes: 3 additions & 0 deletions src/k8s_ops/vs/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod volume_snapshots;

#[cfg(test)]
mod volume_snapshots_tests;
91 changes: 91 additions & 0 deletions src/k8s_ops/vs/volume_snapshots_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#[cfg(test)]
mod tests {
use crate::k8s_ops::{
vs::volume_snapshots::VolumeSnapshotOperator, vsc::retain_policy::VSCRetainPolicy,
};
use pretty_assertions::assert_eq;

#[tokio::test]
async fn test_construct_volume_snapshot_resource() {
let vs_operator = VolumeSnapshotOperator::new(
"test-volume-snapshot".to_string(),
"default".to_string(),
"ebs.csi.aws.com".to_string(),
Some("test-pvc".to_string()),
Some("test-volume-snapshot-content".to_string()),
);
let volume_snapshot = vs_operator.construct_volume_snapshot_resource(
Some("test-snapshot-handle".to_string()),
Some("1Gi".to_string()),
VSCRetainPolicy::Delete,
);
assert_eq!(
volume_snapshot.metadata.name.unwrap(),
"test-volume-snapshot"
);
assert_eq!(volume_snapshot.metadata.namespace.unwrap(), "default");
assert_eq!(
volume_snapshot
.metadata
.annotations
.as_ref()
.unwrap()
.get("pvc-snapshotter/csi-driver-name"),
Some(&"ebs.csi.aws.com".to_string())
);
assert_eq!(
volume_snapshot
.metadata
.annotations
.as_ref()
.unwrap()
.get("pvc-snapshotter/csi-vsc-deletion-policy"),
Some(&"Delete".to_string())
);
assert_eq!(
volume_snapshot
.metadata
.annotations
.as_ref()
.unwrap()
.get("pvc-snapshotter/csi-volumesnapshot-handle"),
Some(&"test-snapshot-handle".to_string())
);
assert_eq!(
volume_snapshot
.metadata
.annotations
.unwrap()
.get("pvc-snapshotter/csi-volumesnapshot-restore-size"),
Some(&"1Gi".to_string())
);
assert_eq!(
volume_snapshot
.metadata
.labels
.unwrap()
.get("app.kubernetes.io/instance"),
Some(&"default".to_string())
);
assert_eq!(
volume_snapshot.spec.volume_snapshot_class_name.unwrap(),
"ebs.csi.aws.com"
);
assert_eq!(
volume_snapshot
.spec
.source
.persistent_volume_claim_name
.unwrap(),
"test-pvc"
);
assert_eq!(
volume_snapshot
.spec
.source
.volume_snapshot_content_name
.unwrap(),
"test-volume-snapshot-content"
);
}
}
3 changes: 3 additions & 0 deletions src/k8s_ops/vsc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod retain_policy;
pub mod volume_snapshot_contents;

#[cfg(test)]
mod volume_snapshot_contents_tests;
23 changes: 23 additions & 0 deletions src/k8s_ops/vsc/retain_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,26 @@ impl From<VSCRetainPolicy> for VolumeSnapshotContentDeletionPolicy {
}
}
}

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

#[test]
fn test_vsc_retain_policy_display() {
assert_eq!(VSCRetainPolicy::Retain.to_string(), "Retain");
assert_eq!(VSCRetainPolicy::Delete.to_string(), "Delete");
}

#[test]
fn test_vsc_retain_policy_into() {
assert_eq!(
VolumeSnapshotContentDeletionPolicy::from(VSCRetainPolicy::Retain),
VolumeSnapshotContentDeletionPolicy::Retain
);
assert_eq!(
VolumeSnapshotContentDeletionPolicy::from(VSCRetainPolicy::Delete),
VolumeSnapshotContentDeletionPolicy::Delete
);
}
}
60 changes: 60 additions & 0 deletions src/k8s_ops/vsc/volume_snapshot_contents_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#[cfg(test)]
mod tests {
use crate::k8s_ops::vsc::{
retain_policy::VSCRetainPolicy, volume_snapshot_contents::VolumeSnapshotContentOperator,
};

#[tokio::test]
async fn test_construct_volume_snapshot_content_resource() {
let vsc_operator = VolumeSnapshotContentOperator::new(
"test-volume-snapshot-content".to_string(),
"default".to_string(),
"test-volume-snapshot".to_string(),
Some("ebs.csi.aws.com".to_string()),
Some("test-snapshot-handle".to_string()),
VSCRetainPolicy::Delete,
);
let volume_snapshot_content = vsc_operator.construct_volume_snapshot_content_resource();
assert_eq!(
volume_snapshot_content.metadata.name.unwrap(),
"test-volume-snapshot-content"
);
assert_eq!(
volume_snapshot_content.metadata.namespace.unwrap(),
"default"
);
assert_eq!(
volume_snapshot_content
.spec
.volume_snapshot_ref
.name
.unwrap(),
"test-volume-snapshot"
);
assert_eq!(
volume_snapshot_content
.spec
.volume_snapshot_class_name
.unwrap(),
"ebs.csi.aws.com"
);
assert_eq!(
volume_snapshot_content.spec.source.snapshot_handle.unwrap(),
"test-snapshot-handle"
);
assert_eq!(
volume_snapshot_content.spec.source_volume_mode.unwrap(),
"Filesystem"
);
assert_eq!(
volume_snapshot_content
.status
.as_ref()
.unwrap()
.snapshot_handle
.as_ref()
.unwrap(),
"test-snapshot-handle"
);
}
}

0 comments on commit 29f952c

Please sign in to comment.