From 9c4e23db57aac0de12966f521b770fa81525ce49 Mon Sep 17 00:00:00 2001 From: Alan Parra Date: Wed, 4 Dec 2024 13:28:46 -0300 Subject: [PATCH] fix: Avoid large unix socket paths on lib/service tests (#49673) --- lib/service/service_test.go | 41 ++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/service/service_test.go b/lib/service/service_test.go index 0ff48c3055395..e0b421ff93780 100644 --- a/lib/service/service_test.go +++ b/lib/service/service_test.go @@ -103,7 +103,7 @@ func TestAdditionalExpectedRoles(t *testing.T) { name: "everything enabled", cfg: func() *servicecfg.Config { cfg := servicecfg.MakeDefaultConfig() - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.SetAuthServerAddress(utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"}) cfg.Auth.StorageConfig.Params["path"] = t.TempDir() cfg.DiagnosticAddr = utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"} @@ -134,7 +134,7 @@ func TestAdditionalExpectedRoles(t *testing.T) { name: "everything enabled with additional roles", cfg: func() *servicecfg.Config { cfg := servicecfg.MakeDefaultConfig() - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.SetAuthServerAddress(utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"}) cfg.Auth.StorageConfig.Params["path"] = t.TempDir() cfg.DiagnosticAddr = utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"} @@ -194,7 +194,7 @@ func TestDynamicClientReuse(t *testing.T) { cfg := servicecfg.MakeDefaultConfig() cfg.Clock = fakeClock - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.SetAuthServerAddress(utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"}) cfg.Auth.Enabled = true cfg.Auth.ListenAddr = utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"} @@ -276,7 +276,7 @@ func TestMonitor(t *testing.T) { cfg := servicecfg.MakeDefaultConfig() cfg.Clock = fakeClock var err error - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.DiagnosticAddr = utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"} cfg.SetAuthServerAddress(utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"}) cfg.Auth.Enabled = true @@ -787,7 +787,7 @@ func TestDesktopAccessFIPS(t *testing.T) { cfg := servicecfg.MakeDefaultConfig() cfg.SetAuthServerAddress(utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"}) cfg.Clock = clockwork.NewFakeClock() - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.Auth.Enabled = false cfg.Proxy.Enabled = false cfg.SSH.Enabled = false @@ -912,7 +912,7 @@ func TestSetupProxyTLSConfig(t *testing.T) { cfg := servicecfg.MakeDefaultConfig() cfg.CircuitBreakerConfig = breaker.NoopBreakerConfig() cfg.Proxy.ACME.Enabled = tc.acmeEnabled - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.Proxy.PublicAddrs = utils.MustParseAddrList("localhost") process := TeleportProcess{ Config: cfg, @@ -937,7 +937,7 @@ func TestTeleportProcess_reconnectToAuth(t *testing.T) { cfg := servicecfg.MakeDefaultConfig() cfg.SetAuthServerAddress(utils.NetAddr{AddrNetwork: "tcp", Addr: "127.0.0.1:0"}) cfg.Clock = clockwork.NewRealClock() - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.Auth.Enabled = false cfg.Proxy.Enabled = false cfg.SSH.Enabled = true @@ -1005,7 +1005,7 @@ func TestTeleportProcessAuthVersionCheck(t *testing.T) { authCfg := servicecfg.MakeDefaultConfig() authCfg.SetAuthServerAddress(listenAddr) - authCfg.DataDir = t.TempDir() + authCfg.DataDir = makeTempDir(t) authCfg.Auth.Enabled = true authCfg.Auth.StaticTokens = staticTokens authCfg.Auth.StorageConfig.Type = lite.GetName() @@ -1027,7 +1027,7 @@ func TestTeleportProcessAuthVersionCheck(t *testing.T) { authListenAddr := authProc.Config.AuthServerAddresses()[0] nodeCfg := servicecfg.MakeDefaultConfig() nodeCfg.SetAuthServerAddress(authListenAddr) - nodeCfg.DataDir = t.TempDir() + nodeCfg.DataDir = makeTempDir(t) nodeCfg.SetToken(token) nodeCfg.Auth.Enabled = false nodeCfg.Proxy.Enabled = false @@ -1614,11 +1614,7 @@ func TestDebugServiceStartSocket(t *testing.T) { t.Parallel() fakeClock := clockwork.NewFakeClock() - var err error - dataDir, err := os.MkdirTemp("", "*") - require.NoError(t, err) - t.Cleanup(func() { os.RemoveAll(dataDir) }) - + dataDir := makeTempDir(t) cfg := servicecfg.MakeDefaultConfig() cfg.DebugService.Enabled = true cfg.Clock = fakeClock @@ -1741,7 +1737,7 @@ func TestInstanceMetadata(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { cfg := newCfg() - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.Auth.StorageConfig.Params["path"] = t.TempDir() cfg.InstanceMetadataClient = tc.imClient @@ -1802,7 +1798,7 @@ func TestInitDatabaseService(t *testing.T) { t.Parallel() cfg := servicecfg.MakeDefaultConfig() - cfg.DataDir = t.TempDir() + cfg.DataDir = makeTempDir(t) cfg.DebugService = servicecfg.DebugConfig{ Enabled: false, } @@ -1858,7 +1854,7 @@ func TestAgentRolloutController(t *testing.T) { // Test setup: create a Teleport Auth config fakeClock := clockwork.NewFakeClock() - dataDir := t.TempDir() + dataDir := makeTempDir(t) cfg := servicecfg.MakeDefaultConfig() cfg.Clock = fakeClock @@ -1920,3 +1916,14 @@ func TestAgentRolloutController(t *testing.T) { return rollout.Spec.GetTargetVersion() == version.Spec.GetAgents().GetTargetVersion() }, time.Second, 10*time.Millisecond) } + +// makeTempDir makes a temp dir with a shorter name than t.TempDir() in order to +// avoid https://github.com/golang/go/issues/62614. +func makeTempDir(t *testing.T) string { + t.Helper() + + tempDir, err := os.MkdirTemp("", "teleport-test-") + require.NoError(t, err, "os.MkdirTemp() failed") + t.Cleanup(func() { os.RemoveAll(tempDir) }) + return tempDir +}