6765569f678536d919e6dcda5c88b2ef21a3a2aa
[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
18 checkRPMNames
19
20 KERNELRPM_VERSION=$( getKernelVersion )
21 QEMURPM_VERSION=$( getQemuVersion )
22
23 if [ -z ${KERNELRPM_VERSION} ];then
24    echo "Kernel RPM not found in build_output Directory"
25    exit 1
26 fi
27 if [ -z ${QEMURPM_VERSION} ];then
28    echo "QEMU RPM not found in build_output Directory"
29    exit 1
30 fi
31
32 #calculating and verifying sha512sum of the guestimage.
33 function verifyGuestImage {
34    scp $WORKSPACE/build_output/guest1.sha512 root@${HOST_IP}:/root/images
35    checksum=$(sudo ssh root@${HOST_IP} "cd /root/images/ && sha512sum -c guest1.sha512 | awk '{print \$2}'")
36    if [ "$checksum" != "OK" ]; then
37       echo "Something wrong with the image, please verify"
38       return 1
39    fi
40 }
41
42 #disabling ftrace and collecting the logs to upload to artifact repository.
43 function ftrace_disable {
44    sudo ssh root@${HOST_IP} "sh /root/workspace/scripts/disable_trace.sh"
45    sudo ssh root@${HOST_IP} "cd /tmp ;  mv trace.txt cyclictest_${env}.txt"
46    mkdir -p $WORKSPACE/build_output/log/kernel_trace
47    scp root@${HOST_IP}:/tmp/cyclictest_${env}.txt $WORKSPACE/build_output/log/kernel_trace/
48    sudo ssh root@${HOST_IP} "cd /tmp ; rm -rf cyclictest_${env}.txt"
49 }
50
51 #Verifying the availability of the host after reboot
52 function connect_host {
53    n=0
54    while [ $n -lt 25 ]; do
55       host_ping_test="ping -c 1 ${HOST_IP}"
56       eval $host_ping_test &> /dev/null
57       if [ ${?} -ne 0 ] ; then
58          sleep 10
59          echo "host machine is still under reboot..trying to connect...."
60          n=$(($n+1))
61       else
62          echo "resuming the execution of test cases....."
63          #Waiting for ssh to be available for the host machine.
64          sleep 30
65          break
66       fi
67       if [ $n == 24 ];then
68          echo "Host machine unable to boot-up!"
69          exit 1
70       fi
71    done
72 }
73
74 #Updating the pod.yaml file with HOST_IP,kvmfornfv_cyclictest_idle_idle.yaml with loops and interval
75 function updateYaml {
76    cd $WORKSPACE/tests/
77    sed -ri "s/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/${HOST_IP}/" pod.yaml
78    sed -ri "s/loops: [0-9]*/loops: ${testTime}/"  kvmfornfv_cyclictest_hostenv_guestenv.yaml
79    sed -ri "0,/interval: [0-9]*/s//interval: 1000/"  kvmfornfv_cyclictest_hostenv_guestenv.yaml
80    cp kvmfornfv_cyclictest_hostenv_guestenv.yaml kvmfornfv_cyclictest_${testName}.yaml
81    sed -ri "s/tc: \"kvmfornfv_cyclictest-node-context\"/tc: \"kvmfornfv_cyclictest_${testName}\"/" kvmfornfv_cyclictest_${testName}.yaml
82    case $testName in
83
84        idle_idle)
85                 ;;
86        cpustress_idle)
87                       sed -i '/host-run-qemu.sh/a\    \- \"stress_daily.sh cpu\"' kvmfornfv_cyclictest_${testName}.yaml
88                       ;;
89        memorystress_idle)
90                       sed -i '/host-run-qemu.sh/a\    \- \"stress_daily.sh memory\"' kvmfornfv_cyclictest_${testName}.yaml
91                       ;;
92        iostress_idle)
93                       sed -i '/host-run-qemu.sh/a\    \- \"stress_daily.sh io\"' kvmfornfv_cyclictest_${testName}.yaml
94                       ;;
95        idle_cpustress)
96                       sed -i '/guest-setup1.sh/a\    \- \"stress_daily.sh cpu\"' kvmfornfv_cyclictest_${testName}.yaml
97                       ;;
98        idle_memorystress)
99                       sed -i '/guest-setup1.sh/a\    \- \"stress_daily.sh memory\"' kvmfornfv_cyclictest_${testName}.yaml
100                       ;;
101        idle_iostress)
102                       sed -i '/guest-setup1.sh/a\    \- \"stress_daily.sh io\"' kvmfornfv_cyclictest_${testName}.yaml
103                       ;;
104         *)
105           echo "Incorrect test environment: $testName"
106           exit 1
107           ;;
108     esac
109 }
110
111 #cleaning the environment after executing the test through yardstick.
112 function env_clean {
113     container_id=`sudo docker ps -a | grep kvmfornfv_${testType} |awk '{print \$1}'|sed -e 's/\r//g'`
114     sudo docker stop ${container_id}
115     sudo docker rm ${container_id}
116     sudo ssh root@${HOST_IP} "rm -rf /root/workspace/*"
117     sudo ssh root@${HOST_IP} "pid=\$(ps aux | grep 'qemu' | awk '{print \$2}' | head -1); echo \$pid |xargs kill"
118     sudo rm -rf /tmp/kvmtest-${testType}*
119 }
120
121 #Cleaning the latest kernel changes on host after executing the test.
122 function host_clean {
123     sudo ssh root@${HOST_IP} "rpm=\$(rpm -qa | grep 'kernel-${KERNELRPM_VERSION}' | awk '{print \$1}'); rpm -ev \$rpm"
124     sudo ssh root@${HOST_IP} "rpm=\$(rpm -qa | grep 'kernel-devel-${KERNELRPM_VERSION}' | awk '{print \$1}'); rpm -ev \$rpm"
125     sudo ssh root@${HOST_IP} "rm -rf /boot/initramfs-${KERNELRPM_VERSION}*.img"
126     sudo ssh root@${HOST_IP} "grub2-mkconfig -o /boot/grub2/grub.cfg"
127     sudo ssh root@${HOST_IP} "rpm=\$(rpm -qa | grep 'qemu-${QEMURPM_VERSION}'| awk '{print \$1}'); rpm -ev \$rpm"
128     sudo ssh root@${HOST_IP} "reboot"
129 }
130
131 function cleanup {
132    output=$1
133    env_clean
134    host_clean
135    if [ $output != 0 ];then
136       echo "Yardstick Failed.Please check cyclictest.sh"
137       return 1
138    else
139       return 0
140    fi
141 }
142
143 #environment setup for executing packet forwarding test cases
144 function setUpPacketForwarding {
145    #copying required files to run packet forwarding test cases
146    ssh root@$HOST_IP "mkdir -p /root/workspace/image"
147    ssh root@$HOST_IP "mkdir -p /root/workspace/rpm"
148    ssh root@$HOST_IP "mkdir -p /root/workspace/scripts"
149    #Copying the host configuration scripts on to host
150    scp -r $WORKSPACE/ci/envs/* root@$HOST_IP:/root/workspace/scripts
151    scp -r $WORKSPACE/tests/vsperf.conf* root@$HOST_IP:/root/workspace/scripts
152    scp -r $WORKSPACE/build_output/kernel-${KERNELRPM_VERSION}*.rpm root@$HOST_IP:/root/workspace/rpm
153    scp -r $WORKSPACE/build_output/kernel-devel-${KERNELRPM_VERSION}*.rpm root@$HOST_IP:/root/workspace/rpm
154    scp -r $WORKSPACE/build_output/qemu-${QEMURPM_VERSION}*.rpm root@$HOST_IP:/root/workspace/rpm
155    #execute host configuration script for installing kvm built kernel.
156    ssh root@$HOST_IP "cd /root/workspace/scripts ; ./host-setup0.sh"
157    ssh root@$HOST_IP "cd /root/workspace/rpm ; rpm -ivh kernel-devel-${KERNELRPM_VERSION}*.rpm"
158    ssh root@$HOST_IP "reboot"
159    sleep 10
160 }
161
162 #executing packet forwarding test cases
163 function runPacketForwarding {
164    testType=$1
165    ssh -t -t root@$HOST_IP "cd /root/workspace/scripts ; sudo scl enable python33 'sh packet_forwarding.sh $testType $QEMURPM_VERSION'"
166 }
167
168 #Creating a docker image with yardstick installed and Verify the results of cyclictest
169 function runCyclicTest {
170    ftrace_enable=$1
171    docker_image_dir=$WORKSPACE/docker_image_build
172    ( cd ${docker_image_dir}; sudo docker build  -t kvmfornfv:latest --no-cache=true . )
173    if [ ${?} -ne 0 ] ; then
174       echo  "Docker image build failed"
175       id=$(sudo docker ps -a  | head  -2 | tail -1 | awk '{print $1}'); sudo docker rm -f $id
176       exit 1
177    fi
178    time_stamp=$(date +%Y%m%d%H%M%S)
179    volume=/tmp/kvmtest-${testType}-${time_stamp}
180    mkdir -p $volume/{image,rpm,scripts}
181    #copying required files to run yardstick cyclic testcase
182    cp $WORKSPACE/build_output/kernel-${KERNELRPM_VERSION}*.rpm ${volume}/rpm
183    cp $WORKSPACE/build_output/qemu-${QEMURPM_VERSION}*.rpm ${volume}/rpm
184    cp -r $WORKSPACE/ci/envs/* ${volume}/scripts
185    cp -r $WORKSPACE/tests/kvmfornfv_cyclictest_${testName}.yaml ${volume}
186    cp -r $WORKSPACE/tests/pod.yaml ${volume}/scripts
187
188    #Launching ubuntu docker container to run yardstick
189    sudo docker run -i -v ${volume}:/opt --net=host --name kvmfornfv_${testType}_${testName} \
190    kvmfornfv:latest /bin/bash -c "cd /opt/scripts && ls; ./cyclictest.sh $testType $testName"
191    cyclictest_output=$?
192    if [ "$testName" == "memorystress_idle" ];then
193       copyLogs
194    fi
195
196    #Disabling ftrace after completion of executing test cases.
197    if [ ${ftrace_enable} -eq '1' ]; then
198       ftrace_disable
199    fi
200
201    #Verifying the results of cyclictest
202    if [ "$testType" == "verify" ];then
203       result=`grep -o '"errors":[^,]*' ${volume}/yardstick.out | awk -F '"' '{print $4}'`
204
205       if [ -z "${result}" ]; then
206          echo "####################################################"
207          echo " "
208          echo `grep -o '"data":[^}]*' ${volume}/yardstick.out | awk -F '{' '{print $2}'`
209          echo ""
210          echo "####################################################"
211          cleanup $cyclictest_output
212       else
213          echo "Testcase failed"
214          echo `grep -o '"errors":[^,]*' ${volume}/yardstick.out | awk -F '"' '{print $4}'`
215          env_clean
216          host_clean
217          return 1
218       fi
219    else
220       cleanup $cyclictest_output
221    fi
222 }