@@ -290,7 +290,100 @@ impl MachineConfig {
290
290
#[ cfg( test) ]
291
291
mod tests {
292
292
use crate :: cpu_config:: templates:: { CpuTemplateType , CustomCpuTemplate , StaticCpuTemplate } ;
293
- use crate :: vmm_config:: machine_config:: MachineConfig ;
293
+ use crate :: vmm_config:: machine_config:: {
294
+ HugePageConfig , MachineConfig , MachineConfigError , MachineConfigUpdate ,
295
+ } ;
296
+
297
+ #[ test]
298
+ #[ allow( unused) ] // some assertions exist only on specific architectures.
299
+ fn test_machine_config_update ( ) {
300
+ let mconf = MachineConfig :: default ( ) ;
301
+
302
+ // Assert that the default machine config is valid
303
+ assert_eq ! (
304
+ mconf
305
+ . update( & MachineConfigUpdate :: from( mconf. clone( ) ) )
306
+ . unwrap( ) ,
307
+ mconf
308
+ ) ;
309
+
310
+ // Invalid vCPU counts
311
+ let res = mconf. update ( & MachineConfigUpdate {
312
+ vcpu_count : Some ( 0 ) ,
313
+ ..Default :: default ( )
314
+ } ) ;
315
+ assert_eq ! ( res, Err ( MachineConfigError :: InvalidVcpuCount ) ) ;
316
+
317
+ let res = mconf. update ( & MachineConfigUpdate {
318
+ vcpu_count : Some ( 33 ) ,
319
+ ..Default :: default ( )
320
+ } ) ;
321
+ assert_eq ! ( res, Err ( MachineConfigError :: InvalidVcpuCount ) ) ;
322
+
323
+ // Invalid memory size
324
+ let res = mconf. update ( & MachineConfigUpdate {
325
+ mem_size_mib : Some ( 0 ) ,
326
+ ..Default :: default ( )
327
+ } ) ;
328
+ assert_eq ! ( res, Err ( MachineConfigError :: InvalidMemorySize ) ) ;
329
+
330
+ // Memory Size incompatible with huge page configuration
331
+ let res = mconf. update ( & MachineConfigUpdate {
332
+ mem_size_mib : Some ( 31 ) ,
333
+ huge_pages : Some ( HugePageConfig :: Hugetlbfs2M ) ,
334
+ ..Default :: default ( )
335
+ } ) ;
336
+ assert_eq ! ( res, Err ( MachineConfigError :: InvalidMemorySize ) ) ;
337
+
338
+ // works if the memory size is a multiple of huge page size indeed
339
+ let updated = mconf
340
+ . update ( & MachineConfigUpdate {
341
+ mem_size_mib : Some ( 32 ) ,
342
+ huge_pages : Some ( HugePageConfig :: Hugetlbfs2M ) ,
343
+ ..Default :: default ( )
344
+ } )
345
+ . unwrap ( ) ;
346
+ assert_eq ! ( updated. huge_pages, HugePageConfig :: Hugetlbfs2M ) ;
347
+ assert_eq ! ( updated. mem_size_mib, 32 ) ;
348
+ }
349
+
350
+ #[ test]
351
+ #[ cfg( target_arch = "aarch64" ) ]
352
+ fn test_machine_config_update_aarch64 ( ) {
353
+ let mconf = MachineConfig :: default ( ) ;
354
+
355
+ // Check that SMT is not supported on aarch64
356
+ let res = mconf. update ( & MachineConfigUpdate {
357
+ smt : Some ( true ) ,
358
+ ..Default :: default ( )
359
+ } ) ;
360
+ assert_eq ! ( res, Err ( MachineConfigError :: SmtNotSupported ) ) ;
361
+ }
362
+
363
+ #[ test]
364
+ #[ cfg( target_arch = "x86_64" ) ]
365
+ fn test_machine_config_update_x86_64 ( ) {
366
+ let mconf = MachineConfig :: default ( ) ;
367
+
368
+ // Test that SMT requires an even vcpu count
369
+ let res = mconf. update ( & MachineConfigUpdate {
370
+ vcpu_count : Some ( 3 ) ,
371
+ smt : Some ( true ) ,
372
+ ..Default :: default ( )
373
+ } ) ;
374
+ assert_eq ! ( res, Err ( MachineConfigError :: InvalidVcpuCount ) ) ;
375
+
376
+ // Works if the vcpu count is even indeed
377
+ let updated = mconf
378
+ . update ( & MachineConfigUpdate {
379
+ vcpu_count : Some ( 32 ) ,
380
+ smt : Some ( true ) ,
381
+ ..Default :: default ( )
382
+ } )
383
+ . unwrap ( ) ;
384
+ assert_eq ! ( updated. vcpu_count, 32 ) ;
385
+ assert ! ( updated. smt) ;
386
+ }
294
387
295
388
// Ensure the special (de)serialization logic for the cpu_template field works:
296
389
// only static cpu templates can be specified via the machine-config endpoint, but
0 commit comments