File: test-setup.sh

package info (click to toggle)
genimage 19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 988 kB
  • sloc: ansic: 5,606; sh: 1,132; makefile: 194
file content (123 lines) | stat: -rw-r--r-- 2,899 bytes parent folder | download
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
#!/bin/bash

testdir="$(readlink -f $(dirname "${0}"))"
genimage="$(pwd)/genimage"

PATH="$PATH:/sbin:/usr/sbin"

set -- -v "$@"


. "${testdir}/sharness.sh"

filelist_orig="$(pwd)/file-list.orig"
filelist_test="$(pwd)/file-list.test"
root_orig="$(pwd)/root.orig"
root_test="$(pwd)/root.test"

setup_data() {
	umask 0022
	mkdir -p "${root_orig}"/{foo,bar,baz,"with spaces"}/{1,2,3}
	touch "${root_orig}"/{foo,bar,baz,"with spaces"}/{1,2,3}/{one,two}
	find "${root_orig}" -print0 | xargs -0 touch -c -d "2011-11-11 UTC"
	find "${root_orig}"/ -mindepth 1 -printf "%P\n" | sort  > "${filelist_orig}"

	cp "${testdir}"/*.conf* "${testdir}"/*.sh .
}

run_genimage_impl() {
	if [ ! -e "${1}" ]; then
		echo "ERROR: genimage config file '${1}' missing!"
		return 130
	fi
	if [ "$verbose" = "t" ]; then
		vargs="--loglevel=3"
	fi
	rm -rf tmp images "${root_test}"
	mkdir "${root_test}" images
	if [ -n "${2}" ]; then
		# create a larger output image to make sure it is recreated properly
		dd if=/dev/zero of="images/${2}" bs=1M seek=30 count=1
	fi
	"${genimage}" \
		${vargs} \
		--outputpath=images \
		--inputpath=input \
		--rootpath="${root}" \
		--tmppath=tmp \
		${extra_opts} \
		--config "${1}"
}

run_genimage_root() {
	root="root.orig" run_genimage_impl "${@}"
}

run_genimage() {
	root="/this/directory/does/not/exist" run_genimage_impl "${@}"
}

get_size() {
	local file="${1}"
	if [ ! -f "${file}" ]; then
		echo "Failed to check file size: '${file}' does not exist!"
		return 1
	fi
	set -- $(du -b "${file}")
	size="${1}"
}

check_size_range() {
	local size
	get_size "${1}" || return
	if [ "${size}" -lt "${2}" -o "${size}" -gt "${3}" ]; then
		echo "Incorrect file size for '${1}': expected min: ${2} max: ${3} found: ${size}"
		return 1
	fi
}

check_size() {
	local size
	get_size "${1}" || return
	if [ "${size}" -ne "${2}" ]; then
		echo "Incorrect file size for '${1}': expected: ${2} found: ${size}"
		return 1
	fi
}

sfdisk_validate() {
	if [ -n "$(sfdisk -q -V "${1}" 2>&1 | grep -v unallocated)" ]; then
		echo "'sfdisk -V' failed with:"
		sfdisk -V "${1}" 2>&1
		return 1
	fi
}

setup_test_images() {
	rm -rf input &&
	mkdir input &&
	dd if=/dev/zero of=input/part1.img bs=512 count=7 &&
	dd if=/dev/zero of=input/part2.img bs=512 count=11 &&
	touch input/part3.img
}

sanitized_fdisk_sfdisk() {
	# check the disk identifier
	fdisk -l "${1}" | grep identifier: &&
	# check partitions; filter output to handle different sfdisk versions
	sfdisk -d "${1}" 2>/dev/null | grep '^images/' | \
		sed -e 's/  *//g' -e 's;Id=;type=;'
}

exec_test_set_prereq() {
	command -v "${1}" > /dev/null && test_set_prereq "${1/./_}"
}

set -o pipefail

setup_data

sfdisk -h | grep -q gpt && test_set_prereq sfdisk-gpt
fdisk -h | grep -q gpt && test_set_prereq fdisk-gpt
# make sure mke2fs supports '-d root-directory'
[ "$(mke2fs |& sed -n 's/.*\(-d \).*/\1/p')" = "-d " ] && test_set_prereq mke2fs