aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_btrfs.bats
blob: 15bf141d4dbfcda21b475f614fbe616c1846c370 (plain)
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
#!/usr/bin/env bats
# Unit tests for installer/lib/btrfs.sh
#
# Coverage scope: pure helpers only. Most of btrfs.sh wraps cryptsetup,
# mkfs.btrfs, snapper, grub-install, and arch-chroot — all deliberately
# VM-tested per the project's testing-strategy.org policy. Only
# get_luks_devices is covered here.

setup() {
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../installer/lib/common.sh"
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../installer/lib/btrfs.sh"
}

#############################
# get_luks_devices
#############################
# Asymmetric naming: index 0 uses the bare LUKS_MAPPER_NAME (no
# suffix), subsequent indices append the index. Tests pin both the
# bare-first behavior and the suffix-on-rest behavior.

@test "get_luks_devices: count=1 emits the bare-named mapper device" {
    LUKS_MAPPER_NAME="cryptroot"
    run get_luks_devices 1
    [ "$status" -eq 0 ]
    [ "$output" = "/dev/mapper/cryptroot" ]
}

@test "get_luks_devices: count=3 emits bare name + suffixed entries" {
    LUKS_MAPPER_NAME="cryptroot"
    run get_luks_devices 3
    [ "$status" -eq 0 ]
    [ "$output" = "/dev/mapper/cryptroot /dev/mapper/cryptroot1 /dev/mapper/cryptroot2" ]
}

@test "get_luks_devices: count=0 emits empty output" {
    LUKS_MAPPER_NAME="cryptroot"
    run get_luks_devices 0
    [ "$status" -eq 0 ]
    [ -z "$output" ]
}

@test "get_luks_devices: count=5 emits five entries with correct suffix progression" {
    LUKS_MAPPER_NAME="cryptroot"
    run get_luks_devices 5
    [ "$status" -eq 0 ]
    [ "$output" = "/dev/mapper/cryptroot /dev/mapper/cryptroot1 /dev/mapper/cryptroot2 /dev/mapper/cryptroot3 /dev/mapper/cryptroot4" ]
}

@test "get_luks_devices: non-numeric count is treated as zero (no crash)" {
    LUKS_MAPPER_NAME="cryptroot"
    run get_luks_devices abc
    [ "$status" -eq 0 ]
    [ -z "$output" ]
}

#############################
# parse_btrfs_subvol_opts
#############################
# Composes the mount-option string for one subvolume from the shared
# BTRFS_OPTS plus the per-subvol extra flags. Pure string transform,
# shared by mount_btrfs_subvolumes and generate_btrfs_fstab. BTRFS_OPTS
# is set at the top of btrfs.sh (sourced in setup), so these pin behavior
# against the real default option string.

@test "parse_btrfs_subvol_opts: no extra flags keeps the default opts" {
    run parse_btrfs_subvol_opts "@home" ""
    [ "$status" -eq 0 ]
    [ "$output" = "subvol=@home,noatime,compress=zstd,space_cache=v2,discard=async" ]
}

@test "parse_btrfs_subvol_opts: compress=no drops compress=zstd" {
    run parse_btrfs_subvol_opts "@media" "compress=no"
    [ "$output" = "subvol=@media,noatime,space_cache=v2,discard=async" ]
}

@test "parse_btrfs_subvol_opts: nodatacow adds nodatacow and drops compress=zstd" {
    run parse_btrfs_subvol_opts "@vms" "nodatacow"
    [ "$output" = "subvol=@vms,noatime,space_cache=v2,discard=async,nodatacow" ]
}

@test "parse_btrfs_subvol_opts: nosuid adds nosuid,nodev and keeps compression" {
    run parse_btrfs_subvol_opts "@tmp" "nosuid"
    [ "$output" = "subvol=@tmp,noatime,compress=zstd,space_cache=v2,discard=async,nosuid,nodev" ]
}

@test "parse_btrfs_subvol_opts: nodatacow and nosuid combine" {
    run parse_btrfs_subvol_opts "@x" "nodatacow,nosuid"
    [ "$output" = "subvol=@x,noatime,space_cache=v2,discard=async,nodatacow,nosuid,nodev" ]
}