Add make commands to standardize running/debugging Go tests (#24606)

# Checklist for submitter

- [X] Manual QA for all new/changed functionality

# Details

This PR adds two new user-facing `make` targets:

* `run-go-tests`: run Go tests for one or more packages, optionally
filtering to specific tests
* `debug-go-tests`: debug (using Delve) Go tests for one or more
packages, optionally filtering to specific tests

Example usage:

```
# Run all tests in the mysql and gdmf packages
make run-go-tests PKG_TO_TEST="server/mdm/apple/gdmf server/datastore/mysql"
```

```
# Run all the TestMDMApple tests in the mysql package
make run-go-tests PKG_TO_TEST=server/datastore/mysql TESTS_TO_RUN="^TestMDMApple\$$" 
```

```
# Run only the TestMDMAppleProfileLabels test in the mysql package
make run-go-tests PKG_TO_TEST=server/datastore/mysql TESTS_TO_RUN="^TestMDMApple\$$/^TestMDMAppleProfileLabels\$$" 
```

# Notes

Two new "private" targets `.run-go-tests` and `.debug-go-tests` were
created as base commands for both `test-go` (used in CI) and the new
user-facing commands.
This commit is contained in:
Scott Gress
2024-12-11 11:06:56 -06:00
committed by GitHub
parent f2762cf507
commit bf7876af54
+53 -12
View File
@@ -1,4 +1,4 @@
.PHONY: build clean clean-assets e2e-reset-db e2e-serve e2e-setup changelog db-reset db-backup db-restore check-go-cloner update-go-cloner
.PHONY: build clean clean-assets e2e-reset-db e2e-serve e2e-setup changelog db-reset db-backup db-restore check-go-cloner update-go-cloner help
export GO111MODULE=on
@@ -10,12 +10,6 @@ REVSHORT = $(shell git rev-parse --short HEAD)
USER = $(shell whoami)
DOCKER_IMAGE_NAME = fleetdm/fleet
ifdef GO_TEST_EXTRA_FLAGS
GO_TEST_EXTRA_FLAGS_VAR := $(GO_TEST_EXTRA_FLAGS)
else
GO_TEST_EXTRA_FLAGS_VAR :=
endif
ifdef GO_BUILD_RACE_ENABLED
GO_BUILD_RACE_ENABLED_VAR := true
else
@@ -87,9 +81,9 @@ define HELP_TEXT
make build - Build the code
make package - Build rpm and deb packages for linux
make test - Run the full test suite
make test-go - Run the Go tests
make test-js - Run the JavaScript tests
make run-go-tests - Run Go tests in specific packages
make debug-go-tests - Debug Go tests in specific packages (with Delve)
make test-js - Run the JavaScript tests
make lint - Run all linters
make lint-go - Run the Go linters
@@ -97,6 +91,11 @@ define HELP_TEXT
make lint-scss - Run the SCSS linters
make lint-ts - Run the TypeScript linters
For use in CI:
make test - Run the full test suite (lint, Go and Javascript)
make test-go - Run the Go tests (all packages and tests)
endef
help:
@@ -144,8 +143,50 @@ lint: lint-go lint-js
dump-test-schema:
go run ./tools/dbutils ./server/datastore/mysql/schema.sql
test-go: dump-test-schema generate-mock
go test -tags full,fts5,netgo ${GO_TEST_EXTRA_FLAGS_VAR} -parallel 8 -coverprofile=coverage.txt -covermode=atomic -coverpkg=github.com/fleetdm/fleet/v4/... ./cmd/... ./ee/... ./orbit/pkg/... ./orbit/cmd/orbit ./pkg/... ./server/... ./tools/...
# This is the base command to run Go tests.
# Wrap this to run tests with presets (see `run-go-tests` and `test-go` targets).
# PKG_TO_TEST: Go packages to test, e.g. "server/datastore/mysql". Separate multiple packages with spaces.
# TESTS_TO_RUN: Name specific tests to run in the specified packages. Leave blank to run all tests in the specified packages.
# GO_TEST_EXTRA_FLAGS: Used to specify other arguments to `go test`.
# GO_TEST_MAKE_FLAGS: Internal var used by other targets to add arguments to `go test`.
#
PKG_TO_TEST := "" # default to empty string; can be overridden on command line.
go_test_pkg_to_test := $(addprefix ./,$(PKG_TO_TEST)) # set paths for packages to test
dlv_test_pkg_to_test := $(addprefix github.com/fleetdm/fleet/v4/,$(PKG_TO_TEST)) # set URIs for packages to debug
.run-go-tests:
ifeq ($(PKG_TO_TEST), "")
@echo "Please specify one or more packages to test with argument PKG_TO_TEST=\"/path/to/pkg/1 /path/to/pkg/2\"...";
else
@echo Running Go tests with command:
go test -tags full,fts5,netgo -run=${TESTS_TO_RUN} ${GO_TEST_MAKE_FLAGS} ${GO_TEST_EXTRA_FLAGS} -parallel 8 -coverprofile=coverage.txt -covermode=atomic -coverpkg=github.com/fleetdm/fleet/v4/... $(go_test_pkg_to_test)
endif
# This is the base command to debug Go tests.
# Wrap this to run tests with presets (see `debug-go-tests`)
# PKG_TO_TEST: Go packages to test, e.g. "server/datastore/mysql". Separate multiple packages with spaces.
# TESTS_TO_RUN: Name specific tests to debug in the specified packages. Leave blank to debug all tests in the specified packages.
# DEBUG_TEST_EXTRA_FLAGS: Internal var used by other targets to add arguments to `dlv test`.
# GO_TEST_EXTRA_FLAGS: Used to specify other arguments to `go test`.
.debug-go-tests:
ifeq ($(PKG_TO_TEST), "")
@echo "Please specify one or more packages to debug with argument PKG_TO_TEST=\"/path/to/pkg/1 /path/to/pkg/2\"...";
else
@echo Debugging tests with command:
dlv test ${dlv_test_pkg_to_test} --api-version=2 --listen=127.0.0.1:61179 ${DEBUG_TEST_EXTRA_FLAGS} -- -test.v -test.run=${TESTS_TO_RUN} ${GO_TEST_EXTRA_FLAGS}
endif
# Command to run specific tests in development. Can run all tests for one or more packages, or specific tests within packages.
run-go-tests:
@MYSQL_TEST=1 REDIS_TEST=1 MINIO_STORAGE_TEST=1 SAML_IDP_TEST=1 NETWORK_TEST=1 make .run-go-tests GO_TEST_MAKE_FLAGS="-v"
debug-go-tests:
@MYSQL_TEST=1 REDIS_TEST=1 MINIO_STORAGE_TEST=1 SAML_IDP_TEST=1 NETWORK_TEST=1 make .debug-go-tests
# Command used in CI to run all tests.
test-go: dump-test-schema generate-mock
make .run-go-tests PKG_TO_TEST="./cmd/... ./ee/... ./orbit/pkg/... ./orbit/cmd/orbit ./pkg/... ./server/... ./tools/..."
analyze-go:
go test -tags full,fts5,netgo -race -cover ./...