Backport tox checkers from master
[csit.git] / resources / libraries / bash / entry / check / tc_naming.sh
1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 set -xeuo pipefail
15
16 # This file should be executed from tox, as the assumend working directory
17 # is different from where this file is located.
18 # This file does not have executable flag nor shebang,
19 # to dissuade non-tox callers.
20
21 # "set -eu" handles failures from the following two lines.
22 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
23 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
24 source "${BASH_FUNCTION_DIR}/common.sh" || {
25     echo "Source failed." >&2
26     exit 1
27 }
28
29 # Grep of interest: We want all tc01- prefixed (skip TC variations for now).
30 # Currently script assumes all variations inside to be part of either
31 # auto-generation or not checked at all (VIRL derivates).
32 r_grep="tc01-"
33 # Parse grep of interest (learn path, learn suite, learn testcase name).
34 r_parse='(.*)\/(.*).robot.*(tc[[:digit:]]{2}-.*)'
35
36 # CSIT Testcase naming convention rules.
37 # https://wiki.fd.io/view/CSIT/csit-test-naming
38 # Rules are defined as regular expressions in ordered array and checked in order
39 # in a loop, where every iteration is catenated with previous rules. This way we
40 # can detect where exactly the naming does not meet criteria and print error
41 # from rule string array. This imply that rules are defined in a way of a single
42 # string. First rule must start with ^ and last is terminated by $.
43 # Rules are written from Left to Right.
44 # Bash regular expression logic is used. Once the error is raised the checker is
45 # breaked for current Testcase marking the expected fail.
46 # One caveat of this solution is that we cannot proceed to check full names now
47 # as majority of Testcases does not meet naming criteria.
48 s_testc_rules=(
49     'TC id'
50     'packet size'
51     'core combination'
52     'NIC driver mode'
53     'packet encapsulation on L2 layer'
54     'test type'
55     )
56 r_testc_rules=(
57     '^tc[[:digit:]]{2}-'
58     '([[:digit:]]{2,4}B|IMIX)-'
59     '([[:digit:]]+c-){0,1}'
60     '(avf-|1lbvpplacp-|2lbvpplacp-){0,1}'
61     '(eth|dot1q|dot1ad)'
62     # TODO: Packet encapsulation (here majority of TC starts failing).
63     #'(ip4|ip6|ip6ip6|icmpv4|icmpv6)'
64     #'(ipsec[[:digit:]]+tnlhw|ipsec[[:digit:]]+tnlsw|'
65     #'srhip6|tcp|udp|lispip6|lispip4|vxlan){0,1}'
66     #'(http){0,1}-'
67     '(.*)-(dev|ndrpdr|bps|cps|rps|reconf)$'
68     )
69 s_suite_rules=(
70     'number of SUT nodes'
71     'NIC card'
72     'NIC driver mode'
73     'packet encapsulation on L2 layer'
74     'test type'
75     )
76 r_suite_rules=(
77     '^(2n1l|2n){0,1}-'
78     '(eth2p|10ge2p1x710)-'
79     '(avf-|1lbvpplacp-|2lbvpplacp-){0,1}'
80     '(eth|dot1q|dot1ad)'
81     # TODO: Packet encapsulation (here majority of TC starts failing).
82     #'(ip4|ip6|ip6ip6|icmpv4|icmpv6)'
83     #'(ipsec[[:digit:]]+tnlhw|ipsec[[:digit:]]+tnlsw|'
84     #'srhip6|tcp|udp|lispip6|lispip4|vxlan){0,1}'
85     #'(http){0,1}-'
86     '(.*)-(dev|ndrpdr|bps|cps|rps|reconf)$'
87     )
88
89 rm -f "tc_naming.log" || die
90
91 # Disabling -x: Following lines are doing too much garbage output.
92 set +x
93
94 # Grep interest.
95 grep_match=$(grep -RE "${r_grep}" tests/*) || die
96 # Extract data from the grep output.
97 suites_dirs=($(printf "${grep_match}" | sed -re "s/${r_parse}/\1/")) || die
98 suites_names=($(printf "${grep_match}" | sed -re "s/${r_parse}/\2/")) || die
99 testcases_names=($(printf "${grep_match}" | sed -re "s/${r_parse}/\3/")) || die
100
101 # Naming check.
102 total_failed_tc=0
103 total_failed_su=0
104 for idx in "${!testcases_names[@]}"; do
105     for pass in "${!r_suite_rules[@]}"; do
106         r_rule=$(printf '%s' "${r_suite_rules[@]:1:pass}")
107         if [[ ! "${suites_names[idx]}" =~ ${r_rule} ]]; then
108             msg=""
109             msg+="${suites_dirs[idx]}/${suites_names[idx]} / "
110             msg+="${testcases_names[idx]} ${s_suite_rules[pass]} "
111             msg+="is not matching suite naming rule!"
112             echo "${msg}" | tee -a "tc_naming.log" || die
113             total_failed_su=$((total_failed_su + 1))
114             break
115         fi
116     done
117     for pass in "${!r_testc_rules[@]}"; do
118         r_rule=$(printf '%s' "${r_testc_rules[@]:1:pass}")
119         if [[ ! "${testcases_names[idx]}" =~ ${r_rule} ]]; then
120             msg=""
121             msg+="${suites_dirs[idx]}/${suites_names[idx]} / "
122             msg+="${testcases_names[idx]} ${s_testc_rules[pass]} "
123             msg+="is not matching testcase naming rule!"
124             echo "${msg}" | tee -a "tc_naming.log" || die
125             total_failed_tc=$((total_failed_tc + 1))
126             break
127         fi
128     done
129 done
130
131 set -x
132
133 if [ $((total_failed_tc + total_failed_su)) != "0" ]; then
134     warn
135     warn "Testcase naming checker: FAIL"
136     exit 1
137 fi
138
139 warn
140 warn "Testcase naming checker: PASS"