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