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