Skip to content

Commit

Permalink
feat: handle error when log file is empty (#4859)
Browse files Browse the repository at this point in the history
Fixes GH-4686
  • Loading branch information
AliyevH authored and blackboxsw committed Feb 15, 2024
1 parent a39ef2d commit b4fb02c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cloudinit/analyze/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import json
import os
import sys
import time

from cloudinit import subp, util
Expand Down Expand Up @@ -370,6 +371,9 @@ def load_events_infile(infile):
:return: json version of logfile, raw file
"""
data = infile.read()
if not data.strip():
sys.stderr.write("Empty file %s\n" % infile.name)
sys.exit(1)
try:
return json.loads(data), data
except ValueError:
Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/analyze/test_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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"
1 change: 1 addition & 0 deletions tools/.github-cla-signers
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ajmyyra
akutz
AlexBaranowski
AlexSv04047
AliyevH
Aman306
andgein
andrew-lee-metaswitch
Expand Down

0 comments on commit b4fb02c

Please sign in to comment.