Skip to content

Commit fcc2f79

Browse files
committed
Create functional integration tests for hotplugging
Create some functional integration tests for hotplugging. Covering positive and negative cases, as well as onlining of vCPUs Signed-off-by: James Curtis <[email protected]>
1 parent 9e7580e commit fcc2f79

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
"""Test hotplugging works as intended"""
22+
uvm_plain.jailer.extra_args.update({"no-seccomp": None})
23+
uvm_plain.spawn()
24+
uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128)
25+
uvm_plain.add_net_iface()
26+
uvm_plain.start()
27+
uvm_plain.wait_for_up()
28+
29+
uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count})
30+
31+
time.sleep(1)
32+
33+
check_guest_cpuid_output(
34+
uvm_plain,
35+
"lscpu",
36+
None,
37+
":",
38+
{
39+
"CPU(s)": str(1 + vcpu_count),
40+
"Off-line CPU(s) list": "1" if vcpu_count == 1 else f"1-{vcpu_count}",
41+
},
42+
)
43+
44+
45+
@pytest.mark.skipif(
46+
platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64."
47+
)
48+
@pytest.mark.parametrize(
49+
"vcpu_count", [-1, 0, MAX_SUPPORTED_VCPUS, MAX_SUPPORTED_VCPUS + 1]
50+
)
51+
def test_negative_hotplug_vcpus(uvm_plain, vcpu_count):
52+
"""Test cases where hotplugging should fail."""
53+
uvm_plain.jailer.extra_args.update({"no-seccomp": None})
54+
uvm_plain.spawn()
55+
uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128)
56+
uvm_plain.start()
57+
58+
# Need to allow time for VM to finish starting before API call is made
59+
time.sleep(0.5)
60+
61+
if vcpu_count == 0:
62+
with pytest.raises(
63+
RuntimeError,
64+
match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be greater than 0.",
65+
):
66+
uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count})
67+
elif vcpu_count < 0:
68+
with pytest.raises(
69+
RuntimeError,
70+
match=re.compile(
71+
"An error occurred when deserializing the json body of a request: invalid value: integer `-\\d+`, expected u8+"
72+
),
73+
):
74+
uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count})
75+
elif vcpu_count > 31:
76+
with pytest.raises(
77+
RuntimeError,
78+
match="Hotplug error: Vcpu hotplugging error: The number of vCPUs added must be less than 32.",
79+
):
80+
uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count})
81+
82+
83+
@pytest.mark.skipif(
84+
platform.machine() != "x86_64", reason="Hotplug only enabled on x86_64."
85+
)
86+
@pytest.mark.parametrize("vcpu_count", [1, MAX_SUPPORTED_VCPUS - 1])
87+
def test_online_hotplugged_vcpus(uvm_plain, vcpu_count):
88+
"""Test that hotplugged CPUs can be onlined"""
89+
uvm_plain.jailer.extra_args.update({"no-seccomp": None})
90+
uvm_plain.spawn()
91+
uvm_plain.basic_config(vcpu_count=1, mem_size_mib=128)
92+
uvm_plain.add_net_iface()
93+
uvm_plain.start()
94+
uvm_plain.wait_for_up()
95+
96+
uvm_plain.api.hotplug.put(Vcpu={"add": vcpu_count})
97+
98+
time.sleep(1)
99+
100+
_, _, stderr = uvm_plain.ssh.run(
101+
f"for i in {{1..{vcpu_count}}}; do echo 1 > /sys/devices/system/cpu/cpu$i/online; done"
102+
)
103+
104+
assert stderr == ""
105+
106+
time.sleep(5)
107+
108+
check_guest_cpuid_output(
109+
uvm_plain,
110+
"lscpu",
111+
None,
112+
":",
113+
{
114+
"CPU(s)": str(1 + vcpu_count),
115+
"On-line CPU(s) list": "0,1" if vcpu_count == 1 else f"0-{vcpu_count}",
116+
},
117+
)

0 commit comments

Comments
 (0)