Add raw image support for lxd scenario
[yardstick.git] / tests / ci / yardstick-verify
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 #
12 # Set up the environment and run yardstick test suites.
13 #
14 # Example invocation: yardstick-verify -r 10.4.4.4 suite1.yaml suite2.yaml
15 #
16 # Openstack credentials must be set and the script must be run from its
17 # original location in the yardstick repo.
18 #
19 # This script is intended to be used by the CI pipeline but it may also
20 # be invoked manually.
21 #
22
23 SCRIPT=$0
24 SCRIPT_ARGS=$@
25
26 usage()
27 {
28     cat << EOF
29 usage: $0 options [TEST_SUITE ...]
30
31 If no test suites are given ping.yaml is run.
32
33 OPTIONS:
34    -h      Show this message
35    -r      Http target (example: -r 213.77.62.197/results)
36    -i      Influxdb target (example: -i 127.0.0.1:8086)
37
38            Default target is dump to file ($DISPATCHER_FILE_NAME)
39
40 EOF
41 }
42
43 DISPATCHER_TYPE=file
44 DISPATCHER_FILE_NAME="/tmp/yardstick.out"
45 DISPATCHER_HTTP_TARGET="http://testresults.opnfv.org/test/api/v1/results"
46 DISPATCHER_INFLUXDB_TARGET=
47 UCA_HOST="cloud-images.ubuntu.com"
48 if [ $YARD_IMG_ARCH = "arm64" ]; then
49     export VIVID_IMG_URL="http://${UCA_HOST}/vivid/current/vivid-server-cloudimg-arm64.tar.gz"
50     if ! grep -q "Defaults env_keep += \"VIVID_IMG_URL\"" "/etc/sudoers"; then
51         sudo echo "Defaults env_keep += \"VIVID_IMG_URL\"" >> /etc/sudoers
52     fi
53 fi
54 while getopts "r:i:h" OPTION; do
55     case $OPTION in
56         h)
57             usage
58             exit 0
59             ;;
60         r)
61             DISPATCHER_TYPE=http
62             DISPATCHER_HTTP_TARGET=http://${OPTARG}
63             DISPATCHER_FILE_NAME=
64             ;;
65         i)
66             DISPATCHER_TYPE=influxdb
67             DISPATCHER_INFLUXDB_TARGET=http://${OPTARG}
68             DISPATCHER_FILE_NAME=
69             ;;
70         *)
71             echo "${OPTION} is not a valid argument"
72             exit 1
73             ;;
74     esac
75 done
76
77 shift $[OPTIND - 1]
78 TEST_SUITES=$@
79
80 cleanup()
81 {
82     echo
83     echo "========== Cleanup =========="
84
85     if ! glance image-list; then
86         return
87     fi
88
89     for image in $(glance image-list | grep -e cirros-0.3.3 -e yardstick-trusty-server -e Ubuntu-14.04 \
90         -e yardstick-vivid-kernel | awk '{print $2}'); do
91         echo "Deleting image $image..."
92         glance image-delete $image || true
93     done
94
95     nova flavor-delete yardstick-flavor &> /dev/null || true
96 }
97
98 exitcode=""
99
100 error_exit()
101 {
102     local rc=$?
103
104     if [ -z "$exitcode" ]; then
105         # In case of recursive traps (!?)
106         exitcode=$rc
107     fi
108
109     cleanup
110
111     echo "Exiting with RC=$exitcode"
112
113     exit $exitcode
114 }
115
116 set -o errexit
117 set -o pipefail
118
119 install_yardstick()
120 {
121     echo
122     echo "========== Installing yardstick =========="
123
124     # uninstall previous version
125     pip uninstall -y yardstick || true
126
127     # Install yardstick
128     pip install .
129 }
130
131 install_storperf()
132 {
133     # Install Storper on huawei-pod1
134     if [ "$NODE_NAME" == "huawei-pod1" ]; then
135         echo
136         echo "========== Installing storperf =========="
137
138         if ! yardstick -d plugin install plugin/CI/storperf.yaml; then
139             echo "Install storperf plugin FAILED";
140             exit 1
141         fi
142
143     fi
144 }
145
146 remove_storperf()
147 {
148     # remove Storper from huawei-pod1
149     if [ "$NODE_NAME" == "huawei-pod1" ]; then
150         echo
151         echo "========== Removing storperf =========="
152
153         if ! yardstick -d plugin remove plugin/CI/storperf.yaml; then
154             echo "Remove storperf plugin FAILED";
155             exit 1
156         fi
157
158     fi
159 }
160
161 build_yardstick_image()
162 {
163     echo
164     echo "========== Build yardstick cloud image =========="
165
166     local cmd="sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
167
168     # Build the image. Retry once if the build fails.
169     $cmd || $cmd
170
171     if [ ! -f $QCOW_IMAGE ]; then
172         echo "Failed building QCOW image"
173         exit 1
174     fi
175 }
176
177 create_nova_flavor()
178 {
179     if ! nova flavor-list | grep -q yardstick-flavor; then
180         echo
181         echo "========== Create nova flavor =========="
182         # Create the nova flavor used by some sample test cases
183         nova flavor-create yardstick-flavor 100 512 3 1
184         # DPDK-enabled OVS requires guest memory to be backed by large pages
185         if [[ "$DEPLOY_SCENARIO" == *"-ovs-"* ]]; then
186             nova flavor-key yardstick-flavor set hw:mem_page_size=large
187         fi
188     fi
189 }
190
191 load_cirros_image()
192 {
193     echo
194     echo "========== Loading cirros cloud image =========="
195
196     local image_file=/home/opnfv/images/cirros-0.3.3-x86_64-disk.img
197
198     output=$(glance image-create \
199         --name  cirros-0.3.3 \
200         --disk-format qcow2 \
201         --container-format bare \
202         --file $image_file)
203     echo "$output"
204
205     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
206     if [ -z "$CIRROS_IMAGE_ID" ]; then
207         echo 'Failed uploading cirros image to cloud'.
208         exit 1
209     fi
210
211     echo "Cirros image id: $CIRROS_IMAGE_ID"
212 }
213
214 load_ubuntu_image()
215 {
216     echo
217     echo "========== Loading ubuntu cloud image =========="
218
219     local ubuntu_image_file=/home/opnfv/images/trusty-server-cloudimg-amd64-disk1.img
220
221     output=$(glance image-create \
222         --name Ubuntu-14.04 \
223         --disk-format qcow2 \
224         --container-format bare \
225         --file $ubuntu_image_file)
226     echo "$output"
227
228     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
229
230     if [ -z "$UBUNTU_IMAGE_ID" ]; then
231         echo 'Failed uploading UBUNTU image to cloud'.
232         exit 1
233     fi
234
235     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
236 }
237
238 load_yardstick_image()
239 {
240     echo
241     echo "========== Loading yardstick cloud image =========="
242     EXTRA_PARAMS=""
243     if [ $YARD_IMG_ARCH = "arm64" ]; then
244         VIVID_IMAGE="/tmp/vivid-server-cloudimg-arm64.tar.gz"
245         VIVID_KERNEL="/tmp/vivid-server-cloudimg-arm64-vmlinuz-generic"
246         cd /tmp
247         if [ ! -f $VIVID_IMAGE ]; then
248             wget $VIVID_IMG_URL
249         fi
250         if [ ! -f $VIVID_KERNEL ]; then
251             tar zxf $VIVID_IMAGE $(basename $VIVID_KERNEL)
252         fi
253         create_vivid_kernel=$(glance --os-image-api-version 1 image-create \
254                 --name yardstick-vivid-kernel \
255                 --is-public true --disk-format qcow2 \
256                 --container-format bare \
257                 --file $VIVID_KERNEL)
258
259         GLANCE_KERNEL_ID=$(echo "$create_vivid_kernel" | grep " id " | awk '{print $(NF-1)}')
260         if [ -z "$GLANCE_KERNEL_ID" ]; then
261             echo 'Failed uploading kernel to cloud'.
262             exit 1
263         fi
264
265         command_line="root=/dev/vdb1 console=tty0 console=ttyS0 console=ttyAMA0 rw"
266
267         EXTRA_PARAMS="--property kernel_id=$GLANCE_KERNEL_ID --property os_command_line=\"$command_line\""
268
269         rm -f $VIVID_KERNEL $VIVID_IMAGE
270         cd $YARDSTICK_REPO_DIR
271     fi
272
273     if [ "$DEPLOY_SCENARIO" == "os-nosdn-lxd-ha" -o "$DEPLOY_SCENARIO" == "os-nosdn-lxd-noha" ]; then
274         DISK_FORMAT="raw"
275     else
276         DISK_FORMAT="qcow2"
277     fi
278
279     output=$(eval glance --os-image-api-version 1 image-create \
280         --name yardstick-trusty-server \
281         --is-public true --disk-format $DISK_FORMAT \
282         --container-format bare \
283         $EXTRA_PARAMS \
284         --file $QCOW_IMAGE)
285
286     echo "$output"
287
288     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
289
290     if [ -z "$GLANCE_IMAGE_ID" ]; then
291         echo 'Failed uploading image to cloud'.
292         exit 1
293     fi
294
295     sudo rm -f $QCOW_IMAGE
296
297     echo "Glance image id: $GLANCE_IMAGE_ID"
298 }
299
300 report(){
301
302     echo
303     echo "========== Reporting Status =========="
304     curl -i -H 'content-type: application/json' -X POST -d \
305         "{\"project_name\": \"yardstick\",
306           \"case_name\": \"scenario_status\",
307           \"pod_name\":\"${NODE_NAME}\",
308           \"installer\":\"${INSTALLER_TYPE}\",
309           \"version\":\"$(basename ${YARDSTICK_BRANCH})\",
310           \"scenario\":\"${DEPLOY_SCENARIO}\",
311           \"description\": \"yardstick ci scenario status\",
312           \"criteria\":\"$1\",
313           \"start_date\":\"$2\",
314           \"stop_date\":\"$3\",
315           \"details\":\"\"}" \
316           ${DISPATCHER_HTTP_TARGET}
317 }
318
319 run_test()
320 {
321     echo
322     echo "========== Running yardstick test suites =========="
323
324     mkdir -p /etc/yardstick
325
326     cat << EOF >> /etc/yardstick/yardstick.conf
327 [DEFAULT]
328 debug = True
329 dispatcher = ${DISPATCHER_TYPE}
330
331 [dispatcher_file]
332 file_name = ${DISPATCHER_FILE_NAME}
333
334 [dispatcher_http]
335 timeout = 5
336 target = ${DISPATCHER_HTTP_TARGET}
337
338 [dispatcher_influxdb]
339 timeout = 5
340 target = ${DISPATCHER_INFLUXDB_TARGET}
341 db_name = yardstick
342 username = opnfv
343 password = 0pnfv2015
344 EOF
345
346     local failed=0
347     local start_date
348     local stop_date
349
350     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
351
352         start_date=$(date '+%Y-%m-%d %H:%M:%S')
353         for suite in ${SUITE_FILES[*]}; do
354
355             echo "---------------------------"
356             echo "Running test suite: $suite"
357             echo "---------------------------"
358             if ! yardstick task start --suite $suite; then
359                  echo "test suite $suite FAILED";
360
361                 # Mark the test suite failed but continue
362                 # running the remaining test suites.
363                 (( ++failed ))
364             fi
365             if [ ${DISPATCHER_TYPE} = file ]; then
366                 echo "---------------------------"
367                 echo "Dump test suite $suite result"
368                 echo "---------------------------"
369                 if [ -f ${DISPATCHER_FILE_NAME} ]; then
370                     cat ${DISPATCHER_FILE_NAME}
371                 else
372                     echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
373                 fi
374             fi
375         done
376         stop_date=$(date '+%Y-%m-%d %H:%M:%S')
377
378
379
380         local scenario_status="SUCCESS"
381
382         if [ $failed -gt 0 ]; then
383             scenario_status="FAILED"
384         fi
385
386         report $scenario_status $start_date $stop_date
387
388         if [ $failed -gt 0 ]; then
389             echo "---------------------------"
390             echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
391             echo "---------------------------"
392             exit 1
393         fi
394
395     else
396
397         echo "---------------------------"
398         echo "Running samples/ping.yaml  "
399         echo "---------------------------"
400
401         if ! yardstick task start samples/ping.yaml; then
402             echo "Yardstick test FAILED"
403             exit 1
404         fi
405
406         if [ ${DISPATCHER_TYPE} = file ]; then
407             echo "---------------------------"
408             echo "Dump samples/ping.yaml test result"
409             echo "---------------------------"
410             if [ -f ${DISPATCHER_FILE_NAME} ]; then
411                 cat ${DISPATCHER_FILE_NAME}
412             else
413                 echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
414             fi
415         fi
416
417     fi
418
419 }
420
421 main()
422 {
423     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
424
425     cd $GITROOT
426
427     export YARDSTICK_VERSION=$(git rev-parse HEAD)
428
429     SUITE_FILES=()
430
431     # find the test suite files
432     for suite in $TEST_SUITES; do
433         if [ -f $suite ]; then
434             SUITE_FILES+=($suite)
435         else
436             tsdir=$GITROOT/tests/opnfv/test_suites
437             if [ ! -f $tsdir/$suite ]; then
438                 echo "Test suite \"$suite\" does not exist"
439                 exit 1
440             fi
441             SUITE_FILES+=($tsdir/$suite)
442         fi
443     done
444
445     echo
446     echo "========== Running Yardstick CI with following parameters =========="
447     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
448     echo "Dispatcher: ${DISPATCHER_TYPE} ${DISPATCHER_FILE_NAME}"
449     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
450     echo "Number of test suites: ${#SUITE_FILES[@]}"
451     for suite in ${SUITE_FILES[*]}; do
452         echo "     $suite"
453     done
454     echo
455
456     # check if some necessary variables is set
457     if [ -z "$OS_AUTH_URL" ]; then
458         echo "OS_AUTH_URL is unset or empty"
459         exit 1
460     fi
461
462     echo "OS_AUTH_URL is $OS_AUTH_URL"
463     echo
464
465     # check OpenStack services
466     echo "Checking OpenStack services:"
467     for cmd in "glance image-list" "nova list" "heat stack-list"; do
468         echo "  checking ${cmd/%\ */} ..."
469         if ! $cmd >/dev/null; then
470             echo "error: command \"$cmd\" failed"
471             exit 1
472         fi
473     done
474
475     echo
476     echo "Checking for External network:"
477     for net in $(neutron net-list --router:external True -c name -f value); do
478         echo "  external network: $net"
479     done
480
481     # install yardstick
482     install_yardstick
483
484     cleanup
485
486     trap "error_exit" EXIT SIGTERM
487
488     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-trusty-server.img"
489
490     build_yardstick_image
491     load_yardstick_image
492     if [ $YARD_IMG_ARCH = "arm64" ]; then
493         sed -i 's/image: cirros-0.3.3/image: TestVM/g' tests/opnfv/test_cases/opnfv_yardstick_tc002.yaml \
494         samples/ping.yaml
495         #We have overlapping IP with the real network
496         for filename in tests/opnfv/test_cases/*; do
497             sed -i "s/cidr: '10.0.1.0\/24'/cidr: '10.3.1.0\/24'/g" $filename
498         done
499     else
500         load_cirros_image
501         load_ubuntu_image
502     fi
503     create_nova_flavor
504     install_storperf
505     run_test
506     remove_storperf
507 }
508
509 main