@@ -37,3 +37,87 @@ pub async fn update(
3737 . await ;
3838 Json ( "ok" . to_string ( ) )
3939}
40+
41+ #[ cfg( test) ]
42+ mod tests {
43+ use super :: * ;
44+ use crate :: test_utils:: test_app_state;
45+ use axum:: Json ;
46+ use cmk_kube_types:: machine_sections:: MachineSections ;
47+ use cmk_kube_types:: metadata:: metrics_fetcher:: FetcherKind ;
48+
49+ fn make_machine_sections ( node : & str ) -> MachineSections {
50+ MachineSections {
51+ sections : cmk_kube_types:: machine_sections:: FetchResult {
52+ node_name : node. to_string ( ) ,
53+ sections : "s" . to_string ( ) ,
54+ } ,
55+ metadata : cmk_kube_types:: metadata:: metrics_fetcher:: Metadata {
56+ static_metadata : cmk_kube_types:: metadata:: StaticMetadata {
57+ node : node. to_string ( ) ,
58+ host_name : "host" . to_string ( ) ,
59+ container_platform : cmk_kube_types:: metadata:: Platform {
60+ os_name : "linux" . to_string ( ) ,
61+ os_version : "1" . to_string ( ) ,
62+ python_version : String :: new ( ) ,
63+ python_compiler : String :: new ( ) ,
64+ } ,
65+ checkmk_kube_agent : cmk_kube_types:: metadata:: CheckmkKubeAgent {
66+ project_version : "v0" . to_string ( ) ,
67+ } ,
68+ } ,
69+ collector_type : FetcherKind :: Machine ,
70+ components : Default :: default ( ) ,
71+ } ,
72+ }
73+ }
74+
75+ #[ tokio:: test]
76+ async fn update_inserts_into_caches ( ) {
77+ let state = test_app_state ( ) ;
78+ let machine_sections = make_machine_sections ( "node-x" ) ;
79+
80+ let resp = update ( State ( state. clone ( ) ) , Json ( machine_sections. clone ( ) ) ) . await ;
81+ assert_eq ! ( resp. 0 , "ok" ) ;
82+
83+ // check machine_sections_cache
84+ let got = state
85+ . machine_sections_cache
86+ . get ( & machine_sections. sections . node_name )
87+ . await
88+ . expect ( "missing" ) ;
89+ assert_eq ! ( got. node_name, machine_sections. sections. node_name) ;
90+
91+ // check metadata cache
92+ let metadata_key = format ! (
93+ "machine_sections:{}" ,
94+ machine_sections. metadata. static_metadata. node
95+ ) ;
96+ let meta = state
97+ . metrics_fetcher_metadata_cache
98+ . get ( & metadata_key)
99+ . await
100+ . expect ( "missing meta" ) ;
101+ assert_eq ! (
102+ meta. static_metadata. node,
103+ machine_sections. metadata. static_metadata. node
104+ ) ;
105+ }
106+
107+ #[ tokio:: test]
108+ async fn get_returns_cached_entries ( ) {
109+ let state = test_app_state ( ) ;
110+ let fr = cmk_kube_types:: machine_sections:: FetchResult {
111+ node_name : "node-a" . to_string ( ) ,
112+ sections : "s" . to_string ( ) ,
113+ } ;
114+ state
115+ . machine_sections_cache
116+ . insert ( fr. node_name . clone ( ) , fr. clone ( ) )
117+ . await ;
118+
119+ let resp = get ( State ( state) ) . await ;
120+ let vec = resp. 0 ;
121+ assert ! ( vec. iter( ) . any( |f| f. node_name == fr. node_name) ) ;
122+ }
123+ }
0 commit comments