|
1 | | -open Async |
2 | | -open Mina_bootstrapper |
3 | | -open Mina_cli |
4 | | -open Mina_daemon |
5 | | -open Runner |
6 | 1 | open Core |
| 2 | +open Async |
| 3 | +open Mina_automation |
7 | 4 |
|
8 | | -(** |
9 | | - * Test the basic functionality of the mina daemon and client through the CLI |
10 | | - *) |
11 | | - |
12 | | -module CliTests = struct |
13 | | - let run test_case mina_path = |
14 | | - Async.Thread_safe.block_on_async_exn (fun () -> |
15 | | - TestRunner.run test_case mina_path ) |
16 | | - |
17 | | - let test_background_daemon mina_path = |
18 | | - let test_case config = |
19 | | - let open Deferred.Or_error.Let_syntax in |
20 | | - let bootstrapper = MinaBootstrapper.create config in |
21 | | - let%bind _ = MinaBootstrapper.start bootstrapper in |
22 | | - let%bind _ = MinaBootstrapper.wait_for_bootstrap bootstrapper in |
23 | | - let mina_cli = MinaCli.create ~port:config.port config.mina_exe in |
24 | | - let%map _ = MinaCli.stop_daemon mina_cli in |
25 | | - () |
| 5 | +module BackgroundMode = struct |
| 6 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 7 | + |
| 8 | + let test_case (test : t) = |
| 9 | + let daemon = Daemon.of_config test.config in |
| 10 | + let%bind () = Daemon.Config.generate_keys test.config in |
| 11 | + let ledger_file = test.config.dirs.conf ^/ "daemon.json" in |
| 12 | + let%bind () = |
| 13 | + Mina_automation_fixture.Daemon.generate_random_config daemon ledger_file |
26 | 14 | in |
27 | | - run test_case mina_path |
28 | | - |
29 | | - let test_daemon_recover mina_path = |
30 | | - let test_case config = |
31 | | - let open Deferred.Or_error.Let_syntax in |
32 | | - let bootstrapper = MinaBootstrapper.create config in |
33 | | - let%bind process = MinaBootstrapper.start bootstrapper in |
34 | | - let daemon = MinaDaemon.create process config in |
35 | | - let%bind _ = MinaBootstrapper.wait_for_bootstrap bootstrapper in |
36 | | - let%bind _ = MinaDaemon.force_kill daemon in |
37 | | - let%bind _ = MinaBootstrapper.start bootstrapper in |
38 | | - let%bind _ = MinaBootstrapper.wait_for_bootstrap bootstrapper in |
39 | | - let mina_cli = MinaCli.create ~port:config.port config.mina_exe in |
40 | | - let%map _ = MinaCli.stop_daemon mina_cli in |
41 | | - () |
| 15 | + let%bind process = Daemon.start daemon in |
| 16 | + let%bind result = Daemon.Client.wait_for_bootstrap process.client () in |
| 17 | + let%bind () = |
| 18 | + match result with |
| 19 | + | Ok () -> |
| 20 | + Deferred.return () |
| 21 | + | Error e -> |
| 22 | + let () = printf "Error:\n%s\n" (Error.to_string_hum e) in |
| 23 | + let log_file = Daemon.Config.ConfigDirs.mina_log test.config.dirs in |
| 24 | + let%bind logs = Reader.file_contents log_file in |
| 25 | + let () = printf "Daemon logs:\n%s\n" logs in |
| 26 | + Writer.flushed (Lazy.force Writer.stdout) |
42 | 27 | in |
43 | | - run test_case mina_path |
| 28 | + let%bind () = Daemon.Client.stop_daemon process.client in |
| 29 | + Deferred.Or_error.return Mina_automation_fixture.Intf.Passed |
| 30 | +end |
44 | 31 |
|
45 | | - let generate_random_ledger mina_cli = |
46 | | - let open Deferred.Or_error.Let_syntax in |
47 | | - let temp_file = Filename.temp_file "commandline" "ledger.json" in |
48 | | - let%bind ledger_content = MinaCli.test_ledger mina_cli ~n:10 in |
49 | | - let accounts = |
50 | | - Yojson.Safe.from_string ledger_content |
51 | | - |> Runtime_config.Accounts.of_yojson |> Result.ok_or_failwith |
| 32 | +module DaemonRecover = struct |
| 33 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 34 | + |
| 35 | + let test_case (test : t) = |
| 36 | + let daemon = Daemon.of_config test.config in |
| 37 | + let%bind () = Daemon.Config.generate_keys test.config in |
| 38 | + let ledger_file = test.config.dirs.conf ^/ "daemon.json" in |
| 39 | + let%bind () = |
| 40 | + Mina_automation_fixture.Daemon.generate_random_config daemon ledger_file |
| 41 | + in |
| 42 | + let%bind process = Daemon.start daemon in |
| 43 | + let%bind.Deferred.Result () = |
| 44 | + Daemon.Client.wait_for_bootstrap process.client () |
52 | 45 | in |
53 | | - Runtime_config.Accounts.to_yojson accounts |> Yojson.Safe.to_file temp_file ; |
54 | | - return (accounts, temp_file) |
55 | | - |
56 | | - let assert_don't_contain_log_output output = |
57 | | - if String.is_substring ~substring:"{\"timestamp\":" output then |
58 | | - failwith "logger output detected" |
59 | | - else () |
60 | | - |
61 | | - let test_ledger_hash mina_path = |
62 | | - let test_case (config : Config.Config.t) = |
63 | | - let open Deferred.Or_error.Let_syntax in |
64 | | - let mina_cli = MinaCli.create config.mina_exe in |
65 | | - let%bind _, ledger_file = generate_random_ledger mina_cli in |
66 | | - let%bind hash = MinaCli.ledger_hash mina_cli ~ledger_file in |
67 | | - assert_don't_contain_log_output hash ; |
68 | | - if not (String.is_prefix ~prefix:"j" hash) then |
69 | | - failwith "invalid ledger hash prefix" ; |
70 | | - |
71 | | - if Int.( <> ) (String.length hash) 52 then |
72 | | - failwithf "invalid ledger hash length (%d)" (String.length hash) () ; |
73 | | - |
74 | | - return () |
| 46 | + let%bind.Deferred.Result _ = Daemon.Process.force_kill process in |
| 47 | + let%bind process = Daemon.start daemon in |
| 48 | + let%bind.Deferred.Result () = |
| 49 | + Daemon.Client.wait_for_bootstrap process.client () |
75 | 50 | in |
76 | | - run test_case mina_path |
77 | | - |
78 | | - let test_ledger_currency mina_path = |
79 | | - let test_case (config : Config.Config.t) = |
80 | | - let open Deferred.Or_error.Let_syntax in |
81 | | - let mina_cli = MinaCli.create config.mina_exe in |
82 | | - let%bind accounts, ledger_file = generate_random_ledger mina_cli in |
83 | | - let total_currency = |
84 | | - List.map accounts ~f:(fun account -> |
85 | | - Currency.Balance.to_nanomina_int account.balance ) |
86 | | - |> List.sum (module Int) ~f:Fn.id |
87 | | - in |
88 | | - let%bind output = MinaCli.ledger_currency mina_cli ~ledger_file in |
89 | | - let actual = Scanf.sscanf output "MINA : %f" (fun actual -> actual) in |
90 | | - let total_currency_float = float_of_int total_currency /. 1000000000.0 in |
91 | | - assert_don't_contain_log_output output ; |
92 | | - if not Float.(abs (total_currency_float - actual) < 0.001) then |
93 | | - failwithf "invalid mina total count %f vs %f" total_currency_float |
94 | | - actual () ; |
95 | | - return () |
| 51 | + let%bind () = Daemon.Client.stop_daemon process.client in |
| 52 | + Deferred.Or_error.return Mina_automation_fixture.Intf.Passed |
| 53 | +end |
| 54 | + |
| 55 | +let contain_log_output output = |
| 56 | + String.is_substring ~substring:"{\"timestamp\":" output |
| 57 | + |
| 58 | +module LedgerHash = struct |
| 59 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 60 | + |
| 61 | + let test_case (test : t) = |
| 62 | + let daemon = Daemon.of_config test.config in |
| 63 | + let client = Daemon.client daemon in |
| 64 | + let ledger_file = test.config.dirs.conf ^/ "daemon.json" in |
| 65 | + let%bind _ = |
| 66 | + Mina_automation_fixture.Daemon.generate_random_accounts daemon ledger_file |
| 67 | + in |
| 68 | + let%bind hash = Daemon.Client.ledger_hash client ~ledger_file in |
| 69 | + Deferred.Or_error.return |
| 70 | + ( if contain_log_output hash then |
| 71 | + Mina_automation_fixture.Intf.Failed "output contains log" |
| 72 | + else if not (String.is_prefix ~prefix:"j" hash) then |
| 73 | + Failed "invalid ledger hash prefix" |
| 74 | + else if Int.( <> ) (String.length hash) 52 then |
| 75 | + Failed |
| 76 | + (Printf.sprintf "invalid ledger hash length (%d)" (String.length hash)) |
| 77 | + else Passed ) |
| 78 | +end |
| 79 | + |
| 80 | +module LedgerCurrency = struct |
| 81 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 82 | + |
| 83 | + let test_case (test : t) = |
| 84 | + let daemon = Daemon.of_config test.config in |
| 85 | + let client = Daemon.client daemon in |
| 86 | + let ledger_file = test.config.dirs.conf ^/ "daemon.json" in |
| 87 | + let%bind accounts = |
| 88 | + Mina_automation_fixture.Daemon.generate_random_accounts daemon ledger_file |
96 | 89 | in |
97 | | - run test_case mina_path |
98 | | - |
99 | | - let test_advanced_print_signature_kind mina_path = |
100 | | - let test_case (config : Config.Config.t) = |
101 | | - let open Deferred.Or_error.Let_syntax in |
102 | | - let mina_cli = MinaCli.create config.mina_exe in |
103 | | - let%bind output = MinaCli.advanced_print_signature_kind mina_cli in |
104 | | - let expected = "testnet" in |
105 | | - assert_don't_contain_log_output output ; |
106 | | - if not (String.equal expected (String.strip output)) then |
107 | | - failwithf "invalid signature kind %s vs %s" expected output () ; |
108 | | - return () |
| 90 | + let total_currency = |
| 91 | + List.map accounts ~f:(fun account -> |
| 92 | + Currency.Balance.to_nanomina_int account.balance ) |
| 93 | + |> List.sum (module Int) ~f:Fn.id |
109 | 94 | in |
110 | | - run test_case mina_path |
111 | | - |
112 | | - let test_advanced_compile_time_constants mina_path = |
113 | | - let test_case (config : Config.Config.t) = |
114 | | - let open Deferred.Or_error.Let_syntax in |
115 | | - let mina_cli = MinaCli.create config.mina_exe in |
116 | | - let%bind config_content = MinaCli.test_ledger mina_cli ~n:10 in |
117 | | - let config_content = |
118 | | - Printf.sprintf "{ \"ledger\":{ \"accounts\":%s } }" config_content |
119 | | - in |
120 | | - let temp_file = Filename.temp_file "commandline" "ledger.json" in |
121 | | - Yojson.Safe.from_string config_content |> Yojson.Safe.to_file temp_file ; |
122 | | - let%bind output = |
123 | | - MinaCli.advanced_compile_time_constants mina_cli ~config_file:temp_file |
124 | | - in |
125 | | - assert_don't_contain_log_output output ; |
126 | | - return () |
| 95 | + let%bind output = Daemon.Client.ledger_currency client ~ledger_file in |
| 96 | + let actual = Scanf.sscanf output "MINA : %f" Fn.id in |
| 97 | + let total_currency_float = float_of_int total_currency /. 1000000000.0 in |
| 98 | + |
| 99 | + Deferred.Or_error.return |
| 100 | + @@ |
| 101 | + if contain_log_output output then |
| 102 | + Mina_automation_fixture.Intf.Failed "output contains log" |
| 103 | + else if not Float.(abs (total_currency_float - actual) < 0.001) then |
| 104 | + Failed |
| 105 | + (Printf.sprintf "invalid mina total count %f vs %f" total_currency_float |
| 106 | + actual ) |
| 107 | + else Passed |
| 108 | +end |
| 109 | + |
| 110 | +module AdvancedPrintSignatureKind = struct |
| 111 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 112 | + |
| 113 | + let test_case (test : t) = |
| 114 | + let daemon = Daemon.of_config test.config in |
| 115 | + let client = Daemon.client daemon in |
| 116 | + let%bind output = Daemon.Client.advanced_print_signature_kind client in |
| 117 | + let expected = "testnet" in |
| 118 | + |
| 119 | + Deferred.Or_error.return |
| 120 | + @@ |
| 121 | + if contain_log_output output then |
| 122 | + Mina_automation_fixture.Intf.Failed "output contains log" |
| 123 | + else if not (String.equal expected (String.strip output)) then |
| 124 | + Failed (Printf.sprintf "invalid signature kind %s vs %s" expected output) |
| 125 | + else Passed |
| 126 | +end |
| 127 | + |
| 128 | +module AdvancedCompileTimeConstants = struct |
| 129 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 130 | + |
| 131 | + let test_case (test : t) = |
| 132 | + let daemon = Daemon.of_config test.config in |
| 133 | + let client = Daemon.client daemon in |
| 134 | + let%bind config_content = Daemon.Client.test_ledger client ~n:10 in |
| 135 | + let config_content = |
| 136 | + Printf.sprintf "{ \"ledger\":{ \"accounts\":%s } }" config_content |
127 | 137 | in |
128 | | - run test_case mina_path |
129 | | - |
130 | | - let test_advanced_constraint_system_digests mina_path = |
131 | | - let test_case (config : Config.Config.t) = |
132 | | - let open Deferred.Or_error.Let_syntax in |
133 | | - let mina_cli = MinaCli.create config.mina_exe in |
134 | | - let%bind output = MinaCli.advanced_constraint_system_digests mina_cli in |
135 | | - assert_don't_contain_log_output output ; |
136 | | - return () |
| 138 | + let temp_file = Filename.temp_file "commandline" "ledger.json" in |
| 139 | + Yojson.Safe.from_string config_content |> Yojson.Safe.to_file temp_file ; |
| 140 | + let%bind output = |
| 141 | + Daemon.Client.advanced_compile_time_constants client |
| 142 | + ~config_file:temp_file |
137 | 143 | in |
138 | | - run test_case mina_path |
| 144 | + |
| 145 | + Deferred.Or_error.return |
| 146 | + @@ |
| 147 | + if contain_log_output output then |
| 148 | + Mina_automation_fixture.Intf.Failed "output contains log" |
| 149 | + else Passed |
139 | 150 | end |
140 | 151 |
|
141 | | -let mina_path = |
142 | | - let doc = "Path Path to mina daemon executable" in |
143 | | - Cmdliner.Arg.( |
144 | | - required |
145 | | - & opt (some string) None |
146 | | - & info [ "mina-path" ] ~doc ~docv:"MINA_PATH") |
| 152 | +module AdvancedConstraintSystemDigests = struct |
| 153 | + type t = Mina_automation_fixture.Daemon.before_bootstrap |
| 154 | + |
| 155 | + let test_case (test : t) = |
| 156 | + let daemon = Daemon.of_config test.config in |
| 157 | + let client = Daemon.client daemon in |
| 158 | + let%bind output = Daemon.Client.advanced_constraint_system_digests client in |
| 159 | + |
| 160 | + Deferred.Or_error.return |
| 161 | + @@ |
| 162 | + if contain_log_output output then |
| 163 | + Mina_automation_fixture.Intf.Failed "output contains log" |
| 164 | + else Passed |
| 165 | +end |
147 | 166 |
|
148 | 167 | let () = |
149 | 168 | let open Alcotest in |
150 | | - run_with_args "Test commadline." mina_path |
151 | | - [ ( "background" |
| 169 | + run "Test commadline." |
| 170 | + [ ( "new-background" |
152 | 171 | , [ test_case "The mina daemon works in background mode" `Quick |
153 | | - CliTests.test_background_daemon |
| 172 | + (Mina_automation_runner.Runner.run_blocking |
| 173 | + ( module Mina_automation_fixture.Daemon |
| 174 | + .Make_FixtureWithoutBootstrap |
| 175 | + (BackgroundMode) ) ) |
154 | 176 | ] ) |
155 | 177 | ; ( "restart" |
156 | 178 | , [ test_case "The mina daemon recovers from crash" `Quick |
157 | | - CliTests.test_daemon_recover |
| 179 | + (Mina_automation_runner.Runner.run_blocking |
| 180 | + ( module Mina_automation_fixture.Daemon |
| 181 | + .Make_FixtureWithoutBootstrap |
| 182 | + (DaemonRecover) ) ) |
158 | 183 | ] ) |
159 | 184 | ; ( "ledger-hash" |
160 | 185 | , [ test_case "The mina ledger hash evaluates correctly" `Quick |
161 | | - CliTests.test_ledger_hash |
| 186 | + (Mina_automation_runner.Runner.run_blocking |
| 187 | + ( module Mina_automation_fixture.Daemon |
| 188 | + .Make_FixtureWithoutBootstrap |
| 189 | + (LedgerHash) ) ) |
162 | 190 | ] ) |
163 | 191 | ; ( "ledger-currency" |
164 | 192 | , [ test_case "The mina ledger currency evaluates correctly" `Quick |
165 | | - CliTests.test_ledger_currency |
| 193 | + (Mina_automation_runner.Runner.run_blocking |
| 194 | + ( module Mina_automation_fixture.Daemon |
| 195 | + .Make_FixtureWithoutBootstrap |
| 196 | + (LedgerCurrency) ) ) |
166 | 197 | ] ) |
167 | 198 | ; ( "advanced-print-signature-kind" |
168 | 199 | , [ test_case "The mina cli prints correct signature kind" `Quick |
169 | | - CliTests.test_advanced_print_signature_kind |
| 200 | + (Mina_automation_runner.Runner.run_blocking |
| 201 | + ( module Mina_automation_fixture.Daemon |
| 202 | + .Make_FixtureWithoutBootstrap |
| 203 | + (AdvancedPrintSignatureKind) ) ) |
170 | 204 | ] ) |
171 | 205 | ; ( "advanced-compile-time-constants" |
172 | 206 | , [ test_case |
173 | 207 | "The mina cli does not print log when printing compile time \ |
174 | 208 | constants" |
175 | | - `Quick CliTests.test_advanced_compile_time_constants |
| 209 | + `Quick |
| 210 | + (Mina_automation_runner.Runner.run_blocking |
| 211 | + ( module Mina_automation_fixture.Daemon |
| 212 | + .Make_FixtureWithoutBootstrap |
| 213 | + (AdvancedCompileTimeConstants) ) ) |
176 | 214 | ] ) |
177 | 215 | ; ( "advanced-constraint-system-digests" |
178 | 216 | , [ test_case |
179 | 217 | "The mina cli does not print log when printing constrain system \ |
180 | 218 | digests" |
181 | | - `Quick CliTests.test_advanced_constraint_system_digests |
| 219 | + `Quick |
| 220 | + (Mina_automation_runner.Runner.run_blocking |
| 221 | + ( module Mina_automation_fixture.Daemon |
| 222 | + .Make_FixtureWithoutBootstrap |
| 223 | + (AdvancedConstraintSystemDigests) ) ) |
182 | 224 | ] ) |
183 | 225 | ] |
0 commit comments