GitLabgitlab-org

cli

main
ply8 git calls197 ms99 KiB5 cachedprotocol v2
175 lines4.5 KiBRaw
1//go:build !integration
3package get
5import (
6 "errors"
7 "testing"
8 "time"
10 "github.com/spf13/cobra"
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/api"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22func Test_GetSSHKey(t *testing.T) {
23 type testCase struct {
24 Name string
25 ExpectedMsg []string
26 wantErr bool
27 cli string
28 wantStderr string
29 setupMock func(tc *gitlabtesting.TestClient)
30 }
32 testKey := &gitlab.SSHKey{
33 ID: 123,
34 Key: "ssh-ed25519 example",
35 CreatedAt: new(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)),
36 UsageType: "auth_and_signing",
37 Title: "mysshkey",
38 }
40 testCases := []testCase{
41 {
42 Name: "Get SSH key by ID",
43 ExpectedMsg: []string{"ssh-ed25519 example"},
44 cli: "123",
45 setupMock: func(tc *gitlabtesting.TestClient) {
46 tc.MockUsers.EXPECT().GetSSHKey(int64(123)).Return(testKey, nil, nil)
47 },
48 },
49 {
50 Name: "Get SSH key without ID",
51 cli: "",
52 wantErr: true,
53 wantStderr: "the <key-id> argument is required when prompts are disabled",
54 setupMock: func(tc *gitlabtesting.TestClient) {},
55 },
56 }
58 for _, tc := range testCases {
59 t.Run(tc.Name, func(t *testing.T) {
60 // GIVEN
61 testClient := gitlabtesting.NewTestClient(t)
62 tc.setupMock(testClient)
63 exec := cmdtest.SetupCmdForTest(
64 t,
65 NewCmdGet,
66 false,
67 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
68 )
70 // WHEN
71 out, err := exec(tc.cli)
73 // THEN
74 if tc.wantErr {
75 require.Error(t, err)
76 assert.Contains(t, err.Error(), tc.wantStderr)
77 return
78 }
79 require.NoError(t, err)
80 for _, msg := range tc.ExpectedMsg {
81 assert.Contains(t, out.OutBuf.String(), msg)
82 }
83 })
84 }
87func TestSshKeyGet_JSON(t *testing.T) {
88 t.Parallel()
90 testKey := &gitlab.SSHKey{
91 ID: 123,
92 Key: "ssh-ed25519 example",
93 CreatedAt: new(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)),
94 UsageType: "auth_and_signing",
95 Title: "mysshkey",
96 }
98 testClient := gitlabtesting.NewTestClient(t)
99 testClient.MockUsers.EXPECT().
100 GetSSHKey(int64(123)).
101 Return(testKey, nil, nil)
103 exec := cmdtest.SetupCmdForTest(
104 t,
105 NewCmdGet,
106 false,
107 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
108 )
110 out, err := exec("123 --output json")
111 require.NoError(t, err)
113 assert.Contains(t, out.String(), `"id":123`)
114 assert.Contains(t, out.String(), `"title":"mysshkey"`)
115 assert.Contains(t, out.String(), `"key":"ssh-ed25519 example"`)
116 assert.Contains(t, out.String(), `"usage_type":"auth_and_signing"`)
117 assert.Empty(t, out.Stderr())
120func TestSshKeyGet_Completions(t *testing.T) {
121 t.Parallel()
123 testKey := &gitlab.SSHKey{ID: 123, Title: "mysshkey"}
125 testCases := []struct {
126 Name string
127 Args []string
128 setupMock func(tc *gitlabtesting.TestClient)
129 wantComps []cobra.Completion
130 wantDirective cobra.ShellCompDirective
131 }{
132 {
133 Name: "Completes key IDs as ID and title",
134 setupMock: func(tc *gitlabtesting.TestClient) {
135 tc.MockUsers.EXPECT().ListSSHKeys(gomock.Any(), gomock.Any()).
136 Return([]*gitlab.SSHKey{testKey}, &gitlab.Response{}, nil)
137 },
138 wantComps: []cobra.Completion{"123\tmysshkey"},
139 wantDirective: cobra.ShellCompDirectiveNoFileComp,
140 },
141 {
142 Name: "No completions when the second argument is completed",
143 Args: []string{"123", "456"},
144 setupMock: func(tc *gitlabtesting.TestClient) {},
145 wantDirective: cobra.ShellCompDirectiveNoFileComp,
146 },
147 {
148 Name: "API error returns no completions",
149 setupMock: func(tc *gitlabtesting.TestClient) {
150 tc.MockUsers.EXPECT().ListSSHKeys(gomock.Any(), gomock.Any()).
151 Return(nil, nil, errors.New("500 Internal Server Error"))
152 },
153 wantDirective: cobra.ShellCompDirectiveNoFileComp,
154 },
155 }
157 for _, tc := range testCases {
158 t.Run(tc.Name, func(t *testing.T) {
159 t.Parallel()
161 testClient := gitlabtesting.NewTestClient(t)
162 tc.setupMock(testClient)
163 f := cmdtest.NewTestFactory(nil,
164 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
165 )
167 cmd := NewCmdGet(f)
168 cmd.SetContext(t.Context())
169 completions, directive := cmd.ValidArgsFunction(cmd, tc.Args, "")
171 assert.Equal(t, tc.wantComps, completions)
172 assert.Equal(t, tc.wantDirective, directive)
173 })
174 }
Diagnostics8 git calls · 197 ms
CallStatusSizeTime0197 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=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=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
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:0200289 B · hit3 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=289
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=1dd4ebf804b44e69b594244d57246af8b61f1ed3
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=289
git.pkt.payload.max=262
git.sideband.band1.bytes=262
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=262
git.pack.trailer=d2a3ec46393f5a5492eae3096186e74c059a8aaf
Open pack map
fetchtree:0200134 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=182
git.response.bytes=134
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=bfada6d29eea3c4cda52d75e0786e05950aa9b79
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=134
git.pkt.payload.max=107
git.sideband.band1.bytes=107
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=107
git.pack.trailer=1ed43afa126e6a4c927eb4b89b2cd96be60f484c
Open pack map
fetch2001.5 KiB182 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1487
git.response.cache=MISS
git.response.duration_ms=182
git.fetch.wants=f0acd3915e66d4c7ed7c7e3b6eef2e23fd194e84
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=1487
git.pkt.payload.max=1460
git.sideband.band1.bytes=1460
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
a5e298c5da382407
Request
73b7476a…
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