GitLabgitlab-org

cli

main
ply5 git calls30 ms96 KiB4 cachedprotocol v2
151 lines3.4 KiBRaw
1package test
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "os/exec"
10 "regexp"
11 "testing"
13 "gitlab.com/gitlab-org/cli/internal/run"
16// TODO copypasta from command package
17type CmdOut struct {
18 OutBuf, ErrBuf *bytes.Buffer
21func (c CmdOut) String() string {
22 return c.OutBuf.String()
25func (c CmdOut) Stderr() string {
26 return c.ErrBuf.String()
29// OutputStub implements a simple utils.Runnable
30type OutputStub struct {
31 Out []byte
32 Error error
35func (s OutputStub) Output() ([]byte, error) {
36 if s.Error != nil {
37 return s.Out, s.Error
38 }
39 return s.Out, nil
42func (s OutputStub) Run() error {
43 if s.Error != nil {
44 return s.Error
45 }
46 return nil
49type CmdStubber struct {
50 Stubs []*OutputStub
51 Count int
52 Calls []*exec.Cmd
55func InitCmdStubber() (*CmdStubber, func()) {
56 cs := CmdStubber{}
57 teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs))
58 return &cs, teardown
61func (cs *CmdStubber) Stub(desiredOutput string) {
62 // TODO maybe have some kind of command mapping but going simple for now
63 cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput), nil})
66func (cs *CmdStubber) StubError(errText string) {
67 // TODO support error types beyond CmdError
68 stderrBuff := bytes.NewBufferString(errText)
69 args := []string{"stub"} // TODO make more real?
70 err := errors.New(errText)
71 cs.Stubs = append(cs.Stubs, &OutputStub{Error: &run.CmdError{
72 Stderr: stderrBuff,
73 Args: args,
74 Err: err,
75 }})
78func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
79 return func(cmd *exec.Cmd) run.Runnable {
80 cs.Calls = append(cs.Calls, cmd)
81 call := cs.Count
82 cs.Count += 1
83 if call >= len(cs.Stubs) {
84 panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd))
85 }
86 return cs.Stubs[call]
87 }
90type T interface {
91 Helper()
92 Errorf(string, ...any)
95func ExpectLines(t T, output string, lines ...string) {
96 t.Helper()
97 var r *regexp.Regexp
98 for _, l := range lines {
99 r = regexp.MustCompile(l)
100 if !r.MatchString(output) {
101 t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
102 return
103 }
104 }
107func ClearEnvironmentVariables(t *testing.T) {
108 t.Helper()
110 // prevent using environment variables for test
111 t.Setenv("GITLAB_TOKEN", "")
112 t.Setenv("VISUAL", "")
113 t.Setenv("EDITOR", "")
114 t.Setenv("GITLAB_ACCESS_TOKEN", "")
115 t.Setenv("OAUTH_TOKEN", "")
116 t.Setenv("USERNAME", "")
119func GetHostOrSkip(tb testing.TB) string {
120 tb.Helper()
122 glTestHost := os.Getenv("GITLAB_TEST_HOST")
123 if glTestHost == "" || os.Getenv("GITLAB_TOKEN_TEST") == "" {
124 // since token requires `api` privileges we only run integration tests in the canonical project
125 ciNamespace := os.Getenv("CI_PROJECT_NAMESPACE")
126 if os.Getenv("CI") == "true" && (ciNamespace == "gitlab-org/cli" || ciNamespace == "gitlab-org/security/cli") {
127 tb.Log("Expected GITLAB_TEST_HOST and GITLAB_TOKEN_TEST to be set in CI. Marking as failed.")
128 tb.Fail()
129 }
130 tb.Skip("Set GITLAB_TEST_HOST and GITLAB_TOKEN_TEST to run this integration test")
131 }
132 tb.Setenv("GITLAB_TOKEN", os.Getenv("GITLAB_TOKEN_TEST"))
133 return glTestHost
136func ReturnBuffer(old *os.File, r *os.File, w *os.File) string {
137 outC := make(chan string)
138 // copy the output in a separate goroutine so printing can't block indefinitely
139 go func() {
140 var buf bytes.Buffer
141 _, _ = io.Copy(&buf, r)
142 outC <- buf.String()
143 }()
145 // back to normal state
146 w.Close()
147 os.Stdout = old // restoring the real stdout
148 out := <-outC
150 return out
Diagnostics5 git calls · 30 ms
CallStatusSizeTime030 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
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 · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=6
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit8 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=8
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=8
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
fetch2001.5 KiB · hit20 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=20
git.response.bytes=1580
git.response.cache=HIT
git.response.duration_ms=20
git.fetch.wants=d21b1e5aed14d2e58351f979f378d7f158f3f488
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=1580
git.pkt.payload.max=1553
git.sideband.band1.bytes=1553
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
a5e129a2ffc77fd8
Request
9e833240…
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