summaryrefslogtreecommitdiff
path: root/tests/setup_disk_test
blob: 7418c8987132861a0eb12369064538be6bd6c506 (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
303
304
#!/usr/bin/env atf-sh

. $(atf_get_srcdir)/test_env.sh
init_tests \
	setup_disk_usage \
	setup_disk_mode_none \
	setup_disk_none \
	setup_disk_func_find_disks \
	setup_disk_func_setup_partitions_dos \
	setup_disk_func_setup_partitions_gpt \
	setup_disk_func_find_efi_size \
	setup_disk_func_uuid_or_device \
	setup_disk_non_existing_block_dev \
	setup_disk_install_mounted_root_nvme \
	setup_disk_install_mounted_root_efi_boot \
	setup_disk_install_mounted_root_bootloader_none

setup_disk_usage_body() {
	test_usage setup-disk
}

setup_disk_mode_none_body() {
	init_env
	atf_check -s exit:0 \
		setup-disk -m none
}

setup_disk_none_body() {
	init_env
	atf_check -s exit:0 \
		setup-disk none
}

fake_disk() {
	local dev="$1"
	local size="${2:-1000}" # size in MiB
	local blocksize="${3:-512}"

	mkdir -p dev \
		sys/block/$dev/device \
		sys/block/$dev/holders \
		sys/block/$dev/queue

	echo $(( $size * 1000000 / $blocksize )) > sys/block/$dev/size
	echo $(( $blocksize )) > sys/block/$dev/queue/logical_block_size
}


fake_partition() {
	mkdir -p sys/block/$1/$1$2/holders
}

fake_mount() {
	mkdir -p proc
	echo "$1" >> proc/mounts
}

fake_raid() {
	local md="$1"
	shift
	for dev; do
		for p in sys/block/*/$dev/holders sys/block/$dev/holders; do
			if [ -d "$p" ]; then
				touch "$p"/$md
			fi
		done
	done
}

setup_disk_func_find_disks_body() {
	init_env
	fake_disk vda
	fake_disk vdb
	fake_partition vdb 1
	fake_disk sda
	fake_disk nvme0n1
	fake_partition nvme0n1 p2

	# simulate vda and vdb1 being part of md0 raid
	fake_raid md0 vda vdb1

	# simulate nvme0n1p2 being mounted
	fake_mount '/dev/nvme0n1p2 /boot vfat rw,relatime,fmask=0022 0 0'

	SETUP_DISK_TESTFUNC=find_disks USE_RAID=1 \
		atf_check -s exit:0 \
		-o match:"^ sda$" \
		-e match:"vda is part of a running raid" \
		-e match:"vdb is part of a running raid" \
		setup-disk
}

setup_disk_func_setup_partitions_dos_body() {
	atf_require_prog sfdisk
	init_env

	mkdir -p dev
	truncate -s 100M dev/vda

	SETUP_DISK_TESTFUNC=setup_partitions  \
		atf_check -s exit:0 \
		-o match:"mdev" \
		setup-disk dev/vda "0M," "10M,83,*", "10M,82", "10M,fd", ",8e"

	SETUP_DISK_TESTFUNC=find_partitions  \
		atf_check -s exit:0 \
		-o match:"dev/vda1" \
		setup-disk dev/vda "boot"

	SETUP_DISK_TESTFUNC=find_partitions  \
		atf_check -s exit:0 \
		-o match:"dev/vda1" \
		setup-disk dev/vda "linux"

	SETUP_DISK_TESTFUNC=find_partitions  \
		atf_check -s exit:0 \
		-o match:"dev/vda2" \
		setup-disk dev/vda "swap"

	SETUP_DISK_TESTFUNC=find_partitions  \
		atf_check -s exit:0 \
		-o match:"dev/vda3" \
		setup-disk dev/vda "raid"

	SETUP_DISK_TESTFUNC=find_partitions  \
		atf_check -s exit:0 \
		-o match:"dev/vda4" \
		setup-disk dev/vda "lvm"
}

setup_disk_func_setup_partitions_gpt_body() {
	atf_require_prog sfdisk
	init_env

	mkdir -p dev
	truncate -s 200M dev/vda

	swap=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
	linux=0FC63DAF-8483-4772-8E79-3D69D8477DE4
	raid=A19D880F-05FC-4D3B-A006-743F0F84911E
	lvm=E6D6D379-F507-44C2-A23C-238F2A3DF928
	prep=9E1A2d38-C612-4316-AA26-8B49521E5A8B
	esp=C12A7328-F81F-11D2-BA4B-00A0C93EC93B

	SETUP_DISK_TESTFUNC=setup_partitions DISKLABEL=gpt \
		atf_check -s exit:0 \
		-o match:"mdev" \
		setup-disk dev/vda "0M," "30M,$esp" "30M,$swap" "30M,$linux" "30M,$raid" "30M,$lvm"

	sfdisk -d dev/vda

	SETUP_DISK_TESTFUNC=find_partitions DISKLABEL=gpt USE_EFI=1 \
		atf_check -s exit:0 \
		-o match:"dev/vda1" \
		setup-disk dev/vda "boot"

	SETUP_DISK_TESTFUNC=find_partitions DISKLABEL=gpt \
		atf_check -s exit:0 \
		-o match:"dev/vda2" \
		setup-disk dev/vda "swap"

	SETUP_DISK_TESTFUNC=find_partitions DISKLABEL=gpt \
		atf_check -s exit:0 \
		-o match:"dev/vda3" \
		setup-disk dev/vda "linux"

	SETUP_DISK_TESTFUNC=find_partitions DISKLABEL=gpt \
		atf_check -s exit:0 \
		-o match:"dev/vda4" \
		setup-disk dev/vda "raid"

	SETUP_DISK_TESTFUNC=find_partitions DISKLABEL=gpt \
		atf_check -s exit:0 \
		-o match:"dev/vda5" \
		setup-disk dev/vda "lvm"
}

setup_disk_func_find_efi_size_body() {
	init_env
	# size:blocksize:expected
	for i in 10000::512 4000::264 1000::160 \
			10000:4096:512 4000:4096:264 1000:4096:264; do

		local diskdata=${i%:*}
		local disksize=${diskdata%:*}
		local blocksize=${diskdata#*:}
		local expected=${i##*:}

		fake_disk vda $disksize $blocksize
		SETUP_DISK_TESTFUNC=find_efi_size \
			atf_check -s exit:0 \
			-o match:"^$expected$" \
			setup-disk /dev/vda
	done
}

setup_disk_func_uuid_or_device_body() {
	init_env
	fake_bin blkid <<-EOF
		#!/bin/sh
		cat <<-EOF2
			/dev/sda3: UUID="4ec0992a-bf5a-4d04-bc49-06c131ae901b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="0ce794d3-03"
			/dev/sda1: UUID="1db12961-31ce-420f-94f0-559a1f6592ff" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="0ce794d3-01"
			/dev/sda2: UUID="a5fbbd70-94f6-4d03-947c-e3afb91a32c8" TYPE="swap" PARTUUID="0ce794d3-02"
			/dev/md0: UUID="86a00eb9-9b5a-4d73-812c-8fc958673a7c" TYPE="LVM2_member"
		EOF2
	EOF

	SETUP_DISK_TESTFUNC=uuid_or_device \
		atf_check -s exit:0 \
			-o match:"/dev/vda1" \
			setup-disk /dev/vda1

	SETUP_DISK_TESTFUNC=uuid_or_device \
		atf_check -s exit:0 \
			-o not-match:"UUID=" \
			-o match:"/dev/md0" \
			setup-disk /dev/md0

	SETUP_DISK_TESTFUNC=uuid_or_device \
		atf_check -s exit:0 \
			-o match:"UUID=1db12961-31ce-420f-94f0-559a1f6592ff" \
			setup-disk /dev/sda1

	SETUP_DISK_TESTFUNC=uuid_or_device \
	NO_DISK_UUID="/dev/sda1 /dev/sda3" \
		atf_check -s exit:0 \
			-o not-match:"UUID" \
			-o match:"/dev/sda1" \
			setup-disk /dev/sda1

}

setup_disk_non_existing_block_dev_body() {
	init_env
	atf_check -s not-exit:0 \
		-e match:"/dev/vda is not a block device suitable for partitioning" \
		-o match:"swapoff" \
		setup-disk -m sys /dev/vda
}

setup_disk_install_mounted_root_nvme_body() {
	init_env
	mkdir -p target/boot/efi \
		sys/firmware/efi

	# simulate nvme0n1p2 being mounted
	fake_mount "/dev/nvme0n1p2 $PWD/target ext4 rw,noatime,data=ordered 0 0"
	fake_mount "/dev/nvme0n1p3 $PWD/target/boot/efi vfat rw,relatime,fmask=0022 0 0"

	atf_check -s exit:0 \
		-o match:"Installing system on /dev/nvme0n1p2" \
		-o match:"grub-install .* --efi-directory=$PWD/target/boot/efi .* --boot-directory=$PWD/target/boot" \
		-o match:"install -D $PWD/target/boot/efi/EFI/alpine/grub.*efi $PWD/target/boot/efi/EFI/boot/bootaa64.efi" \
		setup-disk target

	atf_check \
		-o match:"/dev/nvme0n1p2	/	ext4	rw,noatime,data=ordered 0 1" \
		-o match:"/dev/nvme0n1p3	/boot/efi	vfat	rw,relatime,fmask=0022 0 2" \
		cat target/etc/fstab

	atf_check -o match:"GRUB_CMDLINE_LINUX_DEFAULT=.*ext4,nvme" \
		cat target/etc/default/grub

}

setup_disk_install_mounted_root_efi_boot_body() {
	init_env
	mkdir -p target/boot \
		sys/firmware/efi

	fake_mount "/dev/vda2 $PWD/target ext4 rw,noatime,data=ordered 0 0"
	fake_mount "/dev/vda1 $PWD/target/boot vfat rw,relatime,fmask=0022 0 0"

	atf_check -s exit:0 \
		-o match:"Installing system on /dev/vda2" \
		-o match:"grub-install .* --efi-directory=$PWD/target/boot .* --boot-directory=$PWD/target/boot" \
		-o match:"install -D $PWD/target/boot/EFI/alpine/grub.*efi $PWD/target/boot/EFI/boot/bootaa64.efi" \
		setup-disk -v target

	atf_check \
		-o match:"/dev/vda2	/	ext4	rw,noatime,data=ordered 0 1" \
		-o match:"/dev/vda1	/boot	vfat	rw,relatime,fmask=0022 0 2" \
		cat target/etc/fstab

	atf_check -o match:"GRUB_CMDLINE_LINUX_DEFAULT=.*ext4" \
		cat target/etc/default/grub
}

setup_disk_install_mounted_root_bootloader_none_body() {
	init_env
	mkdir -p target/boot \
		sys/firmware/efi

	fake_mount "/dev/vda2 $PWD/target virtiofs rw,noatime,data=ordered 0 0"

	BOOTLOADER=none atf_check -s exit:0 \
		-o not-match:"grub" \
		setup-disk -v target

	if [ -f target/etc/default/grub ]; then
		atf_fail "etc/default/grub should not exist"
	fi
}