Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add space trimming check in ValidateSysctls #11224

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ func ValidateSysctls(strSlice []string) (map[string]string, error) {
if len(arr) < 2 {
return nil, errors.Errorf("%s is invalid, sysctl values must be in the form of KEY=VALUE", val)
}

trimmed := fmt.Sprintf("%s=%s", strings.TrimSpace(arr[0]), strings.TrimSpace(arr[1]))
if trimmed != val {
return nil, errors.Errorf("'%s' is invalid, extra spaces found", val)
}

if validSysctlMap[arr[0]] {
sysctl[arr[0]] = arr[1]
continue
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -259,6 +260,28 @@ func TestValidateSysctlBadSysctl(t *testing.T) {
assert.Error(t, err)
}

func TestValidateSysctlBadSysctlWithExtraSpaces(t *testing.T) {
expectedError := "'%s' is invalid, extra spaces found"

// should fail fast on first sysctl
strSlice1 := []string{
"net.ipv4.ping_group_range = 0 0",
"net.ipv4.ping_group_range=0 0 ",
}
_, err := ValidateSysctls(strSlice1)
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf(expectedError, strSlice1[0]))

// should fail on second sysctl
strSlice2 := []string{
"net.ipv4.ping_group_range=0 0",
"net.ipv4.ping_group_range=0 0 ",
}
_, err = ValidateSysctls(strSlice2)
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf(expectedError, strSlice2[1]))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to add a test that should pass, perhaps:

	strSlice := []string{
		" net.ipv4.ping_group_range=0 0          ",
		"          net.ipv4.ping_group_range=0 0 ",
	}

Also, can you add a check for the error text you're expecting, or at least "extra spaces found" as the substring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomSweeneyRedHat thanks for suggestions. I was thinking that other tests for ValidateSysctls would still go through my changes, as long as we don't see regressions on the other existing tests I think there's no need to introduce other test cases? I can still add a few if you wish.

Yes, I'll update my PR to check the message as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this won't be (and shouldn't be, IMO) passing, as we do arr := strings.Split(val, "=") in ValidateSysctls.

    strSliceGood := []string{                                                    
        " net.ipv4.ping_group_range=0 0          ",                              
        "          net.ipv4.ping_group_range=0 0 ",                             
    }                                                                            
    _, err := ValidateSysctls(strSliceGood)                                     
    assert.Nil(t, err)


func TestCoresToPeriodAndQuota(t *testing.T) {
cores := 1.0
expectedPeriod := DefaultCPUPeriod
Expand Down