Skip to content

Commit

Permalink
fixing various unit tests on Windows that create abstract sockets
Browse files Browse the repository at this point in the history
by now having them create file-based sockets on windows/darwin

Signed-off-by: Mark Rossetti <[email protected]>

Kubernetes-commit: 5e6611af5594078a76694f0cf9fb7eef1299ff40
  • Loading branch information
marosset authored and k8s-publishing-bot committed Feb 13, 2025
1 parent 46c230e commit 11b535c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/util/net/testing/socket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package nettesting contains utilities for testing networking functionality.
// Don't use these utilities in production code. They have not been security
// reviewed.
package nettesting

import (
"os"
goruntime "runtime"
"testing"
)

// MakeSocketNameForTest returns a socket name to use for the duration of a test.
// On Operating systems that support abstract sockets, it the name is prefixed with `@` to make it an abstract socket.
// On Operating systems that do not support abstract sockets, the name is treated as a filename and a cleanup hook is
// registered to delete the socket at the end of the test.
func MakeSocketNameForTest(t testing.TB, name string) string {
var sockname = name
switch goruntime.GOOS {
case "darwin", "windows":
t.Cleanup(func() { _ = os.Remove(sockname) })
default:
sockname = "@" + name
}
return sockname
}

0 comments on commit 11b535c

Please sign in to comment.