Skip to content

Commit

Permalink
Bugfix/fix api exception handling (#140)
Browse files Browse the repository at this point in the history
* Fix module path to ApiException
* Add a test case for handling ApiException
  • Loading branch information
m-yakovenko authored May 11, 2021
1 parent 0c5cdae commit b484e46
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion k8s_handle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _handler_provision(command, resources, priority_evaluator, use_kubeconfig, s
for resource in resources:
deprecation_checker.run(resource)
available_checker.run(resource)
except client.api_client.ApiException:
except client.exceptions.ApiException:
log.warning("Error while getting API version, deprecation check will be skipped.")

if command == COMMAND_DIFF:
Expand Down
38 changes: 38 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import unittest
from unittest.mock import patch

from k8s_handle import settings
from k8s_handle import handler_deploy
from kubernetes import client


class TestDeployHandler(unittest.TestCase):

def setUp(self):
settings.CONFIG_FILE = 'tests/fixtures/config_with_env_vars.yaml'
settings.TEMPLATES_DIR = 'templates/tests'
os.environ['K8S_CONFIG_DIR'] = '/tmp/kube/'
os.environ['SECTION1'] = 'not found'
os.environ['SECTION'] = 'section-1'

@patch('k8s_handle.templating.Renderer._generate_file')
@patch('kubernetes.client.api.version_api.VersionApi.get_code_with_http_info')
@patch('k8s_handle.k8s.provisioner.Provisioner.run')
def test_api_exception_handling(
self,
mocked_provisioner_run,
mocked_client_version_api_get_code,
mocked_generate_file
):
mocked_client_version_api_get_code.side_effect = client.exceptions.ApiException(
'Max retries exceeded with url: /version/'
)

configs = {
'section': os.environ['SECTION'],
'config': settings.CONFIG_FILE,
"use_kubeconfig": True
}
# client.exceptions.ApiException should be handled
handler_deploy(configs)

0 comments on commit b484e46

Please sign in to comment.