-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_robot_test.sh
executable file
·195 lines (173 loc) · 6.54 KB
/
run_robot_test.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
#!/usr/bin/env bash
set -eux
# -------- Global Variables --------
TEST_CASE_LOCATION=tests
TEST_CASE_FILE=tests
TEST_VARIABLES_FILE=test-variables.yaml
TEST_VARIABLES=""
TEST_ARTIFACT_DIR="test-output"
EXTRA_ROBOT_ARGS=""
TEST_INCLUDE_TAG=""
TEST_EXCLUDE_TAG=""
SKIP_PIP_INSTALL=0
SKIP_VENV_CREATE=0
JENKINS_BUILD_NUMBER=${BUILD_NUMBER:-666}
# -------- functions -------------
override_url_variables() {
# Overrides Openshift URL variables with Current Openshift URL values
echo "Retrieving Openshift Console & API Server URL Values"
console_url=$(oc whoami --show-console)
api_server=$(oc whoami --show-server)
TEST_VARIABLES="${TEST_VARIABLES} --variable OCP_CONSOLE_URL:${console_url} --variable OCP_API_URL:${api_server}"
}
handle_inputs() {
SPECIFIED_INPUTS=$@
OPTSTR="d:e:f:hi:r:st:uv:x"
while getopts $OPTSTR flag; do
case "$flag" in
d) # Specify directory to store artifacts and reports from each test run
TEST_ARTIFACT_DIR=$OPTARG
;;
e) # Specify excluded tags
TEST_EXCLUDE_TAG=$OPTARG
EXTRA_ROBOT_ARGS="${EXTRA_ROBOT_ARGS} -e ${TEST_EXCLUDE_TAG}"
;;
f) # Specify the test variable file
TEST_VARIABLES_FILE=$OPTARG
;;
h) # Display Usage and exit
disp_usage $OPTSTR
exit 0
;;
i) # Specify included tags
# Example: sanityANDinstall sanityORinstall installNOTsanity
TEST_INCLUDE_TAG=$OPTARG
EXTRA_ROBOT_ARGS="${EXTRA_ROBOT_ARGS} -i ${TEST_INCLUDE_TAG}"
;;
r) # Additional arguments to pass to the robot cli
EXTRA_ROBOT_ARGS="${EXTRA_ROBOT_ARGS} $OPTARG"
;;
s) # Skip the pip install during the execution of this script
SKIP_PIP_INSTALL=1
;;
t) # Specify test case to run
TEST_CASE_FILE=$TEST_CASE_LOCATION/$OPTARG
;;
u) # Update existing Python VirtualEnv or Create new one
SKIP_VENV_CREATE=1
;;
v) # Override/Add global variables specified in the test variables file
TEST_VARIABLES="${TEST_VARIABLES} --variable $OPTARG"
;;
x) # Override variable file
override_url_variables
;;
*)
echo "Invalid Input"
disp_usage $OPTSTR
exit 1
;;
esac
done
shift $((OPTIND -1))
# ------ Validate Inputs --------
if [[ ! -f "${TEST_VARIABLES_FILE}" ]]; then
echo "---- Robot Framework test variable file (test-variables.yaml) is missing"
exit 1
fi
}
disp_usage() {
echo
echo "Usage: $0 [$1]"
echo "Possible inputs: -d -e -f [-h] -i -r [-s] -t -u -v -x"
echo "Specified Inputs: $SPECIFIED_INPUTS"
echo "Explanation of inputs "
echo " -d <TEST_ARTIFACT_DIR>: Specify directory to store artifacts and reports from each test run"
echo " -e <TEST_EXCLUDE_TAG>: Specify excluded tags for Robot"
echo " -f <TEST_VARIABLES_FILE>: Specify the test variable file"
echo " -h: Display Usage and exit"
echo " -i <TEST_INCLUDE_TAG>: Specify included tags for Robot"
echo " -r <EXTRA_ROBOT_ARGS>: Additional arguments to pass to the robot cli"
echo " -s: Skip the installation of required Python Libraries"
echo " -t <TEST_CASE_FILE>: Specify test case to run, should be present under tests directory"
echo " -u: Update/Create Python VirtualEnv"
echo " -v <>: Override/Add global variables specified in the test variables file"
echo " -x: Overrides Openshift URL Values with Hosting Openshift"
}
setup_venv() {
# This check is to ensure we are not creating/updating the virtualenv everytime we run a test
echo "--------- Setting up Python VirtualEnv ----------"
VENV_ROOT=${PWD}/venv
cmd="${VENV_ROOT}/bin/python3 --version"
if (! $cmd &> /dev/null) || [[ $SKIP_VENV_CREATE -ne 0 ]]; then
echo "Creating/Updating VirtualEnv"
# setup virtualenv
python3 -m venv "${VENV_ROOT}"
fi
if [[ $SKIP_PIP_INSTALL -eq 0 ]]; then
"${VENV_ROOT}"/bin/pip3 install --upgrade pip
"${VENV_ROOT}"/bin/pip3 install -r requirements.txt
fi
}
create_artifact_dir() {
# Create a unique directory to store the output for current test run
case "$(uname -s)" in
Darwin)
TEST_ARTIFACT_DIR="${TEST_ARTIFACT_DIR}/rhoda-ci-$(date +%Y-%m-%d-%H-%M)-XXXX"
;;
Linux)
TEST_ARTIFACT_DIR="${TEST_ARTIFACT_DIR}/rhoda-${JENKINS_BUILD_NUMBER}-$(date +%Y-%m-%d-%M%S)"
;;
esac
mkdir -p "${TEST_ARTIFACT_DIR}/results"
mkdir -p "${TEST_ARTIFACT_DIR}/attachments"
RP_PAYLOAD_RESULT_DIR="${TEST_ARTIFACT_DIR}/results"
RP_PAYLOAD_ATTACH_DIR="${TEST_ARTIFACT_DIR}/attachments"
}
post_run_action(){
# To Categorize the output results for Report portal
cp ${RP_PAYLOAD_ATTACH_DIR}/xunit_test_result.xml ${RP_PAYLOAD_RESULT_DIR}
echo ${TEST_ARTIFACT_DIR}>result_dir
}
echo "------------- STARTING ${0}"
handle_inputs "$@"
setup_venv
create_artifact_dir
case "$(uname -s)" in
Darwin)
echo "MACOS"
echo "setting driver to $PWD/drivers/MACOS"
export PATH=$PATH:$PWD/drivers/MACOS
echo "$PATH"
;;
Linux)
case "$(lsb_release --id --short)" in
"Fedora"|"CentOS")
## Bootstrap script to setup drivers ##
echo "setting driver to $PWD/drivers/fedora"
export PATH=$PATH:$PWD/drivers/fedora
echo "$PATH"
;;
"Ubuntu")
echo "Not yet supported, but shouldn't be hard for you to fix :) "
echo "Please add the driver, test and submit PR"
exit 1
;;
"openSUSE project"|"SUSE LINUX"|"openSUSE")
echo "Not yet supported, but shouldn't be hard for you to fix :) "
echo "Please add the driver, test and submit PR"
exit 1
;;
esac
;;
* )
echo "Not yet supported OS, but shouldn't be hard for you to fix :) "
echo "Please add the driver, test and submit PR"
exit 1
;;
esac
set +e
./venv/bin/robot ${EXTRA_ROBOT_ARGS} -d ${RP_PAYLOAD_ATTACH_DIR} -x xunit_test_result.xml -r test_report.html ${TEST_VARIABLES} --variablefile ${TEST_VARIABLES_FILE} ${TEST_CASE_FILE}
set -e
post_run_action
echo "------------- END ${0}"