Skip to content

Commit

Permalink
chore: combine case insensitive key/value setting in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Jun 12, 2022
1 parent 1bbc39b commit 8e74fa3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/smb/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
if err = os.Mkdir(internalVolumePath, 0777); err != nil && !os.IsExist(err) {
return nil, status.Errorf(codes.Internal, "failed to make subdirectory: %v", err.Error())
}
parameters[subDirField] = smbVol.subDir
setKeyValueInMap(parameters, subDirField, smbVol.subDir)
} else {
klog.V(2).Infof("CreateVolume(%s) does not create subdirectory", name)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,18 @@ func hasGuestMountOptions(options []string) bool {
}
return false
}

// setKeyValueInMap set key/value pair in map
// key in the map is case insensitive, if key already exists, overwrite existing value
func setKeyValueInMap(m map[string]string, key, value string) {
if m == nil {
return
}
for k := range m {
if strings.EqualFold(k, key) {
m[k] = value
return
}
}
m[key] = value
}
55 changes: 55 additions & 0 deletions pkg/smb/smb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -175,3 +176,57 @@ func TestHasGuestMountOptions(t *testing.T) {
}
}
}

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

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

0 comments on commit 8e74fa3

Please sign in to comment.