Skip to content

Commit

Permalink
Update the unit tests to handle mountFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Aug 12, 2021
1 parent 3572949 commit d29b9e1
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions staging/src/k8s.io/mount-utils/mount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ func TestSensitiveMountOptions(t *testing.T) {
fstype string
options []string
sensitiveOptions []string
mountFlags []string
}{
{

Expand All @@ -455,6 +456,7 @@ func TestSensitiveMountOptions(t *testing.T) {
fstype: "myFS",
options: []string{"o1", "o2"},
sensitiveOptions: []string{"s1", "s2"},
mountFlags: []string{},
},
{

Expand All @@ -463,6 +465,7 @@ func TestSensitiveMountOptions(t *testing.T) {
fstype: "myFS",
options: []string{},
sensitiveOptions: []string{"s1", "s2"},
mountFlags: []string{},
},
{

Expand All @@ -471,26 +474,44 @@ func TestSensitiveMountOptions(t *testing.T) {
fstype: "myFS",
options: []string{"o1", "o2"},
sensitiveOptions: []string{},
mountFlags: []string{},
},
{

source: "mySrc",
target: "myTarget",
fstype: "myFS",
options: []string{"o1", "o2"},
sensitiveOptions: []string{"s1", "s2"},
mountFlags: []string{"--no-canonicalize"},
},
}

for _, v := range testcases {
// Act
mountArgs, mountArgsLogStr := MakeMountArgsSensitive(v.source, v.target, v.fstype, v.options, v.sensitiveOptions)
mountArgs, mountArgsLogStr := MakeMountArgsSensitive(v.source, v.target, v.fstype, v.options, v.sensitiveOptions, v.mountFlags)

// Assert
t.Logf("\r\nmountArgs =%q\r\nmountArgsLogStr=%q", mountArgs, mountArgsLogStr)
for _, mountFlag := range v.mountFlags {
if found := mountArgsContainString(t, mountArgs, mountFlag); !found {
t.Errorf("Expected mountFlag (%q) to exist in returned mountArgs (%q), but it does not", mountFlag, mountArgs)
}
if !strings.Contains(mountArgsLogStr, mountFlag) {
t.Errorf("Expected mountFlag (%q) to exist in returned mountArgsLogStr (%q), but it does", mountFlag, mountArgsLogStr)
}
}
for _, option := range v.options {
if found := contains(mountArgs, option, t); !found {
t.Errorf("Expected option (%q) to exist in returned mountArts (%q), but it does not", option, mountArgs)
if found := mountArgsContainOption(t, mountArgs, option); !found {
t.Errorf("Expected option (%q) to exist in returned mountArgs (%q), but it does not", option, mountArgs)
}
if !strings.Contains(mountArgsLogStr, option) {
t.Errorf("Expected option (%q) to exist in returned mountArgsLogStr (%q), but it does", option, mountArgsLogStr)
}
}
for _, sensitiveOption := range v.sensitiveOptions {
if found := contains(mountArgs, sensitiveOption, t); !found {
t.Errorf("Expected sensitiveOption (%q) to exist in returned mountArts (%q), but it does not", sensitiveOption, mountArgs)
if found := mountArgsContainOption(t, mountArgs, sensitiveOption); !found {
t.Errorf("Expected sensitiveOption (%q) to exist in returned mountArgs (%q), but it does not", sensitiveOption, mountArgs)
}
if strings.Contains(mountArgsLogStr, sensitiveOption) {
t.Errorf("Expected sensitiveOption (%q) to not exist in returned mountArgsLogStr (%q), but it does", sensitiveOption, mountArgsLogStr)
Expand All @@ -499,18 +520,27 @@ func TestSensitiveMountOptions(t *testing.T) {
}
}

func contains(slice []string, str string, t *testing.T) bool {
func mountArgsContainString(t *testing.T, mountArgs []string, wanted string) bool {
for _, mountArg := range mountArgs {
if mountArg == wanted {
return true
}
}
return false
}

func mountArgsContainOption(t *testing.T, mountArgs []string, option string) bool {
optionsIndex := -1
for i, s := range slice {
for i, s := range mountArgs {
if s == "-o" {
optionsIndex = i + 1
break
}
}

if optionsIndex < 0 || optionsIndex >= len(slice) {
if optionsIndex < 0 || optionsIndex >= len(mountArgs) {
return false
}

return strings.Contains(slice[optionsIndex], str)
return strings.Contains(mountArgs[optionsIndex], option)
}

0 comments on commit d29b9e1

Please sign in to comment.