b7fa59122d92781ea325c810bca6467769be0194
[pharos.git] / config / utils / check-jinja2.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set +x
12 set +o errexit
13 export PATH=$PATH:/usr/local/bin/
14
15 # Optional filtering of test matrix: per-lab, per-pod, per-installer
16 # e.g. To check zte-pod{2,3} against all installer adapters:
17 # ./config/utils/check-jinja2.sh zte 'pod(2|3)'
18 FILTER_LAB=${1:-*}                           # e.g. 'zte'  (glob)
19 FILTER_POD=${2:-(pod|virtual)[[:digit:]]+}   # e.g. 'pod1' (regex)
20 FILTER_IA=${3:-*}                            # e.g. 'fuel' (glob)
21
22 GEN_CFG='python ./config/utils/generate_config.py'
23 INSTALLER_ADAPTERS="./config/installers/${FILTER_IA}"
24 TMPF='/tmp/out.yml' # should be outside Jenkins WS to prevent data leakage
25 RC=0
26
27 echo "Using $(yamllint --version)"
28
29 # Build a table header, using ';' as column sep
30 for adapter in 'PDF Verify Matrix' ${INSTALLER_ADAPTERS}; do
31     SUMMARY+="$(basename "${adapter}");"
32 done
33
34 # Iterate all PDFs, check with each installer adapter, log results
35 # shellcheck disable=SC2086
36 while IFS= read -r lab_config; do
37     SUMMARY+="\n${lab_config#labs/};"
38     lab_nodes=$(grep -ce 'node:' "${lab_config}")
39     lab_tmacs=$(grep -ce 'mac_address:' "${lab_config}")
40     ((lab_amacs=lab_tmacs/lab_nodes)); ((lab_nodes-=1))
41     echo "###################### ${lab_config} ######################"
42     for adapter in ${INSTALLER_ADAPTERS}; do
43         pdf_inst=0
44         pdf_inst_pass=0
45         pdf_yaml_pass=0
46         ia_nodes=$(grep -hPo 'nodes\W+\K\d+' -R "${adapter}" | tail -1)
47         ia_rmacs=$(grep -hPo 'interfaces\W+\K\d+' -R "${adapter}" | sort | tail -1)
48         ((ia_nodes+=1)); ((ia_rmacs+=1))
49         if [[ ${ia_nodes} -gt ${lab_nodes} ]]; then
50             SUMMARY+='-;'
51             echo -n "[GENERATE] [SKIP] $(basename "${adapter}") requires at least"
52             echo -e " ${ia_nodes} nodes, but found only ${lab_nodes}, skipping.\n"
53             continue
54         fi
55         if [[ ${ia_rmacs} -ge ${lab_amacs} ]]; then
56             SUMMARY+='-;'
57             echo -n "[GENERATE] [SKIP] $(basename "${adapter}") requires at least"
58             echo -e " ${ia_rmacs} nics, but found ~ ${lab_amacs}, skipping.\n"
59             continue
60         fi
61         while IFS= read -r jinja_template; do
62             pdf_gen_cmd="${GEN_CFG} -y ${lab_config} -j ${jinja_template}"
63             if ${pdf_gen_cmd} > "${TMPF}"; then
64                 ((pdf_inst_pass+=1))
65                 echo "[GENERATE] [OK] ${pdf_gen_cmd}"
66                 if yamllint -s <(sed 's|ENC\[PKCS.*\]|opnfv|g' "${TMPF}"); then
67                     ((pdf_yaml_pass+=1));
68                     echo "[YAMLLINT] [OK] yamllint -s ${jinja_template%.j2}"
69                 else
70                     echo "[YAMLLINT] [ERROR] yamllint -s ${jinja_template%.j2}"
71                 fi
72             else
73                 echo "[GENERATE] [ERROR] ${pdf_gen_cmd}"
74                 RC=1
75             fi
76             ((pdf_inst+=1))
77             echo ''
78         done < <(find "${adapter}" -name '*.j2')
79         SUMMARY+="${pdf_yaml_pass}/${pdf_inst_pass}/${pdf_inst};"
80     done
81 done < <(find labs/${FILTER_LAB} -regextype egrep \
82                                  -regex "labs/.+/${FILTER_POD}.yaml")
83 rm -f "${TMPF}"
84
85 cat <<EOF
86 ###################### Result Matrix ######################
87
88 NOTE: tuple fmt: (valid YAML output/sucessful parse/templates).
89
90 $(echo -e "${SUMMARY}" | sed -e 's/^/;/g' -e 's/;/;| /g' | column -t -s ';')
91
92 To troubleshoot PDF parsing against a specific installer adapter,
93 execute the following commands locally (e.g. for zte-pod2/joid):
94 $ ./config/utils/generate_config.py \\
95     -y labs/zte/pod2.yaml \\
96     -j config/installers/joid/pod_config.yaml.j2
97
98 EOF
99 exit "${RC}"