Skip to content

Commit 19dea9c

Browse files
Merge pull request #91 from amd/alex_dmesg
DmesgPlugin update: skip dmesg.log
2 parents 0541ca5 + 2d24b7c commit 19dea9c

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

nodescraper/plugins/inband/dmesg/collector_args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ class DmesgCollectorArgs(CollectorArgs):
3636

3737
collect_rotated_logs: bool = False
3838
skip_sudo: bool = False
39+
log_dmesg_data: bool = True

nodescraper/plugins/inband/dmesg/dmesg_collector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def collect_data(
155155
self._collect_dmesg_rotations()
156156

157157
if dmesg_content:
158-
dmesg_data = DmesgData(dmesg_content=dmesg_content)
158+
dmesg_data = DmesgData(
159+
dmesg_content=dmesg_content, skip_log_file=not args.log_dmesg_data
160+
)
159161
self.result.message = "Dmesg data collected"
160162
return self.result, dmesg_data
161163

nodescraper/plugins/inband/dmesg/dmesgdata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DmesgData(DataModel):
3535
"""Data model for in band dmesg log"""
3636

3737
dmesg_content: str
38+
skip_log_file: bool = False
3839

3940
@classmethod
4041
def get_new_dmesg_lines(cls, current_dmesg: str, new_dmesg: str) -> str:
@@ -83,6 +84,8 @@ def log_model(self, log_path: str):
8384
Args:
8485
log_path (str): log path
8586
"""
87+
if self.skip_log_file:
88+
return
8689
log_name = os.path.join(log_path, get_unique_filename(log_path, "dmesg.log"))
8790
with open(log_name, "w", encoding="utf-8") as log_file:
8891
log_file.write(self.dmesg_content)

test/functional/test_plugin_configs.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,54 @@ def test_all_plugin_config_files_exist(plugin_config_files):
283283
config = json.load(f)
284284
assert "plugins" in config
285285
assert plugin_name in config["plugins"]
286+
287+
288+
def test_dmesg_plugin_log_dmesg_data_false(run_cli_command, tmp_path):
289+
"""Test DmesgPlugin with log_dmesg_data=false doesn't write dmesg.log."""
290+
config = {
291+
"name": "DmesgNoLogConfig",
292+
"desc": "DmesgPlugin config with log_dmesg_data disabled",
293+
"global_args": {},
294+
"plugins": {"DmesgPlugin": {"collection_args": {"log_dmesg_data": False}}},
295+
"result_collators": {},
296+
}
297+
config_file = tmp_path / "dmesg_no_log_config.json"
298+
config_file.write_text(json.dumps(config, indent=2))
299+
300+
log_path = str(tmp_path / "logs_dmesg_no_log")
301+
result = run_cli_command(
302+
["--log-path", log_path, "--plugin-configs", str(config_file)], check=False
303+
)
304+
305+
assert result.returncode in [0, 1, 2]
306+
307+
dmesg_plugin_dir = Path(log_path) / "dmesg_plugin" / "dmesg_collector"
308+
if dmesg_plugin_dir.exists():
309+
dmesg_log_files = list(dmesg_plugin_dir.glob("dmesg*.log"))
310+
assert (
311+
len(dmesg_log_files) == 0
312+
), f"Found dmesg log files when log_dmesg_data=False: {dmesg_log_files}"
313+
314+
315+
def test_dmesg_plugin_log_dmesg_data_true(run_cli_command, tmp_path):
316+
"""Test DmesgPlugin with log_dmesg_data=true writes dmesg.log."""
317+
config = {
318+
"name": "DmesgWithLogConfig",
319+
"desc": "DmesgPlugin config with log_dmesg_data enabled",
320+
"global_args": {},
321+
"plugins": {"DmesgPlugin": {"collection_args": {"log_dmesg_data": True}}},
322+
"result_collators": {},
323+
}
324+
config_file = tmp_path / "dmesg_with_log_config.json"
325+
config_file.write_text(json.dumps(config, indent=2))
326+
327+
log_path = str(tmp_path / "logs_dmesg_with_log")
328+
result = run_cli_command(
329+
["--log-path", log_path, "--plugin-configs", str(config_file)], check=False
330+
)
331+
332+
if result.returncode in [0, 1]:
333+
dmesg_plugin_dir = Path(log_path) / "dmesg_plugin" / "dmesg_collector"
334+
if dmesg_plugin_dir.exists():
335+
dmesg_log_files = list(dmesg_plugin_dir.glob("dmesg*.log"))
336+
assert len(dmesg_log_files) > 0, "Expected dmesg.log file when log_dmesg_data=True"

test/unit/plugin/test_dmesg_collector.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from nodescraper.enums.systeminteraction import SystemInteractionLevel
3333
from nodescraper.interfaces.task import SystemCompatibilityError
3434
from nodescraper.models.systeminfo import OSFamily
35+
from nodescraper.plugins.inband.dmesg.collector_args import DmesgCollectorArgs
3536
from nodescraper.plugins.inband.dmesg.dmesg_collector import DmesgCollector
3637
from nodescraper.plugins.inband.dmesg.dmesgdata import DmesgData
3738

@@ -276,3 +277,27 @@ def run_map(cmd, **kwargs):
276277

277278
assert isinstance(data, DmesgData)
278279
assert data.dmesg_content == "DMESG OUTPUT\n"
280+
281+
282+
def test_collect_data_with_args(conn_mock, system_info):
283+
"""Test collect_data accepts DmesgCollectorArgs"""
284+
dmesg = "2023-06-01T01:00:00,685236-05:00 test message1\n"
285+
conn_mock.run_command.return_value = CommandArtifact(
286+
exit_code=0,
287+
stdout=dmesg,
288+
stderr="",
289+
command="dmesg --time-format iso",
290+
)
291+
292+
collector = DmesgCollector(
293+
system_info=system_info,
294+
system_interaction_level=SystemInteractionLevel.INTERACTIVE,
295+
connection=conn_mock,
296+
)
297+
298+
args = DmesgCollectorArgs(log_dmesg_data=False)
299+
res, data = collector.collect_data(args=args)
300+
301+
assert res.status == ExecutionStatus.OK
302+
assert data is not None
303+
assert data.dmesg_content == dmesg

0 commit comments

Comments
 (0)