Skip to content

Commit 0e3a1eb

Browse files
authored
Merge pull request #2001 from Zhupku/mengzezhu/ut3
test: add UT coverage for blob/nodeserver.go
2 parents 42b05af + a4df33b commit 0e3a1eb

File tree

1 file changed

+152
-6
lines changed

1 file changed

+152
-6
lines changed

pkg/blob/nodeserver_test.go

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"net"
2324
"os"
2425
"path/filepath"
2526
"reflect"
@@ -43,6 +44,7 @@ import (
4344
mount "k8s.io/mount-utils"
4445
utilexec "k8s.io/utils/exec"
4546
testingexec "k8s.io/utils/exec/testing"
47+
mount_azure_blob "sigs.k8s.io/blob-csi-driver/pkg/blobfuse-proxy/pb"
4648
)
4749

4850
const (
@@ -935,13 +937,157 @@ func TestNodeExpandVolume(t *testing.T) {
935937
}
936938
}
937939

940+
// fakeMountServiceServer implements mount_azure_blob.MountServiceServer for testing
941+
type fakeMountServiceServer struct {
942+
mount_azure_blob.MountServiceServer
943+
mockOutput string
944+
mockError error
945+
}
946+
947+
// MountAzureBlob implements the mount service method
948+
func (s *fakeMountServiceServer) MountAzureBlob(_ context.Context, _ *mount_azure_blob.MountAzureBlobRequest) (*mount_azure_blob.MountAzureBlobResponse, error) {
949+
return &mount_azure_blob.MountAzureBlobResponse{
950+
Output: s.mockOutput,
951+
}, s.mockError
952+
}
953+
938954
func TestMountBlobfuseWithProxy(t *testing.T) {
939-
args := "--tmp-path /tmp"
940-
authEnv := []string{"username=blob", "authkey=blob"}
941-
d := NewFakeDriver()
942-
_, err := d.mountBlobfuseWithProxy(args, "fuse", authEnv)
943-
// should be context.deadlineExceededError{} error
944-
assert.NotNil(t, err)
955+
tests := []struct {
956+
desc string
957+
setup func(*testing.T) (*Driver, *grpc.Server, net.Listener)
958+
args string
959+
protocol string
960+
authEnv []string
961+
expectedOut string
962+
expectedErr error
963+
cleanupCheck func(*testing.T, string, error)
964+
}{
965+
{
966+
desc: "Success case with mock server",
967+
setup: func(t *testing.T) (*Driver, *grpc.Server, net.Listener) {
968+
// Create a mock server for the success test
969+
mockServer := &fakeMountServiceServer{
970+
mockOutput: "mock mount successful",
971+
mockError: nil,
972+
}
973+
974+
// Start a local gRPC server for testing
975+
lis, err := net.Listen("tcp", "127.0.0.1:0") // Use random available port
976+
if err != nil {
977+
t.Fatalf("Failed to listen: %v", err)
978+
}
979+
s := grpc.NewServer()
980+
mount_azure_blob.RegisterMountServiceServer(s, mockServer)
981+
982+
// Start the server in a goroutine
983+
go func() {
984+
if err := s.Serve(lis); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
985+
t.Logf("Server exited with error: %v", err)
986+
}
987+
}()
988+
989+
// Create the driver with the configured proxy
990+
d := NewFakeDriver()
991+
d.blobfuseProxyEndpoint = lis.Addr().String()
992+
d.blobfuseProxyConnTimout = 5 // 5 second timeout
993+
994+
return d, s, lis
995+
},
996+
args: "--tmp-path /tmp",
997+
protocol: "fuse",
998+
authEnv: []string{"username=blob", "authkey=blob"},
999+
expectedOut: "mock mount successful",
1000+
expectedErr: nil,
1001+
cleanupCheck: func(t *testing.T, output string, err error) {
1002+
assert.NoError(t, err)
1003+
assert.Equal(t, "mock mount successful", output)
1004+
},
1005+
},
1006+
{
1007+
desc: "Error from MountAzureBlob",
1008+
setup: func(t *testing.T) (*Driver, *grpc.Server, net.Listener) {
1009+
// Create a mock server for the error test
1010+
mockServer := &fakeMountServiceServer{
1011+
mockOutput: "",
1012+
mockError: fmt.Errorf("mock mount error"),
1013+
}
1014+
1015+
// Start a local gRPC server for testing
1016+
lis, err := net.Listen("tcp", "127.0.0.1:0") // Use random available port
1017+
if err != nil {
1018+
t.Fatalf("Failed to listen: %v", err)
1019+
}
1020+
s := grpc.NewServer()
1021+
mount_azure_blob.RegisterMountServiceServer(s, mockServer)
1022+
1023+
// Start the server in a goroutine
1024+
go func() {
1025+
if err := s.Serve(lis); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
1026+
t.Logf("Server exited with error: %v", err)
1027+
}
1028+
}()
1029+
1030+
// Create the driver with the configured proxy
1031+
d := NewFakeDriver()
1032+
d.blobfuseProxyEndpoint = lis.Addr().String()
1033+
d.blobfuseProxyConnTimout = 5 // 5 second timeout
1034+
1035+
return d, s, lis
1036+
},
1037+
args: "--tmp-path /tmp",
1038+
protocol: "fuse",
1039+
authEnv: []string{"username=blob", "authkey=blob"},
1040+
expectedOut: "",
1041+
expectedErr: fmt.Errorf("mock mount error"),
1042+
cleanupCheck: func(t *testing.T, _ string, err error) {
1043+
assert.Error(t, err)
1044+
assert.Contains(t, err.Error(), "mock mount error")
1045+
},
1046+
},
1047+
{
1048+
desc: "Connection error case",
1049+
setup: func(_ *testing.T) (*Driver, *grpc.Server, net.Listener) {
1050+
// Create the driver with a non-existent endpoint
1051+
d := NewFakeDriver()
1052+
d.blobfuseProxyEndpoint = "unix://non-existent-socket.sock"
1053+
d.blobfuseProxyConnTimout = 1 // 1 second timeout for quick failure
1054+
1055+
// No server or listener for this test
1056+
return d, nil, nil
1057+
},
1058+
args: "--tmp-path /tmp",
1059+
protocol: "fuse",
1060+
authEnv: []string{"username=blob", "authkey=blob"},
1061+
expectedOut: "",
1062+
expectedErr: fmt.Errorf("failed to exit idle mode: invalid (non-empty) authority: non-existent-socket.sock"),
1063+
cleanupCheck: func(t *testing.T, output string, err error) {
1064+
assert.Error(t, err)
1065+
assert.Empty(t, output)
1066+
assert.Contains(t, err.Error(), "invalid (non-empty) authority: non-existent-socket.sock")
1067+
},
1068+
},
1069+
}
1070+
1071+
for _, test := range tests {
1072+
t.Run(test.desc, func(t *testing.T) {
1073+
// Setup the test
1074+
d, s, lis := test.setup(t)
1075+
1076+
// Cleanup resources if needed
1077+
if s != nil && lis != nil {
1078+
defer s.Stop()
1079+
defer lis.Close()
1080+
}
1081+
1082+
// Run the test - call mountBlobfuseWithProxy directly
1083+
output, err := d.mountBlobfuseWithProxy(test.args, test.protocol, test.authEnv)
1084+
1085+
// Verify results
1086+
if test.cleanupCheck != nil {
1087+
test.cleanupCheck(t, output, err)
1088+
}
1089+
})
1090+
}
9451091
}
9461092

9471093
func TestMountBlobfuseInsideDriver(t *testing.T) {

0 commit comments

Comments
 (0)