Skip to content

Commit

Permalink
fix(runtest): clean up after unit test
Browse files Browse the repository at this point in the history
`parse_test` didn't clean up when the test finished and it left some files in `/tmp`.
Because the filenames used by the test are deterministically generated a new test run would find a file created by a previous run and fail.
E.g. the RO test would 'chmod' a file to be read-only, and would thus fail to be 'created' by a subsequent test that attempts to create a VHD by opening and closing it in RW mode.

This doesn't fail on the CI or Koji because they always run the tests in a clean env, but it does fail locally when running the test multiple times.
(Most of the time 'dune cache' will prevent rerunning the test, but not always, e.g. if you've just trimmed the cache because you were low on disk space).

Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Sep 26, 2023
1 parent 79ea2a8 commit f1513e2
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions ocaml/libs/vhd/vhd_format_lwt_test/parse_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@ let fill_sector_with pattern =
done ;
b

let with_new_filename f =
let name = make_new_filename () in
let safe_unlink () =
try Unix.unlink name with Unix.Unix_error(_,_,_) -> ()
in
safe_unlink ();
Fun.protect ~finally:safe_unlink (fun () -> f name)

(* Create a dynamic disk, check headers *)
let check_empty_disk size =
let filename = make_new_filename () in
with_new_filename @@ fun filename ->
Vhd_IO.create_dynamic ~filename ~size () >>= fun vhd ->
Vhd_IO.openchain filename false >>= fun vhd' ->
assert_equal ~printer:Header.to_string ~cmp:Header.equal vhd.Vhd.header
Expand All @@ -64,7 +72,7 @@ let check_empty_disk size =
(* Create a disk, resize it, check headers *)
let check_resize size =
let newsize = max 0L (Int64.pred size) in
let filename = make_new_filename () in
with_new_filename @@ fun filename ->
Vhd_IO.create_dynamic ~filename ~size () >>= fun vhd ->
let vhd = Vhd.resize vhd newsize in
Vhd_IO.close vhd >>= fun () ->
Expand All @@ -75,9 +83,9 @@ let check_resize size =

(* Create a snapshot, check headers *)
let check_empty_snapshot size =
let filename = make_new_filename () in
with_new_filename @@ fun filename ->
Vhd_IO.create_dynamic ~filename ~size () >>= fun vhd ->
let filename = make_new_filename () in
with_new_filename @@ fun filename ->
Vhd_IO.create_difference ~filename ~parent:vhd () >>= fun vhd' ->
Vhd_IO.openchain filename false >>= fun vhd'' ->
assert_equal ~printer:Header.to_string ~cmp:Header.equal vhd'.Vhd.header
Expand All @@ -91,18 +99,18 @@ let check_empty_snapshot size =
let check_reparent () =
let all_ones = fill_sector_with "1" in
let all_twos = fill_sector_with "2" in
let p1 = make_new_filename () in
with_new_filename @@ fun p1 ->
let size = Int64.mul 1024L 1024L in
Vhd_IO.create_dynamic ~filename:p1 ~size () >>= fun vhd ->
(* write '1' into block 0 *)
Vhd_IO.write vhd 0L [all_ones] >>= fun () ->
Vhd_IO.close vhd >>= fun () ->
let p2 = make_new_filename () in
with_new_filename @@ fun p2 ->
Vhd_IO.create_dynamic ~filename:p2 ~size () >>= fun vhd ->
(* write '2' into block 0 *)
Vhd_IO.write vhd 0L [all_twos] >>= fun () ->
Vhd_IO.close vhd >>= fun () ->
let l = make_new_filename () in
with_new_filename @@ fun l ->
Vhd_IO.openchain p1 false >>= fun vhd ->
Vhd_IO.create_difference ~filename:l ~parent:vhd () >>= fun vhd' ->
(* Verify block 0 has '1' *)
Expand All @@ -125,7 +133,7 @@ let check_reparent () =

(* Check ../ works in parent locator *)
let check_parent_parent_dir () =
let filename = make_new_filename () in
with_new_filename @@ fun filename ->
Vhd_IO.create_dynamic ~filename ~size:0L () >>= fun vhd ->
let leaf_path = Filename.(concat (concat tmp_file_dir "leaves") "leaf.vhd") in
let leaf_dir = Filename.dirname leaf_path in
Expand All @@ -140,7 +148,7 @@ let check_parent_parent_dir () =

(* Check we respect RO-ness *)
let check_readonly () =
let filename = make_new_filename () in
with_new_filename @@ fun filename ->
Vhd_IO.create_dynamic ~filename ~size:0L () >>= fun vhd ->
Vhd_IO.close vhd >>= fun () ->
Unix.chmod filename 0o444 ;
Expand Down

0 comments on commit f1513e2

Please sign in to comment.