Skip to content

Commit

Permalink
vcpu_set: Implement plus-minus operators
Browse files Browse the repository at this point in the history
  • Loading branch information
seqizz committed Apr 5, 2024
1 parent b2326e9 commit 0aff980
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions igvm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ def parse_args():
)
subparser.add_argument(
'count',
type=int,
help='New number of CPUs',
help='New number of CPUs, integers with optional prefix + or -',
)
subparser.add_argument(
'--offline',
Expand Down
6 changes: 5 additions & 1 deletion igvm/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ def vcpu_set(vm_hostname, count, offline=False):
)
offline = False

if count == vm.dataset_obj['num_cpu']:
if str(count).startswith('+'):
count = vm.dataset_obj['num_cpu'] + int(str(count)[1:])
elif str(count).startswith('-'):
count = vm.dataset_obj['num_cpu'] - int(str(count)[1:])
elif count == vm.dataset_obj['num_cpu']:
raise Warning('CPU count is the same.')

if offline:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ def _get_cpus_vm():
with self.assertRaises(IGVMError):
vcpu_set(VM_HOSTNAME, -5, offline=True)

vcpu_set(VM_HOSTNAME, -1, offline=True)
self.assertEqual(_get_cpus_hv(), 1)
self.assertEqual(_get_cpus_vm(), 1)

vcpu_set(VM_HOSTNAME, +1)
self.assertEqual(_get_cpus_hv(), 2)
self.assertEqual(_get_cpus_vm(), 2)

def test_sync(self):
obj = (
Query({'hostname': VM_HOSTNAME}, ['disk_size_gib', 'memory']).get()
Expand Down

0 comments on commit 0aff980

Please sign in to comment.