File: test_mounts.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 (176 lines) | stat: -rw-r--r-- 5,428 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Test mounts supervisor client."""

from pathlib import PurePath

from aioresponses import aioresponses
import pytest
from yarl import URL

from aiohasupervisor import SupervisorClient
from aiohasupervisor.models import (
    CIFSMountRequest,
    MountCifsVersion,
    MountsOptions,
    MountUsage,
    NFSMountRequest,
)

from . import load_fixture
from .const import SUPERVISOR_URL


async def test_mounts_info(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test mounts info API."""
    responses.get(
        f"{SUPERVISOR_URL}/mounts", status=200, body=load_fixture("mounts_info.json")
    )
    info = await supervisor_client.mounts.info()
    assert info.default_backup_mount == "Test"

    assert info.mounts[0].name == "Test"
    assert info.mounts[0].server == "test.local"
    assert info.mounts[0].type == "cifs"
    assert info.mounts[0].share == "backup"
    assert info.mounts[0].usage == "backup"
    assert info.mounts[0].read_only is False
    assert info.mounts[0].version is None
    assert info.mounts[0].state == "active"
    assert info.mounts[0].user_path is None

    assert info.mounts[1].usage == "share"
    assert info.mounts[1].read_only is True
    assert info.mounts[1].version == "2.0"
    assert info.mounts[1].port == 12345
    assert info.mounts[1].user_path == PurePath("/share/Test2")

    assert info.mounts[2].type == "nfs"
    assert info.mounts[2].usage == "media"
    assert info.mounts[2].path.as_posix() == "media"
    assert info.mounts[2].user_path == PurePath("/media/Test3")


@pytest.mark.parametrize("mount_name", ["test", None])
async def test_mounts_options(
    responses: aioresponses, supervisor_client: SupervisorClient, mount_name: str | None
) -> None:
    """Test mounts options API."""
    responses.post(f"{SUPERVISOR_URL}/mounts/options", status=200)
    assert (
        await supervisor_client.mounts.options(
            MountsOptions(default_backup_mount=mount_name)
        )
        is None
    )
    assert responses.requests.keys() == {
        ("POST", URL(f"{SUPERVISOR_URL}/mounts/options"))
    }


@pytest.mark.parametrize(
    "mount_config",
    [
        CIFSMountRequest(
            server="test.local",
            share="backup",
            usage=MountUsage.BACKUP,
        ),
        CIFSMountRequest(
            server="test.local",
            share="media",
            port=12345,
            usage=MountUsage.MEDIA,
            version=MountCifsVersion.LEGACY_2_0,
            read_only=True,
            username="test",
            password="test",  # noqa: S106
        ),
        NFSMountRequest(
            server="test.local",
            path=PurePath("share"),
            usage=MountUsage.SHARE,
        ),
        NFSMountRequest(
            server="test.local",
            path=PurePath("backups"),
            port=12345,
            read_only=False,
            usage=MountUsage.BACKUP,
        ),
    ],
)
async def test_create_mount(
    responses: aioresponses,
    supervisor_client: SupervisorClient,
    mount_config: CIFSMountRequest | NFSMountRequest,
) -> None:
    """Test create mount API."""
    responses.post(f"{SUPERVISOR_URL}/mounts", status=200)
    assert await supervisor_client.mounts.create_mount("test", mount_config) is None
    assert responses.requests.keys() == {("POST", URL(f"{SUPERVISOR_URL}/mounts"))}


@pytest.mark.parametrize(
    "mount_config",
    [
        CIFSMountRequest(
            server="test.local",
            share="backup",
            usage=MountUsage.BACKUP,
        ),
        CIFSMountRequest(
            server="test.local",
            share="media",
            port=12345,
            usage=MountUsage.MEDIA,
            version=MountCifsVersion.LEGACY_2_0,
            read_only=True,
            username="test",
            password="test",  # noqa: S106
        ),
        NFSMountRequest(
            server="test.local",
            path=PurePath("share"),
            usage=MountUsage.SHARE,
        ),
        NFSMountRequest(
            server="test.local",
            path=PurePath("backups"),
            port=12345,
            read_only=False,
            usage=MountUsage.BACKUP,
        ),
    ],
)
async def test_update_mount(
    responses: aioresponses,
    supervisor_client: SupervisorClient,
    mount_config: CIFSMountRequest | NFSMountRequest,
) -> None:
    """Test update mount API."""
    responses.put(f"{SUPERVISOR_URL}/mounts/test", status=200)
    assert await supervisor_client.mounts.update_mount("test", mount_config) is None
    assert responses.requests.keys() == {("PUT", URL(f"{SUPERVISOR_URL}/mounts/test"))}


async def test_delete_mount(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test delete mount API."""
    responses.delete(f"{SUPERVISOR_URL}/mounts/test", status=200)
    assert await supervisor_client.mounts.delete_mount("test") is None
    assert responses.requests.keys() == {
        ("DELETE", URL(f"{SUPERVISOR_URL}/mounts/test"))
    }


async def test_reload_mount(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test reload mount API."""
    responses.post(f"{SUPERVISOR_URL}/mounts/test/reload", status=200)
    assert await supervisor_client.mounts.reload_mount("test") is None
    assert responses.requests.keys() == {
        ("POST", URL(f"{SUPERVISOR_URL}/mounts/test/reload"))
    }