From 1e04ec54937c4557159cd80d204dd7253f225eaa Mon Sep 17 00:00:00 2001 From: Evgeniy Zayats Date: Tue, 19 Sep 2023 18:58:27 -0400 Subject: [PATCH] tests: additional smart contracts deploy and update Signed-off-by: Evgeniy Zayats --- pytest_tests/testsuites/conftest.py | 12 +++ .../testsuites/contract/test_contract.py | 91 +++++++++++++++++++ .../contract/test_contract/deploy/go.mod | 5 + .../contract/test_contract/deploy/go.sum | 2 + .../contract/test_contract/deploy/main.go | 26 ++++++ .../contract/test_contract/deploy/neo-go.yml | 6 ++ .../contract/test_contract/update/go.mod | 3 + .../contract/test_contract/update/main.go | 12 +++ .../contract/test_contract/update/neo-go.yml | 6 ++ requirements.txt | 2 +- 10 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 pytest_tests/testsuites/contract/test_contract.py create mode 100644 pytest_tests/testsuites/contract/test_contract/deploy/go.mod create mode 100644 pytest_tests/testsuites/contract/test_contract/deploy/go.sum create mode 100644 pytest_tests/testsuites/contract/test_contract/deploy/main.go create mode 100644 pytest_tests/testsuites/contract/test_contract/deploy/neo-go.yml create mode 100644 pytest_tests/testsuites/contract/test_contract/update/go.mod create mode 100644 pytest_tests/testsuites/contract/test_contract/update/main.go create mode 100644 pytest_tests/testsuites/contract/test_contract/update/neo-go.yml diff --git a/pytest_tests/testsuites/conftest.py b/pytest_tests/testsuites/conftest.py index 98fa2d028..3183e9900 100644 --- a/pytest_tests/testsuites/conftest.py +++ b/pytest_tests/testsuites/conftest.py @@ -5,6 +5,7 @@ import uuid import hashlib from datetime import datetime +from distutils import dir_util import allure import pytest @@ -406,3 +407,14 @@ def create_wallet(client_shell: Shell, temp_directory: str, cluster: Cluster, na ) return wallet_path + + +@pytest.fixture +def datadir(tmpdir, request): + filename = request.module.__file__ + test_dir, _ = os.path.splitext(filename) + + if os.path.isdir(test_dir): + dir_util.copy_tree(test_dir, str(tmpdir)) + + return tmpdir diff --git a/pytest_tests/testsuites/contract/test_contract.py b/pytest_tests/testsuites/contract/test_contract.py new file mode 100644 index 000000000..5ea2f053d --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract.py @@ -0,0 +1,91 @@ +import os + +import allure +import pytest +from cluster import Cluster +from common import NEOFS_ADM_CONFIG_PATH, NEOFS_ADM_EXEC, NEOGO_EXECUTABLE +from neofs_testlib.cli import NeofsAdm, NeoGo +from neofs_testlib.shell import Shell + +from steps.cluster_test_base import ClusterTestBase + + +@pytest.mark.additional_contracts +class TestContract(ClusterTestBase): + @allure.title("Test operations with external smart contracts") + def test_contract(self, datadir, client_shell: Shell, cluster: Cluster): + neogo = NeoGo(client_shell, neo_go_exec_path=NEOGO_EXECUTABLE) + with allure.step("Compile new contract"): + neogo.contract.contract_compile( + i=os.path.join(datadir, "deploy"), + out=os.path.join(datadir, "deploy", "testctr_contract.nef"), + manifest=os.path.join(datadir, "deploy", "config.json"), + config=os.path.join(datadir, "deploy", "neo-go.yml"), + ) + + neofsadm = NeofsAdm( + shell=client_shell, + neofs_adm_exec_path=NEOFS_ADM_EXEC, + config_file=NEOFS_ADM_CONFIG_PATH, + ) + + with allure.step("Try to deploy contract with wrong arguments"): + with pytest.raises(RuntimeError, match=".*deploy has failed.*"): + neofsadm.morph.deploy( + rpc_endpoint=cluster.morph_chain_nodes[0].get_endpoint(), + alphabet_wallets="/".join( + cluster.ir_nodes[0].get_wallet_path().split("/")[:-1] + ), + domain="myzone", + contract=os.path.join(datadir, "deploy"), + post_data="string:shouldFail", + ) + + with allure.step("Try to deploy contract with valid arguments"): + neofsadm.morph.deploy( + rpc_endpoint=cluster.morph_chain_nodes[0].get_endpoint(), + alphabet_wallets="/".join(cluster.ir_nodes[0].get_wallet_path().split("/")[:-1]), + domain="myzone", + contract=os.path.join(datadir, "deploy"), + post_data="string:ok", + ) + + with allure.step("Try to update deployed contract"): + with allure.step("Compile new contract"): + neogo.contract.contract_compile( + i=os.path.join(datadir, "update"), + out=os.path.join(datadir, "update", "testctr_contract.nef"), + manifest=os.path.join(datadir, "update", "config.json"), + config=os.path.join(datadir, "update", "neo-go.yml"), + ) + + with allure.step("Try to deploy updated contract with wrong arguments"): + with pytest.raises(RuntimeError, match=".*update has failed.*"): + neofsadm.morph.deploy( + rpc_endpoint=cluster.morph_chain_nodes[0].get_endpoint(), + alphabet_wallets="/".join( + cluster.ir_nodes[0].get_wallet_path().split("/")[:-1] + ), + domain="myzone", + update=True, + contract=os.path.join(datadir, "update"), + post_data="string:shouldFail", + ) + + with allure.step("Try to deploy updated contract with valid arguments"): + neofsadm.morph.deploy( + rpc_endpoint=cluster.morph_chain_nodes[0].get_endpoint(), + alphabet_wallets="/".join( + cluster.ir_nodes[0].get_wallet_path().split("/")[:-1] + ), + domain="myzone", + update=True, + contract=os.path.join(datadir, "update"), + post_data="string:ok", + ) + + hashes = neofsadm.morph.dump_hashes( + rpc_endpoint=cluster.morph_chain_nodes[0].get_endpoint(), + domain="myzone", + ) + assert hashes != "" diff --git a/pytest_tests/testsuites/contract/test_contract/deploy/go.mod b/pytest_tests/testsuites/contract/test_contract/deploy/go.mod new file mode 100644 index 000000000..9fcd6e43b --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/deploy/go.mod @@ -0,0 +1,5 @@ +module testctr + +go 1.18 + +require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220728091153-a5afa875712b diff --git a/pytest_tests/testsuites/contract/test_contract/deploy/go.sum b/pytest_tests/testsuites/contract/test_contract/deploy/go.sum new file mode 100644 index 000000000..88b9454ab --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/deploy/go.sum @@ -0,0 +1,2 @@ +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220728091153-a5afa875712b h1://k5FEn2ImNwEwUXK34sqM+6MUI9fKX7vFBJji3u0CU= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220728091153-a5afa875712b/go.mod h1:QBE0I30F2kOAISNpT5oks82yF4wkkUq3SCfI3Hqgx/Y= diff --git a/pytest_tests/testsuites/contract/test_contract/deploy/main.go b/pytest_tests/testsuites/contract/test_contract/deploy/main.go new file mode 100644 index 000000000..804906e15 --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/deploy/main.go @@ -0,0 +1,26 @@ +package testdata + +import ( + "github.com/nspcc-dev/neo-go/pkg/interop" + "github.com/nspcc-dev/neo-go/pkg/interop/contract" + "github.com/nspcc-dev/neo-go/pkg/interop/native/management" + "github.com/nspcc-dev/neo-go/pkg/interop/runtime" +) + +func _deploy(data interface{}, isUpdate bool) { + if !isUpdate && data.(string) == "shouldFail" { + panic("deploy has failed") + } +} + +// GetThree is a simple function which does nothing. +func GetThree() int { + return 3 +} + +// Update allows to update this contract. +func Update(nef, manifest []byte, data interface{}) { + contract.Call(interop.Hash160(management.Hash), "update", + contract.All, nef, manifest, data) + runtime.Log("test contract was updated") +} diff --git a/pytest_tests/testsuites/contract/test_contract/deploy/neo-go.yml b/pytest_tests/testsuites/contract/test_contract/deploy/neo-go.yml new file mode 100644 index 000000000..c8a997be7 --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/deploy/neo-go.yml @@ -0,0 +1,6 @@ +name: testctr +sourceurl: http://example.com/ +safemethods: [] +supportedstandards: [] +permissions: + - methods: '*' diff --git a/pytest_tests/testsuites/contract/test_contract/update/go.mod b/pytest_tests/testsuites/contract/test_contract/update/go.mod new file mode 100644 index 000000000..d4f95b691 --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/update/go.mod @@ -0,0 +1,3 @@ +module testctr + +go 1.18 diff --git a/pytest_tests/testsuites/contract/test_contract/update/main.go b/pytest_tests/testsuites/contract/test_contract/update/main.go new file mode 100644 index 000000000..b554ec14e --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/update/main.go @@ -0,0 +1,12 @@ +package testdata + +func _deploy(data interface{}, isUpdate bool) { + if isUpdate && data.(string) == "shouldFail" { + panic("update has failed") + } +} + +// GetThree is a simple function which does nothing. +func GetThree() int { + return 42 +} diff --git a/pytest_tests/testsuites/contract/test_contract/update/neo-go.yml b/pytest_tests/testsuites/contract/test_contract/update/neo-go.yml new file mode 100644 index 000000000..c8a997be7 --- /dev/null +++ b/pytest_tests/testsuites/contract/test_contract/update/neo-go.yml @@ -0,0 +1,6 @@ +name: testctr +sourceurl: http://example.com/ +safemethods: [] +supportedstandards: [] +permissions: + - methods: '*' diff --git a/requirements.txt b/requirements.txt index 00706807d..28803e1fa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,7 +30,7 @@ mmh3==3.0.0 multidict==6.0.2 mypy==0.950 mypy-extensions==0.4.3 -neofs-testlib==1.1.10 +neofs-testlib==1.1.11 netaddr==0.8.0 packaging==21.3 paramiko==2.10.3