Skip to content

Commit

Permalink
Merge pull request #39 from themaxbelov/error-handling
Browse files Browse the repository at this point in the history
Improve error handling for hub config issues
  • Loading branch information
ToxicWar committed Dec 12, 2017
2 parents 4217a68 + 4c79822 commit 8bc6d83
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
22 changes: 18 additions & 4 deletions apsconnectcli/apsconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,25 @@ def _download_file(url, target=None):


def _get_cfg():
cfg = json.load(open(CFG_FILE_PATH))
if not cfg:
print("Run init command.")
try:
with open(CFG_FILE_PATH) as f:
cfg = json.load(f)
except IOError as e:
if e.errno == 2:
print("Could not find connected hub data. "
"Please run the init-hub command to connect Odin Automation hub.")
else:
print("Could not open configuration file:\n{}".format(e))
sys.exit(1)
except ValueError:
print("Could not parse the configuration file, please re-run "
"the init-hub command to regenerate the configuration.")
sys.exit(1)
return cfg
except Exception as e:
print("Failed to read connected hub configuration. Error message:\n{}".format(e))
sys.exit(1)
else:
return cfg


def _to_bytes(raw_str):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
setup(
name='apsconnectcli',
author='Ingram Micro',
version='1.7.18',
version='1.7.19',
keywords='aps apsconnect connector automation',
extras_require={
':python_version<="2.7"': ['backports.tempfile==1.0']},
Expand Down
73 changes: 73 additions & 0 deletions tests/test_apsconnect_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from apsconnectcli.apsconnect import (
KUBE_FILE_PATH,
_osaapi_raise_for_status,
_get_cfg,
_get_k8s_api_client,
_get_properties,
_get_resclass_name,
Expand Down Expand Up @@ -635,3 +636,75 @@ def extract(filename, path):

package_info = _extract_files('zz', 'zz')
self.assertFalse(package_info.user_service)


class GetCfgTest(TestCase):
def test_file_not_found(self):
with patch(_BUILTINS_OPEN) as open_mock, \
patch(_BUILTINS_PRINT) as print_mock, \
patch('apsconnectcli.apsconnect.sys') as sys_mock:
err = IOError()
err.errno = 2
open_mock.side_effect = err
_get_cfg()

self.assertTrue(print_mock.called)
self.assertTrue("Could not find connected hub data."
in print_mock.call_args[0][0])
sys_mock.exit.assert_called_with(1)

def test_file_other_ioerr(self):
with patch(_BUILTINS_OPEN) as open_mock, \
patch(_BUILTINS_PRINT) as print_mock, \
patch('apsconnectcli.apsconnect.sys') as sys_mock:
err = IOError("Error message text")
err.errno = 13
open_mock.side_effect = err
_get_cfg()

self.assertTrue(print_mock.called)
self.assertTrue("Could not open configuration file"
in print_mock.call_args[0][0])
self.assertTrue("Error message text"
in print_mock.call_args[0][0])
sys_mock.exit.assert_called_with(1)

def test_file_unreadable(self):
with patch(_BUILTINS_OPEN), \
patch(_BUILTINS_PRINT) as print_mock, \
patch('apsconnectcli.apsconnect.json') as json_mock, \
patch('apsconnectcli.apsconnect.sys') as sys_mock:
json_mock.load.side_effect = ValueError()

_get_cfg()

self.assertTrue(print_mock.called)
self.assertTrue("Could not parse the configuration file"
in print_mock.call_args[0][0])
sys_mock.exit.assert_called_with(1)

def test_unexpected_error(self):
with patch(_BUILTINS_OPEN), \
patch(_BUILTINS_PRINT) as print_mock, \
patch('apsconnectcli.apsconnect.json') as json_mock, \
patch('apsconnectcli.apsconnect.sys') as sys_mock:
json_mock.load.side_effect = Exception("All is lost")

_get_cfg()

self.assertTrue(print_mock.called)
self.assertTrue("All is lost" in print_mock.call_args[0][0])
sys_mock.exit.assert_called_with(1)

def test_ok(self):
with patch(_BUILTINS_OPEN), \
patch(_BUILTINS_PRINT) as print_mock, \
patch('apsconnectcli.apsconnect.json') as json_mock, \
patch('apsconnectcli.apsconnect.sys') as sys_mock:
json_mock.load.return_value = "Config data"

config = _get_cfg()

self.assertEqual(config, "Config data")
self.assertFalse(print_mock.called)
sys_mock.exit.assert_not_called()

0 comments on commit 8bc6d83

Please sign in to comment.