File: test_jobs.py

package info (click to toggle)
python-aiohasupervisor 0.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: python: 4,666; sh: 37; makefile: 3
file content (156 lines) | stat: -rw-r--r-- 5,731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Test jobs supervisor client."""

from datetime import datetime
from uuid import UUID

from aioresponses import aioresponses
from yarl import URL

from aiohasupervisor import SupervisorClient
from aiohasupervisor.models import JobCondition, JobsOptions

from . import load_fixture
from .const import SUPERVISOR_URL


async def test_jobs_info(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs info API."""
    responses.get(
        f"{SUPERVISOR_URL}/jobs/info", status=200, body=load_fixture("jobs_info.json")
    )
    info = await supervisor_client.jobs.info()
    assert info.ignore_conditions == [JobCondition.FREE_SPACE]

    assert info.jobs[0].name == "backup_manager_partial_backup"
    assert info.jobs[0].reference == "89cafa67"
    assert info.jobs[0].uuid.hex == "2febe59311f94d6ba36f6f9f73357ca8"
    assert info.jobs[0].progress == 0
    assert info.jobs[0].stage == "finishing_file"
    assert info.jobs[0].done is True
    assert info.jobs[0].errors == []
    assert info.jobs[0].created == datetime.fromisoformat(
        "2025-01-30T20:55:12.859349+00:00"
    )
    assert info.jobs[0].child_jobs[0].name == "backup_store_folders"
    assert info.jobs[0].child_jobs[0].child_jobs[0].name == "backup_folder_save"
    assert info.jobs[0].child_jobs[0].child_jobs[0].reference == "ssl"
    assert info.jobs[0].child_jobs[0].child_jobs[0].child_jobs == []

    assert info.jobs[1].name == "backup_manager_partial_restore"
    assert info.jobs[1].reference == "cfddca18"
    assert info.jobs[1].errors[0].type == "BackupInvalidError"
    assert info.jobs[1].errors[0].message == "Invalid password for backup cfddca18"
    assert info.jobs[1].errors[0].stage == "restore_backup"


async def test_jobs_set_options(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs set options API."""
    responses.post(f"{SUPERVISOR_URL}/jobs/options", status=200)
    assert (
        await supervisor_client.jobs.set_options(
            JobsOptions(ignore_conditions=[JobCondition.FREE_SPACE])
        )
        is None
    )
    assert responses.requests.keys() == {
        ("POST", URL(f"{SUPERVISOR_URL}/jobs/options"))
    }


async def test_jobs_reset(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs reset API."""
    responses.post(f"{SUPERVISOR_URL}/jobs/reset", status=200)
    assert await supervisor_client.jobs.reset() is None
    assert responses.requests.keys() == {("POST", URL(f"{SUPERVISOR_URL}/jobs/reset"))}


async def test_jobs_get_job(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs get job API."""
    responses.get(
        f"{SUPERVISOR_URL}/jobs/2febe59311f94d6ba36f6f9f73357ca8",
        status=200,
        body=load_fixture("jobs_get_job.json"),
    )
    info = await supervisor_client.jobs.get_job(
        UUID("2febe59311f94d6ba36f6f9f73357ca8")
    )

    assert info.name == "backup_manager_partial_backup"
    assert info.reference == "89cafa67"
    assert info.uuid.hex == "2febe59311f94d6ba36f6f9f73357ca8"
    assert info.progress == 0
    assert info.stage == "finishing_file"
    assert info.done is True
    assert info.errors == []
    assert info.created == datetime.fromisoformat("2025-01-30T20:55:12.859349+00:00")
    assert info.child_jobs[0].name == "backup_store_folders"
    assert info.child_jobs[0].child_jobs[0].name == "backup_folder_save"
    assert info.child_jobs[0].child_jobs[0].reference == "ssl"
    assert info.child_jobs[0].child_jobs[0].child_jobs == []


async def test_jobs_get_job_extra(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs get job API with extra metadata."""
    responses.get(
        f"{SUPERVISOR_URL}/jobs/2febe59311f94d6ba36f6f9f73357ca8",
        status=200,
        body=load_fixture("jobs_get_job_extra.json"),
    )
    info = await supervisor_client.jobs.get_job(
        UUID("2febe59311f94d6ba36f6f9f73357ca8")
    )

    assert info.name == "docker_interface_install"
    assert info.reference == "homeassistant"
    assert info.child_jobs[0].name == "Pulling container image layer"
    assert info.child_jobs[0].reference == "1e214cd6d7d0"
    assert info.child_jobs[0].progress == 26.1
    assert info.child_jobs[0].stage == "Downloading"
    assert info.child_jobs[0].extra == {"current": 227726144, "total": 436480882}


async def test_jobs_delete_job(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs delete job API."""
    responses.delete(
        f"{SUPERVISOR_URL}/jobs/2febe59311f94d6ba36f6f9f73357ca8", status=200
    )
    assert (
        await supervisor_client.jobs.delete_job(
            UUID("2febe59311f94d6ba36f6f9f73357ca8")
        )
        is None
    )
    assert responses.requests.keys() == {
        ("DELETE", URL(f"{SUPERVISOR_URL}/jobs/2febe59311f94d6ba36f6f9f73357ca8"))
    }


async def test_jobs_info_backward_compatibility_no_stage(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test jobs info API with error lacking stage field for backward compatibility."""
    responses.get(
        f"{SUPERVISOR_URL}/jobs/info",
        status=200,
        body=load_fixture("jobs_info_no_stage.json"),
    )
    info = await supervisor_client.jobs.info()
    assert info.ignore_conditions == [JobCondition.FREE_SPACE]

    assert info.jobs[0].name == "test_job_error_no_stage"
    assert info.jobs[0].reference == "test123"
    assert info.jobs[0].errors[0].type == "TestError"
    assert info.jobs[0].errors[0].message == "Test error without stage field"
    assert info.jobs[0].errors[0].stage is None