|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Advanced Micro Devices, Inc. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +############################################################################### |
| 26 | +from unittest.mock import MagicMock |
| 27 | + |
| 28 | +import pytest |
| 29 | + |
| 30 | +from nodescraper.enums import EventPriority, ExecutionStatus, OSFamily |
| 31 | +from nodescraper.enums.systeminteraction import SystemInteractionLevel |
| 32 | +from nodescraper.models import TaskResult |
| 33 | +from nodescraper.plugins.inband.nvme.nvme_collector import NvmeCollector |
| 34 | +from nodescraper.plugins.inband.nvme.nvmedata import NvmeDataModel |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def collector(system_info, conn_mock): |
| 39 | + c = NvmeCollector( |
| 40 | + system_info=system_info, |
| 41 | + system_interaction_level=SystemInteractionLevel.PASSIVE, |
| 42 | + connection=conn_mock, |
| 43 | + ) |
| 44 | + c._log_event = MagicMock() |
| 45 | + c._run_sut_cmd = MagicMock() |
| 46 | + c.result = TaskResult() |
| 47 | + return c |
| 48 | + |
| 49 | + |
| 50 | +def test_skips_on_windows(collector): |
| 51 | + collector.system_info = MagicMock(os_family=OSFamily.WINDOWS) |
| 52 | + result, data = collector.collect_data() |
| 53 | + |
| 54 | + assert result.status == ExecutionStatus.NOT_RAN |
| 55 | + assert data is None |
| 56 | + collector._log_event.assert_called_once() |
| 57 | + assert "Windows" in collector._log_event.call_args.kwargs["description"] |
| 58 | + |
| 59 | + |
| 60 | +def test_successful_collection(collector): |
| 61 | + collector.system_info = MagicMock(os_family=OSFamily.LINUX) |
| 62 | + |
| 63 | + collector._run_sut_cmd.return_value = MagicMock(exit_code=0, stdout="output") |
| 64 | + |
| 65 | + result, data = collector.collect_data() |
| 66 | + |
| 67 | + assert result.status == ExecutionStatus.OK |
| 68 | + assert result.message == "NVMe data successfully collected" |
| 69 | + assert isinstance(data, NvmeDataModel) |
| 70 | + assert collector._run_sut_cmd.call_count == 7 |
| 71 | + assert any( |
| 72 | + "Collected NVMe data" in call.kwargs["description"] |
| 73 | + for call in collector._log_event.call_args_list |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def test_partial_failures(collector): |
| 78 | + collector.system_info = MagicMock(os_family=OSFamily.LINUX) |
| 79 | + |
| 80 | + def fake_cmd(cmd, sudo): |
| 81 | + return MagicMock(exit_code=0 if "smart-log" in cmd else 1, stdout="out") |
| 82 | + |
| 83 | + collector._run_sut_cmd.side_effect = fake_cmd |
| 84 | + |
| 85 | + result, data = collector.collect_data() |
| 86 | + |
| 87 | + assert result.status in {ExecutionStatus.OK, ExecutionStatus.ERROR} |
| 88 | + assert collector._log_event.call_count >= 1 |
| 89 | + |
| 90 | + |
| 91 | +def test_no_data_collected(collector): |
| 92 | + collector.system_info = MagicMock(os_family=OSFamily.LINUX) |
| 93 | + |
| 94 | + collector._run_sut_cmd.return_value = MagicMock(exit_code=1, stdout="") |
| 95 | + |
| 96 | + result, data = collector.collect_data() |
| 97 | + |
| 98 | + assert result.status == ExecutionStatus.ERROR |
| 99 | + assert data is None |
| 100 | + assert "No NVMe data collected" in result.message |
| 101 | + assert any( |
| 102 | + call.kwargs["priority"] == EventPriority.CRITICAL |
| 103 | + for call in collector._log_event.call_args_list |
| 104 | + ) |
0 commit comments