Skip to content

Commit 5c45e5b

Browse files
committed
fix: update telemtery tag and add Unit test for telemetry parsing
Signed-off-by: mittachaitu <[email protected]>
1 parent 8d9b9fd commit 5c45e5b

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

pkg/blobfuse-proxy/server/server.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ var (
3838
driverVersion string
3939
)
4040

41+
// telemetryTagPrefix is used to identify the mounts done via blobcsi driver
42+
const telemetryTagPrefix = "blobpartner-csi/"
43+
4144
type BlobfuseVersion int
4245

4346
const (
@@ -48,12 +51,14 @@ const (
4851
type MountServer struct {
4952
blobfuseVersion BlobfuseVersion
5053
mount_azure_blob.UnimplementedMountServiceServer
54+
exec func(name string, arg ...string) *exec.Cmd
5155
}
5256

5357
// NewMountServer returns a new Mountserver
5458
func NewMountServiceServer() *MountServer {
5559
mountServer := &MountServer{}
5660
mountServer.blobfuseVersion = getBlobfuseVersion()
61+
mountServer.exec = exec.Command
5762
return mountServer
5863
}
5964

@@ -72,7 +77,7 @@ func (server *MountServer) MountAzureBlob(_ context.Context,
7277
var cmd *exec.Cmd
7378
var result mount_azure_blob.MountAzureBlobResponse
7479
if protocol == blob.Fuse2 || server.blobfuseVersion == BlobfuseV2 {
75-
telemetryTag := "azpartner-aks/" + driverVersion
80+
telemetryTag := telemetryTagPrefix + driverVersion
7681
args = "mount " + args
7782
// add this arg for blobfuse2 to solve the issue:
7883
// https://github.com/Azure/azure-storage-fuse/issues/1015
@@ -91,7 +96,7 @@ func (server *MountServer) MountAzureBlob(_ context.Context,
9196
} else {
9297
// If telemetry flag is already present, check for aks tag if not present
9398
// then user might have their own telemetry tag append aks tag to it
94-
if !strings.Contains(args, "azpartner-aks") {
99+
if !strings.Contains(args, telemetryTagPrefix) {
95100
splitedArgs := strings.Split(args, "--telemetry=")
96101
if len(splitedArgs) == 2 {
97102
args = splitedArgs[0] + " --telemetry=" + telemetryTag + "," + splitedArgs[1]
@@ -101,11 +106,11 @@ func (server *MountServer) MountAzureBlob(_ context.Context,
101106
}
102107
args = util.TrimDuplicatedSpace(args)
103108
klog.V(2).Infof("mount with v2, protocol: %s, args: %s", protocol, args)
104-
cmd = exec.Command("blobfuse2", strings.Split(args, " ")...)
109+
cmd = server.exec("blobfuse2", strings.Split(args, " ")...)
105110
} else {
106111
args = util.TrimDuplicatedSpace(args)
107112
klog.V(2).Infof("mount with v1, protocol: %s, args: %s", protocol, args)
108-
cmd = exec.Command("blobfuse", strings.Split(args, " ")...)
113+
cmd = server.exec("blobfuse", strings.Split(args, " ")...)
109114
}
110115

111116
cmd.Env = append(os.Environ(), authEnv...)

pkg/blobfuse-proxy/server/server_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package server
1818

1919
import (
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

Comments
 (0)