-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_unit.sh
executable file
·260 lines (218 loc) · 8.39 KB
/
test_unit.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
# ------------------------------------------------------------------------------
# OraDBA - Oracle Database Infrastructur and Security, 5630 Muri, Switzerland
# ------------------------------------------------------------------------------
# Name.......: test_unit.sh
# Author.....: Stefan Oehrli (oes) [email protected]
# Editor.....: Stefan Oehrli
# Date.......: 2023.05.04
# Version....: v3.4.8
# Purpose....: Script to test / verify all TNS utilities
# Notes......: --
# Reference..: --
# License....: Apache License Version 2.0, January 2004 as shown
# at http://www.apache.org/licenses/
# ------------------------------------------------------------------------------
# - Customization --------------------------------------------------------------
# - just add/update any kind of customized environment variable here
# - End of Customization -------------------------------------------------------
# Define a bunch of bash option see
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
# https://www.davidpashley.com/articles/writing-robust-shell-scripts/
set -o nounset # exit if script try to use an uninitialised variable
# set -o errexit # exit script if any statement returns a non-true return value
# set -o pipefail # pipefail exit after 1st piped commands failed
set -o noglob # Disable filename expansion (globbing).
# - Environment Variables ------------------------------------------------------
# define generic environment variables
VERSION=v3.4.8
TVDLDAP_VERBOSE=${TVDLDAP_VERBOSE:-"FALSE"} # enable verbose mode
TVDLDAP_DEBUG=${TVDLDAP_DEBUG:-"FALSE"} # enable debug mode
TVDLDAP_QUIET=${TVDLDAP_QUIET:-"FALSE"} # enable quiet mode
TVDLDAP_SCRIPT_NAME=$(basename ${BASH_SOURCE[0]})
TVDLDAP_BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TVDLDAP_LOG_DIR="$(dirname ${TVDLDAP_BIN_DIR})/log"
# define logfile and logging
LOG_BASE=${LOG_BASE:-"${TVDLDAP_LOG_DIR}"} # Use script log directory as default logbase
TIMESTAMP=$(date "+%Y.%m.%d_%H%M%S")
readonly LOGFILE="$LOG_BASE/$(basename $TVDLDAP_SCRIPT_NAME .sh)_$TIMESTAMP.log"
# define tempfile for ldapsearch
TEMPFILE="$LOG_BASE/$(basename $TVDLDAP_SCRIPT_NAME .sh)_$$.ldif"
# Define the color for the output
export INFO="\e[98m%b\e[0m"
export SUCCESS="\e[92m%b\e[0m"
export WARNING="\e[93m%b\e[0m"
export ERROR="\e[91m%b\e[0m"
export TNS_RED="\e[91m%b\e[0m"
export TNS_GREED="\e[92m%b\e[0m"
export TNS_YELLOW="\e[93m%b\e[0m"
export TNS_PURPLE="\e[94m%b\e[0m"
export TNS_MAGENTA="\e[95m%b\e[0m"
export TNS_CYAN="\e[96m%b\e[0m"
export TNS_WHITE="\e[97m%b\e[0m"
export TNS_BLACK="\e[98m%b\e[0m"
export TNS_OTHER_BLACK="\e[99m%b\e[0m"
# - EOF Environment Variables --------------------------------------------------
# - Functions ------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Function...: Usage
# Purpose....: Display Usage and exit script
# ------------------------------------------------------------------------------
Usage() {
# define default values for arguments
error=${1:-"0"} # default error number
error_value=${2:-""} # default error message
cat << EOI
Usage: ${TVDLDAP_SCRIPT_NAME} [options]
where:
services Comma separated list of Oracle Net Service Names to test
Common Options:
-m Usage this message
-v Enable verbose mode (default \$TVDLDAP_VERBOSE=${TVDLDAP_VERBOSE})
-d Enable debug mode (default \$TVDLDAP_DEBUG=${TVDLDAP_DEBUG})
$((get_list_of_config && echo "Command line parameter")|cat -b)
Logfile : ${LOGFILE}
EOI
exit $error
}
# ------------------------------------------------------------------------------
# Function...: ok
# Purpose....: echo OK
# ------------------------------------------------------------------------------
ok() { printf ${INFO:-"\e[98m%b\e[0m"}'\n' "OK" ; }
# ------------------------------------------------------------------------------
# Function...: nok
# Purpose....: echo Not OK
# ------------------------------------------------------------------------------
nok() { printf ${ERROR:-"\e[92m%b\e[0m"}'\n' "Not OK" ; }
# ------------------------------------------------------------------------------
# Function...: nok
# Purpose....: echo Not OK
# ------------------------------------------------------------------------------
display(){
status=${1:-""}
message=${2:-""}
value=${3:-""}
padding='...............................................................................................'
if [ -z $value ]; then
printf "%s : %s %s %s" "${status^^}" "${message}" "${padding:${#message}}" "${value}"
else
value=${value//\\n/}
printf "%s : %s %s\n" "${status^^}" "${message}" "${value}"
fi
}
function try()
{
[[ $- = *e* ]]; SAVED_OPT_E=$?
set +e
}
function throw()
{
exit $1
}
function catch()
{
export exception_code=$?
(( $SAVED_OPT_E )) && set +e
return $exception_code
}
# - EOF Functions --------------------------------------------------------------
# - Initialization -------------------------------------------------------------
touch $LOGFILE 2>/dev/null # initialize logfile
exec &> >(tee -a "$LOGFILE") # Open standard out at `$LOG_FILE` for write.
exec 2>&1
printf $TNS_INFO'\n' "INFO : Start ${TVDLDAP_SCRIPT_NAME} on host $(hostname) at $(date)"
# initialize tempfile for the script
touch $TEMPFILE 2>/dev/null || exit 25
printf $INFO'\n' "INFO : touch logfile $LOGFILE"
printf $INFO'\n' "INFO : touch tempfile $TEMPFILE"
# get options
while getopts mvdE: CurOpt; do
case ${CurOpt} in
m) Usage 0;;
v) TVDLDAP_VERBOSE="TRUE" ;;
d) TVDLDAP_DEBUG="TRUE" ;;
E) clean_quit "${OPTARG}";;
*) Usage 2 $*;;
esac
done
display "INFO" "load file ${TVDLDAP_BIN_DIR}/tns_functions.sh"
if [ -f ${TVDLDAP_BIN_DIR}/tns_functions.sh ]; then . ${TVDLDAP_BIN_DIR}/tns_functions.sh; ok; else nok; fi
display "INFO" "check functions" "..."
display "INFO" "source_env"
if ! source_env 2>$LOGFILE ; then nok ; else ok ; fi
display "INFO" "load_config"
if ! load_config 2>$LOGFILE ; then nok ; else ok ; fi
display "INFO" "check_tools"
if ! check_tools 2>$LOGFILE ; then nok ; else ok ; fi
display "INFO" "check_ldap_tools"
if ! check_ldap_tools 2>$LOGFILE ; then nok ; else ok ; fi
display "INFO" "clean_quit"
try
(
clean_quit 99 >$LOGFILE 2>&1
)
catch || {
case $exception_code in
99) ok;;
*) nok;;
esac
}
display "INFO" "check_ldap_tools"
if ! check_ldap_tools 2>$LOGFILE ; then nok ; else ok ; fi
display "INFO" "get_basedn"
if ! get_local_basedn >$LOGFILE 2>&1 ; then nok ; else ok ; fi
display "INFO" "get_local_basedn"
if ! get_local_basedn >$LOGFILE 2>&1 ; then nok ; else ok ; fi
display "INFO" "get_all_basedn"
if ! get_all_basedn >$LOGFILE 2>&1 ; then nok ; else ok ; fi
display "INFO" "get_ldaphost"
if ! get_ldaphost >$LOGFILE 2>&1 ; then nok ; else ok ; fi
display "INFO" "get_ldapport"
if ! get_ldapport >$LOGFILE 2>&1 ; then nok ; else ok ; fi
display "INFO" "rotate_logfiles"
if ! rotate_logfiles 2>$LOGFILE ; then nok ; else ok ; fi
# alias_enabled
# ask_bindpwd
# basedn_exists
# bulk_enabled
# check_bind_parameter
# check_openldap_tools
# clean_quit
# command_exists
# dryrun_enabled
# dump_runtime_config
# echo_debug
# echo_secret
# echo_stderr
# force_enabled
# get_binddn_param
# get_bindpwd_param
# join_dotora
# ldapadd_command
# ldapadd_options
# ldapmodify_options
# ldapsearch_options
# net_service_exists
# on_int
# on_term
# split_net_service_basedn
# split_net_service_cn
# tidy_dotora
display "INFO" "check scripts" "..."
display "INFO" "tns_add.sh"
try
(
tns_add.sh -S DUMMY.epu.corpintra.net \
-N "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.1.1.12)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=TDB02.trivadislabs.com))(UR=A))" || throw 1
)
catch || {
case $exception_code in
1) nok;;
*) ok;;
esac
}
if ! tns_add.sh >$LOGFILE 2>&1 ; then nok ; else ok ; fi
rotate_logfiles # purge log files based on TVDLDAP_KEEP_LOG_DAYS
clean_quit # clean exit with return code 0
# --- EOF ----------------------------------------------------------------------