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

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
}

@test "libremakepkg builds a trivial package" {
	require network sudo || skip
	cp fixtures/libremakepkg/PKGBUILD-hello "$tmpdir/PKGBUILD"
	cd "$tmpdir"

	testsudo libremakepkg -l "$BATS_TEST_NAME"

	globfile libretools-hello-1.0-1-any.pkg.tar.?z
}

@test "libremakepkg disables networking during prepare" {
	require network sudo || skip
	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.?z
	testsudo libremakepkg -l "$BATS_TEST_NAME" -N
	globfile libretools-netprepare-1.0-1-any.pkg.tar.?z
}

@test "libremakepkg disables networking during build" {
	require network sudo || skip
	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.?z
	testsudo libremakepkg -l "$BATS_TEST_NAME" -N
	globfile libretools-netbuild-1.0-1-any.pkg.tar.?z
}

@test "libremakepkg disables networking during package" {
	require network sudo || skip
	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.?z
	testsudo libremakepkg -l "$BATS_TEST_NAME" -N
	globfile libretools-netpackage-1.0-1-any.pkg.tar.?z
}

@test "libremakepkg cleans the chroot before building" {
	require network sudo || skip
	# 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 test -e /var/cache/pacman/pkg/libretools-testpkg1-1.0-1-any.pkg.tar.?z

	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'
}

@test "libremakepkg handles PKGDEST not existing" {
	require network sudo || skip
	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.?z
}

@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
}

@test "libremakepkg fails if a hook fails" {
	require network sudo || skip
	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'
}

@test "libremakepkg detects distcc files" {
	require network sudo || skip
	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.?z
	rm -f -- libretools-hello-1.0-1-any.pkg.tar.?z
	# 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.?z
}

@test "libremakepkg forwards distcc ports" {
	require network sudo || skip

	# 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.?z
}

@test "libremakepkg doesnt symlink outputs" {
	require network sudo || skip

	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.?z
	not stat libretools-hello-1.0-1-any.src.tar.?z
	globfile "$tmpdir/workdir/pkgdest"/libretools-hello-1.0-1-any.pkg.tar.?z
	globfile "$tmpdir/workdir/srcpkgdest"/libretools-hello-1.0-1-any.src.tar.?z
}

@test "libremakepkg succeeds with good signatures" {
	require network sudo || skip

	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"
}

@test "libremakepkg fails with bad signatures" {
	require network sudo || skip

	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"
}

@test "libremakepkg does not run pkgver" {
	require network sudo || skip

	cp fixtures/libremakepkg/PKGBUILD-pkgver "$tmpdir/PKGBUILD"
	pushd "$tmpdir"

	testsudo libremakepkg -l "$BATS_TEST_NAME"

	globfile libretools-pkgver-1-1-any.pkg.tar.?z
	not globfile libretools-pkgver-2-1-any.pkg.tar.?z
	popd
	diff -u fixtures/libremakepkg/PKGBUILD-pkgver "$tmpdir/PKGBUILD"
}