package release import ( "crypto/sha256" "encoding/json" "fmt" "io/fs" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "testing/fstest" "amadan.net/oficina/equipo/internal/runner/provider" ) func TestBootstrapPartialRelease(t *testing.T) { script, err := os.ReadFile("../../packaging/install/install-v1.sh") if err != nil { t.Fatal(err) } validation := strings.Split(strings.Split(string(script), "<<'PY'\n")[1], "\nPY\n")[0] for _, scenario := range []string{"valid", "old_schema", "no_proxy", "proxy_mismatch", "runner_mismatch", "no_targets", "wrong_target", "wrong_version", "bad_digest"} { t.Run(scenario, func(t *testing.T) { runner, proxy := strings.Repeat("a", 64), strings.Repeat("c", 64) release := BuildManifest() release.Binaries = append(release.Binaries, BinaryDigest{Name: "equipo-tool-proxy", Version: release.LauncherVersion, SHA256: proxy}) m := InstallManifest{Version: "fixture-16", Target: "linux/amd64", RunnerSHA256: runner, NamespaceArgs: provider.NamespaceArguments(), Release: release, Targets: map[string]InstallTarget{TargetLinux: {RunnerSHA256: runner, ToolProxySHA256: proxy}}} if m.Validate("fixture-16") != nil { t.Fatal("the fixture is not a valid install manifest") } switch scenario { case "old_schema": m.Release.Schema = 2 case "no_proxy": m.Release.Binaries = m.Release.Binaries[:4] case "proxy_mismatch": m.Targets[TargetLinux] = InstallTarget{RunnerSHA256: runner, ToolProxySHA256: strings.Repeat("d", 64)} case "runner_mismatch": m.Targets[TargetLinux] = InstallTarget{RunnerSHA256: strings.Repeat("d", 64), ToolProxySHA256: proxy} case "no_targets": m.Targets = nil case "wrong_target": m.Target = "linux/arm64" case "wrong_version": m.Version = "other" case "bad_digest": m.RunnerSHA256 = "bad" } // The runner refuses whatever the bootstrap refuses. if (m.Validate("fixture-16") == nil) != (scenario == "valid") { t.Fatalf("Validate %s", scenario) } raw, _ := json.Marshal(m) path := filepath.Join(t.TempDir(), "manifest.json") if err := os.WriteFile(path, raw, 0600); err != nil { t.Fatal(err) } cmd := exec.Command("python3", "-c", validation, path, "fixture-16") out, err := cmd.CombinedOutput() if (err == nil) != (scenario == "valid") { t.Fatalf("bootstrap %s: %v %s", scenario, err, out) } }) } } // fixtureManifest is this build's manifest with the digest a release // would have recorded for proxy. func fixtureManifest(proxy []byte) Manifest { m := BuildManifest() m.Binaries = append(m.Binaries[:4:4], BinaryDigest{Name: "equipo-tool-proxy", Version: m.LauncherVersion, SHA256: fmt.Sprintf("%x", sha256.Sum256(proxy))}) return m } func embeddedFixture(t *testing.T) (fstest.MapFS, Manifest) { t.Helper() runner, proxy := []byte("fixture runner"), []byte("fixture proxy") m := fixtureManifest(proxy) runnerSum := fmt.Sprintf("%x", sha256.Sum256(runner)) manifest := InstallManifest{Version: "fixture-16", Target: "linux/amd64", RunnerSHA256: runnerSum, NamespaceArgs: provider.NamespaceArguments(), Release: m, Targets: map[string]InstallTarget{TargetLinux: {RunnerSHA256: runnerSum, ToolProxySHA256: m.ToolProxySHA256()}}} raw, err := json.Marshal(manifest) if err != nil { t.Fatal(err) } return fstest.MapFS{"install-manifest.json": {Data: raw}, "equipo-runner": {Data: runner}, "equipo-tool-proxy": {Data: proxy}, "install-v1.sh": {Data: []byte("fixture installer")}}, m } func TestEmbeddedInstallAssets(t *testing.T) { for _, scenario := range []string{"valid", "runner_digest", "proxy_digest", "missing", "version", "manifest", "trailing", "override"} { t.Run(scenario, func(t *testing.T) { files, m := embeddedFixture(t) a := &InstallAssets{Version: "fixture-16", Manifest: m, embedded: files} switch scenario { case "runner_digest": files["equipo-runner"].Data = []byte("tampered") case "proxy_digest": files["equipo-tool-proxy"].Data = []byte("tampered") case "missing": delete(files, "install-v1.sh") case "version": a.Version = "other" case "manifest": a.Manifest.InstanceTarget = "other" case "trailing": files["install-manifest.json"].Data = append(files["install-manifest.json"].Data, []byte("{}")...) case "override": a.Root = t.TempDir() } valid := scenario == "valid" if a.Available() != valid || a.CachedAvailable() != valid { t.Fatal("availability", scenario) } for _, name := range installAssetNames { w := httptest.NewRecorder() a.ServeHTTP(w, httptest.NewRequest("GET", "/releases/"+a.Version+"/"+name, nil)) if valid { if w.Code != 200 || w.Body.String() != string(files[name].Data) { t.Fatal("asset", name, w.Code) } } else if w.Code != 503 { t.Fatal("invalid bundle served", name, w.Code) } } if valid { for _, path := range []string{"/releases/other/equipo-runner", "/releases/fixture-16/../equipo-runner", "/releases/fixture-16/manifest.json"} { w := httptest.NewRecorder() a.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) if w.Code != 404 { t.Fatal("unexpected path served", path) } } w := httptest.NewRecorder() req := httptest.NewRequest("GET", "/releases/fixture-16/equipo-runner", nil) req.Header.Set("Range", "bytes=0-6") a.ServeHTTP(w, req) if w.Code != 206 || w.Body.String() != "fixture" { t.Fatal("range", w.Code, w.Body.String()) } } }) } } func TestBundledInstallAssets(t *testing.T) { files := embeddedInstallFiles() if files == nil { if os.Getenv("EQUIPO_BUNDLE_VERSION") != "" { t.Fatal("release bundle missing") } t.Skip("release build only") } raw, err := fs.ReadFile(files, "install-manifest.json") if err != nil { t.Fatal(err) } var m InstallManifest if err = json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } if want := os.Getenv("EQUIPO_BUNDLE_VERSION"); want != "" && m.Version != want { t.Fatal("bundle version", m.Version, want) } a := NewInstallAssets("", m.Version, BuildManifest()) if !a.CachedAvailable() { t.Fatal("bundled release unavailable") } for _, name := range installAssetNames { w := httptest.NewRecorder() a.ServeHTTP(w, httptest.NewRequest("GET", "/releases/"+m.Version+"/"+name, nil)) want, e := fs.ReadFile(files, name) if e != nil || w.Code != 200 || sha256.Sum256(w.Body.Bytes()) != sha256.Sum256(want) { t.Fatal("bundled HTTP asset", name, w.Code, e) } } // The release names the renderer the Linux runner installs (its tree // pin, archive digest and size), but the instance neither carries nor // serves it: the runner fetches it from the forge. if linux := m.Targets[TargetLinux]; linux.RendererSHA256 == "" || linux.RendererArchiveSHA256 == "" || linux.RendererArchiveSize <= 0 { t.Fatal("the bundled release names no renderer") } if _, err := fs.Stat(files, RendererArchive); err == nil { t.Fatal("the renderer is bundled into the instance") } w := httptest.NewRecorder() a.ServeHTTP(w, httptest.NewRequest("GET", "/releases/"+m.Version+"/"+RendererArchive, nil)) if w.Code != 404 { t.Fatalf("the instance serves the renderer: %d", w.Code) } // And everything bundled stays well under the platform's ship cap. var total int64 fs.WalkDir(files, ".", func(_ string, d fs.DirEntry, err error) error { if err == nil && !d.IsDir() { if info, err := d.Info(); err == nil { total += info.Size() } } return nil }) if total > 160<<20 { t.Fatalf("the bundle is %d bytes, over 160 MiB", total) } }