1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
#!/usr/bin/env bash
set -euo pipefail
ROOT_MODULE="go.etcd.io/etcd"
if [[ "$(go list)" != "${ROOT_MODULE}/v3" ]]; then
echo "must be run from '${ROOT_MODULE}/v3' module directory"
exit 255
fi
function set_root_dir {
ETCD_ROOT_DIR=$(go list -f '{{.Dir}}' "${ROOT_MODULE}/v3")
}
set_root_dir
#### Convenient IO methods #####
COLOR_RED='\033[0;31m'
COLOR_ORANGE='\033[0;33m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTCYAN='\033[0;36m'
COLOR_BLUE='\033[0;94m'
COLOR_MAGENTA='\033[95m'
COLOR_BOLD='\033[1m'
COLOR_NONE='\033[0m' # No Color
function log_error {
>&2 echo -n -e "${COLOR_BOLD}${COLOR_RED}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_warning {
>&2 echo -n -e "${COLOR_ORANGE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_callout {
>&2 echo -n -e "${COLOR_LIGHTCYAN}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_cmd {
>&2 echo -n -e "${COLOR_BLUE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_success {
>&2 echo -n -e "${COLOR_GREEN}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_info {
>&2 echo -n -e "${COLOR_NONE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
# From http://stackoverflow.com/a/12498485
function relativePath {
# both $1 and $2 are absolute paths beginning with /
# returns relative path to $2 from $1
local source=$1
local target=$2
local commonPart=$source
local result=""
# Refer to https://www.shellcheck.net/wiki/SC2295
while [[ "${target#"$commonPart"}" == "${target}" ]]; do
# no match, means that candidate common part is not correct
# go up one level (reduce common part)
commonPart="$(dirname "$commonPart")"
# and record that we went back, with correct / handling
if [[ -z $result ]]; then
result=".."
else
result="../$result"
fi
done
if [[ $commonPart == "/" ]]; then
# special case for root (no common path)
result="$result/"
fi
# since we now have identified the common part,
# compute the non-common part
# Refer to https://www.shellcheck.net/wiki/SC2295
local forwardPart="${target#"$commonPart"}"
# and now stick all parts together
if [[ -n $result ]] && [[ -n $forwardPart ]]; then
result="$result$forwardPart"
elif [[ -n $forwardPart ]]; then
# extra slash removal
result="${forwardPart:1}"
fi
echo "$result"
}
#### Discovery of files/packages within a go module #####
# go_srcs_in_module
# returns list of all not-generated go sources in the current (dir) module.
function go_srcs_in_module {
go list -f "{{with \$c:=.}}{{range \$f:=\$c.GoFiles }}{{\$c.Dir}}/{{\$f}}{{\"\n\"}}{{end}}{{range \$f:=\$c.TestGoFiles }}{{\$c.Dir}}/{{\$f}}{{\"\n\"}}{{end}}{{range \$f:=\$c.XTestGoFiles }}{{\$c.Dir}}/{{\$f}}{{\"\n\"}}{{end}}{{end}}" ./... | grep -vE "(\\.pb\\.go|\\.pb\\.gw.go)"
}
# pkgs_in_module [optional:package_pattern]
# returns list of all packages in the current (dir) module.
# if the package_pattern is given, its being resolved.
function pkgs_in_module {
go list -mod=mod "${1:-./...}";
}
# Prints subdirectory (from the repo root) for the current module.
function module_subdir {
relativePath "${ETCD_ROOT_DIR}" "${PWD}"
}
#### Running actions against multiple modules ####
# run [command...] - runs given command, printing it first and
# again if it failed (in RED). Use to wrap important test commands
# that user might want to re-execute to shorten the feedback loop when fixing
# the test.
function run {
local rpath
local command
rpath=$(module_subdir)
# Quoting all components as the commands are fully copy-parsable:
command=("${@}")
command=("${command[@]@Q}")
if [[ "${rpath}" != "." && "${rpath}" != "" ]]; then
repro="(cd ${rpath} && ${command[*]})"
else
repro="${command[*]}"
fi
log_cmd "% ${repro}"
"${@}" 2> >(while read -r line; do echo -e "${COLOR_NONE}stderr: ${COLOR_MAGENTA}${line}${COLOR_NONE}">&2; done)
local error_code=$?
if [ ${error_code} -ne 0 ]; then
log_error -e "FAIL: (code:${error_code}):\\n % ${repro}"
return ${error_code}
fi
}
# run_for_module [module] [cmd]
# executes given command in the given module for given pkgs.
# module_name - "." (in future: tests, client, server)
# cmd - cmd to be executed - that takes package as last argument
function run_for_module {
local module=${1:-"."}
shift 1
(
cd "${ETCD_ROOT_DIR}/${module}" && "$@"
)
}
function module_dirs() {
echo "api pkg raft client/pkg client/v2 client/v3 server etcdutl etcdctl tests ."
}
# maybe_run [cmd...] runs given command depending on the DRY_RUN flag.
function maybe_run() {
if ${DRY_RUN:-}; then
log_warning -e "# DRY_RUN:\\n % ${*}"
else
run "${@}"
fi
}
function modules() {
modules=(
"${ROOT_MODULE}/api/v3"
"${ROOT_MODULE}/pkg/v3"
"${ROOT_MODULE}/raft/v3"
"${ROOT_MODULE}/client/pkg/v3"
"${ROOT_MODULE}/client/v2"
"${ROOT_MODULE}/client/v3"
"${ROOT_MODULE}/server/v3"
"${ROOT_MODULE}/etcdutl/v3"
"${ROOT_MODULE}/etcdctl/v3"
"${ROOT_MODULE}/tests/v3"
"${ROOT_MODULE}/v3")
echo "${modules[@]}"
}
function modules_exp() {
for m in $(modules); do
echo -n "${m}/... "
done
}
# run_for_modules [cmd]
# run given command across all modules and packages
# (unless the set is limited using ${PKG} or / ${USERMOD})
function run_for_modules {
local pkg="${PKG:-./...}"
if [ -z "${USERMOD:-}" ]; then
for m in $(module_dirs); do
run_for_module "${m}" "$@" "${pkg}" || return "$?"
done
else
run_for_module "${USERMOD}" "$@" "${pkg}" || return "$?"
fi
}
#### Running go test ########
# go_test [packages] [mode] [flags_for_package_func] [$@]
# [mode] supports 3 states:
# - "parallel": fastest as concurrently processes multiple packages, but silent
# till the last package. See: https://github.com/golang/go/issues/2731
# - "keep_going" : executes tests package by package, but postpones reporting error to the last
# - "fail_fast" : executes tests packages 1 by 1, exits on the first failure.
#
# [flags_for_package_func] is a name of function that takes list of packages as parameter
# and computes additional flags to the go_test commands.
# Use 'true' or ':' if you dont need additional arguments.
#
# depends on the VERBOSE top-level variable.
#
# Example:
# go_test "./..." "keep_going" ":" --short
#
# The function returns != 0 code in case of test failure.
function go_test {
local packages="${1}"
local mode="${2}"
local flags_for_package_func="${3}"
shift 3
local goTestFlags=""
local goTestEnv=""
if [ "${VERBOSE:-}" == "1" ]; then
goTestFlags="-v"
fi
# Expanding patterns (like ./...) into list of packages
local unpacked_packages=("${packages}")
if [ "${mode}" != "parallel" ]; then
# shellcheck disable=SC2207
# shellcheck disable=SC2086
if ! unpacked_packages=($(go list ${packages})); then
log_error "Cannot resolve packages: ${packages}"
return 255
fi
fi
local failures=""
# execution of tests against packages:
for pkg in "${unpacked_packages[@]}"; do
local additional_flags
# shellcheck disable=SC2086
additional_flags=$(${flags_for_package_func} ${pkg})
# shellcheck disable=SC2206
local cmd=( go test ${goTestFlags} ${additional_flags} "$@" ${pkg} )
# shellcheck disable=SC2086
if ! run env ${goTestEnv} "${cmd[@]}" ; then
if [ "${mode}" != "keep_going" ]; then
return 2
else
failures=("${failures[@]}" "${pkg}")
fi
fi
done
if [ -n "${failures[*]}" ] ; then
log_error -e "ERROR: Tests for following packages failed:\\n ${failures[*]}"
return 2
fi
}
#### Other ####
# tool_exists [tool] [instruction]
# Checks whether given [tool] is installed. In case of failure,
# prints a warning with installation [instruction] and returns !=0 code.
#
# WARNING: This depend on "any" version of the 'binary' that might be tricky
# from hermetic build perspective. For go binaries prefer 'tool_go_run'
function tool_exists {
local tool="${1}"
local instruction="${2}"
if ! command -v "${tool}" >/dev/null; then
log_warning "Tool: '${tool}' not found on PATH. ${instruction}"
return 255
fi
}
# tool_get_bin [tool] - returns absolute path to a tool binary (or returns error)
function tool_get_bin {
local tool="$1"
local pkg_part="$1"
if [[ "$tool" == *"@"* ]]; then
pkg_part=$(echo "${tool}" | cut -d'@' -f1)
# shellcheck disable=SC2086
run go install ${GOBINARGS:-} "${tool}" || return 2
else
# shellcheck disable=SC2086
run_for_module ./tools/mod run go install ${GOBINARGS:-} "${tool}" || return 2
fi
# remove the version suffix, such as removing "/v3" from "go.etcd.io/etcd/v3".
local cmd_base_name
cmd_base_name=$(basename "${pkg_part}")
if [[ ${cmd_base_name} =~ ^v[0-9]*$ ]]; then
pkg_part=$(dirname "${pkg_part}")
fi
run_for_module ./tools/mod go list -f '{{.Target}}' "${pkg_part}"
}
# tool_pkg_dir [pkg] - returns absolute path to a directory that stores given pkg.
# The pkg versions must be defined in ./tools/mod directory.
function tool_pkg_dir {
run_for_module ./tools/mod run go list -f '{{.Dir}}' "${1}"
}
# tool_get_bin [tool]
function run_go_tool {
local cmdbin
if ! cmdbin=$(tool_get_bin "${1}"); then
return 2
fi
shift 1
run "${cmdbin}" "$@" || return 2
}
# assert_no_git_modifications fails if there are any uncommited changes.
function assert_no_git_modifications {
log_callout "Making sure everything is committed."
if ! git diff --cached --exit-code; then
log_error "Found staged by uncommited changes. Do commit/stash your changes first."
return 2
fi
if ! git diff --exit-code; then
log_error "Found unstaged and uncommited changes. Do commit/stash your changes first."
return 2
fi
}
# makes sure that the current branch is in sync with the origin branch:
# - no uncommitted nor unstaged changes
# - no differencing commits in relation to the origin/$branch
function git_assert_branch_in_sync {
local branch
# TODO: When git 2.22 popular, change to:
# branch=$(git branch --show-current)
branch=$(run git rev-parse --abbrev-ref HEAD)
log_callout "Verify the current branch '${branch}' is clean"
if [[ $(run git status --porcelain --untracked-files=no) ]]; then
log_error "The workspace in '$(pwd)' for branch: ${branch} has uncommitted changes"
log_error "Consider cleaning up / renaming this directory or (cd $(pwd) && git reset --hard)"
return 2
fi
log_callout "Verify the current branch '${branch}' is in sync with the 'origin/${branch}'"
if [ -n "${branch}" ]; then
ref_local=$(run git rev-parse "${branch}")
ref_origin=$(run git rev-parse "origin/${branch}")
if [ "x${ref_local}" != "x${ref_origin}" ]; then
log_error "In workspace '$(pwd)' the branch: ${branch} diverges from the origin."
log_error "Consider cleaning up / renaming this directory or (cd $(pwd) && git reset --hard origin/${branch})"
return 2
fi
else
log_warning "Cannot verify consistency with the origin, as git is on detached branch."
fi
}
# The version present in the .go-verion is the default version that test and build scripts will use.
# However, it is possible to control the version that should be used with the help of env vars:
# - FORCE_HOST_GO: if set to a non-empty value, use the version of go installed in system's $PATH.
# - GO_VERSION: desired version of go to be used, might differ from what is present in .go-version.
# If empty, the value defaults to the version in .go-version.
function determine_go_version {
# Borrowing from how Kubernetes does this:
# https://github.com/kubernetes/kubernetes/blob/17854f0e0a153b06f9d0db096e2cd8ab2fa89c11/hack/lib/golang.sh#L510-L520
#
# default GO_VERSION to content of .go-version
GO_VERSION="${GO_VERSION:-"$(cat "${ETCD_ROOT_DIR}/.go-version")"}"
if [ "${GOTOOLCHAIN:-auto}" != 'auto' ]; then
# no-op, just respect GOTOOLCHAIN
:
elif [ -n "${FORCE_HOST_GO:-}" ]; then
export GOTOOLCHAIN='local'
else
GOTOOLCHAIN="go${GO_VERSION}"
export GOTOOLCHAIN
fi
}
determine_go_version
|