aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_test_install.bats
blob: f339bafe7862812e23c06080ba6658f18a56c1f4 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env bats
# Unit tests for scripts/test-install.sh
#
# Coverage scope: pure-logic helpers. The VM lifecycle (start_vm,
# run_install, verify_install, run_test) shells out to qemu / ssh /
# archangel and is exercised by the integration run itself, not bats.
#
# Sourcing test-install.sh relies on the source-guard at the bottom of
# the script: when sourced, function definitions load but main is not
# called.

setup() {
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../scripts/test-install.sh"
}

#############################
# is_transient_install_failure
#############################

# Normal: a flaky-mirror failure (pacstrap marker + download error) retries.
@test "is_transient_install_failure matches a mirror download flake" {
    local log="==> Installing base system
error: failed retrieving file 'core.db' from mirror.example.org : Operation too slow
error: failed to synchronize all databases
==> ERROR: Failed to install packages to new root"
    run is_transient_install_failure "$log"
    [ "$status" -eq 0 ]
}

@test "is_transient_install_failure matches a name-resolution flake" {
    local log="error: could not resolve host: mirror.archlinux.org
==> ERROR: Failed to install packages to new root"
    run is_transient_install_failure "$log"
    [ "$status" -eq 0 ]
}

@test "is_transient_install_failure matches a connection timeout" {
    local log="error: failed retrieving file: Connection timed out
==> ERROR: Failed to install packages to new root"
    run is_transient_install_failure "$log"
    [ "$status" -eq 0 ]
}

# Error/deterministic: a real regression must NOT retry.
@test "is_transient_install_failure does not match a missing-package failure" {
    local log="error: target not found: bogus-package
==> ERROR: Failed to install packages to new root"
    run is_transient_install_failure "$log"
    [ "$status" -ne 0 ]
}

@test "is_transient_install_failure does not match a network error without the pacstrap marker" {
    # A transient blip somewhere other than base install (e.g. a later
    # pacman step) should not be treated as a pacstrap flake.
    local log="error: failed retrieving file 'extra.db' : Connection timed out
==> Configuring system"
    run is_transient_install_failure "$log"
    [ "$status" -ne 0 ]
}

@test "is_transient_install_failure does not match a clean log" {
    local log="==> Installing base system
info: Base system installed.
==> Installation complete"
    run is_transient_install_failure "$log"
    [ "$status" -ne 0 ]
}

# Boundary: empty input must not match (a timeout can leave an empty log).
@test "is_transient_install_failure does not match empty input" {
    run is_transient_install_failure ""
    [ "$status" -ne 0 ]
}

# Boundary: matching is case-insensitive on the transient indicator.
@test "is_transient_install_failure matches indicator regardless of case" {
    local log="ERROR: Failed Retrieving File from mirror : CONNECTION REFUSED
==> ERROR: Failed to install packages to new root"
    run is_transient_install_failure "$log"
    [ "$status" -eq 0 ]
}

#############################
# char_to_qemu_key
#############################

# Normal: alphanumerics map to themselves; uppercase gains a shift- prefix.
@test "char_to_qemu_key passes lowercase letters through unchanged" {
    [ "$(char_to_qemu_key a)" = "a" ]
    [ "$(char_to_qemu_key z)" = "z" ]
}

@test "char_to_qemu_key prefixes uppercase letters with shift-" {
    [ "$(char_to_qemu_key A)" = "shift-a" ]
    [ "$(char_to_qemu_key Z)" = "shift-z" ]
}

@test "char_to_qemu_key passes digits through unchanged" {
    [ "$(char_to_qemu_key 0)" = "0" ]
    [ "$(char_to_qemu_key 9)" = "9" ]
}

# Boundary: every special character in the mapping table.
@test "char_to_qemu_key maps each special character to its QEMU name" {
    while IFS='|' read -r ch want; do
        run char_to_qemu_key "$ch"
        [ "$status" -eq 0 ]
        [ "$output" = "$want" ] || {
            echo "char '$ch' => '$output', want '$want'"
            false
        }
    done <<'EOF'
 |spc
-|minus
=|equal
.|dot
,|comma
/|slash
\|backslash
;|semicolon
'|apostrophe
[|bracket_left
]|bracket_right
!|shift-1
@|shift-2
#|shift-3
$|shift-4
EOF
}

# Error/passthrough: an unmapped character comes back verbatim.
@test "char_to_qemu_key passes unmapped characters through unchanged" {
    [ "$(char_to_qemu_key '%')" = "%" ]
    [ "$(char_to_qemu_key '*')" = "*" ]
}

@test "char_to_qemu_key returns empty for empty input" {
    run char_to_qemu_key ""
    [ "$status" -eq 0 ]
    [ "$output" = "" ]
}

#############################
# get_disk_count
#############################

@test "get_disk_count returns 1 for a single-disk config" {
    local cfg="$BATS_TEST_TMPDIR/single.conf"
    printf 'DISKS=/dev/vda\n' > "$cfg"
    run get_disk_count "$cfg"
    [ "$status" -eq 0 ]
    [ "$output" = "1" ]
}

@test "get_disk_count returns 2 for a two-disk config" {
    local cfg="$BATS_TEST_TMPDIR/mirror.conf"
    printf 'DISKS=/dev/vda,/dev/vdb\n' > "$cfg"
    run get_disk_count "$cfg"
    [ "$status" -eq 0 ]
    [ "$output" = "2" ]
}

@test "get_disk_count returns 3 for a three-disk config" {
    local cfg="$BATS_TEST_TMPDIR/raidz1.conf"
    printf 'DISKS=/dev/vda,/dev/vdb,/dev/vdc\n' > "$cfg"
    run get_disk_count "$cfg"
    [ "$status" -eq 0 ]
    [ "$output" = "3" ]
}

# Boundary: the ^DISKS= anchor must not match a decoy line.
@test "get_disk_count ignores a non-anchored decoy line" {
    local cfg="$BATS_TEST_TMPDIR/decoy.conf"
    printf 'ROOT_DISKS=/dev/sda,/dev/sdb,/dev/sdc\nDISKS=/dev/vda\n' > "$cfg"
    run get_disk_count "$cfg"
    [ "$status" -eq 0 ]
    [ "$output" = "1" ]
}

# Error/characterization: a config with no DISKS= line counts as 0.
@test "get_disk_count returns 0 when no DISKS line is present" {
    local cfg="$BATS_TEST_TMPDIR/nodisks.conf"
    printf 'HOSTNAME=test\nFILESYSTEM=zfs\n' > "$cfg"
    run get_disk_count "$cfg"
    [ "$status" -eq 0 ]
    [ "$output" = "0" ]
}

#############################
# get_disk_args
#############################

@test "get_disk_args builds one -drive block for a single disk" {
    run get_disk_args 1 single
    [ "$status" -eq 0 ]
    [ "$(grep -o -- '-drive' <<<"$output" | wc -l)" -eq 1 ]
    [[ "$output" == *"test-single-disk1.qcow2"* ]]
    [[ "$output" == *"format=qcow2"* ]]
    [[ "$output" == *"if=virtio"* ]]
}

@test "get_disk_args builds one -drive block per disk for multiple disks" {
    run get_disk_args 2 mirror
    [ "$status" -eq 0 ]
    [ "$(grep -o -- '-drive' <<<"$output" | wc -l)" -eq 2 ]
    [[ "$output" == *"test-mirror-disk1.qcow2"* ]]
    [[ "$output" == *"test-mirror-disk2.qcow2"* ]]
}

# Boundary: zero disks yields no arguments.
@test "get_disk_args returns empty for a zero count" {
    run get_disk_args 0 empty
    [ "$status" -eq 0 ]
    [ "$output" = "" ]
}

#############################
# SSH_PORT override
#############################
# The hostfwd port must be overridable so a test VM can coexist with
# another VM already holding 2222 (re-sourcing applies the top-level
# assignment with the env value in scope).

@test "SSH_PORT honors a preset value" {
    SSH_PORT=3333
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../scripts/test-install.sh"
    [ "$SSH_PORT" = "3333" ]
}

@test "SSH_PORT defaults to 2222 when unset" {
    unset SSH_PORT
    # shellcheck disable=SC1091
    source "${BATS_TEST_DIRNAME}/../../scripts/test-install.sh"
    [ "$SSH_PORT" = "2222" ]
}

#############################
# port_listening_in (pure half of the port-in-use guard)
#############################
# The live ss query lives in port_in_use; this pure predicate takes an
# `ss -tln` snapshot as a string so it's testable with fixtures.

@test "port_listening_in detects a port present in ss output" {
    run port_listening_in 2222 "LISTEN 0 4096 0.0.0.0:2222 0.0.0.0:*"
    [ "$status" -eq 0 ]
}

@test "port_listening_in returns 1 when the port is absent" {
    run port_listening_in 2222 "LISTEN 0 4096 0.0.0.0:22 0.0.0.0:*"
    [ "$status" -eq 1 ]
}

@test "port_listening_in does not match a port that is only a substring" {
    run port_listening_in 2222 "LISTEN 0 4096 0.0.0.0:12222 0.0.0.0:*"
    [ "$status" -eq 1 ]
}

@test "port_listening_in matches an IPv6 listener" {
    run port_listening_in 2222 "LISTEN 0 4096 [::]:2222 [::]:*"
    [ "$status" -eq 0 ]
}

@test "port_listening_in returns 1 on empty ss output" {
    run port_listening_in 2222 ""
    [ "$status" -eq 1 ]
}

#############################
# is_archzfs_cache_corruption
#############################
# Recognizes the stale-archzfs-in-pacoloco failure (not transient — a retry
# hits the same cached file), so the caller prints a cache-clear hint.

@test "is_archzfs_cache_corruption matches an archzfs checksum corruption" {
    local log="==> Installing base system
:: File /mnt/var/cache/pacman/pkg/zfs-utils-2.4.2-2-x86_64.pkg.tar.zst is corrupted (invalid or corrupted package (checksum)).
error: failed to commit transaction (invalid or corrupted package (checksum))
==> ERROR: Failed to install packages to new root"
    run is_archzfs_cache_corruption "$log"
    [ "$status" -eq 0 ]
}

@test "is_archzfs_cache_corruption ignores a transient mirror flake" {
    local log="error: failed retrieving file 'core.db' : Operation too slow
==> ERROR: Failed to install packages to new root"
    run is_archzfs_cache_corruption "$log"
    [ "$status" -eq 1 ]
}

@test "is_archzfs_cache_corruption ignores corruption of a non-archzfs package" {
    local log="==> ERROR: Failed to install packages to new root
:: File /mnt/var/cache/pacman/pkg/glibc-2.43-1-x86_64.pkg.tar.zst is corrupted (invalid or corrupted package (checksum))."
    run is_archzfs_cache_corruption "$log"
    [ "$status" -eq 1 ]
}

@test "is_archzfs_cache_corruption returns 1 on a clean log" {
    run is_archzfs_cache_corruption ""
    [ "$status" -eq 1 ]
}