GitLabgitlab-org
cli
main
1//go:build !integration
3package get
5import (
6 "encoding/json"
7 "errors"
8 "testing"
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/api"
19 "gitlab.com/gitlab-org/cli/internal/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23func setupExec(t *testing.T, tc *gitlabtesting.TestClient, opts ...cmdtest.FactoryOption) cmdtest.CmdExecFunc {
24 t.Helper()
26 opts = append(
27 []cmdtest.FactoryOption{
28 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
29 },
30 opts...,
31 )
33 return cmdtest.SetupCmdForTest(t, NewCmd, false, opts...)
34}
36func noMorePages() *gitlab.Response {
37 return &gitlab.Response{NextPage: 0}
38}
40func TestGet_Project(t *testing.T) {
41 t.Parallel()
43 tc := gitlabtesting.NewTestClient(t)
44 tc.MockProjects.EXPECT().
45 GetProjectServiceAccount("OWNER/REPO", int64(51), gomock.Any()).
46 Return(&gitlab.ProjectServiceAccount{
47 ID: 51,
48 Username: "service_account_project_2_aaa",
49 Name: "Deployer",
50 Email: "deployer@example.com",
51 }, nil, nil)
53 out, err := setupExec(t, tc)("51")
54 require.NoError(t, err)
56 expected := heredoc.Doc(`
57 ID 51
58 Username service_account_project_2_aaa
59 Name Deployer
60 Email deployer@example.com
61 `)
62 assert.Equal(t, expected, out.OutBuf.String())
63 assert.Empty(t, out.ErrBuf.String())
64}
66func TestGet_Group(t *testing.T) {
67 t.Parallel()
69 tc := gitlabtesting.NewTestClient(t)
70 tc.MockGroups.EXPECT().
71 GetServiceAccount("my-group", int64(41), gomock.Any()).
72 Return(&gitlab.GroupServiceAccount{
73 ID: 41,
74 UserName: "service_account_group_1_aaa",
75 }, nil, nil)
77 out, err := setupExec(t, tc)("41 --group my-group")
78 require.NoError(t, err)
80 expected := heredoc.Doc(`
81 ID 41
82 Username service_account_group_1_aaa
83 Name -
84 Email -
85 `)
86 assert.Equal(t, expected, out.OutBuf.String())
87}
89func TestGet_UnconfirmedEmail(t *testing.T) {
90 t.Parallel()
92 tc := gitlabtesting.NewTestClient(t)
93 tc.MockGroups.EXPECT().
94 GetServiceAccount("my-group", int64(41), gomock.Any()).
95 Return(&gitlab.GroupServiceAccount{
96 ID: 41,
97 UserName: "service_account_group_1_aaa",
98 Email: "service_account_group_1_aaa@noreply.gitlab.example.com",
99 UnconfirmedEmail: "deployer@example.com",
100 }, nil, nil)
102 out, err := setupExec(t, tc)("41 --group my-group")
103 require.NoError(t, err)
105 expected := heredoc.Doc(`
106 ID 41
107 Username service_account_group_1_aaa
108 Name -
109 Email service_account_group_1_aaa@noreply.gitlab.example.com
110 Unconfirmed Email deployer@example.com
111 `)
112 assert.Equal(t, expected, out.OutBuf.String())
113}
115// No GetProjectServiceAccount expectation is set: a username is answered from
116// the list, so the command also works before GitLab 19.4.
117func TestGet_ByUsername(t *testing.T) {
118 t.Parallel()
120 tc := gitlabtesting.NewTestClient(t)
121 tc.MockProjects.EXPECT().
122 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
123 Return([]*gitlab.ProjectServiceAccount{
124 {ID: 51, Username: "service_account_project_2_aaa", Name: "Deployer"},
125 {ID: 52, Username: "service_account_project_2_bbb", Name: "Releaser", Email: "releaser@example.com"},
126 }, noMorePages(), nil)
128 out, err := setupExec(t, tc)("service_account_project_2_bbb")
129 require.NoError(t, err)
131 expected := heredoc.Doc(`
132 ID 52
133 Username service_account_project_2_bbb
134 Name Releaser
135 Email releaser@example.com
136 `)
137 assert.Equal(t, expected, out.OutBuf.String())
138}
140func TestGet_ByUsernameJSON(t *testing.T) {
141 t.Parallel()
143 tc := gitlabtesting.NewTestClient(t)
144 tc.MockGroups.EXPECT().
145 ListServiceAccounts("my-group", gomock.Any(), gomock.Any(), gomock.Any()).
146 Return([]*gitlab.GroupServiceAccount{
147 {
148 ID: 41,
149 UserName: "service_account_group_1_aaa",
150 Name: "Releaser",
151 Email: "releaser@example.com",
152 PublicEmail: "public@example.com",
153 },
154 }, noMorePages(), nil)
156 out, err := setupExec(t, tc)("service_account_group_1_aaa --group my-group --output json")
157 require.NoError(t, err)
159 var result gitlab.GroupServiceAccount
160 require.NoError(t, json.Unmarshal(out.OutBuf.Bytes(), &result))
161 assert.Equal(t, int64(41), result.ID)
162 assert.Equal(t, "service_account_group_1_aaa", result.UserName)
163 assert.Equal(t, "Releaser", result.Name)
164 assert.Equal(t, "releaser@example.com", result.Email)
165 assert.Equal(t, "public@example.com", result.PublicEmail)
166}
168func TestGet_UsernameNotFound(t *testing.T) {
169 t.Parallel()
171 tc := gitlabtesting.NewTestClient(t)
172 tc.MockGroups.EXPECT().
173 ListServiceAccounts("my-group", gomock.Any(), gomock.Any(), gomock.Any()).
174 Return([]*gitlab.GroupServiceAccount{
175 {ID: 41, UserName: "service_account_group_1_aaa"},
176 }, noMorePages(), nil)
178 _, err := setupExec(t, tc)("missing --group my-group")
179 require.Error(t, err)
181 var exitErr *cmdutils.ExitError
182 require.ErrorAs(t, err, &exitErr)
183 assert.Equal(t, "failed to get service account", exitErr.Details)
184 assert.Contains(t, err.Error(), `no service account with username "missing" in group my-group`)
185}
187// The group API type carries public_email, which the text output leaves out, so
188// JSON output must print the response rather than the text fields.
189func TestGet_JSON(t *testing.T) {
190 t.Parallel()
192 tc := gitlabtesting.NewTestClient(t)
193 tc.MockGroups.EXPECT().
194 GetServiceAccount("my-group", int64(41), gomock.Any()).
195 Return(&gitlab.GroupServiceAccount{
196 ID: 41,
197 UserName: "service_account_group_1_aaa",
198 Name: "Releaser",
199 Email: "releaser@example.com",
200 PublicEmail: "public@example.com",
201 }, nil, nil)
203 out, err := setupExec(t, tc)("41 --group my-group --output json")
204 require.NoError(t, err)
206 var result gitlab.GroupServiceAccount
207 require.NoError(t, json.Unmarshal(out.OutBuf.Bytes(), &result))
208 assert.Equal(t, int64(41), result.ID)
209 assert.Equal(t, "service_account_group_1_aaa", result.UserName)
210 assert.Equal(t, "Releaser", result.Name)
211 assert.Equal(t, "releaser@example.com", result.Email)
212 assert.Equal(t, "public@example.com", result.PublicEmail)
213}
215func TestGet_APIError(t *testing.T) {
216 t.Parallel()
218 tc := gitlabtesting.NewTestClient(t)
219 tc.MockProjects.EXPECT().
220 GetProjectServiceAccount("OWNER/REPO", int64(51), gomock.Any()).
221 Return(nil, nil, errors.New("404 Not Found"))
223 _, err := setupExec(t, tc)("51")
224 require.Error(t, err)
226 var exitErr *cmdutils.ExitError
227 require.ErrorAs(t, err, &exitErr)
228 assert.Equal(t, "failed to get service account", exitErr.Details)
229 assert.Contains(t, err.Error(), "404 Not Found")
230}
232func TestGet_InvalidID(t *testing.T) {
233 t.Parallel()
235 tc := gitlabtesting.NewTestClient(t)
237 _, err := setupExec(t, tc)("0")
238 require.Error(t, err)
240 var exitErr *cmdutils.ExitError
241 require.ErrorAs(t, err, &exitErr)
242 assert.Equal(t, "failed to get service account", exitErr.Details)
243 assert.Contains(t, err.Error(), `invalid service account ID "0"`)
244}
246func TestGet_RequiresArgument(t *testing.T) {
247 t.Parallel()
249 tc := gitlabtesting.NewTestClient(t)
251 _, err := setupExec(t, tc)("")
252 require.Error(t, err)
253 assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0")
254}
256func TestGet_RepoAndGroupAreMutuallyExclusive(t *testing.T) {
257 t.Parallel()
259 tc := gitlabtesting.NewTestClient(t)
261 _, err := setupExec(t, tc)("41 --repo OWNER/REPO --group my-group")
262 require.Error(t, err)
263 assert.Contains(t, err.Error(), "none of the others can be")
264}
266func TestGet_OutsideRepositoryWithoutGroup(t *testing.T) {
267 t.Parallel()
269 tc := gitlabtesting.NewTestClient(t)
271 _, err := setupExec(t, tc, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("41")
272 require.Error(t, err)
273 assert.Contains(t, err.Error(), "not a git repository")
274}
276func TestGet_OutsideRepositoryWithGroup(t *testing.T) {
277 t.Parallel()
279 tc := gitlabtesting.NewTestClient(t)
280 tc.MockGroups.EXPECT().
281 GetServiceAccount("my-group", int64(41), gomock.Any()).
282 Return(&gitlab.GroupServiceAccount{ID: 41, UserName: "service_account_group_1_aaa"}, nil, nil)
284 out, err := setupExec(t, tc, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("41 --group my-group")
285 require.NoError(t, err)
287 assert.Contains(t, out.OutBuf.String(), "service_account_group_1_aaa")
288}
290// GITLAB_GROUP redirects the command with no flag passed. t.Setenv rules out
291// t.Parallel.
292func TestGet_GroupFromEnvironment(t *testing.T) {
293 t.Setenv("GITLAB_GROUP", "env-group")
295 tc := gitlabtesting.NewTestClient(t)
296 tc.MockGroups.EXPECT().
297 GetServiceAccount("env-group", int64(41), gomock.Any()).
298 Return(&gitlab.GroupServiceAccount{ID: 41, UserName: "service_account_group_1_aaa"}, nil, nil)
300 out, err := setupExec(t, tc)("41")
301 require.NoError(t, err)
303 assert.Contains(t, out.OutBuf.String(), "service_account_group_1_aaa")
304}
Diagnostics8 git calls · 279 ms
CallStatusSizeTime0279 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 KiB9 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=9 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 · 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=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:0 · deepen 1020032 KiB · hit4 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=4 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:0200322 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=322 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=ba2caccf6c55211c6b29b98f40c2358479d91b1f 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=322 git.pkt.payload.max=295 git.sideband.band1.bytes=295 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=295 git.pack.trailer=3488e558d4708096e879feba968e084c249dbe22Open pack map
fetchtree:0200135 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=135 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=7fe485110b597246350bc7da56b52aa930443045 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=135 git.pkt.payload.max=108 git.sideband.band1.bytes=108 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=108 git.pack.trailer=717f7347ad51127f9ca63f2909580355a72479bbOpen pack map
fetch2001.8 KiB259 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=259 git.response.bytes=1867 git.response.cache=MISS git.response.duration_ms=259 git.fetch.wants=ec3a3480ecde80beb8b19f81b0bcf2491b434fce 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=1867 git.pkt.payload.max=1840 git.sideband.band1.bytes=1840 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
- a5e30d438cd68a31
- Request
- 68edfae0…
- 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