Skip to content

Commit

Permalink
Add token validation when necessary, fmt shell code, enable 'gocritic…
Browse files Browse the repository at this point in the history
…s' (#143)

* Add token validation when necessary,
* Add e2e tests,
* Format shell code,
* Enable 'gocritics' and fix errors,
* Add option to run only a specific integration tests
  • Loading branch information
mszostok authored Apr 12, 2022
1 parent a16e4b9 commit 2ae5a4b
Show file tree
Hide file tree
Showing 20 changed files with 337 additions and 163 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
- name: "Integration testing with ${{ matrix.go-version }}"
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_INTEGRATION_TESTS }}
TOKEN_WITH_NO_SCOPES: ${{ secrets.TOKEN_WITH_NO_SCOPES }}
run: |
echo "${{ env.BINARY_PATH }}"
make test-integration
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ run:
linters:
disable-all: true
enable:
- gocritic
- deadcode
- errcheck
- gosimple
Expand All @@ -31,3 +32,13 @@ linters:
- gocyclo

fast: false


linters-settings:
gocritic:
enabled-tags:
- diagnostic
- style
- performance
- experimental
- opinionated
31 changes: 18 additions & 13 deletions hack/compress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ROOT_PATH=$( cd "${CURRENT_DIR}/.." && pwd )
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
readonly CURRENT_DIR
readonly ROOT_PATH

# shellcheck source=./hack/lib/utilities.sh
source "${CURRENT_DIR}/lib/utilities.sh" || { echo 'Cannot load CI utilities.'; exit 1; }
source "${CURRENT_DIR}/lib/utilities.sh" || {
echo 'Cannot load CI utilities.'
exit 1
}

function main() {
# This will find all files (not symlinks) with the executable bit set:
# https://apple.stackexchange.com/a/116371
binariesToCompress=$(find "${ROOT_PATH}/dist" -perm +111 -type f)
# This will find all files (not symlinks) with the executable bit set:
# https://apple.stackexchange.com/a/116371
binariesToCompress=$(find "${ROOT_PATH}/dist" -perm +111 -type f)

shout "Staring compression for: \n$binariesToCompress"
shout "Staring compression for: \n$binariesToCompress"

command -v upx > /dev/null || { echo 'UPX binary not found, skipping compression.'; exit 1; }
command -v upx >/dev/null || {
echo 'UPX binary not found, skipping compression.'
exit 1
}

# I just do not like playing with xargs ¯\_(ツ)_/¯
for i in $binariesToCompress
do
upx --brute "$i"
done
# I just do not like playing with xargs ¯\_(ツ)_/¯
for i in $binariesToCompress; do
upx --brute "$i"
done
}

main
82 changes: 42 additions & 40 deletions hack/run-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ROOT_PATH=$( cd "${CURRENT_DIR}/.." && pwd )
GOLANGCI_LINT_VERSION="v1.44.2"
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
GOLANGCI_LINT_VERSION="v1.45.2"
TMP_DIR=$(mktemp -d)

readonly CURRENT_DIR
Expand All @@ -16,70 +16,72 @@ readonly ROOT_PATH
readonly TMP_DIR

# shellcheck source=./hack/lib/utilities.sh
source "${CURRENT_DIR}/lib/utilities.sh" || { echo 'Cannot load CI utilities.'; exit 1; }
source "${CURRENT_DIR}/lib/utilities.sh" || {
echo 'Cannot load CI utilities.'
exit 1
}

host::install::golangci() {
mkdir -p "${TMP_DIR}/bin"
export PATH="${TMP_DIR}/bin:${PATH}"
mkdir -p "${TMP_DIR}/bin"
export PATH="${TMP_DIR}/bin:${PATH}"

shout "Install the golangci-lint ${GOLANGCI_LINT_VERSION} locally to a tempdir..."
curl -sfSL -o "${TMP_DIR}/golangci-lint.sh" https://install.goreleaser.com/github.com/golangci/golangci-lint.sh
chmod 700 "${TMP_DIR}/golangci-lint.sh"
shout "Install the golangci-lint ${GOLANGCI_LINT_VERSION} locally to a tempdir..."
curl -sfSL -o "${TMP_DIR}/golangci-lint.sh" https://install.goreleaser.com/github.com/golangci/golangci-lint.sh
chmod 700 "${TMP_DIR}/golangci-lint.sh"

"${TMP_DIR}/golangci-lint.sh" -b "${TMP_DIR}/bin" ${GOLANGCI_LINT_VERSION}
"${TMP_DIR}/golangci-lint.sh" -b "${TMP_DIR}/bin" ${GOLANGCI_LINT_VERSION}

echo -e "${GREEN}√ install golangci-lint${NC}"
echo -e "${GREEN}√ install golangci-lint${NC}"
}

golangci::run_checks() {
if [ -z "$(command -v golangci-lint)" ]; then
echo "golangci-lint not found locally. Execute script with env variable INSTALL_DEPS=true"
exit 1
fi
if [ -z "$(command -v golangci-lint)" ]; then
echo "golangci-lint not found locally. Execute script with env variable INSTALL_DEPS=true"
exit 1
fi

GOT_VER=$(golangci-lint version --format=short 2>&1)
if [[ "v${GOT_VER}" != "${GOLANGCI_LINT_VERSION}" ]]; then
echo -e "${RED}✗ golangci-lint version mismatch, expected ${GOLANGCI_LINT_VERSION}, available ${GOT_VER} ${NC}"
exit 1
fi
GOT_VER=$(golangci-lint version --format=short 2>&1)
if [[ "v${GOT_VER}" != "${GOLANGCI_LINT_VERSION}" ]]; then
echo -e "${RED}✗ golangci-lint version mismatch, expected ${GOLANGCI_LINT_VERSION}, available ${GOT_VER} ${NC}"
exit 1
fi

shout "Run golangci-lint checks"
shout "Run golangci-lint checks"

# shellcheck disable=SC2046
golangci-lint run $(golangci::fix_if_requested) "${ROOT_PATH}/..."
# shellcheck disable=SC2046
golangci-lint run $(golangci::fix_if_requested) "${ROOT_PATH}/..."

echo -e "${GREEN}√ run golangci-lint${NC}"
echo -e "${GREEN}√ run golangci-lint${NC}"
}

golangci::fix_if_requested() {
if [[ "${LINT_FORCE_FIX:-x}" == "true" ]]; then
echo "--fix"
fi
if [[ "${LINT_FORCE_FIX:-x}" == "true" ]]; then
echo "--fix"
fi
}

docker::run_dockerfile_checks() {
shout "Run hadolint Dockerfile checks"
docker run --rm -i hadolint/hadolint < "${ROOT_PATH}/Dockerfile"
echo -e "${GREEN}√ run hadolint${NC}"
shout "Run hadolint Dockerfile checks"
docker run --rm -i hadolint/hadolint <"${ROOT_PATH}/Dockerfile"
echo -e "${GREEN}√ run hadolint${NC}"
}


shellcheck::run_checks() {
shout "Run shellcheck checks"
docker run --rm -v "$ROOT_PATH":/mnt koalaman/shellcheck:stable -x ./hack/*.sh
echo -e "${GREEN}√ run shellcheck${NC}"
shout "Run shellcheck checks"
docker run --rm -v "$ROOT_PATH":/mnt koalaman/shellcheck:stable -x ./hack/*.sh
echo -e "${GREEN}√ run shellcheck${NC}"
}

main() {
if [[ "${INSTALL_DEPS:-x}" == "true" ]]; then
host::install::golangci
fi
if [[ "${INSTALL_DEPS:-x}" == "true" ]]; then
host::install::golangci
fi

golangci::run_checks
golangci::run_checks

docker::run_dockerfile_checks
docker::run_dockerfile_checks

shellcheck::run_checks
shellcheck::run_checks
}

main
61 changes: 34 additions & 27 deletions hack/run-test-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,62 @@ set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ROOT_PATH=$( cd "${CURRENT_DIR}/.." && pwd )
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
readonly CURRENT_DIR
readonly ROOT_PATH

# shellcheck source=./hack/lib/utilities.sh
source "${CURRENT_DIR}/lib/utilities.sh" || { echo 'Cannot load CI utilities.'; exit 1; }
source "${CURRENT_DIR}/lib/utilities.sh" || {
echo 'Cannot load CI utilities.'
exit 1
}

pushd "${ROOT_PATH}" > /dev/null
pushd "${ROOT_PATH}" >/dev/null

# Exit handler. This function is called anytime an EXIT signal is received.
# This function should never be explicitly called.
function _trap_exit () {
popd > /dev/null
function _trap_exit() {
popd >/dev/null
}
trap _trap_exit EXIT

function print_info() {
echo -e "${INVERTED}"
echo "USER: ${USER:-"unknown"}"
echo "PATH: ${PATH:-"unknown"}"
echo "GOPATH: ${GOPATH:-"unknown"}"
echo -e "${NC}"
echo -e "${INVERTED}"
echo "USER: ${USER:-"unknown"}"
echo "PATH: ${PATH:-"unknown"}"
echo "GOPATH: ${GOPATH:-"unknown"}"
echo -e "${NC}"
}

function test::integration() {
shout "? go test integration"

# Check if tests passed
# shellcheck disable=SC2046
if ! go test -v -tags=integration ./tests/integration/... $(test::update_golden);
then
echo -e "${RED}✗ go test integration\n${NC}"
exit 1
else
echo -e "${GREEN}√ go test integration${NC}"
fi
shout "? go test integration"

# Check if tests passed
# shellcheck disable=SC2046
if ! go test -v -tags=integration $(test::run::specific) ./tests/integration/... $(test::update_golden); then
echo -e "${RED}✗ go test integration\n${NC}"
exit 1
else
echo -e "${GREEN}√ go test integration${NC}"
fi
}

function test::run::specific() {
if [[ -n "${TEST}" ]]; then
echo "-run=${TEST}"
fi
}
function test::update_golden() {
if [[ "${UPDATE_GOLDEN:-"false"}" == "true" ]]; then
echo "-update"
fi
if [[ "${UPDATE_GOLDEN:-"false"}" == "true" ]]; then
echo "-update"
fi
}

function main() {
print_info
print_info

test::integration
test::integration
}

main
67 changes: 34 additions & 33 deletions hack/run-test-unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,65 @@ set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ROOT_PATH=$( cd "${CURRENT_DIR}/.." && pwd )
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)

readonly CURRENT_DIR
readonly ROOT_PATH

# shellcheck source=./hack/lib/utilities.sh
source "${CURRENT_DIR}/lib/utilities.sh" || { echo 'Cannot load CI utilities.'; exit 1; }
source "${CURRENT_DIR}/lib/utilities.sh" || {
echo 'Cannot load CI utilities.'
exit 1
}

pushd "${ROOT_PATH}" > /dev/null
pushd "${ROOT_PATH}" >/dev/null

# Exit handler. This function is called anytime an EXIT signal is received.
# This function should never be explicitly called.
function _trap_exit () {
popd > /dev/null
function _trap_exit() {
popd >/dev/null
}
trap _trap_exit EXIT

function print_info() {
echo -e "${INVERTED}"
echo "USER: ${USER:-"unknown"}"
echo "PATH: ${PATH:-"unknown"}"
echo "GOPATH: ${GOPATH:-"unknown"}"
echo -e "${NC}"
echo -e "${INVERTED}"
echo "USER: ${USER:-"unknown"}"
echo "PATH: ${PATH:-"unknown"}"
echo "GOPATH: ${GOPATH:-"unknown"}"
echo -e "${NC}"
}

function test::go_modules() {
shout "? go mod tidy"
go mod tidy
STATUS=$(git status --porcelain go.mod go.sum)
if [ -n "$STATUS" ]; then
echo -e "${RED}✗ go mod tidy modified go.mod and/or go.sum${NC}"
exit 1
else
echo -e "${GREEN}√ go mod tidy${NC}"
fi
shout "? go mod tidy"
go mod tidy
STATUS=$(git status --porcelain go.mod go.sum)
if [ -n "$STATUS" ]; then
echo -e "${RED}✗ go mod tidy modified go.mod and/or go.sum${NC}"
exit 1
else
echo -e "${GREEN}√ go mod tidy${NC}"
fi
}


function test::unit() {
shout "? go test"
shout "? go test"

# Check if tests passed
if ! go test -race -coverprofile="${ROOT_PATH}/coverage.txt" ./...;
then
echo -e "${RED}✗ go test\n${NC}"
exit 1
else
echo -e "${GREEN}√ go test${NC}"
fi
# Check if tests passed
if ! go test -race -coverprofile="${ROOT_PATH}/coverage.txt" ./...; then
echo -e "${RED}✗ go test\n${NC}"
exit 1
else
echo -e "${GREEN}√ go test${NC}"
fi
}

function main() {
print_info
print_info

test::go_modules
test::go_modules

test::unit
test::unit
}

main
Loading

0 comments on commit 2ae5a4b

Please sign in to comment.