@@ -22,6 +22,7 @@ import (
2222
2323 corev1 "k8s.io/api/core/v1"
2424 "k8s.io/apimachinery/pkg/types"
25+ virtv1 "kubevirt.io/api/core/v1"
2526
2627 "github.com/deckhouse/virtualization/api/core/v1alpha2"
2728)
@@ -224,6 +225,89 @@ func TestApplyPVNodeAffinity(t *testing.T) {
224225 })
225226}
226227
228+ func TestSetCPUModel (t * testing.T ) {
229+ name := "test-name"
230+ namespace := "test-namespace"
231+
232+ t .Run ("should add required ht feature for model cpu" , func (t * testing.T ) {
233+ builder := NewEmptyKVVM (types.NamespacedName {Name : name , Namespace : namespace }, KVVMOptions {})
234+ class := & v1alpha2.VirtualMachineClass {
235+ Spec : v1alpha2.VirtualMachineClassSpec {
236+ CPU : v1alpha2.CPU {Type : v1alpha2 .CPUTypeModel , Model : "Nehalem" },
237+ },
238+ }
239+
240+ if err := builder .SetCPUModel (class ); err != nil {
241+ t .Fatalf ("SetCPUModel() failed: %v" , err )
242+ }
243+
244+ features := builder .Resource .Spec .Template .Spec .Domain .CPU .Features
245+ if ! containsCPUFeature (features , virtv1.CPUFeature {Name : HTCPUFeature , Policy : "optional" }) {
246+ t .Fatalf ("expected optional ht feature to be added for model cpu, got %#v" , features )
247+ }
248+ })
249+
250+ t .Run ("should add required ht feature for discovery cpu" , func (t * testing.T ) {
251+ builder := NewEmptyKVVM (types.NamespacedName {Name : name , Namespace : namespace }, KVVMOptions {})
252+ class := & v1alpha2.VirtualMachineClass {
253+ Spec : v1alpha2.VirtualMachineClassSpec {
254+ CPU : v1alpha2.CPU {Type : v1alpha2 .CPUTypeDiscovery },
255+ },
256+ Status : v1alpha2.VirtualMachineClassStatus {
257+ CpuFeatures : v1alpha2.CpuFeatures {Enabled : []string {"aes" }},
258+ },
259+ }
260+
261+ if err := builder .SetCPUModel (class ); err != nil {
262+ t .Fatalf ("SetCPUModel() failed: %v" , err )
263+ }
264+
265+ features := builder .Resource .Spec .Template .Spec .Domain .CPU .Features
266+ if ! containsCPUFeature (features , virtv1.CPUFeature {Name : HTCPUFeature , Policy : "require" }) {
267+ t .Fatalf ("expected required ht feature to be added, got %#v" , features )
268+ }
269+ })
270+
271+ t .Run ("should keep existing ht feature policy when already present" , func (t * testing.T ) {
272+ builder := NewEmptyKVVM (types.NamespacedName {Name : name , Namespace : namespace }, KVVMOptions {})
273+ class := & v1alpha2.VirtualMachineClass {
274+ Spec : v1alpha2.VirtualMachineClassSpec {
275+ CPU : v1alpha2.CPU {Type : v1alpha2 .CPUTypeFeatures },
276+ },
277+ Status : v1alpha2.VirtualMachineClassStatus {
278+ CpuFeatures : v1alpha2.CpuFeatures {Enabled : []string {"vmx" , HTCPUFeature }},
279+ },
280+ }
281+
282+ if err := builder .SetCPUModel (class ); err != nil {
283+ t .Fatalf ("SetCPUModel() failed: %v" , err )
284+ }
285+
286+ features := builder .Resource .Spec .Template .Spec .Domain .CPU .Features
287+ htCount := 0
288+ for _ , feature := range features {
289+ if feature .Name == HTCPUFeature {
290+ htCount ++
291+ if feature .Policy != "require" {
292+ t .Fatalf ("expected existing ht policy to stay require, got %#v" , feature )
293+ }
294+ }
295+ }
296+ if htCount != 1 {
297+ t .Fatalf ("expected exactly one ht feature, got %#v" , features )
298+ }
299+ })
300+ }
301+
302+ func containsCPUFeature (features []virtv1.CPUFeature , expected virtv1.CPUFeature ) bool {
303+ for _ , feature := range features {
304+ if feature == expected {
305+ return true
306+ }
307+ }
308+ return false
309+ }
310+
227311func TestSetOsType (t * testing.T ) {
228312 name := "test-name"
229313 namespace := "test-namespace"
0 commit comments