Adding disk mount handling to React UI.
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

This commit is contained in:
Roland 2025-12-16 15:06:41 -07:00
parent 1a02a4eb5b
commit 628b9f3096
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
10 changed files with 600 additions and 51 deletions

View file

@ -205,6 +205,36 @@ class TestSaveConfig:
# Only port should be in the file
assert saved == {"port": 9999}
def test_save_config_removes_file_when_matches_defaults(
self, reset_runtime_config, mock_config_path, mock_drive_mounted
):
"""Should remove config file when config matches defaults."""
# First create a config file with non-default value
update_config("port", 9999)
save_config()
assert mock_config_path.exists()
# Reset to default value
update_config("port", 8087) # Default port
save_config()
# File should be removed since config matches defaults
assert not mock_config_path.exists()
def test_save_config_removes_stale_file(
self, reset_runtime_config, mock_config_path, mock_drive_mounted
):
"""Should remove existing config file when config is reset to defaults."""
# Simulate an existing stale config file
mock_config_path.write_text("drives:\n mounts:\n - device: UUID=1234\n")
assert mock_config_path.exists()
# Runtime config matches defaults (no changes made)
save_config()
# File should be removed
assert not mock_config_path.exists()
class TestUpdateConfig:
"""Tests for update_config function."""

View file

@ -169,6 +169,143 @@ class TestSmartEndpoints:
assert len(data["drives"]) == 1
class TestMountPersistSavesConfig:
"""Tests for mount with persist=true saving to controller.yaml."""
def test_mount_with_persist_saves_config(self, test_client):
"""Mount with persist=true should save to controller.yaml."""
with mock.patch(
"controller.api.disks.mount_drive",
return_value={"success": True, "mount_point": "/media/test"}
):
with mock.patch(
"controller.api.disks.add_fstab_entry",
return_value={"success": True, "device": "UUID=1234", "mount_point": "/media/test"}
):
with mock.patch("controller.api.disks.save_config") as mock_save:
with mock.patch("controller.api.disks.get_config_value", return_value=[]):
with mock.patch("controller.api.disks.update_config") as mock_update:
response = test_client.post(
"/api/disks/mount",
json={
"device": "/dev/sda1",
"mount_point": "/media/test",
"fstype": "ext4",
"options": "defaults",
"persist": True
}
)
assert response.status_code == 200
# Check that update_config was called with correct mount entry
mock_update.assert_called_once()
call_args = mock_update.call_args
assert call_args[0][0] == "drives.mounts"
mounts = call_args[0][1]
assert len(mounts) == 1
assert mounts[0]["device"] == "UUID=1234"
assert mounts[0]["mount_point"] == "/media/test"
# Check that save_config was called
mock_save.assert_called_once()
def test_mount_without_persist_does_not_save_config(self, test_client):
"""Mount without persist=true should not save to controller.yaml."""
with mock.patch(
"controller.api.disks.mount_drive",
return_value={"success": True, "mount_point": "/media/test"}
):
with mock.patch("controller.api.disks.save_config") as mock_save:
response = test_client.post(
"/api/disks/mount",
json={
"device": "/dev/sda1",
"mount_point": "/media/test",
"persist": False
}
)
assert response.status_code == 200
mock_save.assert_not_called()
def test_mount_replaces_existing_entry_for_same_device(self, test_client):
"""Mount should replace existing entry for same device UUID."""
existing_mounts = [
{"device": "UUID=1234", "mount_point": "/media/old", "fstype": "ext4", "options": "defaults"}
]
with mock.patch(
"controller.api.disks.mount_drive",
return_value={"success": True}
):
with mock.patch(
"controller.api.disks.add_fstab_entry",
return_value={"success": True, "device": "UUID=1234", "mount_point": "/media/new"}
):
with mock.patch("controller.api.disks.save_config"):
with mock.patch("controller.api.disks.get_config_value", return_value=existing_mounts):
with mock.patch("controller.api.disks.update_config") as mock_update:
response = test_client.post(
"/api/disks/mount",
json={
"device": "/dev/sda1",
"mount_point": "/media/new",
"fstype": "ext4",
"persist": True
}
)
assert response.status_code == 200
# Should have replaced the old entry
call_args = mock_update.call_args
mounts = call_args[0][1]
assert len(mounts) == 1
assert mounts[0]["mount_point"] == "/media/new"
class TestDeleteFstabRemovesConfig:
"""Tests for delete_fstab removing from controller.yaml."""
def test_delete_fstab_removes_from_config(self, test_client):
"""Delete fstab entry should remove from controller.yaml."""
existing_mounts = [
{"device": "UUID=1234", "mount_point": "/media/test", "fstype": "ext4", "options": "defaults"}
]
with mock.patch(
"controller.api.disks.remove_fstab_entry",
return_value={"success": True, "mount_point": "/media/test"}
):
with mock.patch("controller.api.disks.save_config") as mock_save:
with mock.patch("controller.api.disks.get_config_value", return_value=existing_mounts):
with mock.patch("controller.api.disks.update_config") as mock_update:
response = test_client.delete("/api/disks/fstab/media/test")
assert response.status_code == 200
# Check that update_config was called with empty list
mock_update.assert_called_once()
call_args = mock_update.call_args
assert call_args[0][0] == "drives.mounts"
mounts = call_args[0][1]
assert len(mounts) == 0
# Check that save_config was called
mock_save.assert_called_once()
def test_delete_fstab_preserves_other_mounts(self, test_client):
"""Delete fstab should only remove the specified mount."""
existing_mounts = [
{"device": "UUID=1234", "mount_point": "/media/test", "fstype": "ext4", "options": "defaults"},
{"device": "UUID=5678", "mount_point": "/media/other", "fstype": "ext4", "options": "defaults"},
]
with mock.patch(
"controller.api.disks.remove_fstab_entry",
return_value={"success": True, "mount_point": "/media/test"}
):
with mock.patch("controller.api.disks.save_config"):
with mock.patch("controller.api.disks.get_config_value", return_value=existing_mounts):
with mock.patch("controller.api.disks.update_config") as mock_update:
response = test_client.delete("/api/disks/fstab/media/test")
assert response.status_code == 200
# Should preserve the other mount
call_args = mock_update.call_args
mounts = call_args[0][1]
assert len(mounts) == 1
assert mounts[0]["mount_point"] == "/media/other"
class TestOpenAPIIncludesDisksEndpoints:
"""Tests that OpenAPI documentation includes disks endpoints."""

View file

@ -131,6 +131,45 @@ UUID=5678 /media/test ext4 defaults,nofail 0 2
# The old timestamp should NOT be there
assert "2025-12-15T15:57:37.675757" not in written_str
def test_replaces_entry_for_same_uuid_different_mount_point(self):
"""Should remove old entry when adding same UUID to different mount point.
Scenario: User mounts /dev/sda1 (UUID=5678) to /media/64GB with persist,
then unmounts, then mounts same device to /media/64GB-2 with persist.
The old /media/64GB entry should be removed.
"""
fstab_content = """\
# /etc/fstab
UUID=1234 / ext4 defaults 0 1
# WROLPi managed mount - added 2025-12-15T15:57:37.675757
UUID=5678 /media/64GB exfat defaults,nofail,x-systemd.device-timeout=10s 0 2
"""
written_content = []
def mock_write(content):
written_content.append(content)
mock_file = mock.mock_open(read_data=fstab_content)
mock_file().write = mock_write
with mock.patch("controller.lib.fstab.require_normal_mode"):
with mock.patch("controller.lib.fstab.get_uuid", return_value="5678"):
with mock.patch("controller.lib.fstab.backup_fstab"):
with mock.patch("builtins.open", mock_file):
with mock.patch("subprocess.run"):
# Mount same device to different mount point
result = add_fstab_entry("/dev/sda1", "/media/64GB-2", "exfat")
assert result["success"] is True
written_str = "".join(written_content)
# Should only have ONE entry for UUID=5678 (the new one)
assert written_str.count("UUID=5678") == 1
# Should have the new mount point
assert "/media/64GB-2" in written_str
# Should NOT have the old mount point
assert "/media/64GB " not in written_str
# Should have exactly ONE WROLPi comment
assert written_str.count("WROLPi managed mount") == 1
class TestRemoveFstabEntry:
"""Tests for remove_fstab_entry function."""