|
| 1 | +# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Integration tests for hotplugging vCPUs""" |
| 5 | + |
| 6 | +import re |
| 7 | +import time |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from framework import microvm |
| 12 | +from framework.defs import MAX_SUPPORTED_VCPUS |
| 13 | +from framework.microvm import Serial |
| 14 | +from framework.utils_cpuid import check_guest_cpuid_output |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize("vcpu_count", [1, MAX_SUPPORTED_VCPUS - 1]) |
| 18 | +def test_hotplug_vcpus(uvm_plain, vcpu_count): |
| 19 | + """Test hotplugging works as intended""" |
| 20 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 21 | + uvm_plain.spawn() |
| 22 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 23 | + uvm_plain.add_net_iface() |
| 24 | + uvm_plain.start() |
| 25 | + |
| 26 | + # Need to allow time for VM to finish starting before API call is made |
| 27 | + time.sleep(0.5) |
| 28 | + |
| 29 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 30 | + |
| 31 | + check_guest_cpuid_output( |
| 32 | + uvm_plain, |
| 33 | + "lscpu", |
| 34 | + None, |
| 35 | + ":", |
| 36 | + { |
| 37 | + "CPU(s)": str(1 + vcpu_count), |
| 38 | + "Off-line CPU(s) list": "1" if vcpu_count == 1 else f"1-{vcpu_count}", |
| 39 | + }, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "vcpu_count", [-1, 0, MAX_SUPPORTED_VCPUS, MAX_SUPPORTED_VCPUS + 1] |
| 45 | +) |
| 46 | +def test_negative_hotplug_vcpus(uvm_plain, vcpu_count): |
| 47 | + """Test cases where hotplugging should fail.""" |
| 48 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 49 | + uvm_plain.spawn() |
| 50 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 51 | + uvm_plain.start() |
| 52 | + |
| 53 | + # Need to allow time for VM to finish starting before API call is made |
| 54 | + time.sleep(0.5) |
| 55 | + |
| 56 | + if vcpu_count == 0: |
| 57 | + with pytest.raises( |
| 58 | + RuntimeError, |
| 59 | + match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be greater than 0.", |
| 60 | + ): |
| 61 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 62 | + elif vcpu_count < 0: |
| 63 | + with pytest.raises( |
| 64 | + RuntimeError, |
| 65 | + match=re.compile( |
| 66 | + f"An error occurred when deserializing the json body of a request: invalid value: integer `-\\d+`, expected u8+" |
| 67 | + ), |
| 68 | + ): |
| 69 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 70 | + elif vcpu_count > 31: |
| 71 | + with pytest.raises( |
| 72 | + RuntimeError, |
| 73 | + match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be less than 32.", |
| 74 | + ): |
| 75 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize("vcpu_count", [1, MAX_SUPPORTED_VCPUS - 1]) |
| 79 | +def test_online_hotplugged_vcpus(uvm_plain, vcpu_count): |
| 80 | + """Test that hotplugged CPUs can be onlined""" |
| 81 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 82 | + uvm_plain.spawn() |
| 83 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 84 | + uvm_plain.add_net_iface() |
| 85 | + uvm_plain.start() |
| 86 | + |
| 87 | + # Need to allow time for VM to finish starting before API call is made |
| 88 | + time.sleep(0.5) |
| 89 | + |
| 90 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 91 | + |
| 92 | + _, _, stderr = uvm_plain.ssh.run( |
| 93 | + f"for i in {{1..{vcpu_count}}}; do echo 1 > /sys/devices/system/cpu/cpu$i/online; done" |
| 94 | + ) |
| 95 | + |
| 96 | + assert stderr == "" |
| 97 | + |
| 98 | + check_guest_cpuid_output( |
| 99 | + uvm_plain, |
| 100 | + "lscpu", |
| 101 | + None, |
| 102 | + ":", |
| 103 | + { |
| 104 | + "CPU(s)": str(1 + vcpu_count), |
| 105 | + "On-line CPU(s) list": "0,1" if vcpu_count == 1 else f"0-{vcpu_count}", |
| 106 | + }, |
| 107 | + ) |
0 commit comments