GitLabgitlab-org

cli

main
ply8 git calls369 ms99 KiB6 cachedprotocol v2
215 lines6.8 KiBRaw
1//go:build !integration
3package helpers
5import (
6 "fmt"
7 "slices"
8 "testing"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
18const repoName = "OWNER/REPO"
20func Test_GetPackageByName(t *testing.T) {
21 type testCase struct {
22 name string
23 packageName string
24 packageType string
25 packageVersion string
26 wantID int64
27 wantErr bool
28 wantErrMsg string
29 setupMock func(tc *gitlabtesting.TestClient)
30 }
32 packages := []*gitlab.Package{
33 {ID: 1, Name: "example", Version: "1.0.0", PackageType: "generic"},
34 {ID: 2, Name: "example", Version: "2.0.0", PackageType: "generic"},
35 {ID: 3, Name: "example", Version: "1.0.0", PackageType: "maven"},
36 }
38 expectList := func(tc *gitlabtesting.TestClient) {
39 tc.MockPackages.EXPECT().
40 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
41 Return(packages, &gitlab.Response{}, nil)
42 }
44 testCases := []testCase{
45 {
46 name: "package found by name, type and version",
47 packageName: "example",
48 packageType: "generic",
49 packageVersion: "2.0.0",
50 wantID: 2,
51 setupMock: expectList,
52 },
53 {
54 name: "same name and version but different type is distinguished",
55 packageName: "example",
56 packageType: "maven",
57 packageVersion: "1.0.0",
58 wantID: 3,
59 setupMock: expectList,
60 },
61 {
62 name: "filters are sent to the server",
63 packageName: "example",
64 packageType: "maven",
65 packageVersion: "1.0.0",
66 wantID: 3,
67 setupMock: func(tc *gitlabtesting.TestClient) {
68 name, pkgType, version := "example", "maven", "1.0.0"
69 tc.MockPackages.EXPECT().
70 ListProjectPackages(repoName, &gitlab.ListProjectPackagesOptions{
71 ListOptions: gitlab.ListOptions{Page: 1, PerPage: 100},
72 PackageName: &name,
73 PackageType: &pkgType,
74 PackageVersion: &version,
75 }, gomock.Any()).
76 Return(packages[2:], &gitlab.Response{}, nil)
77 },
78 },
79 {
80 name: "server-side name substring match is not treated as a match",
81 packageName: "exam",
82 packageType: "generic",
83 packageVersion: "1.0.0",
84 wantErr: true,
85 wantErrMsg: "couldn't locate package \"exam\" with package-type \"generic\" and version \"1.0.0\"",
86 setupMock: expectList,
87 },
88 {
89 name: "multiple matching packages is ambiguous",
90 packageName: "example",
91 packageType: "generic",
92 packageVersion: "1.0.0",
93 wantErr: true,
94 wantErrMsg: "found 2 packages named \"example\" with package-type \"generic\" and version \"1.0.0\" (IDs: 1, 4)",
95 setupMock: func(tc *gitlabtesting.TestClient) {
96 tc.MockPackages.EXPECT().
97 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
98 Return(append(slices.Clone(packages), &gitlab.Package{ID: 4, Name: "example", Version: "1.0.0", PackageType: "generic"}),
99 &gitlab.Response{}, nil)
100 },
101 },
102 {
103 name: "version does not match",
104 packageName: "example",
105 packageType: "generic",
106 packageVersion: "9.9.9",
107 wantErr: true,
108 wantErrMsg: "couldn't locate package \"example\" with package-type \"generic\" and version \"9.9.9\"",
109 setupMock: expectList,
110 },
111 {
112 name: "name does not match",
113 packageName: "missing",
114 packageType: "generic",
115 packageVersion: "1.0.0",
116 wantErr: true,
117 wantErrMsg: "couldn't locate package \"missing\" with package-type \"generic\" and version \"1.0.0\"",
118 setupMock: expectList,
119 },
120 {
121 name: "package found on a later page",
122 packageName: "example",
123 packageType: "generic",
124 packageVersion: "2.0.0",
125 wantID: 2,
126 setupMock: func(tc *gitlabtesting.TestClient) {
127 firstPage := tc.MockPackages.EXPECT().
128 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
129 Return(packages[:1], &gitlab.Response{NextPage: 2}, nil)
130 secondPage := tc.MockPackages.EXPECT().
131 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
132 Return(packages[1:], &gitlab.Response{}, nil)
133 gomock.InOrder(firstPage, secondPage)
134 },
135 },
136 {
137 name: "package not found on any page",
138 packageName: "example",
139 packageType: "generic",
140 packageVersion: "9.9.9",
141 wantErr: true,
142 wantErrMsg: "couldn't locate package \"example\" with package-type \"generic\" and version \"9.9.9\"",
143 setupMock: func(tc *gitlabtesting.TestClient) {
144 firstPage := tc.MockPackages.EXPECT().
145 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
146 Return(packages[:1], &gitlab.Response{NextPage: 2}, nil)
147 secondPage := tc.MockPackages.EXPECT().
148 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
149 Return(packages[1:], &gitlab.Response{}, nil)
150 gomock.InOrder(firstPage, secondPage)
151 },
152 },
153 {
154 name: "gitlab API error on a later page",
155 packageName: "example",
156 packageType: "generic",
157 packageVersion: "9.9.9",
158 wantErr: true,
159 wantErrMsg: "error fetching packages: " +
160 "GET https://gitlab.com/api/v4/projects/OWNER%2FREPO/packages: 500",
161 setupMock: func(tc *gitlabtesting.TestClient) {
162 firstPage := tc.MockPackages.EXPECT().
163 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
164 Return(packages[:1], &gitlab.Response{NextPage: 2}, nil)
165 secondPage := tc.MockPackages.EXPECT().
166 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
167 Return(nil, &gitlab.Response{}, fmt.Errorf(
168 "GET https://gitlab.com/api/v4/projects/OWNER%%2FREPO/packages: 500",
169 ))
170 gomock.InOrder(firstPage, secondPage)
171 },
172 },
173 {
174 name: "gitlab API error",
175 packageName: "example",
176 packageType: "generic",
177 packageVersion: "1.0.0",
178 wantErr: true,
179 wantErrMsg: "error fetching packages: " +
180 "GET https://gitlab.com/api/v4/projects/OWNER%2FREPO/packages: 500",
181 setupMock: func(tc *gitlabtesting.TestClient) {
182 tc.MockPackages.EXPECT().
183 ListProjectPackages(repoName, gomock.Any(), gomock.Any()).
184 Return(nil, &gitlab.Response{}, fmt.Errorf(
185 "GET https://gitlab.com/api/v4/projects/OWNER%%2FREPO/packages: 500",
186 ))
187 },
188 },
189 }
191 for _, tc := range testCases {
192 t.Run(tc.name, func(t *testing.T) {
193 // GIVEN
194 testClient := gitlabtesting.NewTestClient(t)
195 tc.setupMock(testClient)
197 // WHEN
198 pkg, err := GetPackageByName(t.Context(), testClient.Client, tc.packageName, tc.packageType, tc.packageVersion, repoName)
200 // THEN
201 if tc.wantErr {
202 require.Error(t, err)
203 assert.Equal(t, tc.wantErrMsg, err.Error())
204 return
205 }
207 require.NoError(t, err)
208 assert.Equal(t, tc.wantID, pkg.ID)
209 })
210 }
213func Test_PackageTypes(t *testing.T) {
214 assert.Contains(t, PackageTypes, DefaultPackageType)
Diagnostics8 git calls · 369 ms
CallStatusSizeTime0369 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 · 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=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
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:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=3
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:0200263 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=263
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=4445ce4c6a6710742ed094287b245091ae79204f
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=263
git.pkt.payload.max=236
git.sideband.band1.bytes=236
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=236
git.pack.trailer=4126ef8ff4c560b52ccdd91c9d0a185a77d21c2d
Open pack map
fetchtree:0200139 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=139
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=41efa46e4f99a06b863724ffd03d301ecbf77ef5
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=7e8bb1262cbf532927ded19c3fd9e7501476e069
Open pack map
fetch2001.4 KiB355 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=355
git.response.bytes=1463
git.response.cache=MISS
git.response.duration_ms=355
git.fetch.wants=9ded9bc6b4dac60c223a291c93a7773e783e2722
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=1463
git.pkt.payload.max=1436
git.sideband.band1.bytes=1436
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
a5e2e42d1b7b2947
Request
d566b984…
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