Skip to content

Commit

Permalink
(feat): Add config.Validator test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 27, 2023
1 parent d780cb5 commit dc6ed06
Show file tree
Hide file tree
Showing 8 changed files with 607 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ go tool cover -html=coverage.out -o coverage.html
rm -rf coverage.out

# The HTML file can either be opened locally or served through HTTP using a CLI tool like miniserve
# https://github.com/svenstaro/miniserve
# https://github.com/svenstaro/miniserve. The latter is great if you are work
# e.g `miniserve coverage.html`
26 changes: 13 additions & 13 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ devices:
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
configPath, err := createConfigFile(subtest.Data)
utils.CheckErrorGlob("createConfigFile()", t, nil, err)
utils.CheckError("createConfigFile()", t, nil, err)
defer os.Remove(configPath)

c, err := New([]string{"ebs-bootstrap", "-config", configPath})
utils.CheckErrorGlob("config.New()", t, subtest.ExpectedErr, err)
// Config contains the unexported attribute "overrides"
// We need to allow go-cmp to inspect the contents of unexported attributes
utils.CheckOutputCmp("config.New()", t, subtest.ExpectedOutput, c, cmp.AllowUnexported(Config{}))
utils.CheckOutput("config.New()", t, subtest.ExpectedOutput, c, cmp.AllowUnexported(Config{}))
})
}
}

func TestFlagParsing(t *testing.T) {
// Create a variable to the current working directory
d, err := os.Getwd()
utils.CheckErrorGlob("os.Getwd()", t, nil, err)
utils.CheckError("os.Getwd()", t, nil, err)

subtests := []struct {
Name string
Expand Down Expand Up @@ -148,7 +148,7 @@ devices:
Name: "Default Options for Non-Existent Device",
Data: []byte(`---
devices:
/dev/null: ~`),
/dev/nonexist: ~`),
ExpectedOutput: &Options{
Mode: model.Healthcheck,
Remount: false,
Expand All @@ -162,11 +162,11 @@ devices:
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
configPath, err := createConfigFile(subtest.Data)
utils.CheckErrorGlob("createConfigFile()", t, nil, err)
utils.CheckError("createConfigFile()", t, nil, err)
defer os.Remove(configPath)

c, err := New([]string{"ebs-bootstrap", "-config", configPath})
utils.CheckErrorGlob("config.New()", t, subtest.ExpectedErr, err)
utils.CheckError("config.New()", t, subtest.ExpectedErr, err)

d := &Options{
Mode: c.GetMode(device),
Expand All @@ -175,7 +175,7 @@ devices:
ResizeFs: c.GetResizeFs(device),
ResizeThreshold: c.GetResizeThreshold(device),
}
utils.CheckOutputCmp("config.New()", t, subtest.ExpectedOutput, d)
utils.CheckOutput("config.New()", t, subtest.ExpectedOutput, d)
})
}
}
Expand All @@ -185,7 +185,7 @@ func TestFlagOptions(t *testing.T) {
c, err := createConfigFile([]byte(fmt.Sprintf(`---
devices:
%s: ~`, device)))
utils.CheckErrorGlob("createConfigFile()", t, nil, err)
utils.CheckError("createConfigFile()", t, nil, err)
defer os.Remove(c)
subtests := []struct {
Name string
Expand Down Expand Up @@ -233,7 +233,7 @@ devices:
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
c, err := New(subtest.Args)
utils.CheckErrorGlob("config.New()", t, subtest.ExpectedErr, err)
utils.CheckError("config.New()", t, subtest.ExpectedErr, err)

o := &Options{
Mode: c.GetMode(device),
Expand All @@ -242,7 +242,7 @@ devices:
ResizeFs: c.GetResizeFs(device),
ResizeThreshold: c.GetResizeThreshold(device),
}
utils.CheckOutputCmp("config.New()", t, subtest.ExpectedOutput, o)
utils.CheckOutput("config.New()", t, subtest.ExpectedOutput, o)
})
}
}
Expand Down Expand Up @@ -309,11 +309,11 @@ devices:
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
configPath, err := createConfigFile(subtest.Data)
utils.CheckErrorGlob("createConfigFile()", t, nil, err)
utils.CheckError("createConfigFile()", t, nil, err)
defer os.Remove(configPath)

c, err := New([]string{"ebs-bootstrap", "-config", configPath})
utils.CheckErrorGlob("config.New()", t, subtest.ExpectedErr, err)
utils.CheckError("config.New()", t, subtest.ExpectedErr, err)

d := &Options{
Mode: c.GetMode(device),
Expand All @@ -322,7 +322,7 @@ devices:
ResizeFs: c.GetResizeFs(device),
ResizeThreshold: c.GetResizeThreshold(device),
}
utils.CheckOutputCmp("config.New()", t, subtest.ExpectedOutput, d)
utils.CheckOutput("config.New()", t, subtest.ExpectedOutput, d)
})
}
}
Expand Down
29 changes: 16 additions & 13 deletions internal/config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (fsv *ModeValidator) Validate(c *Config) error {
mode := string(c.Defaults.Mode)
_, err := model.ParseMode(mode)
if err != nil {
return fmt.Errorf("πŸ”΄ '%s' (defaults) is not a global mode", mode)
return fmt.Errorf("πŸ”΄ '%s' (defaults) is not a supported mode", mode)
}

mode = string(c.overrides.Mode)
Expand All @@ -75,7 +75,7 @@ func (fsv *ModeValidator) Validate(c *Config) error {
mode := string(device.Mode)
_, err := model.ParseMode(mode)
if err != nil {
return fmt.Errorf("πŸ”΄ %s: %s is not a supported mode", name, mode)
return fmt.Errorf("πŸ”΄ %s: '%s' is not a supported mode", name, mode)
}
}
return nil
Expand All @@ -89,14 +89,13 @@ func NewMountPointValidator() *MountPointValidator {

func (apv *MountPointValidator) Validate(c *Config) error {
for name, device := range c.Devices {
if len(device.MountPoint) == 0 {
continue
}
if !path.IsAbs(device.MountPoint) {
return fmt.Errorf("πŸ”΄ %s: %s is not an absolute path", name, device.MountPoint)
}
if device.MountPoint == "/" {
return fmt.Errorf("πŸ”΄ %s: Can not be mounted to the root directory", name)
if len(device.MountPoint) > 0 {
if !path.IsAbs(device.MountPoint) {
return fmt.Errorf("πŸ”΄ %s: %s is not an absolute path", name, device.MountPoint)
}
if device.MountPoint == "/" {
return fmt.Errorf("πŸ”΄ %s: Can not be mounted to the root directory", name)
}
}
}
return nil
Expand Down Expand Up @@ -171,16 +170,20 @@ func NewResizeThresholdValidator() *ResizeThresholdValidator {
}

func (rtv *ResizeThresholdValidator) Validate(c *Config) error {
if c.Defaults.ResizeThreshold < 0 || c.Defaults.ResizeThreshold > 100 {
if !rtv.isValid(c.Defaults.ResizeThreshold) {
return fmt.Errorf("πŸ”΄ '%g' (default) must be a floating point between 0 and 100 (inclusive)", c.Defaults.ResizeThreshold)
}
if c.overrides.ResizeThreshold < 0 || c.overrides.ResizeThreshold > 100 {
if !rtv.isValid(c.overrides.ResizeThreshold) {
return fmt.Errorf("πŸ”΄ '%g' (-resize-threshold) must be a floating point between 0 and 100 (inclusive)", c.overrides.ResizeThreshold)
}
for name, device := range c.Devices {
if device.ResizeThreshold < 0 || device.ResizeThreshold > 100 {
if !rtv.isValid(device.ResizeThreshold) {
return fmt.Errorf("πŸ”΄ %s: '%g' must be a floating point between 0 and 100 (inclusive)", name, device.ResizeThreshold)
}
}
return nil
}

func (rtv *ResizeThresholdValidator) isValid(rt float64) bool {
return rt >= 0 && rt <= 100
}
Loading

0 comments on commit dc6ed06

Please sign in to comment.