|
| 1 | +package tunnel |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func TestDefaultURL(t *testing.T) { |
| 16 | + cases := map[string]string{ |
| 17 | + RuntimeOllama: "http://localhost:11434", |
| 18 | + RuntimeVLLM: "http://localhost:8000", |
| 19 | + RuntimeLMStudio: "http://localhost:1234", |
| 20 | + RuntimeLlamaCPP: "http://localhost:8080", |
| 21 | + RuntimeMLX: "http://localhost:8080", |
| 22 | + "unknown": "http://localhost:11434", |
| 23 | + } |
| 24 | + for runtime, want := range cases { |
| 25 | + if got := DefaultURL(runtime); got != want { |
| 26 | + t.Errorf("DefaultURL(%q) = %q, want %q", runtime, got, want) |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestNodeIDStableAndPersisted(t *testing.T) { |
| 32 | + dir := t.TempDir() |
| 33 | + t.Setenv("XDG_CONFIG_HOME", dir) |
| 34 | + first := NodeID() |
| 35 | + if !strings.HasPrefix(first, "node-") { |
| 36 | + t.Errorf("NodeID = %q, want node- prefix", first) |
| 37 | + } |
| 38 | + if second := NodeID(); second != first { |
| 39 | + t.Errorf("NodeID not stable: %q vs %q", first, second) |
| 40 | + } |
| 41 | + // Persisted to disk and reused across the helper's path. |
| 42 | + if got := nodeIDPath(); got == "" || !strings.Contains(got, "tunnel-node-id") { |
| 43 | + t.Errorf("nodeIDPath = %q", got) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestNodeIDPathUsesHomeWhenNoXDG(t *testing.T) { |
| 48 | + t.Setenv("XDG_CONFIG_HOME", "") |
| 49 | + got := nodeIDPath() |
| 50 | + if got == "" || !strings.Contains(got, "latere") { |
| 51 | + t.Errorf("nodeIDPath without XDG = %q, want a path under the home config dir", got) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestDiscoverBadJSON(t *testing.T) { |
| 56 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 57 | + _, _ = w.Write([]byte("not json")) |
| 58 | + })) |
| 59 | + defer srv.Close() |
| 60 | + if _, err := discover(context.Background(), srv.Client(), RuntimeVLLM, srv.URL, nil); err == nil { |
| 61 | + t.Error("discover with bad JSON = nil error, want decode error") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestHandleClosedStream(t *testing.T) { |
| 66 | + f := &forwarder{ctx: context.Background(), client: &http.Client{}, upstream: "http://127.0.0.1:1"} |
| 67 | + c1, c2 := net.Pipe() |
| 68 | + _ = c2.Close() // peer hangs up before sending a request |
| 69 | + // handle must return without panicking on the ReadRequest error. |
| 70 | + done := make(chan struct{}) |
| 71 | + go func() { f.handle(c1); close(done) }() |
| 72 | + select { |
| 73 | + case <-done: |
| 74 | + case <-time.After(time.Second): |
| 75 | + t.Error("handle did not return on a closed stream") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestWriteErrorProducesBadGateway(t *testing.T) { |
| 80 | + c1, c2 := net.Pipe() |
| 81 | + go func() { |
| 82 | + writeError(c1, io.EOF) |
| 83 | + _ = c1.Close() |
| 84 | + }() |
| 85 | + resp, err := http.ReadResponse(bufio.NewReader(c2), nil) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("read response: %v", err) |
| 88 | + } |
| 89 | + defer resp.Body.Close() |
| 90 | + if resp.StatusCode != http.StatusBadGateway { |
| 91 | + t.Errorf("status = %d, want 502", resp.StatusCode) |
| 92 | + } |
| 93 | + body, _ := io.ReadAll(resp.Body) |
| 94 | + if !strings.Contains(string(body), "local.unreachable") { |
| 95 | + t.Errorf("body = %s, want local.unreachable", body) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestForwarderUnreachableUpstream(t *testing.T) { |
| 100 | + f := &forwarder{ctx: context.Background(), client: &http.Client{Timeout: time.Second}, upstream: "http://127.0.0.1:1"} |
| 101 | + c1, c2 := net.Pipe() |
| 102 | + go f.handle(c1) |
| 103 | + |
| 104 | + req, _ := http.NewRequest(http.MethodPost, "http://x/v1/chat/completions", strings.NewReader(`{}`)) |
| 105 | + go func() { _ = req.Write(c2) }() |
| 106 | + resp, err := http.ReadResponse(bufio.NewReader(c2), req) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("read response: %v", err) |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + if resp.StatusCode != http.StatusBadGateway { |
| 112 | + t.Errorf("status = %d, want 502 for unreachable upstream", resp.StatusCode) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestForwarderForwardsToUpstream(t *testing.T) { |
| 117 | + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 118 | + _, _ = w.Write([]byte("ok:" + r.URL.Path)) |
| 119 | + })) |
| 120 | + defer upstream.Close() |
| 121 | + |
| 122 | + f := &forwarder{ctx: context.Background(), client: &http.Client{}, upstream: upstream.URL} |
| 123 | + c1, c2 := net.Pipe() |
| 124 | + go f.handle(c1) |
| 125 | + req, _ := http.NewRequest(http.MethodPost, "http://x/v1/chat/completions", strings.NewReader(`{}`)) |
| 126 | + go func() { _ = req.Write(c2) }() |
| 127 | + resp, err := http.ReadResponse(bufio.NewReader(c2), req) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("read: %v", err) |
| 130 | + } |
| 131 | + defer resp.Body.Close() |
| 132 | + body, _ := io.ReadAll(resp.Body) |
| 133 | + if string(body) != "ok:/v1/chat/completions" { |
| 134 | + t.Errorf("body = %q", body) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestGetJSONErrors(t *testing.T) { |
| 139 | + // Non-200 surfaces an error. |
| 140 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 141 | + w.WriteHeader(http.StatusInternalServerError) |
| 142 | + })) |
| 143 | + defer srv.Close() |
| 144 | + var dst struct{} |
| 145 | + if err := getJSON(context.Background(), srv.Client(), srv.URL, &dst); err == nil { |
| 146 | + t.Error("getJSON on 500 = nil error, want error") |
| 147 | + } |
| 148 | + // Unreachable host surfaces an error. |
| 149 | + if err := getJSON(context.Background(), &http.Client{Timeout: time.Second}, "http://127.0.0.1:1/x", &dst); err == nil { |
| 150 | + t.Error("getJSON on unreachable = nil error, want error") |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestRunSessionDiscoverError(t *testing.T) { |
| 155 | + // An unreachable upstream makes discovery fail, so runSession returns |
| 156 | + // before any dial. |
| 157 | + err := runSession(context.Background(), Options{ |
| 158 | + LuxURL: "http://127.0.0.1:1", |
| 159 | + Bearer: func(context.Context) (string, error) { return "t", nil }, |
| 160 | + Runtime: RuntimeOllama, |
| 161 | + UpstreamURL: "http://127.0.0.1:1", |
| 162 | + Out: io.Discard, |
| 163 | + }) |
| 164 | + if err == nil { |
| 165 | + t.Error("runSession with unreachable upstream = nil error, want discovery error") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestRunStopsOnContextCancel(t *testing.T) { |
| 170 | + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) |
| 171 | + defer cancel() |
| 172 | + // Upstream unreachable so each session fails fast; Run should keep |
| 173 | + // retrying then return when the context expires. |
| 174 | + err := Run(ctx, Options{ |
| 175 | + LuxURL: "http://127.0.0.1:1", |
| 176 | + Bearer: func(context.Context) (string, error) { return "t", nil }, |
| 177 | + Runtime: RuntimeOllama, |
| 178 | + UpstreamURL: "http://127.0.0.1:1", |
| 179 | + HeartbeatInterval: time.Second, |
| 180 | + Out: io.Discard, |
| 181 | + }) |
| 182 | + if err == nil { |
| 183 | + t.Error("Run = nil error on context expiry, want ctx error") |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestHeartbeatLoopStopsOnContextCancel(t *testing.T) { |
| 188 | + c1, c2 := net.Pipe() |
| 189 | + defer c1.Close() |
| 190 | + defer c2.Close() |
| 191 | + ctx, cancel := context.WithCancel(context.Background()) |
| 192 | + done := make(chan struct{}) |
| 193 | + go func() { |
| 194 | + heartbeatLoop(ctx, c1, Options{ |
| 195 | + HeartbeatInterval: 10 * time.Millisecond, |
| 196 | + Bearer: func(context.Context) (string, error) { return "tok", nil }, |
| 197 | + }) |
| 198 | + close(done) |
| 199 | + }() |
| 200 | + // Drain the pipe so heartbeat writes don't block. |
| 201 | + go io.Copy(io.Discard, c2) |
| 202 | + time.Sleep(40 * time.Millisecond) |
| 203 | + cancel() |
| 204 | + select { |
| 205 | + case <-done: |
| 206 | + case <-time.After(time.Second): |
| 207 | + t.Error("heartbeatLoop did not stop on context cancel") |
| 208 | + } |
| 209 | +} |
0 commit comments