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