Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refine UT for ListenEndpoint in utils.go #2340

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions pkg/csi-common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ func parseEndpoint(ep string) (string, string, error) {
return "", "", fmt.Errorf("Invalid endpoint: %v", ep)
}

var klogFatalf = func(format string, args ...interface{}) {
klog.Fatalf(format, args...)
}

func ListenEndpoint(endpoint string) (net.Listener, error) {
proto, addr, err := parseEndpoint(endpoint)
if err != nil {
klogFatalf("Invalid endpoint: %v", err)
klog.Errorf("%v", err)
return nil, err
}

Expand All @@ -58,14 +54,14 @@ func ListenEndpoint(endpoint string) (net.Listener, error) {
addr = "/" + addr
}
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
klogFatalf("Failed to remove %s, error: %s", addr, err.Error())
klog.Errorf("Failed to remove %s, error: %v", addr, err)
return nil, err
}
}

listener, err := net.Listen(proto, addr)
if err != nil {
klogFatalf("Failed to listen: %v", err)
klog.Errorf("Failed to listen: %v", err)
return nil, err
}
return listener, err
Expand Down
12 changes: 0 additions & 12 deletions pkg/csi-common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,6 @@ func TestListenEndpoint(t *testing.T) {
t.Skip("Skip test on Windows")
}

originalKlogFatalf := klogFatalf
klogFatalf = func(_ string, _ ...interface{}) {}
defer func() { klogFatalf = originalKlogFatalf }()

tests := []struct {
name string
endpoint string
Expand Down Expand Up @@ -344,14 +340,6 @@ func TestListenEndpoint(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if !tt.wantErr {
t.Errorf("ListenEndpoint() panicked unexpectedly: %v", r)
}
}
}()

got, err := ListenEndpoint(tt.endpoint)
if (err != nil) != tt.wantErr {
t.Errorf("Listen() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
Loading