Skip to content

Commit

Permalink
filesystem.go: add warning when path matches device
Browse files Browse the repository at this point in the history
  • Loading branch information
prestist committed Jun 29, 2022
1 parent ed8f87b commit 6c7921a
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/shared/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var (
ErrNoPath = errors.New("path not specified")
ErrPathRelative = errors.New("path not absolute")
ErrDirtyPath = errors.New("path is not fully simplified")
ErrPathCollision = errors.New("path is the same as device name")
ErrRaidLevelRequired = errors.New("raid level is required")
ErrSparesUnsupportedForLevel = errors.New("spares unsupported for linear and raid0 arrays")
ErrUnrecognizedRaidLevel = errors.New("unrecognized raid level")
Expand Down
11 changes: 11 additions & 0 deletions config/v3_0/types/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (f Filesystem) Validate(c path.ContextPath) (r report.Report) {
r.AddOnError(c.Append("device"), validatePath(f.Device))
r.AddOnError(c.Append("format"), f.validateFormat())
r.AddOnError(c.Append("label"), f.validateLabel())
r.AddOnWarn(c.Append("path"), f.warnOnPathSameAsDevice())
return
}

Expand Down Expand Up @@ -102,3 +103,13 @@ func (f Filesystem) validateLabel() error {
}
return nil
}

func (f Filesystem) warnOnPathSameAsDevice() error {
if f.Path == nil || *f.Path == "" || f.Device == "" {
return nil
}
if f.Device == *f.Path {
return errors.ErrPathCollision
}
return nil
}
39 changes: 39 additions & 0 deletions config/v3_0/types/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,42 @@ func TestLabelValidate(t *testing.T) {
}
}
}

func TestFilesystemwarnOnPathSameAsDevice(t *testing.T) {
tests := []struct {
in Filesystem
out error
}{
{
Filesystem{Device: "/dev/sda", Path: util.StrToPtr("/dev/sda")},
errors.ErrPathCollision,
},
{
Filesystem{Path: util.StrToPtr("/dev/sda/part1"), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr(""), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: nil, Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr("/foo"), Device: ""},
nil,
},
{
Filesystem{Path: nil},
nil,
},
}

for i, test := range tests {
err := test.in.warnOnPathSameAsDevice()
if test.out != err {
t.Errorf("#%d: bad error: want %v, got %v", i, test.out, err)
}
}
}
11 changes: 11 additions & 0 deletions config/v3_1/types/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,24 @@ func (f Filesystem) Validate(c path.ContextPath) (r report.Report) {
r.AddOnError(c.Append("device"), validatePath(f.Device))
r.AddOnError(c.Append("format"), f.validateFormat())
r.AddOnError(c.Append("label"), f.validateLabel())
r.AddOnWarn(c.Append("path"), f.warnOnPathSameAsDevice())
return
}

func (f Filesystem) validatePath() error {
return validatePathNilOK(f.Path)
}

func (f Filesystem) warnOnPathSameAsDevice() error {
if f.Path == nil || *f.Path == "" || f.Device == "" {
return nil
}
if f.Device == *f.Path {
return errors.ErrPathCollision
}
return nil
}

func (f Filesystem) validateFormat() error {
if util.NilOrEmpty(f.Format) {
if util.NotEmpty(f.Path) ||
Expand Down
43 changes: 39 additions & 4 deletions config/v3_1/types/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ func TestFilesystemValidatePath(t *testing.T) {
Filesystem{Path: nil},
nil,
},
{
Filesystem{Path: util.StrToPtr("foo")},
errors.ErrPathRelative,
},
}

for i, test := range tests {
Expand Down Expand Up @@ -192,3 +188,42 @@ func TestLabelValidate(t *testing.T) {
}
}
}

func TestFilesystemwarnOnPathSameAsDevice(t *testing.T) {
tests := []struct {
in Filesystem
out error
}{
{
Filesystem{Device: "/dev/sda", Path: util.StrToPtr("/dev/sda")},
errors.ErrPathCollision,
},
{
Filesystem{Path: util.StrToPtr("/dev/sda/part1"), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr(""), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: nil, Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr("/foo"), Device: ""},
nil,
},
{
Filesystem{Path: nil},
nil,
},
}

for i, test := range tests {
err := test.in.warnOnPathSameAsDevice()
if test.out != err {
t.Errorf("#%d: bad error: want %v, got %v", i, test.out, err)
}
}
}
11 changes: 11 additions & 0 deletions config/v3_2/types/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (f Filesystem) Validate(c path.ContextPath) (r report.Report) {
r.AddOnError(c.Append("device"), validatePath(f.Device))
r.AddOnError(c.Append("format"), f.validateFormat())
r.AddOnError(c.Append("label"), f.validateLabel())
r.AddOnWarn(c.Append("path"), f.warnOnPathSameAsDevice())
return
}

Expand Down Expand Up @@ -104,3 +105,13 @@ func (f Filesystem) validateLabel() error {
}
return nil
}

func (f Filesystem) warnOnPathSameAsDevice() error {
if f.Path == nil || *f.Path == "" || f.Device == "" {
return nil
}
if f.Device == *f.Path {
return errors.ErrPathCollision
}
return nil
}
39 changes: 39 additions & 0 deletions config/v3_2/types/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,42 @@ func TestLabelValidate(t *testing.T) {
}
}
}

func TestFilesystemwarnOnPathSameAsDevice(t *testing.T) {
tests := []struct {
in Filesystem
out error
}{
{
Filesystem{Device: "/dev/sda", Path: util.StrToPtr("/dev/sda")},
errors.ErrPathCollision,
},
{
Filesystem{Path: util.StrToPtr("/dev/sda/part1"), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr(""), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: nil, Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr("/foo"), Device: ""},
nil,
},
{
Filesystem{Path: nil},
nil,
},
}

for i, test := range tests {
err := test.in.warnOnPathSameAsDevice()
if test.out != err {
t.Errorf("#%d: bad error: want %v, got %v", i, test.out, err)
}
}
}
11 changes: 11 additions & 0 deletions config/v3_3/types/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (f Filesystem) Validate(c path.ContextPath) (r report.Report) {
r.AddOnError(c.Append("device"), validatePath(f.Device))
r.AddOnError(c.Append("format"), f.validateFormat())
r.AddOnError(c.Append("label"), f.validateLabel())
r.AddOnWarn(c.Append("path"), f.warnOnPathSameAsDevice())
return
}

Expand Down Expand Up @@ -104,3 +105,13 @@ func (f Filesystem) validateLabel() error {
}
return nil
}

func (f Filesystem) warnOnPathSameAsDevice() error {
if f.Path == nil || *f.Path == "" || f.Device == "" {
return nil
}
if f.Device == *f.Path {
return errors.ErrPathCollision
}
return nil
}
39 changes: 39 additions & 0 deletions config/v3_3/types/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,42 @@ func TestLabelValidate(t *testing.T) {
}
}
}

func TestFilesystemwarnOnPathSameAsDevice(t *testing.T) {
tests := []struct {
in Filesystem
out error
}{
{
Filesystem{Device: "/dev/sda", Path: util.StrToPtr("/dev/sda")},
errors.ErrPathCollision,
},
{
Filesystem{Path: util.StrToPtr("/dev/sda/part1"), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr(""), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: nil, Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr("/foo"), Device: ""},
nil,
},
{
Filesystem{Path: nil},
nil,
},
}

for i, test := range tests {
err := test.in.warnOnPathSameAsDevice()
if test.out != err {
t.Errorf("#%d: bad error: want %v, got %v", i, test.out, err)
}
}
}
11 changes: 11 additions & 0 deletions config/v3_4_experimental/types/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (f Filesystem) Validate(c path.ContextPath) (r report.Report) {
r.AddOnError(c.Append("device"), validatePath(f.Device))
r.AddOnError(c.Append("format"), f.validateFormat())
r.AddOnError(c.Append("label"), f.validateLabel())
r.AddOnWarn(c.Append("path"), f.warnOnPathSameAsDevice())
return
}

Expand Down Expand Up @@ -104,3 +105,13 @@ func (f Filesystem) validateLabel() error {
}
return nil
}

func (f Filesystem) warnOnPathSameAsDevice() error {
if f.Path == nil || *f.Path == "" || f.Device == "" {
return nil
}
if f.Device == *f.Path {
return errors.ErrPathCollision
}
return nil
}
39 changes: 39 additions & 0 deletions config/v3_4_experimental/types/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,42 @@ func TestLabelValidate(t *testing.T) {
}
}
}

func TestFilesystemwarnOnPathSameAsDevice(t *testing.T) {
tests := []struct {
in Filesystem
out error
}{
{
Filesystem{Device: "/dev/sda", Path: util.StrToPtr("/dev/sda")},
errors.ErrPathCollision,
},
{
Filesystem{Path: util.StrToPtr("/dev/sda/part1"), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr(""), Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: nil, Device: "/dev/sda"},
nil,
},
{
Filesystem{Path: util.StrToPtr("/foo"), Device: ""},
nil,
},
{
Filesystem{Path: nil},
nil,
},
}

for i, test := range tests {
err := test.in.warnOnPathSameAsDevice()
if test.out != err {
t.Errorf("#%d: bad error: want %v, got %v", i, test.out, err)
}
}
}

0 comments on commit 6c7921a

Please sign in to comment.