From 887ee3dbce8ec8b4645f85b71e5cf15440352b92 Mon Sep 17 00:00:00 2001 From: Carson Long <12767276+ctlong@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:28:17 +0000 Subject: [PATCH] fix: Update testing and linting scripts - Removes old, unused ops files and bosh-lite scripts. - Simplifies and gets working the testing and linting scripts. - Updates GH actions to use the scripts. (cherry picked from commit 5a59fd07e73cbaccb9ef572c57d38b295d8940ec) Signed-off-by: Rebecca Roberts --- .github/workflows/go.yml | 44 ----- .github/workflows/scripts.yml | 30 ++++ scripts/build-docker-images.sh | 8 - scripts/deploy-bosh-lite | 8 - scripts/operations/cf-auth-proxy.yml | 48 ----- scripts/subtests/lint | 18 ++ scripts/subtests/unit-test | 11 ++ scripts/test | 253 +-------------------------- 8 files changed, 64 insertions(+), 356 deletions(-) delete mode 100644 .github/workflows/go.yml create mode 100644 .github/workflows/scripts.yml delete mode 100755 scripts/build-docker-images.sh delete mode 100755 scripts/deploy-bosh-lite delete mode 100644 scripts/operations/cf-auth-proxy.yml create mode 100755 scripts/subtests/lint create mode 100755 scripts/subtests/unit-test diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index 943068e18..000000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Go - -on: - push: - branches: - - main - - v[0-9]* - pull_request: - branches: - - main - - v[0-9]* - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 - with: - go-version-file: "src/go.mod" - - run: go run github.com/onsi/ginkgo/v2/ginkgo -r --randomize-all --randomize-suites --fail-on-pending --keep-going --race --trace - working-directory: src - - vet: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 - with: - go-version-file: "src/go.mod" - - run: go vet $(go list ./... | grep -v internal/promql$) - working-directory: src - - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 - with: - go-version-file: "src/go.mod" - - uses: golangci/golangci-lint-action@v4.0.0 - with: - args: --config .golangci.yml - working-directory: src diff --git a/.github/workflows/scripts.yml b/.github/workflows/scripts.yml new file mode 100644 index 000000000..c96560119 --- /dev/null +++ b/.github/workflows/scripts.yml @@ -0,0 +1,30 @@ +name: scripts + +on: + push: + branches: + - main + - v[0-9]* + pull_request: + branches: + - main + - v[0-9]* + +jobs: + unit-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: "src/go.mod" + - run: ./scripts/subtests/unit-test + + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: "src/go.mod" + - run: ./scripts/subtests/lint diff --git a/scripts/build-docker-images.sh b/scripts/build-docker-images.sh deleted file mode 100755 index d9f204dbf..000000000 --- a/scripts/build-docker-images.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/.. - -docker build -t logcache/log-cache:latest ${SCRIPT_ROOT}/src -f ${SCRIPT_ROOT}/src/cmd/log-cache/Dockerfile -docker build -t logcache/syslog-server:latest ${SCRIPT_ROOT}/src -f ${SCRIPT_ROOT}/src/cmd/syslog-server/Dockerfile -docker build -t logcache/log-cache-gateway:latest ${SCRIPT_ROOT}/src -f ${SCRIPT_ROOT}/src/cmd/gateway/Dockerfile -docker build -t logcache/log-cache-cf-auth-proxy:latest ${SCRIPT_ROOT}/src -f ${SCRIPT_ROOT}/src/cmd/cf-auth-proxy/Dockerfile \ No newline at end of file diff --git a/scripts/deploy-bosh-lite b/scripts/deploy-bosh-lite deleted file mode 100755 index cb9bbe848..000000000 --- a/scripts/deploy-bosh-lite +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -bosh -e lite -d log-cache deploy manifests/log-cache.yml \ - --vars-store deployment-vars.yml \ - --vars-file /tmp/loggregator-vars.yml \ - --var system_domain=bosh-lite.com \ - --var datadog_api_key="$DATADOG_API_KEY" \ - --no-redact diff --git a/scripts/operations/cf-auth-proxy.yml b/scripts/operations/cf-auth-proxy.yml deleted file mode 100644 index 4e5a049cf..000000000 --- a/scripts/operations/cf-auth-proxy.yml +++ /dev/null @@ -1,48 +0,0 @@ -- type: replace - path: /instance_groups/name=log-cache/jobs/- - value: - name: route_registrar - release: routing - properties: - route_registrar: - routes: - - name: log-cache-reverse-proxy - port: 8083 #TODO: pull from a bosh link - registration_interval: 20s - uris: - - log-cache.((system_domain)) - - "*.log-cache.((system_domain))" - -- type: replace - path: /instance_groups/name=log-cache/jobs/- - value: - name: log-cache-cf-auth-proxy - release: log-cache - properties: - proxy_port: 8083 - cc: - capi_internal_addr: "https://cloud-controller-ng.service.cf.internal:9023" - ca_cert: "((log_cache_tls_cc_auth_proxy.ca))" - common_name: cloud-controller-ng.service.cf.internal - cert: "((log_cache_tls_cc_auth_proxy.certificate))" - key: "((log_cache_tls_cc_auth_proxy.private_key))" - uaa: - client_id: doppler - client_secret: "((uaa_clients_doppler_secret))" - internal_addr: https://uaa.service.cf.internal:8443 - ca_cert: "((uaa_ca.certificate))" - -- type: replace - path: /instance_groups/name=log-cache/jobs/name=log-cache-gateway/properties?/gateway_addr? - value: localhost:8081 - -- type: replace - path: /variables/- - value: - name: log_cache_tls_cc_auth_proxy - type: certificate - options: - ca: service_cf_internal_ca - common_name: log-cache - extended_key_usage: - - client_auth diff --git a/scripts/subtests/lint b/scripts/subtests/lint new file mode 100755 index 000000000..bb8f5c95b --- /dev/null +++ b/scripts/subtests/lint @@ -0,0 +1,18 @@ +#!/bin/bash + +set -eux +set -o pipefail + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +set +e +golangci_lint_executable=$(which golangci-lint) +set -e +if [ -z "${golangci_lint_executable}" ] || [ ! -x "${golangci_lint_executable}" ]; then + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest +fi + +pushd "${SCRIPT_DIR}/../../src" > /dev/null + golangci-lint run ./... +popd > /dev/null + diff --git a/scripts/subtests/unit-test b/scripts/subtests/unit-test new file mode 100755 index 000000000..1d50d7eea --- /dev/null +++ b/scripts/subtests/unit-test @@ -0,0 +1,11 @@ +#!/bin/bash + +set -eux +set -o pipefail + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +pushd "${SCRIPT_DIR}/../../src" > /dev/null + go run github.com/onsi/ginkgo/v2/ginkgo -r -p --randomize-all --randomize-suites --fail-on-pending --keep-going --race --trace +popd > /dev/null + diff --git a/scripts/test b/scripts/test index 9264fdc2b..382cecd0a 100755 --- a/scripts/test +++ b/scripts/test @@ -1,253 +1,10 @@ #!/bin/bash -trap "echo Exited!; exit 1;" SIGINT SIGTERM +set -eux +set -o pipefail -PROJECT_DIR="$(cd "$(dirname "$0")/.."; pwd)" +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -function print_usage { - echo "usage: test [subcommand] [go test args]" - echo - echo -e "\033[1mSubcommands:\033[0m" - echo " all Run all the tests, excluding linters (default)" - echo " cleaners Run tools that clean the code base" - echo " unit Run the unit tests" - echo " linters Run common linters against the project" - echo " ops_files Run tests for operation files" - echo " install_tools Install all necessary binaries for these scripts" -} +"${SCRIPT_DIR}/subtests/lint" +"${SCRIPT_DIR}/subtests/unit-test" -function print_checkpoint { - echo - bold_blue "================================== $@" -} - -function green { - echo -e "\e[32m$1\e[0m" -} - -function red { - echo -e "\e[31m$1\e[0m" -} - -function bold_blue { - echo -e "\e[1;34m$1\e[0m" -} - -function check_output { - eval "$@" - local status=$? - exit_on_failure $status -} - -function exit_on_failure { - if [[ $1 -ne 0 ]]; then - red "SUITE FAILURE" - exit $1 - fi -} - -function run_cleaners { - print_checkpoint "Running Cleaners" - - go get github.com/kisielk/gotool - - if ! which goimports > /dev/null 2>&1; then - echo installing goimports - go get golang.org/x/tools/cmd/goimports - fi - if ! which misspell > /dev/null 2>&1; then - echo installing misspell - go get github.com/client9/misspell/cmd/misspell - fi - if ! which unconvert > /dev/null 2>&1; then - echo installing unconvert - go get github.com/mdempsky/unconvert - fi - - local log_cache_pkg="code.cloudfoundry.org/log-cache" - local log_cache_dir="$(dirname $(dirname $0))/src/$log_cache_pkg" - echo running goimports - goimports -w "$log_cache_dir" - echo running gofmt - gofmt -s -w "$log_cache_dir" - echo running misspell - misspell -w "$log_cache_dir" - echo running unconvert - unconvert -v -apply "$log_cache_pkg/..." - echo running go vet - go vet "$log_cache_pkg/..." - return 0 -} - -function run_unit { - pushd $PROJECT_DIR/src > /dev/null - TEST_OPTIONS="-ginkgo.randomizeAllSpecs -ginkgo.slowSpecThreshold 20" - - # The race detector dramatically increases the runtime of certain tests - # As a result, we first run the tests with race detection - # enabled, but only consider actual data races as failures. We then run the test suite - # without race detection and assert on test correctness. - print_checkpoint "Checking for Data Races" - race_log=$(mktemp log_cache_race_log.XXXXXXXX --tmpdir) - - go test -mod=vendor -race ./... \ - ${TEST_OPTIONS} \ - $@ 2>&1 > ${race_log} || true - if egrep 'DATA RACE|race detected' ${race_log}; then - cat ${race_log} - rm ${race_log} - return 1 - fi - rm ${race_log} - echo "No races found" - - print_checkpoint "Running Unit Tests" - go test ./... \ - ${TEST_OPTIONS} \ - $@ - exit_code=$? - popd > /dev/null - return $exit_code -} - -function run_all { - check_output run_cleaners - check_output run_unit $@ -} - -function parse_argc { - command=run_all - if [[ $# -eq 0 ]]; then - return - fi - - arg=$1 - case "$arg" in - -h|-help|--help|help) - print_usage - exit 0 - ;; - all|unit|cleaners|linters|install_tools|ops_files) - command=run_$arg - ;; - *) - echo "Invalid command: $arg\n" - print_usage - exit 1 - ;; - esac -} - -function run_install_tools { - print_checkpoint "Installing Tools" - - # testing - go get github.com/onsi/ginkgo/ginkgo - - # cleaners - go get golang.org/x/tools/cmd/goimports - go get github.com/client9/misspell/cmd/misspell - go get github.com/mdempsky/unconvert - - # linters - go get github.com/tsenart/deadcode - go get github.com/golang/lint/golint - go get github.com/opennota/check/cmd/aligncheck - go get github.com/opennota/check/cmd/structcheck - go get github.com/opennota/check/cmd/varcheck - go get github.com/kisielk/errcheck - go get github.com/gordonklaus/ineffassign - go get mvdan.cc/interfacer - go get honnef.co/go/tools/cmd/megacheck -} - -function run_linters { - print_checkpoint "Running Linters" - - local log_cache_pkg - if [ "$1" = "" ]; then - log_cache_pkg="code.cloudfoundry.org/log-cache" - else - log_cache_pkg="$1" - fi - local log_cache_dir="$(dirname $(dirname $0))/src/$log_cache_pkg" - - echo running go vet - go vet "$log_cache_pkg/..." - echo running deadcode - deadcode $dir - echo running golint - golint "$log_cache_pkg/..." - echo running aligncheck - aligncheck "$log_cache_pkg/..." - echo running structcheck - structcheck "$log_cache_pkg/..." - echo running varcheck - varcheck "$log_cache_pkg/..." - echo running errcheck - errcheck -ignore '[cC]lose' "$log_cache_pkg/..." - echo running ineffassign - ineffassign "$log_cache_dir" - echo running interfacer - interfacer "$log_cache_pkg/..." - echo running megacheck - megacheck "$log_cache_pkg/..." - - return 0 -} - -function run_ops_files { - print_checkpoint "Running Operation File Tests" - - pushd $PROJECT_DIR > /dev/null - echo creating deploy-in-cf.yml ops file - tmp_dir=`mktemp -d` - - cat < $tmp_dir/consumes_cf.yml -- type: replace - path: /instance_groups/name=log-cache/jobs/name=log-cache/consumes/reverse_log_proxy/deployment? - value: cf -EOF - - bosh int manifests/log-cache.yml --ops-file=$tmp_dir/consumes_cf.yml > $tmp_dir/adjusted.yml - - ops_file_path=manifests/operations/deploy-in-cf.yml - instance_groups=`bosh int $tmp_dir/adjusted.yml --path /instance_groups | sed 's/^\-/ /' | sed 's/^/ /'` - variables=`bosh int $tmp_dir/adjusted.yml --path /variables | sed 's/^\-/ /' | sed 's/^/ /'` - - cat < manifests/operations/deploy-in-cf.yml -- type: replace - path: /releases/- - value: - name: log-cache - version: latest -- type: replace - path: /instance_groups/- - value: -$instance_groups -- type: replace - path: /variables/- - value: -$variables -EOF - popd > /dev/null - - return 0 -} - -function setup_env { - export PATH="$PROJECT_DIR/bin:$PATH" - export GORACE="halt_on_error=1" -} - -function main { - setup_env - parse_argc $1 - shift - "$command" $@ - result=$? - exit_on_failure $result - green "SWEET SUITE SUCCESS" -} - -main $@