package assets import ( "crypto/sha256" "crypto/sha512" "encoding/hex" "io/fs" "os" "os/exec" "path/filepath" "strings" "testing" ) // heredoc is the body of the heredoc that base.sh starts with this line, // up to its end marker. func heredoc(t *testing.T, start, end string) string { t.Helper() b, err := fs.ReadFile(FS, "layers/base.sh") if err != nil { t.Fatal(err) } _, rest, ok := strings.Cut(string(b), start+"\n") block, _, closed := strings.Cut(rest, "\n"+end+"\n") if !ok || !closed { t.Fatalf("base.sh has no %q", start) } return block } // Every VM's shells get the tools' environment from base's // /etc/profile.d/devvm-tools.sh, which reads the toolchains' environment // (their telemetry off, their global installs into the VM's home) and // their folders for the PATH from /etc/devvm/toolchains.sh, and the // agents' (their self-updates, version checks and telemetry off) from // /etc/devvm/agents.sh, which every launch's setup writes, so base holds // none of it (internal/vm's TestToolchainsOwnTheirEnvironment): their // installs are read-only shared tools, and mise pins their versions. // Non-login shells read it too (BASH_ENV), but not devvm-telemetry.sh, so // no agent's switch is there. func TestToolsEnvironment(t *testing.T) { block := heredoc(t, "sudo tee /etc/profile.d/devvm-tools.sh >/dev/null <<'SH'", "SH") for _, line := range []string{ "if [ -r /etc/devvm/toolchains.sh ]; then . /etc/devvm/toolchains.sh; fi", ` export PATH="$devvm_path$HOME/.local/bin:$HOME/.local/share/mise/shims:$PATH" ;;`, "if [ -r /etc/devvm/agents.sh ]; then . /etc/devvm/agents.sh; fi", } { if !strings.Contains("\n"+block+"\n", "\n"+line+"\n") { t.Errorf("devvm-tools.sh lacks %q", line) } } telemetry := heredoc(t, "sudo tee /etc/profile.d/devvm-telemetry.sh >/dev/null <<'SH'", "SH") for _, v := range []string{"DISABLE_AUTOUPDATER=", "DISABLE_TELEMETRY=", "DISABLE_ERROR_REPORTING=", "OPENCODE_", "PI_"} { if strings.Contains(block, "export "+v) || strings.Contains(telemetry, "export "+v) { t.Errorf("base sets %s, an agent's, which the setup's agents.sh sets", v) } } } // baseLines is base.sh from the line start through the line end. func baseLines(t *testing.T, start, end string) string { t.Helper() b, err := fs.ReadFile(FS, "layers/base.sh") if err != nil { t.Fatal(err) } i := strings.Index("\n"+string(b), "\n"+start+"\n") if i < 0 { t.Fatalf("base.sh has no %q", start) } block, _, ok := strings.Cut(string(b)[i:], "\n"+end+"\n") if !ok { t.Fatalf("base.sh has no %q after %q", end, start) } return block + "\n" + end + "\n" } // What base.sh drops from Ubuntu's cloud image leaves every package base // installs: one the image has only as a dependency of a dropped package // stays automatic when base's apt-get install upgrades it, as jq, which // ubuntu-server brings, did, and the purge's autoremove took it. A // package gone anyway fails the build rather than recording a base // without it. apt, dpkg-query and sudo are stubs over a folder of // installed packages, each file holding "manual" or "auto"; the purge // removes the packages named and every automatic one. func TestBaseKeepsItsPackages(t *testing.T) { if _, err := exec.LookPath("bash"); err != nil { t.Skip("no bash") } stubs := `set -euo pipefail sudo() { "$@"; } systemctl() { :; } dpkg-query() { shift 2; local p; for p; do [[ ! -e $S/$p ]] || echo "installed $p"; done; } apt-mark() { [[ $1 == manual ]] || return 2; shift; local p; for p; do [[ -e $S/$p ]] || return 100; echo manual > "$S/$p"; done; } apt-get() { [[ $1 == purge ]] || return 2; shift 3 local p f; for p; do rm -f "$S/$p"; done for f in "$S"/*; do [[ $(cat "$f") == manual ]] || rm -f "$f"; done [[ -z ${GONE:-} ]] || rm -f "$S/$GONE" } ` installed := baseLines(t, `base_pkgs="git curl wget ca-certificates build-essential unzip jq ripgrep erofs-utils sqlite3 fd-find`, ` dnsmasq-base socat nftables"`) + `icu=libicu78 for p in $base_pkgs ubuntu-server snapd; do echo manual > "$S/$p"; done for p in jq "$icu" bc libonig5; do echo auto > "$S/$p"; done ` drop := baseLines(t, `keep="bc file htop lsof nano rsync screen strace tcpdump time tmux mtr-tiny pciutils usbutils`, "sudo systemctl mask apt-daily.timer apt-daily-upgrade.timer") for _, tc := range []struct { name, gone string ok bool }{ {"an upgraded automatic package", "", true}, {"a package the purge takes anyway", "ripgrep", false}, } { dir := t.TempDir() cmd := exec.CommandContext(t.Context(), "bash", "-c", stubs+installed+drop) //nolint:gosec // base.sh's own lines, on the test's own folder cmd.Env = append(os.Environ(), "S="+dir, "GONE="+tc.gone) out, err := cmd.CombinedOutput() if (err == nil) != tc.ok { t.Errorf("%s: %v\n%s", tc.name, err, out) continue } if !tc.ok { if !strings.Contains(string(out), tc.gone) { t.Errorf("%s: the failure doesn't name %s:\n%s", tc.name, tc.gone, out) } continue } for p, want := range map[string]bool{"jq": true, "libicu78": true, "git": true, "bc": true, "ubuntu-server": false, "snapd": false, "libonig5": false} { if _, err := os.Stat(filepath.Join(dir, p)); (err == nil) != want { t.Errorf("%s: %s installed %v, want %v", tc.name, p, err == nil, want) } } } } // base.sh's devvm-fetch keeps a download only when its checksum is the // one given: a SHA-256, or a SHA-512 by its length. func TestDevvmFetch(t *testing.T) { for _, c := range []string{"sh", "curl", "sha256sum", "sha512sum", "cut"} { if _, err := exec.LookPath(c); err != nil { t.Skip("no " + c) } } dir := t.TempDir() script := filepath.Join(dir, "devvm-fetch") if err := os.WriteFile(script, []byte(heredoc(t, "sudo tee /usr/local/bin/devvm-fetch >/dev/null <<'SH'", "SH")+"\n"), 0o600); err != nil { t.Fatal(err) } src := filepath.Join(dir, "src") body := []byte("an archive\n") if err := os.WriteFile(src, body, 0o600); err != nil { t.Fatal(err) } s256, s512 := sha256.Sum256(body), sha512.Sum512(body) for _, tc := range []struct { name, sum string ok bool }{ {"its SHA-256", hex.EncodeToString(s256[:]), true}, {"its SHA-512", hex.EncodeToString(s512[:]), true}, {"another SHA-256", strings.Repeat("0", 64), false}, {"another SHA-512", strings.Repeat("0", 128), false}, {"its SHA-256 twice", strings.Repeat(hex.EncodeToString(s256[:]), 2), false}, } { out := filepath.Join(dir, "out") err := exec.CommandContext(t.Context(), "sh", script, "file://"+src, tc.sum, out).Run() //nolint:gosec // base.sh's devvm-fetch, on the test's own files _, kept := os.Stat(out) if (err == nil) != tc.ok || (kept == nil) != tc.ok { t.Errorf("%s: %v, kept %v", tc.name, err, kept == nil) } _ = os.Remove(out) } }