//go:build e2elive package e2elivetest import ( "bytes" "encoding/json" "io" "net/http" "net/http/httptest" "net/http/httputil" "net/url" "os" "path/filepath" "strings" "sync" "testing" ) type livePacket struct { Path string `json:"path"` Request json.RawMessage `json:"request"` Response json.RawMessage `json:"response"` } func liveRecordingProxy(t *testing.T, target string) (string, func()) { t.Helper() u, err := url.Parse(target) executionMust(t, err) proxy := httputil.NewSingleHostReverseProxy(u) var mu sync.Mutex packets := map[string]livePacket{} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Nothing from headers (bearers, cookies or enrollment secrets) is recorded. if !strings.HasPrefix(r.URL.Path, "/api/runner/v2/") { proxy.ServeHTTP(w, r) return } body, e := io.ReadAll(r.Body) if e != nil { http.Error(w, "read", 500) return } r.Body = io.NopCloser(bytes.NewReader(body)) capture := httptest.NewRecorder() proxy.ServeHTTP(capture, r) for k, v := range capture.Header() { w.Header()[k] = v } w.WriteHeader(capture.Code) _, _ = w.Write(capture.Body.Bytes()) if capture.Code != 200 { return } key := "" switch r.URL.Path { case "/api/runner/v2/heartbeat": key = "heartbeat" case "/api/runner/v2/coordinator/poll": if !bytes.Contains(capture.Body.Bytes(), []byte(`"result":[]`)) { key = "poll" } case "/api/runner/v2/coordinator/claim": key = "claim" case "/api/runner/v2/coordinator/tool": if bytes.Contains(body, []byte(`"name":"post_message"`)) { key = "reply" } case "/api/runner/v2/coordinator/event": if bytes.Contains(body, []byte(`"kind":"coordinator.delivery"`)) { key = "delivery" } else if bytes.Contains(body, []byte(`"kind":"coordinator.text"`)) { key = "text" } } if key != "" { mu.Lock() if _, ok := packets[key]; !ok { packets[key] = livePacket{r.URL.Path, body, append([]byte(nil), capture.Body.Bytes()...)} } mu.Unlock() } })) t.Cleanup(server.Close) return server.URL, func() { dir := os.Getenv("EQUIPO_E2E_RECORD") if dir == "" { return } mu.Lock() defer mu.Unlock() if len(packets) != 6 { t.Fatalf("stage wire recording: got %d contracts", len(packets)) } executionMust(t, os.MkdirAll(dir, 0700)) var claim struct { Result struct { Admission struct{ Dispatch struct{ Key string } } } } executionMust(t, json.Unmarshal(packets["claim"].Response, &claim)) var beat struct { Reports []struct { Routing struct { Home string `json:"credential_home_hash"` } } } executionMust(t, json.Unmarshal(packets["heartbeat"].Request, &beat)) for key, p := range packets { raw, e := json.MarshalIndent(p, "", " ") executionMust(t, e) raw = bytes.ReplaceAll(raw, []byte("paul@tito.io"), []byte("owner@example.test")) if claim.Result.Admission.Dispatch.Key != "" { raw = bytes.ReplaceAll(raw, []byte(claim.Result.Admission.Dispatch.Key), []byte("fixture-admission-key")) } for _, report := range beat.Reports { if report.Routing.Home != "" { raw = bytes.ReplaceAll(raw, []byte(report.Routing.Home), []byte(strings.Repeat("b", 64))) } } executionMust(t, os.WriteFile(filepath.Join(dir, key+".json"), append(raw, '\n'), 0600)) } } }