From 581daaa5baf33f17a2dc2cdcd0ce6e8c8561bc9e Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Mon, 14 Jun 2021 10:36:35 -0400 Subject: [PATCH] build: run tests natively on ARM, Mac, and Windows in CI (#111) --- .circleci/config.yml | 104 +++++++++++++++++++++++++++--------- Makefile | 3 +- pkg/signals/context_test.go | 2 + scripts/ci/install-go.sh | 55 +++++++++++++++++++ scripts/ci/setup-system.sh | 39 ++++++++++++++ 5 files changed, 177 insertions(+), 26 deletions(-) create mode 100755 scripts/ci/install-go.sh create mode 100755 scripts/ci/setup-system.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 55ccfc5..aca159e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,67 +1,121 @@ version: "2.1" +executors: + linux-amd64: + machine: + image: ubuntu-2004:202104-01 + + linux-arm64: + machine: + image: ubuntu-2004:202104-01 + resource_class: arm.medium + + mac: + macos: + xcode: 12.4.0 + resource_class: medium + shell: /bin/bash -eo pipefail + + windows: + machine: + image: windows-server-2019-vs2019:stable + resource_class: windows.medium + shell: bash.exe -eo pipefail + workflows: version: 2 - build: + build-and-test: jobs: - lint - check-openapi - - build - - test + - build-all + - test: + matrix: + parameters: + executor: + - linux-amd64 + - linux-arm64 + - mac + - windows + +commands: + setup: + steps: + - run: + name: Install system dependencies + command: ./scripts/ci/setup-system.sh + - run: + name: Install Go + command: | + ./scripts/ci/install-go.sh ${HOME}/.tools + echo 'export PATH=${HOME}/.tools/go/bin:${PATH}' >> $BASH_ENV + - run: + name: Set GOPATH and GOCACHE + command: | + GOPATH=${HOME}/go + echo "export GOPATH=${GOPATH}" >> $BASH_ENV + mkdir -p ${GOPATH}/bin + echo 'export PATH=${GOPATH}/bin:${PATH}' >> $BASH_ENV + mkdir -p ${HOME}/.cache + echo 'export GOCACHE=${HOME}/.cache' >> $BASH_ENV jobs: lint: - docker: - - image: cimg/go:1.16.3 - working_directory: /home/circleci/go/src/github.com/influxdata/influx-cli + executor: linux-amd64 steps: - checkout + - setup - run: make vet - run: make checkfmt - run: make checktidy - run: make staticcheck check-openapi: - machine: true - working_directory: /home/circleci/go/src/github.com/influxdata/influx-cli + executor: linux-amd64 steps: - checkout + - setup - run: name: Init openapi submodule command: git submodule update --init --recursive - - run: - name: Upgrade Go - command: | - wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz - tar -C ${HOME} -xzf go1.16.3.linux-amd64.tar.gz - echo 'export PATH=${HOME}/go/bin:${PATH}' >> $BASH_ENV - run: make checkopenapi - build: - docker: - - image: cimg/go:1.16.3 - working_directory: /home/circleci/go/src/github.com/influxdata/influx-cli + build-all: + executor: linux-amd64 steps: - checkout + - setup - run: make crossbuild - store_artifacts: path: dist test: - docker: - - image: cimg/go:1.16.3 - working_directory: /home/circleci/go/src/github.com/influxdata/influx-cli + parameters: + executor: + type: executor + executor: << parameters.executor >> steps: - checkout + - setup + - run: + name: Download gotestsum + command: | + GOTESTSUM_VERSION=1.6.4 + GOTESTSUM_ARCHIVE=gotestsum_${GOTESTSUM_VERSION}_$(go env GOOS)_$(go env GOARCH).tar.gz + GOTESTSUM_URL=https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/${GOTESTSUM_ARCHIVE} + + wget ${GOTESTSUM_URL} + tar xzf ${GOTESTSUM_ARCHIVE} + install gotestsum ${GOPATH}/bin/ - run: name: Run parallel race tests command: | - mkdir -p /tmp/test-results - GO_TEST_CMD="gotestsum --format standard-quiet --junitfile /tmp/test-results/gotestsum.xml --" + mkdir -p test-results + GO_TEST_CMD="gotestsum --format standard-quiet --junitfile ./test-results/gotestsum.xml --" TESTFILES=($(go list ./... | circleci tests split --split-by=timings)) make GO_TEST="$GO_TEST_CMD" GO_TEST_PATHS="${TESTFILES[*]}" test-race - store_artifacts: - path: /tmp/test-results + path: ./test-results destination: raw-test-output - store_test_results: - path: /tmp/test-results + path: ./test-results diff --git a/Makefile b/Makefile index 23283de..f27954c 100644 --- a/Makefile +++ b/Makefile @@ -91,9 +91,10 @@ mock: ./internal/mock/gen.go go generate ./internal/mock/ test: - $(GO_TEST) $(GO_TEST_PATHS) + CGO_ENABLED=0 $(GO_TEST) $(GO_TEST_PATHS) test-race: + # Race-checking requires CGO. $(GO_TEST) -v -race -count=1 $(GO_TEST_PATHS) ### List of all targets that don't produce a file diff --git a/pkg/signals/context_test.go b/pkg/signals/context_test.go index 222e398..04bb74c 100644 --- a/pkg/signals/context_test.go +++ b/pkg/signals/context_test.go @@ -1,3 +1,5 @@ +//+build !windows + package signals import ( diff --git a/scripts/ci/install-go.sh b/scripts/ci/install-go.sh new file mode 100755 index 0000000..d987def --- /dev/null +++ b/scripts/ci/install-go.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +set -eo pipefail + +declare -r GO_VERSION=1.16.5 + +function install_go_linux () { + ARCHIVE=go${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz + wget https://golang.org/dl/${ARCHIVE} + tar -C $1 -xzf ${ARCHIVE} + rm ${ARCHIVE} +} + +function install_go_mac () { + ARCHIVE=go${GO_VERSION}.darwin-amd64.tar.gz + wget https://golang.org/dl/${ARCHIVE} + tar -C $1 -xzf ${ARCHIVE} + rm ${ARCHIVE} +} + +function install_go_windows () { + ARCHIVE=go${GO_VERSION}.windows-amd64.zip + wget https://golang.org/dl/${ARCHIVE} + unzip -qq -d $1 ${ARCHIVE} + rm ${ARCHIVE} +} + +function main () { + if [[ $# != 1 ]]; then + >&2 echo Usage: $0 '' + exit 1 + fi + local -r install_dir=$1 + + rm -rf "$install_dir" + mkdir -p "$install_dir" + case $(uname) in + Linux) + install_go_linux "$install_dir" + ;; + Darwin) + install_go_mac "$install_dir" + ;; + MSYS_NT*) + install_go_windows "$install_dir" + ;; + *) + >&2 echo Error: unknown OS $(uname) + exit 1 + ;; + esac + + "${install_dir}/go/bin/go" version +} + +main ${@} diff --git a/scripts/ci/setup-system.sh b/scripts/ci/setup-system.sh new file mode 100755 index 0000000..a05b102 --- /dev/null +++ b/scripts/ci/setup-system.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -eo pipefail + +function setup_linux () { + sudo apt-get update + sudo apt-get install -y --no-install-recommends make +} + +function setup_mac () { + # Python and TCL both come pre-installed on Circle's mac executors, and both depend on wget in some way. + # Homebrew will auto-upgrade both of them when wget is installed/upgraded, triggering a chain of upgrades. + # Uninstall them both before adding wget to avoid burning time in CI for things we don't need. + brew remove --force python@3.9 tcl-tk + HOMEBREW_NO_AUTO_UPDATE=1 brew install wget +} + +function setup_windows () { + choco install make mingw wget +} + +function main () { + case $(uname) in + Linux) + setup_linux + ;; + Darwin) + setup_mac + ;; + MSYS_NT*) + setup_windows + ;; + *) + >&2 echo Error: unknown OS $(uname) + exit 1 + ;; + esac +} + +main ${@}