Skip to content

Commit 0a843cc

Browse files
test: Keep track of test artifacts and cleanup
Signed-off-by: Si Beaumont <[email protected]>
1 parent 321b121 commit 0a843cc

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ setup.log
66
setup.ml
77
*.native
88
.test-artifacts/
9+
.native-test/
910
dm-mock*
1011
*.dat
1112
*.gp

test/common.ml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ open Lwt
2020
(* Mock kernel devices so we can run as a regular user *)
2121
let use_mock = ref true
2222

23+
(* Paths that will get cleaned up *)
24+
let artifacts_dir = ".native-test/"
25+
let test_artifact = Filename.concat artifacts_dir
26+
let xenvmd_socket = test_artifact "xenvmd.socket"
27+
let xenvmd_conf = test_artifact "xenvmd.conf"
28+
let xenvmd_log = test_artifact "xenvmd.log"
29+
let xenvm_confdir host = test_artifact (Printf.sprintf "xenvm-host%d.d" host)
30+
let la_socket host = test_artifact (Printf.sprintf "local-allocator-host%d.socket" host)
31+
let la_conf host = test_artifact (Printf.sprintf "local-allocator-host%d.conf" host)
32+
let la_log host = test_artifact (Printf.sprintf "local-allocator-host%d.log" host)
33+
let la_journal host = test_artifact (Printf.sprintf "local-allocator-host%d.journal" host)
34+
let la_toLVM_ring host = (Printf.sprintf "host%d-toLVM" host)
35+
let la_fromLVM_ring host = (Printf.sprintf "host%d-fromLVM" host)
36+
2337
module Time = struct
2438
type 'a io = 'a Lwt.t
2539
let sleep = Lwt_unix.sleep
@@ -252,7 +266,7 @@ let tib = Int64.(gib * kib)
252266
module Client = Xenvm_client.Client
253267

254268
let with_temp_file ?(delete=true) fn =
255-
let filename = Filename.concat (Unix.getcwd ()) "vg" in
269+
let filename = test_artifact "mock-disk" in
256270
let f = Unix.openfile filename [Unix.O_CREAT; Unix.O_RDWR; Unix.O_TRUNC] 0o644 in
257271
(* approximately 10000 4MiB extents for volumes, 100MiB for metadata and
258272
overhead *)
@@ -289,15 +303,11 @@ let with_block filename f =
289303
| `Ok x ->
290304
f x (* no point catching errors here *)
291305

292-
let get_configdir host =
293-
Printf.sprintf "/tmp/xenvm.host%d.d" host
294-
295306
let xenvm ?(host=1) args =
296-
let configdir = get_configdir host in
297307
match args with
298308
| [] -> run "./xenvm.native" []
299309
| cmd :: args ->
300-
let args = "--configdir" :: configdir :: args in
310+
let args = "--configdir" :: xenvm_confdir host :: args in
301311
let args = if !use_mock then "--mock-devmapper" :: (string_of_int host) :: args else args in
302312
run "./xenvm.native" (cmd :: args)
303313
let xenvmd = run "./xenvmd.native"
@@ -308,3 +318,12 @@ let local_allocator ?(host=1) =
308318
| args ->
309319
let args = if !use_mock then "--mock-devmapper" :: (string_of_int host) :: args else args in
310320
run cmd args
321+
322+
let cleanup () =
323+
let pkill pattern =
324+
try run "pkill" ["-e"; pattern] |> print_endline
325+
with Bad_exit(1,_,_,_,_) -> () (* No processes matched -- see pkill(1) *)
326+
in
327+
pkill "xenvm";
328+
pkill "local_allocator";
329+
run "rm" ["-rf"; artifacts_dir; "dm-mock*"] |> print_endline

test/test.ml

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ open Lwt
2020

2121
let vg = "myvg"
2222

23-
let get_hostname host =
24-
Printf.sprintf "host%d" host
25-
26-
let get_local_allocator_sockpath host =
27-
Printf.sprintf "/tmp/host%d-socket" host
28-
2923
let vgcreate =
3024
"vgcreate <vg name> <device>: check that we can create a volume group and re-read the metadata." >::
3125
fun () ->
@@ -42,26 +36,25 @@ let vgcreate =
4236
Lwt_main.run t
4337
)
4438

39+
let set_vg_info ~pvpath ~vg ~host =
40+
xenvm ~host [ "set-vg-info";
41+
"--pvpath"; pvpath;
42+
"-S"; xenvmd_socket;
43+
vg;
44+
"--local-allocator-path"; la_socket host;
45+
"--uri"; "file://local/services/xenvmd/" ^ vg;
46+
] |> ignore_string
47+
4548
let vgs_offline =
4649
"vgs <device>: check that we can read metadata without xenvmd." >::
4750
(fun () ->
48-
with_temp_file (fun filename ->
49-
xenvm [ "vgcreate"; vg; filename ] |> ignore_string;
50-
mkdir_rec "/tmp/xenvm.d" 0o0755;
51-
xenvm [ "set-vg-info"; "--pvpath"; filename; "-S"; "/tmp/xenvmd"; vg; "--local-allocator-path"; (get_local_allocator_sockpath 1); "--uri"; "file://local/services/xenvmd/"^vg ] |> ignore_string;
51+
with_temp_file (fun pvpath ->
52+
xenvm [ "vgcreate"; vg; pvpath ] |> ignore_string;
53+
set_vg_info ~pvpath ~vg ~host:1;
5254
xenvm [ "vgs"; vg ] |> ignore_string
5355
)
5456
)
5557

56-
let set_vg_info pvpath vg host =
57-
xenvm ~host [ "set-vg-info";
58-
"--pvpath"; pvpath;
59-
"-S"; "/tmp/xenvmd";
60-
vg;
61-
"--local-allocator-path"; get_local_allocator_sockpath host;
62-
"--uri"; "file://local/services/xenvmd/"^vg ]
63-
|> ignore_string
64-
6558
let is_xenvmd_ok vg =
6659
try
6760
ignore(xenvm [ "host-list"; vg ]);
@@ -82,19 +75,22 @@ let with_xenvmd ?existing_vg ?(cleanup_vg=true) (f : string -> string -> 'a) =
8275
set_vg_info loop vg host;
8376
let config = {
8477
Config.Xenvmd.listenPort = None;
85-
listenPath = Some "/tmp/xenvmd";
78+
listenPath = Some xenvmd_socket;
8679
host_allocation_quantum = 128L;
8780
host_low_water_mark = 8L;
8881
vg;
8982
devices = [loop];
9083
rrd_ds_owner = Some (Printf.sprintf "xenvmd-%d-stats" (Unix.getpid ()));
9184
} in
9285
Sexplib.Sexp.to_string_hum (Config.Xenvmd.sexp_of_t config)
93-
|> file_of_string "test.xenvmd.conf";
94-
let _ = Lwt_preemptive.detach (fun () -> xenvmd [ "--config"; "./test.xenvmd.conf"; "--log"; "xenvmd.log"]) () in
86+
|> file_of_string xenvmd_conf;
87+
let _ =
88+
Lwt_preemptive.detach (fun () ->
89+
xenvmd [ "--config"; xenvmd_conf; "--log"; xenvmd_log]
90+
) () in
9591
wait_for_xenvmd_to_start vg;
9692
Xenvm_client.Rpc.uri := "file://local/services/xenvmd/" ^ vg;
97-
Xenvm_client.unix_domain_socket_path := "/tmp/xenvmd";
93+
Xenvm_client.unix_domain_socket_path := xenvmd_socket;
9894
finally
9995
(fun () -> f vg loop)
10096
(fun () -> xenvm [ "shutdown"; "/dev/"^vg ] |> ignore_string ) in
@@ -111,7 +107,7 @@ let with_xenvmd ?existing_vg ?(cleanup_vg=true) (f : string -> string -> 'a) =
111107
let la_has_started host =
112108
let dm_name = "XXXdoesntexistXXX" in
113109
let s = Lwt_unix.socket Lwt_unix.PF_UNIX Lwt_unix.SOCK_STREAM 0 in
114-
let sockpath = get_local_allocator_sockpath host in
110+
let sockpath = la_socket host in
115111
Lwt.catch
116112
(fun () ->
117113
Lwt_unix.connect s (Unix.ADDR_UNIX sockpath)
@@ -132,20 +128,22 @@ let la_has_started host =
132128
Lwt.return false)
133129

134130
let start_local_allocator host devices =
135-
let hostname = get_hostname host in
136-
ignore(xenvm [ "host-create"; vg; hostname]);
137-
let config_file = Printf.sprintf "local_allocator.%s.conf" hostname in
131+
let hostname = Printf.sprintf "host%d" host in
132+
ignore(xenvm [ "host-create"; vg; hostname ]);
138133
let config = {
139-
Config.Local_allocator.socket = get_local_allocator_sockpath host;
140-
localJournal = Printf.sprintf "%s-localJournal" hostname;
134+
Config.Local_allocator.socket = la_socket host;
135+
localJournal = la_journal host;
141136
devices = devices;
142-
toLVM = Printf.sprintf "%s-toLVM" hostname;
143-
fromLVM = Printf.sprintf "%s-fromLVM" hostname;
137+
toLVM = la_toLVM_ring host;
138+
fromLVM = la_fromLVM_ring host;
144139
} in
145140
Sexplib.Sexp.to_string_hum (Config.Local_allocator.sexp_of_t config)
146-
|> file_of_string config_file;
141+
|> file_of_string (la_conf host);
147142
ignore(xenvm [ "host-connect"; vg; hostname]);
148-
let la_thread = Lwt_preemptive.detach (fun () -> local_allocator ~host [ "--config"; config_file; "--log"; Printf.sprintf "la.%d.log" host ]) () in
143+
let la_thread =
144+
Lwt_preemptive.detach (fun () ->
145+
local_allocator ~host [ "--config"; la_conf host; "--log"; la_log host ]
146+
) () in
149147
la_thread
150148

151149
let wait_for_local_allocator_to_start host =
@@ -212,9 +210,9 @@ let upgrade =
212210
~printer:string_of_int 2 (LVs.cardinal (Vg_IO.metadata_of vg).Vg.lvs);
213211

214212
(* Upgrade the volume to journalled *)
215-
xenvm [ "upgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
213+
xenvm [ "upgrade"; "--pvpath"; filename; "vg" ] |> ignore_string;
216214
(* Check it's idempotent *)
217-
xenvm [ "upgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
215+
xenvm [ "upgrade"; "--pvpath"; filename; "vg" ] |> ignore_string;
218216

219217
(* check the changing of the magic persisted *)
220218
let module Label_IO = Label.Make(Block) in
@@ -238,9 +236,9 @@ let upgrade =
238236
);
239237

240238
(* Downgrade the volume to lvm2 *)
241-
xenvm [ "downgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
239+
xenvm [ "downgrade"; "--pvpath"; filename; "vg" ] |> ignore_string;
242240
(* Check it's idempotent *)
243-
xenvm [ "downgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
241+
xenvm [ "downgrade"; "--pvpath"; filename; "vg" ] |> ignore_string;
244242

245243
(* check the changing of the magic persisted *)
246244
Label_IO.read block >>:= fun label ->
@@ -654,9 +652,9 @@ let la_extend_multi_fist device =
654652
let la_dead = la_thread_1 >>= fun _ -> la_thread_2 >>= fun _ -> Lwt.return () in
655653
Lwt.choose [la_dead; (Lwt_unix.sleep 30.0 >>= fun () -> Lwt.fail Timeout)]
656654
>>= fun () ->
657-
write_to_file la_thread_1 "local_allocator.1.log"
655+
write_to_file la_thread_1 (la_log 1)
658656
>>= fun () ->
659-
write_to_file la_thread_2 "local_allocator.2.log"
657+
write_to_file la_thread_2 (la_log 2)
660658
)))
661659

662660
let local_allocator_suite device = "Commands which require the local allocator" >::: [
@@ -667,8 +665,9 @@ let local_allocator_suite device = "Commands which require the local allocator"
667665
]
668666

669667
let _ =
668+
Common.cleanup ();
670669
Random.self_init ();
671-
List.iter (fun host -> let configdir = Common.get_configdir host in mkdir_rec configdir 0o0755) [1;2;3;4] in
670+
List.iter (fun host -> mkdir_rec (xenvm_confdir host) 0o755) [1; 2; 3; 4]
672671
let check_results_with_exit_code results =
673672
if List.exists (function RFailure _ | RError _ -> true | _ -> false) results
674673
then exit 1 in

0 commit comments

Comments
 (0)