|
| 1 | +"""Test OS Version evaluation.""" |
| 2 | + |
| 3 | +from unittest.mock import PropertyMock, patch |
| 4 | + |
| 5 | +from awesomeversion import AwesomeVersion |
| 6 | +import pytest |
| 7 | + |
| 8 | +from supervisor.const import CoreState |
| 9 | +from supervisor.coresys import CoreSys |
| 10 | +from supervisor.os.manager import OSManager |
| 11 | +from supervisor.resolution.evaluations.os_version import EvaluateOSVersion |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + "current,latest,expected", |
| 16 | + [ |
| 17 | + ("10.0", "15.0", True), # 5 major behind, should be unsupported |
| 18 | + ("10.0", "14.0", False), # 4 major behind, should be supported |
| 19 | + ("10.2", "11.0", False), # 1 major behind, supported |
| 20 | + ("10.4", "10.5", False), # same major, supported |
| 21 | + ("10.5", "10.5", False), # up to date, supported |
| 22 | + ("10.5", "10.6", False), # same major, supported |
| 23 | + ("10.0", "13.3", False), # 3 major behind, supported |
| 24 | + (None, "15.0", False), # No current version info, check skipped |
| 25 | + ("2.0", None, False), # No latest version info, check skipped |
| 26 | + ( |
| 27 | + "9ccda431973acf17e4221850b08f3280b723df8d", |
| 28 | + "15.0", |
| 29 | + False, |
| 30 | + ), # Dev setup running on a commit hash, check skipped |
| 31 | + ], |
| 32 | +) |
| 33 | +@pytest.mark.usefixtures("os_available") |
| 34 | +async def test_os_version_evaluation( |
| 35 | + coresys: CoreSys, current: str | None, latest: str | None, expected: bool |
| 36 | +): |
| 37 | + """Test evaluation logic on versions.""" |
| 38 | + evaluation = EvaluateOSVersion(coresys) |
| 39 | + await coresys.core.set_state(CoreState.RUNNING) |
| 40 | + with ( |
| 41 | + patch.object( |
| 42 | + OSManager, |
| 43 | + "version", |
| 44 | + new=PropertyMock(return_value=current and AwesomeVersion(current)), |
| 45 | + ), |
| 46 | + patch.object( |
| 47 | + OSManager, |
| 48 | + "latest_version", |
| 49 | + new=PropertyMock(return_value=latest and AwesomeVersion(latest)), |
| 50 | + ), |
| 51 | + ): |
| 52 | + assert evaluation.reason not in coresys.resolution.unsupported |
| 53 | + await evaluation() |
| 54 | + assert (evaluation.reason in coresys.resolution.unsupported) is expected |
| 55 | + |
| 56 | + |
| 57 | +async def test_did_run(coresys: CoreSys): |
| 58 | + """Test that the evaluation ran as expected.""" |
| 59 | + evaluation = EvaluateOSVersion(coresys) |
| 60 | + should_run = evaluation.states |
| 61 | + should_not_run = [state for state in CoreState if state not in should_run] |
| 62 | + assert len(should_run) != 0 |
| 63 | + assert len(should_not_run) != 0 |
| 64 | + |
| 65 | + with patch( |
| 66 | + "supervisor.resolution.evaluations.os_version.EvaluateOSVersion.evaluate", |
| 67 | + return_value=None, |
| 68 | + ) as evaluate: |
| 69 | + for state in should_run: |
| 70 | + await coresys.core.set_state(state) |
| 71 | + await evaluation() |
| 72 | + evaluate.assert_called_once() |
| 73 | + evaluate.reset_mock() |
| 74 | + |
| 75 | + for state in should_not_run: |
| 76 | + await coresys.core.set_state(state) |
| 77 | + await evaluation() |
| 78 | + evaluate.assert_not_called() |
| 79 | + evaluate.reset_mock() |
0 commit comments