Skip to content

Commit 7ce1fd0

Browse files
authored
fix(vm): correct CPU core validation logic for range checks (#824)
Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
1 parent 4dae847 commit 7ce1fd0

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

images/virtualization-artifact/pkg/controller/vm/internal/validators/cpu_count_validator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func (v *CpuCountValidator) Validate(vm *v1alpha2.VirtualMachine) (admission.War
4545
switch {
4646
case cores <= 16:
4747
return nil, nil
48-
case cores <= 32 && cores%2 != 0:
48+
case cores > 16 && cores <= 32 && cores%2 != 0:
4949
return nil, fmt.Errorf("the requested number of cores must be a multiple of 2")
50-
case cores <= 64 && cores%4 != 0:
50+
case cores > 32 && cores <= 64 && cores%4 != 0:
5151
return nil, fmt.Errorf("the requested number of cores must be a multiple of 4")
52-
case cores%8 != 0:
52+
case cores > 64 && cores%8 != 0:
5353
return nil, fmt.Errorf("the requested number of cores must be a multiple of 8")
5454
}
5555

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2024 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package validators
18+
19+
import (
20+
"testing"
21+
22+
"github.com/deckhouse/virtualization/api/core/v1alpha2"
23+
)
24+
25+
func TestCpuCountValidate(t *testing.T) {
26+
tests := []struct {
27+
desiredCores int
28+
valid bool
29+
}{
30+
{1, true},
31+
{2, true},
32+
{3, true},
33+
{4, true},
34+
{5, true},
35+
{15, true},
36+
{16, true},
37+
38+
{18, true},
39+
{19, false},
40+
{20, true},
41+
{31, false},
42+
{32, true},
43+
44+
{36, true},
45+
{37, false},
46+
{40, true},
47+
{60, true},
48+
{63, false},
49+
{64, true},
50+
51+
{72, true},
52+
{76, false},
53+
{80, true},
54+
{248, true},
55+
{256, true},
56+
{252, false},
57+
}
58+
59+
for _, test := range tests {
60+
t.Run("", func(t *testing.T) {
61+
vm := &v1alpha2.VirtualMachine{Spec: v1alpha2.VirtualMachineSpec{CPU: v1alpha2.CPUSpec{Cores: test.desiredCores}}}
62+
cpuCountValidator := NewCpuCountValidator()
63+
64+
_, err := cpuCountValidator.Validate(vm)
65+
66+
if test.valid && err != nil {
67+
t.Errorf("For %d cores, expected valid, but validation failed", test.desiredCores)
68+
}
69+
70+
if !test.valid && err == nil {
71+
t.Errorf("For %d cores, expected not valid, but validation succeeded", test.desiredCores)
72+
}
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)