Skip to content

Commit

Permalink
dune build @fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
reynir committed Dec 11, 2024
1 parent 658d1ad commit 88e9ed0
Showing 1 changed file with 119 additions and 93 deletions.
212 changes: 119 additions & 93 deletions test/config_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ let opt_eq f a b =
| None, Some _ | Some _, None -> false

let ipv4_eq a b = Ipaddr.V4.compare a b = 0

let ipv6_eq a b = Ipaddr.V6.compare a b = 0

let mac_eq a b = Macaddr.compare a b = 0

let mode_eq a b =
Expand All @@ -33,21 +31,23 @@ let dhcp_range_t =
Alcotest.testable pp_dhcp_range equal

let dhcp_host_t =
let equal { id; nets; tags; macs; ipv4; ipv6; lease_time; ignore; domain_name } b =
Option.equal (fun id id' ->
match id, id' with
let equal
{ id; nets; tags; macs; ipv4; ipv6; lease_time; ignore; domain_name } b =
Option.equal
(fun id id' ->
match (id, id') with
| `Any_client_id, `Any_client_id -> true
| `Client_id id, `Client_id id' -> String.equal id id'
| `Any_client_id, `Client_id _ | `Client_id _, `Any_client_id -> false)
id b.id &&
List.equal String.equal nets b.nets &&
List.equal String.equal tags b.tags &&
List.equal mac_eq macs b.macs &&
Option.equal ipv4_eq ipv4 b.ipv4 &&
Option.equal ipv6_eq ipv6 b.ipv6 &&
Option.equal Int.equal lease_time b.lease_time &&
Bool.equal ignore b.ignore &&
Option.equal Domain_name.equal domain_name b.domain_name
id b.id
&& List.equal String.equal nets b.nets
&& List.equal String.equal tags b.tags
&& List.equal mac_eq macs b.macs
&& Option.equal ipv4_eq ipv4 b.ipv4
&& Option.equal ipv6_eq ipv6 b.ipv6
&& Option.equal Int.equal lease_time b.lease_time
&& Bool.equal ignore b.ignore
&& Option.equal Domain_name.equal domain_name b.domain_name
in
Alcotest.testable pp_dhcp_config equal

Expand Down Expand Up @@ -109,126 +109,152 @@ let ok_dhcp_range_static () =
"DHCP range with static is good" (Ok expected)
(parse_one_arg dhcp_range input))

let make_dhcp_config ?id ?(nets=[]) ?(tags=[]) ?(macs=[]) ?ipv4 ?ipv6 ?lease_time ?(ignore=false) ?domain_name () =
let make_dhcp_config ?id ?(nets = []) ?(tags = []) ?(macs = []) ?ipv4 ?ipv6
?lease_time ?(ignore = false) ?domain_name () =
{ id; nets; tags; macs; ipv4; ipv6; lease_time; ignore; domain_name }

let ok_dhcp_host_thedoctor () =
let input = "00:00:5e:00:53:42,thedoctor,192.168.0.10" in
let expected =
make_dhcp_config
~macs:[Macaddr.of_string_exn "00:00:5e:00:53:42"]
~macs:[ Macaddr.of_string_exn "00:00:5e:00:53:42" ]
~domain_name:(Domain_name.of_string_exn "thedoctor")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.10")
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host thedoctor is good"
(Ok expected) (parse_one_arg dhcp_host input)
Alcotest.(check (result dhcp_host_t msg_t))
"DHCP host thedoctor is good" (Ok expected)
(parse_one_arg dhcp_host input)

let ok_dhcp_host_tardis () =
let input = "00:00:5e:00:53:01,00:00:5e:00:53:02,tardis,192.168.0.22" in
let expected =
make_dhcp_config
~macs:Macaddr.[of_string_exn "00:00:5e:00:53:01";
of_string_exn "00:00:5e:00:53:02"]
~macs:
Macaddr.
[
of_string_exn "00:00:5e:00:53:01"; of_string_exn "00:00:5e:00:53:02";
]
~domain_name:(Domain_name.of_string_exn "tardis")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.22")
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host tardis is good"
(Ok expected) (parse_one_arg dhcp_host input)
Alcotest.(check (result dhcp_host_t msg_t))
"DHCP host tardis is good" (Ok expected)
(parse_one_arg dhcp_host input)

let ok_dhcp_host_sonicscrewdriver () =
let input = "00:00:5e:00:53:08,sonicscrewdriver,192.168.0.23" in
let expected =
make_dhcp_config
~macs:[Macaddr.of_string_exn "00:00:5e:00:53:08"]
~macs:[ Macaddr.of_string_exn "00:00:5e:00:53:08" ]
~domain_name:(Domain_name.of_string_exn "sonicscrewdriver")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.23")
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host sonicscrewdriver is good"
(Ok expected) (parse_one_arg dhcp_host input)
Alcotest.(check (result dhcp_host_t msg_t))
"DHCP host sonicscrewdriver is good" (Ok expected)
(parse_one_arg dhcp_host input)

let ok_dhcp_host_apollon () =
let input = "52:54:00:42:6a:43,apollon,10.10.10.51,infinite" in
let expected =
make_dhcp_config
~macs:[Macaddr.of_string_exn "52:54:00:42:6a:43"]
~macs:[ Macaddr.of_string_exn "52:54:00:42:6a:43" ]
~domain_name:(Domain_name.of_string_exn "apollon")
~ipv4:(Ipaddr.V4.of_string_exn "10.10.10.51")
~lease_time:(1 lsl 32) (* infinite *)
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host apollon is good"
(Ok expected) (parse_one_arg dhcp_host input)
Alcotest.(check (result dhcp_host_t msg_t))
"DHCP host apollon is good" (Ok expected)
(parse_one_arg dhcp_host input)

let ok_dhcp_host_dnsmasq_conf_example =
let to_test (name, input, expected) =
(name, `Quick, fun () ->
( name,
`Quick,
fun () ->
Alcotest.(check (result dhcp_host_t msg_t))
(name ^ " is good")
(Ok expected)
(parse_one_arg dhcp_host input))
(name ^ " is good") (Ok expected)
(parse_one_arg dhcp_host input) )
in
List.map to_test [
("First dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,192.168.0.60",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
("Second dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,fred",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~domain_name:(Domain_name.of_string_exn "fred") ());
("Third dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,fred,192.168.0.60,45m",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~domain_name:(Domain_name.of_string_exn "fred")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
~lease_time:(45*60) ());
("Fourth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,12:34:56:78:90:12,192.168.0.60",
make_dhcp_config ~macs:Macaddr.[of_string_exn "11:22:33:44:55:66";
of_string_exn "12:34:56:78:90:12"]
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
("Fifth dhcp-host from dnsmasq.conf.example",
"bert,192.168.0.70,infinite",
make_dhcp_config ~domain_name:(Domain_name.of_string_exn "bert")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.70")
~lease_time:(1 lsl 32) ());
("Sixth dhcp-host from dnsmasq.conf.example",
"id:01:02:02:04,192.168.0.60",
make_dhcp_config ~id:(`Client_id "\x01\x02\x02\x04")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
(* skipping seventh as it's similar to above, but is infiniband in a manner we wouldn't care about *)
("Eigth dhcp-host from dnsmasq.conf.example",
"id:marjorie,192.168.0.60",
make_dhcp_config ~id:(`Client_id "marjorie")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
("Ninth dhcp-host from dnsmasq.conf.example",
"judge",
make_dhcp_config ~domain_name:(Domain_name.of_string_exn "judge")
());
("Tenth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,ignore",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~ignore:true ());
("Eleventh dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,id:*",
make_dhcp_config ~id:`Any_client_id
~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"] ());
("Twelvth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,set:red",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~nets:["red"] ());
(* TODO:
("Thirteenth dhcp-host from dnsmasq.conf.example",
"11:22:33:*:*:*,set:red",
_);
("Fourteenth dhcp-host from dnsmasq.conf.example",
"id:00:01:00:01:16:d2:83:fc:92:d4:19:e2:d8:b2, fred, [1234::5]",
_);
*)
]
List.map to_test
[
( "First dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,192.168.0.60",
make_dhcp_config
~macs:[ Macaddr.of_string_exn "11:22:33:44:55:66" ]
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
() );
( "Second dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,fred",
make_dhcp_config
~macs:[ Macaddr.of_string_exn "11:22:33:44:55:66" ]
~domain_name:(Domain_name.of_string_exn "fred")
() );
( "Third dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,fred,192.168.0.60,45m",
make_dhcp_config
~macs:[ Macaddr.of_string_exn "11:22:33:44:55:66" ]
~domain_name:(Domain_name.of_string_exn "fred")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
~lease_time:(45 * 60) () );
( "Fourth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,12:34:56:78:90:12,192.168.0.60",
make_dhcp_config
~macs:
Macaddr.
[
of_string_exn "11:22:33:44:55:66";
of_string_exn "12:34:56:78:90:12";
]
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
() );
( "Fifth dhcp-host from dnsmasq.conf.example",
"bert,192.168.0.70,infinite",
make_dhcp_config
~domain_name:(Domain_name.of_string_exn "bert")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.70")
~lease_time:(1 lsl 32) () );
( "Sixth dhcp-host from dnsmasq.conf.example",
"id:01:02:02:04,192.168.0.60",
make_dhcp_config ~id:(`Client_id "\x01\x02\x02\x04")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
() );
(* skipping seventh as it's similar to above, but is infiniband in a manner we wouldn't care about *)
( "Eigth dhcp-host from dnsmasq.conf.example",
"id:marjorie,192.168.0.60",
make_dhcp_config ~id:(`Client_id "marjorie")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
() );
( "Ninth dhcp-host from dnsmasq.conf.example",
"judge",
make_dhcp_config ~domain_name:(Domain_name.of_string_exn "judge") () );
( "Tenth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,ignore",
make_dhcp_config
~macs:[ Macaddr.of_string_exn "11:22:33:44:55:66" ]
~ignore:true () );
( "Eleventh dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,id:*",
make_dhcp_config ~id:`Any_client_id
~macs:[ Macaddr.of_string_exn "11:22:33:44:55:66" ]
() );
( "Twelvth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,set:red",
make_dhcp_config
~macs:[ Macaddr.of_string_exn "11:22:33:44:55:66" ]
~nets:[ "red" ] () );
(* TODO:
("Thirteenth dhcp-host from dnsmasq.conf.example",
"11:22:33:*:*:*,set:red",
_);
("Fourteenth dhcp-host from dnsmasq.conf.example",
"id:00:01:00:01:16:d2:83:fc:92:d4:19:e2:d8:b2, fred, [1234::5]",
_);
*)
]

let tests =
[
Expand All @@ -239,8 +265,8 @@ let tests =
("DHCP host tardis", `Quick, ok_dhcp_host_tardis);
("DHCP host sonicscrewdriver", `Quick, ok_dhcp_host_sonicscrewdriver);
("DHCP host apollon", `Quick, ok_dhcp_host_apollon);
] @
ok_dhcp_host_dnsmasq_conf_example
]
@ ok_dhcp_host_dnsmasq_conf_example

let string_of_file filename =
let config_dir = "sample-configuration-files" in
Expand Down

0 comments on commit 88e9ed0

Please sign in to comment.