| 1 | package scripts |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func 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 | |
| 105 | func 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 | |
| 127 | func 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 | |
| 149 | func 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 | |
| 163 | func 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 | |
| 185 | func 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 | |
| 213 | func 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 | |
| 237 | func 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 | |
| 300 | func 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 | |
| 320 | const installHookTest = `#!/usr/bin/env bash |
| 321 | set -euo pipefail |
| 322 | hook="$1" |
| 323 | work="$2" |
| 324 | bin="$work/bin" |
| 325 | log="$work/log" |
| 326 | mode="$work/mode" |
| 327 | etc="$work/etc" |
| 328 | mkdir -p "$bin" "$etc/bluegit" |
| 329 | |
| 330 | cat > "$bin/systemctl" <<'EOF' |
| 331 | #!/bin/sh |
| 332 | printf '%s\n' "$*" >> "$BLUEGIT_TEST_LOG" |
| 333 | mode=$(cat "$BLUEGIT_TEST_MODE" 2>/dev/null || true) |
| 334 | case "$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 | ;; |
| 343 | esac |
| 344 | exit 0 |
| 345 | EOF |
| 346 | printf '#!/bin/sh\nexit 0\n' > "$bin/systemd-sysusers" |
| 347 | printf '#!/bin/sh\nexit 0\n' > "$bin/systemd-tmpfiles" |
| 348 | chmod +x "$bin/systemctl" "$bin/systemd-sysusers" "$bin/systemd-tmpfiles" |
| 349 | |
| 350 | export BLUEGIT_TEST_LOG="$log" |
| 351 | export BLUEGIT_TEST_MODE="$mode" |
| 352 | export PATH="$bin:/usr/bin:/bin" |
| 353 | |
| 354 | mount --bind "$etc" /etc |
| 355 | mount -t tmpfs tmpfs /run |
| 356 | # shellcheck disable=SC1090 |
| 357 | source "$hook" |
| 358 | |
| 359 | reset() { |
| 360 | : > "$log" |
| 361 | unset _BLUEGIT_SYSTEMD_CHECKED _BLUEGIT_SYSTEMD_OK |
| 362 | rm -rf /etc/bluegit /run/systemd |
| 363 | mkdir -p /etc/bluegit |
| 364 | } |
| 365 | |
| 366 | echo ok > "$mode" |
| 367 | |
| 368 | echo "case geen-systemd" >&2 |
| 369 | reset |
| 370 | out=$(post_install) |
| 371 | post_upgrade |
| 372 | pre_remove |
| 373 | post_remove |
| 374 | printf '%s\n' "$out" | grep -q 'systemd draait niet' |
| 375 | printf '%s\n' "$out" | grep -q 'clientgeheim' |
| 376 | if [[ -s "$log" ]]; then |
| 377 | echo "systemctl liep zonder systemd: $(cat "$log")" >&2 |
| 378 | exit 1 |
| 379 | fi |
| 380 | |
| 381 | echo "case zonder-geheim" >&2 |
| 382 | reset |
| 383 | mkdir -p /run/systemd/system |
| 384 | printf 'client_secret: ""\n' > /etc/bluegit/config.yml |
| 385 | out=$(post_install) |
| 386 | printf '%s\n' "$out" | grep -q 'niet gestart' |
| 387 | printf '%s\n' "$out" | grep -q '/usr/share/bluegit/storage.conf.example' |
| 388 | if printf '%s\n' "$out" | grep -q 'systemd draait niet'; then |
| 389 | echo "onterecht chroot" >&2 |
| 390 | exit 1 |
| 391 | fi |
| 392 | grep -qx 'daemon-reload' "$log" |
| 393 | if grep -q 'enable' "$log"; then |
| 394 | echo "enable zonder geheim" >&2 |
| 395 | exit 1 |
| 396 | fi |
| 397 | |
| 398 | echo "case client_secret" >&2 |
| 399 | reset |
| 400 | mkdir -p /run/systemd/system |
| 401 | printf 'base_url: https://git.example\n' > /etc/bluegit/config.yml |
| 402 | printf 'geheim\n' > /etc/bluegit/client_secret |
| 403 | out=$(post_install) |
| 404 | if printf '%s\n' "$out" | grep -q 'niet gestart'; then |
| 405 | echo "onverwachte melding: $out" >&2 |
| 406 | exit 1 |
| 407 | fi |
| 408 | grep -qx 'daemon-reload' "$log" |
| 409 | grep -qx 'enable --now bluegit.service' "$log" |
| 410 | |
| 411 | echo "case secret.env" >&2 |
| 412 | reset |
| 413 | mkdir -p /run/systemd/system |
| 414 | printf 'x: y\n' > /etc/bluegit/config.yml |
| 415 | printf ' \n' > /etc/bluegit/client_secret |
| 416 | printf '%s\n' "BLUEGIT_AUTH_CLIENT_SECRET='geheim'" > /etc/bluegit/secret.env |
| 417 | post_install >/dev/null |
| 418 | grep -qx 'enable --now bluegit.service' "$log" |
| 419 | |
| 420 | echo "case lege-quotes" >&2 |
| 421 | reset |
| 422 | mkdir -p /run/systemd/system |
| 423 | printf 'x: y\n' > /etc/bluegit/config.yml |
| 424 | printf '%s\n' '# BLUEGIT_AUTH_CLIENT_SECRET=nope' 'BLUEGIT_AUTH_CLIENT_SECRET=""' > /etc/bluegit/secret.env |
| 425 | post_install >/dev/null |
| 426 | if grep -q enable "$log"; then |
| 427 | echo "lege quotes telden als geheim" >&2 |
| 428 | exit 1 |
| 429 | fi |
| 430 | |
| 431 | echo "case zonder-config" >&2 |
| 432 | reset |
| 433 | mkdir -p /run/systemd/system |
| 434 | printf 'geheim\n' > /etc/bluegit/client_secret |
| 435 | post_install >/dev/null |
| 436 | if grep -q enable "$log"; then |
| 437 | echo "enable zonder config" >&2 |
| 438 | exit 1 |
| 439 | fi |
| 440 | |
| 441 | echo "case upgrade" >&2 |
| 442 | reset |
| 443 | mkdir -p /run/systemd/system |
| 444 | post_upgrade >/dev/null |
| 445 | grep -qx 'daemon-reload' "$log" |
| 446 | grep -qx 'try-restart bluegit.service' "$log" |
| 447 | if grep -q enable "$log"; then |
| 448 | echo "upgrade zette de service aan" >&2 |
| 449 | exit 1 |
| 450 | fi |
| 451 | |
| 452 | echo "case verwijderen" >&2 |
| 453 | reset |
| 454 | mkdir -p /run/systemd/system |
| 455 | pre_remove >/dev/null |
| 456 | grep -qx 'disable --now bluegit.service' "$log" |
| 457 | : > "$log" |
| 458 | post_remove >/dev/null |
| 459 | grep -qx 'daemon-reload' "$log" |
| 460 | |
| 461 | echo "case bus" >&2 |
| 462 | reset |
| 463 | mkdir -p /run/systemd/system |
| 464 | echo bus > "$mode" |
| 465 | printf 'x: y\n' > /etc/bluegit/config.yml |
| 466 | printf 'geheim\n' > /etc/bluegit/client_secret |
| 467 | post_install >/dev/null |
| 468 | echo ok > "$mode" |
| 469 | |
| 470 | echo "case echte-fout" >&2 |
| 471 | reset |
| 472 | mkdir -p /run/systemd/system |
| 473 | echo other > "$mode" |
| 474 | if post_install >/dev/null; then |
| 475 | echo "echte systemctl-fout werd genegeerd" >&2 |
| 476 | exit 1 |
| 477 | fi |
| 478 | |
| 479 | echo hook-ok |
| 480 | ` |
| 481 | |
| 482 | func 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 | |
| 519 | func 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 | } |