Skip to content

Commit 33f1ed2

Browse files
committed
Merge pull request #48 from euanh/add-unit-testing
Check actual datasource values rather than just list lengths
2 parents e3c7d86 + 7459e12 commit 33f1ed2

File tree

1 file changed

+88
-23
lines changed

1 file changed

+88
-23
lines changed

rrdd/test_rrdd_monitor.ml

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,111 @@
11
open OUnit2
22

3-
let ds_a = Ds.ds_make ~name:"datasource_a" ~units:"(fraction)"
4-
~description:"datasource_a"
3+
let assert_equal_int = assert_equal ~printer:string_of_int
4+
5+
let ds_a = Ds.ds_make ~name:"ds_a" ~units:"(fraction)"
6+
~description:"datasource a"
57
~value:(Rrd.VT_Float 1.0)
68
~ty:Rrd.Gauge ~default:true ()
79

10+
let ds_b = Ds.ds_make ~name:"ds_b" ~units:"(fraction)"
11+
~description:"datasource b"
12+
~value:(Rrd.VT_Float 2.0)
13+
~ty:Rrd.Gauge ~default:true ()
14+
815
let reset_rrdd_shared_state ctxt =
916
Hashtbl.clear Rrdd_shared.vm_rrds;
1017
Rrdd_shared.host_rrd := None
1118

12-
let update_rrds_test dss uuid_domids paused_vms
13-
num_vm_rrds num_host_dss =
14-
fun ctxt ->
15-
OUnit2.bracket reset_rrdd_shared_state (fun () -> ignore) ctxt;
16-
Rrdd_monitor.update_rrds 12345.0 dss uuid_domids paused_vms;
17-
assert_equal num_vm_rrds (Hashtbl.length Rrdd_shared.vm_rrds);
19+
let dump_dss = List.map (fun ds -> ds.Ds.ds_name)
20+
21+
let dump_rrd_hash hash =
22+
Hashtbl.fold
23+
(fun k v acc -> (k, dump_dss v.Rrdd_shared.dss) :: acc)
24+
hash []
25+
26+
let string_of_rrd_dump dump =
27+
let rrds = List.map
28+
(fun (k, v) -> Printf.sprintf "(%s, [%s])" k (String.concat "; " v))
29+
dump
30+
in
31+
Printf.sprintf "[%s]" (String.concat "; " rrds)
32+
33+
let check_vm_rrds expected_rrds =
34+
assert_equal ~printer:string_of_rrd_dump
35+
(List.sort compare (dump_rrd_hash Rrdd_shared.vm_rrds))
36+
(List.sort compare expected_rrds)
37+
38+
let check_host_dss expected_dss =
1839
match !Rrdd_shared.host_rrd with
1940
| None -> assert_failure "host_rrd should have been created"
2041
| Some info ->
21-
assert_equal num_host_dss (List.length Rrdd_shared.(info.dss))
42+
assert_equal ~printer:(String.concat "; ")
43+
(List.sort compare (dump_dss info.Rrdd_shared.dss))
44+
(List.sort compare expected_dss)
45+
46+
let update_rrds_test ~dss ~uuid_domids ~paused_vms
47+
~expected_vm_rrds ~expected_host_dss = fun ctxt ->
48+
OUnit2.bracket reset_rrdd_shared_state (fun () -> ignore) ctxt;
49+
Rrdd_monitor.update_rrds 12345.0 dss uuid_domids paused_vms;
50+
check_vm_rrds expected_vm_rrds;
51+
check_host_dss expected_host_dss
2252

2353
let update_rrds = "update_rrds" >::: let open Rrd in [
24-
("Null update" >::
25-
update_rrds_test [] [] [] 0 0);
54+
"Null update" >:: update_rrds_test
55+
~dss:[]
56+
~uuid_domids:[]
57+
~paused_vms:[]
58+
~expected_vm_rrds:[]
59+
~expected_host_dss:[];
60+
61+
"Single host update" >:: update_rrds_test
62+
~dss:[(Host, ds_a)]
63+
~uuid_domids:[]
64+
~paused_vms:[]
65+
~expected_vm_rrds:[]
66+
~expected_host_dss:["ds_a"];
2667

27-
("Single host update" >::
28-
update_rrds_test [(Host, ds_a)] [] [] 0 1);
68+
"Multiple host updates" >:: update_rrds_test
69+
~dss:[(Host, ds_a); (Host, ds_a)]
70+
~uuid_domids:[]
71+
~paused_vms:[]
72+
~expected_vm_rrds:[]
73+
~expected_host_dss:["ds_a"; "ds_a"];
2974

30-
("Multiple host updates" >::
31-
update_rrds_test [(Host, ds_a); (Host, ds_a)] [] [] 0 2);
75+
"Single non-resident VM update" >:: update_rrds_test
76+
~dss:[(VM "a", ds_a)]
77+
~uuid_domids:[]
78+
~paused_vms:[]
79+
~expected_vm_rrds:[]
80+
~expected_host_dss:[];
3281

33-
("Single non-resident VM update" >::
34-
update_rrds_test [(VM "a", ds_a)] [] [] 0 0);
82+
"Multiple non-resident VM updates" >:: update_rrds_test
83+
~dss:[(VM "a", ds_a); (VM "b", ds_a)]
84+
~uuid_domids:[]
85+
~paused_vms:[]
86+
~expected_vm_rrds:[]
87+
~expected_host_dss:[];
3588

36-
("Multiple non-resident VM updates" >::
37-
update_rrds_test [(VM "a", ds_a); (VM "b", ds_a)] [] [] 0 0);
89+
"Single resident VM update" >:: update_rrds_test
90+
~dss:[(VM "a", ds_a)]
91+
~uuid_domids:[("a", 1)]
92+
~paused_vms:[]
93+
~expected_vm_rrds: ["a", ["ds_a"]]
94+
~expected_host_dss:[];
3895

39-
("Single resident VM update" >::
40-
update_rrds_test [(VM "a", ds_a)] [("a", 1)] [] 1 0);
96+
"Multiple resident VM updates" >:: update_rrds_test
97+
~dss:[(VM "a", ds_a); (VM "b", ds_a); (VM "b", ds_b)]
98+
~uuid_domids:[("a", 1); ("b", 1)]
99+
~paused_vms:[]
100+
~expected_vm_rrds:["a", ["ds_a"]; "b", ["ds_a"; "ds_b"]]
101+
~expected_host_dss:[];
41102

42-
("Multiple resident VM updates" >::
43-
update_rrds_test [(VM "a", ds_a); (VM "b", ds_a)] [("a", 1); ("b", 1)] [] 2 0);
103+
"Multiple resident and non-resident VM updates" >:: update_rrds_test
104+
~dss:[(VM "a", ds_a); (VM "b", ds_a); (VM "c", ds_a)]
105+
~uuid_domids:[("a", 1); ("b", 1)]
106+
~paused_vms:[]
107+
~expected_vm_rrds:["a", ["ds_a"]; "b", ["ds_a"]]
108+
~expected_host_dss:[];
44109
]
45110

46111
let suite = "rrdd monitor test" >:::

0 commit comments

Comments
 (0)