Skip to content

Commit

Permalink
fix: mountPermission settting issue due to case insensitive parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Dec 30, 2023
1 parent 7a00134 commit 34102de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/azurefile/nodeserver.go
Expand Up @@ -76,7 +76,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
return &csi.NodePublishVolumeResponse{}, err
}

if perm := context[mountPermissionsField]; perm != "" {
if perm := getValueInMap(context, mountPermissionsField); perm != "" {
var err error
if mountPermissions, err = strconv.ParseUint(perm, 8, 32); err != nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid mountPermissions %s", perm))
Expand Down
14 changes: 14 additions & 0 deletions pkg/azurefile/utils.go
Expand Up @@ -256,6 +256,20 @@ func setKeyValueInMap(m map[string]string, key, value string) {
m[key] = value
}

// getValueInMap get value from map by key
// key in the map is case insensitive
func getValueInMap(m map[string]string, key string) string {
if m == nil {
return ""
}
for k, v := range m {
if strings.EqualFold(k, key) {
return v
}
}
return ""
}

// replaceWithMap replace key with value for str
func replaceWithMap(str string, m map[string]string) string {
for k, v := range m {
Expand Down
46 changes: 46 additions & 0 deletions pkg/azurefile/utils_test.go
Expand Up @@ -538,6 +538,52 @@ func TestSetKeyValueInMap(t *testing.T) {
}
}

func TestGetValueInMap(t *testing.T) {
tests := []struct {
desc string
m map[string]string
key string
expected string
}{
{
desc: "nil map",
key: "key",
expected: "",
},
{
desc: "empty map",
m: map[string]string{},
key: "key",
expected: "",
},
{
desc: "non-empty map",
m: map[string]string{"k": "v"},
key: "key",
expected: "",
},
{
desc: "same key already exists",
m: map[string]string{"subDir": "value2"},
key: "subDir",
expected: "value2",
},
{
desc: "case insensitive key already exists",
m: map[string]string{"subDir": "value2"},
key: "subdir",
expected: "value2",
},
}

for _, test := range tests {
result := getValueInMap(test.m, test.key)
if result != test.expected {
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, result, test.expected)
}
}
}

func TestReplaceWithMap(t *testing.T) {
tests := []struct {
desc string
Expand Down

0 comments on commit 34102de

Please sign in to comment.