GitLabgitlab-org
cli
main
1//go:build !integration
3package download
5import (
6 "fmt"
7 "io/fs"
8 "net/http"
9 "net/http/httptest"
10 "os"
11 "path/filepath"
12 "strings"
13 "sync/atomic"
14 "testing"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18 "go.uber.org/mock/gomock"
20 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
21 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
23 "gitlab.com/gitlab-org/cli/internal/commands/release/releaseutils/upload"
24 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
25)
27const (
28 assetName = "asset.tar.gz"
29 localContent = "local_content"
30 downloadedContent = "downloaded_content"
31)
33// serveAsset answers every request with status and, on success,
34// downloadedContent. The server is on a different host from the GitLab
35// client, so downloads go through the unauthenticated HTTP path.
36func serveAsset(t *testing.T, status int) (string, *atomic.Int32) {
37 t.Helper()
39 var hits atomic.Int32
40 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
41 hits.Add(1)
42 w.WriteHeader(status)
43 if status == http.StatusOK {
44 _, _ = w.Write([]byte(downloadedContent))
45 }
46 }))
47 t.Cleanup(testServer.Close)
49 return testServer.URL + "/" + assetName, &hits
50}
52func downloadOneAsset(t *testing.T, dir, assetURL string, onExisting existingFileAction) (string, error) {
53 t.Helper()
55 gitlabClient, err := gitlab.NewClient("test-token")
56 require.NoError(t, err)
58 name := assetName
59 io, _, stdout, _ := cmdtest.TestIOStreams()
61 assets := []*upload.ReleaseAsset{{Name: &name, URL: &assetURL}}
62 err = downloadAssets(t.Context(), gitlabClient, io, assets, dir, onExisting)
64 return stdout.String(), err
65}
67func writeLocalFile(t *testing.T, path string) {
68 t.Helper()
69 require.NoError(t, os.WriteFile(path, []byte(localContent), 0o644))
70}
72func assertFileContent(t *testing.T, path, want string) {
73 t.Helper()
74 content, err := os.ReadFile(path)
75 require.NoError(t, err)
76 assert.Equal(t, want, string(content))
77}
79// assertDirEntries also catches a temporary file that was not removed.
80func assertDirEntries(t *testing.T, dir string, want ...string) {
81 t.Helper()
82 entries, err := os.ReadDir(dir)
83 require.NoError(t, err)
85 got := make([]string, 0, len(entries))
86 for _, entry := range entries {
87 got = append(got, entry.Name())
88 }
89 assert.ElementsMatch(t, want, got)
90}
92func TestDownload_ExistingFile(t *testing.T) {
93 t.Parallel()
95 t.Run("without a flag it fails and keeps the file", func(t *testing.T) {
96 t.Parallel()
97 dir := t.TempDir()
98 path := filepath.Join(dir, assetName)
99 writeLocalFile(t, path)
100 assetURL, hits := serveAsset(t, http.StatusOK)
102 _, err := downloadOneAsset(t, dir, assetURL, failOnExisting)
104 require.ErrorIs(t, err, fs.ErrExist)
105 require.EqualError(t, err, fmt.Sprintf("file %q already exists. Use '--clobber' to overwrite it, or '--skip-existing' to keep it: file already exists", path))
106 assertFileContent(t, path, localContent)
107 assertDirEntries(t, dir, assetName)
108 assert.Zero(t, hits.Load(), "the asset must not be requested")
109 })
111 t.Run("--clobber overwrites the file", func(t *testing.T) {
112 t.Parallel()
113 dir := t.TempDir()
114 path := filepath.Join(dir, assetName)
115 require.NoError(t, os.WriteFile(path, []byte("stale_content_that_is_longer"), 0o644))
116 assetURL, _ := serveAsset(t, http.StatusOK)
118 _, err := downloadOneAsset(t, dir, assetURL, overwriteExisting)
119 require.NoError(t, err)
121 // A shorter download must not leave a tail of the previous file.
122 assertFileContent(t, path, downloadedContent)
123 assertDirEntries(t, dir, assetName)
124 })
126 t.Run("--clobber keeps the file when the server fails", func(t *testing.T) {
127 t.Parallel()
128 dir := t.TempDir()
129 path := filepath.Join(dir, assetName)
130 writeLocalFile(t, path)
131 assetURL, _ := serveAsset(t, http.StatusInternalServerError)
133 _, err := downloadOneAsset(t, dir, assetURL, overwriteExisting)
135 require.Error(t, err)
136 assertFileContent(t, path, localContent)
137 assertDirEntries(t, dir, assetName)
138 })
140 t.Run("--clobber replaces a symlink, not its target", func(t *testing.T) {
141 t.Parallel()
142 dir := t.TempDir()
143 target := filepath.Join(t.TempDir(), "target")
144 writeLocalFile(t, target)
145 path := filepath.Join(dir, assetName)
146 if err := os.Symlink(target, path); err != nil {
147 t.Skipf("cannot create a symlink: %v", err)
148 }
149 assetURL, _ := serveAsset(t, http.StatusOK)
151 _, err := downloadOneAsset(t, dir, assetURL, overwriteExisting)
152 require.NoError(t, err)
154 assertFileContent(t, target, localContent)
155 info, err := os.Lstat(path)
156 require.NoError(t, err)
157 assert.True(t, info.Mode().IsRegular(), "the symlink must be replaced by a regular file")
158 assertFileContent(t, path, downloadedContent)
159 })
161 t.Run("--skip-existing keeps the file and reports it", func(t *testing.T) {
162 t.Parallel()
163 dir := t.TempDir()
164 path := filepath.Join(dir, assetName)
165 writeLocalFile(t, path)
166 assetURL, hits := serveAsset(t, http.StatusOK)
168 output, err := downloadOneAsset(t, dir, assetURL, keepExisting)
169 require.NoError(t, err)
171 assertFileContent(t, path, localContent)
172 assert.Contains(t, output, "skipping file")
173 assert.Zero(t, hits.Load(), "the asset must not be requested")
174 })
176 t.Run("--skip-existing downloads a missing file", func(t *testing.T) {
177 t.Parallel()
178 dir := t.TempDir()
179 assetURL, _ := serveAsset(t, http.StatusOK)
181 _, err := downloadOneAsset(t, dir, assetURL, keepExisting)
182 require.NoError(t, err)
184 assertFileContent(t, filepath.Join(dir, assetName), downloadedContent)
185 })
187 t.Run("--clobber downloads a missing file", func(t *testing.T) {
188 t.Parallel()
189 dir := t.TempDir()
190 assetURL, _ := serveAsset(t, http.StatusOK)
192 _, err := downloadOneAsset(t, dir, assetURL, overwriteExisting)
193 require.NoError(t, err)
195 assertFileContent(t, filepath.Join(dir, assetName), downloadedContent)
196 })
197}
199// The temporary file name must not grow with the asset name, or an asset
200// name near the 255-byte file name limit fails with ENAMETOOLONG.
201func TestDownload_LongAssetName(t *testing.T) {
202 t.Parallel()
203 dir := t.TempDir()
204 name := strings.Repeat("a", 250)
205 assetURL, _ := serveAsset(t, http.StatusOK)
207 gitlabClient, err := gitlab.NewClient("test-token")
208 require.NoError(t, err)
210 err = downloadAsset(t.Context(), gitlabClient, assetURL, filepath.Join(dir, name), failOnExisting)
211 require.NoError(t, err)
213 assertFileContent(t, filepath.Join(dir, name), downloadedContent)
214 assertDirEntries(t, dir, name)
215}
217func TestDownload_FailedDownloadLeavesNoFile(t *testing.T) {
218 t.Parallel()
220 tests := []struct {
221 name string
222 onExisting existingFileAction
223 }{
224 {name: "no flag", onExisting: failOnExisting},
225 {name: "--clobber", onExisting: overwriteExisting},
226 {name: "--skip-existing", onExisting: keepExisting},
227 }
229 for _, tt := range tests {
230 t.Run(tt.name, func(t *testing.T) {
231 t.Parallel()
232 dir := t.TempDir()
233 assetURL, _ := serveAsset(t, http.StatusInternalServerError)
235 _, err := downloadOneAsset(t, dir, assetURL, tt.onExisting)
237 require.Error(t, err)
238 assertDirEntries(t, dir)
239 })
240 }
241}
243// --skip-existing must not keep a file that a failed run left behind, or a
244// re-run can never complete the download.
245func TestDownload_SkipExistingAfterFailedDownload(t *testing.T) {
246 t.Parallel()
247 dir := t.TempDir()
249 failingURL, _ := serveAsset(t, http.StatusInternalServerError)
250 _, err := downloadOneAsset(t, dir, failingURL, keepExisting)
251 require.Error(t, err)
253 assetURL, hits := serveAsset(t, http.StatusOK)
254 _, err = downloadOneAsset(t, dir, assetURL, keepExisting)
255 require.NoError(t, err)
257 assertFileContent(t, filepath.Join(dir, assetName), downloadedContent)
258 assert.Equal(t, int32(1), hits.Load())
259}
261func TestDownloadCommand_ExistingFileFlags(t *testing.T) {
262 t.Parallel()
264 tests := []struct {
265 name string
266 flags string
267 wantErr bool
268 wantContent string
269 wantHits int32
270 }{
271 {name: "no flag", flags: "", wantErr: true, wantContent: localContent, wantHits: 0},
272 {name: "--clobber", flags: "--clobber", wantContent: downloadedContent, wantHits: 1},
273 {name: "--skip-existing", flags: "--skip-existing", wantContent: localContent, wantHits: 0},
274 }
276 for _, tt := range tests {
277 t.Run(tt.name, func(t *testing.T) {
278 t.Parallel()
279 dir := t.TempDir()
280 path := filepath.Join(dir, assetName)
281 writeLocalFile(t, path)
282 assetURL, hits := serveAsset(t, http.StatusOK)
284 testClient := gitlabtesting.NewTestClient(t, gitlab.WithBaseURL("https://gitlab.example.com"))
285 testClient.MockReleases.EXPECT().
286 GetRelease("OWNER/REPO", "v1.0.0", gomock.Any()).
287 Return(&gitlab.Release{
288 TagName: "v1.0.0",
289 Name: "Release v1.0.0",
290 Assets: gitlab.ReleaseAssets{
291 Links: []*gitlab.ReleaseLink{{Name: assetName, URL: assetURL}},
292 },
293 }, nil, nil)
295 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
296 cmdtest.WithGitLabClient(testClient.Client),
297 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
298 )
300 _, err := exec(fmt.Sprintf("v1.0.0 --dir '%s' %s", dir, tt.flags))
302 if tt.wantErr {
303 require.ErrorContains(t, err, fmt.Sprintf("file %q already exists.", path))
304 } else {
305 require.NoError(t, err)
306 }
307 assertFileContent(t, path, tt.wantContent)
308 assert.Equal(t, tt.wantHits, hits.Load())
309 })
310 }
311}
313func TestDownload_ClobberAndSkipExistingAreMutuallyExclusive(t *testing.T) {
314 t.Parallel()
316 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
317 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
318 )
320 _, err := exec("v1.0.0 --clobber --skip-existing")
322 require.Error(t, err)
323 assert.Contains(t, err.Error(), "clobber")
324 assert.Contains(t, err.Error(), "skip-existing")
325}
Diagnostics8 git calls · 313 ms
CallStatusSizeTime0313 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 KiB3 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=UPDATING 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
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=3 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=2 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 · 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=350 git.response.cache=HIT git.response.duration_ms=3 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:0200178 B · 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=178 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=17d7a4e972314f40abefc6eac0a09f9eb4ccf7cc 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=178 git.pkt.payload.max=151 git.sideband.band1.bytes=151 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=151 git.pack.trailer=a4a10e7178122a1f1b1ad28fec85c87cc0de9191Open pack map
fetch2002.6 KiB300 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=300 git.response.bytes=2663 git.response.cache=MISS git.response.duration_ms=300 git.fetch.wants=fa1d06804cc1ac047dd10f75c3b6774552445c1e 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=2663 git.pkt.payload.max=2636 git.sideband.band1.bytes=2636 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
- a5e303cd89877929
- Request
- bc253cdf…
- 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