Skip to content

Commit

Permalink
tests: additional smart contracts deploy and update (nspcc-dev#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov authored Sep 21, 2023
2 parents 78dccf9 + 1e04ec5 commit af5aa53
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pytest_tests/testsuites/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
import hashlib
from datetime import datetime
from distutils import dir_util

import allure
import pytest
Expand Down Expand Up @@ -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
91 changes: 91 additions & 0 deletions pytest_tests/testsuites/contract/test_contract.py
Original file line number Diff line number Diff line change
@@ -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 != ""
5 changes: 5 additions & 0 deletions pytest_tests/testsuites/contract/test_contract/deploy/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module testctr

go 1.18

require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220728091153-a5afa875712b
2 changes: 2 additions & 0 deletions pytest_tests/testsuites/contract/test_contract/deploy/go.sum
Original file line number Diff line number Diff line change
@@ -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=
26 changes: 26 additions & 0 deletions pytest_tests/testsuites/contract/test_contract/deploy/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: testctr
sourceurl: http://example.com/
safemethods: []
supportedstandards: []
permissions:
- methods: '*'
3 changes: 3 additions & 0 deletions pytest_tests/testsuites/contract/test_contract/update/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module testctr

go 1.18
12 changes: 12 additions & 0 deletions pytest_tests/testsuites/contract/test_contract/update/main.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: testctr
sourceurl: http://example.com/
safemethods: []
supportedstandards: []
permissions:
- methods: '*'
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af5aa53

Please sign in to comment.