You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cachetest_lib.sh 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/bin/bash
  2. # Copyright 2019-2021 Huawei Technologies Co., Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # ============================================================================
  16. # This file is a collection of functions and globals that are common to the
  17. # test scenarios for cache op testing.
  18. # Set any path variables here
  19. CURRPATH=$(cd "$(dirname $0)"; pwd)
  20. TESTPATH=$(cd "${CURRPATH}/../dataset"; pwd)
  21. PROJECT_PATH=$(cd "${CURRPATH}/../../../../"; pwd)
  22. if [ "x${BUILD_PATH}" == "x" ]; then
  23. BUILD_PATH=${PROJECT_PATH}/build
  24. fi
  25. echo "Using test path: ${TESTPATH}"
  26. echo "Using build path: ${BUILD_PATH}"
  27. # Point to the cache_admin from the build path. The user may also have installed the wheel file but we don't know that.
  28. CACHE_ADMIN="${BUILD_PATH}/package/mindspore/bin/cache_admin"
  29. PYTHON_PYTEST="python -m pytest ${TESTPATH}/"
  30. # These are globals that all testcases use and may get updated during testcase running
  31. failed_tests=0
  32. test_count=0
  33. session_id=0
  34. # sanity check on the cache_admin
  35. if [ ! -f ${CACHE_ADMIN} ]; then
  36. echo "Could not find cache_admin binary. ${CACHE_ADMIN}"
  37. exit 1
  38. fi
  39. #################################################################################
  40. # Function: MsgEnter #
  41. # Description: Display the leading text before entering a block of logic. #
  42. #################################################################################
  43. MsgEnter()
  44. {
  45. printf "%-60s : " "${1}"
  46. }
  47. #################################################################################
  48. # Function: MsgOK #
  49. # Description: Display input msg with a green format for success #
  50. #################################################################################
  51. MsgOk()
  52. {
  53. echo -e '\E[32m'"\033[1m$1\033[0m"
  54. }
  55. #################################################################################
  56. # Function: MsgFail #
  57. # Description: Display input msg with a red format for a failure #
  58. #################################################################################
  59. MsgFail()
  60. {
  61. echo -e '\E[31m'"\033[1m$1\033[0m"
  62. }
  63. #################################################################################
  64. # Function: MsgError #
  65. # Description: If something is not successful, display some info about it #
  66. # #
  67. # Arguments are optional with defaults. You should pass empty string for any #
  68. # args not being used so that it chooses the defaults. #
  69. # #
  70. # Optional arguments: arg 1: An error message. #
  71. # arg 2: The return code. #
  72. # arg 3: The error details #
  73. # #
  74. #################################################################################
  75. MsgError()
  76. {
  77. msg=${1:-none}
  78. err_rc=${2:-none}
  79. err_detail=${3:-none}
  80. if [ "${msg}" != "none" ] ; then
  81. echo "${msg}"
  82. fi
  83. if [ "${err_rc}" != "none" ] ; then
  84. echo "Return code: ${err_rc}"
  85. fi
  86. if [ "${err_detail}" != "none" ] ; then
  87. echo "Error detail:"
  88. echo "{$err_detail}"
  89. fi
  90. echo
  91. }
  92. #################################################################################
  93. # Function: ServerCleanup #
  94. # Description: This is a non-code method to clean up a running cache server. #
  95. # The intended use is for cases when some command has failed, we #
  96. # want to check for any stale process or resources and forcefully #
  97. # remove those resources. #
  98. #################################################################################
  99. ServerCleanup()
  100. {
  101. echo "ServerCleanup is running"
  102. server_pid=$(ps -elf | grep ${USER} | grep cache_server | grep -v grep | awk '{print $4}')
  103. if [ "x${server_pid}" != "x" ]; then
  104. echo "Found a running cache server pid ${server_pid}. Killing this process"
  105. kill -9 ${server_pid}
  106. # small sleep to allow some cleanup time
  107. sleep 2
  108. fi
  109. for i in `ipcs -m | grep ${USER} | awk '{print $2}'`
  110. do
  111. ipcrm -m ${i}
  112. done
  113. echo "ServerCleanup complete."
  114. }
  115. #################################################################################
  116. # Function: CacheAdminCmd #
  117. # Description: Wrapper function for executing cache_admin commands #
  118. # Caller must use HandleRcExit to process the return code. #
  119. # #
  120. # Arguments: arg 1: The command to run #
  121. # arg 2: value of 0 means that we check rc for success. a value #
  122. # of 1 means that we expect a failure from the command. #
  123. #################################################################################
  124. CacheAdminCmd()
  125. {
  126. if [ $# -ne 2 ]; then
  127. echo "Test script invalid. Bad CacheAdminCmd function args."
  128. exit 1
  129. fi
  130. cmd=$1
  131. expect_fail=$2
  132. if [ "${SKIP_ADMIN_COUNTER}" != "true" ]; then
  133. test_count=$(($test_count+1))
  134. echo "Test ${test_count}: ${cmd}"
  135. MsgEnter "Run test ${test_count}"
  136. fi
  137. result=$(${cmd} 2>&1)
  138. rc=$?
  139. if [ "${expect_fail}" -eq 0 ] && [ "${rc}" -ne 0 ]; then
  140. MsgFail "FAILED"
  141. MsgError "cache_admin command failure!" "${rc}" "${result}"
  142. return 1
  143. elif [ "${expect_fail}" -eq 1 ] && [ "${rc}" -eq 0 ]; then
  144. MsgFail "FAILED"
  145. MsgError "Expected failure but got success!" "${rc}" "${result}"
  146. return 1
  147. else
  148. if [ "${SKIP_ADMIN_COUNTER}" != "true" ]; then
  149. MsgOk "OK"
  150. fi
  151. fi
  152. echo
  153. return 0
  154. }
  155. #################################################################################
  156. # Function: PytestCmd #
  157. # Description: Wrapper function for executing pytest #
  158. # Caller must use HandleRcExit to process the return code. #
  159. # #
  160. # Arguments: arg 1: The python script name #
  161. # arg 2: The python function name #
  162. #################################################################################
  163. PytestCmd()
  164. {
  165. test_count=$(($test_count+1))
  166. py_script=$1
  167. py_func=$2
  168. pattern=${3:-0}
  169. # python scripts require special relative paths
  170. cd ..
  171. if [ ${pattern} -eq 0 ]; then
  172. cmd="${PYTHON_PYTEST}${py_script}::${py_func}"
  173. elif [ ${pattern} -eq 1 ]; then
  174. cmd="${PYTHON_PYTEST}${py_script} -k ${py_func}"
  175. else
  176. echo "Invalid Pytest command test script error"
  177. exit 1
  178. fi
  179. echo "Test ${test_count}: ${cmd}"
  180. MsgEnter "Run test ${test_count}"
  181. result=$(${cmd} 2>&1)
  182. rc=$?
  183. if [ ${rc} -ne 0 ]; then
  184. MsgFail "FAILED"
  185. MsgError "pytest call had failure!" "${rc}" "${result}"
  186. cd ${CURRPATH}
  187. return 1
  188. else
  189. MsgOk "OK"
  190. fi
  191. echo
  192. cd ${CURRPATH}
  193. return 0
  194. }
  195. #################################################################################
  196. # Function: StartServer #
  197. # Description: Helper function to call cache_admin to start a default server #
  198. # Caller must use HandleRcExit to process the return code. #
  199. #################################################################################
  200. StartServer()
  201. {
  202. cmd="${CACHE_ADMIN} --start"
  203. CacheAdminCmd "${cmd}" 0
  204. sleep 1
  205. return $?
  206. }
  207. #################################################################################
  208. # Function: StopServer #
  209. # Description: Helper function to call cache_admin to stop cache server #
  210. # Caller must use HandleRcExit to process the return code. #
  211. #################################################################################
  212. StopServer()
  213. {
  214. cmd="${CACHE_ADMIN} --stop"
  215. CacheAdminCmd "${cmd}" 0
  216. return $?
  217. }
  218. #################################################################################
  219. # Function: GetSession #
  220. # Description: Helper function to call cache_admin to generate a session #
  221. # Caller must use HandleRcExit to process the return code. #
  222. #################################################################################
  223. GetSession()
  224. {
  225. # Cannot use CacheAdminCmd for this one because we have special action to set
  226. # the global variable for session id.
  227. cmd="${CACHE_ADMIN} --generate_session"
  228. if [ "${SKIP_ADMIN_COUNTER}" != "true" ]; then
  229. test_count=$(($test_count+1))
  230. echo "Test ${test_count}: ${cmd}"
  231. MsgEnter "Run test ${test_count}"
  232. fi
  233. result=$(${cmd} 2>&1)
  234. rc=$?
  235. if [ ${rc} -ne 0 ]; then
  236. MsgFail "FAILED"
  237. MsgError "cache_admin command failure!" "${rc}" "${result}"
  238. return 1
  239. else
  240. session_id=$(echo $result | awk '{print $NF}')
  241. if [ "${SKIP_ADMIN_COUNTER}" != "true" ]; then
  242. MsgOk "OK"
  243. echo "Generated session id: ${session_id}"
  244. echo
  245. fi
  246. fi
  247. return 0
  248. }
  249. #################################################################################
  250. # Function: DestroySession #
  251. # Description: Helper function to call cache_admin to destroy a session #
  252. # Caller must use HandleRcExit to process the return code. #
  253. #################################################################################
  254. DestroySession()
  255. {
  256. cmd="${CACHE_ADMIN} --destroy_session ${session_id}"
  257. CacheAdminCmd "${cmd}" 0
  258. return $?
  259. }
  260. #################################################################################
  261. # Function: HandlerRcExit #
  262. # Description: handles a return code if you used one of the above helper funcs #
  263. # It updates the global test counters and chooses to quit or not #
  264. # depending on the setting of exit_on_fail argument #
  265. # #
  266. # Arguments: arg 1: The rc to handle #
  267. # arg 2: Set to 1 to cause error exit. 0 for no exit #
  268. # arg 3: Set to 1 to invoke server cleanup on error case #
  269. #################################################################################
  270. HandleRcExit()
  271. {
  272. if [ $# -ne 3 ]; then
  273. echo "Test script invalid. Bad CacheAdminCmd function args."
  274. exit 1
  275. fi
  276. err_rc=$1
  277. exit_on_fail=$2
  278. clean_on_fail=$3
  279. if [ ${err_rc} -ne 0 ]; then
  280. failed_tests=$(($failed_tests+1))
  281. if [ ${clean_on_fail} -eq 1 ]; then
  282. ServerCleanup
  283. fi
  284. if [ ${exit_on_fail} -eq 1 ]; then
  285. exit $failed_tests
  286. else
  287. return 1
  288. fi
  289. fi
  290. return 0
  291. }
  292. #################################################################################
  293. # Function: ExitHandler #
  294. # Description: Invokes final display message of the script before quitting #
  295. #################################################################################
  296. ExitHandler()
  297. {
  298. success_count=$(($test_count-$failed_tests))
  299. echo "------------------------------------"
  300. echo "${test_count} tests run in total."
  301. echo "${success_count} tests ran successfully."
  302. echo "${failed_tests} failed tests."
  303. exit ${failed_tests}
  304. }
  305. trap ExitHandler EXIT SIGINT