summaryrefslogtreecommitdiffstats
path: root/tests/include/utils.sh
blob: 404c3e8487d5383bb520f532afd16b6fc6eb5afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash

# Copyright (c) 2016 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Author: Lin Li   <lilin@redhat.com>


#summary of this script:
#   this script is used to provide the generai utils for the automation script based on shell
#   all the global variables should be upper case and began with under line since this script will be used by many scripts
#   and we don't want the their value is be override by mistake


###############################global variables######################################

#the global variables should begin with under line plus some special keyword??
#global variables, can be used by other scripts
#the stdout and stderr of the cmd execution in Cmd function
#can get the exit status of the Cmd function by $?
_STDOUT=
_STDERR=
#if set to 1 will not print the stdout and stderr in the function Cmd
#should set them to 0 after using it
_IGNORE_STDOUT=0
_IGNORE_STDERR=0

#current hostname, same as the global variable HOSTNAME
#don't use readonly since we maybe source this file many times in the same shell environment
[ -z "$HOSTNAME" ] && HOSTNAME=$(hostname)

#private global variables, only used in current script
#temp dir and files used by Cmd to capture the stderr and exit status of cmd execution
_TEMP_DIR="/tmp/lstf"
[ -d $_TEMP_DIR ] || mkdir -p $_TEMP_DIR >& /dev/null ||  Fail "failed to mkdir $_TEMP_DIR" 

_TEMP_STDERR_FILE="$_TEMP_DIR/stderr.$$"
[ -e $_TEMP_STDERR_FILE ] || touch $_TEMP_STDERR_FILE || Fail "failed to create $_TEMP_STDERR_FILE"
_TEMP_RETURN_FILE="$_TEMP_DIR/return.$$"
[ -e $_TEMP_RETURN_FILE ] || touch $_TEMP_RETURN_FILE || Fail "failed to create $_TEMP_RETURN_FILE"

###############################################################################################

#private function
#print the formated date string

function _date_str (){
    date 2>/dev/null
}

###############################################################################################
#summary:
#   eval the paramters and format the output
#   don't support stderr redirection in the passed command 
#usage:
#   Cmd ls -a
#   if you don't want to print the stdout or stderr you can set the global variables _IGNORE_STDOUT and _IGNORE_STDERR, for example
#   _IGNORE_STDOUT=1  
#   Cmd ls -a
#   _IGNORE_STDOUT=0
#return:
#   return the exit status of paramter value execution
#   store the stdout and stderr to the global variables _STDOUT and _STDERR
###############################################################################################

function Cmd (){
#use the double qutoa to store all the arguments as a single string
    local cmd="$*"
    local date_str=$(_date_str)
    local stdout=$(eval $cmd 2>$_TEMP_STDERR_FILE; echo $? >$_TEMP_RETURN_FILE 2>/dev/null)

#why cannot get the exit status of the value of eval by this ?
#    local exit_status=$?
#remove the \n hence don't use this method
#    local exit_status=$(cat $_TEMP_RETURN_FILE)

    local exit_status=$(< $_TEMP_RETURN_FILE)
    local stderr=$(< $_TEMP_STDERR_FILE)

    _STDOUT=$stdout
    _STDERR=$stderr

#will not print the stdout and stderr if the 2 global variables set to 1
    [ $_IGNORE_STDOUT -eq 1 ] && stdout='redirect the stdout to /dev/null' 
    [ $_IGNORE_STDERR -eq 1 ] && stderr='redirect the stderr to /dev/null' 

    echo "[CMD][$date_str][$HOSTNAME]#$cmd" 
    echo "STDOUT:$stdout"
    echo "STDERR:$stderr"
    echo "RETURN:$exit_status"
    echo

    return $exit_status
}

###############################################################################################
#summary: 
#   print the formated log
#   consider filter the output through the level
#   level should be INFO, ERROR, WARNING, PASSED
#usage:
#   Log "msg" "level"
#   Log "msg"
#  don't use it like this : Log msg level
#return:
#   0
###############################################################################################

#should redirect io to stderr if log_level is error?

function Log (){
    local msg=$1
    local log_level=${2:-INFO}
    local date_str=$(_date_str)

    echo "[$log_level][$date_str]:$msg"
    echo 
    
    return 0
}

###############################################################################################
#summary: 
#   print the formated error message, call stack information and exit the program with 1
#usage:
#   Fail error_msg
#exit:
#   1
###############################################################################################

function Fail (){
    local msg=$*
    local stack=`caller 0`

    Log "$stack"
    Log "$msg" "ERROR"

    exit 1
}

###############################################################################################
#summary: 
#   print the formated passed message and exit the program with 0
#usage:
#   Pass 
#exit:
#   0
###############################################################################################

function Pass (){
    Log "Test case passed" "PASSED"
    
    exit 0
}

###############################################################################################
#summary: 
#   assert the expression, 
#   if the exiting status of the expression is not 0 fail the case and print the failed message
#   if 0 do nothing just return 0
#usage:
#   Assert "$exit_status -eq 0" "failed string" 
#   Assert "ls /dev/sdb" "failed string" 
#return/exit:
#   the exiting value of expression 
###############################################################################################

function Assert (){
#should consider the expr is integra or expression, script?
    local expr=$1
    local failed_msg=$2
    [ $expr ] || Fail "$failed_msg" 

    return 0
}

is_null()
{
    local string=$1
    string=$(echo $string | sed 's/\s//g')
    test -z $string
}