Skip to content

Commit

Permalink
Validate size contraints (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Dec 20, 2023
1 parent a84432c commit 33427a9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/metal-api/internal/metal/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ func (s *Size) overlaps(so *Size) bool {
return true
}

// Overlaps returns nil if Size does not overlap with any other size, otherwise returs overlapping Size
// Validate a size, returns error if a invalid size is passed
func (s *Size) Validate() error {
for _, c := range s.Constraints {
if c.Max < c.Min {
return fmt.Errorf("size:%q type:%q max:%d is smaller than min:%d", s.ID, c.Type, c.Max, c.Min)
}
}
return nil
}

// Overlaps returns nil if Size does not overlap with any other size, otherwise returns overlapping Size
func (s *Size) Overlaps(ss *Sizes) *Size {
for i := range *ss {
so := (*ss)[i]
Expand Down
73 changes: 73 additions & 0 deletions cmd/metal-api/internal/metal/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package metal
import (
"reflect"
"testing"

"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -538,10 +541,80 @@ func TestSizes_Overlaps(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
err := tt.sz.Validate()
require.NoError(t, err)
got := tt.sz.Overlaps(&tt.args.sizes)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Sizes.Overlaps() = %v, want %v", got, tt.want)
}
})
}
}

func TestSize_Validate(t *testing.T) {
tests := []struct {
name string
size Size
wantErrMessage *string
}{
{
name: "cpu min and max wrong",
size: Size{
Base: Base{
ID: "broken-cpu-size",
},
Constraints: []Constraint{
{
Type: CoreConstraint,
Min: 8,
Max: 2,
},
},
},
wantErrMessage: pointer.Pointer("size:\"broken-cpu-size\" type:\"cores\" max:2 is smaller than min:8"),
},
{
name: "memory min and max wrong",
size: Size{
Base: Base{
ID: "broken-memory-size",
},
Constraints: []Constraint{
{
Type: MemoryConstraint,
Min: 8,
Max: 2,
},
},
},
wantErrMessage: pointer.Pointer("size:\"broken-memory-size\" type:\"memory\" max:2 is smaller than min:8"),
},
{
name: "storage is working",
size: Size{
Base: Base{
ID: "storage-size",
},
Constraints: []Constraint{
{
Type: StorageConstraint,
Min: 2,
Max: 8,
},
},
},
wantErrMessage: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.size.Validate()
if err != nil {
require.EqualError(t, err, *tt.wantErrMessage)
}
if err == nil && tt.wantErrMessage != nil {
t.Errorf("expected error not raise:%s", *tt.wantErrMessage)
}
})
}
}
12 changes: 12 additions & 0 deletions cmd/metal-api/internal/service/size-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ func (r *sizeResource) createSize(request *restful.Request, response *restful.Re
return
}

err = s.Validate()
if err != nil {
r.sendError(request, response, defaultError(err))
return
}

if so := s.Overlaps(&ss); so != nil {
r.sendError(request, response, httperrors.BadRequest(fmt.Errorf("size overlaps with %q", so.GetID())))
return
Expand Down Expand Up @@ -303,6 +309,12 @@ func (r *sizeResource) updateSize(request *restful.Request, response *restful.Re
return
}

err = newSize.Validate()
if err != nil {
r.sendError(request, response, defaultError(err))
return
}

if so := newSize.Overlaps(&ss); so != nil {
r.sendError(request, response, httperrors.BadRequest(fmt.Errorf("size overlaps with %q", so.GetID())))
return
Expand Down

0 comments on commit 33427a9

Please sign in to comment.