Skip to content

Commit 80e4447

Browse files
author
Bohdan Biloshytskyi
committed
Added tests
1 parent eab4843 commit 80e4447

3 files changed

Lines changed: 133 additions & 2 deletions

File tree

metrics-cache/src/section/node.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,57 @@ impl<'a> KubeNodeInfoV1<'a> {
8686
impl Section for KubeNodeInfoV1<'_> {
8787
const NAME: &'static str = "kube_node_info_v1";
8888
}
89+
90+
#[cfg(test)]
91+
mod tests {
92+
use super::*;
93+
use regex::Regex;
94+
95+
use crate::host_settings::AnnotationKeyPattern;
96+
use crate::test_support::{host_settings, node, node_prefilled};
97+
98+
#[test]
99+
fn kube_node_info_v1() {
100+
let node = node_prefilled("worker-1");
101+
let mut settings = host_settings();
102+
insta::assert_json_snapshot!(KubeNodeInfoV1::from_node(&node, &settings));
103+
104+
// This pattern should only match one annotation
105+
settings.annotation_key_pattern =
106+
AnnotationKeyPattern::Pattern(Regex::new("^example").unwrap());
107+
assert_eq!(
108+
KubeNodeInfoV1::from_node(&node, &settings)
109+
.unwrap()
110+
.annotations
111+
.len(),
112+
1
113+
);
114+
115+
// Ignore all annotations, emit 0 of them
116+
settings.annotation_key_pattern = AnnotationKeyPattern::IgnoreAll;
117+
assert_eq!(
118+
KubeNodeInfoV1::from_node(&node, &settings)
119+
.unwrap()
120+
.annotations
121+
.len(),
122+
0
123+
);
124+
125+
// Import all annotations (fixture default, captured by insta above, but let's be explicit)
126+
settings.annotation_key_pattern = AnnotationKeyPattern::ImportAll;
127+
assert_eq!(
128+
KubeNodeInfoV1::from_node(&node, &settings)
129+
.unwrap()
130+
.annotations
131+
.len(),
132+
2
133+
);
134+
}
135+
136+
/// A Node without `status.nodeInfo` cannot fill the five mandatory fields
137+
/// of the section, so we emit nothing rather than something unparseable.
138+
#[test]
139+
fn kube_node_info_v1_without_node_info() {
140+
assert!(KubeNodeInfoV1::from_node(&node("worker-1"), &host_settings()).is_none());
141+
}
142+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
source: metrics-cache/src/section/node.rs
3+
expression: "KubeNodeInfoV1::from_node(&node, &settings)"
4+
---
5+
{
6+
"architecture": "amd64",
7+
"kernel_version": "6.8.0-51-generic",
8+
"os_image": "Ubuntu 22.04.5 LTS",
9+
"operating_system": "linux",
10+
"container_runtime_version": "containerd://1.7.24",
11+
"name": "worker-1",
12+
"creation_timestamp": 1786130565.0,
13+
"labels": {
14+
"kubernetes.io/arch": {
15+
"name": "kubernetes.io/arch",
16+
"value": "amd64"
17+
},
18+
"kubernetes.io/hostname": {
19+
"name": "kubernetes.io/hostname",
20+
"value": "worker-1"
21+
}
22+
},
23+
"annotations": {
24+
"checkmk.com/promote-to-host": "true",
25+
"example.com/cool-animal": "monkeys"
26+
},
27+
"addresses": [
28+
{
29+
"address": "10.0.0.5",
30+
"type_": "InternalIP"
31+
},
32+
{
33+
"address": "worker-1",
34+
"type_": "Hostname"
35+
}
36+
],
37+
"cluster": "the-cluster",
38+
"kubernetes_cluster_hostname": "cluster.host.tld"
39+
}

metrics-cache/src/test_support.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
use k8s_openapi::api::apps::v1::ReplicaSet;
44
use k8s_openapi::api::batch::v1::{CronJob, CronJobSpec, Job};
55
use k8s_openapi::api::core::v1::{
6-
Container, Namespace, Node, PersistentVolumeClaim, PersistentVolumeClaimSpec,
7-
PersistentVolumeClaimStatus, Pod, PodSpec, VolumeResourceRequirements,
6+
Container, Namespace, Node, NodeAddress, NodeStatus, NodeSystemInfo, PersistentVolumeClaim,
7+
PersistentVolumeClaimSpec, PersistentVolumeClaimStatus, Pod, PodSpec,
8+
VolumeResourceRequirements,
89
};
910
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
1011
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ObjectMeta, OwnerReference, Time};
@@ -103,6 +104,43 @@ pub fn node(name: &str) -> Node {
103104
}
104105
}
105106

107+
pub fn node_prefilled(name: &str) -> Node {
108+
let timestamp: Timestamp = "2026-08-07 15:22:45-04".parse().unwrap();
109+
let mut node = node(name);
110+
node.metadata.creation_timestamp = Some(Time(timestamp));
111+
node.metadata.labels = Some(BTreeMap::from([
112+
(s("kubernetes.io/hostname"), name.to_string()),
113+
(s("kubernetes.io/arch"), s("amd64")),
114+
]));
115+
node.metadata.annotations = Some(BTreeMap::from([
116+
(s("example.com/cool-animal"), s("monkeys")),
117+
(s("checkmk.com/promote-to-host"), s("true")),
118+
]));
119+
node.status = Some(NodeStatus {
120+
addresses: Some(vec![
121+
NodeAddress {
122+
address: s("10.0.0.5"),
123+
type_: s("InternalIP"),
124+
},
125+
NodeAddress {
126+
address: name.to_string(),
127+
type_: s("Hostname"),
128+
},
129+
]),
130+
node_info: Some(NodeSystemInfo {
131+
architecture: s("amd64"),
132+
container_runtime_version: s("containerd://1.7.24"),
133+
kernel_version: s("6.8.0-51-generic"),
134+
kubelet_version: s("v1.34.0"),
135+
operating_system: s("linux"),
136+
os_image: s("Ubuntu 22.04.5 LTS"),
137+
..Default::default()
138+
}),
139+
..Default::default()
140+
});
141+
node
142+
}
143+
106144
pub fn owner_ref(kind: &str, name: &str, uid: &str) -> OwnerReference {
107145
OwnerReference {
108146
kind: kind.into(),

0 commit comments

Comments
 (0)