Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcpu_set: Implement plus-minus operators #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 10 additions & 2 deletions igvm/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,20 @@ 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('-'):
if not offline:
raise IGVMError(
'Decreasing CPU count is only allowed offline.'
)
count = vm.dataset_obj['num_cpu'] - int(str(count)[1:])
elif int(count) == vm.dataset_obj['num_cpu']:
raise Warning('CPU count is the same.')

if offline:
vm.shutdown()
vm.set_num_cpu(count)
vm.set_num_cpu(int(count))
if offline:
vm.start()

Expand Down
14 changes: 12 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,21 @@ def _get_cpus_vm():
with self.assertRaises(IGVMError):
vcpu_set(VM_HOSTNAME, 0, offline=True)

# Has to be offline
with self.assertRaises(IGVMError):
vcpu_set(VM_HOSTNAME, -5)
vcpu_set(VM_HOSTNAME, '-1')

# Not enough CPUs to remove
with self.assertRaises(IGVMError):
vcpu_set(VM_HOSTNAME, -5, offline=True)
vcpu_set(VM_HOSTNAME, '-5', offline=True)

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

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

def test_sync(self):
obj = (
Expand Down