From ab69d6775e4a9d8f0fb7ffcdfc4a660f0fa1aba9 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Tue, 27 Sep 2022 08:27:23 +0200 Subject: [PATCH 1/9] Tezt: use [//] in mockup.ml --- tezt/tests/mockup.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index c469fcb86ff8..2af288f83080 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -632,7 +632,7 @@ let test_create_mockup_dir_exists_nonempty = ~tags:["mockup"; "client"; "base_dir"] @@ fun protocol -> let base_dir = Temp.dir "mockup_dir" in - Base.write_file ~contents:"" (base_dir ^ "/" ^ "whatever") ; + write_file ~contents:"" (base_dir // "whatever") ; let client = Client.create_with_mode ~base_dir Client.Mockup in let* () = Client.spawn_create_mockup client ~protocol -- GitLab From 62546b73ac58de3e099e2ebf7b40e440841e6be8 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 18:51:17 +0200 Subject: [PATCH 2/9] Tezt: add [Check.directory_exists] --- tezt/lib_core/check.ml | 4 ++++ tezt/lib_core/check.mli | 3 +++ 2 files changed, 7 insertions(+) diff --git a/tezt/lib_core/check.ml b/tezt/lib_core/check.ml index 073865fcfc0f..bd5e7d2ff80a 100644 --- a/tezt/lib_core/check.ml +++ b/tezt/lib_core/check.ml @@ -381,6 +381,10 @@ let file_exists ?__LOC__ path = if not (Sys.file_exists path) then Test.fail ?__LOC__ "expected that file %s exists" path +let directory_exists ?__LOC__ path = + if not (Sys.is_directory path) then + Test.fail ?__LOC__ "expected that directory %s exists" path + let is_true ?__LOC__ b ~error_msg = if not b then Test.fail ?__LOC__ "%s" error_msg diff --git a/tezt/lib_core/check.mli b/tezt/lib_core/check.mli index 9a9abc81d580..557bc939e8a6 100644 --- a/tezt/lib_core/check.mli +++ b/tezt/lib_core/check.mli @@ -279,6 +279,9 @@ val raises : (** Check that a file with the given name exists. *) val file_exists : ?__LOC__:string -> string -> unit +(** Check that a directory with the given name exists. *) +val directory_exists : ?__LOC__:string -> string -> unit + (** {2 Predicates on booleans} *) (** Check that a boolean is true. -- GitLab From 573c94c23fec7979784d795c172e41885ad575dc Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 18:52:28 +0200 Subject: [PATCH 3/9] Tezt: translate [transfer_bad_base_dir] of [test_mockup.py] --- tests_python/tests_014/test_mockup.py | 40 ------------------------ tests_python/tests_015/test_mockup.py | 39 ----------------------- tests_python/tests_alpha/test_mockup.py | 39 ----------------------- tezt/tests/mockup.ml | 41 ++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 119 deletions(-) diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index 0de1f26906d6..54b0e555403f 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -9,10 +9,8 @@ """ import json import os -import re import shutil import tempfile -from pathlib import Path from typing import Any, List, Optional, Tuple import pytest from launchers.sandbox import Sandbox @@ -57,44 +55,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_transfer_bad_base_dir(sandbox: Sandbox): - """Executes `octez-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir looks like a dubious base directory. - Checks that a warning is printed. - """ - try: - unmanaged_client = sandbox.create_client() - res = unmanaged_client.create_mockup( - protocol=protocol.HASH - ).create_mockup_result - assert res == CreateMockupResult.OK - base_dir = unmanaged_client.base_dir - mockup_dir = os.path.join(base_dir, "mockup") - - # A valid mockup has a directory named "mockup", in its directory: - assert os.path.isdir(mockup_dir) - mock_client = sandbox.create_client(base_dir=base_dir, mode="mockup") - # Delete this directory: - shutil.rmtree(mockup_dir) - # And put a file instead: - Path(os.path.join(mockup_dir)).touch() - - # Now execute a command - cmd = ["transfer", "1", "from", "bootstrap1", "to", "bootstrap2"] - (_, err_output, _) = mock_client.run_generic(cmd, check=False) - # See - # https://gitlab.com/tezos/tezos/-/merge_requests/1760#note_329071488 - # for the content being matched - searched = "Some commands .* might not work correctly." - # Witness that warning is printed: - assert re.search( - searched, err_output - ), f"'{searched}' not matched in error output" - finally: - shutil.rmtree(base_dir) - - @pytest.mark.client def test_config_show_mockup(mockup_client: Client): """Executes `octez-client --mode mockup config show` in diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index 85aec03afa31..0add220e67cc 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -12,7 +12,6 @@ import os import re import shutil import tempfile -from pathlib import Path from typing import Any, List, Optional, Tuple, Iterator, Dict from pprint import pformat import pytest @@ -59,44 +58,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_transfer_bad_base_dir(sandbox: Sandbox): - """Executes `octez-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir looks like a dubious base directory. - Checks that a warning is printed. - """ - try: - unmanaged_client = sandbox.create_client() - res = unmanaged_client.create_mockup( - protocol=protocol.HASH - ).create_mockup_result - assert res == CreateMockupResult.OK - base_dir = unmanaged_client.base_dir - mockup_dir = os.path.join(base_dir, "mockup") - - # A valid mockup has a directory named "mockup", in its directory: - assert os.path.isdir(mockup_dir) - mock_client = sandbox.create_client(base_dir=base_dir, mode="mockup") - # Delete this directory: - shutil.rmtree(mockup_dir) - # And put a file instead: - Path(os.path.join(mockup_dir)).touch() - - # Now execute a command - cmd = ["transfer", "1", "from", "bootstrap1", "to", "bootstrap2"] - (_, err_output, _) = mock_client.run_generic(cmd, check=False) - # See - # https://gitlab.com/tezos/tezos/-/merge_requests/1760#note_329071488 - # for the content being matched - searched = "Some commands .* might not work correctly." - # Witness that warning is printed: - assert re.search( - searched, err_output - ), f"'{searched}' not matched in error output" - finally: - shutil.rmtree(base_dir) - - @pytest.mark.client def test_config_show_mockup(mockup_client: Client): """Executes `octez-client --mode mockup config show` in diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index c8de37b60abd..7fbfe9177565 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -12,7 +12,6 @@ import os import re import shutil import tempfile -from pathlib import Path from typing import Any, List, Optional, Tuple, Iterator, Dict from pprint import pformat import pytest @@ -59,44 +58,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_transfer_bad_base_dir(sandbox: Sandbox): - """Executes `octez-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir looks like a dubious base directory. - Checks that a warning is printed. - """ - try: - unmanaged_client = sandbox.create_client() - res = unmanaged_client.create_mockup( - protocol=protocol.HASH - ).create_mockup_result - assert res == CreateMockupResult.OK - base_dir = unmanaged_client.base_dir - mockup_dir = os.path.join(base_dir, "mockup") - - # A valid mockup has a directory named "mockup", in its directory: - assert os.path.isdir(mockup_dir) - mock_client = sandbox.create_client(base_dir=base_dir, mode="mockup") - # Delete this directory: - shutil.rmtree(mockup_dir) - # And put a file instead: - Path(os.path.join(mockup_dir)).touch() - - # Now execute a command - cmd = ["transfer", "1", "from", "bootstrap1", "to", "bootstrap2"] - (_, err_output, _) = mock_client.run_generic(cmd, check=False) - # See - # https://gitlab.com/tezos/tezos/-/merge_requests/1760#note_329071488 - # for the content being matched - searched = "Some commands .* might not work correctly." - # Witness that warning is printed: - assert re.search( - searched, err_output - ), f"'{searched}' not matched in error output" - finally: - shutil.rmtree(base_dir) - - @pytest.mark.client def test_config_show_mockup(mockup_client: Client): """Executes `octez-client --mode mockup config show` in diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index 2af288f83080..e826eeef686e 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -771,6 +771,44 @@ let test_create_mockup_custom_bootstrap_accounts = ~error_msg:"Expected names %R, got %L") ; unit +let rmdir dir = Process.spawn "rm" ["-rf"; dir] |> Process.check + +(* Executes [tezos-client --base-dir /tmp/mdir create mockup] when + [/tmp/mdir] looks like a dubious base directory. Checks that a warning + is printed. *) +let test_transfer_bad_base_dir = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Transfer bad base dir." + ~tags:["mockup"; "client"; "initialization"] + @@ fun protocol -> + Log.info "First create mockup with an empty base dir" ; + let base_dir = Temp.dir "mockup-dir" in + Sys.rmdir base_dir ; + let client = Client.create_with_mode ~base_dir Client.Mockup in + let* () = Client.create_mockup ~protocol client in + let base_dir = Client.base_dir client in + let mockup_dir = base_dir // "mockup" in + Log.info "A valid mockup has a directory named [mockup], in its directory" ; + Check.directory_exists ~__LOC__ mockup_dir ; + + Log.info "Delete this directory:" ; + let* () = rmdir mockup_dir in + Log.info "And put a file instead:" ; + write_file mockup_dir ~contents:"" ; + + Log.info "Now execute a command" ; + let* () = + Client.spawn_transfer + ~amount:Tez.one + ~giver:"bootstrap1" + ~receiver:"bootstrap2" + client + |> Process.check_error + ~msg:(rex "Some commands .* might not work correctly.") + in + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -787,7 +825,8 @@ let register ~protocols = test_retrieve_addresses protocols ; test_create_mockup_already_initialized protocols ; test_create_mockup_custom_constants protocols ; - test_create_mockup_custom_bootstrap_accounts protocols + test_create_mockup_custom_bootstrap_accounts protocols ; + test_transfer_bad_base_dir protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; -- GitLab From 5d419d31c5ac69fd7e28717b96579f7ebaa3ff8e Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 19:22:39 +0200 Subject: [PATCH 4/9] Tezt: add [Client.config_show] --- tezt/lib_tezos/client.ml | 8 ++++++++ tezt/lib_tezos/client.mli | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 0deca8832705..c2e477256e37 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -2239,3 +2239,11 @@ let convert_script ~script ~src_format ~dst_format client = |> Process.check_and_read_stdout let bootstrapped client = spawn_command client ["bootstrapped"] |> Process.check + +let spawn_config_show ?protocol client = + spawn_command client + @@ Cli_arg.optional_arg "protocol" Protocol.hash protocol + @ ["config"; "show"] + +let config_show ?protocol client = + spawn_config_show ?protocol client |> Process.check diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index f314774d2cb5..150628c1e6b0 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -1668,3 +1668,9 @@ val convert_script : (** Run [octez-client bootstrapped]. *) val bootstrapped : t -> unit Lwt.t + +(** Run [tezos-client config show]. *) +val config_show : ?protocol:Protocol.t -> t -> unit Lwt.t + +(** Same as [config_show], but do not wait for the process to exit. *) +val spawn_config_show : ?protocol:Protocol.t -> t -> Process.t -- GitLab From d98ed89141c0cbe551038267ac8003b717952cab Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 19:23:17 +0200 Subject: [PATCH 5/9] Tezt: translate [test_config_show_mockup{,_fail}] of [test_mockup.py] --- tests_python/tests_014/test_mockup.py | 26 --------------------- tests_python/tests_015/test_mockup.py | 26 --------------------- tests_python/tests_alpha/test_mockup.py | 26 --------------------- tezt/tests/mockup.ml | 30 ++++++++++++++++++++++++- 4 files changed, 29 insertions(+), 79 deletions(-) diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index 54b0e555403f..c04f9793c1d8 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -55,32 +55,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_show_mockup(mockup_client: Client): - """Executes `octez-client --mode mockup config show` in - a state where it should succeed. - """ - mockup_client.run_generic(["--protocol", protocol.HASH, "config", "show"]) - - -@pytest.mark.client -def test_config_show_mockup_fail(mockup_client: Client): - """Executes `octez-client --mode mockup config show` when - base dir is NOT a mockup. It should fail as this is dangerous - (the default base directory could contain sensitive data, - such as private keys) - """ - shutil.rmtree(mockup_client.base_dir) # See test_config_init_mockup_fail - # for a variant of how to make the base dir invalid for the mockup mode - _, _, return_code = mockup_client.run_generic( - ["config", "show"], check=False - ) - - # recreate directory: the cleanup later on expects its existence - os.mkdir(mockup_client.base_dir) - assert return_code != 0 - - @pytest.mark.client def test_config_init_mockup(mockup_client: Client): """Executes `octez-client config init mockup` in diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index 0add220e67cc..f77e1d01ba36 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -58,32 +58,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_show_mockup(mockup_client: Client): - """Executes `octez-client --mode mockup config show` in - a state where it should succeed. - """ - mockup_client.run_generic(["--protocol", protocol.HASH, "config", "show"]) - - -@pytest.mark.client -def test_config_show_mockup_fail(mockup_client: Client): - """Executes `octez-client --mode mockup config show` when - base dir is NOT a mockup. It should fail as this is dangerous - (the default base directory could contain sensitive data, - such as private keys) - """ - shutil.rmtree(mockup_client.base_dir) # See test_config_init_mockup_fail - # for a variant of how to make the base dir invalid for the mockup mode - _, _, return_code = mockup_client.run_generic( - ["config", "show"], check=False - ) - - # recreate directory: the cleanup later on expects its existence - os.mkdir(mockup_client.base_dir) - assert return_code != 0 - - @pytest.mark.client def test_config_init_mockup(mockup_client: Client): """Executes `octez-client config init mockup` in diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index 7fbfe9177565..dab61a257acc 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -58,32 +58,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_show_mockup(mockup_client: Client): - """Executes `octez-client --mode mockup config show` in - a state where it should succeed. - """ - mockup_client.run_generic(["--protocol", protocol.HASH, "config", "show"]) - - -@pytest.mark.client -def test_config_show_mockup_fail(mockup_client: Client): - """Executes `octez-client --mode mockup config show` when - base dir is NOT a mockup. It should fail as this is dangerous - (the default base directory could contain sensitive data, - such as private keys) - """ - shutil.rmtree(mockup_client.base_dir) # See test_config_init_mockup_fail - # for a variant of how to make the base dir invalid for the mockup mode - _, _, return_code = mockup_client.run_generic( - ["config", "show"], check=False - ) - - # recreate directory: the cleanup later on expects its existence - os.mkdir(mockup_client.base_dir) - assert return_code != 0 - - @pytest.mark.client def test_config_init_mockup(mockup_client: Client): """Executes `octez-client config init mockup` in diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index e826eeef686e..d534fe38a959 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -809,6 +809,32 @@ let test_transfer_bad_base_dir = in unit +(* Executes [tezos-client --mode mockup config show] in a state where + it should succeed. *) +let test_config_show_mockup = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Show config." + ~tags:["mockup"; "client"; "config"] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let* _ = Client.config_show ~protocol client in + unit + +(* Executes [tezos-client --mode mockup config show] when base dir is + NOT a mockup. It should fail as this is dangerous (the default base + directory could contain sensitive data, such as private keys) *) +let test_config_show_mockup_fail = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Show config failure." + ~tags:["mockup"; "client"; "config"] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let* () = rmdir (Client.base_dir client) in + let* _ = Client.spawn_config_show ~protocol client |> Process.check_error in + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -826,7 +852,9 @@ let register ~protocols = test_create_mockup_already_initialized protocols ; test_create_mockup_custom_constants protocols ; test_create_mockup_custom_bootstrap_accounts protocols ; - test_transfer_bad_base_dir protocols + test_transfer_bad_base_dir protocols ; + test_config_show_mockup protocols ; + test_config_show_mockup_fail protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; -- GitLab From d506abf8485cd5e9652799321e976df0c43b79fb Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 19:28:47 +0200 Subject: [PATCH 6/9] Tezt: add [Client.config_init] --- tezt/lib_tezos/client.ml | 11 +++++++++++ tezt/lib_tezos/client.mli | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index c2e477256e37..2b4567c19262 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -2247,3 +2247,14 @@ let spawn_config_show ?protocol client = let config_show ?protocol client = spawn_config_show ?protocol client |> Process.check + +let spawn_config_init ?protocol ?bootstrap_accounts ?protocol_constants client = + spawn_command client + @@ Cli_arg.optional_arg "protocol" Protocol.hash protocol + @ ["config"; "init"] + @ Cli_arg.optional_arg "bootstrap-accounts" Fun.id bootstrap_accounts + @ Cli_arg.optional_arg "protocol-constants" Fun.id protocol_constants + +let config_init ?protocol ?bootstrap_accounts ?protocol_constants client = + spawn_config_init ?protocol ?bootstrap_accounts ?protocol_constants client + |> Process.check diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index 150628c1e6b0..e47c30f9d1b0 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -1674,3 +1674,19 @@ val config_show : ?protocol:Protocol.t -> t -> unit Lwt.t (** Same as [config_show], but do not wait for the process to exit. *) val spawn_config_show : ?protocol:Protocol.t -> t -> Process.t + +(** Run [tezos-client config show]. *) +val config_init : + ?protocol:Protocol.t -> + ?bootstrap_accounts:string -> + ?protocol_constants:string -> + t -> + unit Lwt.t + +(** Same as [config_init], but do not wait for the process to exit. *) +val spawn_config_init : + ?protocol:Protocol.t -> + ?bootstrap_accounts:string -> + ?protocol_constants:string -> + t -> + Process.t -- GitLab From a8d1b3ccb22713c3e0695d1748c1a4353f5617bc Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 19:40:24 +0200 Subject: [PATCH 7/9] Tezt: translate [config_init_mockup] of [test_mockup.py] --- tests_python/tests_014/test_mockup.py | 35 ---------------- tests_python/tests_015/test_mockup.py | 35 ---------------- tests_python/tests_alpha/test_mockup.py | 35 ---------------- tezt/tests/mockup.ml | 54 ++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 106 deletions(-) diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index c04f9793c1d8..cc6e41cd984a 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -55,41 +55,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_init_mockup(mockup_client: Client): - """Executes `octez-client config init mockup` in - a state where it should succeed. - """ - # We cannot use NamedTemporaryFile because `config init mockup` - # does not overwrite files. Because NamedTemporaryFile creates the file - # it would make the test fail. - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - # 1/ call `config init mockup` - mockup_client.run( - [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - ) - - # 2/ Try loading the files, to check they are valid json - with open(ba_json_file) as handle: - json.load(handle) - with open(pc_json_file) as handle: - json.load(handle) - - # Cleanup - os.remove(ba_json_file) - os.remove(pc_json_file) - - @pytest.mark.client def test_config_init_mockup_fail(mockup_client: Client): """Executes `octez-client config init mockup` when diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index f77e1d01ba36..9dd04932186d 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -58,41 +58,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_init_mockup(mockup_client: Client): - """Executes `octez-client config init mockup` in - a state where it should succeed. - """ - # We cannot use NamedTemporaryFile because `config init mockup` - # does not overwrite files. Because NamedTemporaryFile creates the file - # it would make the test fail. - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - # 1/ call `config init mockup` - mockup_client.run( - [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - ) - - # 2/ Try loading the files, to check they are valid json - with open(ba_json_file) as handle: - json.load(handle) - with open(pc_json_file) as handle: - json.load(handle) - - # Cleanup - os.remove(ba_json_file) - os.remove(pc_json_file) - - @pytest.mark.client def test_config_init_mockup_fail(mockup_client: Client): """Executes `octez-client config init mockup` when diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index dab61a257acc..72ea08c906ce 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -58,41 +58,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_init_mockup(mockup_client: Client): - """Executes `octez-client config init mockup` in - a state where it should succeed. - """ - # We cannot use NamedTemporaryFile because `config init mockup` - # does not overwrite files. Because NamedTemporaryFile creates the file - # it would make the test fail. - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - # 1/ call `config init mockup` - mockup_client.run( - [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - ) - - # 2/ Try loading the files, to check they are valid json - with open(ba_json_file) as handle: - json.load(handle) - with open(pc_json_file) as handle: - json.load(handle) - - # Cleanup - os.remove(ba_json_file) - os.remove(pc_json_file) - - @pytest.mark.client def test_config_init_mockup_fail(mockup_client: Client): """Executes `octez-client config init mockup` when diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index d534fe38a959..6fd18d3a9def 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -835,6 +835,57 @@ let test_config_show_mockup_fail = let* _ = Client.spawn_config_show ~protocol client |> Process.check_error in unit +(* @pytest.mark.client + def test_config_init_mockup(mockup_client: Client): + """Executes `tezos-client config init mockup` in + a state where it should succeed. + """ + # We cannot use NamedTemporaryFile because `config init mockup` + # does not overwrite files. Because NamedTemporaryFile creates the file + # it would make the test fail. + ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') + pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') + # 1/ call `config init mockup` + mockup_client.run( + [ + "--protocol", + protocol.HASH, + "config", + "init", + f"--{_BA_FLAG}", + ba_json_file, + f"--{_PC_FLAG}", + pc_json_file, + ] + ) + + # 2/ Try loading the files, to check they are valid json + with open(ba_json_file) as handle: + json.load(handle) + with open(pc_json_file) as handle: + json.load(handle) + + # Cleanup + os.remove(ba_json_file) + os.remove(pc_json_file) *) +(* Executes [tezos-client config init mockup] in a state where it + should succeed *) +let test_config_init_mockup = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Mockup config initialization." + ~tags:["mockup"; "client"; "config"; "initialization"] + @@ fun protocol -> + let protocol_constants = Temp.file "protocol-constants.json" in + let bootstrap_accounts = Temp.file "bootstrap-accounts.json" in + let* client = Client.init_mockup ~protocol () in + let* () = + Client.config_init ~protocol ~bootstrap_accounts ~protocol_constants client + in + let (_ : JSON.t) = JSON.parse_file protocol_constants in + let (_ : JSON.t) = JSON.parse_file bootstrap_accounts in + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -854,7 +905,8 @@ let register ~protocols = test_create_mockup_custom_bootstrap_accounts protocols ; test_transfer_bad_base_dir protocols ; test_config_show_mockup protocols ; - test_config_show_mockup_fail protocols + test_config_show_mockup_fail protocols ; + test_config_init_mockup protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; -- GitLab From d2b1201764ad3d27c100ab567fb9807c132a1fae Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 19:48:16 +0200 Subject: [PATCH 8/9] Tezt: add [Check.{file,directory}_not_exists] --- tezt/lib_core/check.ml | 10 +++++++++- tezt/lib_core/check.mli | 14 +++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tezt/lib_core/check.ml b/tezt/lib_core/check.ml index bd5e7d2ff80a..94c0bc9ae24a 100644 --- a/tezt/lib_core/check.ml +++ b/tezt/lib_core/check.ml @@ -381,10 +381,18 @@ let file_exists ?__LOC__ path = if not (Sys.file_exists path) then Test.fail ?__LOC__ "expected that file %s exists" path +let file_not_exists ?__LOC__ path = + if Sys.file_exists path then + Test.fail ?__LOC__ "expected that file %s does not exists" path + let directory_exists ?__LOC__ path = - if not (Sys.is_directory path) then + if not (Sys.file_exists path && Sys.is_directory path) then Test.fail ?__LOC__ "expected that directory %s exists" path +let directory_not_exists ?__LOC__ path = + if Sys.file_exists path && Sys.is_directory path then + Test.fail ?__LOC__ "expected that directory %s does not exists" path + let is_true ?__LOC__ b ~error_msg = if not b then Test.fail ?__LOC__ "%s" error_msg diff --git a/tezt/lib_core/check.mli b/tezt/lib_core/check.mli index 557bc939e8a6..23d9d712bca8 100644 --- a/tezt/lib_core/check.mli +++ b/tezt/lib_core/check.mli @@ -279,9 +279,21 @@ val raises : (** Check that a file with the given name exists. *) val file_exists : ?__LOC__:string -> string -> unit -(** Check that a directory with the given name exists. *) +(** Check that a file with the given name does not exist. *) +val file_not_exists : ?__LOC__:string -> string -> unit + +(** Check that a directory with the given name exists. + + This [directory_exists path] succeeds if there is a file at + [path] and it is a directory. *) val directory_exists : ?__LOC__:string -> string -> unit +(** Check that a directory with the given name does not exist. + + This [directory_not_exists path] succeeds either if there is a + non-directory file at [path] or if there is no file at [path]. *) +val directory_not_exists : ?__LOC__:string -> string -> unit + (** {2 Predicates on booleans} *) (** Check that a boolean is true. -- GitLab From 5117a3029cdbaf2ace7fbf9d687c1af9f737fc10 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Mon, 26 Sep 2022 19:49:37 +0200 Subject: [PATCH 9/9] Tezt: translate [config_init_mockup_fail] of [test_mockup.py] --- tests_python/tests_014/test_mockup.py | 36 -------------- tests_python/tests_015/test_mockup.py | 36 -------------- tests_python/tests_alpha/test_mockup.py | 36 -------------- tezt/tests/mockup.ml | 62 +++++++++++-------------- 4 files changed, 28 insertions(+), 142 deletions(-) diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index cc6e41cd984a..19c74ace3920 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -9,7 +9,6 @@ """ import json import os -import shutil import tempfile from typing import Any, List, Optional, Tuple import pytest @@ -55,41 +54,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_init_mockup_fail(mockup_client: Client): - """Executes `octez-client config init mockup` when - base dir is NOT a mockup. It should fail as this is dangerous - (the default base directory could contain sensitive data, - such as private keys) - """ - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - cmd = [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - - # A valid mockup has a directory named "mockup" in its base_dir: - mockup_dir = os.path.join(mockup_client.base_dir, "mockup") - assert os.path.isdir(mockup_dir) - # Delete this directory, so that the base_dir is not a valid mockup - # base dir anymore: - shutil.rmtree(mockup_dir) # See test_config_show_mockup_fail above - # for a variant of how to make the base_dir invalid for the mockup mode - - _, _, return_code = mockup_client.run_generic(cmd, check=False) - assert return_code != 0 - # Check the test doesn't leak directories: - assert not os.path.exists(ba_json_file) - assert not os.path.exists(pc_json_file) - - def _try_json_loads(flag: str, string: str) -> Any: """Converts the given string to a json object""" try: diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index 9dd04932186d..453686ad3402 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -10,7 +10,6 @@ import json import os import re -import shutil import tempfile from typing import Any, List, Optional, Tuple, Iterator, Dict from pprint import pformat @@ -58,41 +57,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_init_mockup_fail(mockup_client: Client): - """Executes `octez-client config init mockup` when - base dir is NOT a mockup. It should fail as this is dangerous - (the default base directory could contain sensitive data, - such as private keys) - """ - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - cmd = [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - - # A valid mockup has a directory named "mockup" in its base_dir: - mockup_dir = os.path.join(mockup_client.base_dir, "mockup") - assert os.path.isdir(mockup_dir) - # Delete this directory, so that the base_dir is not a valid mockup - # base dir anymore: - shutil.rmtree(mockup_dir) # See test_config_show_mockup_fail above - # for a variant of how to make the base_dir invalid for the mockup mode - - _, _, return_code = mockup_client.run_generic(cmd, check=False) - assert return_code != 0 - # Check the test doesn't leak directories: - assert not os.path.exists(ba_json_file) - assert not os.path.exists(pc_json_file) - - def _try_json_loads(flag: str, string: str) -> Any: """Converts the given string to a json object""" try: diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index 72ea08c906ce..9aeeeefc483b 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -10,7 +10,6 @@ import json import os import re -import shutil import tempfile from typing import Any, List, Optional, Tuple, Iterator, Dict from pprint import pformat @@ -58,41 +57,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_config_init_mockup_fail(mockup_client: Client): - """Executes `octez-client config init mockup` when - base dir is NOT a mockup. It should fail as this is dangerous - (the default base directory could contain sensitive data, - such as private keys) - """ - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - cmd = [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - - # A valid mockup has a directory named "mockup" in its base_dir: - mockup_dir = os.path.join(mockup_client.base_dir, "mockup") - assert os.path.isdir(mockup_dir) - # Delete this directory, so that the base_dir is not a valid mockup - # base dir anymore: - shutil.rmtree(mockup_dir) # See test_config_show_mockup_fail above - # for a variant of how to make the base_dir invalid for the mockup mode - - _, _, return_code = mockup_client.run_generic(cmd, check=False) - assert return_code != 0 - # Check the test doesn't leak directories: - assert not os.path.exists(ba_json_file) - assert not os.path.exists(pc_json_file) - - def _try_json_loads(flag: str, string: str) -> Any: """Converts the given string to a json object""" try: diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index 6fd18d3a9def..64e4fd8180b1 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -835,39 +835,6 @@ let test_config_show_mockup_fail = let* _ = Client.spawn_config_show ~protocol client |> Process.check_error in unit -(* @pytest.mark.client - def test_config_init_mockup(mockup_client: Client): - """Executes `tezos-client config init mockup` in - a state where it should succeed. - """ - # We cannot use NamedTemporaryFile because `config init mockup` - # does not overwrite files. Because NamedTemporaryFile creates the file - # it would make the test fail. - ba_json_file = tempfile.mktemp(prefix='tezos-bootstrap-accounts') - pc_json_file = tempfile.mktemp(prefix='tezos-proto-consts') - # 1/ call `config init mockup` - mockup_client.run( - [ - "--protocol", - protocol.HASH, - "config", - "init", - f"--{_BA_FLAG}", - ba_json_file, - f"--{_PC_FLAG}", - pc_json_file, - ] - ) - - # 2/ Try loading the files, to check they are valid json - with open(ba_json_file) as handle: - json.load(handle) - with open(pc_json_file) as handle: - json.load(handle) - - # Cleanup - os.remove(ba_json_file) - os.remove(pc_json_file) *) (* Executes [tezos-client config init mockup] in a state where it should succeed *) let test_config_init_mockup = @@ -886,6 +853,32 @@ let test_config_init_mockup = let (_ : JSON.t) = JSON.parse_file bootstrap_accounts in unit +(* Executes [tezos-client config init mockup] when base dir is NOT a + mockup. It should fail as this is dangerous (the default base + directory could contain sensitive data, such as private keys) *) +let test_config_init_mockup_fail = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Mockup config initialization failure." + ~tags:["mockup"; "client"; "config"; "initialization"] + @@ fun protocol -> + let protocol_constants = Temp.file "protocol-constants.json" in + let bootstrap_accounts = Temp.file "bootstrap-accounts.json" in + let* client = Client.init_mockup ~protocol () in + Log.info "remove the mockup directory to invalidate the mockup state" ; + let* () = rmdir (Client.base_dir client // "mockup") in + let* () = + Client.spawn_config_init + ~protocol + ~bootstrap_accounts + ~protocol_constants + client + |> Process.check_error + in + Check.file_not_exists ~__LOC__ protocol_constants ; + Check.file_not_exists ~__LOC__ bootstrap_accounts ; + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -906,7 +899,8 @@ let register ~protocols = test_transfer_bad_base_dir protocols ; test_config_show_mockup protocols ; test_config_show_mockup_fail protocols ; - test_config_init_mockup protocols + test_config_init_mockup protocols ; + test_config_init_mockup_fail protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; -- GitLab