|
| 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 platform |
| 7 | +import re |
| 8 | +import time |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from framework.defs import MAX_SUPPORTED_VCPUS |
| 13 | +from framework.utils_cpuid import check_guest_cpuid_output |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.skipif( |
| 17 | + platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64." |
| 18 | +) |
| 19 | +@pytest.mark.parametrize("vcpu_count", [1, MAX_SUPPORTED_VCPUS - 1]) |
| 20 | +def test_hotplug_vcpus(microvm_factory, guest_kernel_linux_6_1, rootfs_rw, vcpu_count): |
| 21 | + """ |
| 22 | + Test that hot-plugging API call functions as intended. |
| 23 | +
|
| 24 | + After the API call has been made, the new vCPUs should show up in the |
| 25 | + guest as offline. |
| 26 | + """ |
| 27 | + uvm_plain = microvm_factory.build(guest_kernel_linux_6_1, rootfs_rw) |
| 28 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 29 | + uvm_plain.spawn() |
| 30 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 31 | + uvm_plain.add_net_iface() |
| 32 | + uvm_plain.start() |
| 33 | + uvm_plain.wait_for_up() |
| 34 | + |
| 35 | + # Default udev rules are flaky, sometimes they automatically online CPUs, |
| 36 | + # but other times they don't. Remove the respective rule in this test so |
| 37 | + # they are added as offline every time. |
| 38 | + uvm_plain.ssh.run( |
| 39 | + "rm /usr/lib/udev/rules.d/40-vm-hotadd.rules && udevadm control --reload-rules" |
| 40 | + ) |
| 41 | + |
| 42 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 43 | + |
| 44 | + time.sleep(5) |
| 45 | + |
| 46 | + check_guest_cpuid_output( |
| 47 | + uvm_plain, |
| 48 | + "lscpu", |
| 49 | + None, |
| 50 | + ":", |
| 51 | + { |
| 52 | + "CPU(s)": str(1 + vcpu_count), |
| 53 | + "Off-line CPU(s) list": "1" if vcpu_count == 1 else f"1-{vcpu_count}", |
| 54 | + }, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.skipif( |
| 59 | + platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64." |
| 60 | +) |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "vcpu_count", [-1, 0, MAX_SUPPORTED_VCPUS, MAX_SUPPORTED_VCPUS + 1] |
| 63 | +) |
| 64 | +def test_negative_hotplug_vcpus( |
| 65 | + microvm_factory, guest_kernel_linux_6_1, rootfs_rw, vcpu_count |
| 66 | +): |
| 67 | + """ |
| 68 | + Test that the API rejects invalid calls. |
| 69 | +
|
| 70 | + Test cases where the API should reject the hot-plug request, where the |
| 71 | + number of vCPUs is either too high or too low. |
| 72 | + """ |
| 73 | + uvm_plain = microvm_factory.build(guest_kernel_linux_6_1, rootfs_rw) |
| 74 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 75 | + uvm_plain.spawn() |
| 76 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 77 | + uvm_plain.add_net_iface() |
| 78 | + uvm_plain.start() |
| 79 | + uvm_plain.wait_for_up() |
| 80 | + |
| 81 | + if vcpu_count == 0: |
| 82 | + with pytest.raises( |
| 83 | + RuntimeError, |
| 84 | + match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be greater than 0.", |
| 85 | + ): |
| 86 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 87 | + elif vcpu_count < 0: |
| 88 | + with pytest.raises( |
| 89 | + RuntimeError, |
| 90 | + match=re.compile( |
| 91 | + "An error occurred when deserializing the json body of a request: invalid value: integer `-\\d+`, expected u8+" |
| 92 | + ), |
| 93 | + ): |
| 94 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 95 | + elif vcpu_count > 31: |
| 96 | + with pytest.raises( |
| 97 | + RuntimeError, |
| 98 | + match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be less than 32.", |
| 99 | + ): |
| 100 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.skipif( |
| 104 | + platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64." |
| 105 | +) |
| 106 | +@pytest.mark.parametrize("vcpu_count", [1, MAX_SUPPORTED_VCPUS - 1]) |
| 107 | +def test_online_hotplugged_vcpus( |
| 108 | + microvm_factory, guest_kernel_linux_6_1, rootfs_rw, vcpu_count |
| 109 | +): |
| 110 | + """ |
| 111 | + Full end-to-end test of vCPU hot-plugging. |
| 112 | +
|
| 113 | + Makes API call and then tries to online vCPUs inside the guest. |
| 114 | + """ |
| 115 | + uvm_plain = microvm_factory.build(guest_kernel_linux_6_1, rootfs_rw) |
| 116 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 117 | + uvm_plain.spawn() |
| 118 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 119 | + uvm_plain.add_net_iface() |
| 120 | + uvm_plain.start() |
| 121 | + uvm_plain.wait_for_up() |
| 122 | + |
| 123 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 124 | + |
| 125 | + time.sleep(5) |
| 126 | + |
| 127 | + _, _, stderr = uvm_plain.ssh.run( |
| 128 | + f"for i in {{1..{vcpu_count}}}; do echo 1 > /sys/devices/system/cpu/cpu$i/online; done" |
| 129 | + ) |
| 130 | + |
| 131 | + assert stderr == "" |
| 132 | + |
| 133 | + time.sleep(5) |
| 134 | + |
| 135 | + check_guest_cpuid_output( |
| 136 | + uvm_plain, |
| 137 | + "lscpu", |
| 138 | + None, |
| 139 | + ":", |
| 140 | + { |
| 141 | + "CPU(s)": str(1 + vcpu_count), |
| 142 | + "On-line CPU(s) list": "0,1" if vcpu_count == 1 else f"0-{vcpu_count}", |
| 143 | + }, |
| 144 | + ) |
0 commit comments