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

View File

@ -130,26 +130,49 @@ func TestVerifyInputs(t *testing.T) {
defer cleanup(t)
tests := []struct {
name string
cfg *config.Bootstrap
args []string
expectedErr error
}{
{
name: "missing-container-field",
cfg: &config.Bootstrap{
Container: &config.Container{},
},
expectedErr: config.ErrWrongConfig{},
expectedErr: config.ErrMissingConfig{
What: "Must specify volume bind for ISO builder container",
},
},
{
name: "missing-filenames",
cfg: &config.Bootstrap{
Container: &config.Container{
Volume: tempVol + ":/dst",
},
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{
Container: &config.Container{
Volume: tempVol,
@ -161,22 +184,13 @@ func TestVerifyInputs(t *testing.T) {
},
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 {
actualErr := verifyInputs(tt.cfg)
assert.Equal(t, tt.expectedErr, actualErr)
tt := tt
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)
}
// ErrWrongConfig returned in case of incorrect configuration
type ErrWrongConfig struct {
// ErrInvalidConfig returned in case of incorrect configuration
type ErrInvalidConfig struct {
What string
}
func (e ErrWrongConfig) Error() string {
return "Wrong configuration"
func (e ErrInvalidConfig) Error() string {
return fmt.Sprintf("Invalid configuration: %s", e.What)
}
// ErrMissingConfig returned in case of missing configuration