Merge pull request #22 from weaveworks/shell-lint

Shell lint
This commit is contained in:
Jonathan Lange
2016-07-13 10:25:59 +01:00
committed by GitHub
14 changed files with 164 additions and 124 deletions

View File

@@ -4,11 +4,15 @@ Included in this repo are tools shared by weave.git and scope.git. They include
- ```cover```: a tool which merges overlapping coverage reports generated by go - ```cover```: a tool which merges overlapping coverage reports generated by go
test test
- ```files-with-type```: a tool to search directories for files of a given
MIME type
- ```lint```: a script to lint Go project; runs various tools like golint, go - ```lint```: a script to lint Go project; runs various tools like golint, go
vet, errcheck etc vet, errcheck etc
- ```rebuild-image```: a script to rebuild docker images when their input files - ```rebuild-image```: a script to rebuild docker images when their input files
change; useful when you using docker images to build your software, but you change; useful when you using docker images to build your software, but you
don't want to build the image every time. don't want to build the image every time.
- ```shell-lint```: a script to lint multiple shell files with
[shellcheck](http://www.shellcheck.net/)
- ```socks```: a simple, dockerised SOCKS proxy for getting your laptop onto - ```socks```: a simple, dockerised SOCKS proxy for getting your laptop onto
the Weave network the Weave network
- ```test```: a script to run all go unit tests in subdirectories, gather the - ```test```: a script to run all go unit tests in subdirectories, gather the

View File

@@ -3,19 +3,18 @@
# merges them and produces a complete report. # merges them and produces a complete report.
set -ex set -ex
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DESTINATION=$1 DESTINATION=$1
FROMDIR=$2 FROMDIR=$2
mkdir -p $DESTINATION mkdir -p "$DESTINATION"
if [ -n "$CIRCLECI" ]; then if [ -n "$CIRCLECI" ]; then
for i in $(seq 1 $(($CIRCLE_NODE_TOTAL - 1))); do for i in $(seq 1 $((CIRCLE_NODE_TOTAL - 1))); do
scp node$i:$FROMDIR/* $DESTINATION || true scp "node$i:$FROMDIR"/* "$DESTINATION" || true
done done
fi fi
go get github.com/weaveworks/build-tools/cover go get github.com/weaveworks/build-tools/cover
cover $DESTINATION/* >profile.cov cover "$DESTINATION"/* >profile.cov
go tool cover -html=profile.cov -o coverage.html go tool cover -html=profile.cov -o coverage.html
go tool cover -func=profile.cov -o coverage.txt go tool cover -func=profile.cov -o coverage.txt
tar czf coverage.tar.gz $DESTINATION tar czf coverage.tar.gz "$DESTINATION"

13
files-with-type Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
# Find all files with a given MIME type.
#
# e.g.
# $ files-with-type text/x-shellscript k8s infra
#
# Assumes `find`, `xargs`, and `file` are all installed.
mime_type=$1
shift
find "$@" -print0 -type f |xargs -0 file --mime-type | grep "${mime_type}" | sed -e 's/:.*$//'

View File

@@ -24,14 +24,14 @@ export INVARIANT=${INVARIANT:-}
export CONTINUE=${CONTINUE:-} export CONTINUE=${CONTINUE:-}
args="$(getopt -n "$0" -l \ args="$(getopt -n "$0" -l \
verbose,help,stop,discover,invariant,continue vhxdic $*)" \ verbose,help,stop,discover,invariant,continue vhxdic "$@")" \
|| exit -1 || exit -1
for arg in $args; do for arg in $args; do
case "$arg" in case "$arg" in
-h) -h)
echo "$0 [-vxidc]" \ echo "$0 [-vxidc]" \
"[--verbose] [--stop] [--invariant] [--discover] [--continue]" "[--verbose] [--stop] [--invariant] [--discover] [--continue]"
echo "`sed 's/./ /g' <<< "$0"` [-h] [--help]" echo "$(sed 's/./ /g' <<< "$0") [-h] [--help]"
exit 0;; exit 0;;
--help) --help)
cat <<EOF cat <<EOF
@@ -83,17 +83,17 @@ assert_end() {
# to get report_time split tests_time on 2 substrings: # to get report_time split tests_time on 2 substrings:
# ${tests_time:0:${#tests_time}-9} - seconds # ${tests_time:0:${#tests_time}-9} - seconds
# ${tests_time:${#tests_time}-9:3} - milliseconds # ${tests_time:${#tests_time}-9:3} - milliseconds
[[ -z "$INVARIANT" ]] \ if [[ -z "$INVARIANT" ]]; then
&& report_time=" in ${tests_time:0:${#tests_time}-9}.${tests_time:${#tests_time}-9:3}s" \ report_time=" in ${tests_time:0:${#tests_time}-9}.${tests_time:${#tests_time}-9:3}s"
|| report_time= else
report_time=
fi
if [[ "$tests_failed" -eq 0 ]]; then if [[ "$tests_failed" -eq 0 ]]; then
echo "all $tests passed$report_time." echo "all $tests passed$report_time."
else else
for error in "${tests_errors[@]}"; do echo "$error"; done for error in "${tests_errors[@]}"; do echo "$error"; done
echo "$tests_failed of $tests failed$report_time." echo "$tests_failed of $tests failed$report_time."
fi fi
tests_failed_previous=$tests_failed
[[ $tests_failed -gt 0 ]] && tests_suite_status=1 [[ $tests_failed -gt 0 ]] && tests_suite_status=1
_assert_reset _assert_reset
} }
@@ -103,7 +103,7 @@ assert() {
(( tests_ran++ )) || : (( tests_ran++ )) || :
[[ -z "$DISCOVERONLY" ]] || return [[ -z "$DISCOVERONLY" ]] || return
expected=$(echo -ne "${2:-}") expected=$(echo -ne "${2:-}")
result="$(eval 2>/dev/null $1 <<< ${3:-})" || true result="$(eval 2>/dev/null "$1" <<< "${3:-}")" || true
if [[ "$result" == "$expected" ]]; then if [[ "$result" == "$expected" ]]; then
[[ -z "$DEBUG" ]] || echo -n . [[ -z "$DEBUG" ]] || echo -n .
return return
@@ -119,7 +119,7 @@ assert_raises() {
(( tests_ran++ )) || : (( tests_ran++ )) || :
[[ -z "$DISCOVERONLY" ]] || return [[ -z "$DISCOVERONLY" ]] || return
status=0 status=0
(eval $1 <<< ${3:-}) > /dev/null 2>&1 || status=$? (eval "$1" <<< "${3:-}") > /dev/null 2>&1 || status=$?
expected=${2:-0} expected=${2:-0}
if [[ "$status" -eq "$expected" ]]; then if [[ "$status" -eq "$expected" ]]; then
[[ -z "$DEBUG" ]] || echo -n . [[ -z "$DEBUG" ]] || echo -n .
@@ -143,7 +143,7 @@ _assert_fail() {
skip_if() { skip_if() {
# skip_if <command ..> # skip_if <command ..>
(eval $@) > /dev/null 2>&1 && status=0 || status=$? (eval "$@") > /dev/null 2>&1 && status=0 || status=$?
[[ "$status" -eq 0 ]] || return [[ "$status" -eq 0 ]] || return
skip skip
} }

View File

@@ -7,15 +7,15 @@
set -e set -e
: ${KEY_FILE:=/tmp/gce_private_key.json} : "${KEY_FILE:=/tmp/gce_private_key.json}"
: ${SSH_KEY_FILE:=$HOME/.ssh/gce_ssh_key} : "${SSH_KEY_FILE:=$HOME/.ssh/gce_ssh_key}"
: ${IMAGE:=ubuntu-14-04} : "${IMAGE:=ubuntu-14-04}"
: ${ZONE:=us-central1-a} : "${ZONE:=us-central1-a}"
: ${PROJECT:=} : "${PROJECT:=}"
: ${TEMPLATE_NAME:=} : "${TEMPLATE_NAME:=}"
: ${NUM_HOSTS:=} : "${NUM_HOSTS:=}"
if [ -z "${PROJECT}" -o -z "${NUM_HOSTS}" -o -z "${TEMPLATE_NAME}" ]; then if [ -z "${PROJECT}" ] || [ -z "${NUM_HOSTS}" ] || [ -z "${TEMPLATE_NAME}" ]; then
echo "Must specify PROJECT, NUM_HOSTS and TEMPLATE_NAME" echo "Must specify PROJECT, NUM_HOSTS and TEMPLATE_NAME"
exit 1 exit 1
fi fi
@@ -26,21 +26,21 @@ if [ -n "$CIRCLECI" ]; then
fi fi
# Setup authentication # Setup authentication
gcloud auth activate-service-account --key-file $KEY_FILE 1>/dev/null gcloud auth activate-service-account --key-file "$KEY_FILE" 1>/dev/null
gcloud config set project $PROJECT gcloud config set project "$PROJECT"
function vm_names { function vm_names {
local names= local names=
for i in $(seq 1 $NUM_HOSTS); do for i in $(seq 1 "$NUM_HOSTS"); do
names="host$i$SUFFIX $names" names=( "host$i$SUFFIX" "${names[@]}" )
done done
echo "$names" echo "${names[@]}"
} }
# Delete all vms in this account # Delete all vms in this account
function destroy { function destroy {
names="$(vm_names)" names="$(vm_names)"
if [ $(gcloud compute instances list --zone $ZONE -q $names | wc -l) -le 1 ] ; then if [ "$(gcloud compute instances list --zone "$ZONE" -q "$names" | wc -l)" -le 1 ] ; then
return 0 return 0
fi fi
for i in {0..10}; do for i in {0..10}; do
@@ -60,23 +60,23 @@ function destroy {
} }
function internal_ip { function internal_ip {
jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].networkIP" $1 jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].networkIP" "$1"
} }
function external_ip { function external_ip {
jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].accessConfigs[0].natIP" $1 jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].accessConfigs[0].natIP" "$1"
} }
function try_connect { function try_connect {
for i in {0..10}; do for i in {0..10}; do
ssh -t $1 true && return ssh -t "$1" true && return
sleep 2 sleep 2
done done
} }
function install_docker_on { function install_docker_on {
name=$1 name=$1
ssh -t $name sudo bash -x -s <<EOF ssh -t "$name" sudo bash -x -s <<EOF
curl -sSL https://get.docker.com/gpg | sudo apt-key add - curl -sSL https://get.docker.com/gpg | sudo apt-key add -
curl -sSL https://get.docker.com/ | sh curl -sSL https://get.docker.com/ | sh
apt-get update -qq; apt-get update -qq;
@@ -87,72 +87,72 @@ service docker restart
EOF EOF
# It seems we need a short delay for docker to start up, so I put this in # It seems we need a short delay for docker to start up, so I put this in
# a separate ssh connection. This installs nsenter. # a separate ssh connection. This installs nsenter.
ssh -t $name sudo docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter ssh -t "$name" sudo docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
} }
function copy_hosts { function copy_hosts {
hostname=$1 hostname=$1
hosts=$2 hosts=$2
cat $hosts | ssh -t "$hostname" "sudo -- sh -c \"cat >>/etc/hosts\"" ssh -t "$hostname" "sudo -- sh -c \"cat >>/etc/hosts\"" < "$hosts"
} }
# Create new set of VMs # Create new set of VMs
function setup { function setup {
destroy destroy
names="$(vm_names)" names=( $(vm_names) )
gcloud compute instances create $names --image $TEMPLATE_NAME --zone $ZONE gcloud compute instances create "${names[@]}" --image "$TEMPLATE_NAME" --zone "$ZONE"
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE gcloud compute config-ssh --ssh-key-file "$SSH_KEY_FILE"
sed -i '/UserKnownHostsFile=\/dev\/null/d' ~/.ssh/config sed -i '/UserKnownHostsFile=\/dev\/null/d' ~/.ssh/config
# build an /etc/hosts file for these vms # build an /etc/hosts file for these vms
hosts=$(mktemp hosts.XXXXXXXXXX) hosts=$(mktemp hosts.XXXXXXXXXX)
json=$(mktemp json.XXXXXXXXXX) json=$(mktemp json.XXXXXXXXXX)
gcloud compute instances list --format=json >$json gcloud compute instances list --format=json > "$json"
for name in $names; do for name in "${names[@]}"; do
echo "$(internal_ip $json $name) $name.$ZONE.$PROJECT" >>$hosts echo "$(internal_ip "$json" "$name") $name.$ZONE.$PROJECT" >> "$hosts"
done done
for name in $names; do for name in "${names[@]}"; do
hostname="$name.$ZONE.$PROJECT" hostname="$name.$ZONE.$PROJECT"
# Add the remote ip to the local /etc/hosts # Add the remote ip to the local /etc/hosts
sudo sed -i "/$hostname/d" /etc/hosts sudo sed -i "/$hostname/d" /etc/hosts
sudo sh -c "echo \"$(external_ip $json $name) $hostname\" >>/etc/hosts" sudo sh -c "echo \"$(external_ip "$json" "$name") $hostname\" >>/etc/hosts"
try_connect $hostname try_connect "$hostname"
copy_hosts $hostname $hosts & copy_hosts "$hostname" "$hosts" &
done done
wait wait
rm $hosts $json rm "$hosts" "$json"
} }
function make_template { function make_template {
gcloud compute instances create $TEMPLATE_NAME --image $IMAGE --zone $ZONE gcloud compute instances create "$TEMPLATE_NAME" --image "$IMAGE" --zone "$ZONE"
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE gcloud compute config-ssh --ssh-key-file "$SSH_KEY_FILE"
name="$TEMPLATE_NAME.$ZONE.$PROJECT" name="$TEMPLATE_NAME.$ZONE.$PROJECT"
try_connect $name try_connect "$name"
install_docker_on $name install_docker_on "$name"
gcloud -q compute instances delete $TEMPLATE_NAME --keep-disks boot --zone $ZONE gcloud -q compute instances delete "$TEMPLATE_NAME" --keep-disks boot --zone "$ZONE"
gcloud compute images create $TEMPLATE_NAME --source-disk $TEMPLATE_NAME --source-disk-zone $ZONE gcloud compute images create "$TEMPLATE_NAME" --source-disk "$TEMPLATE_NAME" --source-disk-zone "$ZONE"
} }
function hosts { function hosts {
hosts= hosts=
args= args=
json=$(mktemp json.XXXXXXXXXX) json=$(mktemp json.XXXXXXXXXX)
gcloud compute instances list --format=json >$json gcloud compute instances list --format=json > "$json"
for name in $(vm_names); do for name in $(vm_names); do
hostname="$name.$ZONE.$PROJECT" hostname="$name.$ZONE.$PROJECT"
hosts="$hostname $hosts" hosts=( $hostname "${hosts[@]}" )
args="--add-host=$hostname:$(internal_ip $json $name) $args" args=( "--add-host=$hostname:$(internal_ip "$json" "$name")" "${args[@]}" )
done done
echo export SSH=\"ssh -l vagrant\" echo export SSH=\"ssh -l vagrant\"
echo export HOSTS=\"$hosts\" echo "export HOSTS=\"${hosts[*]}\""
echo export ADD_HOST_ARGS=\"$args\" echo "export ADD_HOST_ARGS=\"${args[*]}\""
rm $json rm "$json"
} }
case "$1" in case "$1" in
@@ -170,7 +170,7 @@ destroy)
make_template) make_template)
# see if template exists # see if template exists
if ! gcloud compute images list | grep $PROJECT | grep $TEMPLATE_NAME; then if ! gcloud compute images list | grep "$PROJECT" | grep "$TEMPLATE_NAME"; then
make_template make_template
fi fi
esac esac

View File

@@ -1,6 +1,9 @@
#!/bin/bash #!/bin/bash
set -ex
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090
. "$DIR/config.sh" . "$DIR/config.sh"
whitely echo Sanity checks whitely echo Sanity checks
@@ -10,18 +13,19 @@ if ! bash "$DIR/sanity_check.sh"; then
fi fi
whitely echo ...ok whitely echo ...ok
TESTS="${@:-$(find . -name '*_test.sh')}" # shellcheck disable=SC2068
RUNNER_ARGS="" TESTS=( ${@:-$(find . -name '*_test.sh')} )
RUNNER_ARGS=( )
# If running on circle, use the scheduler to work out what tests to run # If running on circle, use the scheduler to work out what tests to run
if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" ]; then if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ]; then
RUNNER_ARGS="$RUNNER_ARGS -scheduler" RUNNER_ARGS=( "${RUNNER_ARGS[@]}" -scheduler )
fi fi
# If running on circle or PARALLEL is not empty, run tests in parallel # If running on circle or PARALLEL is not empty, run tests in parallel
if [ -n "$CIRCLECI" -o -n "$PARALLEL" ]; then if [ -n "$CIRCLECI" ] || [ -n "$PARALLEL" ]; then
RUNNER_ARGS="$RUNNER_ARGS -parallel" RUNNER_ARGS=( "${RUNNER_ARGS[@]}" -parallel )
fi fi
make -C ${DIR}/../runner make -C "${DIR}/../runner"
HOSTS="$HOSTS" "${DIR}/../runner/runner" $RUNNER_ARGS $TESTS HOSTS="$HOSTS" "${DIR}/../runner/runner" "${RUNNER_ARGS[@]}" "${TESTS[@]}"

View File

@@ -1,5 +1,5 @@
#! /bin/bash #! /bin/bash
# shellcheck disable=SC1091
. ./config.sh . ./config.sh
set -e set -e
@@ -7,7 +7,7 @@ set -e
whitely echo Ping each host from the other whitely echo Ping each host from the other
for host in $HOSTS; do for host in $HOSTS; do
for other in $HOSTS; do for other in $HOSTS; do
[ $host = $other ] || run_on $host $PING $other [ "$host" = "$other" ] || run_on "$host" "$PING" "$other"
done done
done done
@@ -15,12 +15,12 @@ whitely echo Check we can reach docker
for host in $HOSTS; do for host in $HOSTS; do
echo echo
echo Host Version Info: $host echo "Host Version Info: $host"
echo ===================================== echo "====================================="
echo "# docker version" echo "# docker version"
docker_on $host version docker_on "$host" version
echo "# docker info" echo "# docker info"
docker_on $host info docker_on "$host" info
echo "# weave version" echo "# weave version"
weave_on $host version weave_on "$host" version
done done

12
lint
View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# This scipt lints go files for common errors. # This scipt lints go files for common errors.
# #
# Its runs gofmt and go vet, and optionally golint and # It runs gofmt and go vet, and optionally golint and
# gocyclo, if they are installed. # gocyclo, if they are installed.
# #
# With no arguments, it lints the current files staged # With no arguments, it lints the current files staged
@@ -41,7 +41,7 @@ function spell_check {
local lint_result=0 local lint_result=0
# we don't want to spell check tar balls or binaries # we don't want to spell check tar balls or binaries
if file $filename | grep executable >/dev/null 2>&1; then if file "$filename" | grep executable >/dev/null 2>&1; then
return $lint_result return $lint_result
fi fi
if [[ $filename == *".tar" || $filename == *".gz" ]]; then if [[ $filename == *".tar" || $filename == *".gz" ]]; then
@@ -63,11 +63,11 @@ function spell_check {
function test_mismatch { function test_mismatch {
filename="$1" filename="$1"
package=$(grep '^package ' $filename | awk '{print $2}') package=$(grep '^package ' "$filename" | awk '{print $2}')
local lint_result=0 local lint_result=0
if [[ $package == "main" ]]; then if [[ $package == "main" ]]; then
continue # in package main, all bets are off return # in package main, all bets are off
fi fi
if [[ $filename == *"_internal_test.go" ]]; then if [[ $filename == *"_internal_test.go" ]]; then
@@ -115,7 +115,7 @@ function lint_go {
# don't have it installed. Also never blocks a commit, # don't have it installed. Also never blocks a commit,
# it just warns. # it just warns.
if type gocyclo >/dev/null 2>&1; then if type gocyclo >/dev/null 2>&1; then
gocyclo -over 25 "${filename}" | while read line; do gocyclo -over 25 "${filename}" | while read -r line; do
echo "${filename}": higher than 25 cyclomatic complexity - "${line}" echo "${filename}": higher than 25 cyclomatic complexity - "${line}"
done done
fi fi
@@ -158,7 +158,7 @@ function lint {
function lint_files { function lint_files {
local lint_result=0 local lint_result=0
while read filename; do while read -r filename; do
lint "${filename}" || lint_result=1 lint "${filename}" || lint_result=1
done done
exit $lint_result exit $lint_result

View File

@@ -3,7 +3,7 @@
set -e set -e
set -o pipefail set -o pipefail
: ${PRODUCT:=} : "${PRODUCT:=}"
fatal() { fatal() {
echo "$@" >&2 echo "$@" >&2

View File

@@ -5,37 +5,39 @@
set -eux set -eux
IMAGENAME=$1 IMAGENAME=$1
SAVEDNAME=$(echo $IMAGENAME | sed "s/[\/\-]/\./g") # shellcheck disable=SC2001
SAVEDNAME=$(echo "$IMAGENAME" | sed "s/[\/\-]/\./g")
IMAGEDIR=$2 IMAGEDIR=$2
shift 2 shift 2
INPUTFILES=$@ INPUTFILES=( "$@" )
CACHEDIR=$HOME/docker/ CACHEDIR=$HOME/docker/
# Rebuild the image # Rebuild the image
rebuild() { rebuild() {
mkdir -p $CACHEDIR mkdir -p "$CACHEDIR"
rm $CACHEDIR/$SAVEDNAME* || true rm "$CACHEDIR/$SAVEDNAME"* || true
docker build -t $IMAGENAME $IMAGEDIR docker build -t "$IMAGENAME" "$IMAGEDIR"
docker save $IMAGENAME:latest | gzip - > $CACHEDIR/$SAVEDNAME-$CIRCLE_SHA1.gz docker save "$IMAGENAME:latest" | gzip - > "$CACHEDIR/$SAVEDNAME-$CIRCLE_SHA1.gz"
} }
# Get the revision the cached image was build at # Get the revision the cached image was build at
cached_image_rev() { cached_image_rev() {
find $CACHEDIR -name "$SAVEDNAME-*" -type f | sed -n 's/^[^\-]*\-\([a-z0-9]*\).gz$/\1/p' find "$CACHEDIR" -name "$SAVEDNAME-*" -type f | sed -n 's/^[^\-]*\-\([a-z0-9]*\).gz$/\1/p'
} }
# Have there been any revision between $1 and $2 # Have there been any revision between $1 and $2
has_changes() { has_changes() {
local rev1=$1 local rev1=$1
local rev2=$2 local rev2=$2
local changes=$(git diff --oneline $rev1..$rev2 -- $INPUTFILES | wc -l) local changes
changes=$(git diff --oneline "$rev1..$rev2" -- "${INPUTFILES[@]}" | wc -l)
[ "$changes" -gt 0 ] [ "$changes" -gt 0 ]
} }
commit_timestamp() { commit_timestamp() {
local rev=$1 local rev=$1
git show -s --format=%ct $rev git show -s --format=%ct "$rev"
} }
cached_revision=$(cached_image_rev) cached_revision=$(cached_image_rev)
@@ -46,14 +48,14 @@ if [ -z "$cached_revision" ]; then
fi fi
echo ">>> Found cached image rev $cached_revision" echo ">>> Found cached image rev $cached_revision"
if has_changes $cached_revision $CIRCLE_SHA1 ; then if has_changes "$cached_revision" "$CIRCLE_SHA1" ; then
echo ">>> Found changes, rebuilding" echo ">>> Found changes, rebuilding"
rebuild rebuild
exit 0 exit 0
fi fi
IMAGE_TIMEOUT="$(( 3 * 24 * 60 * 60 ))" IMAGE_TIMEOUT="$(( 3 * 24 * 60 * 60 ))"
if [ "$(commit_timestamp $cached_revision)" -lt "${IMAGE_TIMEOUT}" ]; then if [ "$(commit_timestamp "$cached_revision")" -lt "${IMAGE_TIMEOUT}" ]; then
echo ">>> Image is more the 24hrs old; rebuilding" echo ">>> Image is more the 24hrs old; rebuilding"
rebuild rebuild
exit 0 exit 0
@@ -61,4 +63,4 @@ fi
# we didn't rebuild; import cached version # we didn't rebuild; import cached version
echo ">>> No changes found, importing cached image" echo ">>> No changes found, importing cached image"
zcat $CACHEDIR/$SAVEDNAME-$cached_revision.gz | docker load zcat "$CACHEDIR/$SAVEDNAME-$cached_revision.gz" | docker load

View File

@@ -262,9 +262,10 @@ func main() {
verbose = true verbose = true
} }
tests, err := getTests(mflag.Args()) testArgs := mflag.Args()
tests, err := getTests(testArgs)
if err != nil { if err != nil {
fmt.Printf("Error parsing tests: %v\n", err) fmt.Printf("Error parsing tests: %v (%v)\n", err, testArgs)
os.Exit(1) os.Exit(1)
} }

13
shell-lint Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
# Lint all shell files in given directories with `shellcheck`.
#
# e.g.
# $ shell-lint infra k8s
#
# Depends on:
# - shellcheck
# - files-with-type
# - file >= 5.22
"$(dirname "${BASH_SOURCE[0]}")/files-with-type" text/x-shellscript "$@" | xargs shellcheck

View File

@@ -10,14 +10,17 @@ fi
HOST=$1 HOST=$1
echo "Starting proxy container..." echo "Starting proxy container..."
PROXY_CONTAINER=$(ssh $HOST weave run -d weaveworks/socksproxy) PROXY_CONTAINER=$(ssh "$HOST" weave run -d weaveworks/socksproxy)
function finish { function finish {
echo "Removing proxy container.." echo "Removing proxy container.."
ssh $HOST docker rm -f $PROXY_CONTAINER # shellcheck disable=SC2029
ssh "$HOST" docker rm -f "$PROXY_CONTAINER"
} }
trap finish EXIT trap finish EXIT
PROXY_IP=$(ssh $HOST -- "docker inspect --format='{{.NetworkSettings.IPAddress}}' $PROXY_CONTAINER") # shellcheck disable=SC2029
PROXY_IP=$(ssh "$HOST" -- "docker inspect --format='{{.NetworkSettings.IPAddress}}' $PROXY_CONTAINER")
echo 'Please configure your browser for proxy http://localhost:8080/proxy.pac' echo 'Please configure your browser for proxy http://localhost:8080/proxy.pac'
ssh -L8000:$PROXY_IP:8000 -L8080:$PROXY_IP:8080 $HOST docker attach $PROXY_CONTAINER # shellcheck disable=SC2029
ssh "-L8000:$PROXY_IP:8000" "-L8080:$PROXY_IP:8080" "$HOST" docker attach "$PROXY_CONTAINER"

49
test
View File

@@ -1,9 +1,9 @@
#!/bin/bash #!/bin/bash
set -e set -ex
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GO_TEST_ARGS="-tags netgo -cpu 4 -timeout 8m" GO_TEST_ARGS=( -tags netgo -cpu 4 -timeout 8m )
SLOW= SLOW=
NO_GO_GET= NO_GO_GET=
@@ -28,66 +28,67 @@ while [ $# -gt 0 ]; do
esac esac
done done
if [ -n "$SLOW" -o -n "$CIRCLECI" ]; then if [ -n "$SLOW" ] || [ -n "$CIRCLECI" ]; then
SLOW=true SLOW=true
fi fi
if [ -n "$SLOW" ]; then if [ -n "$SLOW" ]; then
GO_TEST_ARGS="$GO_TEST_ARGS -race -covermode=atomic" GO_TEST_ARGS=( "${GO_TEST_ARGS[@]}" -race -covermode=atomic )
# shellcheck disable=SC2153
if [ -n "$COVERDIR" ] ; then if [ -n "$COVERDIR" ] ; then
coverdir="$COVERDIR" coverdir="$COVERDIR"
else else
coverdir=$(mktemp -d coverage.XXXXXXXXXX) coverdir=$(mktemp -d coverage.XXXXXXXXXX)
fi fi
mkdir -p $coverdir mkdir -p "$coverdir"
fi fi
fail=0 fail=0
TESTDIRS=$(find . -type f -name '*_test.go' | xargs -n1 dirname | grep -vE '^\./(\.git|vendor|prog|experimental)/' | sort -u) TESTDIRS=( $(find . -type f -name '*_test.go' -print0 | xargs -0 -n1 dirname | grep -vE '^\./(\.git|vendor|prog|experimental)/' | sort -u) )
# If running on circle, use the scheduler to work out what tests to run on what shard # If running on circle, use the scheduler to work out what tests to run on what shard
if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" -a -x "$DIR/sched" ]; then if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ] && [ -x "$DIR/sched" ]; then
PREFIX=$(go list -e ./ | sed -e 's/\//-/g') PREFIX=$(go list -e ./ | sed -e 's/\//-/g')
TESTDIRS=$(echo $TESTDIRS | "$DIR/sched" sched $PREFIX-$CIRCLE_BUILD_NUM $CIRCLE_NODE_TOTAL $CIRCLE_NODE_INDEX) TESTDIRS=( $(echo "${TESTDIRS[@]}" | "$DIR/sched" sched "$PREFIX-$CIRCLE_BUILD_NUM" "$CIRCLE_NODE_TOTAL" "$CIRCLE_NODE_INDEX") )
echo $TESTDIRS echo "${TESTDIRS[@]}"
fi fi
PACKAGE_BASE=$(go list -e ./) PACKAGE_BASE=$(go list -e ./)
# Speed up the tests by compiling and installing their dependancies first. # Speed up the tests by compiling and installing their dependencies first.
go test -i $GO_TEST_ARGS $TESTDIRS go test -i "${GO_TEST_ARGS[@]}" "${TESTDIRS[@]}"
for dir in $TESTDIRS; do for dir in "${TESTDIRS[@]}"; do
if [ -z "$NO_GO_GET" ]; then if [ -z "$NO_GO_GET" ]; then
go get -t -tags netgo $dir go get -t -tags netgo "$dir"
fi fi
GO_TEST_ARGS_RUN="$GO_TEST_ARGS" GO_TEST_ARGS_RUN=( "${GO_TEST_ARGS[@]}" )
if [ -n "$SLOW" ]; then if [ -n "$SLOW" ]; then
COVERPKGS=$( (go list $dir; go list -f '{{join .Deps "\n"}}' $dir | grep -v "vendor" | grep "^$PACKAGE_BASE/") | paste -s -d, -) COVERPKGS=$( (go list "$dir"; go list -f '{{join .Deps "\n"}}' "$dir" | grep -v "vendor" | grep "^$PACKAGE_BASE/") | paste -s -d, -)
output=$(mktemp $coverdir/unit.XXXXXXXXXX) output=$(mktemp "$coverdir/unit.XXXXXXXXXX")
GO_TEST_ARGS_RUN="$GO_TEST_ARGS -coverprofile=$output -coverpkg=$COVERPKGS" GO_TEST_ARGS_RUN=( "${GO_TEST_ARGS[@]}" -coverprofile=$output -coverpkg=$COVERPKGS )
fi fi
START=$(date +%s) START=$(date +%s)
if ! go test $GO_TEST_ARGS_RUN $dir; then if ! go test "${GO_TEST_ARGS_RUN[@]}" "$dir"; then
fail=1 fail=1
fi fi
RUNTIME=$(( $(date +%s) - $START )) RUNTIME=$(( $(date +%s) - START ))
# Report test runtime when running on circle, to help scheduler # Report test runtime when running on circle, to help scheduler
if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" -a -x "$DIR/sched" ]; then if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ] && [ -x "$DIR/sched" ]; then
"$DIR/sched" time $dir $RUNTIME "$DIR/sched" time "$dir" $RUNTIME
fi fi
done done
if [ -n "$SLOW" -a -z "$COVERDIR" ] ; then if [ -n "$SLOW" ] && [ -z "$COVERDIR" ] ; then
go get github.com/weaveworks/tools/cover go get github.com/weaveworks/tools/cover
cover $coverdir/* >profile.cov cover "$coverdir"/* >profile.cov
rm -rf $coverdir rm -rf "$coverdir"
go tool cover -html=profile.cov -o=coverage.html go tool cover -html=profile.cov -o=coverage.html
go tool cover -func=profile.cov | tail -n1 go tool cover -func=profile.cov | tail -n1
fi fi