-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_it.ml
726 lines (643 loc) · 19.3 KB
/
test_it.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
(*
Copyright (C) iNuron - [email protected]
This file is part of Open vStorage. For license information, see <LICENSE.txt>
*)
open Kinetic_util
let vco2s =
show_option
(fun ((v:bytes),version) ->
let version_s = show_option Bytes.to_string version in
Printf.sprintf "Some(%S, %s)" (Bytes.to_string v) version_s
)
open Lwt
type test_result =
| Ok
| Failed of string
| Skipped
let show_test_result = function
| Ok -> "Ok"
| Failed s -> Printf.sprintf "Failed(%s)" s
| Skipped -> "Skipped"
open Kinetic
module K = Make(BytesIntegration)
let assert_test_result = function
| Result.Ok _ -> Ok |> Lwt.return
| Error e -> Failed (Error.show e) |> Lwt.return
let lwt_test name (f:unit -> unit K.result) : test_result Lwt.t=
Lwt_log.debug_f "starting:%s" name >>= fun () ->
let timeout = 300.
(* overkill value, but:
the simulator isn't that fast (FLUSH | WRITETHROUGH)
and we sometimes need to test the real drives
via ssh port forwarding.
*)
in
Lwt.catch
(fun () ->
Lwt_unix.with_timeout timeout f
>>= assert_test_result
)
(fun exn ->
Lwt_log.info_f ~exn "failing:%s" name >>= fun () ->
Lwt.return (Failed (Printexc.to_string exn))
)
>>= fun r ->
Lwt_log.debug_f "end of :%s" name >>= fun () ->
Lwt.return r
let (~~) s = Bytes.of_string s
let test_get_non_existing client : unit K.result =
let key_s = "I do not exist?" in
K.get client ~~key_s >>=? fun vo ->
match vo with
| None -> Lwt_result.return ()
| Some _ -> Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
let test_put_no_tag client =
let key_s = "test_put_no_tag" in
let key = ~~key_s in
let v_s = key_s in
let v = key in
let v_slice = key, 0, Bytes.length v in
let synchronization = Some K.WRITEBACK in
Lwt_io.printlf "drive[%S] <- Some %S%!" key_s v_s >>= fun () ->
K.put
client
key v_slice
~db_version:None
~new_version:None
~forced:None
~tag:None
~synchronization
>>= function
| Error (Error.KineticError(16,"Tag required")) -> Lwt_result.return ()
| _ -> Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
let test_put_empty_string client =
let key_s = "test_put_empty_string" in
let v_s = "" in
let v = ~~v_s
and key = ~~key_s
in
let v_slice = v, 0, Bytes.length v in
let tag = Some (Tag.Crc32 0x0l) in
let synchronization = Some K.WRITEBACK in
Lwt_io.printlf "drive[%S] <- Some %S%!" key_s v_s >>= fun () ->
K.put
client
key v_slice
~db_version:None
~new_version:None
~forced:None
~tag
~synchronization
>>=? fun () ->
K.get client key >>=? fun vco ->
Lwt_log.debug_f "vco=%s" (vco2s vco) >>= fun () ->
match vco with
| None -> Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
| Some (v2, version) ->
if v2 <> v
then Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
else
if version <> Some Bytes.empty
then Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
else Lwt_result.return ()
let test_put_timeout client =
let key_s = "test_put_timeout" in
let key = ~~key_s in
let v = key in
let v_slice = key, 0, Bytes.length v in
let tag = K.make_sha1 v_slice in
let timeout = 1L in (* 1 ms *)
K.put ~timeout client key v_slice
~db_version:None
~new_version:None
~forced:None
~tag:(Some tag)
~synchronization:(Some K.WRITETHROUGH)
>>= fun e ->
(* it does not timeout ? *)
Lwt_result.return ()
let test_noop client =
K.noop client
let batch_single_put client =
let v_s = "ZZZ" in
let v = ~~v_s in
let v_slice = v, 0 , Bytes.length v in
let tag = K.make_sha1 v_slice in
let pe = K.Entry.make
~key:~~"zzz"
~db_version:None
~new_version:(Some ~~"ZZZ")
(Some (v_slice,tag))
in
K.do_batch client ~trace_batch:true [BPut (pe,Some true)] >>=? fun r ->
Lwt_log.debug_f "tracing_info %S" (so2s r) >>= fun () ->
Lwt_result.return ()
let batch_test_put_delete client =
let v = ~~"XXX" in
let v_slice = v,0,Bytes.length v in
let tag = K.make_sha1 v_slice in
let pe = K.Entry.make
~key:~~"xxx"
~db_version:None
~new_version:None
(Some (v_slice, tag))
in
let de = K.Entry.make
~key:~~"xxx"
~db_version:None
~new_version: None
None
in
K.do_batch client [BPut (pe, Some true);BDel (de, Some true)]
>>=? fun r ->
Lwt_result.return ()
let batch_delete_non_existing client =
let de =
K.Entry.make
~key:~~"I do not exist"
~db_version:None
~new_version:None
None
in
K.do_batch client [BDel (de, Some true)]
>>=? fun r ->
Lwt_result.return ()
let _make_batch_put key v =
let v_slice = v,0,Bytes.length v in
let pe =
let tag = K.make_sha1 v_slice in
K.Entry.make
~key
~db_version:None
~new_version:None
(Some (v_slice, tag))
in
K.BPut (pe, Some true)
let batch_3_puts client =
let make_key i = ~~(Printf.sprintf "batch_test_3_puts:key_%03i" i) in
K.do_batch client (List.map (fun k -> let k' = make_key k in _make_batch_put k' k') [0;1;2])
>>=? fun r ->
Lwt_result.return ()
let batch_too_fat client =
let session = K.get_session client in
let cfg = K.get_config session in
let max = cfg.max_operation_count_per_batch in
match max with
| None ->
Lwt_log.debug_f "client version:%s => no testing needed Ok" cfg.version
>>= fun () ->
Lwt_result.return ()
| Some max ->
begin
let make_key i = ~~(Printf.sprintf "batch_too_fat:key_%03i" i) in
let n = max + 5 in
let rec loop operations i =
if i = n
then List.rev operations
else
begin
let key = make_key 0 in
let operations' = (_make_batch_put key key)::operations in
loop operations' (i+1)
end
in
K.do_batch client (loop [] 0)
>>= function
| Result.Ok _ -> assert false (* It should fail !*)
| Result.Error (Error.KineticError(21, msg) as e) -> (* needs to be 21 = too many operations in a batch *)
Lwt_log.debug_f "errored, as expected: %s" (Error.show e) >>= fun () ->
Lwt_result.return ()
| _ -> assert false
end
let test_crc32 client =
let key = ~~"test_crc32_key" in
let v = key in
let v_slice = v,0,Bytes.length v in
(*let tag = K.Crc32 0xEAE10D3Al in*)
let tag = Tag.Crc32 0x0l in
let synchronization = Some K.WRITEBACK in
K.put client key v_slice
~db_version:None
~new_version:None
~forced:None
~tag:(Some tag)
~synchronization
let test_put_get_delete client =
let rec loop i =
if i = 400
then Lwt_result.return ()
else
let key_s = Printf.sprintf "x_%05i" i in
let key = ~~key_s in
let v_s = Printf.sprintf "value_%05i" i in
let v = ~~v_s in
let v_slice = v,0,Bytes.length v in
let synchronization = Some K.WRITEBACK in
Lwt_io.printlf "drive[%S] <- Some %S%!" key_s v_s >>= fun () ->
let tag = K.make_sha1 v_slice in
K.put
client key v_slice
~db_version:None
~new_version:None
~forced:None
~tag:(Some tag)
~synchronization
>>=? fun () ->
K.get client key >>=? fun vco ->
Lwt_io.printlf "drive[%S]=%s%!" key_s (vco2s vco) >>= fun () ->
let () = match vco with
| None -> failwith "should be present"
| Some (value2, version) ->
begin
assert (v = value2);
assert (version = Some Bytes.empty);
end
in
K.delete_forced client key >>=? fun () ->
Lwt_io.printlf "deleted %S" key_s >>= fun () ->
K.get client key >>=? fun vco ->
Lwt_io.printlf "drive[%S]=%s" key_s (vco2s vco) >>= fun () ->
assert (vco = None);
loop (i+1)
in
loop 0
let test_put_largish client =
let key = ~~"largish" in
let v = Bytes.create 100_000 in
let v_slice = v,0,Bytes.length v in
let tag = K.make_sha1 v_slice in
let synchronization = Some K.FLUSH in
K.put client key v_slice
~new_version:None
~db_version:None
~forced:(Some true)
~synchronization
~tag:(Some tag)
>>=? fun () ->
K.get client key >>=? function
| None -> Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
| Some vc -> Lwt_result.return ()
let test_put_version client =
let key_s = "with_version" in
let key = ~~key_s in
K.delete_forced client key >>=? fun () ->
Lwt_log.debug_f "deleted %S" key_s >>= fun () ->
let v_s = "the_value" in
let v = ~~v_s in
let v_slice = v,0,Bytes.length v in
let tag = K.make_sha1 v_slice in
let version = Some ~~"0" in
let synchronization = Some K.FLUSH in
K.put
client key v_slice
~new_version:version
~db_version:None
~forced:(Some true)
~synchronization
~tag:(Some tag)
>>=? fun () ->
K.get client key >>=? fun vco ->
Lwt_log.debug_f "vco=%s" (vco2s vco) >>= fun () ->
let new_version = Some ~~"1" in
let v2 = ~~"next_value" in
let v2_slice = v2,0,Bytes.length v2 in
let tag2 = K.make_sha1 v2_slice in
Lwt_log.debug_f "new_version:%s" (show_option Bytes.to_string new_version) >>= fun () ->
K.put
client key v2_slice
~db_version:new_version ~new_version
~forced:None
~synchronization
~tag:(Some tag2)
>>= function
| Result.Error (Error.KineticError(_, _)) ->
K.get client key >>=? fun vco2 ->
Lwt_io.printlf "vco2=%s" (vco2s vco2) >>= fun () ->
Lwt_result.return ()
| Result.Error e ->
Lwt_log.warning_f "r:%s" (Error.show e) >>= fun () ->
Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
| _ -> Error.Generic(__FILE__,__LINE__, "test case assert failed") |> Lwt_result.fail
let fill client n =
let synchronization = Some K.WRITEBACK in
let rec loop i =
if i = n
then Lwt_result.return ()
else
let key_s = Printf.sprintf "x_%05i" i in
let key = ~~key_s in
let v_s = Printf.sprintf "value_%05i" i in
let v = ~~v_s in
let v_slice = v, 0, Bytes.length v in
let tag = K.make_sha1 v_slice in
begin
if i mod 100 = 0 then Lwt_io.printlf "i:%i" i else Lwt.return ()
end
>>= fun ()->
K.put
client key v_slice
~db_version:None
~new_version:None
~forced:(Some true)
~synchronization
~tag:(Some tag)
>>=? fun () ->
loop (i+1)
in
loop 0
let assert_string ?(msg="") expected got =
if String.equal expected got
then ()
else failwith (Printf.sprintf "%s expected:%S got:%S" msg expected got)
let range_test client =
fill client 1000 >>=? fun () ->
K.get_key_range
client
~~"x" true
(Some (~~"y",true))
false 20
>>=? fun keys ->
Lwt_io.printlf "result = [%s] (len = %i)\n%!" (bl2s keys) (List.length keys) >>= fun () ->
assert (List.length keys = 20);
let hd = List.hd keys in
Lwt_io.printlf "head = %s\n" (hd |> Bytes.to_string) >>= fun () ->
assert_string ~msg:"hd" (List.hd keys |> Bytes.to_string) "x_00000";
Lwt_result.return ()
let range_test_reverse client =
fill client 1000 >>=? fun () ->
(* note the order, which differs from the specs *)
K.get_key_range client ~~"x" true (Some(~~"y",true)) true 20 >>=? fun keys ->
Lwt_io.printlf "[%s]\n%!" (bl2s keys) >>= fun () ->
assert (List.length keys = 20);
assert_string ~msg:"hd" (List.hd keys |> Bytes.to_string) "x_00999";
Lwt_result.return ()
let get_capacities_test client =
K.get_capacities client >>=? fun (cap, fill_rate) ->
Lwt_io.printlf "(%Li,%f)" cap fill_rate >>= fun () ->
Lwt_result.return ()
(*
let peer2peer_test session conn =
let peer = "192.168.11.102", 8000, false in
let operations = [
("x_00000", None);
("x_00010", None);
("x_00100", Some "y_00100");
]
in
K.p2p_push session conn peer operations
*)
let maybe_init_ssl =
let ok = ref false in
(fun () ->
if !ok then ()
else
begin
Ssl_threads.init ();
Ssl.init ~thread_safe:true ();
ok := true
end
)
let make_socket_address h p = Unix.ADDR_INET(Unix.inet_addr_of_string h, p)
let ssl_connect ctx ip port =
Lwt_log.debug_f "ssl_connect:(%s,%i)" ip port >>= fun () ->
let sa = Unix.ADDR_INET(Unix.inet_addr_of_string ip, port) in
let domain = Unix.domain_of_sockaddr sa in
let socket = Lwt_unix.socket domain Unix.SOCK_STREAM 0 in
Lwt_unix.connect socket sa >>= fun () ->
Lwt_log.debug_f "connected" >>= fun () ->
(*
Ssl.set_verify ctx
[Ssl.Verify_peer; Ssl.Verify_fail_if_no_peer_cert]
(Some Ssl.client_verify_callback);
Ssl.load_verify_locations ctx ca_cert "";
*)
Lwt_ssl.ssl_connect socket ctx >>= fun ssl_socket ->
Lwt_log.debug_f "ssl_connect ok" >>= fun () ->
Lwt.return ssl_socket
let make_client ?ctx ?secret ?cluster_version ?trace ~ip ~port =
let sa = make_socket_address ip port in
let domain = Unix.domain_of_sockaddr sa in
match ctx with
| None ->
let socket = Lwt_unix.socket domain Unix.SOCK_STREAM 0 in
Lwt_unix.connect socket sa >>= fun () ->
let closer () =
Lwt.catch
(fun () -> Lwt_unix.close socket )
(fun exn -> Lwt_log.info ~exn "during close")
in
let ssl_socket = Lwt_ssl.plain socket in
K.wrap_socket ?secret ?cluster_version ?trace ssl_socket closer
| Some ctx ->
ssl_connect ctx ip port >>= fun ssl_socket ->
let closer () =
Lwt.catch
(fun () -> Lwt_ssl.close ssl_socket)
(fun exn -> Lwt_log.info ~exn "during close ")
in
K.wrap_socket ?trace ?secret ?cluster_version ssl_socket closer
let with_client ?ctx ?secret ?cluster_version ?trace ~ip ~port (f:K.client -> 'a K.result) : 'a K.result =
make_client ?ctx ?secret ?cluster_version ?trace ~ip ~port
>>=? fun client ->
Lwt.finalize
(fun () -> f client)
(fun () -> K.dispose client)
let fail_on_error =
function
| Result.Ok x -> Lwt.return x
| Error e -> Lwt.fail_with (Error.show e)
let run_with_client ip port trace ssl (f:K.client -> 'a K.result) : 'a =
let ctx =
if ssl then
begin
maybe_init_ssl ();
let protocol = Ssl.TLSv1_2 in
let ctx = Ssl.create_context protocol Ssl.Client_context in
Some ctx
end
else
None
in
let t =
Lwt_log.debug_f
"ip:%S port:%i trace:%b" ip port trace
>>= fun () ->
with_client ?ctx ~ip ~port ~trace f
>>= fail_on_error
in
Lwt_log.add_rule "*" Lwt_log.Debug;
Lwt_main.run t
let get_info ip port trace ssl =
run_with_client ip port trace ssl
(fun client ->
Lwt_io.printlf "config:%s" (client |> K.get_session |> K.get_config |> Config.show)
>>= fun () ->
Lwt_result.return ()
)
let instant_secure_erase ip port trace =
let f client =
let session = K.get_session client in
let config = K.get_config session in
Lwt_io.printlf "%s" (Config.show config) >>= fun () ->
K.instant_secure_erase client
in
let ssl = true in
run_with_client ip port trace ssl f
let download_firmware ip port trace file_name =
let f client =
let session = K.get_session client in
let config = K.get_config session in
Lwt_io.printlf "%s" (Config.show config) >>= fun () ->
Lwt_unix.stat file_name >>= fun stat ->
let size = stat.Lwt_unix.st_size in
let slod = Bytes.create size in
Lwt_io.with_file
~mode:Lwt_io.input file_name
(fun ic ->
Lwt_io.read_into_exactly ic slod 0 size
)
>>= fun () ->
Lwt_io.printlf "update has %i bytes%!" size >>= fun () ->
let v_slice = (slod,0,size) in
K.download_firmware client v_slice
in
let ssl = true in
run_with_client ip port trace ssl f
let run_tests ip port trace ssl filter =
let run_tests client tests =
Lwt_list.map_s
(fun (test_name, test) ->
if filter = [] || List.mem test_name filter
then
begin
lwt_test test_name
(fun () -> test client)
>>= fun (tr: test_result) ->
Lwt.return (test_name, tr)
end
else
Lwt.return (test_name, Skipped)
)
tests
in
let f client : unit K.result =
run_tests client [
"get_non_existing",test_get_non_existing;
"noop", test_noop;
"put_get_delete", test_put_get_delete;
"put_version", test_put_version;
"put_empty_string", test_put_empty_string;
"put_timeout", test_put_timeout;
"put_largish", test_put_largish;
"range_test", range_test;
"range_test_reverse", range_test_reverse;
"batch_single_put", batch_single_put;
"batch_put_delete", batch_test_put_delete;
"batch_delete_non_existing", batch_delete_non_existing;
"batch_3_puts", batch_3_puts;
"crc32", test_crc32;
"get_capacities", get_capacities_test;
"put_no_tag", test_put_no_tag;
"batch_too_fat", batch_too_fat;
(*"peer2peer", peer2peer_test;*)
]
>>= fun results ->
Lwt_list.iter_s
(fun (n, r) -> Lwt_io.printlf "%-32s => %s" n (show_test_result r))
results
>>= fun () ->
Lwt_result.return ()
in
run_with_client ip port trace ssl f
module Cli = struct
open Cmdliner
let ip =
Arg.(value
& opt string "::1"
& info ["h";"host"] ~docv:"HOST" ~doc:"the host to connect with")
let port default =
let doc = "tcp $(docv)" in
Arg.(value
& opt int default
& info ["p"; "port"] ~docv:"PORT" ~doc)
let trace =
Arg.(value
& flag
& info ["trace"]
~doc:"trace sent/received messages")
let ssl =
Arg.(value
& flag
& info ["ssl"]
~doc:"perform communication using ssl sockets")
let filter =
Arg.(value
& opt_all string []
& info ["filter"] ~doc:"run test(s) matching"
)
let file =
Arg.(required
& opt (some non_dir_file) None
& info ["file"] ~docv:"FILE")
let run_tests_cmd =
let open Term in
(pure run_tests
$ ip
$ port 8123
$ trace
$ ssl
$ filter
),
info
"run-tests"
~doc:"runs tests"
let instant_secure_erase_cmd =
let open Term in
(pure instant_secure_erase
$ ip
$ port 8443
$ trace
),
info
"instant-secure-erase"
~doc:"erases all data from the drive. Warranty void. You have been warned"
let download_firmware_cmd =
let open Term in
(pure download_firmware
$ ip
$ port 8443
$ trace
$ file
),
info
"download-firmware"
~doc:"flash new firmware on drive. Warranty void. You have been warned."
let get_info_cmd =
let open Term in
(pure get_info
$ ip
$ port 8443
$ trace
$ ssl
),
info "get-info"
~doc:"retrieve & dump some information about a drive"
let default () =
Printf.printf "an ocaml client for kinetic drives: tester & cli %!"
let default_cmd =
Term.(const default $ const ()),
Term.info "" ~doc:"what's possible?"
end
let () =
let open Cli in
let open Cmdliner in
let cmds =
[run_tests_cmd;
instant_secure_erase_cmd;
download_firmware_cmd;
get_info_cmd;
]
in
match Term.eval_choice default_cmd cmds with
| `Error _ -> exit 1
| _ -> exit 0