ligo/test/lib/node_helpers.ml
Milo Davis 0a7f9a39a9 Switch to Jbuilder
This is a rewrite of the build system with `jbuilder`, with just a
minimal toplevel Makefile for backward compatibility.

This first patch preserves the project architecture, we only gain
proper dependencies handling and always up-to-date `.merlin` files.
A latter patch may split the project in smaller "sub-package",
i.e. multiple `.opam` files.

The embedded versions of the economic protocol are now compiled with
`jbuilder` instead of `tezos-protocol-compiler`, potentially allowing
proper inlining at the cost of slightly-less-stricter
sandboxing. Nevertheless, dynamically loaded protocol are still
compiled with the `tezos-protocol-compiler` and thus strictly
sandboxed ; and a CI rule also checks the proper sandboxing of
embedded protocols.

This patch is coauthored with @hnrgrgr
2017-10-31 20:33:56 +00:00

72 lines
2.8 KiB
OCaml

(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
exception Node_exited_prematurely
let handle_error res log_file_name =
match res with
| 0, _ ->
()
| pid, Unix.WEXITED x ->
Printf.eprintf "Wait: %d, exit %d\n\nDumping log:\n\n%!" pid x ;
ignore (Sys.command (Printf.sprintf "cat %s" log_file_name) : int) ;
raise Node_exited_prematurely
| pid, Unix.WSIGNALED x ->
Printf.eprintf "Wait: %d, signaled %d\n\nDumping log:\n\n%!" pid x ;
ignore (Sys.command (Printf.sprintf "cat %s" log_file_name) : int) ;
raise Node_exited_prematurely
| pid, Unix.WSTOPPED x ->
Printf.eprintf "Wait: %d, stopped %d\n\nDumping log:\n\n%!" pid x ;
ignore (Sys.command (Printf.sprintf "cat %s" log_file_name) : int) ;
raise Node_exited_prematurely
let fork_node ?(timeout = 4) ?(port = 18732) ?sandbox () =
let data_dir =
Printf.sprintf
"%s/tezos_node_%6X"
(Filename.get_temp_dir_name ())
(Random.int 0xFF_FF_FF) in
let log_file_name, log_file =
Filename.open_temp_file "tezos_node_" ".log" in
let log_fd = Unix.descr_of_out_channel log_file in
let null_fd = Unix.(openfile "/dev/null" [O_RDONLY] 0o644) in
let exe =
let (//) = Filename.concat in
Filename.(Sys.getcwd () // ".." // "src" // "node_main.exe") in
let pid =
Unix.create_process exe
[| "tezos-node" ;
"run" ;
"--data-dir"; data_dir ;
(match sandbox with
| None -> "--sandbox"
| Some path -> "--sandbox=" ^ path);
"--rpc-addr"; "[::]:" ^ string_of_int port |]
null_fd log_fd log_fd in
Printf.printf "Created node, pid: %d, log: %s\n%!" pid log_file_name ;
Printf.printf "Waiting %d seconds for its initialisation\n%!" timeout ;
Unix.sleep timeout ;
match Unix.waitpid [Unix.WNOHANG] pid with
| 0, _ ->
Pervasives.at_exit begin fun () ->
begin
match Unix.waitpid [Unix.WNOHANG] pid with
| 0, _ ->
Unix.kill pid Sys.sigkill ;
Unix.sleep 1
| res ->
handle_error res log_file_name
end ;
ignore (Sys.command (Printf.sprintf "rm -fr \"%s\"" data_dir))
end ;
pid
| res ->
handle_error res log_file_name ;
0