Skip to content

Commit 8e74fa3

Browse files
committed
chore: combine case insensitive key/value setting in parameters
1 parent 1bbc39b commit 8e74fa3

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

pkg/smb/controllerserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
110110
if err = os.Mkdir(internalVolumePath, 0777); err != nil && !os.IsExist(err) {
111111
return nil, status.Errorf(codes.Internal, "failed to make subdirectory: %v", err.Error())
112112
}
113-
parameters[subDirField] = smbVol.subDir
113+
setKeyValueInMap(parameters, subDirField, smbVol.subDir)
114114
} else {
115115
klog.V(2).Infof("CreateVolume(%s) does not create subdirectory", name)
116116
}

pkg/smb/smb.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,18 @@ func hasGuestMountOptions(options []string) bool {
141141
}
142142
return false
143143
}
144+
145+
// setKeyValueInMap set key/value pair in map
146+
// key in the map is case insensitive, if key already exists, overwrite existing value
147+
func setKeyValueInMap(m map[string]string, key, value string) {
148+
if m == nil {
149+
return
150+
}
151+
for k := range m {
152+
if strings.EqualFold(k, key) {
153+
m[k] = value
154+
return
155+
}
156+
}
157+
m[key] = value
158+
}

pkg/smb/smb_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io/ioutil"
2121
"os"
2222
"path/filepath"
23+
"reflect"
2324
"testing"
2425

2526
"github.com/stretchr/testify/assert"
@@ -175,3 +176,57 @@ func TestHasGuestMountOptions(t *testing.T) {
175176
}
176177
}
177178
}
179+
180+
func TestSetKeyValueInMap(t *testing.T) {
181+
tests := []struct {
182+
desc string
183+
m map[string]string
184+
key string
185+
value string
186+
expected map[string]string
187+
}{
188+
{
189+
desc: "nil map",
190+
key: "key",
191+
value: "value",
192+
},
193+
{
194+
desc: "empty map",
195+
m: map[string]string{},
196+
key: "key",
197+
value: "value",
198+
expected: map[string]string{"key": "value"},
199+
},
200+
{
201+
desc: "non-empty map",
202+
m: map[string]string{"k": "v"},
203+
key: "key",
204+
value: "value",
205+
expected: map[string]string{
206+
"k": "v",
207+
"key": "value",
208+
},
209+
},
210+
{
211+
desc: "same key already exists",
212+
m: map[string]string{"subDir": "value2"},
213+
key: "subDir",
214+
value: "value",
215+
expected: map[string]string{"subDir": "value"},
216+
},
217+
{
218+
desc: "case insentive key already exists",
219+
m: map[string]string{"subDir": "value2"},
220+
key: "subdir",
221+
value: "value",
222+
expected: map[string]string{"subDir": "value"},
223+
},
224+
}
225+
226+
for _, test := range tests {
227+
setKeyValueInMap(test.m, test.key, test.value)
228+
if !reflect.DeepEqual(test.m, test.expected) {
229+
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, test.m, test.expected)
230+
}
231+
}
232+
}

0 commit comments

Comments
 (0)