GitLabgitlab-org
cli
main
1//go:build !integration
3package upload
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "go.uber.org/mock/gomock"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
15 "gitlab.com/gitlab-org/cli/internal/glinstance"
16 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
17)
19func TestReleaseUpload(t *testing.T) {
20 t.Parallel()
22 tests := []struct {
23 name string
24 cli string
26 wantType bool
27 expectedType gitlab.LinkTypeValue
28 }{
29 {
30 name: "when a file is uploaded using filename only, and does not send a link_type",
31 cli: "0.0.1 testdata/test_file.txt",
33 wantType: false,
34 },
35 {
36 name: "when a file is uploaded using a filename, display name and type",
37 cli: "0.0.1 testdata/test_file.txt#test_file#other",
39 wantType: true,
40 expectedType: gitlab.OtherLinkType,
41 },
42 {
43 name: "when a file is uploaded using a filename and type only",
44 cli: "0.0.1 testdata/test_file.txt##package",
46 wantType: true,
47 expectedType: gitlab.PackageLinkType,
48 },
49 }
51 for _, tc := range tests {
52 t.Run(tc.name, func(t *testing.T) {
53 t.Parallel()
55 testClient := gitlabtesting.NewTestClient(t, gitlab.WithBaseURL("https://"+glinstance.DefaultHostname))
57 // Mock GetRelease to validate the release exists
58 testClient.MockReleases.EXPECT().
59 GetRelease("OWNER/REPO", "0.0.1", gomock.Any()).
60 Return(&gitlab.Release{
61 Name: "test1",
62 TagName: "0.0.1",
63 }, nil, nil)
65 // Mock UploadProjectMarkdown for file upload
66 testClient.MockProjectMarkdownUploads.EXPECT().
67 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "test_file.txt", gomock.Any()).
68 Return(&gitlab.MarkdownUploadedFile{
69 Alt: "test_file",
70 URL: "/uploads/66dbcd21ec5d24ed6ea225176098d52b/test_file.txt",
71 FullPath: "/namespace1/project1/uploads/66dbcd21ec5d24ed6ea225176098d52b/test_file.txt",
72 Markdown: "",
73 }, nil, nil)
75 // Mock CreateReleaseLink and verify link_type
76 testClient.MockReleaseLinks.EXPECT().
77 CreateReleaseLink("OWNER/REPO", "0.0.1", gomock.Any(), gomock.Any()).
78 DoAndReturn(func(pid any, tagName string, opts *gitlab.CreateReleaseLinkOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ReleaseLink, *gitlab.Response, error) {
79 if tc.wantType {
80 require.NotNil(t, opts.LinkType)
81 assert.Equal(t, tc.expectedType, *opts.LinkType)
82 } else {
83 assert.Nil(t, opts.LinkType)
84 }
86 return &gitlab.ReleaseLink{
87 ID: 2,
88 Name: "test_file.txt",
89 URL: "https://gitlab.example.com/mynamespace/hello/-/jobs/688/artifacts/raw/testdata/test_file.txt",
90 DirectAssetURL: "https://gitlab.example.com/mynamespace/hello/-/releases/0.0.1/downloads/testdata/test_file.txt",
91 LinkType: gitlab.OtherLinkType,
92 }, nil, nil
93 })
95 exec := cmdtest.SetupCmdForTest(t, NewCmdUpload, false,
96 cmdtest.WithGitLabClient(testClient.Client),
97 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
98 )
100 output, err := exec(tc.cli)
102 if assert.NoErrorf(t, err, "error running command `release upload %s`: %v", tc.cli, err) {
103 assert.Contains(t, output.String(), `• Validating tag repo=OWNER/REPO tag=0.0.1
104• Uploading release assets repo=OWNER/REPO tag=0.0.1
105• Uploading to release file=testdata/test_file.txt name=test_file.txt
106✓ Upload succeeded after`)
107 assert.Empty(t, output.Stderr())
108 }
109 })
110 }
111}
113func TestReleaseUpload_WithAssetsLinksJSON(t *testing.T) {
114 t.Parallel()
116 tests := []struct {
117 name string
118 cli string
119 expectedOutput string
120 }{
121 {
122 name: "with direct_asset_path",
123 cli: `0.0.1 --assets-links='[{"name": "any-name", "url": "https://example.com/any-asset-url", "direct_asset_path": "/any-path"}]'`,
124 expectedOutput: `• Validating tag repo=OWNER/REPO tag=0.0.1
125• Uploading release assets repo=OWNER/REPO tag=0.0.1
126✓ Added release asset name=any-name url=https://gitlab.example.com/OWNER/REPO/releases/0.0.1/downloads/any-path
127✓ Upload succeeded after`,
128 },
129 {
130 name: "with filepath aliased to direct_asset_path",
131 cli: `0.0.1 --assets-links='[{"name": "any-name", "url": "https://example.com/any-asset-url", "filepath": "/any-path"}]'`,
132 expectedOutput: `• Validating tag repo=OWNER/REPO tag=0.0.1
133• Uploading release assets repo=OWNER/REPO tag=0.0.1
134✓ Added release asset name=any-name url=https://gitlab.example.com/OWNER/REPO/releases/0.0.1/downloads/any-path
135 ! Aliased deprecated ` + "`filepath`" + ` field to ` + "`direct_asset_path`" + `. Replace ` + "`filepath`" + ` with ` + "`direct_asset_path`" + ` name=any-name
136✓ Upload succeeded after`,
137 },
138 }
140 for _, tt := range tests {
141 t.Run(tt.name, func(t *testing.T) {
142 t.Parallel()
144 testClient := gitlabtesting.NewTestClient(t, gitlab.WithBaseURL("https://"+glinstance.DefaultHostname))
146 // Mock GetRelease to validate the release exists
147 testClient.MockReleases.EXPECT().
148 GetRelease("OWNER/REPO", "0.0.1", gomock.Any()).
149 Return(&gitlab.Release{
150 Name: "test1",
151 TagName: "0.0.1",
152 }, nil, nil)
154 // Mock CreateReleaseLink and verify the parameters
155 testClient.MockReleaseLinks.EXPECT().
156 CreateReleaseLink("OWNER/REPO", "0.0.1", gomock.Any(), gomock.Any()).
157 DoAndReturn(func(pid any, tagName string, opts *gitlab.CreateReleaseLinkOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ReleaseLink, *gitlab.Response, error) {
158 // Verify direct_asset_path is set and filepath is NOT set
159 assert.NotNil(t, opts.DirectAssetPath)
160 assert.Equal(t, "/any-path", *opts.DirectAssetPath)
162 return &gitlab.ReleaseLink{
163 ID: 1,
164 Name: "any-name",
165 URL: "https://example.com/any-asset-url",
166 DirectAssetURL: "https://gitlab.example.com/OWNER/REPO/releases/0.0.1/downloads/any-path",
167 LinkType: gitlab.OtherLinkType,
168 }, nil, nil
169 })
171 exec := cmdtest.SetupCmdForTest(t, NewCmdUpload, false,
172 cmdtest.WithGitLabClient(testClient.Client),
173 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
174 )
176 output, err := exec(tt.cli)
178 if assert.NoErrorf(t, err, "error running command `release upload %s`: %v", tt.cli, err) {
179 assert.Contains(t, output.String(), tt.expectedOutput)
180 assert.Empty(t, output.Stderr())
181 }
182 })
183 }
184}
186func TestReleaseUpload_InvalidAssetsLinksJSON(t *testing.T) {
187 t.Parallel()
189 tests := []struct {
190 name string
191 assetsLinks string
192 wantErr string
193 }{
194 {
195 name: "only a null link",
196 assetsLinks: `[null]`,
197 wantErr: "failed to parse JSON string: the asset link at index 0 is null",
198 },
199 {
200 name: "a null link after a valid one",
201 assetsLinks: `[{"name": "any-name", "url": "https://example.com/any-asset-url"}, null]`,
202 wantErr: "failed to parse JSON string: the asset link at index 1 is null",
203 },
204 {
205 name: "malformed JSON",
206 assetsLinks: `[{"name": "any-name"`,
207 wantErr: "failed to parse JSON string: unexpected end of JSON input",
208 },
209 }
211 for _, tt := range tests {
212 t.Run(tt.name, func(t *testing.T) {
213 t.Parallel()
215 // No expectations: the links are rejected before any request is sent.
216 testClient := gitlabtesting.NewTestClient(t)
218 exec := cmdtest.SetupCmdForTest(t, NewCmdUpload, false,
219 cmdtest.WithGitLabClient(testClient.Client),
220 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
221 )
223 _, err := exec(`0.0.1 --assets-links='` + tt.assetsLinks + `'`)
225 require.EqualError(t, err, tt.wantErr)
226 })
227 }
228}
Diagnostics8 git calls · 287 ms
CallStatusSizeTime0287 ms
discover200185 B4 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 · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=4 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=2 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=44Open pack map
fetchtree:02001.8 KiB · hit2 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=1857 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 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=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200350 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=350 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=b2a837e646ba305993f35203fafafb109e57a7ca 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=350 git.pkt.payload.max=323 git.sideband.band1.bytes=323 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=323 git.pack.trailer=1232131042f2ba4c9c4dbd99382667a1f0849e15Open pack map
fetchtree:0200170 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=271 git.response.bytes=170 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=bf82235aa6ec4e87a691af6db75446f20b210b48 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=170 git.pkt.payload.max=143 git.sideband.band1.bytes=143 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=143 git.pack.trailer=9d90a84ccb7108242bb8bc9cc50e207dd5587939Open pack map
fetch2001.9 KiB271 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1982 git.response.cache=MISS git.response.duration_ms=271 git.fetch.wants=803c701a2db6c9c45abeaa308a803cb24b7307d0 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=1982 git.pkt.payload.max=1955 git.sideband.band1.bytes=1955 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
- a5e2d054ab0aefad
- Request
- 7cc08204…
- 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