GitLabgitlab-org

cli

main
ply8 git calls246 ms99 KiB5 cachedprotocol v2
194 lines5.0 KiBRaw
1//go:build !integration
3package view
5import (
6 "regexp"
7 "testing"
8 "time"
10 "github.com/MakeNowJust/heredoc/v2"
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/testing/cmdtest"
21func Test_ReleaseView(t *testing.T) {
22 type testCase struct {
23 name string
24 cli string
25 wantErr bool
26 wantStderr string
27 setupMock func(tc *gitlabtesting.TestClient)
28 }
30 createdAt, _ := time.Parse(time.RFC3339, "2020-01-23T07:13:17.721Z")
31 releasedAt, _ := time.Parse(time.RFC3339, "2020-01-23T07:13:17.721Z")
33 testRelease := &gitlab.Release{
34 Name: "test_release",
35 TagName: "0.0.1",
36 Description: "",
37 CreatedAt: &createdAt,
38 ReleasedAt: &releasedAt,
39 UpcomingRelease: false,
40 Author: gitlab.BasicUser{
41 ID: 11809982,
42 Username: "test_user",
43 Name: "Test User",
44 State: "active",
45 AvatarURL: "https://gitlab.com/uploads/-/system/user/avatar/11809982/avatar.png",
46 WebURL: "https://gitlab.com/test_user",
47 },
48 Commit: gitlab.Commit{
49 ID: "26e80b26fd9f8515401a4d10c331904f034e9f05",
50 ShortID: "26e80b26",
51 Title: "Add new feature",
52 Message: "Added a great new feature",
53 AuthorName: "Test User",
54 AuthorEmail: "test_user@gitlab.com",
55 CommitterName: "test_user",
56 CommitterEmail: "test_user@gitlab.com",
57 WebURL: "https://gitlab.com/OWNER/REPO/-/commit/26e80b26fd9f8515401a4d10c331904f034e9f05",
58 },
59 CommitPath: "/OWNER/REPO/-/commit/26e80b26fd9f8515401a4d10c331904f034e9f05",
60 TagPath: "/OWNER/REPO/-/tags/0.0.1",
61 Assets: gitlab.ReleaseAssets{
62 Count: 2,
63 Sources: []gitlab.ReleaseAssetsSource{
64 {
65 Format: "zip",
66 URL: "https://gitlab.com/OWNER/REPO/-/archive/0.0.1/REPO-0.0.1.zip",
67 },
68 },
69 Links: []*gitlab.ReleaseLink{
70 {
71 ID: 1294469,
72 Name: "test asset",
73 URL: "https://gitlab.com/some/location/1133",
74 DirectAssetURL: "https://gitlab.com/OWNER/REPO/-/releases/0.0.1/downloads/test_asset",
75 LinkType: "other",
76 },
77 },
78 },
79 Links: gitlab.ReleaseLinks{
80 Self: "https://gitlab.com/OWNER/REPO/-/releases/0.0.1",
81 },
82 }
84 testCases := []testCase{
85 {
86 name: "view release with specific tag",
87 cli: "0.0.1",
88 setupMock: func(tc *gitlabtesting.TestClient) {
89 tc.MockReleases.EXPECT().
90 GetRelease("OWNER/REPO", "0.0.1", gomock.Any()).
91 Return(testRelease, nil, nil)
92 },
93 },
94 {
95 name: "view latest release",
96 cli: "",
97 setupMock: func(tc *gitlabtesting.TestClient) {
98 tc.MockReleases.EXPECT().
99 ListReleases("OWNER/REPO", gomock.Any()).
100 Return([]*gitlab.Release{testRelease}, nil, nil)
101 },
102 },
103 }
105 expectedOut := heredoc.Doc(`test_release
106 Test User released this about X years ago
107 26e80b26 - 0.0.1
111 ASSETS
112 test asset https://gitlab.com/OWNER/REPO/-/releases/0.0.1/downloads/test_asset
114 SOURCES
115 https://gitlab.com/OWNER/REPO/-/archive/0.0.1/REPO-0.0.1.zip
118 View this release on GitLab at https://gitlab.com/OWNER/REPO/-/releases/0.0.1
119 `)
121 for _, tc := range testCases {
122 t.Run(tc.name, func(t *testing.T) {
123 // GIVEN
124 testClient := gitlabtesting.NewTestClient(t)
125 tc.setupMock(testClient)
126 exec := cmdtest.SetupCmdForTest(
127 t,
128 NewCmdView,
129 false,
130 cmdtest.WithGitLabClient(testClient.Client),
131 )
133 // WHEN
134 out, err := exec(tc.cli)
136 // THEN
137 if tc.wantErr {
138 require.Error(t, err)
139 assert.Equal(t, tc.wantStderr, err.Error())
140 return
141 }
142 require.NoError(t, err)
144 // Normalize the time in the output
145 outStr := out.String()
146 timeRE := regexp.MustCompile(`\d+ years`)
147 outStr = timeRE.ReplaceAllString(outStr, "X years")
149 assert.Equal(t, expectedOut, outStr)
150 assert.Empty(t, out.Stderr())
151 })
152 }
155func TestReleaseView_JSON(t *testing.T) {
156 t.Parallel()
158 createdAt, _ := time.Parse(time.RFC3339, "2020-01-23T07:13:17.721Z")
159 releasedAt, _ := time.Parse(time.RFC3339, "2020-01-23T07:13:17.721Z")
161 testRelease := &gitlab.Release{
162 Name: "test_release",
163 TagName: "0.0.1",
164 Description: "Test description",
165 CreatedAt: &createdAt,
166 ReleasedAt: &releasedAt,
167 UpcomingRelease: false,
168 Author: gitlab.BasicUser{
169 ID: 11809982,
170 Username: "test_user",
171 Name: "Test User",
172 },
173 }
175 testClient := gitlabtesting.NewTestClient(t)
176 testClient.MockReleases.EXPECT().
177 GetRelease("OWNER/REPO", "0.0.1", gomock.Any()).
178 Return(testRelease, nil, nil)
180 exec := cmdtest.SetupCmdForTest(
181 t,
182 NewCmdView,
183 false,
184 cmdtest.WithGitLabClient(testClient.Client),
185 )
187 out, err := exec("0.0.1 --output json")
188 require.NoError(t, err)
190 assert.Contains(t, out.String(), `"tag_name":"0.0.1"`)
191 assert.Contains(t, out.String(), `"name":"test_release"`)
192 assert.Contains(t, out.String(), `"description":"Test description"`)
193 assert.Empty(t, out.Stderr())
Diagnostics8 git calls · 246 ms
CallStatusSizeTime0246 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=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 KiB2 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=UPDATING
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 · hit1 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=4273
git.response.cache=HIT
git.response.duration_ms=1
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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=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=fb03183b7909876778f740d196d7516811003517
Open 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=1232131042f2ba4c9c4dbd99382667a1f0849e15
Open pack map
fetchtree:0200168 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=233
git.response.bytes=168
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=7def0f4e0a74f8ffdb83c9e3bfb46139f12c1333
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=168
git.pkt.payload.max=141
git.sideband.band1.bytes=141
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=141
git.pack.trailer=10f4babb776ace75845c5f58e336a8d23cf85e0c
Open pack map
fetch2001.7 KiB234 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1706
git.response.cache=MISS
git.response.duration_ms=234
git.fetch.wants=c0dcc801ca3ef644b6addaef967c1d9709a3c09a
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=1706
git.pkt.payload.max=1679
git.sideband.band1.bytes=1679
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
a5e2cca0e85d7929
Request
8b6a2e5d…
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