66afbb45cf88b70c8263c178fdcb1d5051b03910
[csit.git] / resources / libraries / bash / entry / check / tc_coverage.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2021 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 rm -f "tc_coverage.log" || die
39
40 # Disabling -x: Following lines are doing too much garbage output.
41 set +x
42
43 # Grep interest.
44 grep_match=$(grep -RE "${r_grep}" tests/*) || die
45 # Extract data from the grep output.
46 suites_dirs=($(printf "${grep_match}" | sed -re "s/${r_parse}/\1/")) || die
47 suites_names=($(printf "${grep_match}" | sed -re "s/${r_parse}/\2/")) || die
48 testcases_names=($(printf "${grep_match}" | sed -re "s/${r_parse}/\3/")) || die
49
50 # Extract 2N suites from the global testcase list and normalize output.
51 suites_2n=($(printf '%s\n' "${suites_names[@]}" | \
52            grep -E "^(2n1l|2n)-" | \
53            sed -re "s/(2n1l|2n)-//"))
54 # Extract 3N suites from the global testcase list.
55 suites_3n=($(printf '%s\n' "${suites_names[@]}" | \
56            grep -vE "^(2n1l|2n|eth2p)-"))
57 # Extract vpp_device suites from the global testcase list.
58 # Code will try to map the naming to perf by normalizing output.
59 suites_dev=($(printf '%s\n' "${suites_names[@]}" | \
60             grep -E "^eth2p-" | \
61             sed -re "s/eth2p-/10ge2p1x710-/" | \
62             sed -re "s/-dev/-ndrpdr/" | \
63             sed -re "s/-ethicmpv/-ethip/"))
64
65 # Compute intersection of arrays.
66 intersection_3n2n=($(comm -12 <(printf '%s\n' "${suites_3n[@]}" | sort) \
67     <(printf '%s\n' "${suites_2n[@]}" | sort)))
68 intersection_3ndev=($(comm -12 <(printf '%s\n' "${suites_2n[@]}" | sort) \
69     <(printf '%s\n' "${suites_dev[@]}" | sort)))
70
71 # Print the results in CSV format.
72 echo "Suite name;Testcase name;2n version;VPP Device version" \
73     > tc_coverage.log
74 for i in "${!suites_names[@]}"; do
75     if [[ ! "${suites_names[i]}" =~ ^(2n1l|2n|eth2p)-.* ]]; then
76         echo -n "${suites_names[i]};${testcases_names[i]}" \
77             >> tc_coverage.log
78         if [[ "${intersection_3n2n[@]}" =~ "${suites_names[i]}" ]]; then
79             echo -n ";yes" >> tc_coverage.log
80         else
81             echo -n ";no" >> tc_coverage.log
82         fi
83         if [[ "${intersection_3ndev[@]}" =~ "${suites_names[i]}" ]]; then
84             echo ";yes" >> tc_coverage.log
85         else
86             echo ";no" >> tc_coverage.log
87         fi
88     fi
89 done
90
91 set -x
92
93 echo "Count 2n: ${#suites_2n[@]}"
94 echo "Count 3n: ${#suites_3n[@]}"
95 echo "Count dev: ${#suites_dev[@]}"
96 echo "Coverage 2n3n: ${#intersection_3n2n[@]}"
97 echo "Coverage 3ndev: ${#intersection_3ndev[@]}"
98
99 warn
100 warn "Testcase coverage checker: PASS"