mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-11-14 21:40:20 +00:00
Add JobManager and Job decorator (#2225)
* Adds condition decorator to block execution that require internet * Fix exsisting tests * Add internet state to network info * Add healthy condition * Add tests * It's all changed * rename
This commit is contained in:
@@ -13,6 +13,7 @@ from supervisor.bootstrap import initialize_coresys
|
||||
from supervisor.coresys import CoreSys
|
||||
from supervisor.dbus.network import NetworkManager
|
||||
from supervisor.docker import DockerAPI
|
||||
from supervisor.host.const import ConnectivityState
|
||||
from supervisor.utils.gdbus import DBus
|
||||
|
||||
from tests.common import exists_fixture, load_fixture, load_json_fixture
|
||||
@@ -136,6 +137,10 @@ async def coresys(loop, docker, network_manager, aiohttp_client) -> CoreSys:
|
||||
# Mock docker
|
||||
coresys_obj._docker = docker
|
||||
|
||||
# Set internet state
|
||||
coresys_obj.supervisor._connectivity = True
|
||||
coresys_obj.host.network._connectivity = ConnectivityState.FULL
|
||||
|
||||
yield coresys_obj
|
||||
|
||||
|
||||
|
||||
41
tests/host/test_connectivity.py
Normal file
41
tests/host/test_connectivity.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""Test supported features."""
|
||||
# pylint: disable=protected-access
|
||||
from unittest.mock import patch
|
||||
|
||||
from supervisor.coresys import CoreSys
|
||||
from supervisor.host.const import ConnectivityState
|
||||
|
||||
|
||||
async def test_connectivity_unknown(coresys: CoreSys):
|
||||
"""Test host unknown connectivity."""
|
||||
with patch("supervisor.utils.gdbus.DBus._send", return_value="[0]"):
|
||||
await coresys.host.network.check_connectivity()
|
||||
assert coresys.host.network.connectivity == ConnectivityState.UNKNOWN
|
||||
|
||||
|
||||
async def test_connectivity_none(coresys: CoreSys):
|
||||
"""Test host none connectivity."""
|
||||
with patch("supervisor.utils.gdbus.DBus._send", return_value="[1]"):
|
||||
await coresys.host.network.check_connectivity()
|
||||
assert coresys.host.network.connectivity == ConnectivityState.NONE
|
||||
|
||||
|
||||
async def test_connectivity_portal(coresys: CoreSys):
|
||||
"""Test host portal connectivity."""
|
||||
with patch("supervisor.utils.gdbus.DBus._send", return_value="[2]"):
|
||||
await coresys.host.network.check_connectivity()
|
||||
assert coresys.host.network.connectivity == ConnectivityState.PORTAL
|
||||
|
||||
|
||||
async def test_connectivity_limited(coresys: CoreSys):
|
||||
"""Test host limited connectivity."""
|
||||
with patch("supervisor.utils.gdbus.DBus._send", return_value="[3]"):
|
||||
await coresys.host.network.check_connectivity()
|
||||
assert coresys.host.network.connectivity == ConnectivityState.LIMITED
|
||||
|
||||
|
||||
async def test_connectivity_full(coresys: CoreSys):
|
||||
"""Test host full connectivity."""
|
||||
with patch("supervisor.utils.gdbus.DBus._send", return_value="[4]"):
|
||||
await coresys.host.network.check_connectivity()
|
||||
assert coresys.host.network.connectivity == ConnectivityState.FULL
|
||||
73
tests/job/test_job_decorator.py
Normal file
73
tests/job/test_job_decorator.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""Test the condition decorators."""
|
||||
# pylint: disable=protected-access,import-error
|
||||
from unittest.mock import patch
|
||||
|
||||
from supervisor.coresys import CoreSys
|
||||
from supervisor.job.decorator import Job, JobCondition
|
||||
|
||||
|
||||
async def test_healthy(coresys: CoreSys):
|
||||
"""Test the healty decorator."""
|
||||
|
||||
class TestClass:
|
||||
"""Test class."""
|
||||
|
||||
def __init__(self, coresys: CoreSys):
|
||||
"""Initialize the test class."""
|
||||
self.coresys = coresys
|
||||
|
||||
@Job(conditions=[JobCondition.HEALTHY])
|
||||
async def execute(self):
|
||||
"""Execute the class method."""
|
||||
return True
|
||||
|
||||
test = TestClass(coresys)
|
||||
assert await test.execute()
|
||||
|
||||
coresys.core.healthy = False
|
||||
assert not await test.execute()
|
||||
|
||||
|
||||
async def test_internet(coresys: CoreSys):
|
||||
"""Test the internet decorator."""
|
||||
|
||||
class TestClass:
|
||||
"""Test class."""
|
||||
|
||||
def __init__(self, coresys: CoreSys):
|
||||
"""Initialize the test class."""
|
||||
self.coresys = coresys
|
||||
|
||||
@Job(conditions=[JobCondition.INTERNET])
|
||||
async def execute(self):
|
||||
"""Execute the class method."""
|
||||
return True
|
||||
|
||||
test = TestClass(coresys)
|
||||
assert await test.execute()
|
||||
|
||||
coresys.supervisor._connectivity = False
|
||||
assert not await test.execute()
|
||||
|
||||
|
||||
async def test_free_space(coresys: CoreSys):
|
||||
"""Test the free_space decorator."""
|
||||
|
||||
class TestClass:
|
||||
"""Test class."""
|
||||
|
||||
def __init__(self, coresys: CoreSys):
|
||||
"""Initialize the test class."""
|
||||
self.coresys = coresys
|
||||
|
||||
@Job(conditions=[JobCondition.FREE_SPACE])
|
||||
async def execute(self):
|
||||
"""Execute the class method."""
|
||||
return True
|
||||
|
||||
test = TestClass(coresys)
|
||||
with patch("shutil.disk_usage", return_value=(42, 42, (1024.0 ** 3))):
|
||||
assert await test.execute()
|
||||
|
||||
with patch("shutil.disk_usage", return_value=(42, 42, (512.0 ** 3))):
|
||||
assert not await test.execute()
|
||||
39
tests/job/test_job_manager.py
Normal file
39
tests/job/test_job_manager.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Test the condition decorators."""
|
||||
# pylint: disable=protected-access,import-error
|
||||
from supervisor.coresys import CoreSys
|
||||
|
||||
TEST_JOB = "test"
|
||||
|
||||
|
||||
async def test_add_job(coresys: CoreSys):
|
||||
"""Test adding jobs."""
|
||||
job = coresys.jobs.get_job(TEST_JOB)
|
||||
|
||||
assert job.name in coresys.jobs.jobs
|
||||
|
||||
|
||||
async def test_remove_job_directly(coresys: CoreSys):
|
||||
"""Test removing jobs from manager."""
|
||||
job = coresys.jobs.get_job(TEST_JOB)
|
||||
|
||||
assert job.name in coresys.jobs.jobs
|
||||
coresys.jobs.remove_job(job)
|
||||
assert job.name not in coresys.jobs.jobs
|
||||
|
||||
|
||||
async def test_remove_job_with_progress(coresys: CoreSys):
|
||||
"""Test removing jobs by setting progress to 100."""
|
||||
job = coresys.jobs.get_job(TEST_JOB)
|
||||
|
||||
assert job.name in coresys.jobs.jobs
|
||||
await job.update(progress=100)
|
||||
assert job.name not in coresys.jobs.jobs
|
||||
|
||||
|
||||
async def test_update_job(coresys: CoreSys):
|
||||
"""Test updating jobs."""
|
||||
job = coresys.jobs.get_job(TEST_JOB)
|
||||
|
||||
await job.update(progress=50, stage="stage")
|
||||
assert job.progress == 50
|
||||
assert job.stage == "stage"
|
||||
Reference in New Issue
Block a user