|
| 1 | +package openshell |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/BitterSecurity/Decepticon/clients/launcher/internal/runtime" |
| 12 | +) |
| 13 | + |
| 14 | +// TestLivePrepareGatewayPodman is a real-host integration check: it runs the |
| 15 | +// actual PrepareGateway code to render the gateway TOML + export OPENSHELL_*, |
| 16 | +// then brings up ONLY the openshell gateway service via `podman compose` using |
| 17 | +// that output and asserts the gateway comes up healthy on the podman driver. |
| 18 | +// |
| 19 | +// Guarded: skips unless OPENSHELL_LIVE=1 and podman is present. Needs the repo |
| 20 | +// docker-compose.yml, the podman user socket, and the gateway image pulled. |
| 21 | +// Run on ra-trial: |
| 22 | +// |
| 23 | +// OPENSHELL_LIVE=1 go test -run TestLivePrepareGatewayPodman -v ./internal/openshell/ |
| 24 | +func TestLivePrepareGatewayPodman(t *testing.T) { |
| 25 | + if os.Getenv("OPENSHELL_LIVE") != "1" { |
| 26 | + t.Skip("set OPENSHELL_LIVE=1 to run the live gateway integration test") |
| 27 | + } |
| 28 | + if _, err := exec.LookPath("podman"); err != nil { |
| 29 | + t.Skip("podman not on PATH") |
| 30 | + } |
| 31 | + |
| 32 | + repoCompose := findRepoFile(t, "docker-compose.yml") |
| 33 | + repoRoot := filepath.Dir(repoCompose) |
| 34 | + |
| 35 | + home := t.TempDir() |
| 36 | + // The launcher runs compose from DECEPTICON_HOME; mirror that by copying |
| 37 | + // the compose file in. Services it references by build context are not |
| 38 | + // started (we bring up only the gateway service), so a copy is enough. |
| 39 | + must(t, copyFile(repoCompose, filepath.Join(home, "docker-compose.yml"))) |
| 40 | + must(t, os.WriteFile(filepath.Join(home, ".env"), nil, 0o600)) |
| 41 | + |
| 42 | + // Detect podman exactly as the launcher does. |
| 43 | + rt, err := runtime.Detect() |
| 44 | + if err != nil || rt.Name != "podman" { |
| 45 | + t.Skipf("podman runtime not detected (got %q, err %v)", rt.Name, err) |
| 46 | + } |
| 47 | + |
| 48 | + // THE CODE UNDER TEST: render config + export OPENSHELL_* env. |
| 49 | + profile, label, err := PrepareGateway(home, rt) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("PrepareGateway: %v", err) |
| 52 | + } |
| 53 | + t.Logf("engine label=%q profile=%q", label, profile) |
| 54 | + |
| 55 | + // Bring up only the gateway service via the docker CLI pointed at the |
| 56 | + // podman socket (the proven compose-on-podman path). This verifies the |
| 57 | + // launcher-rendered TOML + exported OPENSHELL_* env, which is what W1 |
| 58 | + // adds; the launcher's own compose-command selection is pre-existing. |
| 59 | + project := "dcp-w1-live" |
| 60 | + composeEnv := append(os.Environ(), "DOCKER_HOST=unix://"+os.Getenv("OPENSHELL_COMPUTE_SOCK")) |
| 61 | + up := exec.Command("docker", "compose", "-p", project, |
| 62 | + "--profile", profile, "up", "-d", "openshell-gateway") |
| 63 | + up.Dir = home |
| 64 | + up.Env = composeEnv |
| 65 | + if out, err := up.CombinedOutput(); err != nil { |
| 66 | + t.Fatalf("compose up gateway failed: %v\n%s", err, out) |
| 67 | + } |
| 68 | + t.Cleanup(func() { |
| 69 | + down := exec.Command("docker", "compose", "-p", project, "--profile", profile, "down") |
| 70 | + down.Dir = home |
| 71 | + down.Env = composeEnv |
| 72 | + _ = down.Run() |
| 73 | + }) |
| 74 | + |
| 75 | + // Health: a bridge container reaches the host-net gateway via |
| 76 | + // host.containers.internal (the real host can't, under rootless podman). |
| 77 | + var healthy bool |
| 78 | + var lastOut string |
| 79 | + for i := 0; i < 60; i++ { |
| 80 | + // /readyz returns the status JSON ({"status":"healthy",...}); /healthz |
| 81 | + // is an empty 200, so assert against /readyz. |
| 82 | + probe := exec.Command("podman", "run", "--rm", "docker.io/curlimages/curl:latest", |
| 83 | + "-fsS", "--max-time", "3", "http://host.containers.internal:8081/readyz") |
| 84 | + probe.Env = up.Env |
| 85 | + out, err := probe.CombinedOutput() |
| 86 | + lastOut = string(out) |
| 87 | + if err == nil && strings.Contains(lastOut, "healthy") { |
| 88 | + healthy = true |
| 89 | + break |
| 90 | + } |
| 91 | + time.Sleep(time.Second) |
| 92 | + } |
| 93 | + if !healthy { |
| 94 | + logs := exec.Command("podman", "logs", "decepticon-openshell-gateway") |
| 95 | + logs.Env = up.Env |
| 96 | + lg, _ := logs.CombinedOutput() |
| 97 | + t.Fatalf("gateway not healthy after 30s; last probe=%q\n--- logs ---\n%s", lastOut, lg) |
| 98 | + } |
| 99 | + t.Logf("gateway healthy via host.containers.internal: %s", strings.TrimSpace(lastOut)) |
| 100 | + |
| 101 | + // Confirm the podman compute driver connected (the launcher-rendered TOML). |
| 102 | + logs := exec.Command("podman", "logs", "decepticon-openshell-gateway") |
| 103 | + logs.Env = up.Env |
| 104 | + lg, _ := logs.CombinedOutput() |
| 105 | + if !strings.Contains(string(lg), "compute driver") || !strings.Contains(string(lg), "podman") { |
| 106 | + t.Errorf("gateway log does not confirm podman compute driver:\n%s", lg) |
| 107 | + } |
| 108 | + |
| 109 | + _ = repoRoot |
| 110 | +} |
| 111 | + |
| 112 | +func findRepoFile(t *testing.T, name string) string { |
| 113 | + t.Helper() |
| 114 | + dir, err := os.Getwd() |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("getwd: %v", err) |
| 117 | + } |
| 118 | + for i := 0; i < 8; i++ { |
| 119 | + p := filepath.Join(dir, name) |
| 120 | + if _, err := os.Stat(p); err == nil { |
| 121 | + return p |
| 122 | + } |
| 123 | + parent := filepath.Dir(dir) |
| 124 | + if parent == dir { |
| 125 | + break |
| 126 | + } |
| 127 | + dir = parent |
| 128 | + } |
| 129 | + t.Skipf("could not locate %s above %s", name, dir) |
| 130 | + return "" |
| 131 | +} |
| 132 | + |
| 133 | +func copyFile(src, dst string) error { |
| 134 | + b, err := os.ReadFile(src) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + return os.WriteFile(dst, b, 0o644) |
| 139 | +} |
| 140 | + |
| 141 | +func must(t *testing.T, err error) { |
| 142 | + t.Helper() |
| 143 | + if err != nil { |
| 144 | + t.Fatal(err) |
| 145 | + } |
| 146 | +} |
0 commit comments