GitLabgitlab-org

cli

main
ply6 git calls27 ms97 KiB5 cachedprotocol v2
258 lines7.7 KiBRaw
1package upload
3import (
4 "bytes"
5 "errors"
6 "io"
7 "os"
8 "path/filepath"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.uber.org/mock/gomock"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
21var (
22 pngContent = []byte("\x89PNG\r\n\x1a\nfake png body")
23 jpegContent = []byte("\xff\xd8\xff\xe0fake jpeg body")
26func testIOStreams() *iostreams.IOStreams {
27 return iostreams.New(
28 iostreams.WithStdin(io.NopCloser(&bytes.Buffer{}), false),
29 iostreams.WithStdout(io.Discard, false),
30 iostreams.WithStderr(io.Discard, false),
31 )
34func writeTempFile(t *testing.T, name string, content []byte) string {
35 t.Helper()
37 path := filepath.Join(t.TempDir(), name)
38 require.NoError(t, os.WriteFile(path, content, 0o600))
39 return path
42func TestAttachments_File(t *testing.T) {
43 t.Parallel()
45 path := writeTempFile(t, "diagram.png", pngContent)
47 attachments, err := Attachments(nil, []string{path})
48 require.NoError(t, err)
49 require.Len(t, attachments, 1)
50 assert.Equal(t, "diagram.png", attachments[0].Name)
52 content, err := attachments[0].Open()
53 require.NoError(t, err)
54 defer content.Close()
56 got, err := io.ReadAll(content)
57 require.NoError(t, err)
58 assert.Equal(t, pngContent, got)
61func TestAttachments_MultipleFilesKeepOrder(t *testing.T) {
62 t.Parallel()
64 first := writeTempFile(t, "first.png", pngContent)
65 second := writeTempFile(t, "second.jpg", jpegContent)
67 attachments, err := Attachments(nil, []string{first, second})
68 require.NoError(t, err)
69 require.Len(t, attachments, 2)
70 assert.Equal(t, "first.png", attachments[0].Name)
71 assert.Equal(t, "second.jpg", attachments[1].Name)
74func TestAttachments_MissingFile(t *testing.T) {
75 t.Parallel()
77 _, err := Attachments(nil, []string{filepath.Join(t.TempDir(), "absent.png")})
78 require.Error(t, err)
79 assert.Contains(t, err.Error(), "failed to read attachment")
82func TestAttachments_Directory(t *testing.T) {
83 t.Parallel()
85 dir := t.TempDir()
87 _, err := Attachments(nil, []string{dir})
88 require.Error(t, err)
89 assert.Contains(t, err.Error(), "is a directory")
92func TestAttachments_StdinSniffsPNGExtension(t *testing.T) {
93 t.Parallel()
95 attachments, err := Attachments(bytes.NewReader(pngContent), []string{StdinPath})
96 require.NoError(t, err)
97 require.Len(t, attachments, 1)
98 assert.Equal(t, "upload.png", attachments[0].Name)
101func TestAttachments_StdinSniffsJPEGExtension(t *testing.T) {
102 t.Parallel()
104 attachments, err := Attachments(bytes.NewReader(jpegContent), []string{StdinPath})
105 require.NoError(t, err)
106 require.Len(t, attachments, 1)
107 // Not ".jfif", which is what mime.ExtensionsByType would return first.
108 assert.Equal(t, "upload.jpg", attachments[0].Name)
111func TestAttachments_StdinUnrecognizedContentHasNoExtension(t *testing.T) {
112 t.Parallel()
114 attachments, err := Attachments(bytes.NewReader([]byte("just some notes")), []string{StdinPath})
115 require.NoError(t, err)
116 require.Len(t, attachments, 1)
117 assert.Equal(t, "upload", attachments[0].Name)
120func TestAttachments_StdinReadsFullContentAfterSniffing(t *testing.T) {
121 t.Parallel()
123 // Longer than the sniff window, to prove Peek did not consume the stream.
124 content := append(bytes.Clone(pngContent), bytes.Repeat([]byte("x"), sniffLen*3)...)
126 attachments, err := Attachments(bytes.NewReader(content), []string{StdinPath})
127 require.NoError(t, err)
128 require.Len(t, attachments, 1)
130 body, err := attachments[0].Open()
131 require.NoError(t, err)
132 defer body.Close()
134 got, err := io.ReadAll(body)
135 require.NoError(t, err)
136 assert.Equal(t, content, got)
139func TestAttachments_StdinEmpty(t *testing.T) {
140 t.Parallel()
142 _, err := Attachments(bytes.NewReader(nil), []string{StdinPath})
143 require.Error(t, err)
144 assert.Contains(t, err.Error(), "empty")
147func TestAttachments_StdinGivenTwice(t *testing.T) {
148 t.Parallel()
150 _, err := Attachments(bytes.NewReader(pngContent), []string{StdinPath, StdinPath})
151 require.Error(t, err)
152 assert.Contains(t, err.Error(), "can only be given once")
155func TestAttachments_NoPaths(t *testing.T) {
156 t.Parallel()
158 attachments, err := Attachments(nil, nil)
159 require.NoError(t, err)
160 assert.Empty(t, attachments)
163func TestUpload_ReturnsReferencesInOrder(t *testing.T) {
164 t.Parallel()
166 tc := gitlabtesting.NewTestClient(t)
167 tc.MockProjectMarkdownUploads.EXPECT().
168 UploadProjectMarkdown("group/project", gomock.Any(), "first.png", gomock.Any()).
169 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: "![first](/uploads/aaa/first.png)"}, nil, nil)
170 tc.MockProjectMarkdownUploads.EXPECT().
171 UploadProjectMarkdown("group/project", gomock.Any(), "second.png", gomock.Any()).
172 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: "![second](/uploads/bbb/second.png)"}, nil, nil)
174 references, err := Upload(t.Context(), testIOStreams(), tc.Client, "group/project", []Attachment{
175 staticAttachment("first.png", pngContent),
176 staticAttachment("second.png", pngContent),
177 })
178 require.NoError(t, err)
179 require.Len(t, references, 2)
180 assert.Equal(t, "![first](/uploads/aaa/first.png)", references[0])
181 assert.Equal(t, "![second](/uploads/bbb/second.png)", references[1])
184func TestUpload_FailureNamesAlreadyUploadedFiles(t *testing.T) {
185 t.Parallel()
187 tc := gitlabtesting.NewTestClient(t)
188 tc.MockProjectMarkdownUploads.EXPECT().
189 UploadProjectMarkdown("group/project", gomock.Any(), "first.png", gomock.Any()).
190 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: "![first](/uploads/aaa/first.png)"}, nil, nil)
191 tc.MockProjectMarkdownUploads.EXPECT().
192 UploadProjectMarkdown("group/project", gomock.Any(), "second.png", gomock.Any()).
193 Return(nil, nil, errors.New("413 Payload Too Large"))
195 _, err := Upload(t.Context(), testIOStreams(), tc.Client, "group/project", []Attachment{
196 staticAttachment("first.png", pngContent),
197 staticAttachment("second.png", pngContent),
198 })
199 require.Error(t, err)
200 assert.Contains(t, err.Error(), "failed to upload second.png")
201 assert.Contains(t, err.Error(), "413 Payload Too Large")
202 assert.Contains(t, err.Error(), "![first](/uploads/aaa/first.png)")
205func TestUpload_FirstFailureHasNothingToReuse(t *testing.T) {
206 t.Parallel()
208 tc := gitlabtesting.NewTestClient(t)
209 tc.MockProjectMarkdownUploads.EXPECT().
210 UploadProjectMarkdown("group/project", gomock.Any(), "only.png", gomock.Any()).
211 Return(nil, nil, errors.New("403 Forbidden"))
213 _, err := Upload(t.Context(), testIOStreams(), tc.Client, "group/project", []Attachment{
214 staticAttachment("only.png", pngContent),
215 })
216 require.Error(t, err)
217 assert.Contains(t, err.Error(), "failed to upload only.png")
218 assert.NotContains(t, err.Error(), "Already uploaded")
221func TestAppend_EmptyBodyUsesReferencesAlone(t *testing.T) {
222 t.Parallel()
224 assert.Equal(t, "![a](/uploads/a)", Append("", []string{"![a](/uploads/a)"}))
225 assert.Equal(t, "![a](/uploads/a)", Append(" \n ", []string{"![a](/uploads/a)"}))
228func TestAppend_SeparatesFromExistingBody(t *testing.T) {
229 t.Parallel()
231 assert.Equal(t, "Look at this.\n\n![a](/uploads/a)", Append("Look at this.", []string{"![a](/uploads/a)"}))
234func TestAppend_CollapsesTrailingNewlines(t *testing.T) {
235 t.Parallel()
237 assert.Equal(t, "Look at this.\n\n![a](/uploads/a)", Append("Look at this.\n\n\n", []string{"![a](/uploads/a)"}))
240func TestAppend_MultipleReferencesOnSeparateLines(t *testing.T) {
241 t.Parallel()
243 got := Append("Body.", []string{"![a](/uploads/a)", "![b](/uploads/b)"})
244 assert.Equal(t, "Body.\n\n![a](/uploads/a)\n![b](/uploads/b)", got)
247func TestAppend_NoReferencesLeavesBodyAlone(t *testing.T) {
248 t.Parallel()
250 assert.Equal(t, "Body.\n\n", Append("Body.\n\n", nil))
253func staticAttachment(name string, content []byte) Attachment {
254 return Attachment{
255 Name: name,
256 Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(content)), nil },
257 }
Diagnostics6 git calls · 27 ms
CallStatusSizeTime027 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=3
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:0 · deepen 1020032 KiB · hit3 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=3
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
fetchtree:0200139 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=19
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=d49ba2eccc7edfaff7ed3e62032e49b6b478cea7
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=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=04984e458394c157b6d8250a04d09c8e72b3716a
Open pack map
fetch2001.8 KiB · hit19 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1819
git.response.cache=HIT
git.response.duration_ms=19
git.fetch.wants=4958940e272ddf1b4c998d2f6a0fc133fd78e439
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=1819
git.pkt.payload.max=1792
git.sideband.band1.bytes=1792
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
a5e2329aebfba594
Request
2396e96a…
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