1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-05-08 17:08:36 +01:00

Prevent multiple data disks with name hassos-data-external (#4222)

* Prevent multiple data disks with name hassos-data-external

* Fix pylint issues

* Do rename before format
This commit is contained in:
Mike Degatano
2023-04-01 02:40:19 -04:00
committed by GitHub
parent 842e550dda
commit fce0d2aaed
11 changed files with 487 additions and 15 deletions
+20 -1
View File
@@ -11,6 +11,7 @@ from supervisor.dbus.udisks2.block import UDisks2Block
from supervisor.dbus.udisks2.const import FormatType, PartitionTableType
from supervisor.dbus.udisks2.data import FormatOptions
from supervisor.dbus.udisks2.filesystem import UDisks2Filesystem
from supervisor.dbus.udisks2.partition import UDisks2Partition
from supervisor.dbus.udisks2.partition_table import UDisks2PartitionTable
from supervisor.utils.dbus import DBus
@@ -57,6 +58,7 @@ async def test_block_device_info(
assert sda1.id_label is None
assert sda1.symlinks is None
assert sda1.filesystem is None
assert sda1.partition is None
await sda.connect(dbus_session_bus)
await sda1.connect(dbus_session_bus)
@@ -66,6 +68,7 @@ async def test_block_device_info(
assert sda.id_label == ""
assert sda.partition_table.type == PartitionTableType.GPT
assert sda.filesystem is None
assert sda.partition is None
assert sda1.id_label == "hassos-data-old"
assert sda1.symlinks == [
@@ -80,6 +83,7 @@ async def test_block_device_info(
]
assert sda1.partition_table is None
assert sda1.filesystem.mount_points == []
assert sda1.partition.number == 1
block_sda_service.emit_properties_changed({"IdLabel": "test"})
await block_sda_service.ping()
@@ -135,9 +139,10 @@ async def test_check_type(dbus_session_bus: MessageBus):
await sda.connect(dbus_session_bus)
await sda1.connect(dbus_session_bus)
# Connected but neither are filesystems are partition tables
# Connected but neither are filesystems, partitions or partition tables
assert sda.partition_table is None
assert sda1.filesystem is None
assert sda1.partition is None
assert sda.id_label == ""
assert sda1.id_label == "hassos-data-old"
@@ -147,11 +152,13 @@ async def test_check_type(dbus_session_bus: MessageBus):
{
"udisks2_partition_table": "/org/freedesktop/UDisks2/block_devices/sda",
"udisks2_filesystem": "/org/freedesktop/UDisks2/block_devices/sda1",
"udisks2_partition": "/org/freedesktop/UDisks2/block_devices/sda1",
},
dbus_session_bus,
)
sda_pt_service = services["udisks2_partition_table"]
sda1_fs_service = services["udisks2_filesystem"]
sda1_part_service = services["udisks2_partition"]
await sda.check_type()
await sda1.check_type()
@@ -159,11 +166,14 @@ async def test_check_type(dbus_session_bus: MessageBus):
# Check that the type is now correct and property changes are syncing
assert sda.partition_table
assert sda1.filesystem
assert sda1.partition
partition_table: UDisks2PartitionTable = sda.partition_table
filesystem: UDisks2Filesystem = sda1.filesystem
partition: UDisks2Partition = sda1.partition
assert partition_table.type == PartitionTableType.GPT
assert filesystem.size == 250058113024
assert partition.name_ == "hassos-data-external"
sda_pt_service.emit_properties_changed({"Type": "dos"})
await sda_pt_service.ping()
@@ -173,6 +183,10 @@ async def test_check_type(dbus_session_bus: MessageBus):
await sda1_fs_service.ping()
assert filesystem.size == 100
sda1_part_service.emit_properties_changed({"Name": "test"})
await sda1_part_service.ping()
assert partition.name_ == "test"
# Force introspection to return the original block device only introspection and re-check type
with patch.object(DBus, "introspect", return_value=orig_introspection):
await sda.check_type()
@@ -183,6 +197,7 @@ async def test_check_type(dbus_session_bus: MessageBus):
assert sda1.is_connected is True
assert sda.partition_table is None
assert sda1.filesystem is None
assert sda1.partition is None
# Property changes should still sync for the block devices
sda_block_service.emit_properties_changed({"IdLabel": "test"})
@@ -201,3 +216,7 @@ async def test_check_type(dbus_session_bus: MessageBus):
sda1_fs_service.emit_properties_changed({"Size": 250058113024})
await sda1_fs_service.ping()
assert filesystem.size == 100
sda1_part_service.emit_properties_changed({"Name": "hassos-data-external"})
await sda1_part_service.ping()
assert partition.name_ == "test"
+161
View File
@@ -0,0 +1,161 @@
"""Test UDisks2 Partition."""
from dbus_fast import Variant
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.udisks2.data import DeletePartitionOptions
from supervisor.dbus.udisks2.partition import UDisks2Partition
from supervisor.exceptions import DBusNotConnectedError
from tests.common import mock_dbus_services
from tests.dbus_service_mocks.udisks2_partition import Partition as PartitionService
@pytest.fixture(name="partition_sda1_service")
async def fixture_partition_sda1_service(
dbus_session_bus: MessageBus,
) -> PartitionService:
"""Mock sda1 Partition service."""
yield (
await mock_dbus_services(
{"udisks2_partition": "/org/freedesktop/UDisks2/block_devices/sda1"},
dbus_session_bus,
)
)["udisks2_partition"]
@pytest.fixture(name="partition_sdb1_service")
async def fixture_partition_sdb_service(
dbus_session_bus: MessageBus,
) -> PartitionService:
"""Mock sdb1 Partition service."""
yield (
await mock_dbus_services(
{"udisks2_partition": "/org/freedesktop/UDisks2/block_devices/sdb1"},
dbus_session_bus,
)
)["udisks2_partition"]
async def test_partition_table_info(
partition_sda1_service: PartitionService,
partition_sdb1_service: PartitionService,
dbus_session_bus: MessageBus,
):
"""Test partition table info."""
sda1 = UDisks2Partition("/org/freedesktop/UDisks2/block_devices/sda1")
sdb1 = UDisks2Partition(
"/org/freedesktop/UDisks2/block_devices/sdb1", sync_properties=False
)
assert sda1.name_ is None
assert sda1.size is None
assert sdb1.name_ is None
assert sdb1.size is None
await sda1.connect(dbus_session_bus)
await sdb1.connect(dbus_session_bus)
assert sda1.name_ == "hassos-data-external"
assert sda1.size == 250058113024
assert sdb1.name_ == ""
assert sdb1.size == 67108864
partition_sda1_service.emit_properties_changed({"Name": "test"})
await partition_sda1_service.ping()
assert sda1.name_ == "test"
partition_sda1_service.emit_properties_changed({}, ["Name"])
await partition_sda1_service.ping()
await partition_sda1_service.ping()
assert sda1.name_ == "hassos-data-external"
# Prop changes should not sync for this one
partition_sdb1_service.emit_properties_changed({"Name": "test"})
await partition_sdb1_service.ping()
assert sdb1.name_ == ""
async def test_set_type(
partition_sda1_service: PartitionService, dbus_session_bus: MessageBus
):
"""Test setting partition type."""
partition_sda1_service.SetType.calls.clear()
sda1 = UDisks2Partition("/org/freedesktop/UDisks2/block_devices/sda1")
with pytest.raises(DBusNotConnectedError):
await sda1.set_type("0FC63DAF-8483-4772-8E79-3D69D8477DE4")
await sda1.connect(dbus_session_bus)
await sda1.set_type("0FC63DAF-8483-4772-8E79-3D69D8477DE4")
assert partition_sda1_service.SetType.calls == [
(
"0FC63DAF-8483-4772-8E79-3D69D8477DE4",
{"auth.no_user_interaction": Variant("b", True)},
)
]
async def test_set_name(
partition_sda1_service: PartitionService, dbus_session_bus: MessageBus
):
"""Test setting partition name."""
partition_sda1_service.SetName.calls.clear()
sda1 = UDisks2Partition("/org/freedesktop/UDisks2/block_devices/sda1")
with pytest.raises(DBusNotConnectedError):
await sda1.set_name("test")
await sda1.connect(dbus_session_bus)
await sda1.set_name("test")
assert partition_sda1_service.SetName.calls == [
("test", {"auth.no_user_interaction": Variant("b", True)})
]
async def test_resize(
partition_sda1_service: PartitionService, dbus_session_bus: MessageBus
):
"""Test resizing partition."""
partition_sda1_service.Resize.calls.clear()
sda1 = UDisks2Partition("/org/freedesktop/UDisks2/block_devices/sda1")
with pytest.raises(DBusNotConnectedError):
await sda1.resize()
await sda1.connect(dbus_session_bus)
await sda1.resize()
assert partition_sda1_service.Resize.calls == [
(0, {"auth.no_user_interaction": Variant("b", True)})
]
async def test_delete(
partition_sda1_service: PartitionService, dbus_session_bus: MessageBus
):
"""Test deleting partition."""
partition_sda1_service.Delete.calls.clear()
sda1 = UDisks2Partition("/org/freedesktop/UDisks2/block_devices/sda1")
with pytest.raises(DBusNotConnectedError):
await sda1.delete(DeletePartitionOptions(tear_down=True))
await sda1.connect(dbus_session_bus)
await sda1.delete(DeletePartitionOptions(tear_down=True))
assert partition_sda1_service.Delete.calls == [
(
{
"tear-down": Variant("b", True),
"auth.no_user_interaction": Variant("b", True),
},
)
]