mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 23:46:18 -05:00
Add controller onboarding flow for primary drive setup
Introduce an onboarding system in the Controller that guides users through selecting and mounting their primary media drive. This is the fallback UI flow used when the main React app is unavailable. Key changes: - Add onboarding API endpoints and schemas (controller/api/onboarding.py) - Add onboarding logic to detect drives, handle already-mounted drives (needed for RPi which auto-mounts), and save primary drive selection to the controller config when no mounts are configured - Extend the Controller's index.html template with the onboarding UI - Skip root cert creation until onboarding is complete - Update first_startup.sh to support the onboarding/repair flow - Add comprehensive tests for onboarding logic and API
This commit is contained in:
parent
f146b332a8
commit
07f601e77f
15 changed files with 1464 additions and 66 deletions
|
|
@ -211,8 +211,19 @@ class TestDriveMountedStatus:
|
|||
data = response.json()
|
||||
assert data["drive_mounted"] is expected
|
||||
|
||||
def test_dashboard_shows_drive_not_mounted_banner(self, reset_runtime_config, mock_docker_mode):
|
||||
"""Dashboard should show banner when drive is not mounted."""
|
||||
def test_dashboard_shows_onboarding_when_drive_not_mounted(self, reset_runtime_config, mock_docker_mode):
|
||||
"""Dashboard should show onboarding banner in native mode when drive is not mounted."""
|
||||
from controller.main import app
|
||||
|
||||
with mock.patch(
|
||||
"controller.main.is_primary_drive_mounted", return_value=False
|
||||
):
|
||||
with TestClient(app) as client:
|
||||
response = client.get("/")
|
||||
assert "Welcome to WROLPi" in response.text
|
||||
|
||||
def test_dashboard_shows_drive_not_mounted_banner_in_docker(self, reset_runtime_config, mock_docker_mode_enabled):
|
||||
"""Dashboard should show info banner in Docker mode when drive is not mounted."""
|
||||
from controller.main import app
|
||||
|
||||
with mock.patch(
|
||||
|
|
|
|||
|
|
@ -83,32 +83,21 @@ class TestIsDockerMode:
|
|||
class TestIsPrimaryDriveMounted:
|
||||
"""Tests for is_primary_drive_mounted function."""
|
||||
|
||||
def test_drive_mounted_when_config_dir_exists(self, test_config_directory):
|
||||
"""Should return True when /media/wrolpi/config exists."""
|
||||
# We need to mock the path check to use our test directory
|
||||
from unittest import mock
|
||||
def test_drive_mounted_when_controller_yaml_exists(self, mock_config_path):
|
||||
"""Should return True when controller.yaml exists."""
|
||||
mock_config_path.write_text("{}")
|
||||
assert is_primary_drive_mounted() is True
|
||||
|
||||
with mock.patch(
|
||||
"controller.lib.config.Path"
|
||||
) as mock_path:
|
||||
mock_path.return_value.exists.return_value = True
|
||||
# Re-import to get fresh function
|
||||
# Actually test with real path
|
||||
def test_drive_not_mounted_when_controller_yaml_missing(self, mock_config_path):
|
||||
"""Should return False when controller.yaml doesn't exist."""
|
||||
# mock_config_path points to a non-existent file by default
|
||||
assert is_primary_drive_mounted() is False
|
||||
|
||||
# The real test - check that the function works correctly
|
||||
def test_drive_not_mounted_when_only_config_dir_exists(self, test_config_directory, mock_config_path):
|
||||
"""Should return False when config/ dir exists but controller.yaml doesn't."""
|
||||
# test_config_directory creates the config dir but not controller.yaml
|
||||
assert test_config_directory.exists()
|
||||
|
||||
def test_drive_not_mounted_when_config_dir_missing(self, test_directory):
|
||||
"""Should return False when config dir doesn't exist."""
|
||||
from unittest import mock
|
||||
|
||||
with mock.patch(
|
||||
"controller.lib.config.Path"
|
||||
) as mock_path:
|
||||
mock_instance = mock.MagicMock()
|
||||
mock_instance.exists.return_value = False
|
||||
mock_path.return_value = mock_instance
|
||||
assert is_primary_drive_mounted() is False
|
||||
assert is_primary_drive_mounted() is False
|
||||
|
||||
|
||||
class TestReloadConfigFromDrive:
|
||||
|
|
@ -175,12 +164,18 @@ class TestReloadConfigFromDrive:
|
|||
class TestSaveConfig:
|
||||
"""Tests for save_config function."""
|
||||
|
||||
def test_save_config_raises_when_drive_not_mounted(
|
||||
self, reset_runtime_config, mock_drive_not_mounted
|
||||
def test_save_config_raises_when_config_dir_missing(
|
||||
self, reset_runtime_config, test_directory
|
||||
):
|
||||
"""Should raise RuntimeError when drive not mounted."""
|
||||
with pytest.raises(RuntimeError, match="primary drive not mounted"):
|
||||
save_config()
|
||||
"""Should raise RuntimeError when config directory doesn't exist."""
|
||||
from unittest import mock
|
||||
from pathlib import Path
|
||||
|
||||
# Point CONFIG_PATH_ON_DRIVE to a non-existent directory
|
||||
fake_path = Path(str(test_directory)) / "nonexistent" / "controller.yaml"
|
||||
with mock.patch("controller.lib.config.CONFIG_PATH_ON_DRIVE", fake_path):
|
||||
with pytest.raises(RuntimeError, match="primary drive not mounted"):
|
||||
save_config()
|
||||
|
||||
def test_save_config_creates_file(
|
||||
self, reset_runtime_config, mock_config_path, mock_drive_mounted
|
||||
|
|
@ -205,10 +200,10 @@ class TestSaveConfig:
|
|||
# Only port should be in the file
|
||||
assert saved == {"port": 9999}
|
||||
|
||||
def test_save_config_removes_file_when_matches_defaults(
|
||||
def test_save_config_writes_empty_when_matches_defaults(
|
||||
self, reset_runtime_config, mock_config_path, mock_drive_mounted
|
||||
):
|
||||
"""Should remove config file when config matches defaults."""
|
||||
"""Should still write file when config matches defaults (marker behavior)."""
|
||||
# First create a config file with non-default value
|
||||
update_config("port", 9999)
|
||||
save_config()
|
||||
|
|
@ -218,22 +213,20 @@ class TestSaveConfig:
|
|||
update_config("port", 8087) # Default port
|
||||
save_config()
|
||||
|
||||
# File should be removed since config matches defaults
|
||||
assert not mock_config_path.exists()
|
||||
# File should still exist (serves as marker that drive is set up)
|
||||
assert mock_config_path.exists()
|
||||
saved = yaml.safe_load(mock_config_path.read_text())
|
||||
assert saved == {} or saved is None
|
||||
|
||||
def test_save_config_removes_stale_file(
|
||||
def test_save_config_always_writes_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()
|
||||
|
||||
"""Should always write controller.yaml even with default config."""
|
||||
# Runtime config matches defaults (no changes made)
|
||||
save_config()
|
||||
|
||||
# File should be removed
|
||||
assert not mock_config_path.exists()
|
||||
# File should exist as a marker
|
||||
assert mock_config_path.exists()
|
||||
|
||||
|
||||
class TestUpdateConfig:
|
||||
|
|
|
|||
439
controller/test/test_onboarding.py
Normal file
439
controller/test/test_onboarding.py
Normal file
|
|
@ -0,0 +1,439 @@
|
|||
"""
|
||||
Unit tests for onboarding orchestration logic.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from controller.lib.onboarding import (
|
||||
TEMP_MOUNT_PATH,
|
||||
cancel_probe,
|
||||
commit_onboarding,
|
||||
get_onboarding_candidates,
|
||||
probe_drive,
|
||||
)
|
||||
|
||||
|
||||
class TestGetOnboardingCandidates:
|
||||
"""Tests for get_onboarding_candidates."""
|
||||
|
||||
def test_excludes_wrolpi_primary_mount(self, mock_docker_mode):
|
||||
"""Drives mounted at /media/wrolpi should not appear as candidates."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="500G", fstype="ext4",
|
||||
mountpoint="/media/wrolpi", label="wrolpi", uuid="abc", model="SanDisk",
|
||||
),
|
||||
BlockDevice(
|
||||
name="sdb1", path="/dev/sdb1", size="1T", fstype="ext4",
|
||||
mountpoint=None, label="backup", uuid="def", model="WD",
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 1
|
||||
assert candidates[0]["path"] == "/dev/sdb1"
|
||||
|
||||
def test_excludes_system_mounts(self, mock_docker_mode):
|
||||
"""Drives at /, /boot, /boot/firmware should be excluded."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="mmcblk0p1", path="/dev/mmcblk0p1", size="256M", fstype="vfat",
|
||||
mountpoint="/boot/firmware", label="boot", uuid="aaa", model=None,
|
||||
),
|
||||
BlockDevice(
|
||||
name="mmcblk0p2", path="/dev/mmcblk0p2", size="32G", fstype="ext4",
|
||||
mountpoint="/", label="rootfs", uuid="bbb", model=None,
|
||||
),
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="1T", fstype="ext4",
|
||||
mountpoint=None, label="data", uuid="ccc", model="WD",
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 1
|
||||
assert candidates[0]["path"] == "/dev/sda1"
|
||||
|
||||
def test_includes_auto_mounted_drives(self, mock_docker_mode):
|
||||
"""Drives auto-mounted under /media/pi/ should appear as candidates."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="500G", fstype="ext4",
|
||||
mountpoint="/media/pi/MyDrive", label="MyDrive", uuid="abc", model="SanDisk",
|
||||
),
|
||||
BlockDevice(
|
||||
name="sdb1", path="/dev/sdb1", size="1T", fstype="ext4",
|
||||
mountpoint="/media/pi/Backup", label="Backup", uuid="def", model="WD",
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 2
|
||||
assert candidates[0]["mountpoint"] == "/media/pi/MyDrive"
|
||||
assert candidates[1]["mountpoint"] == "/media/pi/Backup"
|
||||
|
||||
def test_returns_empty_in_docker(self, mock_docker_mode_enabled):
|
||||
"""Docker mode returns no block devices, so no candidates."""
|
||||
candidates = get_onboarding_candidates()
|
||||
assert candidates == []
|
||||
|
||||
def test_returns_all_unmounted(self, mock_docker_mode):
|
||||
"""All unmounted drives should be candidates with mountpoint=None."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="500G", fstype="ext4",
|
||||
mountpoint=None, label=None, uuid="abc", model="SanDisk",
|
||||
),
|
||||
BlockDevice(
|
||||
name="sdb1", path="/dev/sdb1", size="1T", fstype="btrfs",
|
||||
mountpoint=None, label="data", uuid="def", model=None,
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 2
|
||||
assert candidates[0]["fstype"] == "ext4"
|
||||
assert candidates[0]["mountpoint"] is None
|
||||
assert candidates[1]["fstype"] == "btrfs"
|
||||
|
||||
def test_excludes_efi_partitions(self, mock_docker_mode):
|
||||
"""EFI system partitions should not appear as candidates."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="200M", fstype="vfat",
|
||||
mountpoint=None, label="EFI", uuid="67E3-17ED", model="SanDisk",
|
||||
),
|
||||
BlockDevice(
|
||||
name="sda2", path="/dev/sda2", size="500G", fstype="ext4",
|
||||
mountpoint=None, label="data", uuid="abc-123", model="SanDisk",
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 1
|
||||
assert candidates[0]["path"] == "/dev/sda2"
|
||||
|
||||
def test_excludes_efi_partitions_case_insensitive(self, mock_docker_mode):
|
||||
"""EFI label filtering should be case-insensitive."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="200M", fstype="vfat",
|
||||
mountpoint=None, label="efi", uuid="67E3-17ED", model="SanDisk",
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 0
|
||||
|
||||
def test_excludes_temp_onboarding_mount(self, mock_docker_mode):
|
||||
"""Drives mounted at the temp onboarding path should be excluded."""
|
||||
from controller.lib.disks import BlockDevice
|
||||
|
||||
devices = [
|
||||
BlockDevice(
|
||||
name="sda1", path="/dev/sda1", size="500G", fstype="ext4",
|
||||
mountpoint="/media/wrolpi_temp_onboarding", label=None, uuid="abc", model="SanDisk",
|
||||
),
|
||||
]
|
||||
with mock.patch("controller.lib.onboarding.get_block_devices", return_value=devices):
|
||||
candidates = get_onboarding_candidates()
|
||||
|
||||
assert len(candidates) == 0
|
||||
|
||||
|
||||
class TestProbeDrive:
|
||||
"""Tests for probe_drive."""
|
||||
|
||||
def test_probe_with_config_present(self, mock_docker_mode, tmp_path):
|
||||
"""Should find config and return mounts."""
|
||||
config_dir = tmp_path / "config"
|
||||
config_dir.mkdir()
|
||||
config_data = {"drives": {"mounts": [
|
||||
{"device": "/dev/sda1", "mount_point": "/media/wrolpi", "fstype": "ext4"},
|
||||
{"device": "/dev/sdb1", "mount_point": "/media/backup", "fstype": "ext4"},
|
||||
]}}
|
||||
(config_dir / "controller.yaml").write_text(yaml.dump(config_data))
|
||||
|
||||
with mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.TEMP_MOUNT_PATH", str(tmp_path)), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding._get_current_mountpoint", return_value=None):
|
||||
result = probe_drive("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["config_found"] is True
|
||||
assert len(result["mounts"]) == 2
|
||||
assert result["device_path"] == "/dev/sda1"
|
||||
|
||||
def test_probe_detects_legacy_wrolpi_config(self, mock_docker_mode, tmp_path):
|
||||
"""Should detect a WROLPi drive that has wrolpi.yaml but no controller.yaml."""
|
||||
config_dir = tmp_path / "config"
|
||||
config_dir.mkdir()
|
||||
(config_dir / "wrolpi.yaml").write_text("ignore_outdated_zims: true\n")
|
||||
|
||||
with mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.TEMP_MOUNT_PATH", str(tmp_path)), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding._get_current_mountpoint", return_value=None):
|
||||
result = probe_drive("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["config_found"] is True
|
||||
assert result["mounts"] == []
|
||||
|
||||
def test_probe_with_no_config(self, mock_docker_mode, tmp_path):
|
||||
"""Should report config_found=False when no config exists."""
|
||||
with mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.TEMP_MOUNT_PATH", str(tmp_path)), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding._get_current_mountpoint", return_value=None):
|
||||
result = probe_drive("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["config_found"] is False
|
||||
assert result["mounts"] == []
|
||||
|
||||
def test_probe_mount_failure(self, mock_docker_mode):
|
||||
"""Should return error when mount fails."""
|
||||
with mock.patch(
|
||||
"controller.lib.onboarding.mount_drive",
|
||||
return_value={"success": False, "error": "Permission denied"},
|
||||
), mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding._get_current_mountpoint", return_value=None):
|
||||
result = probe_drive("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "Permission denied" in result["error"]
|
||||
|
||||
def test_probe_unmounts_auto_mounted_drive(self, mock_docker_mode, tmp_path):
|
||||
"""Probe should unmount an auto-mounted drive before temp-mounting."""
|
||||
with mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.TEMP_MOUNT_PATH", str(tmp_path)), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding._get_current_mountpoint", return_value="/media/pi/MyDrive"), \
|
||||
mock.patch("controller.lib.onboarding.unmount_drive", return_value={"success": True}) as mock_unmount:
|
||||
result = probe_drive("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
mock_unmount.assert_called_once_with("/media/pi/MyDrive", lazy=True)
|
||||
|
||||
def test_probe_fails_if_unmount_fails(self, mock_docker_mode):
|
||||
"""Probe should fail if the auto-mounted drive can't be unmounted."""
|
||||
with mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding._get_current_mountpoint", return_value="/media/pi/MyDrive"), \
|
||||
mock.patch("controller.lib.onboarding.unmount_drive", return_value={"success": False, "error": "busy"}):
|
||||
result = probe_drive("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "Failed to unmount" in result["error"]
|
||||
|
||||
|
||||
class TestCommitOnboarding:
|
||||
"""Tests for commit_onboarding."""
|
||||
|
||||
def test_full_flow_with_config(self, mock_docker_mode, tmp_path):
|
||||
"""Full onboarding with config file on drive."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
media_dir.mkdir()
|
||||
config_dir = media_dir / "config"
|
||||
config_dir.mkdir()
|
||||
|
||||
config_data = {"drives": {"mounts": [
|
||||
{"device": "/dev/sdb1", "mount_point": "/media/backup", "fstype": "ext4"},
|
||||
]}}
|
||||
(config_dir / "controller.yaml").write_text(yaml.dump(config_data))
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=True), \
|
||||
mock.patch("controller.lib.onboarding.save_config"), \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
result = commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
assert str(media_dir) in result["mounts"]
|
||||
assert result["repair_started"] is True
|
||||
|
||||
def test_fresh_drive_with_force(self, mock_docker_mode, tmp_path):
|
||||
"""Fresh drive with force=True should succeed."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=False), \
|
||||
mock.patch("controller.lib.onboarding.save_config"), \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
result = commit_onboarding("/dev/sda1", "ext4", force=True)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["repair_started"] is True
|
||||
|
||||
def test_cleans_transient_config_before_mount(self, mock_docker_mode, tmp_path):
|
||||
"""Should delete transient SSL files before mounting the real drive."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
media_dir.mkdir()
|
||||
# Simulate transient config created by generate_certificates.sh
|
||||
transient_config = media_dir / "config"
|
||||
transient_config.mkdir()
|
||||
transient_ssl = transient_config / "ssl"
|
||||
transient_ssl.mkdir()
|
||||
(transient_ssl / "ca.key").write_text("fake-key")
|
||||
(transient_ssl / "ca.crt").write_text("fake-cert")
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=False), \
|
||||
mock.patch("controller.lib.onboarding.save_config"), \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
# media_dir is not a mount point (it's a tmp_path), so transient cleanup runs
|
||||
result = commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
# Transient SSL files should have been removed before mount
|
||||
# (config dir is recreated by step 3, but the stale SSL should be gone)
|
||||
assert not transient_ssl.exists()
|
||||
assert not (transient_ssl / "ca.key").exists()
|
||||
|
||||
def test_calls_save_config_after_reload(self, mock_docker_mode, tmp_path):
|
||||
"""Should call save_config after reload to ensure controller.yaml exists."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=False), \
|
||||
mock.patch("controller.lib.onboarding.save_config") as mock_save, \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
mock_save.assert_called()
|
||||
|
||||
def test_saves_primary_drive_to_config_when_mounts_empty(self, mock_docker_mode, tmp_path):
|
||||
"""Should save primary drive to drives.mounts when config has no mounts."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True, "device": "UUID=abc-123"}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=False), \
|
||||
mock.patch("controller.lib.onboarding.save_config"), \
|
||||
mock.patch("controller.lib.onboarding.get_config_value", return_value=[]), \
|
||||
mock.patch("controller.lib.onboarding.update_config") as mock_update, \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
mock_update.assert_called_once_with("drives.mounts", [{
|
||||
"device": "UUID=abc-123",
|
||||
"mount_point": str(media_dir),
|
||||
"fstype": "ext4",
|
||||
"options": "defaults",
|
||||
}])
|
||||
|
||||
def test_does_not_overwrite_existing_mounts(self, mock_docker_mode, tmp_path):
|
||||
"""Should not modify drives.mounts when config already has mounts."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
existing_mounts = [{"device": "UUID=old", "mount_point": "/media/wrolpi", "fstype": "ext4"}]
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=True), \
|
||||
mock.patch("controller.lib.onboarding.save_config"), \
|
||||
mock.patch("controller.lib.onboarding.get_config_value", return_value=existing_mounts), \
|
||||
mock.patch("controller.lib.onboarding.update_config") as mock_update, \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
mock_update.assert_not_called()
|
||||
|
||||
def test_mount_failure(self, mock_docker_mode, tmp_path):
|
||||
"""Should return error when primary mount fails."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch(
|
||||
"controller.lib.onboarding.mount_drive",
|
||||
return_value={"success": False, "error": "Device busy"},
|
||||
):
|
||||
result = commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "Device busy" in result["error"]
|
||||
|
||||
def test_secondary_mount_failure_continues(self, mock_docker_mode, tmp_path):
|
||||
"""Secondary mount failure should not abort onboarding."""
|
||||
media_dir = tmp_path / "wrolpi"
|
||||
media_dir.mkdir()
|
||||
config_dir = media_dir / "config"
|
||||
config_dir.mkdir()
|
||||
|
||||
config_data = {"drives": {"mounts": [
|
||||
{"device": "/dev/sdb1", "mount_point": "/media/backup", "fstype": "ext4"},
|
||||
]}}
|
||||
(config_dir / "controller.yaml").write_text(yaml.dump(config_data))
|
||||
|
||||
call_count = 0
|
||||
|
||||
def mount_side_effect(device, mount_point, **kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
return {"success": True} # Primary mount succeeds
|
||||
return {"success": False, "error": "Not found"} # Secondary fails
|
||||
|
||||
with mock.patch("controller.lib.onboarding.get_media_directory", return_value=media_dir), \
|
||||
mock.patch("controller.lib.onboarding._cleanup_temp_mount"), \
|
||||
mock.patch("controller.lib.onboarding.mount_drive", side_effect=mount_side_effect), \
|
||||
mock.patch("controller.lib.onboarding.add_fstab_entry", return_value={"success": True}), \
|
||||
mock.patch("controller.lib.onboarding.reload_config_from_drive", return_value=True), \
|
||||
mock.patch("controller.lib.onboarding.save_config"), \
|
||||
mock.patch("controller.lib.onboarding.start_script", return_value={"success": True}):
|
||||
result = commit_onboarding("/dev/sda1", "ext4")
|
||||
|
||||
assert result["success"] is True
|
||||
# Only primary mount should be in the list
|
||||
assert len(result["mounts"]) == 1
|
||||
|
||||
|
||||
class TestCancelProbe:
|
||||
"""Tests for cancel_probe."""
|
||||
|
||||
def test_cancel_cleans_up(self, mock_docker_mode):
|
||||
"""Cancel should clean up temp mount."""
|
||||
with mock.patch("controller.lib.onboarding._cleanup_temp_mount") as mock_cleanup:
|
||||
result = cancel_probe()
|
||||
|
||||
assert result["success"] is True
|
||||
mock_cleanup.assert_called_once()
|
||||
171
controller/test/test_onboarding_api.py
Normal file
171
controller/test/test_onboarding_api.py
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
"""
|
||||
Integration tests for onboarding API endpoints.
|
||||
"""
|
||||
|
||||
from unittest import mock
|
||||
|
||||
|
||||
class TestOnboardingCandidatesEndpoint:
|
||||
"""Tests for GET /api/onboarding/candidates."""
|
||||
|
||||
def test_returns_501_in_docker_mode(self, test_client_docker_mode):
|
||||
"""Should return 501 in Docker mode."""
|
||||
response = test_client_docker_mode.get("/api/onboarding/candidates")
|
||||
assert response.status_code == 501
|
||||
|
||||
def test_returns_409_when_drive_mounted(self, test_client):
|
||||
"""Should return 409 when primary drive is already mounted."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.is_primary_drive_mounted", return_value=True,
|
||||
):
|
||||
response = test_client.get("/api/onboarding/candidates")
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_returns_candidates(self, test_client):
|
||||
"""Should return list of unmounted drives."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.is_primary_drive_mounted", return_value=False,
|
||||
), mock.patch(
|
||||
"controller.api.onboarding.get_onboarding_candidates",
|
||||
return_value=[{
|
||||
"path": "/dev/sda1", "name": "sda1", "size": "500G",
|
||||
"fstype": "ext4", "label": None, "uuid": "abc-123", "model": "SanDisk",
|
||||
}],
|
||||
):
|
||||
response = test_client.get("/api/onboarding/candidates")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert len(data) == 1
|
||||
assert data[0]["path"] == "/dev/sda1"
|
||||
|
||||
def test_returns_empty_list(self, test_client):
|
||||
"""Should return empty list when no unmounted drives."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.is_primary_drive_mounted", return_value=False,
|
||||
), mock.patch(
|
||||
"controller.api.onboarding.get_onboarding_candidates", return_value=[],
|
||||
):
|
||||
response = test_client.get("/api/onboarding/candidates")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
class TestOnboardingProbeEndpoint:
|
||||
"""Tests for POST /api/onboarding/probe."""
|
||||
|
||||
def test_returns_501_in_docker_mode(self, test_client_docker_mode):
|
||||
"""Should return 501 in Docker mode."""
|
||||
response = test_client_docker_mode.post(
|
||||
"/api/onboarding/probe",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 501
|
||||
|
||||
def test_returns_409_when_drive_mounted(self, test_client):
|
||||
"""Should return 409 when primary drive is already mounted."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.is_primary_drive_mounted", return_value=True,
|
||||
):
|
||||
response = test_client.post(
|
||||
"/api/onboarding/probe",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_probe_success_with_config(self, test_client):
|
||||
"""Should return probe result with config found."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.is_primary_drive_mounted", return_value=False,
|
||||
), mock.patch(
|
||||
"controller.api.onboarding.probe_drive",
|
||||
return_value={
|
||||
"success": True,
|
||||
"config_found": True,
|
||||
"mounts": [{"device": "/dev/sda1", "mount_point": "/media/wrolpi"}],
|
||||
"device_path": "/dev/sda1",
|
||||
"fstype": "ext4",
|
||||
},
|
||||
):
|
||||
response = test_client.post(
|
||||
"/api/onboarding/probe",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["config_found"] is True
|
||||
assert len(data["mounts"]) == 1
|
||||
|
||||
def test_probe_failure(self, test_client):
|
||||
"""Should return 500 when probe fails."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.is_primary_drive_mounted", return_value=False,
|
||||
), mock.patch(
|
||||
"controller.api.onboarding.probe_drive",
|
||||
return_value={"success": False, "error": "Mount failed"},
|
||||
):
|
||||
response = test_client.post(
|
||||
"/api/onboarding/probe",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 500
|
||||
|
||||
|
||||
class TestOnboardingCommitEndpoint:
|
||||
"""Tests for POST /api/onboarding/commit."""
|
||||
|
||||
def test_returns_501_in_docker_mode(self, test_client_docker_mode):
|
||||
"""Should return 501 in Docker mode."""
|
||||
response = test_client_docker_mode.post(
|
||||
"/api/onboarding/commit",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 501
|
||||
|
||||
def test_commit_success(self, test_client):
|
||||
"""Should return success on commit."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.commit_onboarding",
|
||||
return_value={
|
||||
"success": True,
|
||||
"mounts": ["/media/wrolpi"],
|
||||
"repair_started": True,
|
||||
},
|
||||
):
|
||||
response = test_client.post(
|
||||
"/api/onboarding/commit",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["success"] is True
|
||||
assert data["repair_started"] is True
|
||||
|
||||
def test_commit_failure(self, test_client):
|
||||
"""Should return 500 on failure."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.commit_onboarding",
|
||||
return_value={"success": False, "error": "Device busy"},
|
||||
):
|
||||
response = test_client.post(
|
||||
"/api/onboarding/commit",
|
||||
json={"device_path": "/dev/sda1", "fstype": "ext4"},
|
||||
)
|
||||
assert response.status_code == 500
|
||||
|
||||
|
||||
class TestOnboardingCancelEndpoint:
|
||||
"""Tests for POST /api/onboarding/cancel."""
|
||||
|
||||
def test_returns_501_in_docker_mode(self, test_client_docker_mode):
|
||||
"""Should return 501 in Docker mode."""
|
||||
response = test_client_docker_mode.post("/api/onboarding/cancel")
|
||||
assert response.status_code == 501
|
||||
|
||||
def test_cancel_success(self, test_client):
|
||||
"""Should return success on cancel."""
|
||||
with mock.patch(
|
||||
"controller.api.onboarding.cancel_probe",
|
||||
return_value={"success": True},
|
||||
):
|
||||
response = test_client.post("/api/onboarding/cancel")
|
||||
assert response.status_code == 200
|
||||
Loading…
Add table
Add a link
Reference in a new issue