|
| 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(uvm_plain, 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.jailer.extra_args.update({"no-seccomp": None}) |
| 28 | + uvm_plain.spawn() |
| 29 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 30 | + uvm_plain.add_net_iface() |
| 31 | + uvm_plain.start() |
| 32 | + uvm_plain.wait_for_up() |
| 33 | + |
| 34 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 35 | + |
| 36 | + time.sleep(5) |
| 37 | + |
| 38 | + check_guest_cpuid_output( |
| 39 | + uvm_plain, |
| 40 | + "lscpu", |
| 41 | + None, |
| 42 | + ":", |
| 43 | + { |
| 44 | + "CPU(s)": str(1 + vcpu_count), |
| 45 | + "Off-line CPU(s) list": "1" if vcpu_count == 1 else f"1-{vcpu_count}", |
| 46 | + }, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.skipif( |
| 51 | + platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64." |
| 52 | +) |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "vcpu_count", [-1, 0, MAX_SUPPORTED_VCPUS, MAX_SUPPORTED_VCPUS + 1] |
| 55 | +) |
| 56 | +def test_negative_hotplug_vcpus(uvm_plain, vcpu_count): |
| 57 | + """ |
| 58 | + Test that the API rejects invalid calls. |
| 59 | +
|
| 60 | + Test cases where the API should reject the hot-plug request, where the |
| 61 | + number of vCPUs is either too high or too low. |
| 62 | + """ |
| 63 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 64 | + uvm_plain.spawn() |
| 65 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 66 | + uvm_plain.start() |
| 67 | + uvm_plain.wait_for_up() |
| 68 | + |
| 69 | + if vcpu_count == 0: |
| 70 | + with pytest.raises( |
| 71 | + RuntimeError, |
| 72 | + match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be greater than 0.", |
| 73 | + ): |
| 74 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 75 | + elif vcpu_count < 0: |
| 76 | + with pytest.raises( |
| 77 | + RuntimeError, |
| 78 | + match=re.compile( |
| 79 | + "An error occurred when deserializing the json body of a request: invalid value: integer `-\\d+`, expected u8+" |
| 80 | + ), |
| 81 | + ): |
| 82 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 83 | + elif vcpu_count > 31: |
| 84 | + with pytest.raises( |
| 85 | + RuntimeError, |
| 86 | + match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be less than 32.", |
| 87 | + ): |
| 88 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.skipif( |
| 92 | + platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64." |
| 93 | +) |
| 94 | +@pytest.mark.parametrize("vcpu_count", [1, MAX_SUPPORTED_VCPUS - 1]) |
| 95 | +def test_online_hotplugged_vcpus(uvm_plain, vcpu_count): |
| 96 | + """ |
| 97 | + Full end-to-end test of vCPU hot-plugging. |
| 98 | +
|
| 99 | + Makes API call and then tries to online vCPUs inside the guest. |
| 100 | + """ |
| 101 | + uvm_plain.jailer.extra_args.update({"no-seccomp": None}) |
| 102 | + uvm_plain.spawn() |
| 103 | + uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128) |
| 104 | + uvm_plain.add_net_iface() |
| 105 | + uvm_plain.start() |
| 106 | + uvm_plain.wait_for_up() |
| 107 | + |
| 108 | + uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count}) |
| 109 | + |
| 110 | + time.sleep(5) |
| 111 | + |
| 112 | + _, _, stderr = uvm_plain.ssh.run( |
| 113 | + f"for i in {{1..{vcpu_count}}}; do echo 1 > /sys/devices/system/cpu/cpu$i/online; done" |
| 114 | + ) |
| 115 | + |
| 116 | + assert stderr == "" |
| 117 | + |
| 118 | + time.sleep(5) |
| 119 | + |
| 120 | + check_guest_cpuid_output( |
| 121 | + uvm_plain, |
| 122 | + "lscpu", |
| 123 | + None, |
| 124 | + ":", |
| 125 | + { |
| 126 | + "CPU(s)": str(1 + vcpu_count), |
| 127 | + "On-line CPU(s) list": "0,1" if vcpu_count == 1 else f"0-{vcpu_count}", |
| 128 | + }, |
| 129 | + ) |
0 commit comments