Skip to content

Commit 294f8ec

Browse files
committed
feat(kc-compat): include distro version in --report
1 parent 8b166f3 commit 294f8ec

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

kc-compat.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ def get_kernel_hash_from_data(version_data):
4343
return sha1(version_data).hexdigest()
4444

4545

46-
47-
4846
def inside_vz_container():
4947
"""
5048
determines if we are inside Virtuozzo container
@@ -60,24 +58,29 @@ def inside_lxc_container():
6058
def get_distro_info():
6159
"""
6260
Get current distribution name and version
63-
:return: distro name or None if detection fails
61+
:return: tuple of (distro_name, distro_version) or (None, None) if detection fails
6462
"""
6563

6664
def parse_value(line):
6765
return line.split('=', 1)[1].strip().strip('"\'')
6866

6967
os_release_path = '/etc/os-release'
7068
if not os.path.exists(os_release_path):
71-
return None
69+
return None, None
7270

7371
try:
72+
distro_name = None
73+
distro_version = None
7474
with open(os_release_path, 'r') as f:
7575
for line in f:
7676
line = line.strip()
7777
if line.startswith('ID='):
78-
return parse_value(line)
78+
distro_name = parse_value(line)
79+
elif line.startswith('VERSION_ID='):
80+
distro_version = parse_value(line)
81+
return distro_name, distro_version
7982
except (IOError, OSError):
80-
return None
83+
return None, None
8184

8285

8386
def is_distro_supported(distro_name):
@@ -127,13 +130,13 @@ def main():
127130
version_data = b''
128131

129132
kernel_hash = get_kernel_hash_from_data(version_data)
130-
distro_name = get_distro_info()
133+
distro_name, distro_version = get_distro_info()
131134

132135
if report:
133136
print("=== KernelCare Compatibility Report ===")
134137
print(f"Kernel Hash: {kernel_hash}")
135138
print(f"Distribution: {distro_name or 'Unknown'}")
136-
print(f"Version: Not available")
139+
print(f"Version: {distro_version or 'Unknown'}")
137140
print(f"Kernel: {version_data.decode('utf-8', errors='replace').strip()}")
138141
print("=====================================")
139142

test_kc_compat.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_get_kernel_hash_from_data(self):
1919

2020

2121

22+
2223
class TestContainerDetection:
2324
@patch('os.path.exists')
2425
def test_inside_vz_container_true(self, mock_exists):
@@ -57,20 +58,22 @@ class TestGetDistroInfo:
5758
@patch('os.path.exists', return_value=True)
5859
@patch('builtins.open', new_callable=mock_open, read_data='ID=centos\nVERSION_ID="7"\n')
5960
def test_get_distro_info_success(self, mock_file, mock_exists):
60-
name = kc_compat.get_distro_info()
61+
name, version = kc_compat.get_distro_info()
6162
assert name == 'centos'
62-
63+
assert version == '7'
6364

6465
@patch('os.path.exists', return_value=False)
6566
def test_get_distro_info_no_file(self, mock_exists):
66-
name = kc_compat.get_distro_info()
67+
name, version = kc_compat.get_distro_info()
6768
assert name is None
69+
assert version is None
6870

6971
@patch('os.path.exists', return_value=True)
7072
@patch('builtins.open', side_effect=IOError("Permission denied"))
7173
def test_get_distro_info_read_error(self, mock_file, mock_exists):
72-
name = kc_compat.get_distro_info()
74+
name, version = kc_compat.get_distro_info()
7375
assert name is None
76+
assert version is None
7477

7578

7679
class TestIsDistroSupported:
@@ -149,7 +152,7 @@ def test_main_compatible(self, mock_print, mock_compat, mock_lxc, mock_vz):
149152
@patch.object(kc_compat, 'inside_vz_container', return_value=False)
150153
@patch.object(kc_compat, 'inside_lxc_container', return_value=False)
151154
@patch.object(kc_compat, 'is_compat', return_value=False)
152-
@patch.object(kc_compat, 'get_distro_info', return_value='centos')
155+
@patch.object(kc_compat, 'get_distro_info', return_value=('centos', '7'))
153156
@patch.object(kc_compat, 'is_distro_supported', return_value=True)
154157
@patch('builtins.print')
155158
def test_main_kernel_not_found_but_distro_supported(self, mock_print, mock_distro_supported,
@@ -167,7 +170,7 @@ def test_main_kernel_not_found_but_distro_supported(self, mock_print, mock_distr
167170
@patch.object(kc_compat, 'inside_vz_container', return_value=False)
168171
@patch.object(kc_compat, 'inside_lxc_container', return_value=False)
169172
@patch.object(kc_compat, 'is_compat', return_value=False)
170-
@patch.object(kc_compat, 'get_distro_info', return_value='unknown')
173+
@patch.object(kc_compat, 'get_distro_info', return_value=('unknown', None))
171174
@patch.object(kc_compat, 'is_distro_supported', return_value=False)
172175
@patch('builtins.print')
173176
def test_main_kernel_not_found_distro_not_supported(self, mock_print, mock_distro_supported,
@@ -192,7 +195,7 @@ def test_main_silent_mode(self, mock_print, mock_compat, mock_lxc, mock_vz):
192195
mock_print.assert_not_called()
193196

194197
@patch('sys.argv', ['kc-compat.py', '--report'])
195-
@patch.object(kc_compat, 'get_distro_info', return_value='centos')
198+
@patch.object(kc_compat, 'get_distro_info', return_value=('centos', '7'))
196199
@patch.object(kc_compat, 'inside_vz_container', return_value=False)
197200
@patch.object(kc_compat, 'inside_lxc_container', return_value=False)
198201
@patch.object(kc_compat, 'is_compat', return_value=True)
@@ -210,7 +213,7 @@ def test_main_report_mode(self, mock_print, mock_file, mock_compat, mock_lxc, mo
210213
assert calls[0].args[0] == "=== KernelCare Compatibility Report ==="
211214
assert calls[1].args[0].startswith("Kernel Hash: ")
212215
assert calls[2].args[0] == "Distribution: centos"
213-
assert calls[3].args[0] == "Version: Not available"
216+
assert calls[3].args[0] == "Version: 7"
214217
assert calls[4].args[0] == "Kernel: Linux version 5.4.0-test"
215218
assert calls[5].args[0] == "====================================="
216219
assert calls[6].args[0] == "COMPATIBLE"

0 commit comments

Comments
 (0)