Function test updates
[nonrtric.git] / test / common / format_endpoint_stats.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021-2023 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # This script format http endpoint stats generated by testscripts
21
22 print_usage() {
23   echo "Usage: format_endpoint_stats <log-base-dir> <app-id> <app-description> [tc-id]+ "
24 }
25
26 SUMMARYFILE=""
27 SUMMARYFILE_TMP=""
28
29 update_summary() {
30
31   input=$@
32   inputarr=(${input// / })
33   inputp=${inputarr[3]}
34   inputn=${inputarr[4]}
35   inputposarr=(${inputp//\// })
36   inputnegarr=(${inputn//\// })
37   >  $SUMMARYFILE_TMP
38   found=0
39   while read -r line; do
40     linearr=(${line// / })
41     linep=${linearr[3]}
42     linen=${linearr[4]}
43     lineposarr=(${linep//\// })
44     linenegarr=(${linen//\// })
45     if [[ ${linearr[1]} == ${inputarr[1]} ]] && [[ ${linearr[2]} == ${inputarr[2]} ]]; then
46       let lineposarr[0]=lineposarr[0]+inputposarr[0]
47       let lineposarr[1]=lineposarr[1]+inputposarr[1]
48       let linenegarr[0]=linenegarr[0]+inputnegarr[0]
49       let linenegarr[1]=linenegarr[1]+inputnegarr[1]
50       found=1
51     fi
52     printf '%-2s %-10s %-45s %-16s %-16s' "#" "${linearr[1]}" "${linearr[2]}" "${lineposarr[0]}/${lineposarr[1]}" "${linenegarr[0]}/${linenegarr[1]}" >> $SUMMARYFILE_TMP
53     echo "" >> $SUMMARYFILE_TMP
54   done < $SUMMARYFILE
55   if [ $found -eq 0 ]; then
56     printf '%-2s %-10s %-45s %-16s %-16s' "#" "${inputarr[1]}" "${inputarr[2]}" "${inputposarr[0]}/${inputposarr[1]}" "${inputnegarr[0]}/${inputnegarr[1]}" >> $SUMMARYFILE_TMP
57     echo "" >> $SUMMARYFILE_TMP
58   fi
59   cp $SUMMARYFILE_TMP $SUMMARYFILE
60 }
61
62 if [ $# -lt 4 ]; then
63   print_usage
64   exit 1
65 fi
66 BASE_DIR=$1
67 if [ ! -d $BASE_DIR ]; then
68   print_usage
69   echo "<log-base-dir> $BASE_DIR does not exist or is not a dir"
70   exit 1
71 fi
72 SUMMARYFILE=$BASE_DIR/endpoint_summary.log
73 rm $SUMMARYFILE
74 touch $SUMMARYFILE
75 SUMMARYFILE_TMP=$BASE_DIR/endpoint_summary_tmp.log
76 TC_FAIL=0
77 shift
78 APP_ID=$1
79 shift
80 echo ""
81 echo "==================================================="
82 echo "Functional test cases for $1"
83 echo "Report date: $(date)"
84 echo "==================================================="
85 echo
86 shift
87 while [ $# -gt 0 ]; do
88   FTC_DIR=$BASE_DIR/$1
89   if [ ! -d $FTC_DIR ]; then
90     echo "Dir $FTC_DIR does not exist"
91     exit 1
92   fi
93   IMAGE_INFO_FILE=$FTC_DIR/imageinfo_$APP_ID".log"
94   if [ -f $IMAGE_INFO_FILE ]; then
95     echo "===  Testscript: $1  ==="
96     echo "Slogan: "$(cat $FTC_DIR/endpoint_tc_slogan.log)
97     echo "Image: "$(cat $IMAGE_INFO_FILE)
98     image_name=$(< $IMAGE_INFO_FILE)
99     image_sha=$( docker inspect --format='{{.RepoDigests}}' $image_name) 2> /dev/null
100     echo "Sha: $image_sha"
101     echo "Test start: $(cat $FTC_DIR/endpoint_tc_start.log)"
102     echo "Test end: $(cat $FTC_DIR/endpoint_tc_end.log)"
103     echo
104     TC_RES_FILE=$FTC_DIR/.result$1.txt
105     if [ -f "$TC_RES_FILE" ]; then
106       TC_RESULT=$(< "$TC_RES_FILE")
107       if [ $TC_RESULT -ne 0 ]; then
108         echo " !!!!! TESTCASE FAILED !!!!!"
109         let TC_FAIL=TC_FAIL+1
110       fi
111     fi
112     echo "===  Results: positive=2XX http status, negative=non 2XX http status - (ok/total)==="
113     echo "Method     Endpoint                                      Positive         Negative"
114     grep --no-filename "#" $FTC_DIR/endpoint_$APP_ID* | cut -c 4-
115     for filename in $FTC_DIR/endpoint_$APP_ID* ; do
116       filedata=$(< $filename)
117       update_summary $filedata
118     done
119     echo "==============================="
120     echo
121   else
122     echo "===  No stats collected by Testscript $1  ==="
123     echo ""
124   fi
125   shift
126 done
127
128 echo "Summary of all testscripts"
129 if [ $TC_FAIL -ne 0 ]; then
130   echo " !!!!! ONE OR MORE TESTCASE(S) FAILED - CHECK INDIVIDUAL TEST RESULT!!!!!"
131 fi
132 echo "===  Results: positive=2XX http status, negative=non 2XX http status - (ok/total)==="
133 echo "Method     Endpoint                                      Positive         Negative"
134 cat $SUMMARYFILE | cut -c 4-
135
136 echo "-- end of test report -- "
137 exit 0
138