Enhance the ErrWrongConfig error

This changes the name of ErrWrongConfig to ErrInvalidConfig, and also
adds the `What` field. This allows a user of the error to supply context
as to how configuration was incorrect.

Change-Id: I476ef9ed737e9a4eee7bef9f3b0f3417ebff60f6
This commit is contained in:
Ian Howell 2020-02-24 16:31:14 -06:00
parent 090d80ed70
commit c54705364e
3 changed files with 44 additions and 26 deletions

View File

@ -71,13 +71,15 @@ func GenerateBootstrapIso(settings *environment.AirshipCTLSettings, args []strin
func verifyInputs(cfg *config.Bootstrap) error { func verifyInputs(cfg *config.Bootstrap) error {
if cfg.Container.Volume == "" { if cfg.Container.Volume == "" {
log.Print("Specify volume bind for ISO builder container") return config.ErrMissingConfig{
return config.ErrWrongConfig{} What: "Must specify volume bind for ISO builder container",
}
} }
if (cfg.Builder.UserDataFileName == "") || (cfg.Builder.NetworkConfigFileName == "") { if (cfg.Builder.UserDataFileName == "") || (cfg.Builder.NetworkConfigFileName == "") {
log.Print("UserDataFileName or NetworkConfigFileName are not specified in ISO builder config") return config.ErrMissingConfig{
return config.ErrWrongConfig{} What: "UserDataFileName or NetworkConfigFileName are not specified in ISO builder config",
}
} }
vols := strings.Split(cfg.Container.Volume, ":") vols := strings.Split(cfg.Container.Volume, ":")
@ -85,8 +87,9 @@ func verifyInputs(cfg *config.Bootstrap) error {
case len(vols) == 1: case len(vols) == 1:
cfg.Container.Volume = fmt.Sprintf("%s:%s", vols[0], vols[0]) cfg.Container.Volume = fmt.Sprintf("%s:%s", vols[0], vols[0])
case len(vols) > 2: case len(vols) > 2:
log.Print("Bad container volume format. Use hostPath:contPath") return config.ErrInvalidConfig{
return config.ErrWrongConfig{} What: "Bad container volume format. Use hostPath:contPath",
}
} }
return nil return nil
} }

View File

@ -130,26 +130,49 @@ func TestVerifyInputs(t *testing.T) {
defer cleanup(t) defer cleanup(t)
tests := []struct { tests := []struct {
name string
cfg *config.Bootstrap cfg *config.Bootstrap
args []string args []string
expectedErr error expectedErr error
}{ }{
{ {
name: "missing-container-field",
cfg: &config.Bootstrap{ cfg: &config.Bootstrap{
Container: &config.Container{}, Container: &config.Container{},
}, },
expectedErr: config.ErrWrongConfig{}, expectedErr: config.ErrMissingConfig{
What: "Must specify volume bind for ISO builder container",
},
}, },
{ {
name: "missing-filenames",
cfg: &config.Bootstrap{ cfg: &config.Bootstrap{
Container: &config.Container{ Container: &config.Container{
Volume: tempVol + ":/dst", Volume: tempVol + ":/dst",
}, },
Builder: &config.Builder{}, Builder: &config.Builder{},
}, },
expectedErr: config.ErrWrongConfig{}, expectedErr: config.ErrMissingConfig{
What: "UserDataFileName or NetworkConfigFileName are not specified in ISO builder config",
},
}, },
{ {
name: "invalid-host-path",
cfg: &config.Bootstrap{
Container: &config.Container{
Volume: tempVol + ":/dst:/dst1",
},
Builder: &config.Builder{
UserDataFileName: "user-data",
NetworkConfigFileName: "net-conf",
},
},
expectedErr: config.ErrInvalidConfig{
What: "Bad container volume format. Use hostPath:contPath",
},
},
{
name: "success",
cfg: &config.Bootstrap{ cfg: &config.Bootstrap{
Container: &config.Container{ Container: &config.Container{
Volume: tempVol, Volume: tempVol,
@ -161,22 +184,13 @@ func TestVerifyInputs(t *testing.T) {
}, },
expectedErr: nil, expectedErr: nil,
}, },
{
cfg: &config.Bootstrap{
Container: &config.Container{
Volume: tempVol + ":/dst:/dst1",
},
Builder: &config.Builder{
UserDataFileName: "user-data",
NetworkConfigFileName: "net-conf",
},
},
expectedErr: config.ErrWrongConfig{},
},
} }
for _, tt := range tests { for _, tt := range tests {
actualErr := verifyInputs(tt.cfg) tt := tt
assert.Equal(t, tt.expectedErr, actualErr) t.Run(tt.name, func(subTest *testing.T) {
actualErr := verifyInputs(tt.cfg)
assert.Equal(subTest, tt.expectedErr, actualErr)
})
} }
} }

View File

@ -60,12 +60,13 @@ func (e ErrBootstrapInfoNotFound) Error() string {
return fmt.Sprintf("Bootstrap info %q not found", e.Name) return fmt.Sprintf("Bootstrap info %q not found", e.Name)
} }
// ErrWrongConfig returned in case of incorrect configuration // ErrInvalidConfig returned in case of incorrect configuration
type ErrWrongConfig struct { type ErrInvalidConfig struct {
What string
} }
func (e ErrWrongConfig) Error() string { func (e ErrInvalidConfig) Error() string {
return "Wrong configuration" return fmt.Sprintf("Invalid configuration: %s", e.What)
} }
// ErrMissingConfig returned in case of missing configuration // ErrMissingConfig returned in case of missing configuration