-
Notifications
You must be signed in to change notification settings - Fork 908
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
handle error when log file is empty #4859
Conversation
Thank you for contributing! Since this is your first PR for cloud-init, please fill out the CLA form and then add your username to And then also be sure to add an appropriate commit message to your PR's |
@a-dubs Already signed CLA. Added username and proposed commit message. |
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.
@AliyevH , thanks for the contribution. While this does solve the problem, adding a sys.exit()
into what is essentially a helper function several calls deep isn't ideal. It doesn't allow for any recovery for any other caller that might call the function.
Instead, can we raise a custom exception here (something like EmptyFileException
), and then catch that exception at the bottom of cloudinit/analyze/__init__.py
and print the proper text there?
@TheRealFalcon Thanks for review!
|
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.
cloudinit/analyze/init.py here we can not catch exception, because this file is not called directly.
Sorry about that. You're right. It's actually called from main.py
.
I still don't love having a sys.exit(1)
here, but doing it a different way would require some more substantial refactors which isn't fair to ask for in this PR. It is ok as-as.
I left one inline comment and can we also get a test? Since we don't already have a unit test file for cloud-init analyze show
, you can create one at tests/unittests/analyze/test_show.py
. It should look something like
from collections import namedtuple
import pytest
from cloudinit.analyze import analyze_show
@pytest.fixture
def mock_io(tmp_path):
"""Mock args for configure_io function"""
infile = tmp_path / "infile"
outfile = tmp_path / "outfile"
return namedtuple("MockIO", ["infile", "outfile"])(infile, outfile)
class TestAnalyzeShow:
"""Test analyze_show (and/or helpers) in cloudinit/analyze/__init__.py"""
def test_empty_logfile(self, mock_io, capsys):
"""Test analyze_show with an empty logfile"""
mock_io.infile.write_text("")
with pytest.raises(SystemExit):
analyze_show("dontcare", mock_io)
assert capsys.readouterr().err == f"Empty file {mock_io.infile}\n"
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.
LGTM! Thanks for the contribution.
Thank you for guiding me ! |
Proposed Commit Message
Additional Context
Test Steps
Checklist
Merge type