Skip to content

Commit

Permalink
Fix for identifier matching with filepath. (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored May 29, 2024
1 parent affa2f8 commit 2956956
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
20 changes: 9 additions & 11 deletions cmd/metal-api/internal/metal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,21 +496,19 @@ func capacityOf[V any](identifier string, vs []V, countFn func(v V) (model strin
matched []V
)

if identifier == "" {
identifier = "*"
}

for _, v := range vs {
model, count := countFn(v)

matches, err := filepath.Match(identifier, model)
if err != nil {
// illegal identifiers are already prevented by size validation
continue
}
if identifier != "" {
matches, err := filepath.Match(identifier, model)
if err != nil {
// illegal identifiers are already prevented by size validation
continue
}

if !matches {
continue
if !matches {
continue
}
}

sum += count
Expand Down
55 changes: 55 additions & 0 deletions cmd/metal-api/internal/metal/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ import (
)

var (
miniLabSize = Size{
Base: Base{
ID: "v1-small-x86",
},
Constraints: []Constraint{
{
Type: CoreConstraint,
Min: 1,
Max: 4,
},
{
Type: MemoryConstraint,
Min: 500000000,
Max: 4000000000,
},
{
Type: StorageConstraint,
Min: 1000000000,
Max: 10000000000,
},
},
}
mixedDiskSize = Size{
Base: Base{
ID: "mixedDisk",
Expand Down Expand Up @@ -639,11 +661,44 @@ func TestSizes_FromHardware(t *testing.T) {
want: &amdCPUSize,
wantErr: false,
},
{
name: "mini lab",
sz: Sizes{
miniLabSize,
},
args: args{
hardware: MachineHardware{
MetalCPUs: []MetalCPU{
{
Model: "11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz",
Cores: 1,
Threads: 1,
Vendor: "GenuineIntel",
},
},
Memory: 2147483648,
Disks: []BlockDevice{
{
Name: "/dev/vda",
Size: 5368709120,
},
},
},
},
want: &miniLabSize,
wantErr: false,
},
}

for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
for _, s := range tt.sz {
if err := s.Validate(nil, nil); err != nil {
t.Errorf("size validation failed: %f", err)
}
}

got, err := tt.sz.FromHardware(tt.args.hardware)
if (err != nil) != tt.wantErr {
t.Errorf("Sizes.FromHardware() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit 2956956

Please sign in to comment.