Skip to content

Commit

Permalink
Plumbing validation error back to user (aws#3832)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Dribinsky <dribinm@amazon.com>
  • Loading branch information
2 people authored and ahreehong committed Nov 8, 2022
1 parent eba4552 commit c442b65
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/api/v1alpha1/cloudstackmachineconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *CloudStackResourceDiskOffering) IsEmpty() bool {
func (r *CloudStackResourceDiskOffering) Validate() (err error, field string, value string) {
if r != nil && (len(r.Id) > 0 || len(r.Name) > 0) {
if len(r.MountPath) < 2 || !strings.HasPrefix(r.MountPath, "/") {
return errors.New("must be non-empty and starts with /"), "mountPath", r.MountPath
return errors.New("must be non-empty and start with /"), "mountPath", r.MountPath
}
if len(r.Filesystem) < 1 {
return errors.New("empty filesystem"), "filesystem", r.Filesystem
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1alpha1/cloudstackmachineconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestCloudStackMachineConfigDiskOfferingInValidMountPathRoot(t *testing.T) {
g.Expect(err != nil).To(BeTrue())
g.Expect(fieldName == "mountPath").To(BeTrue())
g.Expect(fieldValue == "/").To(BeTrue())
g.Expect(err.Error() == "must be non-empty and starts with /").To(BeTrue())
g.Expect(err.Error() == "must be non-empty and start with /").To(BeTrue())
}

func TestCloudStackMachineConfigDiskOfferingInValidMountPathRelative(t *testing.T) {
Expand All @@ -276,7 +276,7 @@ func TestCloudStackMachineConfigDiskOfferingInValidMountPathRelative(t *testing.
g.Expect(err != nil).To(BeTrue())
g.Expect(fieldName == "mountPath").To(BeTrue())
g.Expect(fieldValue == "data").To(BeTrue())
g.Expect(err.Error() == "must be non-empty and starts with /").To(BeTrue())
g.Expect(err.Error() == "must be non-empty and start with /").To(BeTrue())
}

func TestCloudStackMachineConfigDiskOfferingValid(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1alpha1/cloudstackmachineconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (r *CloudStackMachineConfig) ValidateCreate() error {
cloudstackmachineconfiglog.Info("validate create", "name", r.Name)

if err, fieldName, fieldValue := r.Spec.DiskOffering.Validate(); err != nil {
return apierrors.NewBadRequest(fmt.Sprintf("disk offering %s:%v, preventing CloudStackMachineConfig resource creation", fieldName, fieldValue))
return apierrors.NewBadRequest(fmt.Sprintf("disk offering %s:%v, preventing CloudStackMachineConfig resource creation: %v", fieldName, fieldValue, err))
}
if err, fieldName, fieldValue := r.Spec.Symlinks.Validate(); err != nil {
return apierrors.NewBadRequest(fmt.Sprintf("symlinks %s:%v, preventing CloudStackMachineConfig resource creation", fieldName, fieldValue))
return apierrors.NewBadRequest(fmt.Sprintf("symlinks %s:%v, preventing CloudStackMachineConfig resource creation: %v", fieldName, fieldValue, err))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/cloudstack/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func TestSetupAndValidateInValidDiskOfferingBadMountPath(t *testing.T) {
cmk.EXPECT().ValidateAffinityGroupsPresent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

err = validator.ValidateClusterMachineConfigs(ctx, cloudStackClusterSpec)
wantErrMsg := "machine config test validation failed: mountPath: / invalid, must be non-empty and starts with /"
wantErrMsg := "machine config test validation failed: mountPath: / invalid, must be non-empty and start with /"
assert.Contains(t, err.Error(), wantErrMsg, "expected error containing %q, got %v", wantErrMsg, err)
}

Expand Down

0 comments on commit c442b65

Please sign in to comment.