@@ -12,12 +12,21 @@ import (
1212 "strings"
1313 "testing"
1414 "time"
15+
16+ "golang.org/x/net/nettest"
1517)
1618
19+ func unixDomainSocketPath (t * testing.T ) string {
20+ t .Helper ()
21+ sockPath , err := nettest .LocalPath ()
22+ if err != nil {
23+ t .Fatalf ("Failed to create temporary directory: %v" , err )
24+ }
25+ return sockPath
26+ }
27+
1728func TestHTTPLogServer (t * testing.T ) {
18- // Create a temporary socket path
19- tmpDir := t .TempDir ()
20- sockPath := filepath .Join (tmpDir , "test.sock" )
29+ sockPath := unixDomainSocketPath (t )
2130
2231 // Create log channel
2332 stopLogCh := make (chan struct {})
@@ -238,8 +247,8 @@ func TestHTTPLogServerInvalidSocketPath(t *testing.T) {
238247
239248func TestHTTPLogServerSocketInUse (t * testing.T ) {
240249 // Create a temporary socket path
241- tmpDir := t . TempDir ( )
242- sockPath := filepath . Join ( tmpDir , "test.sock" )
250+ sockPath := unixDomainSocketPath ( t )
251+ defer os . Remove ( sockPath )
243252
244253 // Create the first server
245254 stopLogCh1 := make (chan struct {})
@@ -261,15 +270,12 @@ func TestHTTPLogServerSocketInUse(t *testing.T) {
261270 if ! strings .Contains (err .Error (), "could not listen log socket" ) {
262271 t .Errorf ("Expected 'could not listen log socket' error, got: %v" , err )
263272 }
264-
265- // Clean up
266- os .Remove (sockPath )
267273}
268274
269275func TestHTTPLogServerConcurrentRequests (t * testing.T ) {
270276 // Create a temporary socket path
271- tmpDir := t . TempDir ( )
272- sockPath := filepath . Join ( tmpDir , "test.sock" )
277+ sockPath := unixDomainSocketPath ( t )
278+ defer os . Remove ( sockPath )
273279
274280 // Create log channel
275281 stopLogCh := make (chan struct {})
@@ -348,15 +354,12 @@ func TestHTTPLogServerConcurrentRequests(t *testing.T) {
348354 t .Errorf ("Log '%s' was not stored" , expectedLog )
349355 }
350356 }
351-
352- // Clean up
353- os .Remove (sockPath )
354357}
355358
356359func TestHTTPLogServerErrorHandling (t * testing.T ) {
357360 // Create a temporary socket path
358- tmpDir := t . TempDir ( )
359- sockPath := filepath . Join ( tmpDir , "test.sock" )
361+ sockPath := unixDomainSocketPath ( t )
362+ defer os . Remove ( sockPath )
360363
361364 // Create log channel
362365 stopLogCh := make (chan struct {})
@@ -393,9 +396,6 @@ func TestHTTPLogServerErrorHandling(t *testing.T) {
393396 t .Errorf ("Expected status 200, got %d" , resp .StatusCode )
394397 }
395398 })
396-
397- // Clean up
398- os .Remove (sockPath )
399399}
400400
401401func BenchmarkHTTPLogServer (b * testing.B ) {
@@ -440,8 +440,8 @@ func BenchmarkHTTPLogServer(b *testing.B) {
440440
441441func TestHTTPLogClient (t * testing.T ) {
442442 // Create a temporary socket path
443- tmpDir := t . TempDir ( )
444- sockPath := filepath . Join ( tmpDir , "test.sock" )
443+ sockPath := unixDomainSocketPath ( t )
444+ defer os . Remove ( sockPath )
445445
446446 // Create log channel
447447 stopLogCh := make (chan struct {})
@@ -502,9 +502,6 @@ func TestHTTPLogClient(t *testing.T) {
502502 t .Error ("Timeout waiting for channel closure" )
503503 }
504504 })
505-
506- // Clean up
507- os .Remove (sockPath )
508505}
509506
510507func TestHTTPLogClientServerUnavailable (t * testing.T ) {
@@ -570,8 +567,8 @@ func BenchmarkHTTPLogClient(b *testing.B) {
570567
571568func TestHTTPLogServerWithLogWriter (t * testing.T ) {
572569 // Create a temporary socket path
573- tmpDir := t . TempDir ( )
574- sockPath := filepath . Join ( tmpDir , "test.sock" )
570+ sockPath := unixDomainSocketPath ( t )
571+ defer os . Remove ( sockPath )
575572
576573 // Create log channel
577574 stopLogCh := make (chan struct {})
@@ -632,8 +629,7 @@ func TestHTTPLogServerWithLogWriter(t *testing.T) {
632629
633630 t .Run ("Empty logs endpoint" , func (t * testing.T ) {
634631 // Create a new server for this test
635- tmpDir2 := t .TempDir ()
636- sockPath2 := filepath .Join (tmpDir2 , "test2.sock" )
632+ sockPath2 := unixDomainSocketPath (t )
637633 stopLogCh2 := make (chan struct {})
638634
639635 go func () {
@@ -684,15 +680,12 @@ func TestHTTPLogServerWithLogWriter(t *testing.T) {
684680 t .Error ("Timeout waiting for channel closure" )
685681 }
686682 })
687-
688- // Clean up
689- os .Remove (sockPath )
690683}
691684
692685func TestHTTPLogClientGetLogs (t * testing.T ) {
693686 // Create a temporary socket path
694- tmpDir := t . TempDir ( )
695- sockPath := filepath . Join ( tmpDir , "test.sock" )
687+ sockPath := unixDomainSocketPath ( t )
688+ defer os . Remove ( sockPath )
696689
697690 // Create log channel
698691 stopLogCh := make (chan struct {})
@@ -731,8 +724,7 @@ func TestHTTPLogClientGetLogs(t *testing.T) {
731724
732725 t .Run ("Get empty logs" , func (t * testing.T ) {
733726 // Create a new client for empty logs test
734- tmpDir2 := t .TempDir ()
735- sockPath2 := filepath .Join (tmpDir2 , "test2.sock" )
727+ sockPath2 := unixDomainSocketPath (t )
736728 stopLogCh2 := make (chan struct {})
737729
738730 go func () {
@@ -752,7 +744,4 @@ func TestHTTPLogClientGetLogs(t *testing.T) {
752744
753745 os .Remove (sockPath2 )
754746 })
755-
756- // Clean up
757- os .Remove (sockPath )
758747}
0 commit comments