From edd95b46a103b15ba205893cd664ac61b9dbbc34 Mon Sep 17 00:00:00 2001 From: mengze zhu Date: Wed, 22 Jan 2025 06:15:15 +0000 Subject: [PATCH] test: add mock client in UT for azurefile_dataplane_client.go --- pkg/blobfuse-proxy/main.go | 3 ++- pkg/blobfuse-proxy/main_test.go | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 pkg/blobfuse-proxy/main_test.go diff --git a/pkg/blobfuse-proxy/main.go b/pkg/blobfuse-proxy/main.go index 10990990d..95ae8c02b 100644 --- a/pkg/blobfuse-proxy/main.go +++ b/pkg/blobfuse-proxy/main.go @@ -29,6 +29,7 @@ import ( var ( blobfuseProxyEndpoint = flag.String("blobfuse-proxy-endpoint", "unix://tmp/blobfuse-proxy.sock", "blobfuse-proxy endpoint") + grpcServerRunner = server.RunGRPCServer ) func main() { @@ -55,7 +56,7 @@ func main() { mountServer := server.NewMountServiceServer() klog.V(2).Infof("Listening for connections on address: %v\n", listener.Addr()) - if err = server.RunGRPCServer(mountServer, false, listener); err != nil { + if err = grpcServerRunner(mountServer, false, listener); err != nil { klog.Fatalf("Error running grpc server %v. Error: %v", listener.Addr(), err) } } diff --git a/pkg/blobfuse-proxy/main_test.go b/pkg/blobfuse-proxy/main_test.go new file mode 100644 index 000000000..649d62ea4 --- /dev/null +++ b/pkg/blobfuse-proxy/main_test.go @@ -0,0 +1,48 @@ +/* +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 main + +import ( + "net" + "os" + "runtime" + "testing" + + "sigs.k8s.io/blob-csi-driver/pkg/blobfuse-proxy/pb" +) + +func mockRunGRPCServer(_ pb.MountServiceServer, _ bool, _ net.Listener) error { + return nil +} + +func TestMain(t *testing.T) { + // Skip test on windows + if runtime.GOOS == "windows" { + t.Skip("Skipping test on ", runtime.GOOS) + } + + // mock the grpcServerRunner + originalGRPCServerRunner := grpcServerRunner + grpcServerRunner = mockRunGRPCServer + defer func() { grpcServerRunner = originalGRPCServerRunner }() + + // Set the blobfuse-proxy-endpoint + os.Args = []string{"cmd", "-blobfuse-proxy-endpoint=unix://tmp/test.sock"} + + // Run main + main() +}