Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic building and testing scripts #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/_output
image-inspector
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: go

go:
- 1.5.3
- 1.6

install:
- export PATH=$GOPATH/bin:./_tools/etcd/bin:$PATH
- make install-travis

script:
- make verify test-unit

notifications:
irc: "chat.freenode.net#openshift-dev"

sudo: false
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Old-skool build tools.
#
# Targets (see each target for more information):
# all: Build code.
# build: Build code.
# test-unit: Run unit tests.
# clean: Clean up.

OUT_DIR = _output
OUT_PKG_DIR = Godeps/_workspace/pkg

# Build code.
#
# Example:
# make
# make all
all build:
hack/build-go.sh
.PHONY: all build

# Remove all build artifacts.
#
# Example:
# make clean
clean:
rm -rf $(OUT_DIR) $(OUT_PKG_DIR)
.PHONY: clean

# Verify code conventions are properly setup.
#
# Example:
# make verify
verify: build
hack/verify-gofmt.sh
.PHONY: verify

# Run unit tests.
#
# Args:
# WHAT: Directory names to test. All *_test.go files under these
# directories will be run. If not specified, "everything" will be tested.
# TESTS: Same as WHAT.
# GOFLAGS: Extra flags to pass to 'go' when building.
# TESTFLAGS: Extra flags that should only be passed to hack/test-go.sh
#
# Example:
# make test-unit
# make test-unit WHAT=pkg/build GOFLAGS=-v
test-unit:
GOTEST_FLAGS="$(TESTFLAGS)" hack/test-go.sh $(WHAT) $(TESTS)
.PHONY: test-unit

# Install travis dependencies
#
install-travis:
hack/install-tools.sh
.PHONY: install-travis
17 changes: 17 additions & 0 deletions hack/build-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# This script sets up a go workspace locally and builds all go components.

set -o errexit
set -o nounset
set -o pipefail

STARTTIME=$(date +%s)
CODE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${CODE_ROOT}/hack/util.sh"
source "${CODE_ROOT}/hack/common.sh"
ii::log::install_errexit

ii::build::build_binaries "$@"

ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret"
85 changes: 85 additions & 0 deletions hack/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash

# The root of the build/dist directory
readonly II_ROOT=$(
unset CDPATH
ii_root=$(dirname "${BASH_SOURCE}")/..

cd "${ii_root}"
ii_root=`pwd`
if [ -h "${ii_root}" ]; then
readlink "${ii_root}"
else
pwd
fi
)

readonly II_GOPATH=$(
unset CDPATH
cd ${II_ROOT}/../../../..
pwd
)

readonly II_GO_PACKAGE=github.com/simon3z/image-inspector
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now that there is a parent repo for this in openshift. I'll change the package paths to match.

@simon3z would you prefer that I submit PRs there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, it looks like your repo is upstream for openshift? Just let me know if this is wrong. Basically this script is expecting to find the path structure to look like whatever that upstream is just like we require in OpenShift

readonly II_OUTPUT_SUBPATH="${II_OUTPUT_SUBPATH:-_output/local}"
readonly II_OUTPUT="${II_ROOT}/${II_OUTPUT_SUBPATH}"
readonly II_OUTPUT_BINPATH="${II_OUTPUT}/bin"

# ii::build::setup_env will check that the `go` commands is available in
# ${PATH}. If not running on Travis, it will also check that the Go version is
# good enough for the webdav code requirements (1.5+).
#
# Output Vars:
# export GOPATH - A modified GOPATH to our created tree along with extra
# stuff.
# export GOBIN - This is actively unset if already set as we want binaries
# placed in a predictable place.
ii::build::setup_env() {
if [[ -z "$(which go)" ]]; then
cat <<EOF

Can't find 'go' in PATH, please fix and retry.
See http://golang.org/doc/install for installation instructions.

EOF
exit 2
fi

# Travis continuous build uses a head go release that doesn't report
# a version number, so we skip this check on Travis. It's unnecessary
# there anyway.
if [[ "${TRAVIS:-}" != "true" ]]; then
local go_version
go_version=($(go version))
if [[ "${go_version[2]}" < "go1.5" ]]; then
cat <<EOF

Detected Go version: ${go_version[*]}.
image-inspector builds require Go version 1.5 or greater.

EOF
exit 2
fi
fi

unset GOBIN

export GOPATH=${II_ROOT}/Godeps/_workspace:${II_GOPATH}
export II_TARGET_BIN=${II_GOPATH}/bin
}

# Build image-inspector.go binary.
ii::build::build_binaries() {
# Create a sub-shell so that we don't pollute the outer environment
(
# Check for `go` binary and set ${GOPATH}.
ii::build::setup_env

# Making this super simple for now.
local platform="local"
export GOBIN="${II_OUTPUT_BINPATH}/${platform}"

mkdir -p "${II_OUTPUT_BINPATH}/${platform}"
go install image-inspector.go
)
}
24 changes: 24 additions & 0 deletions hack/install-tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

STARTTIME=$(date +%s)
CODE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${OS_ROOT}/hack/common.sh"

GO_VERSION=($(go version))
echo "Detected go version: $(go version)"

go get golang.org/x/tools/cmd/cover github.com/tools/godep golang.org/x/tools/cmd/vet

# Check out a stable commit for go vet in order to version lock it to something we can work with
pushd $GOPATH/src/golang.org/x/tools >/dev/null 2>&1
git fetch
git checkout c262de870b618eed648983aa994b03bc04641c72
popd >/dev/null 2>&1

# Re-install using this version of the tool
go install golang.org/x/tools/cmd/vet


ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret"
131 changes: 131 additions & 0 deletions hack/test-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/bin/bash
#
# This script runs Go language unit tests for the image-inspector repository. Arguments to this script
# are parsed as a list of packages to test until the first argument starting with '-' or '--' is
# found. That argument and all following arguments are interpreted as flags to be passed directly
# to `go test`. If no arguments are given, then "all" packages are tested.
#
# Coverage reports and jUnit XML reports can be generated by this script as well, but both cannot
# be generated at once.
#
# This script consumes the following parameters as environment variables:
# - DRY_RUN: prints all packages that would be tested with the args that would be used and exits
# - TIMEOUT: the timeout for any one unit test (default '60s')
# - DETECT_RACES: toggles the 'go test' race detector (defaults '-race')
# - COVERAGE_SPEC: a set of flags for 'go test' that specify the coverage behavior (default '-cover -covermode=atomic')
# - GOTEST_FLAGS: any other flags to be sent to 'go test'
set -o errexit
set -o nounset
set -o pipefail

function exit_trap() {
local return_code=$?
echo "[DEBUG] Exit trap handler got return code ${return_code}"

end_time=$(date +%s)

if [[ "${return_code}" -eq "0" ]]; then
verb="succeeded"
else
verb="failed"
fi

echo "$0 ${verb} after $((${end_time} - ${start_time})) seconds"
exit "${return_code}"
}

trap exit_trap EXIT

start_time=$(date +%s)
CODE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${CODE_ROOT}/hack/common.sh"
source "${CODE_ROOT}/hack/util.sh"
cd "${CODE_ROOT}"
ii::log::install_errexit
ii::build::setup_env

# Internalize environment variables we consume and default if they're not set
dry_run="${DRY_RUN:-}"
test_timeout="${TIMEOUT:-120s}"
detect_races="${DETECT_RACES:-true}"
coverage_spec="${COVERAGE_SPEC:--cover -covermode atomic}"
gotest_flags="${GOTEST_FLAGS:-}"

# determine if user wanted verbosity
verbose=
if [[ "${gotest_flags}" =~ -v( |$) ]]; then
verbose=true
fi

# Build arguments for 'go test'
if [[ -z "${verbose}" ]]; then
gotest_flags+=" -v"
fi

if [[ "${detect_races}" == "true" ]]; then
gotest_flags+=" -race"
fi

# check to see if user has not disabled coverage mode
if [[ -n "${coverage_spec}" ]]; then
# if we have a coverage spec set, we add it. '-race' implies '-cover -covermode atomic'
# but specifying both at the same time does not lead to an error so we can add both specs
gotest_flags+=" ${coverage_spec}"
fi

# check to see if user has not disabled test timeouts
if [[ -n "${test_timeout}" ]]; then
gotest_flags+=" -timeout ${test_timeout}"
fi

# list_test_packages_under lists all packages containing Golang test files that we want to run as unit tests
# under the given base dir in the OpenShift Origin tree
function list_test_packages_under() {
local basedir=$@

# we do not quote ${basedir} to allow for multiple arguments to be passed in as well as to allow for
# arguments that use expansion, e.g. paths containing brace expansion or wildcards
find ${basedir} -not \( \
\( \
-path 'Godeps' \
-o -path '*_output' \
-o -path '*.git' \
-o -path '*Godeps/*' \
-o -path '*test/*' \
\) -prune \
\) -name '*_test.go' | xargs -n1 dirname | sort -u | xargs -n1 printf "${II_GO_PACKAGE}/%s\n"
}

# Break up the positional arguments into packages that need to be tested and arguments that need to be passed to `go test`
package_args=
for arg in "$@"; do
if [[ "${arg}" =~ -.* ]]; then
# we found an arg that begins with a dash, so we stop interpreting arguments
# henceforth as packages and instead interpret them as flags to give to `go test`
break
fi
# an arg found before the first flag is a package
package_args+=" ${arg}"
shift
done
gotest_flags+=" $*"

# Determine packages to test
godeps_package_prefix="Godeps/_workspace/src/"
test_packages=
if [[ -n "${package_args}" ]]; then
for package in ${package_args}; do
# If we're trying to recursively test a package under Godeps, strip the Godeps prefix so go test can find the packages correctly
if [[ "${package}" == "${godeps_package_prefix}"*"/..." ]]; then
test_packages="${test_packages} ${package:${#godeps_package_prefix}}"
else
test_packages="${test_packages} ${II_GO_PACKAGE}/${package}"
fi
done
else
# If no packages are given to test, we need to generate a list of all packages with unit tests
ii_test_packages="$(list_test_packages_under '*')"
test_packages="${ii_test_packages}"
fi

go test ${gotest_flags} ${test_packages}
Loading