mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Update the unit tests to handle mountFlags
This commit is contained in:
parent
338f8ba0bf
commit
b9b76dba6e
@ -447,6 +447,7 @@ func TestSensitiveMountOptions(t *testing.T) {
|
|||||||
fstype string
|
fstype string
|
||||||
options []string
|
options []string
|
||||||
sensitiveOptions []string
|
sensitiveOptions []string
|
||||||
|
mountFlags []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -455,6 +456,7 @@ func TestSensitiveMountOptions(t *testing.T) {
|
|||||||
fstype: "myFS",
|
fstype: "myFS",
|
||||||
options: []string{"o1", "o2"},
|
options: []string{"o1", "o2"},
|
||||||
sensitiveOptions: []string{"s1", "s2"},
|
sensitiveOptions: []string{"s1", "s2"},
|
||||||
|
mountFlags: []string{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -463,6 +465,7 @@ func TestSensitiveMountOptions(t *testing.T) {
|
|||||||
fstype: "myFS",
|
fstype: "myFS",
|
||||||
options: []string{},
|
options: []string{},
|
||||||
sensitiveOptions: []string{"s1", "s2"},
|
sensitiveOptions: []string{"s1", "s2"},
|
||||||
|
mountFlags: []string{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -471,26 +474,44 @@ func TestSensitiveMountOptions(t *testing.T) {
|
|||||||
fstype: "myFS",
|
fstype: "myFS",
|
||||||
options: []string{"o1", "o2"},
|
options: []string{"o1", "o2"},
|
||||||
sensitiveOptions: []string{},
|
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 {
|
for _, v := range testcases {
|
||||||
// Act
|
// 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
|
// Assert
|
||||||
t.Logf("\r\nmountArgs =%q\r\nmountArgsLogStr=%q", mountArgs, mountArgsLogStr)
|
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 {
|
for _, option := range v.options {
|
||||||
if found := contains(mountArgs, option, t); !found {
|
if found := mountArgsContainOption(t, mountArgs, option); !found {
|
||||||
t.Errorf("Expected option (%q) to exist in returned mountArts (%q), but it does not", option, mountArgs)
|
t.Errorf("Expected option (%q) to exist in returned mountArgs (%q), but it does not", option, mountArgs)
|
||||||
}
|
}
|
||||||
if !strings.Contains(mountArgsLogStr, option) {
|
if !strings.Contains(mountArgsLogStr, option) {
|
||||||
t.Errorf("Expected option (%q) to exist in returned mountArgsLogStr (%q), but it does", option, mountArgsLogStr)
|
t.Errorf("Expected option (%q) to exist in returned mountArgsLogStr (%q), but it does", option, mountArgsLogStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, sensitiveOption := range v.sensitiveOptions {
|
for _, sensitiveOption := range v.sensitiveOptions {
|
||||||
if found := contains(mountArgs, sensitiveOption, t); !found {
|
if found := mountArgsContainOption(t, mountArgs, sensitiveOption); !found {
|
||||||
t.Errorf("Expected sensitiveOption (%q) to exist in returned mountArts (%q), but it does not", sensitiveOption, mountArgs)
|
t.Errorf("Expected sensitiveOption (%q) to exist in returned mountArgs (%q), but it does not", sensitiveOption, mountArgs)
|
||||||
}
|
}
|
||||||
if strings.Contains(mountArgsLogStr, sensitiveOption) {
|
if strings.Contains(mountArgsLogStr, sensitiveOption) {
|
||||||
t.Errorf("Expected sensitiveOption (%q) to not exist in returned mountArgsLogStr (%q), but it does", sensitiveOption, mountArgsLogStr)
|
t.Errorf("Expected sensitiveOption (%q) to not exist in returned mountArgsLogStr (%q), but it does", sensitiveOption, mountArgsLogStr)
|
||||||
@ -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
|
optionsIndex := -1
|
||||||
for i, s := range slice {
|
for i, s := range mountArgs {
|
||||||
if s == "-o" {
|
if s == "-o" {
|
||||||
optionsIndex = i + 1
|
optionsIndex = i + 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if optionsIndex < 0 || optionsIndex >= len(slice) {
|
if optionsIndex < 0 || optionsIndex >= len(mountArgs) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Contains(slice[optionsIndex], str)
|
return strings.Contains(mountArgs[optionsIndex], option)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user