Skip to content

Commit

Permalink
Add the validator for IOPriority
Browse files Browse the repository at this point in the history
Signed-off-by: utam0k <[email protected]>
  • Loading branch information
utam0k committed Jun 11, 2023
1 parent dc31dc0 commit df79b02
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions libcontainer/configs/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func Validate(config *configs.Config) error {
sysctl,
intelrdtCheck,
rootlessEUIDCheck,
ioPriority,
}
for _, c := range checks {
if err := c(config); err != nil {
Expand Down Expand Up @@ -286,3 +287,15 @@ func isHostNetNS(path string) (bool, error) {

return (st1.Dev == st2.Dev) && (st1.Ino == st2.Ino), nil
}

func ioPriority(config *configs.Config) error {
ioPriroty := config.IOPriority
if ioPriroty == nil {
return nil
}
priority := config.IOPriority.Priority
if priority < 0 || priority > 7 {
return fmt.Errorf("invalide ioPriority.Priority: %d", priority)
}
return nil
}
29 changes: 29 additions & 0 deletions libcontainer/configs/validate/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,32 @@ func TestValidateMounts(t *testing.T) {
}
}
}

func TestValidateIOPriority(t *testing.T) {
testCases := []struct {
isErr bool
priority int
}{
{isErr: false, priority: 0},
{isErr: false, priority: 7},
{isErr: true, priority: -1},
}

for _, tc := range testCases {
ioPriroty := configs.IOPriority{
Priority: tc.priority,
}
config := &configs.Config{
Rootfs: "/var",
IOPriority: &ioPriroty,
}

err := Validate(config)
if tc.isErr && err == nil {
t.Errorf("iopriroty: %d, expected error, got nil", tc.priority)
}
if !tc.isErr && err != nil {
t.Errorf("iopriroty: %d, expected nil, got error %v", tc.priority, err)
}
}
}

0 comments on commit df79b02

Please sign in to comment.