aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_disk.bats
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-04-26 18:31:35 -0500
committerCraig Jennings <c@cjennings.net>2026-04-26 18:31:35 -0500
commitf02ba03c12599bdd61b5208c9a65f458ae05aa15 (patch)
tree249a2e5eed52aaaa5cc2c9ca57d549a21560c8c6 /tests/unit/test_disk.bats
parent6de9f378cf52b9e9b0e89b396a12b978700241ff (diff)
downloadarchangel-f02ba03c12599bdd61b5208c9a65f458ae05aa15.tar.gz
archangel-f02ba03c12599bdd61b5208c9a65f458ae05aa15.zip
refactor: unify partition_disks across ZFS and Btrfs install paths
The monolith's partition_disks() at installer/archangel was ZFS-only and silently shadowed lib/disk.sh:partition_disks(), which had been dead code since the Btrfs install path was added. install_btrfs was assembling partitioning manually via partition_disk (singular) plus a separate format_efi_partitions call. Two parallel implementations meant fixes had to land in two places and the lib version drifted with no visible warning. The unified partition_disks now lives in lib/disk.sh. It reads SELECTED_DISKS, dispatches the per-disk layout on FILESYSTEM (BF00 for ZFS, 8300 for Btrfs), populates EFI_PARTS + ROOT_PARTS, and formats each EFI partition with EFI0, EFI1, ... labels. Folded in two pre-existing divergences while consolidating. wipefs -af now runs on every disk, not just the ZFS path. The Btrfs path was missing this defense against non-GPT signatures (LVM, mdadm, ext) that sgdisk --zap-all alone won't touch. EFI labels standardized on EFI0, EFI1, ... across both paths. The lib version was producing asymmetric EFI / EFI2 labels. No consumer in the repo references the labels after format, so that change is cosmetic. ZFS_PARTS renamed to ROOT_PARTS for symmetry with get_root_partition. Deleted four orphaned helpers: format_efi, format_efi_partitions (only caller was the collapsed install_btrfs section), get_efi_partitions, get_root_partitions (test-only callers after install_btrfs simplified). Verified end to end: bats 134/134, make test-install passing 12/12 configs across both install paths.
Diffstat (limited to 'tests/unit/test_disk.bats')
-rw-r--r--tests/unit/test_disk.bats190
1 files changed, 146 insertions, 44 deletions
diff --git a/tests/unit/test_disk.bats b/tests/unit/test_disk.bats
index f4e6929..1b3cfba 100644
--- a/tests/unit/test_disk.bats
+++ b/tests/unit/test_disk.bats
@@ -1,11 +1,13 @@
#!/usr/bin/env bats
# Unit tests for installer/lib/disk.sh
#
-# Coverage scope: pure partition-path helpers only. Side-effecting
-# functions (partition_disk, partition_disks, format_efi,
-# format_efi_partitions, select_disks) wrap sgdisk / mkfs.fat /
-# partprobe / fzf and are validated by VM integration per the
-# project's testing-strategy.org policy.
+# Coverage scope: pure partition-path helpers (get_efi_partition,
+# get_root_partition) plus the partition_disks orchestration shape
+# (which globals get populated, which destructive tools get invoked,
+# how often). The destructive tools themselves (sgdisk, wipefs,
+# partprobe, mkfs.fat) and the interactive select_disks (fzf) are
+# validated by VM integration per the project's testing-strategy.org
+# policy.
setup() {
# shellcheck disable=SC1091
@@ -75,58 +77,158 @@ setup() {
}
#############################
-# get_efi_partitions
+# partition_disks (orchestration)
#############################
+# partition_disks reads SELECTED_DISKS, dispatches the per-disk
+# layout (sgdisk + wipefs + mkfs.fat) on FILESYSTEM, and populates
+# EFI_PARTS + ROOT_PARTS for downstream callers (create_zfs_pool,
+# btrfs_open_encryption, sync_efi_partitions, fstab generation).
+#
+# The destructive system tools (sgdisk, wipefs, partprobe, mkfs.fat)
+# are mocked to capture invocation shape only — actual partition
+# behavior is validated by VM integration per testing-strategy.org.
-@test "get_efi_partitions: two SATA disks emit two suffixed partitions" {
- run get_efi_partitions /dev/sda /dev/sdb
- [ "$status" -eq 0 ]
- [ "$output" = "/dev/sda1
-/dev/sdb1" ]
+partition_disks_setup() {
+ SELECTED_DISKS=()
+ EFI_PARTS=()
+ ROOT_PARTS=()
+ CALLS=()
+ sgdisk() { CALLS+=("sgdisk $*"); return 0; }
+ wipefs() { CALLS+=("wipefs $*"); return 0; }
+ partprobe() { CALLS+=("partprobe $*"); return 0; }
+ mkfs.fat() { CALLS+=("mkfs.fat $*"); return 0; }
+ sleep() { :; }
+ info() { :; }
+ step() { :; }
+ error() { CALLS+=("error $*"); return 1; }
}
-@test "get_efi_partitions: mixed SATA + NVMe gets correct per-disk suffix" {
- run get_efi_partitions /dev/sda /dev/nvme0n1
- [ "$status" -eq 0 ]
- [ "$output" = "/dev/sda1
-/dev/nvme0n1p1" ]
+@test "partition_disks: populates EFI_PARTS and ROOT_PARTS for single SATA disk" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda)
+ FILESYSTEM=zfs
+
+ partition_disks
+
+ [ "${#EFI_PARTS[@]}" -eq 1 ]
+ [ "${#ROOT_PARTS[@]}" -eq 1 ]
+ [ "${EFI_PARTS[0]}" = "/dev/sda1" ]
+ [ "${ROOT_PARTS[0]}" = "/dev/sda2" ]
}
-@test "get_efi_partitions: single NVMe disk emits one p1 partition" {
- run get_efi_partitions /dev/nvme0n1
- [ "$status" -eq 0 ]
- [ "$output" = "/dev/nvme0n1p1" ]
+@test "partition_disks: NVMe disk gets p1/p2 suffixes in globals" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/nvme0n1)
+ FILESYSTEM=zfs
+
+ partition_disks
+
+ [ "${EFI_PARTS[0]}" = "/dev/nvme0n1p1" ]
+ [ "${ROOT_PARTS[0]}" = "/dev/nvme0n1p2" ]
}
-@test "get_efi_partitions: three disks emit three lines in order" {
- run get_efi_partitions /dev/sda /dev/sdb /dev/sdc
- [ "$status" -eq 0 ]
- local lines
- lines=$(echo "$output" | wc -l)
- [ "$lines" -eq 3 ]
+@test "partition_disks: multi-disk SATA populates both arrays in order" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda /dev/sdb /dev/sdc)
+ FILESYSTEM=zfs
+
+ partition_disks
+
+ [ "${#EFI_PARTS[@]}" -eq 3 ]
+ [ "${#ROOT_PARTS[@]}" -eq 3 ]
+ [ "${EFI_PARTS[0]}" = "/dev/sda1" ]
+ [ "${EFI_PARTS[1]}" = "/dev/sdb1" ]
+ [ "${EFI_PARTS[2]}" = "/dev/sdc1" ]
+ [ "${ROOT_PARTS[0]}" = "/dev/sda2" ]
+ [ "${ROOT_PARTS[1]}" = "/dev/sdb2" ]
+ [ "${ROOT_PARTS[2]}" = "/dev/sdc2" ]
}
-#############################
-# get_root_partitions
-#############################
+@test "partition_disks: mixed SATA + NVMe applies correct suffix per disk" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda /dev/nvme0n1)
+ FILESYSTEM=zfs
-@test "get_root_partitions: two SATA disks emit two suffix-2 partitions" {
- run get_root_partitions /dev/sda /dev/sdb
- [ "$status" -eq 0 ]
- [ "$output" = "/dev/sda2
-/dev/sdb2" ]
+ partition_disks
+
+ [ "${EFI_PARTS[0]}" = "/dev/sda1" ]
+ [ "${EFI_PARTS[1]}" = "/dev/nvme0n1p1" ]
+ [ "${ROOT_PARTS[0]}" = "/dev/sda2" ]
+ [ "${ROOT_PARTS[1]}" = "/dev/nvme0n1p2" ]
}
-@test "get_root_partitions: mixed SATA + NVMe applies correct suffix per disk" {
- run get_root_partitions /dev/sda /dev/nvme0n1
- [ "$status" -eq 0 ]
- [ "$output" = "/dev/sda2
-/dev/nvme0n1p2" ]
+@test "partition_disks: ZFS dispatch passes BF00 root type to sgdisk" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda)
+ FILESYSTEM=zfs
+
+ partition_disks
+
+ [[ " ${CALLS[*]} " == *"sgdisk -n 2:0:0 -t 2:BF00"* ]]
}
-@test "get_root_partitions: NVMe array gets p2 suffix on each entry" {
- run get_root_partitions /dev/nvme0n1 /dev/nvme1n1
- [ "$status" -eq 0 ]
- [ "$output" = "/dev/nvme0n1p2
-/dev/nvme1n1p2" ]
+@test "partition_disks: Btrfs dispatch passes 8300 root type to sgdisk" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda)
+ FILESYSTEM=btrfs
+
+ partition_disks
+
+ [[ " ${CALLS[*]} " == *"sgdisk -n 2:0:0 -t 2:8300"* ]]
+}
+
+@test "partition_disks: invokes wipefs -af on each disk" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda /dev/sdb)
+ FILESYSTEM=zfs
+
+ partition_disks
+
+ local count
+ count=$(printf '%s\n' "${CALLS[@]}" | grep -c '^wipefs -af ')
+ [ "$count" -eq 2 ]
+ [[ " ${CALLS[*]} " == *"wipefs -af /dev/sda"* ]]
+ [[ " ${CALLS[*]} " == *"wipefs -af /dev/sdb"* ]]
+}
+
+@test "partition_disks: invokes mkfs.fat once per EFI partition with EFI<i> label" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda /dev/sdb)
+ FILESYSTEM=zfs
+
+ partition_disks
+
+ local count
+ count=$(printf '%s\n' "${CALLS[@]}" | grep -c '^mkfs.fat ')
+ [ "$count" -eq 2 ]
+ [[ " ${CALLS[*]} " == *"mkfs.fat -F32 -n EFI0 /dev/sda1"* ]]
+ [[ " ${CALLS[*]} " == *"mkfs.fat -F32 -n EFI1 /dev/sdb1"* ]]
+}
+
+@test "partition_disks: re-initializes globals on second call" {
+ partition_disks_setup
+ SELECTED_DISKS=(/dev/sda /dev/sdb)
+ FILESYSTEM=zfs
+
+ partition_disks
+ [ "${#EFI_PARTS[@]}" -eq 2 ]
+
+ SELECTED_DISKS=(/dev/sdc)
+ partition_disks
+
+ [ "${#EFI_PARTS[@]}" -eq 1 ]
+ [ "${#ROOT_PARTS[@]}" -eq 1 ]
+ [ "${EFI_PARTS[0]}" = "/dev/sdc1" ]
+}
+
+@test "partition_disks: empty SELECTED_DISKS calls error and skips work" {
+ partition_disks_setup
+ SELECTED_DISKS=()
+ FILESYSTEM=zfs
+
+ partition_disks || true
+
+ [[ " ${CALLS[*]} " == *"error partition_disks: SELECTED_DISKS is empty"* ]]
+ [[ " ${CALLS[*]} " != *"sgdisk"* ]]
+ [[ " ${CALLS[*]} " != *"wipefs"* ]]
}