Commit adde8d1 [storage/multipath] move include files to single directory

6 files Authored and Committed by lilin a year ago
[storage/multipath] move include files to single directory

    
  1 @@ -0,0 +1,260 @@
  2 + #!/bin/bash
  3 + 
  4 + # Copyright (c) 2016 Red Hat, Inc. All rights reserved.
  5 + #
  6 + # This program is free software: you can redistribute it and/or modify
  7 + # it under the terms of the GNU General Public License as published by
  8 + # the Free Software Foundation, either version 3 of the License, or
  9 + # (at your option) any later version.
 10 + #
 11 + # This program is distributed in the hope that it will be useful,
 12 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 + # GNU General Public License for more details.
 15 + #
 16 + # You should have received a copy of the GNU General Public License
 17 + # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 + 
 19 + # Author: Lin Li   <lilin@redhat.com>
 20 + 
 21 + path=$(pwd)
 22 + source ../include/utils.sh || exit 1
 23 + source ../include/include.sh || exit 1
 24 + source ../include/tc.sh || exit 1
 25 + source ../include/mpath.sh || exit 1 
 26 + source ../include/scsi_debug.sh || exit 1 
 27 + 
 28 + #this script usually is only used by current case
 29 + #private global variables, lower case and no under line is okay 
 30 + mpath=
 31 + 
 32 + function _isconfig (){
 33 +     [ -z $mpath ] && return 2
 34 +     Cmd "multipath -ll $mpath"
 35 + }
 36 + 
 37 + function _init (){
 38 + #will improve this function
 39 + #should return the multipathed disk created by scsi_debug, don't consider the other mutipathed disks
 40 + #olnly append the black list exception to /etc/multipath.conf not override
 41 +     Setup_Multipath || Fail "failed to create multipathed device via scsi_debug" 
 42 +     mpath=$RETURN_STR
 43 + 
 44 +     _isconfig
 45 + }
 46 + 
 47 + function _destroy (){
 48 +     sleep 10
 49 +     Cmd "multipath -F"
 50 +     sleep 5
 51 +     Cmd "modprobe -r scsi_debug"
 52 + }
 53 + 
 54 + 
 55 + # ---------------------------------------------------------#
 56 + # Mutipath_installation()
 57 + # Usage:
 58 + # check if multipath starts, if not install it with yum
 59 + # Parameter:
 60 + #   NULL
 61 + # Returns:
 62 + #   Return code:
 63 + #       0 on success
 64 + #       1 if something went wrong.
 65 + # ---------------------------------------------------------#
 66 + function Multipath_installation (){
 67 +     Cmd "rpm -qi device-mapper-multipath" && return 0
 68 +     Cmd "yum install -y device-mapper-multipath"
 69 + }
 70 + 
 71 + 
 72 + # ---------------------------------------------------------#
 73 + # Print_kernel_info()
 74 + # Usage:
 75 + # print the detail running kernel information. 
 76 + # Parameter: #   NULL
 77 + # Returns:
 78 + #   Return code:
 79 + #       0 on success
 80 + #       1 if something went wrong.
 81 + # ---------------------------------------------------------#
 82 + function Print_kernel_info (){
 83 +     Cmd "lsb_release -a"
 84 +     Cmd "uname -a"
 85 + }
 86 + 
 87 + 
 88 + # ---------------------------------------------------------#
 89 + # Print_multipath_pkginfo()()
 90 + # Usage:
 91 + # print the multipath pacakge information
 92 + # Parameter:
 93 + #   NULL
 94 + # Returns:
 95 + #   Return code:
 96 + #       0 on success
 97 + #       1 if something went wrong.
 98 + # ---------------------------------------------------------#
 99 + function Print_multipath_pkginfo (){
100 +     Cmd "rpm -qi device-mapper-multipath"
101 +     Cmd "rpm -qi kpartx"
102 + }
103 + 
104 + 
105 + # ---------------------------------------------------------#
106 + # Setup_Multipath ()
107 + # Usage:
108 + #   return mpath_name if we have multipath devices, if not,
109 + #   we use scsi_debug to create a multipath device.
110 + # Parameter:
111 + #   NULL
112 + # Returns:
113 + #   Return code:
114 + #       0 on success
115 + #       1 if something went wrong.
116 + #   Return string:
117 + #       RETURN_STR  # $mpath_name_list, like "mpath0 mpath1"
118 + # ---------------------------------------------------------#
119 + 
120 + function Setup_Multipath (){
121 +     RETURN_STR=''
122 +     local mpath_name_list=$(dmsetup table \
123 +         | perl -ne 'print "$1 " if /(mpath[a-z0-9]+):[0-9 ]+multipath.*/')
124 +     if [ "CHK${mpath_name_list}" != "CHK" ];then
125 +         mpath_name_list="$(echo ${mpath_name_list} | sed -e 's/ $//')"
126 +         echo "INFO: Found multipath devices: ${mpath_name_list}"
127 + #RHEL 5 will disable some wwn mpath if we install OS with ondisk=mapper/mpath0
128 + # option, so we need to enable them all
129 +         if [ "CHK$(uname -r | egrep "2\.6\.18.*el5")" != "CHK" ];then
130 +             cat << AA > /etc/multipath.conf
131 + defaults {
132 +     user_friendly_names     yes
133 + }
134 + blacklist {
135 +     device {
136 +         vendor .*
137 +         product .*
138 +     }
139 + }
140 + blacklist_exceptions {
141 +     device {
142 +         vendor Linux
143 +         product scsi_debug
144 +     }
145 +     device {
146 +         vendor IQSTOR
147 +         product .*
148 +     }
149 +     device {
150 +         vendor NETAPP
151 +         product .*
152 +     }
153 +     device {
154 +         vendor HITACHI
155 +         product .*
156 +     }
157 + }
158 + AA
159 +         service multipathd start
160 +         sleep 5s #multipathd return premature
161 +         multipath -r
162 +         multipathd -k'reconfigure'
163 +         mpath_name_list=$(dmsetup table \
164 +             | perl -ne 'print "$1 " if /(mpath[a-z0-9]+):[0-9 ]+multipath.*/')
165 +         fi
166 +         if [ "CHK${mpath_name_list}" == "CHK" ];then
167 +             echo -n "FATAL: still no mulipath devices setup,"
168 +             echo " check code in Setup_Multipath()"
169 +             RETURN_STR=''
170 +             return 1
171 +         fi
172 +         RETURN_STR="${mpath_name_list}"
173 +         return 0
174 +     fi
175 + #setup scsi_debug
176 +     echo "INFO: Loading scsi_debug module for simulation of mpath"
177 +     local dev_size=100
178 +     if [ "CHK$(uname -m)" == "CHKi386" ] \
179 +         || [ "CHK$(uname -m)" == "CHKi686" ]; then
180 + #i386 platform cannot allocate 100 MiB in kernel space
181 +         dev_size=10
182 +     fi
183 +     modprobe scsi_debug dev_size_mb=${dev_size} \
184 +         num_tgts=1 vpd_use_hostno=0 \
185 +         add_host=4 delay=20 \
186 +         max_luns=2 no_lun_0=1 2>&1 1>/dev/null
187 + 
188 +     echo "INFO: Waiting for udev to create /dev/sdX"
189 +     sleep 15s #wait for udev to create /dev/sdX
190 +     rpm -q device-mapper-multipath 2>/dev/null 1>/dev/null
191 +     if [ $? -ne 0 ];then
192 +         echo "INFO: Installing device-mapper-multipath via yum"
193 +         yum -y install device-mapper-multipath
194 +     fi
195 + #enable multipath for scsi_debug.
196 +     cat << AA > /etc/multipath.conf
197 + defaults {
198 + #Enable multibus is for mutlbus testing
199 +     path_grouping_policy multibus
200 +     user_friendly_names     yes
201 + }
202 + blacklist {
203 +         device {
204 +                 vendor .*
205 +                 product .*
206 +         }
207 + }
208 + blacklist_exceptions {
209 +         device {
210 +                 vendor Linux
211 +                 product scsi_debug
212 +         }
213 + }
214 + AA
215 +     echo "INFO: /etc/multipath.conf updated"
216 +     cat /etc/multipath.conf
217 +     echo "INFO: Restarting multiapth and reload configuration"
218 +     service multipathd restart
219 +     sleep 5s #multipathd return premature
220 +     multipathd -k'reconfig'
221 + 
222 +     mpath_name_list=$(dmsetup table | perl -ne 'print "$1 " if /(mpath[a-z0-9]+):[0-9 ]+multipath.*/')
223 +     if [ "CHK${mpath_name_list}" != "CHK" ];then
224 +         mpath_name_list="$(echo ${mpath_name_list} | sed -e 's/ $//')"
225 +         echo "INFO: found mpath: ${mpath_name_list}"
226 +         RETURN_STR="${mpath_name_list}"
227 +         return 0
228 +     fi
229 +     return 1
230 + } #end of functoin Setup_Multipath
231 + 
232 + 
233 + get_wwid_of_disk()
234 + {
235 + # we should not use scsi_id or /dev/disk/by-id to get the wwid
236 + # since multipath could replace the white spaces of wwid if
237 + # having white spaces. we should use
238 + # multipathd show paths format %w %d
239 + # to get the wwid
240 + 
241 + #    for dev in `ls /dev/disk/by-id/*`
242 + #    do
243 + #        if readlink $dev | grep -qw "$disk$"
244 + #        then
245 + #            wwid=$(basename $dev | sed 's/^[^-]*-//g')
246 + #            break
247 + #        fi
248 + #    done
249 + #    if test X$wwid = X
250 + #    then
251 + #        wwid=$(/lib/udev/scsi_id --page=0x83 --whitelisted --device=/dev/$disk)
252 + #    fi
253 + #    echo $wwid
254 + 
255 +     multipathd_running || texit "multipathd is not running"
256 +     local disk=$1
257 +     local wwid=$(multipathd show paths format %d,%w | grep "^$disk\s*," | \
258 +         awk -F, '{ print $2 }' | sed 's/\s//g')
259 +     echo $wwid
260 + }
261 + 
  1 @@ -0,0 +1,124 @@
  2 + #!/bin/bash
  3 + 
  4 + # Copyright (c) 2016 Red Hat, Inc. All rights reserved.
  5 + #
  6 + # This program is free software: you can redistribute it and/or modify
  7 + # it under the terms of the GNU General Public License as published by
  8 + # the Free Software Foundation, either version 3 of the License, or
  9 + # (at your option) any later version.
 10 + #
 11 + # This program is distributed in the hope that it will be useful,
 12 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 + # GNU General Public License for more details.
 15 + #
 16 + # You should have received a copy of the GNU General Public License
 17 + # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 + 
 19 + # Author: Lin Li   <lilin@redhat.com>
 20 + 
 21 + 
 22 + 
 23 + #----------------------------------------------------------------------------#
 24 + # Mp_Conf_Up_Def ()
 25 + # Usage:
 26 + #   Update default section of /etc/multipath.conf, will also reload conf.
 27 + # Parameter:
 28 + #   $config_change #config string want to change, like 'polling_interval 5'
 29 + # Returns:
 30 + #   Return code:
 31 + #       0 on success
 32 + #       1 if something went wrong.
 33 + #   Return string:
 34 + #       NULL
 35 + #----------------------------------------------------------------------------#
 36 + 
 37 + function Mp_Conf_Up_Def (){
 38 +     EX_USAGE=64 # Bad arg format
 39 +     if [ $# -lt 1 ]; then
 40 +         echo 'Usage: Mp_Conf_Up_Def $config_change'
 41 +         exit "${EX_USAGE}"
 42 +     fi
 43 +     RETURN_STR=''
 44 +     local multipath_conf_filename="/etc/multipath.conf"
 45 +     local config_change="$1"
 46 +     echo ${config_change}
 47 +     if [ "CHK${config_change}" == "CHK" ];then
 48 +         echo 'Usage: Mp_Conf_Up_Def $config_change'
 49 +         exit "${EX_USAGE}"
 50 +     fi
 51 +     python2 -c "
 52 + import sys
 53 + sys.path.append('"${MP_INCLUDE_PATH}"')
 54 + from mpconf import *
 55 + update_df_section('""${config_change}""')
 56 + "
 57 + #TODO: we need to check configuration before we return 0
 58 +     RETURN_STR=""
 59 +     return 0
 60 + } #end of functoin Mp_Conf_Up_Def
 61 + 
 62 + 
 63 + # ---------------------------------------------------------#
 64 + # Print_multipath_pkginfo()()
 65 + # Usage:
 66 + # print the multipath pacakge information
 67 + # Parameter:
 68 + #   NULL
 69 + # Returns:
 70 + #   Return code:
 71 + #       0 on success
 72 + #       1 if something went wrong.
 73 + # ---------------------------------------------------------#
 74 + function Print_multipath_pkginfo (){
 75 +     Cmd "rpm -qi device-mapper-multipath"
 76 +     Cmd "rpm -qi kpartx"
 77 + }
 78 + 
 79 + # ---------------------------------------------------------#
 80 + # Mutipathd_stop()
 81 + # Usage:
 82 + # check if multipathd stops, if not stop it
 83 + # Parameter:
 84 + #   NULL
 85 + # Returns:
 86 + #   Return code:
 87 + #       0 on success
 88 + #       1 if something went wrong.
 89 + # ---------------------------------------------------------#
 90 + function Multipathd_stop (){
 91 + #    { ! Cmd pidof multipathd ; } || Cmd "service multipathd stop"
 92 +     { ! Cmd pidof multipathd ; } || Cmd "kill -9 `pidof multipathd`"
 93 + }
 94 + 
 95 + # ---------------------------------------------------------#
 96 + # Mutipathd_start()
 97 + # Usage:
 98 + # check if multipathd is installed, if not start it
 99 + # Parameter:
100 + #   NULL
101 + # Returns:
102 + #   Return code:
103 + #       0 on success
104 + #       1 if something went wrong.
105 + # ---------------------------------------------------------#
106 + function Multipathd_start (){
107 + #    { Cmd pidof multipathd ; } || Cmd "service multipathd start"
108 +     { Cmd pidof multipathd ; } || Cmd "multipathd"
109 + }
110 + 
111 + # ---------------------------------------------------------#
112 + # Mutipath_installation()
113 + # Usage:
114 + # check if multipath starts, if not install it with yum
115 + # Parameter:
116 + #   NULL
117 + # Returns:
118 + #   Return code:
119 + #       0 on success
120 + #       1 if something went wrong.
121 + # ---------------------------------------------------------#
122 + function Multipath_installation (){
123 +     Cmd "rpm -qi device-mapper-multipath" && return 0
124 +     Cmd "yum install -y device-mapper-multipath" 
125 + } 
  1 @@ -0,0 +1,156 @@
  2 + #!/bin/bash
  3 + 
  4 + # Copyright (c) 2016 Red Hat, Inc. All rights reserved.
  5 + #
  6 + # This program is free software: you can redistribute it and/or modify
  7 + # it under the terms of the GNU General Public License as published by
  8 + # the Free Software Foundation, either version 3 of the License, or
  9 + # (at your option) any later version.
 10 + #
 11 + # This program is distributed in the hope that it will be useful,
 12 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 + # GNU General Public License for more details.
 15 + #
 16 + # You should have received a copy of the GNU General Public License
 17 + # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 + 
 19 + # Author: Lin Li   <lilin@redhat.com>
 20 + 
 21 + 
 22 + # filename: loop.sh
 23 + 
 24 + # USAGE
 25 + 
 26 + test x$LXT_MPATH = x || return
 27 + LXT_MPATH=1
 28 + 
 29 + #source /mnt/tests/kernel/storage/include/bash_modules/lxt/tc.sh
 30 + 
 31 + # make sure multipathd is running
 32 + get_mpath_disks()
 33 + {
 34 +     multipathd_running || texit "multipathd is not running" 
 35 +     multipathd -k'show maps' 2>/dev/null | grep '^mpath' | awk '{print
 36 + "/dev/mapper/" $1}'
 37 +     return 0
 38 + }
 39 + 
 40 + # make sure multipathd is running
 41 + get_wwid_of_disk()
 42 + {
 43 + # we should not use scsi_id or /dev/disk/by-id to get the wwid
 44 + # since multipath could replace the white spaces of wwid if 
 45 + # having white spaces. we should use 
 46 + # multipathd show paths format %w %d 
 47 + # to get the wwid
 48 + 
 49 + #    for dev in `ls /dev/disk/by-id/*`
 50 + #    do
 51 + #        if readlink $dev | grep -qw "$disk$" 
 52 + #        then
 53 + #            wwid=$(basename $dev | sed 's/^[^-]*-//g')
 54 + #            break
 55 + #        fi
 56 + #    done
 57 + #    if test X$wwid = X
 58 + #    then
 59 + #        wwid=$(/lib/udev/scsi_id --page=0x83 --whitelisted --device=/dev/$disk)
 60 + #    fi
 61 + #    echo $wwid
 62 + 
 63 +     multipathd_running || texit "multipathd is not running" 
 64 +     local disk=$1
 65 +     local wwid=$(multipathd show paths format %d,%w | grep "^$disk\s*," | \
 66 +         awk -F, '{ print $2 }' | sed 's/\s//g')
 67 +     echo $wwid
 68 + }
 69 + 
 70 + get_scsi_id()
 71 + {
 72 +     dev=$1
 73 +     wwid=$(/lib/udev/scsi_id --page=0x83 --whitelisted --device=$dev --replace-whitespace)
 74 +     echo $wwid
 75 + }
 76 + 
 77 + # make sure multipathd is running
 78 + get_mpath_disk_by_scsi_device()
 79 + {
 80 + #    multipathd_running || texit "multipathd is not running" 
 81 +     local disk=$1
 82 +     wwid=$(get_wwid_of_disk $disk)
 83 +     local mpath=$(multipathd show maps format %w,%n | grep "^$wwid\s*," \
 84 +         | awk -F, '{ print $2 }' | sed 's/\s//g')
 85 +     echo $mpath
 86 + }
 87 + 
 88 + # make sure multipathd is running
 89 + get_major_minor_by_scsi_device()
 90 + {
 91 +     multipathd_running || texit "multipathd is not running" 
 92 +     local disk=$1
 93 +     local mm=$(multipathd show paths format %d,%D | grep "^$disk\s*," | \
 94 +         awk -F, '{ print $2 }' | sed 's/\s//g')
 95 +     echo $mm
 96 + }
 97 + 
 98 + # make sure multipathd is running
 99 + get_hcil_by_scsi_device()
100 + {
101 +     multipathd_running || texit "multipathd is not running" 
102 +     local disk=$1
103 +     local hcil=$(multipathd show paths format %d,%i | grep "^$disk\s*," | \
104 +         awk -F, '{ print $2 }' | sed 's/\s//g')
105 +     echo $hcil
106 + }
107 + 
108 + # make sure multipathd is running
109 + is_mpath()
110 + {
111 +     multipathd_running || texit "multipathd is not running" 
112 +     local mpath=$1
113 +     multipathd show maps format %n | grep -w "^$mpath" &> /dev/null
114 + }
115 + 
116 + multipathd_running()
117 + {
118 +     pidof multipathd &> /dev/null && return 0
119 +     return 1
120 + }
121 + 
122 + # will set find_multipaths no and start multipathd
123 + setup_multipath()
124 + {
125 +     trun "rpm -q device-mapper-multipath || yum install -y device-mapper-multipath" || texit "fail to install multipath"
126 +     test -f /etc/multipath.conf && cp /etc/multipath.conf /etc/multipath.conf.storage_qe 
127 +     trun "mpathconf --enable --find_multipaths n --with_multipathd y" 
128 +     trun "multipath -r"
129 + }
130 + 
131 + clear_multipath()
132 + {
133 +     tlog "disable multipath and remove the mpath devices"
134 +     tlog "revert back the multipath.conf if has backup file"
135 +     test -f /etc/multipath.conf.storage_qe && cp /etc/multipath.conf.storage_qe /etc/multipath.conf &>/dev/null
136 +     ( multipath -q && multipath -F ) &>/dev/null
137 +     stop_multipathd
138 + }
139 + 
140 + get_unused_mpath()
141 + {
142 +     multipathd_running &>/dev/null || texit "multipathd is not running" 
143 +     rootdisk=$(get_root_disk)
144 +     rootmpath=$(get_mpath_disk_by_scsi_device $rootdisk)
145 +     if is_null $rootmpath; then
146 +         $maps=$(multipathd show maps format "%n" | grep -v name)
147 +     else 
148 +         $maps=$(multipathd show maps format "%n" | grep -v name | grep -v $rootmpath)
149 +     fi
150 +     
151 +     echo $maps
152 + }
153 + 
154 + stop_multipathd() {
155 +     tlog "stop multipathd"
156 +     ( pidof multipathd && service multipathd stop || pkill multipathd ) &>/dev/null
157 + }
 1 @@ -0,0 +1,36 @@
 2 + #!/bin/bash
 3 + 
 4 + # Copyright (c) 2016 Red Hat, Inc. All rights reserved.
 5 + #
 6 + # This program is free software: you can redistribute it and/or modify
 7 + # it under the terms of the GNU General Public License as published by
 8 + # the Free Software Foundation, either version 3 of the License, or
 9 + # (at your option) any later version.
10 + #
11 + # This program is distributed in the hope that it will be useful,
12 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + # GNU General Public License for more details.
15 + #
16 + # You should have received a copy of the GNU General Public License
17 + # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 + 
19 + # Author: Lin Li   <lilin@redhat.com>
20 + 
21 + # filename: function
22 + 
23 + # USAGE
24 + 
25 + test x$LXT_SCSI_DEBUG = x || return
26 + LXT_SCSI_DEBUG=1
27 + 
28 + 
29 + get_scsi_debug_devices ()
30 + {
31 +     ls /sys/block/sd* -d 2>/dev/null | while read dev; do
32 +         dev=$(basename $dev);
33 +         grep -qw scsi_debug /sys/block/$dev/device/model && echo "/dev/$dev" && break;
34 +     done
35 + 
36 +     return 0
37 + }
  1 @@ -0,0 +1,272 @@
  2 + #!/bin/bash
  3 + 
  4 + # Copyright (c) 2016 Red Hat, Inc. All rights reserved.
  5 + #
  6 + # This program is free software: you can redistribute it and/or modify
  7 + # it under the terms of the GNU General Public License as published by
  8 + # the Free Software Foundation, either version 3 of the License, or
  9 + # (at your option) any later version.
 10 + #
 11 + # This program is distributed in the hope that it will be useful,
 12 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 + # GNU General Public License for more details.
 15 + #
 16 + # You should have received a copy of the GNU General Public License
 17 + # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 + 
 19 + # Author: Lin Li   <lilin@redhat.com>
 20 + 
 21 + # filename: function
 22 + 
 23 + # USAGE
 24 + 
 25 + test x$LXT_TC = x || return
 26 + LXT_TC=1
 27 + 
 28 + #
 29 + # print the current date
 30 + # usage: d=$(tdate)
 31 + #
 32 + tdate () 
 33 + {
 34 +     date '+%T' 2>/dev/null
 35 + }
 36 + 
 37 + #
 38 + # print the log information
 39 + # usage: tlog "hello world" "WARNING"
 40 + #
 41 + tlog ()
 42 + {
 43 +     local msg=$1
 44 +     local log_level=${2:-INFO}
 45 +     local cur_date=$(tdate)
 46 + 
 47 +     echo "[$log_level][$cur_date]$msg"
 48 +     
 49 +     return 0
 50 + }
 51 + 
 52 + #
 53 + # run the cmd and format the log. return the exitint status of cmd
 54 + # use the global variables: tSTDOUT and tSTDERR to return the stdout and stderr
 55 + # usage: trun "ls"
 56 + #        trun "ls"; echo $?
 57 + #        stdout=$tSTDOUT
 58 + #        stderr=$tSTDERR
 59 + #
 60 + trun ()
 61 + {
 62 +     local cmd="$*"
 63 +     
 64 +     _trun_ "$cmd"
 65 + }
 66 + 
 67 + #
 68 + # verify the execution of command
 69 + # if the cmd return 0, mark this checkpoint failed and return 1
 70 + # if not, mark it passed and return 0
 71 + # usage: tnot "ls /not_existing"
 72 + #
 73 + tnot () {
 74 +     local cmd="$*"
 75 +     _trun_ "$cmd" 1
 76 +     if test $? -eq 0; then 
 77 +         tfail_ "$cmd" ;
 78 +     else
 79 +         tpass_ "$cmd" ;
 80 +     fi 
 81 + }
 82 + 
 83 + #
 84 + # verify the execution of command
 85 + # if the cmd return 0, mark this checkpoint passed and return 0
 86 + # if not, mark it failed and return 1
 87 + # usage: tok "ls /"
 88 + #
 89 + tok ()
 90 + {
 91 +     local cmd="$*"
 92 +     _trun_ "$cmd" 0
 93 +     if test $? -eq 0; then 
 94 +         tpass_ "$cmd" ;
 95 +     else
 96 +         tfail_ "$cmd" ;
 97 +     fi 
 98 + }
 99 + 
100 + #
101 + # verify the execution of command
102 + # if the cmd return 0, mark this checkpoint passed and return 0
103 + # if not, mark it failes and exit
104 + # usage: terr "ls"
105 + #
106 + #terr ()
107 + #{
108 + #    tok "$*" || tend
109 + #}
110 + 
111 + #
112 + # verify the execution of command
113 + # if the cmd return 0, will continue to run the script
114 + # if not, mark it failes and exit
115 + # usage: terr "ls"
116 + #
117 + terr ()
118 + {
119 +     local cmd="$*"
120 +     _trun_ "$cmd" 0
121 +     if test $? -ne 0; then 
122 +         tfail_ "$cmd" ;
123 +         tend ;
124 +     fi 
125 + }
126 + 
127 + #
128 + # exit the program and print the log message
129 + # usage: texit "error message" 100
130 + # similar to the exception 
131 + #
132 + texit ()
133 + {
134 +     msg=$1
135 +     err=$2
136 +     is_null $err && err=1
137 +     test $err -lt 1 || err=1 
138 + 
139 +     tlog "$msg" "ERROR"
140 +     exit $2 
141 + }
142 + 
143 + #
144 + # print the test report, cleanup the testing bed and  close the testing. 
145 + # usage: tend
146 + #
147 + tend ()
148 + {
149 +     local pcount=$(wc -l $tPASS_FILE | awk '{print $1}')
150 +     local fcount=$(wc -l $tFAIL_FILE | awk '{print $1}')
151 +     local total=$(( $pcount + $fcount ))
152 + 
153 +     echo "#################################Test Report###############################"
154 +     echo "TOTAL   : $total" 
155 +     echo "PASSED  : $pcount" 
156 +     echo "FAILED  : $fcount" 
157 +     cat $tPASS_FILE $tFAIL_FILE
158 +     echo "###########################End of running $0########################"
159 + 
160 + #cleanup
161 +     rm -f $tPASS_FILE $tFAIL_FILE $tRETURN_FILE $tSTDERR_FILE 
162 + #   rm -rf $LXT_TMP_DIR
163 + »       if [[ $pcount -eq 0 ]] && [[ $total -eq 0 ]];then
164 + »         exit 0
165 +     fi
166 +     test $pcount -eq 0 && exit 1
167 +     test $pcount -eq $total && exit 0 
168 +     exit 1
169 + }
170 + 
171 + #
172 + # private function
173 + #
174 + 
175 + #
176 + # print the error message and call stack. return 1
177 + #
178 + tfail_ ()
179 + {
180 +     local msg=$*
181 +     tlog "$msg" "ERROR" >>$tFAIL_FILE 
182 + 
183 +     return 1
184 + }
185 + 
186 + #
187 + # print the sucessful message. return 0
188 + #
189 + tpass_ ()
190 + {
191 +     local msg=$*
192 +     tlog "$msg" "PASS" >> $tPASS_FILE
193 + 
194 +     return 0
195 + }
196 + 
197 + _trun_ ()
198 + {
199 +     local cmd="$1"
200 +     local chk="$2"
201 +     local cur_date=$(tdate)
202 + 
203 +     local stdout=$(eval "$cmd" 2>$tSTDERR_FILE; echo $? >$tRETURN_FILE 2>/dev/null)
204 + #timeout -- how to set timeout?
205 +     local exit_status=$(< $tRETURN_FILE)
206 +     local stderr=$(< $tSTDERR_FILE)
207 +     local msg=CMD
208 + #tnot
209 +     if test x$chk = x1; then
210 +         test $exit_status -eq 0 || msg=PASS
211 +         test $exit_status -eq 0 && msg=FAIL
212 + #should let the tester know this is the negative testing
213 + #if cmd return 0 we will return 1 and vice versa
214 +         cmd="[NOT] $cmd"
215 +     fi
216 + #tok
217 +     if test x$chk = x0; then
218 +         test $exit_status -eq 0 && msg=PASS
219 +         test $exit_status -eq 0 || msg=FAIL
220 +     fi
221 + 
222 +     tSTDOUT=$stdout
223 +     tSTDERR=$stderr
224 + 
225 +     test $tIGNORE_STDOUT -eq 1 && stdout='redirect the stdout to /dev/null' 
226 +     test $tIGNORE_STDERR -eq 1 && stderr='redirect the stderr to /dev/null' 
227 + 
228 +     echo "[$msg][$cur_date][$HOSTNAME]$cmd" 
229 +     echo "STDOUT:"
230 +     test "x$stdout" = x || echo "$stdout"
231 +     echo "STDERR:$stderr"
232 +     echo "RETURN:$exit_status"
233 +     echo
234 + 
235 +     return $exit_status
236 + }
237 + 
238 + #
239 + # setup the testing environment
240 + #
241 + _tsetup_ ()
242 + {
243 + 
244 +     LXT_TMP_DIR="/mnt/testarea/lxt";
245 + 
246 +     test -z "$HOSTNAME" && HOSTNAME=$(hostname)
247 +     test -d "$LXT_TMP_DIR" || mkdir -p "$LXT_TMP_DIR" >& /dev/null || exit 1
248 + 
249 +     tSTDERR_FILE="$LXT_TMP_DIR/stderr.$$"
250 +     test -e "$tSTDERR_FILE" || > "$tSTDERR_FILE" || exit 1
251 +     tRETURN_FILE="$LXT_TMP_DIR/return.$$"
252 +     test -e "$tRETURN_FILE" || > "$tRETURN_FILE" || exit 1
253 +     tPASS_FILE="$LXT_TMP_DIR/tc.pass.$$"
254 +     test -e "$tPASS_FILE" || > "$tPASS_FILE" || exit 1
255 +     tFAIL_FILE="$LXT_TMP_DIR/tc.fail.$$"
256 +     test -e "$tFAIL_FILE" || > "$tFAIL_FILE" || exit 1
257 + }
258 + 
259 + #
260 + # main
261 + #
262 + 
263 + # global variables
264 + tIGNORE_STDOUT=0
265 + tIGNORE_STDERR=0
266 + tSTDOUT=
267 + tSTDERR=
268 + #LXT_TMP_DIR
269 + # only used in this file
270 + tPASS_FILE=
271 + tFAIL_FILE=
272 + 
273 + _tsetup_
  1 @@ -0,0 +1,188 @@
  2 + #!/bin/bash
  3 + 
  4 + # Copyright (c) 2016 Red Hat, Inc. All rights reserved.
  5 + #
  6 + # This program is free software: you can redistribute it and/or modify
  7 + # it under the terms of the GNU General Public License as published by
  8 + # the Free Software Foundation, either version 3 of the License, or
  9 + # (at your option) any later version.
 10 + #
 11 + # This program is distributed in the hope that it will be useful,
 12 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 + # GNU General Public License for more details.
 15 + #
 16 + # You should have received a copy of the GNU General Public License
 17 + # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 + 
 19 + # Author: Lin Li   <lilin@redhat.com>
 20 + 
 21 + 
 22 + #summary of this script:
 23 + #   this script is used to provide the generai utils for the automation script based on shell
 24 + #   all the global variables should be upper case and began with under line since this script will be used by many scripts
 25 + #   and we don't want the their value is be override by mistake
 26 + 
 27 + 
 28 + ###############################global variables######################################
 29 + 
 30 + #the global variables should begin with under line plus some special keyword??
 31 + #global variables, can be used by other scripts
 32 + #the stdout and stderr of the cmd execution in Cmd function
 33 + #can get the exit status of the Cmd function by $?
 34 + _STDOUT=
 35 + _STDERR=
 36 + #if set to 1 will not print the stdout and stderr in the function Cmd
 37 + #should set them to 0 after using it
 38 + _IGNORE_STDOUT=0
 39 + _IGNORE_STDERR=0
 40 + 
 41 + #current hostname, same as the global variable HOSTNAME
 42 + #don't use readonly since we maybe source this file many times in the same shell environment
 43 + [ -z "$HOSTNAME" ] && HOSTNAME=$(hostname)
 44 + 
 45 + #private global variables, only used in current script
 46 + #temp dir and files used by Cmd to capture the stderr and exit status of cmd execution
 47 + _TEMP_DIR="/tmp/lstf"
 48 + [ -d $_TEMP_DIR ] || mkdir -p $_TEMP_DIR >& /dev/null ||  Fail "failed to mkdir $_TEMP_DIR" 
 49 + 
 50 + _TEMP_STDERR_FILE="$_TEMP_DIR/stderr.$$"
 51 + [ -e $_TEMP_STDERR_FILE ] || touch $_TEMP_STDERR_FILE || Fail "failed to create $_TEMP_STDERR_FILE"
 52 + _TEMP_RETURN_FILE="$_TEMP_DIR/return.$$"
 53 + [ -e $_TEMP_RETURN_FILE ] || touch $_TEMP_RETURN_FILE || Fail "failed to create $_TEMP_RETURN_FILE"
 54 + 
 55 + ###############################################################################################
 56 + 
 57 + #private function
 58 + #print the formated date string
 59 + 
 60 + function _date_str (){
 61 +     date 2>/dev/null
 62 + }
 63 + 
 64 + ###############################################################################################
 65 + #summary:
 66 + #   eval the paramters and format the output
 67 + #   don't support stderr redirection in the passed command 
 68 + #usage:
 69 + #   Cmd ls -a
 70 + #   if you don't want to print the stdout or stderr you can set the global variables _IGNORE_STDOUT and _IGNORE_STDERR, for example
 71 + #   _IGNORE_STDOUT=1  
 72 + #   Cmd ls -a
 73 + #   _IGNORE_STDOUT=0
 74 + #return:
 75 + #   return the exit status of paramter value execution
 76 + #   store the stdout and stderr to the global variables _STDOUT and _STDERR
 77 + ###############################################################################################
 78 + 
 79 + function Cmd (){
 80 + #use the double qutoa to store all the arguments as a single string
 81 +     local cmd="$*"
 82 +     local date_str=$(_date_str)
 83 +     local stdout=$(eval $cmd 2>$_TEMP_STDERR_FILE; echo $? >$_TEMP_RETURN_FILE 2>/dev/null)
 84 + 
 85 + #why cannot get the exit status of the value of eval by this ?
 86 + #    local exit_status=$?
 87 + #remove the \n hence don't use this method
 88 + #    local exit_status=$(cat $_TEMP_RETURN_FILE)
 89 + 
 90 +     local exit_status=$(< $_TEMP_RETURN_FILE)
 91 +     local stderr=$(< $_TEMP_STDERR_FILE)
 92 + 
 93 +     _STDOUT=$stdout
 94 +     _STDERR=$stderr
 95 + 
 96 + #will not print the stdout and stderr if the 2 global variables set to 1
 97 +     [ $_IGNORE_STDOUT -eq 1 ] && stdout='redirect the stdout to /dev/null' 
 98 +     [ $_IGNORE_STDERR -eq 1 ] && stderr='redirect the stderr to /dev/null' 
 99 + 
100 +     echo "[CMD][$date_str][$HOSTNAME]#$cmd" 
101 +     echo "STDOUT:$stdout"
102 +     echo "STDERR:$stderr"
103 +     echo "RETURN:$exit_status"
104 +     echo
105 + 
106 +     return $exit_status
107 + }
108 + 
109 + ###############################################################################################
110 + #summary: 
111 + #   print the formated log
112 + #   consider filter the output through the level
113 + #   level should be INFO, ERROR, WARNING, PASSED
114 + #usage:
115 + #   Log "msg" "level"
116 + #   Log "msg"
117 + #  don't use it like this : Log msg level
118 + #return:
119 + #   0
120 + ###############################################################################################
121 + 
122 + #should redirect io to stderr if log_level is error?
123 + 
124 + function Log (){
125 +     local msg=$1
126 +     local log_level=${2:-INFO}
127 +     local date_str=$(_date_str)
128 + 
129 +     echo "[$log_level][$date_str]:$msg"
130 +     echo 
131 +     
132 +     return 0
133 + }
134 + 
135 + ###############################################################################################
136 + #summary: 
137 + #   print the formated error message, call stack information and exit the program with 1
138 + #usage:
139 + #   Fail error_msg
140 + #exit:
141 + #   1
142 + ###############################################################################################
143 + 
144 + function Fail (){
145 +     local msg=$*
146 +     local stack=`caller 0`
147 + 
148 +     Log "$stack"
149 +     Log "$msg" "ERROR"
150 + 
151 +     exit 1
152 + }
153 + 
154 + ###############################################################################################
155 + #summary: 
156 + #   print the formated passed message and exit the program with 0
157 + #usage:
158 + #   Pass 
159 + #exit:
160 + #   0
161 + ###############################################################################################
162 + 
163 + function Pass (){
164 +     Log "Test case passed" "PASSED"
165 +     
166 +     exit 0
167 + }
168 + 
169 + ###############################################################################################
170 + #summary: 
171 + #   assert the expression, 
172 + #   if the exiting status of the expression is not 0 fail the case and print the failed message
173 + #   if 0 do nothing just return 0
174 + #usage:
175 + #   Assert "$exit_status -eq 0" "failed string" 
176 + #   Assert "ls /dev/sdb" "failed string" 
177 + #return/exit:
178 + #   the exiting value of expression 
179 + ###############################################################################################
180 + 
181 + function Assert (){
182 + #should consider the expr is integra or expression, script?
183 +     local expr=$1
184 +     local failed_msg=$2
185 +     [ $expr ] || Fail "$failed_msg" 
186 + 
187 +     return 0
188 + }
189 +