Skip to content

Commit

Permalink
New tests sys exits tests, removed exit from constants.
Browse files Browse the repository at this point in the history
Resolves: rhbz#2091919
  • Loading branch information
ahitacat committed Jun 20, 2023
1 parent 28c9f6e commit b0c87f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/insights_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ def run_phase(phase, client, validated_eggs):
# 100 and 101 are unrecoverable, like post-unregistration, or
# a machine not being registered yet, or simply a 'dump & die'
# CLI option
# * 100: Success, exit
# * 101: Failure, exit
sys.exit(process.returncode % 100)
# All attemps to run phase have failed
sys.exit(1)
Expand Down Expand Up @@ -197,7 +199,7 @@ def _main():
config = InsightsConfig(_print_errors=True).load_all()
except ValueError as e:
sys.stderr.write('ERROR: ' + str(e) + '\n')
sys.exit(constants.sig_kill_bad)
sys.exit('Unable to load Insights Config')

if config["version"]:
from insights_client.constants import InsightsConstants as constants
Expand Down
2 changes: 1 addition & 1 deletion src/insights_client/tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist_check_SCRIPTS = test_commands.py
dist_check_SCRIPTS = test_commands.py test_client.py
nodist_check_SCRIPTS = test_sed.py


Expand Down
38 changes: 38 additions & 0 deletions src/insights_client/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from sys import stderr, stdout
from unittest import mock
from unittest.mock import patch
import insights_client
import pytest


# Test config load error
@patch('os.getuid', return_value=0)
@patch('insights.client.InsightsConfig.load_all', side_effect = ValueError('mocked error'))
def test_load_config_error(os_uid, insightsConfig):
with pytest.raises(SystemExit) as sys_exit:
insights_client._main()
assert sys_exit.value.code != 0


# test keyboardinterrupt handler
@patch('os.getuid', return_value = 0)
@patch('insights.client.InsightsConfig.load_all', side_effect = KeyboardInterrupt)
def test_keyboard_interrupt(os_uid, client):
with pytest.raises(SystemExit) as sys_exit:
insights_client._main()
assert sys_exit.value.code != 0

#check run phase error 100 handler
@patch('os.getuid', return_value = 0)
@patch('insights.client.phase.v1.get_phases')
@patch('insights.client.InsightsClient')
@patch('insights_client.sorted_eggs', return_value = "/var/lib/insights/newest.egg")
@patch('insights_client.subprocess.Popen')
@patch('insights_client.enumerate', return_value = [("1", "egg")])
@patch('insights_client.os.path.isfile', return_value = True)
def test_phase_error_100(isfile, enumerate, mock_subprocess, sorted_eggs, client, p, os_uid):
with pytest.raises(SystemExit) as sys_exit:
mock_subprocess.return_value.returncode= 100
mock_subprocess.return_value.communicate.return_value = ('output', 'error')
insights_client.run_phase(p, client, sorted_eggs)
assert sys_exit.value.code == 0

0 comments on commit b0c87f6

Please sign in to comment.