Skip to content

Commit b484e46

Browse files
authored
Bugfix/fix api exception handling (#140)
* Fix module path to ApiException * Add a test case for handling ApiException
1 parent 0c5cdae commit b484e46

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

k8s_handle/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _handler_provision(command, resources, priority_evaluator, use_kubeconfig, s
116116
for resource in resources:
117117
deprecation_checker.run(resource)
118118
available_checker.run(resource)
119-
except client.api_client.ApiException:
119+
except client.exceptions.ApiException:
120120
log.warning("Error while getting API version, deprecation check will be skipped.")
121121

122122
if command == COMMAND_DIFF:

tests/test_handlers.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import unittest
3+
from unittest.mock import patch
4+
5+
from k8s_handle import settings
6+
from k8s_handle import handler_deploy
7+
from kubernetes import client
8+
9+
10+
class TestDeployHandler(unittest.TestCase):
11+
12+
def setUp(self):
13+
settings.CONFIG_FILE = 'tests/fixtures/config_with_env_vars.yaml'
14+
settings.TEMPLATES_DIR = 'templates/tests'
15+
os.environ['K8S_CONFIG_DIR'] = '/tmp/kube/'
16+
os.environ['SECTION1'] = 'not found'
17+
os.environ['SECTION'] = 'section-1'
18+
19+
@patch('k8s_handle.templating.Renderer._generate_file')
20+
@patch('kubernetes.client.api.version_api.VersionApi.get_code_with_http_info')
21+
@patch('k8s_handle.k8s.provisioner.Provisioner.run')
22+
def test_api_exception_handling(
23+
self,
24+
mocked_provisioner_run,
25+
mocked_client_version_api_get_code,
26+
mocked_generate_file
27+
):
28+
mocked_client_version_api_get_code.side_effect = client.exceptions.ApiException(
29+
'Max retries exceeded with url: /version/'
30+
)
31+
32+
configs = {
33+
'section': os.environ['SECTION'],
34+
'config': settings.CONFIG_FILE,
35+
"use_kubeconfig": True
36+
}
37+
# client.exceptions.ApiException should be handled
38+
handler_deploy(configs)

0 commit comments

Comments
 (0)