From fd3968882d20559ffe1222efa6ab6ecb4feaa4fb Mon Sep 17 00:00:00 2001 From: Dongjun Na Date: Tue, 30 Jul 2024 22:27:12 +0900 Subject: [PATCH 1/2] Add -race flag to Makefile and integration tests; fix data race in CreateTailscaleNodesInUser --- .github/workflows/test-integration.yaml | 2 +- Makefile | 4 ++-- integration/scenario.go | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-integration.yaml b/.github/workflows/test-integration.yaml index bf55e2de30..57594aa2bd 100644 --- a/.github/workflows/test-integration.yaml +++ b/.github/workflows/test-integration.yaml @@ -101,7 +101,7 @@ jobs: --volume $PWD/control_logs:/tmp/control \ --env HEADSCALE_INTEGRATION_POSTGRES=${{env.USE_POSTGRES}} \ golang:1 \ - go run gotest.tools/gotestsum@latest -- ./... \ + go run gotest.tools/gotestsum@latest -- -race ./... \ -failfast \ -timeout 120m \ -parallel 1 \ diff --git a/Makefile b/Makefile index 719393f594..127dae71de 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ build: dev: lint test build test: - gotestsum -- -short -coverprofile=coverage.out ./... + gotestsum -- -short -race -coverprofile=coverage.out ./... test_integration: docker run \ @@ -33,7 +33,7 @@ test_integration: -v /var/run/docker.sock:/var/run/docker.sock \ -v $$PWD/control_logs:/tmp/control \ golang:1 \ - go run gotest.tools/gotestsum@latest -- -failfast ./... -timeout 120m -parallel 8 + go run gotest.tools/gotestsum@latest -- -race -failfast ./... -timeout 120m -parallel 8 lint: golangci-lint run --fix --timeout 10m diff --git a/integration/scenario.go b/integration/scenario.go index bd004247dc..2784a99548 100644 --- a/integration/scenario.go +++ b/integration/scenario.go @@ -327,18 +327,22 @@ func (s *Scenario) CreateTailscaleNodesInUser( cert := headscale.GetCert() hostname := headscale.GetHostname() + s.mu.Lock() opts = append(opts, tsic.WithHeadscaleTLS(cert), tsic.WithHeadscaleName(hostname), ) + s.mu.Unlock() user.createWaitGroup.Go(func() error { + s.mu.Lock() tsClient, err := tsic.New( s.pool, version, s.network, opts..., ) + s.mu.Unlock() if err != nil { return fmt.Errorf( "failed to create tailscale (%s) node: %w", From 126177c7ab0fd966246c85cae6635f04163934cf Mon Sep 17 00:00:00 2001 From: Dongjun Na Date: Tue, 20 Aug 2024 18:13:13 +0900 Subject: [PATCH 2/2] Fix data race in ExecuteCommand by using local buffers and mutex Signed-off-by: Dongjun Na --- integration/dockertestutil/execute.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/integration/dockertestutil/execute.go b/integration/dockertestutil/execute.go index 5a8e92b334..04dfdfe0e4 100644 --- a/integration/dockertestutil/execute.go +++ b/integration/dockertestutil/execute.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "sync" "time" "github.com/ory/dockertest/v3" @@ -25,7 +26,6 @@ type ExecuteCommandOption func(*ExecuteCommandConfig) error func ExecuteCommandTimeout(timeout time.Duration) ExecuteCommandOption { return ExecuteCommandOption(func(conf *ExecuteCommandConfig) error { conf.timeout = timeout - return nil }) } @@ -38,6 +38,7 @@ func ExecuteCommand( ) (string, string, error) { var stdout bytes.Buffer var stderr bytes.Buffer + var mu sync.Mutex execConfig := ExecuteCommandConfig{ timeout: dockerExecuteTimeout, @@ -59,14 +60,23 @@ func ExecuteCommand( // Run your long running function in it's own goroutine and pass back it's // response into our channel. go func() { + var localStdout bytes.Buffer + var localStderr bytes.Buffer + exitCode, err := resource.Exec( cmd, dockertest.ExecOptions{ Env: append(env, "HEADSCALE_LOG_LEVEL=disabled"), - StdOut: &stdout, - StdErr: &stderr, + StdOut: &localStdout, + StdErr: &localStderr, }, ) + + mu.Lock() + stdout.Write(localStdout.Bytes()) + stderr.Write(localStderr.Bytes()) + mu.Unlock() + resultChan <- result{exitCode, err} }() @@ -88,7 +98,6 @@ func ExecuteCommand( return stdout.String(), stderr.String(), nil case <-time.After(execConfig.timeout): - return stdout.String(), stderr.String(), ErrDockertestCommandTimeout } }