@@ -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,127 @@ 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 (_ string , args ... string ) * exec.Cmd {
72+ return exec .Command ("echo" , append ([]string {"-n" }, args ... )... )
73+ }
74+
75+ func TestAddTelemetryTagToArgs (t * testing.T ) {
76+ driverVersion = "fake-version-ut"
77+ t .Parallel ()
78+ testCases := []struct {
79+ name string
80+ args string
81+ expectedArgs string
82+ }{
83+ {
84+ name : "args_without_telemetry_option" ,
85+ args : "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp" ,
86+ expectedArgs : "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp --telemetry=" + telemetryTagPrefix + driverVersion ,
87+ },
88+ {
89+ name : "args_with_some_telemetry_option" ,
90+ args : "--account-name=testaccount --container-name=testcontainer --telemetry=app1-volume1 --tmp-path=/tmp/blobfuse-tmp" ,
91+ expectedArgs : "--account-name=testaccount --container-name=testcontainer --telemetry=" + telemetryTagPrefix + driverVersion + ",app1-volume1 --tmp-path=/tmp/blobfuse-tmp" ,
92+ },
93+ {
94+ name : "args_with_csi_driver_telemetry_option" ,
95+ args : "--account-name=testaccount --container-name=testcontainer --telemetry=" + telemetryTagPrefix + driverVersion + ",app1-volume1 --tmp-path=/tmp/blobfuse-tmp" ,
96+ expectedArgs : "--account-name=testaccount --container-name=testcontainer --telemetry=" + telemetryTagPrefix + driverVersion + ",app1-volume1 --tmp-path=/tmp/blobfuse-tmp" ,
97+ },
98+ {
99+ name : "args_with_some_telemetry_option_only" ,
100+ args : "--telemetry=app1-volume1" ,
101+ expectedArgs : "--telemetry=" + telemetryTagPrefix + driverVersion + ",app1-volume1" ,
102+ },
103+ {
104+ name : "args_with_multiple_telemetry_options" ,
105+ args : "--account-name=testaccount --container-name=testcontainer --telemetry=app1 --tmp-path=/tmp/blobfuse-tmp --telemetry=app2" ,
106+ expectedArgs : "--account-name=testaccount --container-name=testcontainer --telemetry=" + telemetryTagPrefix + driverVersion + ",app1 --tmp-path=/tmp/blobfuse-tmp --telemetry=app2" ,
107+ },
108+ }
109+ for i := range testCases {
110+ tc := testCases [i ]
111+
112+ t .Run (tc .name , func (t * testing.T ) {
113+ t .Parallel ()
114+ actualArgs := addTelemetryTagToArgs (tc .args )
115+ require .Equal (t , tc .expectedArgs , actualArgs )
116+ })
117+ }
118+ }
119+
120+ func TestServerMountAzureBlob_Telemetry (t * testing.T ) {
121+ driverVersion = "fake-version"
122+ t .Parallel ()
123+ testCases := []struct {
124+ name string
125+ args string
126+ code codes.Code
127+ mountServer MountServer
128+ areValidTelemetryArgs func (cmdArgs string ) bool
129+ }{
130+ {
131+ name : "mount_with_telemetry_tag_blobfusev2" ,
132+ args : "--account-name=testaccount --container-name=testcontainer --telemetry=volume1-app1 --tmp-path=/tmp/blobfuse-tmp" ,
133+ mountServer : MountServer {
134+ blobfuseVersion : BlobfuseV2 ,
135+ exec : fakeExecCommandEchoArgs ,
136+ },
137+ code : codes .OK ,
138+ areValidTelemetryArgs : func (cmdArgs string ) bool {
139+ expectedTelemetryArg := "--telemetry=" + telemetryTagPrefix + driverVersion + ",volume1-app1"
140+ return strings .Contains (cmdArgs , expectedTelemetryArg )
141+ },
142+ },
143+ {
144+ name : "mount_without_telemetry_tag_blobfusev2" ,
145+ args : "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp" ,
146+ mountServer : MountServer {
147+ blobfuseVersion : BlobfuseV2 ,
148+ exec : fakeExecCommandEchoArgs ,
149+ },
150+ code : codes .OK ,
151+ areValidTelemetryArgs : func (cmdArgs string ) bool {
152+ expectedTelemetryArg := "--telemetry=" + telemetryTagPrefix + driverVersion
153+ return strings .Contains (cmdArgs , expectedTelemetryArg )
154+ },
155+ },
156+ {
157+ name : "mount_with_blobfusev1" ,
158+ args : "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp" ,
159+ mountServer : MountServer {
160+ blobfuseVersion : BlobfuseV1 ,
161+ exec : fakeExecCommandEchoArgs ,
162+ },
163+ code : codes .OK ,
164+ areValidTelemetryArgs : func (cmdArgs string ) bool {
165+ // No telemetry arg should be added for blobfuse v1
166+ return ! strings .Contains (cmdArgs , "--telemetry=" ) && cmdArgs == "--account-name=testaccount --container-name=testcontainer --tmp-path=/tmp/blobfuse-tmp"
167+ },
168+ },
169+ }
170+
171+ for i := range testCases {
172+ tc := testCases [i ]
173+
174+ t .Run (tc .name , func (t * testing.T ) {
175+ t .Parallel ()
176+
177+ req := mount_azure_blob.MountAzureBlobRequest {
178+ MountArgs : tc .args ,
179+ AuthEnv : []string {},
180+ }
181+ res , err := tc .mountServer .MountAzureBlob (context .Background (), & req )
182+ if tc .code == codes .OK {
183+ require .NoError (t , err )
184+ require .NotNil (t , res )
185+ require .True (t , tc .areValidTelemetryArgs (res .Output ), "telemetry args are mismatching in command args: %s" , res .Output )
186+ } else {
187+ require .Error (t , err )
188+ require .NotNil (t , res )
189+ }
190+ })
191+ }
192+ }
0 commit comments