Skip to content

Commit

Permalink
Feat: Add validations, error handle and specs for that.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasminvalim committed Oct 9, 2023
1 parent 884ef13 commit aa69e5d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ var (

// Kernel arguments
ErrGeneralKernelArgumentSupport = errors.New("kernel argument customization is not supported in this spec version")

// Selinux Module
ErrContentInvalid = errors.New("Content is empty, please provide content.")
ErrPathInvalid = errors.New("Path is empty, please provide a valid path.")
)

type ErrUnmarshal struct {
Expand Down
11 changes: 11 additions & 0 deletions config/fcos/v1_6_exp/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,14 @@ func (user GrubUser) Validate(c path.ContextPath) (r report.Report) {
}
return
}

func (se SelinuxModule) Validate(c path.ContextPath) (r report.Report) {
if se.Path != "" && se.Content == "" {
r.AddOnError(c.Append("content"), common.ErrContentInvalid)
}

if se.Content != "" && se.Path == "" {
r.AddOnError(c.Append("path"), common.ErrPathInvalid)
}
return r
}
52 changes: 52 additions & 0 deletions config/fcos/v1_6_exp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,55 @@ func TestValidateConfig(t *testing.T) {
})
}
}

func TestValidateSelinuxModule(t *testing.T) {
tests := []struct {
in SelinuxModule
out error
errPath path.ContextPath
}{
{
// content is empty, path is specified
in: SelinuxModule{
Content: "",
Path: "/some/path",
},
out: common.ErrContentInvalid,
errPath: path.New("yaml", "content"),
},
{
// path is empty, content is specified
in: SelinuxModule{
Path: "",
Content: "some content",
},
out: common.ErrPathInvalid,
errPath: path.New("yaml", "path"),
},
{
// path and content are empty
in: SelinuxModule{},
out: nil,
errPath: path.New("yaml"),
},
{
// path and content are specified
in: SelinuxModule{
Content: "some content",
Path: "/some/path",
},
out: nil,
errPath: path.New("yaml"),
},
}

for i, test := range tests {
t.Run(fmt.Sprintf("validate %d", i), func(t *testing.T) {
actual := test.in.Validate(path.New("yaml"))
baseutil.VerifyReport(t, test.in, actual)
expected := report.Report{}
expected.AddOnError(test.errPath, test.out)
assert.Equal(t, expected, actual, "bad report")
})
}
}

0 comments on commit aa69e5d

Please sign in to comment.