Skip to content

Commit

Permalink
Add make commands to standardize running/debugging Go tests (#24606)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
sgress454 authored Dec 11, 2024
1 parent f2762cf commit bf7876a
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -87,16 +81,21 @@ 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
make lint-js - Run the JavaScript linters
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:
Expand Down Expand Up @@ -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 ./...
Expand Down

0 comments on commit bf7876a

Please sign in to comment.