@@ -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
4850const (
@@ -935,13 +937,72 @@ 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 (ctx context.Context , req * mount_azure_blob.MountAzureBlobRequest ) (* mount_azure_blob.MountAzureBlobResponse , error ) {
949+ return & mount_azure_blob.MountAzureBlobResponse {
950+ Output : s .mockOutput ,
951+ }, s .mockError
952+ }
953+
938954func TestMountBlobfuseWithProxy (t * testing.T ) {
955+ // Create a mock gRPC server and client for better test coverage
956+
957+ // Create a test server with our implementation
958+ mockServer := & fakeMountServiceServer {
959+ mockOutput : "mock mount successful" ,
960+ mockError : nil ,
961+ }
962+
963+ // Start a local gRPC server for testing
964+ lis , err := net .Listen ("tcp" , "127.0.0.1:0" ) // Use random available port
965+ if err != nil {
966+ t .Fatalf ("Failed to listen: %v" , err )
967+ }
968+ s := grpc .NewServer ()
969+ mount_azure_blob .RegisterMountServiceServer (s , mockServer )
970+
971+ // Start the server in a goroutine
972+ go func () {
973+ if err := s .Serve (lis ); err != nil {
974+ t .Logf ("Server exited with error: %v" , err )
975+ }
976+ }()
977+ defer s .Stop () // Ensure server is stopped after test
978+
979+ // Run test case 1: Success case with mock server
939980 args := "--tmp-path /tmp"
940981 authEnv := []string {"username=blob" , "authkey=blob" }
941982 d := NewFakeDriver ()
942- _ , err := d .mountBlobfuseWithProxy (args , "fuse" , authEnv )
943- // should be context.deadlineExceededError{} error
944- assert .NotNil (t , err )
983+
984+ // Point to our mock server
985+ d .blobfuseProxyEndpoint = lis .Addr ().String ()
986+ d .blobfuseProxyConnTimout = 5 // 5 second timeout
987+
988+ // Test the success path
989+ output , err := d .mountBlobfuseWithProxy (args , "fuse" , authEnv )
990+ assert .NoError (t , err )
991+ assert .Equal (t , "mock mount successful" , output )
992+
993+ // Test case 2: Error from MountAzureBlob
994+ mockServer .mockError = fmt .Errorf ("mock mount error" )
995+ output , err = d .mountBlobfuseWithProxy (args , "fuse" , authEnv )
996+ assert .Error (t , err )
997+ assert .Contains (t , err .Error (), "mock mount error" )
998+
999+ // Test case 3: Connection error case (using non-existent endpoint)
1000+ d .blobfuseProxyEndpoint = "unix://non-existent-socket.sock"
1001+ d .blobfuseProxyConnTimout = 1 // 1 second timeout for quick failure
1002+
1003+ output , err = d .mountBlobfuseWithProxy (args , "fuse" , authEnv )
1004+ assert .Error (t , err )
1005+ assert .Empty (t , output )
9451006}
9461007
9471008func TestMountBlobfuseInsideDriver (t * testing.T ) {
0 commit comments