Skip to content

Commit

Permalink
ci: use groups of tests in gha (#15018)
Browse files Browse the repository at this point in the history
* [no ci] use json for grouping packages for testing

* [no ci] able to get packages in group

* [no ci] able to run groups of tests

* [no ci] more

* [no ci] try disable circle unit tests

* ci: use actions/checkout@v3

* ci: rename to quick

* ci: need make dev in mods cache step

* ci: make compile step depend on checks step

* ci: bump consul and vault versions

* ci: need make dev for group tests

* ci: update ci unit testing docs

* docs: spell plumbing correctly

Co-authored-by: Tim Gross <[email protected]>

Co-authored-by: Tim Gross <[email protected]>
  • Loading branch information
shoenig and tgross authored Oct 27, 2022
1 parent 75736b6 commit f715cc1
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 167 deletions.
52 changes: 0 additions & 52 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,6 @@ workflows:
- /^backport/docs-.*/
- stable-website

# Note: comment-out this job in ENT
- build-darwin-binaries:
filters: *backend_check_branches_filter

- test-e2e:
filters: *backend_check_branches_filter

Expand All @@ -546,51 +542,3 @@ workflows:
- /^backport/docs-.*/
- /^e2e-.*/
- stable-website

- test-machine:
name: "test-client"
test_packages: "./client/..."
# test branches are the branches that can impact unit tests
filters: &backend_test_branches_filter
branches:
ignore:
- /^.-ui\b.*/
- /^docs-.*/
- /^backport/docs-.*/
- /^e2e-.*/
- stable-website
- test-machine:
name: "test-nomad"
test_packages: "./nomad/..."
filters: *backend_test_branches_filter
- test-machine:
# API Tests run in a VM rather than container due to the FS tests
# requiring `mount` priviliges.
name: "test-api"
test_module: "api"
filters: *backend_test_branches_filter
enable_race_testing: true
- test-machine:
name: "test-other"
exclude_packages: "./api|./client|./drivers/docker|./drivers/exec|./drivers/shared/executor|./nomad|./e2e"
filters: *backend_test_branches_filter
- test-machine:
name: "test-docker"
test_packages: "./drivers/docker"
executor: go-machine
filters: *backend_test_branches_filter
- test-machine:
name: "test-exec"
test_packages: "./drivers/exec"
filters: *backend_test_branches_filter
- test-machine:
name: "test-shared-exec"
test_packages: "./drivers/shared/executor"
filters: *backend_test_branches_filter
- test-machine:
name: "test-32bit"
# Currently we only explicitly test fingerprinting on 32bit
# architectures.
test_packages: "./client/fingerprint"
goarch: "386"
filters: *backend_test_branches_filter
68 changes: 15 additions & 53 deletions .github/workflows/test-core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ env:
GO_VERSION: 1.19.1
GOBIN: /usr/local/bin
GOTESTARCH: amd64
CONSUL_VERSION: 1.11.3
VAULT_VERSION: 1.9.3
CONSUL_VERSION: 1.12.6
VAULT_VERSION: 1.12.0
NOMAD_SLOW_TEST: 0
NOMAD_TEST_LOG_LEVEL: OFF
jobs:
Expand All @@ -48,7 +48,7 @@ jobs:
runs-on: ubuntu-22.04
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0 # needs tags for checkproto
- uses: magnetikonline/action-golang-cache@v1
Expand All @@ -61,15 +61,15 @@ jobs:
make bootstrap
make check
compile:
needs: [mods]
needs: [mods, checks]
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-11, windows-2019]
runs-on: ${{matrix.os}}
timeout-minutes: 20
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: magnetikonline/action-golang-cache@v1
with:
go-version: ${{env.GO_VERSION}}
Expand All @@ -85,7 +85,7 @@ jobs:
runs-on: ubuntu-22.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: magnetikonline/action-golang-cache@v1
with:
go-version: ${{env.GO_VERSION}}
Expand All @@ -98,72 +98,34 @@ jobs:
make generate-all
sudo sed -i 's!Defaults!#Defaults!g' /etc/sudoers
sudo -E env "PATH=$PATH" make test-nomad-module
tests-pkgs:
tests-groups:
needs: [mods]
runs-on: ubuntu-22.04
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
pkg:
- acl/...
groups:
- nomad
- client
- client/allocdir/...
- client/allochealth/...
- client/allocrunner/...
- client/allocwatcher/...
- client/config/...
- client/consul/...
- client/devicemanager/...
- client/dynamicplugins/...
- client/fingerprint/...
- client/interfaces/...
- client/lib/...
- client/logmon/...
- client/pluginmanager/...
- client/servers/...
- client/serviceregistration/...
- client/state/...
- client/stats/...
- client/structs/...
- client/taskenv/...
- command
- command/agent/...
- command/raft_tools/...
- drivers/docker/...
- drivers/exec/...
- drivers/java/...
- drivers/mock/...
- drivers/rawexec/...
- drivers/shared/...
- drivers/qemu/...
- helper/...
- internal/...
- jobspec/...
- lib/...
- nomad
- nomad/deploymentwatcher/...
- nomad/drainer/...
- nomad/state/...
- nomad/stream/...
- nomad/structs/...
- nomad/volumewatcher/...
- plugins/...
- scheduler/...
- testutil/...
- drivers
- quick
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: magnetikonline/action-golang-cache@v1
with:
go-version: ${{env.GO_VERSION}}
cache-key-suffix: -core
- name: Run Matrix Tests
env:
GOTEST_PKGS: ./${{matrix.pkg}}
GOTEST_GROUP: ${{matrix.groups}}
run: |
make bootstrap
make generate-all
make dev
sudo hc-install install -version ${{env.VAULT_VERSION}} -path /usr/local/bin vault
sudo hc-install install -version ${{env.CONSUL_VERSION}} -path /usr/local/bin consul
sudo sed -i 's!Defaults!#Defaults!g' /etc/sudoers
sudo -E env "PATH=$PATH" make test-nomad
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ e2e/remotetasks/input/ecs.vars

# local terraform overrides
*.auto.tfvars

# Tools files
tools/missing/missing
16 changes: 6 additions & 10 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ ifndef NOMAD_NO_UI
GO_TAGS := ui $(GO_TAGS)
endif

ifeq ($(origin GOTEST_PKGS_EXCLUDE), undefined)
GOTEST_PKGS ?= "./..."
else
GOTEST_PKGS=$(shell go list ./... | sed 's/github.com\/hashicorp\/nomad/./' | egrep -v "^($(GOTEST_PKGS_EXCLUDE))(/.*)?$$")
endif

# tag corresponding to latest release we maintain backward compatibility with
PROTO_COMPARE_TAG ?= v1.0.3$(if $(findstring ent,$(GO_TAGS)),+ent,)

Expand Down Expand Up @@ -283,9 +277,11 @@ release: clean $(foreach t,$(ALL_TARGETS),pkg/$(t).zip) ## Build all release pac
@tree --dirsfirst $(PROJECT_ROOT)/pkg

.PHONY: test-nomad
test-nomad: dev ## Run Nomad unit tests
@echo "==> Running Nomad unit tests $(GOTEST_PKGS)"
gotestsum --format=testname --rerun-fails=3 --packages=$(GOTEST_PKGS) -- \
test-nomad: GOTEST_PKGS=$(shell go run -modfile=tools/go.mod tools/missing/main.go ci/test-core.json $(GOTEST_GROUP))
test-nomad: # dev ## Run Nomad unit tests
@echo "==> Running Nomad unit tests $(GOTEST_GROUP)"
@echo "==> with packages $(GOTEST_PKGS)"
gotestsum --format=testname --rerun-fails=3 --packages="$(GOTEST_PKGS)" -- \
-cover \
-timeout=20m \
-count=1 \
Expand Down Expand Up @@ -407,7 +403,7 @@ endif
.PHONY: missing
missing: ## Check for packages not being tested
@echo "==> Checking for packages not being tested ..."
@go run -modfile tools/go.mod tools/missing/main.go .github/workflows/test-core.yaml
@go run -modfile tools/go.mod tools/missing/main.go ci/test-core.json

.PHONY: ec2info
ec2info: ## Generate AWS EC2 CPU specification table
Expand Down
34 changes: 34 additions & 0 deletions ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# CI (unit testing)

This README describes how the Core CI Tests Github Actions works, which provides
Nomad with continuous integration unit testing.

## Steps

1. When a branch is pushed, GHA triggers `.github/workflows/test-core.yaml`.

2. The first job is `mods` which creates a pre-cache of Go modules.
- Only useful for the followup jobs on Linux runners
- Is keyed on `hash(go.sum)`, so a cache is re-used until deps are modified.

3. The `checks`, `test-api`, `test-*` jobs are started.
- The checks job runs `make check`
- The test job runs groups of tests, see below

3i. The check step also runs `make missing`
- Invokes `tools/missing` to scan `ci/test-cores.json` && nomad source.
- Fails the build if any packages in Nomad are not covered.

4a. The `test-*` jobs are run.
- Configured as a matrix of "groups"; each group is a set of packages.
- The GHA invokes `test-nomad` with $GOTEST_GROUP for each group.
- The makefile uses `tools/missing` to translate the group into packages
- Package groups are configured in `ci/test-core.json`

4b. The `test-api` job is run.
- Because `api` is a submodule, invokation of test command is special.
- The GHA invokes `test-nomad-module` with the name of the submodule.

5. The `compile` jobs are run
- Waits on checks to complete first
- Runs on each of `linux`, `macos`, `windows`
45 changes: 45 additions & 0 deletions ci/test-core.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"nomad": ["nomad"],
"client": [
"client",
"client/allocrunner/..."
],
"command": ["command"],
"drivers": ["drivers/..."],
"quick": [
"acl/...",
"client/allocdir/...",
"client/allochealth/...",
"client/allocwatcher/...",
"client/config/...",
"client/consul/...",
"client/devicemanager/...",
"client/dynamicplugins/...",
"client/fingerprint/...",
"client/interfaces/...",
"client/lib/...",
"client/logmon/...",
"client/pluginmanager/...",
"client/servers/...",
"client/serviceregistration/...",
"client/state/...",
"client/stats/...",
"client/structs/...",
"client/taskenv/...",
"command/agent/...",
"command/raft_tools/...",
"helper/...",
"internal/...",
"jobspec/...",
"lib/...",
"nomad/deploymentwatcher/...",
"nomad/drainer/...",
"nomad/state/...",
"nomad/stream/...",
"nomad/structs/...",
"nomad/volumewatcher/...",
"plugins/...",
"scheduler/...",
"testutil/..."
]
}
25 changes: 17 additions & 8 deletions contributing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@ demonstrating correct functionality.

Each unit test should meet a few criteria:

- Use testify
- Prefer using require.* functions
- Use [github.com/shoenig/test](https://github.com/shoenig/test)
- Prefer using `must.*`` functions
- Use `test.*`` functions when cleanup must happen, etc
- Feel free to refactor testify tests; but consider separate commits / PRs

- Undo any changes to the environment
- Set environment variables must be unset
- Scratch files/dirs must be removed (use t.TempDir)
- Consumed ports must be freed (e.g. TestServer.Cleanup, freeport.Return)
- Set environment variables must be unset (use `t.Setenv`)
- Scratch files/dirs must be removed (use `t.TempDir`)
- Consumed ports must be freed (e.g. `TestServer.Cleanup`, `freeport.Return`)

- Able to run in parallel
- All package level Test* functions should start with ci.Parallel
- All package level `Test*` functions should start with `ci.Parallel`
- Always use dynamic scratch dirs, files
- Always get ports from helpers (TestServer, TestClient, TestAgent, freeport.Get)
- Always get ports from helpers (`TestServer`, `TestClient`, `TestAgent`, `freeport.Get`)

- Log control
- Logging must go through the testing.T (use helper/testlog.HCLogger)
- Logging must go through the `testing.T` (use `helper/testlog.HCLogger`)
- Avoid excessive logging in test cases - prefer failure messages
- Annotate failures with `must.Sprint\f` post-scripts

## API tests

Testing in the `api` package requires an already-built Nomad
binary. If you're writing `api` tests, you'll need to build a Nomad
binary (ex. with `make dev`) that includes any changes your API
exercises.


# CI Plumbing

See [ci/README.md] for details on how the [Core CI Tests](https://github.com/hashicorp/nomad/actions/workflows/test-core.yaml)
Github Action works.
8 changes: 3 additions & 5 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ go 1.18

require (
github.com/aws/aws-sdk-go v1.37.26
github.com/stretchr/testify v1.7.1
gopkg.in/yaml.v2 v2.2.8
github.com/hashicorp/go-set v0.1.6
github.com/shoenig/test v0.4.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Loading

0 comments on commit f715cc1

Please sign in to comment.