|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Package e2e contains end-to-end tests that build and run the full |
| 4 | +// model-runner stack (server + llama.cpp backend + CLI) from source. |
| 5 | +// |
| 6 | +// These tests require: |
| 7 | +// - The llamacpp submodule to be initialised and built (make build-llamacpp) |
| 8 | +// - A successful `make build` so that model-runner, model-cli, and dmr exist |
| 9 | +// |
| 10 | +// Run with: |
| 11 | +// |
| 12 | +// go test -v -count=1 -tags=e2e -timeout=15m ./e2e/ |
| 13 | +package e2e |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "net" |
| 19 | + "net/http" |
| 20 | + "os" |
| 21 | + "os/exec" |
| 22 | + "path/filepath" |
| 23 | + "strconv" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + // testModel is small enough to pull quickly in CI. |
| 30 | + testModel = "ai/smollm2:135M-Q4_0" |
| 31 | + |
| 32 | + serverStartTimeout = 60 * time.Second |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + // serverURL is the base URL of the running model-runner instance. |
| 37 | + serverURL string |
| 38 | + // cliBin is the absolute path to the model-cli binary. |
| 39 | + cliBin string |
| 40 | +) |
| 41 | + |
| 42 | +// TestMain builds the binaries, starts the server (same pattern as dmr), |
| 43 | +// and tears it down after all tests complete. |
| 44 | +func TestMain(m *testing.M) { |
| 45 | + code := run(m) |
| 46 | + os.Exit(code) |
| 47 | +} |
| 48 | + |
| 49 | +func run(m *testing.M) int { |
| 50 | + // go test sets cwd to the package directory (e2e/), so the repo root is ../ |
| 51 | + root, err := filepath.Abs("..") |
| 52 | + if err != nil { |
| 53 | + fmt.Fprintf(os.Stderr, "e2e: %v\n", err) |
| 54 | + return 1 |
| 55 | + } |
| 56 | + |
| 57 | + // ── 1. Build binaries ────────────────────────────────────────────── |
| 58 | + fmt.Fprintln(os.Stderr, "e2e: building server and CLI...") |
| 59 | + if err := makeTarget(root, "build"); err != nil { |
| 60 | + fmt.Fprintf(os.Stderr, "e2e: make build failed: %v\n", err) |
| 61 | + return 1 |
| 62 | + } |
| 63 | + |
| 64 | + serverBin := filepath.Join(root, "model-runner") |
| 65 | + cliBin = filepath.Join(root, "cmd", "cli", "model-cli") |
| 66 | + llamaBin := filepath.Join(root, "llamacpp", "install", "bin") |
| 67 | + |
| 68 | + for _, path := range []string{serverBin, cliBin, llamaBin} { |
| 69 | + if _, err := os.Stat(path); err != nil { |
| 70 | + fmt.Fprintf(os.Stderr, "e2e: not found: %s\n", path) |
| 71 | + return 1 |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // ── 2. Start model-runner (same pattern as cmd/dmr) ──────────────── |
| 76 | + port, err := freePort() |
| 77 | + if err != nil { |
| 78 | + fmt.Fprintf(os.Stderr, "e2e: %v\n", err) |
| 79 | + return 1 |
| 80 | + } |
| 81 | + serverURL = "http://localhost:" + strconv.Itoa(port) |
| 82 | + fmt.Fprintf(os.Stderr, "e2e: starting model-runner on port %d\n", port) |
| 83 | + |
| 84 | + ctx, cancel := context.WithCancel(context.Background()) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + server := exec.CommandContext(ctx, serverBin) |
| 88 | + server.Dir = root |
| 89 | + server.Env = append(os.Environ(), |
| 90 | + "MODEL_RUNNER_PORT="+strconv.Itoa(port), |
| 91 | + "LLAMA_SERVER_PATH="+llamaBin, |
| 92 | + ) |
| 93 | + server.Stdout = os.Stderr |
| 94 | + server.Stderr = os.Stderr |
| 95 | + |
| 96 | + if err := server.Start(); err != nil { |
| 97 | + fmt.Fprintf(os.Stderr, "e2e: failed to start server: %v\n", err) |
| 98 | + return 1 |
| 99 | + } |
| 100 | + defer func() { |
| 101 | + cancel() |
| 102 | + _ = server.Wait() |
| 103 | + }() |
| 104 | + |
| 105 | + // ── 3. Wait for health ───────────────────────────────────────────── |
| 106 | + if err := waitForServer(serverURL+"/models", serverStartTimeout); err != nil { |
| 107 | + fmt.Fprintf(os.Stderr, "e2e: %v\n", err) |
| 108 | + return 1 |
| 109 | + } |
| 110 | + fmt.Fprintf(os.Stderr, "e2e: server ready at %s\n", serverURL) |
| 111 | + |
| 112 | + // ── 4. Run tests ─────────────────────────────────────────────────── |
| 113 | + return m.Run() |
| 114 | +} |
| 115 | + |
| 116 | +func makeTarget(dir, target string) error { |
| 117 | + cmd := exec.Command("make", target) |
| 118 | + cmd.Dir = dir |
| 119 | + cmd.Stdout = os.Stderr |
| 120 | + cmd.Stderr = os.Stderr |
| 121 | + return cmd.Run() |
| 122 | +} |
| 123 | + |
| 124 | +func freePort() (int, error) { |
| 125 | + l, err := net.Listen("tcp", "127.0.0.1:0") |
| 126 | + if err != nil { |
| 127 | + return 0, fmt.Errorf("finding free port: %w", err) |
| 128 | + } |
| 129 | + defer l.Close() |
| 130 | + return l.Addr().(*net.TCPAddr).Port, nil |
| 131 | +} |
| 132 | + |
| 133 | +func waitForServer(url string, timeout time.Duration) error { |
| 134 | + client := &http.Client{Timeout: 2 * time.Second} |
| 135 | + deadline := time.Now().Add(timeout) |
| 136 | + for time.Now().Before(deadline) { |
| 137 | + resp, err := client.Get(url) |
| 138 | + if err == nil { |
| 139 | + resp.Body.Close() |
| 140 | + if resp.StatusCode == http.StatusOK { |
| 141 | + return nil |
| 142 | + } |
| 143 | + } |
| 144 | + time.Sleep(200 * time.Millisecond) |
| 145 | + } |
| 146 | + return fmt.Errorf("server not ready after %s", timeout) |
| 147 | +} |
| 148 | + |
| 149 | +// runCLI executes the model-cli binary with the given arguments and |
| 150 | +// MODEL_RUNNER_HOST pointing to the test server. The subprocess is |
| 151 | +// cancelled if the test's context expires. |
| 152 | +func runCLI(t *testing.T, args ...string) (string, error) { |
| 153 | + t.Helper() |
| 154 | + cmd := exec.CommandContext(t.Context(), cliBin, args...) |
| 155 | + cmd.Env = append(os.Environ(), "MODEL_RUNNER_HOST="+serverURL) |
| 156 | + out, err := cmd.CombinedOutput() |
| 157 | + return string(out), err |
| 158 | +} |
0 commit comments