summaryrefslogtreecommitdiff
path: root/test/cases/libremakepkg.bats
blob: 985d6f6cbd87e184f9bf6dcb0b7092af015f3ea1 (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
load ../lib/common

shopt -s extglob

setup() {
	common_setup

	cat >> "$XDG_CONFIG_HOME/pacman/makepkg.conf" <<-eot
		unset PKGDEST SRCPKGDEST
		eot
}

teardown() {
	local file
	for file in "$tmpdir"/*.pid; do
		[[ -f $file ]] || continue
		xargs -a "$file" kill --
	done

	common_teardown
}

# bats test_tags=network,sudo
@test "libremakepkg builds a trivial package" {
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	testsudo libremakepkg -l "$BATS_TEST_NAME"

	globfile libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg disables networking during prepare" {
	cp fixtures/libremakepkg/PKGBUILD-netprepare "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	not testsudo libremakepkg -l "$BATS_TEST_NAME"
	not globfile libretools-netprepare-1.0-1-any.pkg.tar?(.!(sig|*.*))
	testsudo libremakepkg -l "$BATS_TEST_NAME" -N
	globfile libretools-netprepare-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg disables networking during build" {
	cp fixtures/libremakepkg/PKGBUILD-netbuild "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	not testsudo libremakepkg -l "$BATS_TEST_NAME"
	not globfile libretools-netbuild-1.0-1-any.pkg.tar?(.!(sig|*.*))
	testsudo libremakepkg -l "$BATS_TEST_NAME" -N
	globfile libretools-netbuild-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg disables networking during package" {
	cp fixtures/libremakepkg/PKGBUILD-netpackage "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	not testsudo libremakepkg -l "$BATS_TEST_NAME"
	not globfile libretools-netpackage-1.0-1-any.pkg.tar?(.!(sig|*.*))
	testsudo libremakepkg -l "$BATS_TEST_NAME" -N
	globfile libretools-netpackage-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg cleans the chroot before building" {
	# 1. First, we build testpkg1
	# 2. Then, we build testpkg2, which depends on testpkg1
	#    Therefore, testpkg1 will be installed after testpkg2 is built, we
	#    check for that.
	# 3. Then, we build hello, which depends on neither, so testpkg1 should
	#    be removed.

	# Also, do funny things with the output of libremakepkg to get a helpful
	# fail case.

	mkdir -p "$tmpdir"/{1,2,3}
	cp fixtures/libremakepkg/PKGBUILD-testpkg1 "$tmpdir/1/PKGBUILD"
	cp fixtures/libremakepkg/PKGBUILD-testpkg2 "$tmpdir/2/PKGBUILD"
	cp fixtures/libremakepkg/PKGBUILD-hello    "$tmpdir/3/PKGBUILD"


	cd "$tmpdir/1"
	testsudo libremakepkg -l "$BATS_TEST_NAME" &> "$tmpdir/out" || { status=$?; tail "$tmpdir/out"|cat -v; return $status; }

	cd "$tmpdir/2"
	testsudo libremakepkg -l "$BATS_TEST_NAME" &> "$tmpdir/out" || { status=$?; tail "$tmpdir/out"|cat -v; return $status; }
	testsudo librechroot -l "$BATS_TEST_NAME" run libretools-testpkg1 'first time, pass'

	# This next line is actually a separate test, but it fits in well with this test, and chroot tests are slow.
	# @test "libremakepkg doesnt cache local packages" {
	not testsudo librechroot -l "$BATS_TEST_NAME" run bash -O extglob -c 'test -e /var/cache/pacman/pkg/libretools-testpkg1-1.0-1-any.pkg.tar?(.!(sig|*.*))'

	cd "$tmpdir/3"
	testsudo libremakepkg -l "$BATS_TEST_NAME" &> "$tmpdir/out" || { status=$?; tail "$tmpdir/out"|cat -v; return $status; }
	not testsudo librechroot -l "$BATS_TEST_NAME" run libretools-testpkg1 'second time, fail'
}

# bats test_tags=network,sudo
@test "libremakepkg handles PKGDEST not existing" {
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	testsudo env PKGDEST="$tmpdir/dest/pkgdest" libremakepkg -l "$BATS_TEST_NAME"

	globfile dest/pkgdest/libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

@test "libremakepkg displays help as normal user" {
	rm -rf "$XDG_CONFIG_HOME"
	LC_ALL=C libremakepkg -h >$tmpdir/stdout 2>$tmpdir/stderr

	[[ "$(sed 1q $tmpdir/stdout)" =~ Usage:.* ]]
	empty $tmpdir/stderr
}

@test "libremakepkg otherwise fails as normal user" {
	# I do this to give it a chance of passing
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	libremakepkg >$tmpdir/stdout 2>$tmpdir/stderr || status=$?

	[[ $status != 0 ]]
	empty $tmpdir/stdout
	not empty $tmpdir/stderr
}

# bats test_tags=network,sudo
@test "libremakepkg fails if a hook fails" {
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	cat >> "$XDG_CONFIG_HOME/libretools/libretools.conf" <<-eot
		BLACKLIST='phony://example.com'
		eot

	testsudo libremakepkg -l "$BATS_TEST_NAME" >$tmpdir/stdout 2>$tmpdir/stderr || status=$?

	[[ $status != 0 ]]
	tail -n1 $tmpdir/stderr | grep -qF '==> ERROR: Failure(s) in check_pkgbuild: check_pkgbuild_nonfree'
}

# bats test_tags=network,sudo
@test "libremakepkg detects distcc files" {
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	cat >> "$XDG_CONFIG_HOME/libretools/chroot.conf" <<-eot
		CHROOTEXTRAPKG+=(distcc-nozeroconf socat)
		eot
	testsudo librechroot -l "$BATS_TEST_NAME" install-name distcc-nozeroconf socat

	# first make sure that the engine works
	testsudo libremakepkg -l "$BATS_TEST_NAME"
	globfile libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
	rm -f -- libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
	# now throw a wrench in it
	testsudo librechroot -l "$BATS_TEST_NAME" run touch /bin/distcc-tool
	# and make sure that the engine broke
	testsudo libremakepkg -l "$BATS_TEST_NAME" || status=$?
	[[ $status != 0 ]]
	not globfile libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg forwards distcc ports" {
	# The maximum AF_UNIX socket path is 108 bytes; so let's have
	# a chroot name that's guaranteed to be >110 characters, to
	# make sure we handle that.
	local chrootname=$BATS_TEST_NAME.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

	local distcc_port
	distcc_port=$(./lib/runserver "$tmpdir/distcc.pid" \
		echo 'Hello, I am a distcc server')

	cat >> "$XDG_CONFIG_HOME/libretools/chroot.conf" <<-eot
		CHROOTEXTRAPKG+=(distcc-nozeroconf socat)
		eot
	cat >> "$XDG_CONFIG_HOME/pacman/makepkg.conf" <<-eot
		DISTCC_HOSTS=127.0.0.1:${distcc_port@Q}
		eot

	cp fixtures/libremakepkg/PKGBUILD-distcc "$tmpdir/PKGBUILD"
	cd "$tmpdir"
	testsudo librechroot -l "$chrootname" install-name distcc-nozeroconf socat
	testsudo libremakepkg -l "$chrootname"
	globfile libretools-distcc-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg doesnt symlink outputs" {
	sed -i /^unset/d "$XDG_CONFIG_HOME/pacman/makepkg.conf"

	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	testsudo libremakepkg -l "$BATS_TEST_NAME"

	not stat libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
	not stat libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*))
	globfile "$tmpdir/workdir/pkgdest"/libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
	globfile "$tmpdir/workdir/srcpkgdest"/libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg succeeds with good signatures" {
	cp fixtures/libremakepkg/PKGBUILD-signed "$tmpdir/PKGBUILD"
	cp fixtures/libremakepkg/hello.sh "$tmpdir/hello.sh"
	cd "$tmpdir"
	gpg --detach-sign --use-agent --no-armor hello.sh

	testsudo libremakepkg -l "$BATS_TEST_NAME"
}

# bats test_tags=network,sudo
@test "libremakepkg fails with bad signatures" {
	cp fixtures/libremakepkg/PKGBUILD-signed "$tmpdir/PKGBUILD"
	cp fixtures/libremakepkg/hello.sh "$tmpdir/hello.sh"
	cd "$tmpdir"
	gpg --detach-sign --use-agent --no-armor hello.sh
	echo 'echo pwned' >> hello.sh
	makepkg -g >> PKGBUILD

	not testsudo libremakepkg -l "$BATS_TEST_NAME"
}

# bats test_tags=network,sudo
@test "libremakepkg does not run pkgver" {
	cp fixtures/libremakepkg/PKGBUILD-pkgver "$tmpdir/PKGBUILD"
	pushd "$tmpdir"

	testsudo libremakepkg -l "$BATS_TEST_NAME"

	globfile libretools-pkgver-1-1-any.pkg.tar?(.!(sig|*.*))
	not globfile libretools-pkgver-2-1-any.pkg.tar?(.!(sig|*.*))
	popd
	diff -u fixtures/libremakepkg/PKGBUILD-pkgver "$tmpdir/PKGBUILD"
}

# bats test_tags=network,sudo
@test "libremakepkg has a flag to make startdir rw" {
	cp fixtures/libremakepkg/PKGBUILD-rwstartdir "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	not testsudo libremakepkg -l "$BATS_TEST_NAME"
	not globfile libretools-rwstartdir-1.0-1-any.pkg.tar?(.!(sig|*.*))
	testsudo libremakepkg -l "$BATS_TEST_NAME" -W
	globfile libretools-rwstartdir-1.0-1-any.pkg.tar?(.!(sig|*.*))
}

# bats test_tags=network,sudo
@test "libremakepkg can re-use source-packages" {
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	# I'm tempted to use file checksums here, but that would break
	# when we gain support for reproducible builds.  So just use
	# timestamps.

	testsudo libremakepkg -l "$BATS_TEST_NAME"
	globfile libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*))
	globfile libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
	a_stime=$(stat -c %Y -- libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*)))
	a_ptime=$(stat -c %Y -- libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*)))

	testsudo libremakepkg -l "$BATS_TEST_NAME" -S libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*))
	globfile libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*))
	globfile libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*))
	b_stime=$(stat -c %Y -- libretools-hello-1.0-1-any.src.tar?(.!(sig|*.*)))
	b_ptime=$(stat -c %Y -- libretools-hello-1.0-1-any.pkg.tar?(.!(sig|*.*)))

	(( a_stime == b_stime ))
	(( a_ptime < b_ptime ))
	(( a_ptime > a_stime ))
	(( b_ptime > a_stime ))
}