github.com/turingchain2020/turingchain@v1.1.21/build/autotest/gitlabci/autotest.sh (about) 1 #!/usr/bin/env bash 2 3 set -e 4 set -o pipefail 5 #set -o verbose 6 #set -o xtrace 7 8 # os: ubuntu16.04 x64 9 # first, you must install jq tool of json 10 # sudo apt-get install jq 11 # sudo apt-get install shellcheck, in order to static check shell script 12 # sudo apt-get install parallel 13 # ./run-autotest.sh build 14 15 PWD=$(cd "$(dirname "$0")" && pwd) 16 export PATH="$PWD:$PATH" 17 18 PROJECT_NAME="${1}" 19 20 NODE3="autotest-turingchain" 21 CLI="docker exec ${NODE3} /root/turingchain-cli" 22 23 NODE2="autotest-chain32" 24 CLI2="docker exec ${NODE2} /root/turingchain-cli" 25 26 sedfix="" 27 if [ "$(uname)" == "Darwin" ]; then 28 sedfix=".bak" 29 fi 30 31 function init() { 32 # update test environment 33 sed -i $sedfix 's/^Title.*/Title="local"/g' turingchain.toml 34 sed -i $sedfix 's/^TestNet=.*/TestNet=true/g' turingchain.toml 35 36 # p2p 37 sed -i $sedfix 's/^seeds=.*/seeds=["turingchain:13802","chain32:13802"]/g' turingchain.toml 38 #sed -i $sedfix 's/^enable=.*/enable=true/g' turingchain.toml 39 sed -i $sedfix '0,/^enable=.*/s//enable=true/' turingchain.toml 40 sed -i $sedfix 's/^isSeed=.*/isSeed=true/g' turingchain.toml 41 sed -i $sedfix 's/^innerSeedEnable=.*/innerSeedEnable=false/g' turingchain.toml 42 sed -i $sedfix 's/^useGithub=.*/useGithub=false/g' turingchain.toml 43 44 # rpc 45 sed -i $sedfix 's/^jrpcBindAddr=.*/jrpcBindAddr="0.0.0.0:9671"/g' turingchain.toml 46 sed -i $sedfix 's/^grpcBindAddr=.*/grpcBindAddr="0.0.0.0:9672"/g' turingchain.toml 47 sed -i $sedfix 's/^whitelist=.*/whitelist=["localhost","127.0.0.1","0.0.0.0"]/g' turingchain.toml 48 49 # wallet 50 sed -i $sedfix 's/^minerdisable=.*/minerdisable=false/g' turingchain.toml 51 52 } 53 54 function start() { 55 56 # remove exsit container 57 docker-compose -p "${PROJECT_NAME}" -f compose-autotest.yml down --remove-orphans 58 59 # create and run docker-compose container 60 docker-compose -p "${PROJECT_NAME}" -f compose-autotest.yml up --build -d 61 62 local SLEEP=30 63 echo "=========== sleep ${SLEEP}s =============" 64 sleep ${SLEEP} 65 66 # docker-compose ps 67 docker-compose -p "${PROJECT_NAME}" -f compose-autotest.yml ps 68 69 # query node run status 70 ${CLI} block last_header 71 ${CLI} net info 72 73 ${CLI} net peer 74 peersCount=$(${CLI} net peer | jq '.[] | length') 75 echo "${peersCount}" 76 if [ "${peersCount}" -lt 2 ]; then 77 echo "peers error" 78 exit 1 79 fi 80 81 #echo "=========== # create seed for wallet =============" 82 #seed=$(${CLI} seed generate -l 0 | jq ".seed") 83 #if [ -z "${seed}" ]; then 84 # echo "create seed error" 85 # exit 1 86 #fi 87 88 echo "=========== # save seed to wallet =============" 89 result=$(${CLI} seed save -p 1314fuzamei -s "tortoise main civil member grace happy century convince father cage beach hip maid merry rib" | jq ".isok") 90 if [ "${result}" = "false" ]; then 91 echo "save seed to wallet error seed, result: ${result}" 92 exit 1 93 fi 94 95 sleep 1 96 97 echo "=========== # unlock wallet =============" 98 result=$(${CLI} wallet unlock -p 1314fuzamei -t 0 | jq ".isok") 99 if [ "${result}" = "false" ]; then 100 exit 1 101 fi 102 103 echo "=========== # import private key returnAddr =============" 104 result=$(${CLI} account import_key -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944 -l returnAddr | jq ".label") 105 echo "${result}" 106 if [ -z "${result}" ]; then 107 exit 1 108 fi 109 110 echo "=========== # import private key mining =============" 111 result=$(${CLI} account import_key -k 4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01 -l minerAddr | jq ".label") 112 echo "${result}" 113 if [ -z "${result}" ]; then 114 exit 1 115 fi 116 117 echo "=========== # close auto mining =============" 118 result=$(${CLI} wallet auto_mine -f 0 | jq ".isok") 119 if [ "${result}" = "false" ]; then 120 exit 1 121 fi 122 123 echo "=========== sleep ${SLEEP}s =============" 124 sleep ${SLEEP} 125 126 echo "=========== query height ========== " 127 ${CLI} block last_header 128 result=$(${CLI} block last_header | jq ".height") 129 if [ "${result}" -lt 1 ]; then 130 exit 1 131 fi 132 133 sync_status "${CLI}" 134 135 ${CLI} wallet status 136 ${CLI} account list 137 ${CLI} mempool list 138 } 139 140 function block_wait() { 141 if [ "$#" -lt 2 ]; then 142 echo "wrong block_wait params" 143 exit 1 144 fi 145 cur_height=$(${1} block last_header | jq ".height") 146 expect=$((cur_height + ${2})) 147 count=0 148 while true; do 149 new_height=$(${1} block last_header | jq ".height") 150 if [ "${new_height}" -ge "${expect}" ]; then 151 break 152 fi 153 count=$((count + 1)) 154 sleep 1 155 done 156 echo "wait new block $count s" 157 } 158 159 function sync_status() { 160 echo "=========== query sync status========== " 161 local sync_status 162 local count=100 163 local wait_sec=0 164 while [ $count -gt 0 ]; do 165 sync_status=$(${1} net is_sync) 166 if [ "${sync_status}" = "true" ]; then 167 break 168 fi 169 ((count--)) 170 wait_sec=$((wait_sec + 1)) 171 sleep 1 172 done 173 echo "sync wait ${wait_sec} s" 174 175 echo "=========== query clock sync status========== " 176 sync_status=$(${1} net is_clock_sync) 177 if [ "${sync_status}" = "false" ]; then 178 exit 1 179 fi 180 } 181 182 function sync() { 183 echo "=========== stop ${NODE2} node========== " 184 docker stop "${NODE2}" 185 sleep 20 186 187 echo "=========== start ${NODE2} node========== " 188 docker start "${NODE2}" 189 190 sleep 1 191 sync_status "${CLI2}" 192 } 193 194 function auto_test() { 195 196 echo "=========== #run auto test =============" 197 echo "=========== #transfer to token amdin =============" 198 hash=$(${CLI} send coins transfer -a 10 -n test -t 1Q8hGLfoGe63efeWa8fJ4Pnukhkngt6poK -k 4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01) 199 200 block_wait "${CLI}" 2 201 txs=$(${CLI} tx query_hash -s "${hash}" | jq ".txs") 202 if [ "${txs}" == "null" ]; then 203 echo "transferTokenAdmin cannot find tx" 204 exit 1 205 fi 206 207 echo "=========== #config token blacklist =============" 208 rawData=$(${CLI} config config_tx -c token-blacklist -o add -v BTC) 209 signData=$(${CLI} wallet sign -d "${rawData}" -k 0xc34b5d9d44ac7b754806f761d3d4d2c4fe5214f6b074c19f069c4f5c2a29c8cc) 210 hash=$(${CLI} wallet send -d "${signData}") 211 block_wait "${CLI}" 2 212 213 echo "=========== #start auto-test program =============" 214 docker exec "${NODE3}" /root/autotest 215 } 216 217 function stop_turingchain() { 218 219 rv=$? 220 echo "=========== #stop docker-compose =============" 221 docker-compose -p "${PROJECT_NAME}" -f compose-autotest.yml down && rm -rf ./turingchain* ./*.toml ./autotest 222 echo "=========== #remove related images ============" 223 docker rmi "${PROJECT_NAME}"_autotest || true 224 exit ${rv} 225 } 226 227 function main() { 228 echo "==========================================main begin========================================================" 229 init 230 start 231 auto_test 232 echo "==========================================main end=========================================================" 233 } 234 235 # check args 236 if [ "$#" -ne 1 ]; then 237 echo "Suggest Usage: $0 build" 238 exit 1 239 fi 240 241 #trap exit 242 trap "stop_turingchain" INT TERM EXIT 243 244 # run script 245 main