|
11 | 11 |
|
12 | 12 |
|
13 | 13 | class TestGetKernelHash: |
14 | | - @patch('builtins.open', new_callable=mock_open, read_data=b'Linux version 5.4.0-test') |
15 | | - def test_get_kernel_hash_success(self, mock_file): |
16 | | - result = kc_compat.get_kernel_hash() |
| 14 | + def test_get_kernel_hash_from_data(self): |
| 15 | + version_data = b'Linux version 5.4.0-test' |
| 16 | + result = kc_compat.get_kernel_hash_from_data(version_data) |
17 | 17 | assert isinstance(result, str) |
18 | 18 | assert len(result) == 40 # SHA1 hex digest length |
19 | | - mock_file.assert_called_once_with('/proc/version', 'rb') |
20 | 19 |
|
21 | | - @patch('builtins.open', side_effect=IOError("File not found")) |
22 | | - def test_get_kernel_hash_file_error(self, mock_file): |
23 | | - with pytest.raises(IOError): |
24 | | - kc_compat.get_kernel_hash() |
25 | 20 |
|
26 | 21 |
|
27 | 22 | class TestContainerDetection: |
@@ -86,32 +81,28 @@ def test_is_distro_supported(self): |
86 | 81 |
|
87 | 82 |
|
88 | 83 | class TestIsCompat: |
89 | | - @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') |
90 | 84 | @patch.object(kc_compat, 'urlopen') |
91 | | - def test_is_compat_success(self, mock_urlopen, mock_hash): |
| 85 | + def test_is_compat_success(self, mock_urlopen): |
92 | 86 | mock_urlopen.return_value = MagicMock() |
93 | | - assert kc_compat.is_compat() == True |
| 87 | + assert kc_compat.is_compat('abcdef123456') == True |
94 | 88 | mock_urlopen.assert_called_once_with('http://patches.kernelcare.com/abcdef123456/version') |
95 | 89 |
|
96 | | - @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') |
97 | 90 | @patch.object(kc_compat, 'urlopen') |
98 | | - def test_is_compat_404_error_returns_false(self, mock_urlopen, mock_hash): |
| 91 | + def test_is_compat_404_error_returns_false(self, mock_urlopen): |
99 | 92 | mock_urlopen.side_effect = HTTPError(None, 404, 'Not Found', None, None) |
100 | | - assert kc_compat.is_compat() == False |
| 93 | + assert kc_compat.is_compat('abcdef123456') == False |
101 | 94 |
|
102 | | - @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') |
103 | 95 | @patch.object(kc_compat, 'urlopen') |
104 | | - def test_is_compat_500_error_raises(self, mock_urlopen, mock_hash): |
| 96 | + def test_is_compat_500_error_raises(self, mock_urlopen): |
105 | 97 | mock_urlopen.side_effect = HTTPError(None, 500, 'Server Error', None, None) |
106 | 98 | with pytest.raises(HTTPError): |
107 | | - kc_compat.is_compat() |
| 99 | + kc_compat.is_compat('abcdef123456') |
108 | 100 |
|
109 | | - @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') |
110 | 101 | @patch.object(kc_compat, 'urlopen') |
111 | | - def test_is_compat_url_error_raises(self, mock_urlopen, mock_hash): |
| 102 | + def test_is_compat_url_error_raises(self, mock_urlopen): |
112 | 103 | mock_urlopen.side_effect = URLError('Connection refused') |
113 | 104 | with pytest.raises(URLError): |
114 | | - kc_compat.is_compat() |
| 105 | + kc_compat.is_compat('abcdef123456') |
115 | 106 |
|
116 | 107 |
|
117 | 108 | class TestMyprint: |
@@ -201,27 +192,28 @@ def test_main_silent_mode(self, mock_print, mock_compat, mock_lxc, mock_vz): |
201 | 192 | mock_print.assert_not_called() |
202 | 193 |
|
203 | 194 | @patch('sys.argv', ['kc-compat.py', '--report']) |
204 | | - @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') |
205 | 195 | @patch.object(kc_compat, 'get_distro_info', return_value='centos') |
206 | 196 | @patch.object(kc_compat, 'inside_vz_container', return_value=False) |
207 | 197 | @patch.object(kc_compat, 'inside_lxc_container', return_value=False) |
208 | 198 | @patch.object(kc_compat, 'is_compat', return_value=True) |
209 | | - @patch('builtins.open', new_callable=mock_open, read_data='Linux version 5.4.0-test') |
| 199 | + @patch('builtins.open', new_callable=mock_open, read_data=b'Linux version 5.4.0-test') |
210 | 200 | @patch('builtins.print') |
211 | | - def test_main_report_mode(self, mock_print, mock_file, mock_compat, mock_lxc, mock_vz, mock_distro, mock_hash): |
| 201 | + def test_main_report_mode(self, mock_print, mock_file, mock_compat, mock_lxc, mock_vz, mock_distro): |
212 | 202 | result = kc_compat.main() |
213 | 203 | assert result == 0 |
214 | 204 | # Check that report header and information are printed, followed by COMPATIBLE |
215 | | - expected_calls = [ |
216 | | - (("=== KernelCare Compatibility Report ===",),), |
217 | | - (("Kernel Hash: abcdef123456",),), |
218 | | - (("Distribution: centos",),), |
219 | | - (("Version: Not available",),), |
220 | | - (("Kernel: Linux version 5.4.0-test",),), |
221 | | - (("=====================================",),), |
222 | | - (("COMPATIBLE",),) |
223 | | - ] |
224 | | - mock_print.assert_has_calls(expected_calls) |
| 205 | + # We need to check the actual calls made, not exact matches |
| 206 | + calls = mock_print.call_args_list |
| 207 | + assert len(calls) >= 7 # At least 7 print calls |
| 208 | + |
| 209 | + # Check specific calls |
| 210 | + assert calls[0].args[0] == "=== KernelCare Compatibility Report ===" |
| 211 | + assert calls[1].args[0].startswith("Kernel Hash: ") |
| 212 | + assert calls[2].args[0] == "Distribution: centos" |
| 213 | + assert calls[3].args[0] == "Version: Not available" |
| 214 | + assert calls[4].args[0] == "Kernel: Linux version 5.4.0-test" |
| 215 | + assert calls[5].args[0] == "=====================================" |
| 216 | + assert calls[6].args[0] == "COMPATIBLE" |
225 | 217 |
|
226 | 218 |
|
227 | 219 | @patch('sys.argv', ['kc-compat.py']) |
|
0 commit comments