Skip to content

Commit

Permalink
chore: add more tests, benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
deansheather committed Apr 15, 2024
1 parent 15230a8 commit f81cbb9
Show file tree
Hide file tree
Showing 19 changed files with 1,099 additions and 104 deletions.
60 changes: 3 additions & 57 deletions .github/workflows/enterprise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ concurrency:
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
test-go:
name: "test/go"
test-go-enterprise:
name: "test/go-enterprise"
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
Expand All @@ -57,59 +57,5 @@ jobs:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}

- name: Install gotestsum
uses: jaxxstorm/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
repo: gotestyourself/gotestsum
tag: v1.10.1

- name: Test
shell: bash
run: |
set +e
gotestsum \
--junitfile="gotests.xml" \
--jsonfile="gotestsum.json" \
--packages="./..." \
--debug \
-- \
-exec sudo \
-parallel=8 \
-timeout=5m \
-short \
-failfast \
-covermode=atomic \
-coverprofile="gotests.coverage" \
-coverpkg=./...
ret=$?
set -e
if ((ret)); then
# Eternalize test timeout logs because "re-run failed" erases
# artifacts and gotestsum doesn't always capture it:
# https://github.com/gotestyourself/gotestsum/issues/292
# Multiple test packages could've failed, each one may or may
# not run into the edge case. PS. Don't summon ShellCheck here.
for testWithStack in $(grep 'panic: test timed out' gotestsum.json | grep -E -o '("Test":[^,}]*)'); do
if [ -n "$testWithStack" ] && grep -q "${testWithStack}.*PASS" gotestsum.json; then
echo "Conditions met for gotestsum stack trace missing bug, outputting panic trace:"
grep -A 999999 "${testWithStack}.*panic: test timed out" gotestsum.json
fi
done
fi
exit $ret
- uses: actions/upload-artifact@v3
if: success() || failure()
with:
name: gotestsum-debug.json
path: ./gotestsum.json
retention-days: 7

- uses: actions/upload-artifact@v3
if: success() || failure()
with:
name: gotests.xml
path: ./gotests.xml
retention-days: 30
run: make test/go-enterprise
31 changes: 31 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ jobs:
- name: Run make lint/go/other
run: make lint/go/other

test-go:
name: "test/go"
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.21.9"

- name: Echo Go Cache Paths
id: go-cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}

- name: Go Mod Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}

- name: Test
run: make test/go

lint-c:
name: lint/c
runs-on: ubuntu-20.04
Expand Down
20 changes: 9 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,20 @@ lint/shellcheck:
./ci/scripts/shellcheck.sh

.PHONY: test
test: test/go
test: test/go test/go-enterprise

.PHONY: test/go
test/go:
go clean -testcache
gotestsum --debug -- -v -short ./...
go test -exec sudo -v -count 1 ./...

.PHONY: test/go-enterprise
test/go-enterprise:
cd enterprise
gotestsum --debug -- -v -short ./...
go test -exec sudo -v -count 1 ./...

# test/go-sudo is equivalent to test/go but runs the test binary using sudo.
# Some tests are skipped if not running as root.
.PHONY: test/go-sudo
test/go-sudo:
.PHONY: bench
bench:
go clean -testcache
gotestsum --debug -- -exec sudo -v -short ./...
cd enterprise
gotestsum --debug -- -exec sudo -v -short ./...
COUNT=10000 ./bench/bench.sh

include Makefile.enterprise
23 changes: 23 additions & 0 deletions bench/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Runs go benchmarks in a new PID namespace. Depends on `go` and `unshare`.
#
# Usage: COUNT=10000 ./bench.sh
# Runs `go test -bench=. -run="^#" -count 1 -benchtime "${COUNT:-1000}x"` ./
#
# Usage: ./bench.sh -bench=... ...
# Runs `go test "$@"`. COUNT is ignored.

set -euo pipefail

cd "$(dirname "$0")"

args=("$@")
if [[ "${#args[@]}" -eq 0 ]]; then
args=('-bench=.' '-run="^#"' '-count=1' "-benchtime=${COUNT:-1000}x" ./)
fi

# Start the go test process in a new PID namespace and exec with sudo.
go_binary=$(command -v go)
set -x
exec sudo -E unshare --pid --fork --setuid "$(id -u)" --setgid "$(id -g)" -- "$go_binary" test -exec sudo "${args[@]}"
20 changes: 20 additions & 0 deletions bench/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/coder/exectrace/bench

go 1.21.0

require (
github.com/DataDog/ebpfbench v0.0.0-20230731170724-14eabe1f3e9a
github.com/coder/exectrace v0.2.4
github.com/stretchr/testify v1.9.0
)

require (
github.com/cilium/ebpf v0.10.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit f81cbb9

Please sign in to comment.