FakeBuildLogRecorder starts a server that fakes a Coder deployment for the purpose of pushing build logs. It returns a type for asserting that expected log make it through the expected endpoint.
(t testing.TB, l net.Listener, cert tls.Certificate)
| 49 | // It returns a type for asserting that expected log |
| 50 | // make it through the expected endpoint. |
| 51 | func FakeBuildLogRecorder(t testing.TB, l net.Listener, cert tls.Certificate) *BuildLogRecorder { |
| 52 | t.Helper() |
| 53 | |
| 54 | recorder := &BuildLogRecorder{} |
| 55 | mux := http.NewServeMux() |
| 56 | mux.Handle("/api/v2/buildinfo", http.HandlerFunc( |
| 57 | func(w http.ResponseWriter, _ *http.Request) { |
| 58 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 59 | w.WriteHeader(http.StatusOK) |
| 60 | |
| 61 | enc := json.NewEncoder(w) |
| 62 | enc.SetEscapeHTML(true) |
| 63 | |
| 64 | // We can't really do much about these errors, it's probably due to a |
| 65 | // dropped connection. |
| 66 | _ = enc.Encode(&codersdk.BuildInfoResponse{ |
| 67 | Version: "v1.0.0", |
| 68 | }) |
| 69 | })) |
| 70 | |
| 71 | mux.Handle("/api/v2/workspaceagents/me/logs", http.HandlerFunc( |
| 72 | func(w http.ResponseWriter, r *http.Request) { |
| 73 | var logs agentsdk.PatchLogs |
| 74 | err := json.NewDecoder(r.Body).Decode(&logs) |
| 75 | require.NoError(t, err) |
| 76 | w.WriteHeader(http.StatusOK) |
| 77 | for _, log := range logs.Logs { |
| 78 | recorder.append(log.Output) |
| 79 | } |
| 80 | })) |
| 81 | |
| 82 | mux.Handle("/", http.HandlerFunc( |
| 83 | func(_ http.ResponseWriter, r *http.Request) { |
| 84 | t.Fatalf("unexpected route %v", r.URL.Path) |
| 85 | })) |
| 86 | |
| 87 | s := httptest.NewUnstartedServer(mux) |
| 88 | s.Listener = l |
| 89 | if cert.Certificate != nil { |
| 90 | //nolint:gosec |
| 91 | s.TLS = &tls.Config{ |
| 92 | Certificates: []tls.Certificate{cert}, |
| 93 | } |
| 94 | s.StartTLS() |
| 95 | } else { |
| 96 | s.Start() |
| 97 | } |
| 98 | |
| 99 | t.Cleanup(s.Close) |
| 100 | return recorder |
| 101 | } |