diff --git a/tests_python/tests_013/test_mockup.py b/tests_python/tests_013/test_mockup.py index db7ae705972cf2ece4988619ebec59737bba2508..61f10925244d60f26687f5098f5608d4bdeac794 100644 --- a/tests_python/tests_013/test_mockup.py +++ b/tests_python/tests_013/test_mockup.py @@ -25,86 +25,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -@pytest.mark.client -def test_list_mockup_protocols(sandbox: Sandbox): - """Executes `tezos-client list mockup protocols` - The call must succeed and return a non empty list. - """ - try: - client = sandbox.create_client() - protocols = client.list_mockup_protocols().mockup_protocols - assert protocols - finally: - shutil.rmtree(client.base_dir) - - -@pytest.mark.client -def test_create_mockup_dir_exists_nonempty(sandbox: Sandbox): - """Executes `tezos-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is a non empty directory which is NOT a mockup - directory. The call must fail. - """ - with tempfile.TemporaryDirectory(prefix='tezos-client.') as base_dir: - # Make the directory not empty - with open(os.path.join(base_dir, "whatever"), "w") as handle: - handle.write("") - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - assert res == CreateMockupResult.DIR_NOT_EMPTY - - -@pytest.mark.client -def test_retrieve_addresses(mockup_client: Client): - """Retrieves known addresses of a fresh mockup. - The call must succeed. - """ - addresses = mockup_client.get_known_addresses().wallet - assert addresses == { - 'bootstrap1': 'tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx', - 'bootstrap2': 'tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN', - 'bootstrap3': 'tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU', - 'bootstrap4': 'tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv', - 'bootstrap5': 'tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv', - } - - -@pytest.mark.client -def test_create_mockup_already_initialized(mockup_client: Client): - """Executes `tezos-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is not fresh. - The call must fail. - """ - # mockup was created already by fixture, try to create it second time: - res = mockup_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - # it should fail: - assert res == CreateMockupResult.ALREADY_INITIALIZED - - -@pytest.mark.client -def test_transfer(mockup_client: Client): - """Executes `tezos-client --base-dir /tmp/mdir -M mockup - transfer 1 from bootstrap1 to bootstrap2` - in a valid mockup environment. - The call must succeed and the balances must be updated correctly. - """ - giver = "bootstrap1" - receiver = "bootstrap2" - transferred = 1.0 - - giver_balance_before = mockup_client.get_balance(giver) - receiver_balance_before = mockup_client.get_balance(receiver) - mockup_client.transfer(transferred, giver, receiver) - giver_balance_after = mockup_client.get_balance(giver) - receiver_balance_after = mockup_client.get_balance(receiver) - - assert giver_balance_after < giver_balance_before - transferred - assert receiver_balance_after == receiver_balance_before + transferred - - # It's impossible to guess values of chain_id, these ones have been # obtained by looking at the output of `compute chain id from seed` @pytest.mark.parametrize( diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index d8b03e378c103aa7501f09a130fd16f0ac84709b..1000e5fd48cffe250d4ac1cce90caf08ed155108 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -25,86 +25,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -@pytest.mark.client -def test_list_mockup_protocols(sandbox: Sandbox): - """Executes `tezos-client list mockup protocols` - The call must succeed and return a non empty list. - """ - try: - client = sandbox.create_client() - protocols = client.list_mockup_protocols().mockup_protocols - assert protocols - finally: - shutil.rmtree(client.base_dir) - - -@pytest.mark.client -def test_create_mockup_dir_exists_nonempty(sandbox: Sandbox): - """Executes `tezos-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is a non empty directory which is NOT a mockup - directory. The call must fail. - """ - with tempfile.TemporaryDirectory(prefix='tezos-client.') as base_dir: - # Make the directory not empty - with open(os.path.join(base_dir, "whatever"), "w") as handle: - handle.write("") - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - assert res == CreateMockupResult.DIR_NOT_EMPTY - - -@pytest.mark.client -def test_retrieve_addresses(mockup_client: Client): - """Retrieves known addresses of a fresh mockup. - The call must succeed. - """ - addresses = mockup_client.get_known_addresses().wallet - assert addresses == { - 'bootstrap1': 'tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx', - 'bootstrap2': 'tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN', - 'bootstrap3': 'tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU', - 'bootstrap4': 'tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv', - 'bootstrap5': 'tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv', - } - - -@pytest.mark.client -def test_create_mockup_already_initialized(mockup_client: Client): - """Executes `tezos-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is not fresh. - The call must fail. - """ - # mockup was created already by fixture, try to create it second time: - res = mockup_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - # it should fail: - assert res == CreateMockupResult.ALREADY_INITIALIZED - - -@pytest.mark.client -def test_transfer(mockup_client: Client): - """Executes `tezos-client --base-dir /tmp/mdir -M mockup - transfer 1 from bootstrap1 to bootstrap2` - in a valid mockup environment. - The call must succeed and the balances must be updated correctly. - """ - giver = "bootstrap1" - receiver = "bootstrap2" - transferred = 1.0 - - giver_balance_before = mockup_client.get_balance(giver) - receiver_balance_before = mockup_client.get_balance(receiver) - mockup_client.transfer(transferred, giver, receiver) - giver_balance_after = mockup_client.get_balance(giver) - receiver_balance_after = mockup_client.get_balance(receiver) - - assert giver_balance_after < giver_balance_before - transferred - assert receiver_balance_after == receiver_balance_before + transferred - - # It's impossible to guess values of chain_id, these ones have been # obtained by looking at the output of `compute chain id from seed` @pytest.mark.parametrize( diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index aee36e0be9627d5bd0e284fe41394dbc028a3658..0c4c4ff739a32ff6368527c72c1e721dbb836d8d 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -27,86 +27,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -@pytest.mark.client -def test_list_mockup_protocols(sandbox: Sandbox): - """Executes `tezos-client list mockup protocols` - The call must succeed and return a non empty list. - """ - try: - client = sandbox.create_client() - protocols = client.list_mockup_protocols().mockup_protocols - assert protocols - finally: - shutil.rmtree(client.base_dir) - - -@pytest.mark.client -def test_create_mockup_dir_exists_nonempty(sandbox: Sandbox): - """Executes `tezos-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is a non empty directory which is NOT a mockup - directory. The call must fail. - """ - with tempfile.TemporaryDirectory(prefix='tezos-client.') as base_dir: - # Make the directory not empty - with open(os.path.join(base_dir, "whatever"), "w") as handle: - handle.write("") - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - assert res == CreateMockupResult.DIR_NOT_EMPTY - - -@pytest.mark.client -def test_retrieve_addresses(mockup_client: Client): - """Retrieves known addresses of a fresh mockup. - The call must succeed. - """ - addresses = mockup_client.get_known_addresses().wallet - assert addresses == { - 'bootstrap1': 'tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx', - 'bootstrap2': 'tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN', - 'bootstrap3': 'tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU', - 'bootstrap4': 'tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv', - 'bootstrap5': 'tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv', - } - - -@pytest.mark.client -def test_create_mockup_already_initialized(mockup_client: Client): - """Executes `tezos-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is not fresh. - The call must fail. - """ - # mockup was created already by fixture, try to create it second time: - res = mockup_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - # it should fail: - assert res == CreateMockupResult.ALREADY_INITIALIZED - - -@pytest.mark.client -def test_transfer(mockup_client: Client): - """Executes `tezos-client --base-dir /tmp/mdir -M mockup - transfer 1 from bootstrap1 to bootstrap2` - in a valid mockup environment. - The call must succeed and the balances must be updated correctly. - """ - giver = "bootstrap1" - receiver = "bootstrap2" - transferred = 1.0 - - giver_balance_before = mockup_client.get_balance(giver) - receiver_balance_before = mockup_client.get_balance(receiver) - mockup_client.transfer(transferred, giver, receiver) - giver_balance_after = mockup_client.get_balance(giver) - receiver_balance_after = mockup_client.get_balance(receiver) - - assert giver_balance_after < giver_balance_before - transferred - assert receiver_balance_after == receiver_balance_before + transferred - - # It's impossible to guess values of chain_id, these ones have been # obtained by looking at the output of `compute chain id from seed` @pytest.mark.parametrize( diff --git a/tezt/lib_core/cli.ml b/tezt/lib_core/cli.ml index f84326c287c7d39709d58cac8c37bb491d4ae518..58ffb40fa92ea015dd2cbc648722bb5f113baf43 100644 --- a/tezt/lib_core/cli.ml +++ b/tezt/lib_core/cli.ml @@ -289,14 +289,14 @@ let init ?args () = Arg.String (fun pattern -> options.patterns_to_run <- - Base.rex pattern :: options.patterns_to_run), - " Only run tests matching PERL_REGEXP (see SELECTING \ - TESTS)." ); + Base.rex ~opts:[`Caseless] pattern :: options.patterns_to_run), + " Only run tests matching PERL_REGEXP (case \ + insensitive) (see SELECTING TESTS)." ); ( "-m", Arg.String (fun pattern -> options.patterns_to_run <- - Base.rex pattern :: options.patterns_to_run), + Base.rex ~opts:[`Caseless] pattern :: options.patterns_to_run), " Same as --match." ); ( "--title", Arg.String diff --git a/tezt/lib_core/test.ml b/tezt/lib_core/test.ml index ddebbc706e6a55bcda80296e2204996d1ac11248..1dbe065c7822aa7077d50972901c5cd393821eea 100644 --- a/tezt/lib_core/test.ml +++ b/tezt/lib_core/test.ml @@ -862,6 +862,9 @@ let run_with_scheduler scheduler = @ List.map (fun x -> "--title " ^ Log.quote_shell x) Cli.options.tests_to_run + @ List.map + (fun r -> "--match " ^ Log.quote_shell (show_rex r)) + Cli.options.patterns_to_run @ Cli.options.tags_to_run @ List.map (sf "/%s") Cli.options.tags_not_to_run)) ; if Cli.options.list = None then diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 0343dbb325c27f23875048ecfce3bde0a577f66c..e635c5706766b6768f4f8c646f16daff4f4c7d97 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -721,6 +721,27 @@ let show_address ~alias client = in return @@ Account.parse_client_output ~alias ~client_output +let spawn_list_known_addresses client = + spawn_command client ["list"; "known"; "addresses"] + +let list_known_addresses client = + let* client_output = + spawn_list_known_addresses client |> Process.check_and_read_stdout + in + let addresses = + client_output |> String.trim |> String.split_on_char '\n' + |> List.map @@ fun line -> + match line =~** rex "(.*): ([^ ]*)" with + | Some (alias, pkh) -> (alias, pkh) + | None -> + Test.fail + "Cannot parse line %S from list of known addresses from \ + client_output: %s" + line + client_output + in + return addresses + let gen_and_show_keys ?alias client = let* alias = gen_keys ?alias client in show_address ~alias client diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index 8d90dc59d9c335def636df798bb4f736be9f4e22..ec41e53d1590d53e5e60625df3bee6ef74ed1ec8 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -556,6 +556,14 @@ val show_address : alias:string -> t -> Account.key Lwt.t (which also implies that there is no output key to parse). *) val spawn_show_address : alias:string -> t -> Process.t +(** Run [tezos-client list known addresses] and parse the output into + a association list from aliases to public key hashes. *) +val list_known_addresses : t -> (string * string) list Lwt.t + +(** Same as [list_known_addresses] but do not wait for the process to + exit. *) +val spawn_list_known_addresses : t -> Process.t + (** Run [tezos-client gen keys] and return the key alias. The default value for [alias] is a fresh alias of the form [tezt_]. *) diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index 83105c949a555f9b051df2afebaf489061b9224f..32d7ee63a216a5c31a6b1cad91c5ef5cfd04082d 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -63,7 +63,7 @@ let test_rpc_header_shell = let transfer_data = (Constant.bootstrap1.alias, Tez.one, Constant.bootstrap2.alias) -let test_balances_after_transfer giver amount receiver = +let check_balances_after_transfer giver amount receiver = let giver_balance_before, giver_balance_after = giver in let receiver_balance_before, receiver_balance_after = receiver in if not Tez.(giver_balance_after < giver_balance_before - amount) then @@ -109,7 +109,7 @@ let test_transfer = let* receiver_balance_after = Client.get_balance_for ~account:receiver client in - test_balances_after_transfer + check_balances_after_transfer (giver_balance_before, giver_balance_after) amount (receiver_balance_before, receiver_balance_after) ; @@ -455,7 +455,7 @@ let test_migration_transfer ?migration_spec () = let* receiver_balance_after = Client.get_balance_for ~account:receiver client in - test_balances_after_transfer + check_balances_after_transfer (giver_balance_before, giver_balance_after) amount (receiver_balance_before, receiver_balance_after) ; @@ -609,6 +609,74 @@ let test_storage_from_file = in unit) +(* Executes `tezos-client list mockup protocols`. The call must + succeed and return a non empty list. *) +let test_list_mockup_protocols () = + Test.register + ~__FILE__ + ~title:"(Mockup) List mockup protocols." + ~tags:["mockup"; "client"; "protocols"] + @@ fun () -> + let client = Client.create_with_mode Client.Mockup in + let* protocols = Client.list_protocols `Mockup client in + if protocols = [] then Test.fail "List of mockup protocols must be non-empty" ; + unit + +(* Executes [tezos-client --base-dir /tmp/mdir create mockup] when + [/tmp/mdir] is a non empty directory which is NOT a mockup + directory. The call must fail. *) +let test_create_mockup_dir_exists_nonempty = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Create mockup in existing base dir" + ~tags:["mockup"; "client"; "base_dir"] + @@ fun protocol -> + let base_dir = Temp.dir "mockup_dir" in + Base.write_file ~contents:"" (base_dir ^ "/" ^ "whatever") ; + let client = Client.create_with_mode ~base_dir Client.Mockup in + let* () = + Client.spawn_create_mockup client ~protocol + |> Process.check_error + ~msg:(rex "is not empty, please specify a fresh base directory") + in + unit + +let test_retrieve_addresses = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Retrieve addresses" + ~tags:["mockup"; "client"; "wallet"] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let* addresses = Client.list_known_addresses client in + let expected_addresses = + Account.Bootstrap.keys |> Array.to_list |> List.rev + |> List.map @@ fun Account.{alias; public_key_hash; _} -> + (alias, public_key_hash) + in + Check.( + (addresses = expected_addresses) + ~__LOC__ + (list (tuple2 string string)) + ~error_msg:"Expected addresses %R, got %L") ; + unit + +(* Executes [tezos-client --base-dir /tmp/mdir create mockup] when + [/tmp/mdir] is not fresh. The call must fail. *) +let test_create_mockup_already_initialized = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Create mockup when already initialized." + ~tags:["mockup"; "client"; "base_dir"] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let* () = + Client.spawn_create_mockup client ~protocol + |> Process.check_error + ~msg:(rex "is already initialized as a mockup directory") + in + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -620,7 +688,10 @@ let register ~protocols = test_rpc_header_shell protocols ; test_origination_from_unrevealed_fees protocols ; test_multiple_transfers protocols ; - test_storage_from_file protocols + test_storage_from_file protocols ; + test_create_mockup_dir_exists_nonempty protocols ; + test_retrieve_addresses protocols ; + test_create_mockup_already_initialized protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; @@ -633,4 +704,6 @@ let register_global_constants ~protocols = let register_constant_migration ~migrate_from ~migrate_to = test_migration_constants ~migrate_from ~migrate_to -let register_protocol_independent () = test_migration_transfer () +let register_protocol_independent () = + test_migration_transfer () ; + test_list_mockup_protocols ()