b10acddd13b906548cad16156498e5c3a2e354b4
[kvmfornfv.git] / ci / cyclicTestTrigger.sh
1 #!/bin/bash
2
3 #############################################################
4 ## This script defines the functions present in
5 ## test_kvmfornfv.sh interface.These will launch ubuntu
6 ## docker container runs cyclictest through yardstick
7 ## and verifies the test results.
8 ############################################################
9
10 HOST_IP=$1
11 testTime=$2
12 testType=$3
13 testName=$4
14
15 source $WORKSPACE/ci/envs/utils.sh
16 source $WORKSPACE/ci/envs/host-config
17 KERNELRPM_VERSION=$( getKernelVersion )
18 QEMURPM_VERSION=$( getQemuVersion )
19
20 if [ -z ${KERNELRPM_VERSION} ];then
21    echo "Kernel RPM not found in build_output Directory"
22    exit 1
23 fi
24 if [ -z ${QEMURPM_VERSION} ];then
25    echo "QEMU RPM not found in build_output Directory"
26    exit 1
27 fi
28
29 #calculating and verifying sha512sum of the guestimage.
30 function verifyGuestImage {
31    scp $WORKSPACE/build_output/guest1.sha512 root@${HOST_IP}:/root/images
32    checksum=$(sudo ssh root@${HOST_IP} "cd /root/images/ && sha512sum -c guest1.sha512 | awk '{print \$2}'")
33    if [ "$checksum" != "OK" ]; then
34       echo "Something wrong with the image, please verify"
35       return 1
36    fi
37 }
38
39 #Updating the pod.yaml file with HOST_IP,kvmfornfv_cyclictest_idle_idle.yaml with loops and interval
40 function updateYaml {
41    cd $WORKSPACE/tests/
42    sed -ri "s/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/${HOST_IP}/" pod.yaml
43    sed -ri "s/loops: [0-9]*/loops: ${testTime}/"  kvmfornfv_cyclictest_hostenv_guestenv.yaml
44    sed -ri "0,/interval: [0-9]*/s//interval: 1000/"  kvmfornfv_cyclictest_hostenv_guestenv.yaml
45    sed -ri "s/tc: \"kvmfornfv_cyclictest-node-context\"/tc: \"kvmfornfv_cyclictest_${testName}\"/" kvmfornfv_cyclictest_hostenv_guestenv.yaml
46    cp kvmfornfv_cyclictest_hostenv_guestenv.yaml kvmfornfv_cyclictest_${testName}.yaml
47    case $testName in
48
49        idle_idle)
50                 ;;
51        cpustress_idle)
52                       sed -i '/host-run-qemu.sh/a\    \- \"stress_daily.sh cpu\"' kvmfornfv_cyclictest_${testName}.yaml
53                       ;;
54        memorystress_idle)
55                       sed -i '/host-run-qemu.sh/a\    \- \"stress_daily.sh memory\"' kvmfornfv_cyclictest_${testName}.yaml
56                       ;;
57        iostress_idle)
58                       sed -i '/host-run-qemu.sh/a\    \- \"stress_daily.sh io\"' kvmfornfv_cyclictest_${testName}.yaml
59                       ;;
60        idle_cpustress)
61                       sed -i '/guest-setup1.sh/a\    \- \"stress_daily.sh cpu\"' kvmfornfv_cyclictest_${testName}.yaml
62                       ;;
63        idle_memorystress)
64                       sed -i '/guest-setup1.sh/a\    \- \"stress_daily.sh memory\"' kvmfornfv_cyclictest_${testName}.yaml
65                       ;;
66        idle_iostress)
67                       sed -i '/guest-setup1.sh/a\    \- \"stress_daily.sh memory\"' kvmfornfv_cyclictest_${testName}.yaml
68                       ;;
69         *)
70           echo "Incorrect test environment: $testName"
71           exit 1
72           ;;
73     esac
74 }
75
76 #cleaning the environment after executing the test through yardstick.
77 function env_clean {
78     container_id=`sudo docker ps -a | grep kvmfornfv_${testType}_${testName} |awk '{print \$1}'|sed -e 's/\r//g'`
79     sudo docker stop ${container_id}
80     sudo docker rm ${container_id}
81     sudo ssh root@${HOST_IP} "rm -rf /root/workspace/*"
82     sudo ssh root@${HOST_IP} "pid=\$(ps aux | grep 'qemu' | awk '{print \$2}' | head -1); echo \$pid |xargs kill"
83     sudo rm -rf /tmp/kvmtest-${testType}*
84 }
85
86 #Cleaning the latest kernel changes on host after executing the test.
87 function host_clean {
88     sudo ssh root@${HOST_IP} "rpm=\$(rpm -qa | grep 'kernel-${KERNELRPM_VERSION}' | awk '{print \$1}'); rpm -ev \$rpm"
89     sudo ssh root@${HOST_IP} "rm -rf /boot/initramfs-${KERNELRPM_VERSION}*.img"
90     sudo ssh root@${HOST_IP} "grub2-mkconfig -o /boot/grub2/grub.cfg"
91     sudo ssh root@${HOST_IP} "rpm=\$(rpm -qa | grep 'qemu-${QEMURPM_VERSION}'| awk '{print \$1}'); rpm -ev \$rpm"
92     sudo ssh root@${HOST_IP} "reboot"
93 }
94
95 function cleanup {
96    output=$1
97    env_clean
98    host_clean
99    if [ $output != 0 ];then
100       echo "Yardstick Failed.Please check cyclictest.sh"
101       return 1
102    else
103       return 0
104    fi
105 }
106
107 #Creating a docker image with yardstick installed and Verify the results of cyclictest
108 function runCyclicTest {
109    docker_image_dir=$WORKSPACE/docker_image_build
110    ( cd ${docker_image_dir}; sudo docker build  -t kvmfornfv:latest --no-cache=true . )
111    if [ ${?} -ne 0 ] ; then
112       echo  "Docker image build failed"
113       id=$(sudo docker ps -a  | head  -2 | tail -1 | awk '{print $1}'); sudo docker rm -f $id
114       err_exit 1
115    fi
116    time_stamp=$(date +%Y%m%d%H%M%S)
117    volume=/tmp/kvmtest-${testType}-${time_stamp}
118    mkdir -p $volume/{image,rpm,scripts}
119
120    #copying required files to run yardstick cyclic testcase
121    cp $WORKSPACE/build_output/kernel-${KERNELRPM_VERSION}*.rpm ${volume}/rpm
122    cp $WORKSPACE/build_output/qemu-${QEMURPM_VERSION}*.rpm ${volume}/rpm
123    cp -r $WORKSPACE/ci/envs/* ${volume}/scripts
124    cp -r $WORKSPACE/tests/kvmfornfv_cyclictest_${testName}.yaml ${volume}
125    cp -r $WORKSPACE/tests/pod.yaml ${volume}/scripts
126
127    #Launching ubuntu docker container to run yardstick
128    sudo docker run -i -v ${volume}:/opt --net=host --name kvmfornfv_${testType}_${testName} \
129    kvmfornfv:latest  /bin/bash -c "cd /opt/scripts && ls; ./cyclictest.sh $testType $testName"
130    cyclictest_output=$?
131    #Verifying the results of cyclictest
132
133    if [ "$testType" == "verify" ];then
134       result=`grep -o '"errors":[^,]*' ${volume}/yardstick.out | awk -F '"' '{print $4}'`
135
136       if [ -z "${result}" ]; then
137          echo "####################################################"
138          echo ""
139          echo `grep -o '"data":[^}]*' ${volume}/yardstick.out | awk -F '{' '{print $2}'`
140          echo ""
141          echo "####################################################"
142          cleanup $cyclictest_output
143       else
144          echo "Testcase failed"
145          echo `grep -o '"errors":[^,]*' ${volume}/yardstick.out | awk -F '"' '{print $4}'`
146          env_clean
147          host_clean
148          return 1
149       fi
150    else
151       cleanup $cyclictest_output
152    fi
153 }