@@ -59,134 +59,13 @@ def _run_sut_cmd(cmd, *args, **kwargs):
5959 return c
6060
6161
62- def test_get_journals_happy_path (monkeypatch , system_info , conn_mock ):
63- paths = [
64- "/var/log/journal/m1/system.journal" ,
65- "/var/log/journal/m1/[email protected] " ,
66- "/var/log/journal/m2/system.journal" ,
67- ]
68- ls_out = "\n " .join (paths ) + "\n "
69-
70- def run_map (cmd , ** kwargs ):
71- if cmd .startswith ("ls -1 /var/log/journal" ):
72- return DummyRes (command = cmd , stdout = ls_out , exit_code = 0 )
73-
74- if cmd .startswith ("journalctl " ) and "--file=" in cmd :
75- if paths [0 ] in cmd :
76- return DummyRes (cmd , stdout = '{"MESSAGE":"a"}\n ' , exit_code = 0 )
77- if paths [1 ] in cmd :
78- return DummyRes (cmd , stdout = b'{"MESSAGE":"b"}\n ' , exit_code = 0 )
79- if paths [2 ] in cmd :
80- return DummyRes (cmd , stdout = '{"MESSAGE":"c"}\n ' , exit_code = 0 )
81-
82- return DummyRes (command = cmd , stdout = "" , exit_code = 1 , stderr = "unexpected" )
83-
84- c = get_collector (monkeypatch , run_map , system_info , conn_mock )
85-
86- collected = c ._get_journals ()
87- assert len (collected ) == 3
88-
89- expected_names = {
90- "journalctl__var__log__journal__m1__system.journal.json" ,
91- "journalctl__var__log__journal__m1__system@0000000000000001-0000000000000002.journal.json" ,
92- "journalctl__var__log__journal__m2__system.journal.json" ,
93- }
94- names = {a .filename for a in c .result .artifacts }
95- assert names == expected_names
96-
97- contents = {a .filename : a .contents for a in c .result .artifacts }
98- assert (
99- contents ["journalctl__var__log__journal__m1__system.journal.json" ].strip ()
100- == '{"MESSAGE":"a"}'
101- )
102- assert (
103- contents [
104- "journalctl__var__log__journal__m1__system@0000000000000001-0000000000000002.journal.json"
105- ].strip ()
106- == '{"MESSAGE":"b"}'
107- )
108- assert (
109- contents ["journalctl__var__log__journal__m2__system.journal.json" ].strip ()
110- == '{"MESSAGE":"c"}'
111- )
112-
113- assert any (
114- evt .get ("description" ) == "Collected journal logs."
115- and getattr (evt .get ("priority" ), "name" , str (evt .get ("priority" ))) == "INFO"
116- for evt in c ._events
117- )
118- assert c .result .message == "journalctl logs collected"
119-
120-
121- def test_get_journals_no_files (monkeypatch , system_info , conn_mock ):
122- def run_map (cmd , ** kwargs ):
123- if cmd .startswith ("ls -1 /var/log/journal" ):
124- return DummyRes (command = cmd , stdout = "" , exit_code = 0 )
125- return DummyRes (command = cmd , stdout = "" , exit_code = 1 )
126-
127- c = get_collector (monkeypatch , run_map , system_info , conn_mock )
128-
129- collected = c ._get_journals ()
130- assert collected == []
131- assert c .result .artifacts == []
132-
133- assert any (
134- evt .get ("description" , "" ).startswith ("No /var/log/journal files found" )
135- and getattr (evt .get ("priority" ), "name" , str (evt .get ("priority" ))) == "WARNING"
136- for evt in c ._events
137- )
138-
139-
140- def test_get_journals_partial_failure (monkeypatch , system_info , conn_mock ):
141- ok_path = "/var/log/journal/m1/system.journal"
142- bad_path = "/var/log/journal/m1/[email protected] " 143- ls_out = ok_path + "\n " + bad_path + "\n "
144-
145- def run_map (cmd , ** kwargs ):
146- if cmd .startswith ("ls -1 /var/log/journal" ):
147- return DummyRes (command = cmd , stdout = ls_out , exit_code = 0 )
148-
149- if cmd .startswith ("journalctl " ) and "--file=" in cmd :
150- if ok_path in cmd :
151- return DummyRes (cmd , stdout = '{"MESSAGE":"ok"}\n ' , exit_code = 0 )
152- if bad_path in cmd :
153- return DummyRes (cmd , stdout = "" , exit_code = 1 , stderr = "cannot read" )
154-
155- return DummyRes (command = cmd , stdout = "" , exit_code = 1 )
156-
157- c = get_collector (monkeypatch , run_map , system_info , conn_mock )
158-
159- collected = c ._get_journals ()
160- assert collected == ["journalctl__var__log__journal__m1__system.journal.json" ]
161- assert [a .filename for a in c .result .artifacts ] == [
162- "journalctl__var__log__journal__m1__system.journal.json"
163- ]
164-
165- assert any (
166- evt .get ("description" ) == "Some journal files could not be read with journalctl."
167- and getattr (evt .get ("priority" ), "name" , str (evt .get ("priority" ))) == "WARNING"
168- for evt in c ._events
169- )
170-
171-
17262def test_collect_data_integration (monkeypatch , system_info , conn_mock ):
173- dummy_path = "/var/log/journal/m1/system.journal"
174- ls_out = dummy_path + "\n "
175-
17663 def run_map (cmd , ** kwargs ):
177- if cmd .startswith ("ls -1 /var/log/journal" ):
178- return DummyRes (command = cmd , stdout = ls_out , exit_code = 0 )
179- if cmd .startswith ("journalctl " ) and "--file=" in cmd and dummy_path in cmd :
180- return DummyRes (command = cmd , stdout = '{"MESSAGE":"hello"}\n ' , exit_code = 0 )
181- return DummyRes (command = cmd , stdout = "" , exit_code = 1 )
64+ return DummyRes (command = cmd , stdout = '{"MESSAGE":"hello"}\n ' , exit_code = 0 )
18265
18366 c = get_collector (monkeypatch , run_map , system_info , conn_mock )
18467
18568 result , data = c .collect_data ()
18669 assert isinstance (data , JournalData )
18770
188- expected_name = "journalctl__var__log__journal__m1__system.journal.json"
189- assert data .journal_logs == [expected_name ]
190- assert c .result .message == "journalctl logs collected"
191-
192- assert [a .filename for a in c .result .artifacts ] == [expected_name ]
71+ assert data .journal_log == '{"MESSAGE":"hello"}\n '
0 commit comments