Skip to content

Commit

Permalink
Merge pull request #2340 from Zhupku/mengzezhu/ut
Browse files Browse the repository at this point in the history
test: refine UT for ListenEndpoint in utils.go
  • Loading branch information
andyzhangx authored Jan 12, 2025
2 parents 6a555ac + 01c407b commit 0266aca
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
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

0 comments on commit 0266aca

Please sign in to comment.