Improve quoting of asterisks
[csit.git] / resources / libraries / bash / function / gather.sh
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Copyright (c) 2019 PANTHEON.tech and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -exuo pipefail
16
17 # This library defines functions used mainly by "bootstrap" entry scripts.
18 # Generally, the functions assume "common.sh" library has been sourced already.
19
20 # Keep functions ordered alphabetically, please.
21
22 # TODO: Add a link to bash style guide.
23
24
25 function gather_build () {
26
27     # Variables read:
28     # - TEST_CODE - String affecting test selection, usually jenkins job name.
29     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
30     # Variables set:
31     # - DUT - CSIT test/ subdirectory containing suites to execute.
32     # Directories updated:
33     # - ${DOWNLOAD_DIR} - Files needed by tests are gathered here.
34     # Functions called:
35     # - die - Print to stderr and exit, defined in common.sh
36     # - gather_os - Parse os parameter for OS/distro name.
37     # - gather_dpdk, gather_vpp - See their definitions.
38     # Multiple other side effects are possible,
39     # see functions called from here for their current description.
40
41     # TODO: Separate DUT-from-TEST_CODE from gather-for-DUT,
42     #   when the first one becomes relevant for per_patch.
43
44     set -exuo pipefail
45
46     pushd "${DOWNLOAD_DIR}" || die "Pushd failed."
47     case "${TEST_CODE}" in
48         *"hc2vpp"*)
49             DUT="hc2vpp"
50             # FIXME: Avoid failing on empty ${DOWNLOAD_DIR}.
51             ;;
52         *"vpp"*)
53             DUT="vpp"
54             gather_vpp || die "The function should have died on error."
55             ;;
56         *"dpdk"*)
57             DUT="dpdk"
58             gather_dpdk || die "The function should have died on error."
59             ;;
60         *)
61             die "Unable to identify DUT type from: ${TEST_CODE}"
62             ;;
63     esac
64     popd || die "Popd failed."
65 }
66
67
68 function gather_dpdk () {
69
70     # Ensure latest DPDK archive is downloaded.
71     #
72     # Variables read:
73     # - TEST_CODE - The test selection string from environment or argument.
74     # Hardcoded:
75     # - dpdk archive name to download if TEST_CODE is not time based.
76     # Directories updated:
77     # - ./ - Assumed ${DOWNLOAD_DIR}, dpdk-*.tar.xz is downloaded if not there.
78     # Functions called:
79     # - die - Print to stderr and exit, defined in common.sh
80
81     set -exuo pipefail
82
83     dpdk_repo="https://fast.dpdk.org/rel"
84     # Use downloaded packages with specific version
85     if [[ "${TEST_CODE}" == *"daily"* ]] || \
86        [[ "${TEST_CODE}" == *"weekly"* ]] || \
87        [[ "${TEST_CODE}" == *"timed"* ]];
88     then
89         echo "Downloading latest DPDK packages from repo..."
90         # URL is not in quotes, calling command from variable keeps them.
91         wget_command=("wget" "--no-check-certificate" "-nv" "-O" "-")
92         wget_command+=("${dpdk_repo}")
93         dpdk_stable_ver="$("${wget_command[@]}" | grep -v "2015"\
94             | grep -Eo 'dpdk-[^\"]+xz' | tail -1)" || {
95             die "Composite piped command failed."
96         }
97     else
98         echo "Downloading DPDK package of specific version from repo ..."
99         # Downloading DPDK version based on what VPP is using. Currently
100         # it is not easy way to detect from VPP version automatically.
101         dpdk_stable_ver="$(< "${CSIT_DIR}/DPDK_VPP_VER")".tar.xz || {
102             die "Failed to read DPDK VPP version!"
103         }
104     fi
105     # TODO: Use "wget -N" instead checking for file presence?
106     if [[ ! -f "${dpdk_stable_ver}" ]]; then
107         wget -nv --no-check-certificate "${dpdk_repo}/${dpdk_stable_ver}" || {
108             die "Failed to get DPDK package from: ${dpdk_repo}"
109         }
110     fi
111 }
112
113
114 function gather_vpp () {
115
116     # Variables read:
117     # - BASH_FUNCTION_DIR - Bash directory with functions.
118     # - TEST_CODE - The test selection string from environment or argument.
119     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
120     # - CSIT_DIR - Path to existing root of local CSIT git repository.
121     # Variables set:
122     # - VPP_VERSION - VPP stable version under test.
123     # Files read:
124     # - ${CSIT_DIR}/DPDK_STABLE_VER - DPDK version to use
125     #   by csit-vpp not-timed jobs.
126     # - ${CSIT_DIR}/VPP_STABLE_VER_UBUNTU - Ubuntu VPP version to usee.
127     # - ../*vpp*.deb|rpm - Relative to ${DOWNLOAD_DIR}, copied for vpp-csit jobs.
128     # Directories updated:
129     # - ${DOWNLOAD_DIR}, vpp-*.deb files are copied here for vpp-csit jobs.
130     # - ./ - Assumed ${DOWNLOAD_DIR}, *vpp*.deb|rpm files
131     #   are downloaded here for csit-vpp.
132     # Functions called:
133     # - die - Print to stderr and exit, defined in common_functions.sh
134     # Bash scripts executed:
135     # - ${CSIT_DIR}/resources/tools/scripts/download_install_vpp_pkgs.sh
136     #   - Should download and extract requested files to ./.
137
138     set -exuo pipefail
139
140     case "${TEST_CODE}" in
141         "csit-"*)
142             # Use downloaded packages with specific version.
143             if [[ "${TEST_CODE}" == *"daily"* ]] || \
144                ([[ "${TEST_CODE}" == *"weekly"* ]] && \
145                 [[ "${TEST_CODE}" != *"device"* ]]) || \
146                [[ "${TEST_CODE}" == *"semiweekly"* ]];
147             then
148                 warn "Downloading latest VPP packages from Packagecloud."
149             else
150                 warn "Downloading stable VPP packages from Packagecloud."
151                 VPP_VERSION="$(<"${CSIT_DIR}/${VPP_VER_FILE}")" || {
152                     die "Read VPP stable version failed."
153                 }
154             fi
155             source "${BASH_FUNCTION_DIR}/artifacts.sh" || die "Source failed."
156             download_artifacts || die
157             ;;
158         "vpp-csit-"*)
159             # Use locally built packages.
160             mv "${DOWNLOAD_DIR}"/../*vpp*."${PKG_SUFFIX}" "${DOWNLOAD_DIR}"/ || {
161                 die "Move command failed."
162             }
163             ;;
164         *)
165             die "Unable to identify job type from: ${TEST_CODE}"
166             ;;
167     esac
168 }