aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_archangel.bats
blob: c31cebb4cbe047155c53b1f4585d849f604d984c (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env bats
# Unit tests for the installer/archangel monolith.
#
# Coverage scope: gather_input() in unattended mode — the validation
# of required config values, defaulting of optional ones, and the
# filesystem-specific encryption checks. The interactive branch
# (everything reachable via `if [[ "$UNATTENDED" != true ]]`) is not
# unit-tested per the project's testing-strategy.org policy on
# fzf / arch-chroot / mkfs / cryptsetup wrappers.
#
# Sourcing archangel relies on the source-guard at the bottom of
# the script: when sourced, function definitions load but main is
# not called, init_logging is not run (so /tmp/archangel-*.log is
# not created), and the banner is not printed.

setup() {
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../installer/archangel"
    UNATTENDED=true
}

#############################
# Required-field validation
#############################

@test "gather_input unattended errors when HOSTNAME is missing" {
    HOSTNAME=""
    TIMEZONE=UTC
    ROOT_PASSWORD=secret
    SELECTED_DISKS=(/dev/sda)
    NO_ENCRYPT=yes
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"HOSTNAME"* ]]
}

@test "gather_input unattended errors when TIMEZONE is missing" {
    HOSTNAME=h
    TIMEZONE=""
    ROOT_PASSWORD=secret
    SELECTED_DISKS=(/dev/sda)
    NO_ENCRYPT=yes
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"TIMEZONE"* ]]
}

@test "gather_input unattended errors when ROOT_PASSWORD is missing" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=""
    SELECTED_DISKS=(/dev/sda)
    NO_ENCRYPT=yes
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"ROOT_PASSWORD"* ]]
}

@test "gather_input unattended errors when SELECTED_DISKS is empty" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=secret
    SELECTED_DISKS=()
    NO_ENCRYPT=yes
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"DISKS"* ]]
}

#############################
# Optional-field defaults
#############################

@test "gather_input unattended defaults FILESYSTEM to zfs when empty" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=""
    NO_ENCRYPT=yes
    gather_input >/dev/null
    [ "$FILESYSTEM" = "zfs" ]
}

@test "gather_input unattended defaults LOCALE, KEYMAP, ENABLE_SSH when empty" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=zfs
    NO_ENCRYPT=yes
    LOCALE=""
    KEYMAP=""
    ENABLE_SSH=""
    gather_input >/dev/null
    [ "$LOCALE" = "en_US.UTF-8" ]
    [ "$KEYMAP" = "us" ]
    [ "$ENABLE_SSH" = "yes" ]
}

@test "gather_input unattended preserves explicit non-default values" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=btrfs
    NO_ENCRYPT=yes
    LOCALE="en_GB.UTF-8"
    KEYMAP="dvorak"
    ENABLE_SSH="no"
    gather_input >/dev/null
    [ "$FILESYSTEM" = "btrfs" ]
    [ "$LOCALE" = "en_GB.UTF-8" ]
    [ "$KEYMAP" = "dvorak" ]
    [ "$ENABLE_SSH" = "no" ]
}

#############################
# Filesystem-specific encryption validation
#############################

@test "gather_input unattended errors when ZFS without ZFS_PASSPHRASE and encryption on" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=zfs
    NO_ENCRYPT=no
    ZFS_PASSPHRASE=""
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"ZFS_PASSPHRASE"* ]]
}

@test "gather_input unattended errors when Btrfs without LUKS_PASSPHRASE and encryption on" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=btrfs
    NO_ENCRYPT=no
    LUKS_PASSPHRASE=""
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"LUKS_PASSPHRASE"* ]]
}

@test "gather_input unattended accepts ZFS with NO_ENCRYPT=yes and no passphrase" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=zfs
    NO_ENCRYPT=yes
    ZFS_PASSPHRASE=""
    run gather_input
    [ "$status" -eq 0 ]
}

#############################
# Filesystem validity
#############################

@test "gather_input unattended errors when FILESYSTEM is neither zfs nor btrfs" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=ext4
    NO_ENCRYPT=yes
    run gather_input
    [ "$status" -eq 1 ]
    [[ "$output" == *"Invalid FILESYSTEM"* ]]
}

#############################
# RAID-level defaulting
#############################

@test "gather_input unattended defaults RAID_LEVEL to mirror for multi-disk install" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda /dev/sdb)
    FILESYSTEM=zfs
    NO_ENCRYPT=yes
    RAID_LEVEL=""
    gather_input >/dev/null
    [ "$RAID_LEVEL" = "mirror" ]
}

@test "gather_input unattended preserves an explicit RAID_LEVEL on multi-disk install" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda /dev/sdb /dev/sdc)
    FILESYSTEM=zfs
    NO_ENCRYPT=yes
    RAID_LEVEL="raidz1"
    gather_input >/dev/null
    [ "$RAID_LEVEL" = "raidz1" ]
}

@test "gather_input unattended leaves RAID_LEVEL empty for single-disk install" {
    HOSTNAME=h
    TIMEZONE=UTC
    ROOT_PASSWORD=x
    SELECTED_DISKS=(/dev/sda)
    FILESYSTEM=zfs
    NO_ENCRYPT=yes
    RAID_LEVEL=""
    gather_input >/dev/null
    [ -z "$RAID_LEVEL" ]
}