Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/framework/utils_cpuid.py
1956 views
1
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Helper functions for testing CPU identification functionality."""
4
5
import subprocess
6
from enum import Enum, auto
7
8
from framework.utils import run_cmd
9
import host_tools.network as net_tools
10
11
12
class CpuVendor(Enum):
13
"""CPU vendors enum."""
14
15
AMD = auto()
16
INTEL = auto()
17
18
19
def get_cpu_vendor():
20
"""Return the CPU vendor."""
21
brand_str = subprocess.check_output("lscpu", shell=True).strip().decode()
22
if 'AuthenticAMD' in brand_str:
23
return CpuVendor.AMD
24
return CpuVendor.INTEL
25
26
27
def get_cpu_model_name():
28
"""Return the CPU model name."""
29
_, stdout, _ = run_cmd("cat /proc/cpuinfo | grep 'model name' | uniq")
30
info = stdout.strip().split(sep=":")
31
assert len(info) == 2
32
return info[1].strip()
33
34
35
def check_guest_cpuid_output(vm, guest_cmd, expected_header,
36
expected_separator,
37
expected_key_value_store):
38
"""Parse cpuid output inside guest and match with expected one."""
39
ssh_connection = net_tools.SSHConnection(vm.ssh_config)
40
_, stdout, stderr = ssh_connection.execute_command(guest_cmd)
41
42
assert stderr.read() == ''
43
while True:
44
line = stdout.readline()
45
if line != '':
46
# All the keys have been matched. Stop.
47
if not expected_key_value_store:
48
break
49
50
# Try to match the header if needed.
51
if expected_header not in (None, ''):
52
if line.strip() == expected_header:
53
expected_header = None
54
continue
55
56
# See if any key matches.
57
# We Use a try-catch block here since line.split() may fail.
58
try:
59
[key, value] = list(
60
map(lambda x: x.strip(), line.split(expected_separator)))
61
except ValueError:
62
continue
63
64
if key in expected_key_value_store.keys():
65
assert value == expected_key_value_store[key], \
66
"%s does not have the expected value" % key
67
del expected_key_value_store[key]
68
else:
69
for given_key in expected_key_value_store.keys():
70
if given_key in key:
71
assert value == expected_key_value_store[given_key], \
72
"%s does not have the expected value" % given_key
73
del expected_key_value_store[given_key]
74
break
75
else:
76
break
77
78
assert not expected_key_value_store, \
79
"some keys in dictionary have not been found in the output: %s" \
80
% expected_key_value_store
81
82
83
def read_guest_file(vm, file):
84
"""Parse cpuid output inside guest and match with expected one."""
85
ssh_connection = net_tools.SSHConnection(vm.ssh_config)
86
_, stdout, stderr = ssh_connection.execute_command("cat {}".format(file))
87
assert stderr.read() == ""
88
return stdout.read().strip()
89
90