-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): extract url checker and support multiple api endpoints #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,19 +85,69 @@ def test_is_distro_supported(self): | |
| assert kc_compat.is_distro_supported('debian') == False | ||
|
|
||
|
|
||
| class TestCheckUrl: | ||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_check_url_success(self, mock_urlopen): | ||
| mock_urlopen.return_value = MagicMock() | ||
| assert kc_compat._check_url('http://example.com') == True | ||
|
|
||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_check_url_404(self, mock_urlopen): | ||
| mock_urlopen.side_effect = HTTPError(None, 404, 'Not Found', None, None) | ||
| assert kc_compat._check_url('http://example.com') == False | ||
|
|
||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_check_url_500_raises(self, mock_urlopen): | ||
| mock_urlopen.side_effect = HTTPError(None, 500, 'Server Error', None, None) | ||
| with pytest.raises(HTTPError): | ||
| kc_compat._check_url('http://example.com') | ||
|
|
||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_check_url_url_error_raises(self, mock_urlopen): | ||
| mock_urlopen.side_effect = URLError('Connection refused') | ||
| with pytest.raises(URLError): | ||
| kc_compat._check_url('http://example.com') | ||
|
|
||
|
|
||
| class TestIsCompat: | ||
| BASE = 'http://patches.kernelcare.com/abcdef123456' | ||
|
|
||
| @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') | ||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_is_compat_success(self, mock_urlopen, mock_hash): | ||
| def test_is_compat_version_hit(self, mock_urlopen, mock_hash): | ||
| mock_urlopen.return_value = MagicMock() | ||
| assert kc_compat.is_compat() == True | ||
| mock_urlopen.assert_called_once_with('http://patches.kernelcare.com/abcdef123456/version') | ||
| mock_urlopen.assert_called_once_with(self.BASE + '/version') | ||
|
|
||
| @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') | ||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_is_compat_fallback_to_v2(self, mock_urlopen, mock_hash): | ||
| mock_urlopen.side_effect = [ | ||
| HTTPError(None, 404, 'Not Found', None, None), | ||
| MagicMock(), | ||
| ] | ||
| assert kc_compat.is_compat() == True | ||
| assert mock_urlopen.call_count == 2 | ||
| mock_urlopen.assert_called_with(self.BASE + '/latest.v2') | ||
|
|
||
|
Comment on lines
+124
to
+132
|
||
| @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') | ||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_is_compat_fallback_to_v3(self, mock_urlopen, mock_hash): | ||
| mock_urlopen.side_effect = [ | ||
| HTTPError(None, 404, 'Not Found', None, None), | ||
| HTTPError(None, 404, 'Not Found', None, None), | ||
| MagicMock(), | ||
| ] | ||
| assert kc_compat.is_compat() == True | ||
| assert mock_urlopen.call_count == 3 | ||
| mock_urlopen.assert_called_with(self.BASE + '/latest.v3') | ||
|
Comment on lines
+135
to
+143
|
||
|
|
||
| @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') | ||
| @patch.object(kc_compat, 'urlopen') | ||
| def test_is_compat_404_error_returns_false(self, mock_urlopen, mock_hash): | ||
| def test_is_compat_all_404_returns_false(self, mock_urlopen, mock_hash): | ||
| mock_urlopen.side_effect = HTTPError(None, 404, 'Not Found', None, None) | ||
| assert kc_compat.is_compat() == False | ||
| assert mock_urlopen.call_count == 3 | ||
|
Comment on lines
145
to
+150
|
||
|
|
||
| @patch.object(kc_compat, 'get_kernel_hash', return_value='abcdef123456') | ||
| @patch.object(kc_compat, 'urlopen') | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_check_url() calls urlopen(url) but never closes the returned response object and uses the default (potentially unbounded) timeout. This can leak sockets/file descriptors and may cause the CLI to hang indefinitely on slow/stalled connections. Consider opening the response with an explicit timeout and ensuring it is closed (e.g., close in a finally block or use contextlib.closing).