From 58905147fe0655171a4b72c2fabc34503ae4a42f Mon Sep 17 00:00:00 2001 From: hujiang Date: Fri, 1 Apr 2022 18:19:05 +0800 Subject: [PATCH] refactor: add basic test --- test/basic/scripts/create-contract.py | 69 +++++++++++++++++++ test/basic/scripts/get-balance.py | 24 +++++++ test/basic/scripts/get-block-hash.py | 23 +++++++ test/basic/scripts/get-block-number.py | 22 ++++++ test/basic/scripts/get-block.py | 48 +++++++++++++ test/basic/scripts/get-nonce.py | 32 +++++++++ .../{get_peer_count.sh => get-peer-count.py} | 11 +-- test/basic/scripts/get-peers-info.py | 35 ++++++++++ test/basic/scripts/get-system-config.py | 32 +++++++++ test/basic/scripts/get-tx.py | 38 ++++++++++ test/basic/scripts/get-version.py | 30 ++++++++ test/basic/startup.sh | 2 +- 12 files changed, 361 insertions(+), 5 deletions(-) create mode 100644 test/basic/scripts/create-contract.py create mode 100644 test/basic/scripts/get-balance.py create mode 100644 test/basic/scripts/get-block-hash.py create mode 100644 test/basic/scripts/get-block-number.py create mode 100644 test/basic/scripts/get-block.py create mode 100644 test/basic/scripts/get-nonce.py rename test/basic/scripts/{get_peer_count.sh => get-peer-count.py} (81%) mode change 100755 => 100644 create mode 100644 test/basic/scripts/get-peers-info.py create mode 100644 test/basic/scripts/get-system-config.py create mode 100644 test/basic/scripts/get-tx.py create mode 100644 test/basic/scripts/get-version.py diff --git a/test/basic/scripts/create-contract.py b/test/basic/scripts/create-contract.py new file mode 100644 index 0000000..d9f2fd3 --- /dev/null +++ b/test/basic/scripts/create-contract.py @@ -0,0 +1,69 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# creat contract get receipt get code +import json +import subprocess + +import schedule + +no_receipt_message = ' message: "Not get the receipt"' +hex_prefix = '0x' + +bad_hash = hex_prefix + ''.join(['0' for i in range(64)]) +bad_contract_addr = hex_prefix + ''.join(['0' for j in range(40)]) + +contract_code = "0x608060405234801561001057600080fd5b5060f58061001f6000396000f300608060405260" \ + "0436106053576000357c01000000000000000000000000000000000000000000000000000000" \ + "00900463ffffffff16806306661abd1460585780634f2be91f146080578063d826f88f146094" \ + "575b600080fd5b348015606357600080fd5b50606a60a8565b60405180828152602001915050" \ + "60405180910390f35b348015608b57600080fd5b50609260ae565b005b348015609f57600080" \ + "fd5b5060a660c0565b005b60005481565b60016000808282540192505081905550565b600080" \ + "819055505600a165627a7a72305820faa1d1f51d7b5ca2b200e0f6cdef4f2d7e44ee686209e300beb1146f40d32dee0029" + +create_fmt = 'cldi create {}' +get_receipt_fmt = 'cldi get receipt {}' +get_code_fmt = 'cldi get code {}' + + +def check(get_receipt): + result = subprocess.getoutput(get_receipt) + try: + json_obj = json.loads(result) + except ValueError: + if not result.__contains__(no_receipt_message): + exit(1) + return + code = subprocess.getoutput(get_code_fmt.format(json_obj['contract_addr'])) + if not code.startswith(hex_prefix): + exit(1) + exit(0) + + +if __name__ == "__main__": + + create_result = subprocess.getoutput(create_fmt.format(contract_code)) + if not create_result.startswith(hex_prefix) or not len(create_result) == 2 + 64: + exit(1) + bad_receipt = subprocess.getoutput(get_receipt_fmt.format(bad_hash)) + if not bad_receipt.__contains__(no_receipt_message): + exit(1) + + if hex_prefix != subprocess.getoutput(get_code_fmt.format(bad_contract_addr)): + exit(1) + schedule.every(1).seconds.do(check, get_receipt_fmt.format(create_result)) + while True: + schedule.run_pending() diff --git a/test/basic/scripts/get-balance.py b/test/basic/scripts/get-balance.py new file mode 100644 index 0000000..692a48d --- /dev/null +++ b/test/basic/scripts/get-balance.py @@ -0,0 +1,24 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import subprocess + +if __name__ == "__main__": + hex_prefix = '0x' + good_addr = hex_prefix + ''.join(['0' for i in range(40)]) + zero_balance = hex_prefix + ''.join(['0' for i in range(64)]) + if subprocess.getoutput("cldi get balance {}".format(good_addr)) == zero_balance: + exit(0) + exit(1) \ No newline at end of file diff --git a/test/basic/scripts/get-block-hash.py b/test/basic/scripts/get-block-hash.py new file mode 100644 index 0000000..ba8ef1a --- /dev/null +++ b/test/basic/scripts/get-block-hash.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import subprocess + +if __name__ == "__main__": + hex_prefix = '0x' + result = subprocess.getoutput("cldi get block-hash 1") + if result.startswith(hex_prefix) and len(result) == len(hex_prefix) + 64: + exit(0) + exit(1) diff --git a/test/basic/scripts/get-block-number.py b/test/basic/scripts/get-block-number.py new file mode 100644 index 0000000..3bf68cc --- /dev/null +++ b/test/basic/scripts/get-block-number.py @@ -0,0 +1,22 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import subprocess +result = subprocess.getoutput("cldi get block-number") +if result.isdigit(): + exit(0) +exit(1) + diff --git a/test/basic/scripts/get-block.py b/test/basic/scripts/get-block.py new file mode 100644 index 0000000..39fd603 --- /dev/null +++ b/test/basic/scripts/get-block.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import subprocess, sys, json + +if __name__ == "__main__": + good_height = 1 + bad_height = sys.maxsize + bad_hash = '0x' + ''.join(['0' for i in range(64)]) + cmd = "cldi get block {}" + height_good_result = subprocess.getoutput(cmd.format(1)) + height_bad_result = subprocess.getoutput(cmd.format(sys.maxsize)) + try: + result = json.loads(height_good_result) + if result['height'] != good_height: + exit(1) + except ValueError: + exit(1) + if not height_bad_result.__contains__(' message: "NoBlock"'): + exit(1) + + good_hash = subprocess.getoutput("cldi get block-hash {}".format(good_height)) + hash_good_result = subprocess.getoutput(cmd.format(good_hash)) + hash_bad_result = subprocess.getoutput(cmd.format(bad_hash)) + + try: + result = json.loads(hash_good_result) + if result['height'] != good_height: + exit(1) + except ValueError: + exit(1) + if not hash_bad_result.__contains__(' message: "NoBlockHeight"'): + exit(1) + exit(0) + + diff --git a/test/basic/scripts/get-nonce.py b/test/basic/scripts/get-nonce.py new file mode 100644 index 0000000..4870162 --- /dev/null +++ b/test/basic/scripts/get-nonce.py @@ -0,0 +1,32 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import subprocess + +if __name__ == "__main__": + good_flag = False + bad_flag = False + hex_prefix = '0x' + good_addr = hex_prefix + ''.join(['0' for i in range(40)]) + bad_addr = hex_prefix + ''.join(['0' for j in range(39)]) + get_nonce_fmt = "cldi get nonce {}" + good_result = subprocess.getoutput(get_nonce_fmt.format(good_addr)) + bad_result = subprocess.getoutput(get_nonce_fmt.format(bad_addr)) + if good_result.startswith(hex_prefix) \ + and len(good_result) == 64 + len(hex_prefix) and bad_result.__contains__('invalid hex input'): + exit(0) + exit(1) + diff --git a/test/basic/scripts/get_peer_count.sh b/test/basic/scripts/get-peer-count.py old mode 100755 new mode 100644 similarity index 81% rename from test/basic/scripts/get_peer_count.sh rename to test/basic/scripts/get-peer-count.py index 01210f7..d9dfe9f --- a/test/basic/scripts/get_peer_count.sh +++ b/test/basic/scripts/get-peer-count.py @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/python # # Copyright Rivtower Technologies LLC. # @@ -13,7 +13,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# -# -echo "get peer count" \ No newline at end of file +import subprocess +result = subprocess.getoutput("cldi get peer-count") +if result.isdigit(): + exit(0) +exit(1) + diff --git a/test/basic/scripts/get-peers-info.py b/test/basic/scripts/get-peers-info.py new file mode 100644 index 0000000..c66b690 --- /dev/null +++ b/test/basic/scripts/get-peers-info.py @@ -0,0 +1,35 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import json +import subprocess + +if __name__ == "__main__": + + result = subprocess.getoutput("cldi get peers-info") + try: + json_obj = json.loads(result) + node_list = json_obj['nodes'] + if isinstance(node_list, list): + if len(node_list) == 0: + exit(0) + else: + node_0 = node_list[0] + if len(node_0) == 4 and isinstance(node_0['address'], str) and isinstance(node_0['host'], str) \ + and isinstance(node_0['origin'], int) and isinstance(node_0['port'], int): + exit(0) + except ValueError: + pass + exit(1) \ No newline at end of file diff --git a/test/basic/scripts/get-system-config.py b/test/basic/scripts/get-system-config.py new file mode 100644 index 0000000..e6f5dd7 --- /dev/null +++ b/test/basic/scripts/get-system-config.py @@ -0,0 +1,32 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import subprocess, json + + +def is_json(result): + try: + json.loads(result) + except ValueError: + return False + return True + + +if __name__ == "__main__": + result = subprocess.getoutput("cldi get system-config") + if is_json(result): + exit(0) + exit(1) diff --git a/test/basic/scripts/get-tx.py b/test/basic/scripts/get-tx.py new file mode 100644 index 0000000..45c7a89 --- /dev/null +++ b/test/basic/scripts/get-tx.py @@ -0,0 +1,38 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import subprocess, json, schedule + + +def check(get_tx): + result = subprocess.getoutput(get_tx) + try: + json_obj = json.loads(result) + except ValueError: + if not result.__contains__(' message: "NoTransaction"') and not result.__contains__(' message: "NoTxHeight"'): + exit(1) + return + if isinstance(json_obj['height'], int) and isinstance(json_obj['index'], int): + exit(0) + exit(1) + + +if __name__ == "__main__": + + send_result = subprocess.getoutput("cldi send {} 0x".format('0x' + ''.join(['0' for i in range(40)]))) + cmd = "cldi get tx {}".format(send_result) + schedule.every(1).seconds.do(check, cmd) + while True: + schedule.run_pending() diff --git a/test/basic/scripts/get-version.py b/test/basic/scripts/get-version.py new file mode 100644 index 0000000..57e46a0 --- /dev/null +++ b/test/basic/scripts/get-version.py @@ -0,0 +1,30 @@ +#!/usr/bin/python +# +# Copyright Rivtower Technologies LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import subprocess, re + +pattern = r'\d+\.(?:\d+\.)\d+' + + +def is_version(result): + return re.match(pattern, result) + + +if __name__ == "__main__": + result = subprocess.getoutput("cldi get version") + if is_version(result): + exit(0) + exit(1) diff --git a/test/basic/startup.sh b/test/basic/startup.sh index a256b8f..b8e12b7 100755 --- a/test/basic/startup.sh +++ b/test/basic/startup.sh @@ -20,7 +20,7 @@ dir=`dirname $0` path=$pwd/$dir/scripts for file in `ls "$path"`; do - bash "$path/$file" + python3 "$path/$file" if [ "$?" = "0" ]; then echo "exec $file successful" else -- Gitee