oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "net/http"
8 "strings"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/wire"
12 "github.com/coder/websocket"
13)
14
15// FakeRunner is a scripted runner stream client for instance contract
16// tests. It records every frame in both directions.
17type FakeRunner struct {
18 Conn *websocket.Conn
19 Welcome wire.Welcome
20 Received [][]byte
21 Sent [][]byte
22}
23
24// DialRunner connects and says hello with generation (protocol 1, the
25// slice 4 hello).
26func DialRunner(ctx context.Context, origin, token string, generation int64) (*FakeRunner, error) {
27 return DialRunnerHello(ctx, origin, token, wire.Hello{Generation: generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}})
28}
29
30// WorkerHello is a protocol 2 hello from a Mac worker, with the fixture's
31// capability report.
32func WorkerHello(generation int64) wire.Hello {
33 return wire.Hello{Generation: generation, Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}, Protocol: wire.Protocol2, Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Label: "Tito Studio", Sandbox: wire.SandboxSeatbelt,
34 Capabilities: &wire.Capabilities{Xcode: wire.Xcode{Version: "16.2", Build: "16C5032a"}, Simulators: []wire.Simulator{{Name: "iPhone 16 Pro", Runtime: "iOS 18.2", UDID: "5C0A1E2B-3D4F-4A5B-9C6D-7E8F90A1B2C3", State: "Shutdown"}}, Tags: []string{wire.TagMacOS, wire.TagXcode, wire.TagIOSSimulator},
35 Probes: []wire.Probe{{ID: "P1", Verdict: wire.ProbePassed, Detail: "xcode:16.2,16C5032a;ios_simulators:1;computer:Tito Studio"}, {ID: "P2", Verdict: wire.ProbeFailed, Detail: "allowed:test.keychain;srt_ran_xcode"}, {ID: "P3", Verdict: wire.ProbeSkipped}, {ID: "P4", Verdict: wire.ProbePassed}, {ID: "P5", Verdict: wire.ProbeSkipped}, {ID: "P6", Verdict: wire.ProbePassed}, {ID: "P7", Verdict: wire.ProbePassed}}}}
36}
37
38// DialRunnerHello connects and says hello.
39func DialRunnerHello(ctx context.Context, origin, token string, hello wire.Hello) (*FakeRunner, error) {
40 return DialRunnerFrom(ctx, origin, token, hello, nil)
41}
42
43// DialRunnerFrom is DialRunnerHello with extra request headers (an edge's
44// X-Forwarded-For).
45func DialRunnerFrom(ctx context.Context, origin, token string, hello wire.Hello, extra http.Header) (*FakeRunner, error) {
46 url := "ws" + strings.TrimPrefix(origin, "http") + wire.StreamPath
47 header := http.Header{"Authorization": {"Bearer " + token}}
48 for k, v := range extra {
49 header[k] = v
50 }
51 c, _, err := websocket.Dial(ctx, url, &websocket.DialOptions{HTTPHeader: header})
52 if err != nil {
53 return nil, err
54 }
55 c.SetReadLimit(wire.MaxFrameBytes)
56 r := &FakeRunner{Conn: c}
57 if err = r.Send(ctx, "h0", wire.FrameHello, hello); err != nil {
58 return nil, err
59 }
60 f, body, err := r.Next(ctx)
61 if err != nil {
62 return nil, err
63 }
64 w, ok := body.(*wire.Welcome)
65 if !ok || f.Type != wire.FrameWelcome {
66 return nil, errors.New("no welcome")
67 }
68 r.Welcome = *w
69 return r, nil
70}
71
72func (r *FakeRunner) Send(ctx context.Context, id, kind string, body any) error {
73 f, err := wire.NewFrame(id, kind, body)
74 if err != nil {
75 return err
76 }
77 raw, _ := json.Marshal(f)
78 r.Sent = append(r.Sent, raw)
79 return r.Conn.Write(ctx, websocket.MessageText, raw)
80}
81
82// frameWait bounds the wait for one frame. A loaded CI runner (load 40
83// and up) has taken more than 10 seconds to send the next frame of a
84// transfer contract; a wait that ends closes the stream, so it fails the
85// test rather than a slow box being the verdict.
86const frameWait = 45 * time.Second
87
88// Next reads one frame.
89func (r *FakeRunner) Next(ctx context.Context) (wire.Frame, any, error) {
90 ctx, cancel := context.WithTimeout(ctx, frameWait)
91 defer cancel()
92 _, raw, err := r.Conn.Read(ctx)
93 if err != nil {
94 return wire.Frame{}, nil, err
95 }
96 r.Received = append(r.Received, raw)
97 return wire.ParseFrame(raw)
98}
99
100// Expect reads frames until one of kind arrives, skipping acks unless
101// kind is ack.
102func (r *FakeRunner) Expect(ctx context.Context, kind string) (wire.Frame, any, error) {
103 for {
104 f, body, err := r.Next(ctx)
105 if err != nil || f.Type == kind {
106 return f, body, err
107 }
108 if f.Type != wire.FrameAck {
109 return f, body, errors.New("unexpected frame " + f.Type)
110 }
111 }
112}
113
114func (r *FakeRunner) Close() { r.Conn.CloseNow() }
115
116// CloseStatus is the close code the instance sent, if any.
117func CloseStatus(err error) websocket.StatusCode { return websocket.CloseStatus(err) }
118