@@ -18,6 +18,8 @@ package server
1818
1919import (
2020 "context"
21+ "os/exec"
22+ "strings"
2123 "testing"
2224
2325 "github.com/stretchr/testify/require"
@@ -64,3 +66,95 @@ func TestServerMountAzureBlob(t *testing.T) {
6466 })
6567 }
6668}
69+
70+ // fakeExecCommand is used to mock exec.Command for testing, it returns list of args
71+ func fakeExecCommandEchoArgs (command string , args ... string ) * exec.Cmd {
72+ return exec .Command ("echo" , append ([]string {"-n" }, args ... )... )
73+ }
74+
75+ func TestServerMountAzureBlob_Telemetry (t * testing.T ) {
76+ driverVersion = "fake-version"
77+ t .Parallel ()
78+ testCases := []struct {
79+ name string
80+ args string
81+ code codes.Code
82+ mountServer MountServer
83+ areValidTelemetryArgs func (cmdArgs string ) bool
84+ }{
85+ {
86+ name : "mount_with_telemetry_tag_blobfusev2" ,
87+ args : "--account-name=testaccount --container-name=testcontainer --telemetry=volume1-app1 --tmp-path=/tmp/blobfuse-tmp" ,
88+ mountServer : MountServer {
89+ blobfuseVersion : BlobfuseV2 ,
90+ exec : fakeExecCommandEchoArgs ,
91+ },
92+ code : codes .OK ,
93+ areValidTelemetryArgs : func (cmdArgs string ) bool {
94+ expectedTelemetryArg := "--telemetry=" + telemetryTagPrefix + driverVersion + ",volume1-app1"
95+ return strings .Contains (cmdArgs , expectedTelemetryArg )
96+ },
97+ },
98+ {
99+ name : "mount_without_telemetry_tag_blobfusev2" ,
100+ args : "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp" ,
101+ mountServer : MountServer {
102+ blobfuseVersion : BlobfuseV2 ,
103+ exec : fakeExecCommandEchoArgs ,
104+ },
105+ code : codes .OK ,
106+ areValidTelemetryArgs : func (cmdArgs string ) bool {
107+ expectedTelemetryArg := "--telemetry=" + telemetryTagPrefix + driverVersion
108+ return strings .Contains (cmdArgs , expectedTelemetryArg )
109+ },
110+ },
111+ {
112+ name : "mount_with_same_telemetry_tag_blobfusev2" ,
113+ args : "--account-name=testaccount --container-name=testcontainer --telemetry=" + telemetryTagPrefix + driverVersion ,
114+ mountServer : MountServer {
115+ blobfuseVersion : BlobfuseV2 ,
116+ exec : fakeExecCommandEchoArgs ,
117+ },
118+ code : codes .OK ,
119+ areValidTelemetryArgs : func (cmdArgs string ) bool {
120+ // Argument order should remain unchanged
121+ return strings .Contains (cmdArgs , "--account-name=testaccount --container-name=testcontainer --telemetry=" + telemetryTagPrefix + driverVersion )
122+ },
123+ },
124+ {
125+ name : "mount_with_blobfusev1" ,
126+ args : "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp" ,
127+ mountServer : MountServer {
128+ blobfuseVersion : BlobfuseV1 ,
129+ exec : fakeExecCommandEchoArgs ,
130+ },
131+ code : codes .OK ,
132+ areValidTelemetryArgs : func (cmdArgs string ) bool {
133+ // No telemetry arg should be added for blobfuse v1
134+ return ! strings .Contains (cmdArgs , "--telemetry=" ) && cmdArgs == "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp"
135+ },
136+ },
137+ }
138+
139+ for i := range testCases {
140+ tc := testCases [i ]
141+
142+ t .Run (tc .name , func (t * testing.T ) {
143+ t .Parallel ()
144+
145+ req := mount_azure_blob.MountAzureBlobRequest {
146+ MountArgs : tc .args ,
147+ AuthEnv : []string {},
148+ }
149+ res , err := tc .mountServer .MountAzureBlob (context .Background (), & req )
150+ if tc .code == codes .OK {
151+ require .NoError (t , err )
152+ require .NotNil (t , res )
153+ require .True (t , tc .areValidTelemetryArgs (res .Output ), "telemetry args are not valid in command args: %s" , res .Output )
154+ } else {
155+ require .Error (t , err )
156+ require .NotNil (t , res )
157+ }
158+ })
159+ }
160+ }
0 commit comments