GitLabgitlab-org

cli

main
ply8 git calls342 ms99 KiB5 cachedprotocol v2
178 lines5.1 KiBRaw
1//go:build !integration
3package mirror
5import (
6 "errors"
7 "testing"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "go.uber.org/mock/gomock"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
16 "gitlab.com/gitlab-org/cli/internal/glinstance"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20func TestProjectMirror_PullMirror(t *testing.T) {
21 t.Parallel()
23 tc := gitlabtesting.NewTestClient(t)
25 // Mock getting the project details
26 tc.MockProjects.EXPECT().
27 GetProject("foo/bar", gomock.Any(), gomock.Any()).
28 Return(&gitlab.Project{
29 ID: 123,
30 PathWithNamespace: "foo/bar",
31 }, nil, nil)
33 // Mock updating the project to enable pull mirroring
34 tc.MockProjects.EXPECT().
35 EditProject(int64(123), gomock.Any(), gomock.Any()).
36 Return(&gitlab.Project{
37 ID: 123,
38 ImportURL: "https://gitlab.example.com/source/repo",
39 Mirror: true,
40 }, nil, nil)
42 exec := cmdtest.SetupCmdForTest(t, NewCmdMirror, true,
43 cmdtest.WithGitLabClient(tc.Client),
44 cmdtest.WithBaseRepo("foo", "bar", glinstance.DefaultHostname),
45 )
47 output, err := exec(`--direction=pull --url="https://gitlab.example.com/source/repo"`)
49 require.NoError(t, err)
50 assert.Contains(t, output.String(), "Created pull mirror")
51 assert.Contains(t, output.String(), "foo/bar")
54func TestProjectMirror_PushMirror(t *testing.T) {
55 t.Parallel()
57 tc := gitlabtesting.NewTestClient(t)
59 // Mock getting the project details
60 tc.MockProjects.EXPECT().
61 GetProject("foo/bar", gomock.Any(), gomock.Any()).
62 Return(&gitlab.Project{
63 ID: 123,
64 PathWithNamespace: "foo/bar",
65 }, nil, nil)
67 // Mock creating a push mirror
68 tc.MockProjectMirrors.EXPECT().
69 AddProjectMirror(int64(123), gomock.Any(), gomock.Any()).
70 Return(&gitlab.ProjectMirror{
71 ID: 456,
72 URL: "https://gitlab-backup.example.com/target/repo",
73 Enabled: true,
74 }, nil, nil)
76 exec := cmdtest.SetupCmdForTest(t, NewCmdMirror, true,
77 cmdtest.WithGitLabClient(tc.Client),
78 cmdtest.WithBaseRepo("foo", "bar", glinstance.DefaultHostname),
79 )
81 output, err := exec(`--direction=push --url="https://gitlab-backup.example.com/target/repo"`)
83 require.NoError(t, err)
84 assert.Contains(t, output.String(), "Created push mirror")
85 assert.Contains(t, output.String(), "foo/bar")
88func TestProjectMirror_ProjectNotFound(t *testing.T) {
89 t.Parallel()
91 tc := gitlabtesting.NewTestClient(t)
93 // Mock project not found
94 tc.MockProjects.EXPECT().
95 GetProject("foo/bar", gomock.Any(), gomock.Any()).
96 Return(nil, nil, errors.New("404 Project Not Found"))
98 exec := cmdtest.SetupCmdForTest(t, NewCmdMirror, true,
99 cmdtest.WithGitLabClient(tc.Client),
100 cmdtest.WithBaseRepo("foo", "bar", glinstance.DefaultHostname),
101 )
103 _, err := exec(`--direction=pull --url="https://gitlab.example.com/source/repo"`)
105 // Should error when project is not found
106 require.Error(t, err)
107 assert.Contains(t, err.Error(), "404")
110func TestProjectMirror_AllowDivergenceWithPull(t *testing.T) {
111 t.Parallel()
113 tc := gitlabtesting.NewTestClient(t)
115 // Mock getting the project details
116 tc.MockProjects.EXPECT().
117 GetProject("foo/bar", gomock.Any(), gomock.Any()).
118 Return(&gitlab.Project{
119 ID: 123,
120 PathWithNamespace: "foo/bar",
121 }, nil, nil)
123 // Mock updating the project
124 tc.MockProjects.EXPECT().
125 EditProject(int64(123), gomock.Any(), gomock.Any()).
126 Return(&gitlab.Project{
127 ID: 123,
128 ImportURL: "https://gitlab.example.com/source/repo",
129 Mirror: true,
130 }, nil, nil)
132 exec := cmdtest.SetupCmdForTest(t, NewCmdMirror, true,
133 cmdtest.WithGitLabClient(tc.Client),
134 cmdtest.WithBaseRepo("foo", "bar", glinstance.DefaultHostname),
135 )
137 output, err := exec(`--direction=pull --url="https://gitlab.example.com/source/repo" --allow-divergence`)
139 require.NoError(t, err)
140 // Should show warning that allow-divergence has no effect for pull mirrors
141 assert.Contains(t, output.String(), "[Warning]")
142 assert.Contains(t, output.String(), "allow-divergence")
143 assert.Contains(t, output.String(), "pull mirroring")
146func TestProjectMirror_ProtectedBranchesOnly(t *testing.T) {
147 t.Parallel()
149 tc := gitlabtesting.NewTestClient(t)
151 // Mock getting the project details
152 tc.MockProjects.EXPECT().
153 GetProject("foo/bar", gomock.Any(), gomock.Any()).
154 Return(&gitlab.Project{
155 ID: 123,
156 PathWithNamespace: "foo/bar",
157 }, nil, nil)
159 // Mock creating a push mirror with protected branches only
160 tc.MockProjectMirrors.EXPECT().
161 AddProjectMirror(int64(123), gomock.Any(), gomock.Any()).
162 Return(&gitlab.ProjectMirror{
163 ID: 456,
164 URL: "https://gitlab-backup.example.com/target/repo",
165 Enabled: true,
166 OnlyProtectedBranches: true,
167 }, nil, nil)
169 exec := cmdtest.SetupCmdForTest(t, NewCmdMirror, true,
170 cmdtest.WithGitLabClient(tc.Client),
171 cmdtest.WithBaseRepo("foo", "bar", glinstance.DefaultHostname),
172 )
174 output, err := exec(`--direction=push --url="https://gitlab-backup.example.com/target/repo" --protected-branches-only`)
176 require.NoError(t, err)
177 assert.Contains(t, output.String(), "Created push mirror")
Diagnostics8 git calls · 342 ms
CallStatusSizeTime0342 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 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=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=1
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:02001.8 KiB · hit1 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=1
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200614 B · 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=614
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=3a89d0cfc8e81880c982180388e42e561a0b7eee
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=614
git.pkt.payload.max=587
git.sideband.band1.bytes=587
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=587
git.pack.trailer=eb8250f8e8340ff2fafc22c5aba90db34fd906a9
Open pack map
fetchtree:0200145 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=329
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=e395af8aa4461b7aeed97c5dcfaac0f700a81dd0
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=145
git.pkt.payload.max=118
git.sideband.band1.bytes=118
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=118
git.pack.trailer=9136b8fb22290652e906998a8c95b23b9125e63d
Open pack map
fetch2001.0 KiB329 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1033
git.response.cache=MISS
git.response.duration_ms=329
git.fetch.wants=36bf67eaf934170d2f27e56c5183b5741bca8da0
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=1033
git.pkt.payload.max=1006
git.sideband.band1.bytes=1006
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
a5ea56a4a87e7089
Request
ed926d52…
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