From 32c21a05f881698ac625f531785ba076745b4002 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 18 Oct 2022 14:41:20 +0200 Subject: [PATCH 1/5] cache go mod in docker, speed up local Signed-off-by: Kristoffer Dalby --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index eeac2805..12c42360 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ test_integration_cli: docker network create headscale-test || true docker run -t --rm \ --network headscale-test \ + -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ go test -failfast -tags integration_cli,integration -timeout 30m -count=1 ./... @@ -40,6 +41,7 @@ test_integration_derp: docker network create headscale-test || true docker run -t --rm \ --network headscale-test \ + -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ go test -failfast -tags integration_derp,integration -timeout 30m -count=1 ./... @@ -49,6 +51,7 @@ test_integration_general: docker network create headscale-test || true docker run -t --rm \ --network headscale-test \ + -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ go test -failfast -tags integration_general,integration -timeout 30m -count=1 ./... @@ -58,6 +61,7 @@ test_integration_oidc: docker network create headscale-test || true docker run -t --rm \ --network headscale-test \ + -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ go test -failfast -tags integration_oidc,integration -timeout 30m -count=1 ./... From c6f82c36463e5e66e9ced6190ad7875bc924b6de Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 18 Oct 2022 14:41:48 +0200 Subject: [PATCH 2/5] Switch from hacking buildtags to selecting tests Signed-off-by: Kristoffer Dalby --- Makefile | 8 ++++---- integration_cli_test.go | 4 +--- integration_common_test.go | 2 -- integration_embedded_derp_test.go | 4 +--- integration_general_test.go | 4 +--- integration_oidc_test.go | 4 +--- 6 files changed, 8 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 12c42360..90ee5107 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ test_integration_cli: -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ - go test -failfast -tags integration_cli,integration -timeout 30m -count=1 ./... + go test -failfast -timeout 30m -count=1 -run IntegrationCLI ./... test_integration_derp: docker network rm $$(docker network ls --filter name=headscale --quiet) || true @@ -44,7 +44,7 @@ test_integration_derp: -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ - go test -failfast -tags integration_derp,integration -timeout 30m -count=1 ./... + go test -failfast -timeout 30m -count=1 -run IntegrationDERP ./... test_integration_general: docker network rm $$(docker network ls --filter name=headscale --quiet) || true @@ -54,7 +54,7 @@ test_integration_general: -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ - go test -failfast -tags integration_general,integration -timeout 30m -count=1 ./... + go test -failfast -timeout 30m -count=1 -run IntegrationGeneral ./... test_integration_oidc: docker network rm $$(docker network ls --filter name=headscale --quiet) || true @@ -64,7 +64,7 @@ test_integration_oidc: -v ~/.cache/hs-integration-go:/go \ -v $$PWD:$$PWD -w $$PWD \ -v /var/run/docker.sock:/var/run/docker.sock golang:1 \ - go test -failfast -tags integration_oidc,integration -timeout 30m -count=1 ./... + go test -failfast -timeout 30m -count=1 -run IntegrationOIDC ./... coverprofile_func: go tool cover -func=coverage.out diff --git a/integration_cli_test.go b/integration_cli_test.go index 0f5d69ad..dc854034 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -1,5 +1,3 @@ -//go:build integration_cli - package headscale import ( @@ -28,7 +26,7 @@ type IntegrationCLITestSuite struct { env []string } -func TestCLIIntegrationTestSuite(t *testing.T) { +func TestIntegrationCLITestSuite(t *testing.T) { s := new(IntegrationCLITestSuite) suite.Run(t, s) diff --git a/integration_common_test.go b/integration_common_test.go index 9fe435d6..4b931840 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -1,5 +1,3 @@ -//go:build integration - package headscale import ( diff --git a/integration_embedded_derp_test.go b/integration_embedded_derp_test.go index c83f6106..343a04ad 100644 --- a/integration_embedded_derp_test.go +++ b/integration_embedded_derp_test.go @@ -1,5 +1,3 @@ -//go:build integration_derp - package headscale import ( @@ -46,7 +44,7 @@ type IntegrationDERPTestSuite struct { joinWaitGroup sync.WaitGroup } -func TestDERPIntegrationTestSuite(t *testing.T) { +func TestIntegrationDERPTestSuite(t *testing.T) { saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG") if err != nil { saveLogs = false diff --git a/integration_general_test.go b/integration_general_test.go index 5b5abf07..8f318d32 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -1,5 +1,3 @@ -//go:build integration_general - package headscale import ( @@ -41,7 +39,7 @@ type IntegrationTestSuite struct { joinWaitGroup sync.WaitGroup } -func TestIntegrationTestSuite(t *testing.T) { +func TestIntegrationGeneralTestSuite(t *testing.T) { saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG") if err != nil { saveLogs = false diff --git a/integration_oidc_test.go b/integration_oidc_test.go index ba0b00f6..8e5f7910 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -1,5 +1,3 @@ -//go:build integration_oidc - package headscale import ( @@ -45,7 +43,7 @@ type IntegrationOIDCTestSuite struct { joinWaitGroup sync.WaitGroup } -func TestOIDCIntegrationTestSuite(t *testing.T) { +func TestIntegrationOIDCTestSuite(t *testing.T) { saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG") if err != nil { saveLogs = false From dfadb965b7014618848afe706d7916c3b3519495 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 18 Oct 2022 14:45:18 +0200 Subject: [PATCH 3/5] Use short test to signal that we dont run integration Signed-off-by: Kristoffer Dalby --- Makefile | 2 +- integration_cli_test.go | 4 ++++ integration_embedded_derp_test.go | 4 ++++ integration_general_test.go | 4 ++++ integration_oidc_test.go | 4 ++++ 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 90ee5107..c5a2d88c 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ build: dev: lint test build test: - @go test -coverprofile=coverage.out ./... + @go test -short -coverprofile=coverage.out ./... test_integration: test_integration_cli test_integration_derp test_integration_oidc test_integration_general diff --git a/integration_cli_test.go b/integration_cli_test.go index dc854034..06771483 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -27,6 +27,10 @@ type IntegrationCLITestSuite struct { } func TestIntegrationCLITestSuite(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration tests due to short flag") + } + s := new(IntegrationCLITestSuite) suite.Run(t, s) diff --git a/integration_embedded_derp_test.go b/integration_embedded_derp_test.go index 343a04ad..0b7a8d27 100644 --- a/integration_embedded_derp_test.go +++ b/integration_embedded_derp_test.go @@ -45,6 +45,10 @@ type IntegrationDERPTestSuite struct { } func TestIntegrationDERPTestSuite(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration tests due to short flag") + } + saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG") if err != nil { saveLogs = false diff --git a/integration_general_test.go b/integration_general_test.go index 8f318d32..ad92a2b2 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -40,6 +40,10 @@ type IntegrationTestSuite struct { } func TestIntegrationGeneralTestSuite(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration tests due to short flag") + } + saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG") if err != nil { saveLogs = false diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 8e5f7910..2f4d7ddf 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -44,6 +44,10 @@ type IntegrationOIDCTestSuite struct { } func TestIntegrationOIDCTestSuite(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration tests due to short flag") + } + saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG") if err != nil { saveLogs = false From 4df47de3f29cc11a397bf3f1e4da8176c0b57439 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 18 Oct 2022 14:57:22 +0200 Subject: [PATCH 4/5] add nolint to integrationtests, they are going away :tm: Signed-off-by: Kristoffer Dalby --- integration_cli_test.go | 1 + integration_common_test.go | 2 +- integration_embedded_derp_test.go | 5 ++--- integration_general_test.go | 3 ++- integration_oidc_test.go | 3 ++- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/integration_cli_test.go b/integration_cli_test.go index 06771483..ee6a9e1f 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -1,3 +1,4 @@ +//nolint package headscale import ( diff --git a/integration_common_test.go b/integration_common_test.go index 4b931840..a1658863 100644 --- a/integration_common_test.go +++ b/integration_common_test.go @@ -1,3 +1,4 @@ +//nolint package headscale import ( @@ -326,7 +327,6 @@ func GetEnvBool(key string) (bool, error) { func GetFirstOrCreateNetwork(pool *dockertest.Pool, name string) (dockertest.Network, error) { networks, err := pool.NetworksByName(name) if err != nil || len(networks) == 0 { - if _, err := pool.CreateNetwork(name); err == nil { // Create does not give us an updated version of the resource, so we need to // get it again. diff --git a/integration_embedded_derp_test.go b/integration_embedded_derp_test.go index 0b7a8d27..5bd6deed 100644 --- a/integration_embedded_derp_test.go +++ b/integration_embedded_derp_test.go @@ -1,3 +1,4 @@ +//nolint package headscale import ( @@ -15,13 +16,12 @@ import ( "testing" "time" + "github.com/ccding/go-stun/stun" v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - - "github.com/ccding/go-stun/stun" ) const ( @@ -122,7 +122,6 @@ func (s *IntegrationDERPTestSuite) SetupSuite() { } headscaleOptions := &dockertest.RunOptions{ - Name: headscaleDerpHostname, Mounts: []string{ fmt.Sprintf( diff --git a/integration_general_test.go b/integration_general_test.go index ad92a2b2..52bfddd4 100644 --- a/integration_general_test.go +++ b/integration_general_test.go @@ -1,3 +1,4 @@ +//nolint package headscale import ( @@ -506,7 +507,7 @@ func getIPsfromIPNstate(status ipnstate.Status) []netip.Addr { return ips } -// TODO: Adopt test for cross communication between namespaces +// TODO: Adopt test for cross communication between namespaces. func (s *IntegrationTestSuite) TestPingAllPeersByAddress() { for _, scales := range s.namespaces { ips, err := getIPs(scales.tailscales) diff --git a/integration_oidc_test.go b/integration_oidc_test.go index 2f4d7ddf..8ca46db7 100644 --- a/integration_oidc_test.go +++ b/integration_oidc_test.go @@ -1,3 +1,4 @@ +//nolint package headscale import ( @@ -199,7 +200,7 @@ oidc: log.Println(config) configPath := path.Join(currentPath, "integration_test/etc_oidc/config.yaml") - err = os.WriteFile(configPath, []byte(config), 0644) + err = os.WriteFile(configPath, []byte(config), 0o644) if err != nil { s.FailNow(fmt.Sprintf("Could not write config: %s", err), "") } From 2f36a11a8eee8bfe723d323aee6b887435bfd138 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 18 Oct 2022 15:08:48 +0200 Subject: [PATCH 5/5] use short flag for nix build test Signed-off-by: Kristoffer Dalby --- flake.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/flake.nix b/flake.nix index f7d0c669..6775e9cb 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,9 @@ version = headscaleVersion; src = pkgs.lib.cleanSource self; + # Only run unit tests when testing a build + checkFlags = ["-short"]; + # When updating go.mod or go.sum, a new sha will need to be calculated, # update this if you have a mismatch after doing a change to thos files. vendorSha256 = "sha256-DosFCSiQ5FURbIrt4NcPGkExc84t2MGMqe9XLxNHdIM=";