BlueGit

timfa bluegit

Files

bluegit / scripts / aur_test.go

aur_test.go

fix: reken ISO-8601-pkgver om zonder GNU date
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
06b050d1 History
aur_test.go 15.5 KiB Download Raw
1package scripts
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11func TestRewritePkgbuild(t *testing.T) {
12 root := filepath.Join("..")
13 for _, name := range []string{"publish-aur.sh", "aur-ssh-setup.sh", "aur-common.sh", "aur-publish-local.sh"} {
14 cmd := exec.Command("bash", "-n", filepath.Join(root, "scripts", name))
15 if out, err := cmd.CombinedOutput(); err != nil {
16 t.Fatalf("%s: %v %s", name, err, out)
17 }
18 }
19 publish, err := os.ReadFile(filepath.Join(root, "scripts", "publish-aur.sh"))
20 if err != nil {
21 t.Fatal(err)
22 }
23 if strings.Contains(string(publish), "CI_JOB_TOKEN") || strings.Contains(string(publish), "JOB-TOKEN") {
24 t.Fatal("het archief hoort anoniem opgehaald te worden")
25 }
26 dir := t.TempDir()
27 src, err := os.ReadFile(filepath.Join(root, "packaging", "aur", "PKGBUILD"))
28 if err != nil {
29 t.Fatal(err)
30 }
31 dest := filepath.Join(dir, "PKGBUILD")
32 if err := os.WriteFile(dest, src, 0o644); err != nil {
33 t.Fatal(err)
34 }
35 version, commit, sum := "20261022183000", "0123456789abcdef0123456789abcdef01234567", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
36 cmd := exec.Command("bash", filepath.Join(root, "scripts", "publish-aur.sh"), "--rewrite-only", version, commit, sum, dest)
37 if out, err := cmd.CombinedOutput(); err != nil {
38 t.Fatalf("rewrite: %v %s", err, out)
39 }
40 got, err := os.ReadFile(dest)
41 if err != nil {
42 t.Fatal(err)
43 }
44 text := string(got)
45 for _, want := range []string{
46 "pkgver=" + version,
47 "pkgrel=1",
48 "_commit=" + commit,
49 "sha256sums=('" + sum + "')",
50 "https://gitlab.com/Timfa/bluegit/-/archive/${_commit}/bluegit-${_commit}.tar.gz",
51 } {
52 if !strings.Contains(text, want) {
53 t.Fatalf("mist %q in\n%s", want, text)
54 }
55 }
56 hook, err := os.ReadFile(filepath.Join(root, "packaging", "aur", "bluegit.install"))
57 if err != nil {
58 t.Fatal(err)
59 }
60 hookText := string(hook)
61 for _, want := range []string{
62 "systemd-sysusers bluegit.conf",
63 "systemd-tmpfiles --create",
64 "systemctl daemon-reload",
65 "enable --now bluegit.service",
66 "try-restart bluegit.service",
67 "disable --now bluegit.service",
68 "pre_remove",
69 "/etc/bluegit/config.yml",
70 "/etc/bluegit/client_secret",
71 "BLUEGIT_AUTH_CLIENT_SECRET",
72 "/run/systemd/system",
73 "User=bluegit",
74 } {
75 if !strings.Contains(hookText, want) {
76 t.Fatalf("hook mist %q", want)
77 }
78 }
79 if strings.Contains(hookText, "rm ") {
80 t.Fatal("hook hoort geen bestanden te verwijderen")
81 }
82 installBody, ok := shellFunc(hookText, "post_install")
83 if !ok || !strings.Contains(installBody, "enable --now bluegit.service") {
84 t.Fatal("post_install start de service niet")
85 }
86 upgradeBody, ok := shellFunc(hookText, "post_upgrade")
87 if !ok || strings.Contains(upgradeBody, "post_install") || !strings.Contains(upgradeBody, "try-restart bluegit.service") {
88 t.Fatalf("post_upgrade hoort alleen te herstarten:\n%s", upgradeBody)
89 }
90 removeBody, ok := shellFunc(hookText, "pre_remove")
91 if !ok || !strings.Contains(removeBody, "disable --now bluegit.service") {
92 t.Fatal("pre_remove zet de service niet uit")
93 }
94 local, err := os.ReadFile(filepath.Join(root, "scripts", "aur-publish-local.sh"))
95 if err != nil {
96 t.Fatal(err)
97 }
98 for _, bad := range []string{"CI_COMMIT_SHA", "CI_JOB_TOKEN", "JOB-TOKEN", "AUR_SSH_PRIVATE_KEY", "AUR_GIT_AUTHOR", "GIT_SSH_COMMAND", "aur-ssh-setup"} {
99 if strings.Contains(string(local), bad) {
100 t.Fatalf("lokaal script noemt %s", bad)
101 }
102 }
103}
104
105func shellFunc(script, name string) (string, bool) {
106 marker := name + "() {"
107 i := strings.Index(script, marker)
108 if i < 0 {
109 return "", false
110 }
111 rest := script[i+len(marker):]
112 depth := 1
113 for j, r := range rest {
114 switch r {
115 case '{':
116 depth++
117 case '}':
118 depth--
119 if depth == 0 {
120 return rest[:j], true
121 }
122 }
123 }
124 return "", false
125}
126
127func scriptEnv(extra ...string) []string {
128 skip := []string{
129 "GIT_AUTHOR_", "GIT_COMMITTER_", "GIT_DIR=", "GIT_WORK_TREE=",
130 "GIT_CONFIG_COUNT=", "GIT_CONFIG_KEY_", "GIT_CONFIG_VALUE_",
131 "GIT_CONFIG_GLOBAL=", "GIT_CONFIG_SYSTEM=",
132 }
133 out := make([]string, 0, len(os.Environ())+len(extra))
134 for _, e := range os.Environ() {
135 drop := false
136 for _, p := range skip {
137 if strings.HasPrefix(e, p) {
138 drop = true
139 break
140 }
141 }
142 if !drop {
143 out = append(out, e)
144 }
145 }
146 return append(out, extra...)
147}
148
149func runCmd(t *testing.T, dir string, env []string, name string, args ...string) string {
150 t.Helper()
151 cmd := exec.Command(name, args...)
152 if dir != "" {
153 cmd.Dir = dir
154 }
155 cmd.Env = env
156 out, err := cmd.CombinedOutput()
157 if err != nil {
158 t.Fatalf("%s %v: %v\n%s", name, args, err, out)
159 }
160 return string(out)
161}
162
163func initPublishedMaster(t *testing.T, env []string) string {
164 t.Helper()
165 root := t.TempDir()
166 repo := filepath.Join(root, "repo")
167 bare := filepath.Join(root, "origin.git")
168 if err := os.MkdirAll(repo, 0o755); err != nil {
169 t.Fatal(err)
170 }
171 runCmd(t, repo, env, "git", "init", "-b", "master")
172 runCmd(t, repo, env, "git", "config", "user.name", "Test")
173 runCmd(t, repo, env, "git", "config", "user.email", "test@example.com")
174 if err := os.WriteFile(filepath.Join(repo, "README"), []byte("x\n"), 0o644); err != nil {
175 t.Fatal(err)
176 }
177 runCmd(t, repo, env, "git", "add", "README")
178 runCmd(t, repo, env, "git", "commit", "-m", "init")
179 runCmd(t, root, env, "git", "init", "--bare", "-b", "master", bare)
180 runCmd(t, repo, env, "git", "remote", "add", "origin", bare)
181 runCmd(t, repo, env, "git", "push", "-u", "origin", "master")
182 return repo
183}
184
185func TestAurCommitPublished(t *testing.T) {
186 dir := t.TempDir()
187 pkg := filepath.Join(dir, "PKGBUILD")
188 sha := "0123456789abcdef0123456789abcdef01234567"
189 if err := os.WriteFile(pkg, []byte("pkgver=1\npkgrel=1\n_commit="+sha+"\n"), 0o644); err != nil {
190 t.Fatal(err)
191 }
192 common := filepath.Join("..", "scripts", "aur-common.sh")
193 ask := func(commit, file string) string {
194 t.Helper()
195 cmd := exec.Command("bash", "-c", `source "$1"; if aur_commit_published "$2" "$3"; then echo yes; else echo no; fi`, "bash", common, file, commit)
196 out, err := cmd.CombinedOutput()
197 if err != nil {
198 t.Fatalf("aur_commit_published: %v %s", err, out)
199 }
200 return strings.TrimSpace(string(out))
201 }
202 if ask(sha, pkg) != "yes" {
203 t.Fatal("bekende sha hoort al gepubliceerd te zijn")
204 }
205 if ask("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", pkg) != "no" {
206 t.Fatal("andere sha hoort nog niet gepubliceerd te zijn")
207 }
208 if ask(sha, filepath.Join(dir, "missing")) != "no" {
209 t.Fatal("ontbrekende PKGBUILD is niet gepubliceerd")
210 }
211}
212
213func TestCopyPackagingAur(t *testing.T) {
214 src := t.TempDir()
215 dest := t.TempDir()
216 for _, name := range []string{"PKGBUILD", "bluegit.install", ".SRCINFO"} {
217 if err := os.WriteFile(filepath.Join(src, name), []byte(name+"\n"), 0o644); err != nil {
218 t.Fatal(err)
219 }
220 }
221 script := filepath.Join("..", "scripts", "aur-publish-local.sh")
222 cmd := exec.Command("bash", "-c", `source "$1"; copy_packaging_aur "$2" "$3"`, "bash", script, src, dest)
223 if out, err := cmd.CombinedOutput(); err != nil {
224 t.Fatalf("kopie: %v %s", err, out)
225 }
226 for _, name := range []string{"PKGBUILD", "bluegit.install", ".SRCINFO"} {
227 got, err := os.ReadFile(filepath.Join(dest, name))
228 if err != nil {
229 t.Fatalf("mist %s: %v", name, err)
230 }
231 if string(got) != name+"\n" {
232 t.Fatalf("%s = %q", name, got)
233 }
234 }
235}
236
237func TestLocalAurCheckOnly(t *testing.T) {
238 env := scriptEnv(
239 "GIT_AUTHOR_NAME=Test",
240 "GIT_AUTHOR_EMAIL=test@example.com",
241 "GIT_COMMITTER_NAME=Test",
242 "GIT_COMMITTER_EMAIL=test@example.com",
243 )
244 script := filepath.Join("..", "scripts", "aur-publish-local.sh")
245 check := func(t *testing.T, repo string) (string, error) {
246 t.Helper()
247 cache := filepath.Join(t.TempDir(), "cache")
248 cmd := exec.Command("bash", script, "--check-only", repo)
249 cmd.Env = append(append([]string{}, env...), "BLUEGIT_AUR_CACHE="+cache)
250 out, err := cmd.CombinedOutput()
251 if _, statErr := os.Stat(cache); !os.IsNotExist(statErr) {
252 t.Fatal("check-only raakte de AUR-werkmap")
253 }
254 return string(out), err
255 }
256
257 t.Run("gelijk", func(t *testing.T) {
258 repo := initPublishedMaster(t, env)
259 sha := strings.TrimSpace(runCmd(t, repo, env, "git", "rev-parse", "origin/master"))
260 out, err := check(t, repo)
261 if err != nil {
262 t.Fatalf("check-only: %v\n%s", err, out)
263 }
264 if !strings.Contains(out, "Publiceren: "+sha) {
265 t.Fatalf("sha van origin/master ontbreekt:\n%s", out)
266 }
267 })
268 t.Run("niet schoon", func(t *testing.T) {
269 repo := initPublishedMaster(t, env)
270 if err := os.WriteFile(filepath.Join(repo, "dirty"), []byte("z\n"), 0o644); err != nil {
271 t.Fatal(err)
272 }
273 out, err := check(t, repo)
274 if err == nil || !strings.Contains(out, "niet schoon") {
275 t.Fatalf("vuile checkout hoorde te falen:\n%s", out)
276 }
277 })
278 t.Run("niet master", func(t *testing.T) {
279 repo := initPublishedMaster(t, env)
280 runCmd(t, repo, env, "git", "checkout", "-b", "other")
281 out, err := check(t, repo)
282 if err == nil || !strings.Contains(out, "niet op master") {
283 t.Fatalf("andere branch hoorde te falen:\n%s", out)
284 }
285 })
286 t.Run("wijkt af", func(t *testing.T) {
287 repo := initPublishedMaster(t, env)
288 if err := os.WriteFile(filepath.Join(repo, "README"), []byte("y\n"), 0o644); err != nil {
289 t.Fatal(err)
290 }
291 runCmd(t, repo, env, "git", "add", "README")
292 runCmd(t, repo, env, "git", "commit", "-m", "lokaal")
293 out, err := check(t, repo)
294 if err == nil || !strings.Contains(out, "wijkt af van origin/master") {
295 t.Fatalf("lokale commit hoorde te falen:\n%s", out)
296 }
297 })
298}
299
300func TestInstallHookService(t *testing.T) {
301 if _, err := exec.LookPath("unshare"); err != nil {
302 t.Skip("unshare ontbreekt")
303 }
304 work := t.TempDir()
305 hook, err := filepath.Abs(filepath.Join("..", "packaging", "aur", "bluegit.install"))
306 if err != nil {
307 t.Fatal(err)
308 }
309 script := filepath.Join(work, "hooktest.sh")
310 if err := os.WriteFile(script, []byte(installHookTest), 0o755); err != nil {
311 t.Fatal(err)
312 }
313 cmd := exec.Command("unshare", "-U", "-r", "-m", "bash", script, hook, work)
314 out, err := cmd.CombinedOutput()
315 if err != nil || !strings.Contains(string(out), "hook-ok") {
316 t.Fatalf("install-hook: %v\n%s", err, out)
317 }
318}
319
320const installHookTest = `#!/usr/bin/env bash
321set -euo pipefail
322hook="$1"
323work="$2"
324bin="$work/bin"
325log="$work/log"
326mode="$work/mode"
327etc="$work/etc"
328mkdir -p "$bin" "$etc/bluegit"
329
330cat > "$bin/systemctl" <<'EOF'
331#!/bin/sh
332printf '%s\n' "$*" >> "$BLUEGIT_TEST_LOG"
333mode=$(cat "$BLUEGIT_TEST_MODE" 2>/dev/null || true)
334case "$mode" in
335 bus)
336 echo "System has not been booted with systemd." >&2
337 exit 1
338 ;;
339 other)
340 echo "Failed to enable unit: Unit bluegit.service not found." >&2
341 exit 1
342 ;;
343esac
344exit 0
345EOF
346printf '#!/bin/sh\nexit 0\n' > "$bin/systemd-sysusers"
347printf '#!/bin/sh\nexit 0\n' > "$bin/systemd-tmpfiles"
348chmod +x "$bin/systemctl" "$bin/systemd-sysusers" "$bin/systemd-tmpfiles"
349
350export BLUEGIT_TEST_LOG="$log"
351export BLUEGIT_TEST_MODE="$mode"
352export PATH="$bin:/usr/bin:/bin"
353
354mount --bind "$etc" /etc
355mount -t tmpfs tmpfs /run
356# shellcheck disable=SC1090
357source "$hook"
358
359reset() {
360 : > "$log"
361 unset _BLUEGIT_SYSTEMD_CHECKED _BLUEGIT_SYSTEMD_OK
362 rm -rf /etc/bluegit /run/systemd
363 mkdir -p /etc/bluegit
364}
365
366echo ok > "$mode"
367
368echo "case geen-systemd" >&2
369reset
370out=$(post_install)
371post_upgrade
372pre_remove
373post_remove
374printf '%s\n' "$out" | grep -q 'systemd draait niet'
375printf '%s\n' "$out" | grep -q 'clientgeheim'
376if [[ -s "$log" ]]; then
377 echo "systemctl liep zonder systemd: $(cat "$log")" >&2
378 exit 1
379fi
380
381echo "case zonder-geheim" >&2
382reset
383mkdir -p /run/systemd/system
384printf 'client_secret: ""\n' > /etc/bluegit/config.yml
385out=$(post_install)
386printf '%s\n' "$out" | grep -q 'niet gestart'
387printf '%s\n' "$out" | grep -q '/usr/share/bluegit/storage.conf.example'
388if printf '%s\n' "$out" | grep -q 'systemd draait niet'; then
389 echo "onterecht chroot" >&2
390 exit 1
391fi
392grep -qx 'daemon-reload' "$log"
393if grep -q 'enable' "$log"; then
394 echo "enable zonder geheim" >&2
395 exit 1
396fi
397
398echo "case client_secret" >&2
399reset
400mkdir -p /run/systemd/system
401printf 'base_url: https://git.example\n' > /etc/bluegit/config.yml
402printf 'geheim\n' > /etc/bluegit/client_secret
403out=$(post_install)
404if printf '%s\n' "$out" | grep -q 'niet gestart'; then
405 echo "onverwachte melding: $out" >&2
406 exit 1
407fi
408grep -qx 'daemon-reload' "$log"
409grep -qx 'enable --now bluegit.service' "$log"
410
411echo "case secret.env" >&2
412reset
413mkdir -p /run/systemd/system
414printf 'x: y\n' > /etc/bluegit/config.yml
415printf ' \n' > /etc/bluegit/client_secret
416printf '%s\n' "BLUEGIT_AUTH_CLIENT_SECRET='geheim'" > /etc/bluegit/secret.env
417post_install >/dev/null
418grep -qx 'enable --now bluegit.service' "$log"
419
420echo "case lege-quotes" >&2
421reset
422mkdir -p /run/systemd/system
423printf 'x: y\n' > /etc/bluegit/config.yml
424printf '%s\n' '# BLUEGIT_AUTH_CLIENT_SECRET=nope' 'BLUEGIT_AUTH_CLIENT_SECRET=""' > /etc/bluegit/secret.env
425post_install >/dev/null
426if grep -q enable "$log"; then
427 echo "lege quotes telden als geheim" >&2
428 exit 1
429fi
430
431echo "case zonder-config" >&2
432reset
433mkdir -p /run/systemd/system
434printf 'geheim\n' > /etc/bluegit/client_secret
435post_install >/dev/null
436if grep -q enable "$log"; then
437 echo "enable zonder config" >&2
438 exit 1
439fi
440
441echo "case upgrade" >&2
442reset
443mkdir -p /run/systemd/system
444post_upgrade >/dev/null
445grep -qx 'daemon-reload' "$log"
446grep -qx 'try-restart bluegit.service' "$log"
447if grep -q enable "$log"; then
448 echo "upgrade zette de service aan" >&2
449 exit 1
450fi
451
452echo "case verwijderen" >&2
453reset
454mkdir -p /run/systemd/system
455pre_remove >/dev/null
456grep -qx 'disable --now bluegit.service' "$log"
457: > "$log"
458post_remove >/dev/null
459grep -qx 'daemon-reload' "$log"
460
461echo "case bus" >&2
462reset
463mkdir -p /run/systemd/system
464echo bus > "$mode"
465printf 'x: y\n' > /etc/bluegit/config.yml
466printf 'geheim\n' > /etc/bluegit/client_secret
467post_install >/dev/null
468echo ok > "$mode"
469
470echo "case echte-fout" >&2
471reset
472mkdir -p /run/systemd/system
473echo other > "$mode"
474if post_install >/dev/null; then
475 echo "echte systemctl-fout werd genegeerd" >&2
476 exit 1
477fi
478
479echo hook-ok
480`
481
482func TestPkgverUsesCommitterTime(t *testing.T) {
483 root := filepath.Join("..")
484 script := filepath.Join(root, "scripts", "publish-aur.sh")
485 out, err := exec.Command("git", "-C", root, "rev-parse", "HEAD").Output()
486 if err != nil {
487 t.Fatal(err)
488 }
489 sha := strings.TrimSpace(string(out))
490 if got := printPkgver(t, script, sha, "2026-10-22T22:32:33Z"); got != "20261022223233" {
491 t.Fatalf("Z: %s", got)
492 }
493 if got := printPkgver(t, script, sha, "2026-10-22T23:32:33+01:00"); got != "20261022223233" {
494 t.Fatalf("offset: %s", got)
495 }
496 if got := printPkgver(t, script, sha, "2026-10-22T00:30:00+02:00"); got != "20261021223000" {
497 t.Fatalf("rollover: %s", got)
498 }
499
500 got := printPkgver(t, script, sha, "")
501 updater := exec.Command("git", "-C", root, "show", "-s", "--date=format-local:%Y%m%d%H%M%S", "--format=%cd", sha)
502 updater.Env = append(os.Environ(), "TZ=UTC")
503 up, err := updater.Output()
504 if err != nil {
505 t.Fatal(err)
506 }
507 if got != strings.TrimSpace(string(up)) {
508 t.Fatalf("pkgver %s wijkt af van de updater %s", got, strings.TrimSpace(string(up)))
509 }
510 now, err := exec.Command("date", "-u", "+%Y%m%d%H%M%S").Output()
511 if err != nil {
512 t.Fatal(err)
513 }
514 if got == strings.TrimSpace(string(now)) && got != strings.TrimSpace(string(up)) {
515 t.Fatal("pkgver volgt de klok van de job")
516 }
517}
518
519func printPkgver(t *testing.T, script, sha, stamp string) string {
520 t.Helper()
521 env := make([]string, 0, len(os.Environ()))
522 for _, e := range os.Environ() {
523 if strings.HasPrefix(e, "CI_COMMIT_TIMESTAMP=") || strings.HasPrefix(e, "CI_COMMIT_SHA=") {
524 continue
525 }
526 env = append(env, e)
527 }
528 env = append(env, "CI_COMMIT_SHA="+sha)
529 if stamp != "" {
530 env = append(env, "CI_COMMIT_TIMESTAMP="+stamp)
531 }
532 cmd := exec.Command("bash", script, "--print-pkgver")
533 cmd.Env = env
534 out, err := cmd.CombinedOutput()
535 if err != nil {
536 t.Fatalf("print-pkgver: %v %s", err, out)
537 }
538 return strings.TrimSpace(string(out))
539}