Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more tests for mina cli. #16402

Open
wants to merge 1 commit into
base: compatible
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 130 additions & 6 deletions src/test/command_line_tests/command_line_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ open Mina_bootstrapper
open Mina_cli
open Mina_daemon
open Runner
open Core

(**
* Test the basic functionality of the mina daemon and client through the CLI
*)

module CliTests = struct
let run test_case mina_path =
Async.Thread_safe.block_on_async_exn (fun () ->
TestRunner.run test_case mina_path )

let test_background_daemon mina_path =
let test_case config =
let open Deferred.Or_error.Let_syntax in
let bootstrapper = MinaBootstrapper.create config in
let%bind _ = MinaBootstrapper.start bootstrapper in
let%bind _ = MinaBootstrapper.wait_for_bootstrap bootstrapper in
let mina_cli = MinaCli.create config.port config.mina_exe in
let mina_cli = MinaCli.create ~port:config.port config.mina_exe in
let%map _ = MinaCli.stop_daemon mina_cli in
()
in
Async.Thread_safe.block_on_async_exn (fun () ->
TestRunner.run test_case mina_path )
run test_case mina_path

let test_daemon_recover mina_path =
let test_case config =
Expand All @@ -32,12 +36,108 @@ module CliTests = struct
let%bind _ = MinaDaemon.force_kill daemon in
let%bind _ = MinaBootstrapper.start bootstrapper in
let%bind _ = MinaBootstrapper.wait_for_bootstrap bootstrapper in
let mina_cli = MinaCli.create config.port config.mina_exe in
let mina_cli = MinaCli.create ~port:config.port config.mina_exe in
let%map _ = MinaCli.stop_daemon mina_cli in
()
in
Async.Thread_safe.block_on_async_exn (fun () ->
TestRunner.run test_case mina_path )
run test_case mina_path

let generate_random_ledger mina_cli =
let open Deferred.Or_error.Let_syntax in
let temp_file = Filename.temp_file "commandline" "ledger.json" in
let%bind ledger_content = MinaCli.test_ledger mina_cli ~n:10 in
let accounts =
Yojson.Safe.from_string ledger_content
|> Runtime_config.Accounts.of_yojson |> Result.ok_or_failwith
in
Runtime_config.Accounts.to_yojson accounts |> Yojson.Safe.to_file temp_file ;
return (accounts, temp_file)

let assert_don't_contain_log_output output =
if String.is_substring ~substring:"{\"timestamp\":" output then
failwith "logger output detected"
else ()

let test_ledger_hash mina_path =
let test_case (config : Config.Config.t) =
let open Deferred.Or_error.Let_syntax in
let mina_cli = MinaCli.create config.mina_exe in
let%bind _, ledger_file = generate_random_ledger mina_cli in
let%bind hash = MinaCli.ledger_hash mina_cli ~ledger_file in
assert_don't_contain_log_output hash ;
if not (String.is_prefix ~prefix:"j" hash) then
failwith "invalid ledger hash prefix" ;

if Int.( <> ) (String.length hash) 51 then
failwith "invalid ledger hash length" ;

return ()
in
run test_case mina_path

let test_ledger_currency mina_path =
let test_case (config : Config.Config.t) =
let open Deferred.Or_error.Let_syntax in
let mina_cli = MinaCli.create config.mina_exe in
let%bind accounts, ledger_file = generate_random_ledger mina_cli in
let total_currency =
List.map accounts ~f:(fun account ->
Currency.Balance.to_nanomina_int account.balance )
|> List.sum (module Int) ~f:Fn.id
in
let%bind output = MinaCli.ledger_currency mina_cli ~ledger_file in
let total_currency_float = float_of_int total_currency /. 1000000000.0 in
let total_currency_string =
Printf.sprintf "MINA: %.9f" total_currency_float
in
assert_don't_contain_log_output output ;
if not (String.equal total_currency_string (String.strip output)) then
failwithf "invalid mina total count %s vs %s" total_currency_string
output () ;
return ()
in
run test_case mina_path

let test_advanced_print_signature_kind mina_path =
let test_case (config : Config.Config.t) =
let open Deferred.Or_error.Let_syntax in
let mina_cli = MinaCli.create config.mina_exe in
let%bind output = MinaCli.advanced_print_signature_kind mina_cli in
let expected = "testnet" in
assert_don't_contain_log_output output ;
if not (String.equal expected (String.strip output)) then
failwithf "invalid signature kind %s vs %s" expected output () ;
return ()
in
run test_case mina_path

let test_advanced_compile_time_constants mina_path =
let test_case (config : Config.Config.t) =
let open Deferred.Or_error.Let_syntax in
let mina_cli = MinaCli.create config.mina_exe in
let%bind config_content = MinaCli.test_ledger mina_cli ~n:10 in
let config_content =
Printf.sprintf "{ \"ledger\":{ \"accounts\":%s } }" config_content
in
let temp_file = Filename.temp_file "commandline" "ledger.json" in
Yojson.Safe.from_string config_content |> Yojson.Safe.to_file temp_file ;
let%bind output =
MinaCli.advanced_compile_time_constants mina_cli ~config_file:temp_file
in
assert_don't_contain_log_output output ;
return ()
in
run test_case mina_path

let test_advanced_constraint_system_digests mina_path =
let test_case (config : Config.Config.t) =
let open Deferred.Or_error.Let_syntax in
let mina_cli = MinaCli.create config.mina_exe in
let%bind output = MinaCli.advanced_constraint_system_digests mina_cli in
assert_don't_contain_log_output output ;
return ()
in
run test_case mina_path
end

let mina_path =
Expand All @@ -58,4 +158,28 @@ let () =
, [ test_case "The mina daemon recovers from crash" `Quick
CliTests.test_daemon_recover
] )
; ( "ledger-hash"
, [ test_case "The mina ledger hash evaluates correctly" `Quick
CliTests.test_ledger_hash
] )
; ( "ledger-currency"
, [ test_case "The mina ledger currency evaluates correctly" `Quick
CliTests.test_ledger_currency
] )
; ( "advanced-print-signature-kind"
, [ test_case "The mina cli prints correct signature kind" `Quick
CliTests.test_advanced_print_signature_kind
] )
; ( "advanced-compile-time-constants"
, [ test_case
"The mina cli does not print log when printing compile time \
constants"
`Quick CliTests.test_advanced_compile_time_constants
] )
; ( "advanced-constraint-system-digests"
, [ test_case
"The mina cli does not print log when printing constrain system \
digests"
`Quick CliTests.test_advanced_constraint_system_digests
] )
]
2 changes: 1 addition & 1 deletion src/test/command_line_tests/mina_bootstrapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module MinaBootstrapper = struct
Process.create ~prog ~args ()

let wait_for_bootstrap t =
let mina_cli = MinaCli.create t.config.port t.config.mina_exe in
let mina_cli = MinaCli.create ~port:t.config.port t.config.mina_exe in
Async.printf "Waiting initial %d s. before connecting\n"
(int_of_float t.client_delay) ;
let%bind _ =
Expand Down
31 changes: 30 additions & 1 deletion src/test/command_line_tests/mina_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open Async
module MinaCli = struct
type t = { port : int; mina_exe : string }

let create port mina_exe = { port; mina_exe }
let create ?(port = 3085) mina_exe = { port; mina_exe }

let stop_daemon t =
Process.run () ~prog:t.mina_exe
Expand All @@ -14,4 +14,33 @@ module MinaCli = struct
Process.run ~prog:t.mina_exe
~args:[ "client"; "status"; "-daemon-port"; sprintf "%d" t.port ]
()

let ledger_hash t ~ledger_file =
Process.run ~prog:t.mina_exe
~args:[ "ledger"; "hash"; "--ledger-file"; ledger_file ]
()

let ledger_currency t ~ledger_file =
Process.run ~prog:t.mina_exe
~args:[ "ledger"; "currency"; "--ledger-file"; ledger_file ]
()

let test_ledger t ~(n : int) =
Process.run ~prog:t.mina_exe
~args:[ "ledger"; "test"; "generate-accounts"; "-n"; string_of_int n ]
()

let advanced_print_signature_kind t =
Process.run ~prog:t.mina_exe ~args:[ "advanced"; "print-signature-kind" ] ()

let advanced_compile_time_constants t ~config_file =
Process.run ~prog:t.mina_exe
~args:
[ "advanced"; "compile-time-constants"; "--config-file"; config_file ]
()

let advanced_constraint_system_digests t =
Process.run ~prog:t.mina_exe
~args:[ "advanced"; "constraint-system-digests" ]
()
end