GitLabgitlab-org
cli
main
1package autoflowtest
3import (
4 "context"
5 "encoding/pem"
6 "net"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "path/filepath"
11 "sync/atomic"
12 "testing"
13 "time"
15 "github.com/golang-jwt/jwt/v5"
16 "github.com/stretchr/testify/require"
17 autoflowrpcv1 "gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/rpc/v1"
18 "go.uber.org/mock/gomock"
19 "google.golang.org/grpc"
20 "google.golang.org/grpc/credentials"
21 "google.golang.org/grpc/metadata"
23 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
24 "gitlab.com/gitlab-org/cli/internal/testing/tokenexchangetest"
25)
27// NamespaceID is the organization the tokens of NewTokenServer are issued
28// for, and so the namespace the tests address.
29const NamespaceID = "5b4a3928-1706-4f5e-8d4c-3b2a19081726"
31// Relay is a mocked AutoFlow service listening on Address with a self-signed
32// certificate, which CACertFile holds.
33type Relay struct {
34 Mock *MockAutoFlowServiceServer
35 Address string
36 CACertFile string
37}
39// NewRelay starts a Relay. A call the test did not expect fails it.
40func NewRelay(t *testing.T) *Relay {
41 t.Helper()
43 certSrc := httptest.NewTLSServer(http.NotFoundHandler())
44 t.Cleanup(certSrc.Close)
46 caCertFile := filepath.Join(t.TempDir(), "ca.pem")
47 require.NoError(t, os.WriteFile(caCertFile, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certSrc.Certificate().Raw}), 0o600))
49 relay := serve(t, grpc.Creds(credentials.NewServerTLSFromCert(&certSrc.TLS.Certificates[0])))
50 relay.CACertFile = caCertFile
51 return relay
52}
54// NewCleartextRelay starts a Relay without TLS. Its CACertFile is empty.
55func NewCleartextRelay(t *testing.T) *Relay {
56 t.Helper()
58 return serve(t)
59}
61func serve(t *testing.T, opts ...grpc.ServerOption) *Relay {
62 t.Helper()
64 lis, err := net.Listen("tcp", "127.0.0.1:0")
65 require.NoError(t, err)
67 mock := NewMockAutoFlowServiceServer(gomock.NewController(t))
68 srv := grpc.NewServer(opts...)
69 // The mock cannot embed UnimplementedAutoFlowServiceServer, so
70 // UnsafeAutoFlowServiceServer supplies the unexported method instead.
71 autoflowrpcv1.RegisterAutoFlowServiceServer(srv, struct {
72 autoflowrpcv1.UnsafeAutoFlowServiceServer
73 *MockAutoFlowServiceServer
74 }{MockAutoFlowServiceServer: mock})
75 go func() { _ = srv.Serve(lis) }()
76 t.Cleanup(srv.Stop)
78 return &Relay{Mock: mock, Address: lis.Addr().String()}
79}
81// Args returns the flags that address the relay.
82func (r *Relay) Args() string {
83 return "--address " + r.Address + " --ca-cert " + r.CACertFile
84}
86// Call is one RPC the relay received.
87type Call[Req any] struct {
88 Req Req
89 Auth []string
90}
92// Answer returns a mock action that answers with resp and err and sends the
93// call on calls. The call travels on a channel because the race detector
94// cannot see the socket ordering the RPC with the test's reads.
95func Answer[Req, Resp any](calls chan<- Call[Req], resp Resp, err error) func(context.Context, Req) (Resp, error) {
96 return func(ctx context.Context, req Req) (Resp, error) {
97 md, _ := metadata.FromIncomingContext(ctx)
98 calls <- Call[Req]{Req: req, Auth: md.Get("authorization")}
99 return resp, err
100 }
101}
103// Received returns the next call on calls, failing the test if there is none.
104func Received[Req any](t *testing.T, calls <-chan Call[Req]) Call[Req] {
105 t.Helper()
107 select {
108 case call := <-calls:
109 return call
110 default:
111 require.FailNow(t, "the RPC was not called")
112 return Call[Req]{}
113 }
114}
116// NewTokenServer starts a token exchange endpoint that answers with a
117// gitlab-autoflow token, which it returns along with a count of exchanges.
118func NewTokenServer(t *testing.T, gotBody *tokenexchangetest.WireRequest) (*httptest.Server, *atomic.Int32, string) {
119 t.Helper()
121 token := MakeToken(t, map[string]any{"origin": "organization", "origin_id": NamespaceID})
122 srv, count := tokenexchangetest.NewTokenExchangeServer(t, token, gotBody)
123 return srv, count, token
124}
126// MakeToken mints a gitlab-autoflow token with the gitlab claim set to
127// gitlabClaims.
128func MakeToken(t *testing.T, gitlabClaims map[string]any) string {
129 t.Helper()
131 token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
132 "iss": "https://gitlab.example.com",
133 "sub": "gid://gitlab/User/1",
134 "aud": "gitlab-autoflow",
135 "exp": time.Now().Add(time.Minute).Unix(),
136 "gitlab": gitlabClaims,
137 }).SignedString([]byte("test-signing-key"))
138 require.NoError(t, err)
139 return token
140}
142// NewTestExec wires cmdFunc to a token exchange endpoint that answers with
143// the token it returns.
144func NewTestExec(t *testing.T, cmdFunc cmdtest.CmdFunc, opts ...cmdtest.FactoryOption) (cmdtest.CmdExecFunc, string) {
145 t.Helper()
147 var gotBody tokenexchangetest.WireRequest
148 srv, _, token := NewTokenServer(t, &gotBody)
149 return tokenexchangetest.NewTestExec(t, srv, cmdFunc, opts...), token
150}
152// WriteFile writes content to a new file and returns its path.
153func WriteFile(t *testing.T, content string) string {
154 t.Helper()
156 path := filepath.Join(t.TempDir(), "file")
157 require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
158 return path
159}
Diagnostics7 git calls · 213 ms
CallStatusSizeTime0213 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0200233 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=233 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=be3b2f46264fd78e3aeec243c47ee7f44f4b940d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=233 git.pkt.payload.max=206 git.sideband.band1.bytes=206 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=206 git.pack.trailer=60d385382f0daa4bf5b182241b9c3f6f6404da58Open pack map
fetchtree:0200173 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=173 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=66861ec11d163fed4e1463454b1d8e5f9a413ae8 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=173 git.pkt.payload.max=146 git.sideband.band1.bytes=146 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=146 git.pack.trailer=ed83f2f286a703c2b5db0098a2b83adbb817fad3Open pack map
fetch2002.1 KiB198 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=198 git.response.bytes=2129 git.response.cache=MISS git.response.duration_ms=198 git.fetch.wants=0092b3f1554d31fa23c1eaac3d6d1be277a9e8b4 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2129 git.pkt.payload.max=2102 git.sideband.band1.bytes=2102 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e286ab39324af4
- Request
- d2bb3a34…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format