GitLabgitlab-org
cli
main
1//go:build !integration
3package list
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 TestList_Project(t *testing.T) {
41 t.Parallel()
43 tc := gitlabtesting.NewTestClient(t)
44 tc.MockProjects.EXPECT().
45 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
46 Return([]*gitlab.ProjectServiceAccount{
47 {ID: 51, Username: "service_account_project_2_aaa", Name: "Deployer", Email: "deployer@example.com"},
48 {ID: 52, Username: "service_account_project_2_bbb"},
49 }, noMorePages(), nil)
51 out, err := setupExec(t, tc)("")
52 require.NoError(t, err)
54 expected := heredoc.Doc(`
55 ID Username Name Email
56 51 service_account_project_2_aaa Deployer deployer@example.com
57 52 service_account_project_2_bbb - -
58 `)
59 assert.Equal(t, expected, out.OutBuf.String())
60 assert.Empty(t, out.ErrBuf.String())
61}
63func TestList_Group(t *testing.T) {
64 t.Parallel()
66 tc := gitlabtesting.NewTestClient(t)
67 tc.MockGroups.EXPECT().
68 ListServiceAccounts("my-group", gomock.Any(), gomock.Any(), gomock.Any()).
69 Return([]*gitlab.GroupServiceAccount{
70 {ID: 41, UserName: "service_account_group_1_aaa", Name: "Releaser", Email: "releaser@example.com"},
71 }, noMorePages(), nil)
73 out, err := setupExec(t, tc)("--group my-group")
74 require.NoError(t, err)
76 expected := heredoc.Doc(`
77 ID Username Name Email
78 41 service_account_group_1_aaa Releaser releaser@example.com
79 `)
80 assert.Equal(t, expected, out.OutBuf.String())
81 assert.Empty(t, out.ErrBuf.String())
82}
84func TestList_Requests100PerPage(t *testing.T) {
85 t.Parallel()
87 tc := gitlabtesting.NewTestClient(t)
88 tc.MockProjects.EXPECT().
89 ListProjectServiceAccounts("OWNER/REPO", gomock.Cond(func(o *gitlab.ListProjectServiceAccountsOptions) bool { return o.PerPage == 100 }), gomock.Any(), gomock.Any()).
90 Return([]*gitlab.ProjectServiceAccount{}, noMorePages(), nil)
92 _, err := setupExec(t, tc)("")
93 require.NoError(t, err)
94}
96func TestList_ProjectJSON(t *testing.T) {
97 t.Parallel()
99 tc := gitlabtesting.NewTestClient(t)
100 tc.MockProjects.EXPECT().
101 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
102 Return([]*gitlab.ProjectServiceAccount{
103 {ID: 51, Username: "service_account_project_2_aaa", Name: "Deployer", Email: "deployer@example.com"},
104 }, noMorePages(), nil)
106 out, err := setupExec(t, tc)("--output json")
107 require.NoError(t, err)
109 var result []*gitlab.ProjectServiceAccount
110 require.NoError(t, json.Unmarshal(out.OutBuf.Bytes(), &result))
111 require.Len(t, result, 1)
112 assert.Equal(t, int64(51), result[0].ID)
113 assert.Equal(t, "service_account_project_2_aaa", result[0].Username)
114 assert.Equal(t, "Deployer", result[0].Name)
115 assert.Equal(t, "deployer@example.com", result[0].Email)
116}
118// The group API type carries public_email, which the text table leaves out, so
119// JSON output must print the response rather than the table's columns.
120func TestList_GroupJSON(t *testing.T) {
121 t.Parallel()
123 tc := gitlabtesting.NewTestClient(t)
124 tc.MockGroups.EXPECT().
125 ListServiceAccounts("my-group", gomock.Any(), gomock.Any(), gomock.Any()).
126 Return([]*gitlab.GroupServiceAccount{
127 {ID: 41, UserName: "service_account_group_1_aaa", Name: "Releaser", Email: "releaser@example.com", PublicEmail: "public@example.com"},
128 }, noMorePages(), nil)
130 out, err := setupExec(t, tc)("--group my-group --output json")
131 require.NoError(t, err)
133 var result []*gitlab.GroupServiceAccount
134 require.NoError(t, json.Unmarshal(out.OutBuf.Bytes(), &result))
135 require.Len(t, result, 1)
136 assert.Equal(t, int64(41), result[0].ID)
137 assert.Equal(t, "service_account_group_1_aaa", result[0].UserName)
138 assert.Equal(t, "Releaser", result[0].Name)
139 assert.Equal(t, "releaser@example.com", result[0].Email)
140 assert.Equal(t, "public@example.com", result[0].PublicEmail)
141}
143func TestList_Empty(t *testing.T) {
144 t.Parallel()
146 tc := gitlabtesting.NewTestClient(t)
147 tc.MockProjects.EXPECT().
148 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
149 Return([]*gitlab.ProjectServiceAccount{}, noMorePages(), nil)
151 out, err := setupExec(t, tc)("")
152 require.NoError(t, err)
154 assert.Equal(t, "No service accounts found.\n", out.OutBuf.String())
155}
157func TestList_EmptyJSON(t *testing.T) {
158 t.Parallel()
160 tc := gitlabtesting.NewTestClient(t)
161 tc.MockProjects.EXPECT().
162 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
163 Return(nil, noMorePages(), nil)
165 out, err := setupExec(t, tc)("--output json")
166 require.NoError(t, err)
168 assert.Equal(t, "[]\n", out.OutBuf.String())
169}
171func TestList_APIError(t *testing.T) {
172 t.Parallel()
174 tc := gitlabtesting.NewTestClient(t)
175 tc.MockProjects.EXPECT().
176 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
177 Return(nil, nil, errors.New("403 Forbidden"))
179 _, err := setupExec(t, tc)("")
180 require.Error(t, err)
182 var exitErr *cmdutils.ExitError
183 require.ErrorAs(t, err, &exitErr)
184 assert.Equal(t, "failed to list service accounts", exitErr.Details)
185 assert.Contains(t, err.Error(), "403 Forbidden")
186}
188func TestList_RepoAndGroupAreMutuallyExclusive(t *testing.T) {
189 t.Parallel()
191 tc := gitlabtesting.NewTestClient(t)
193 _, err := setupExec(t, tc)("--repo OWNER/REPO --group my-group")
194 require.Error(t, err)
195 assert.Contains(t, err.Error(), "none of the others can be")
196}
198func TestList_OutsideRepositoryWithoutGroup(t *testing.T) {
199 t.Parallel()
201 tc := gitlabtesting.NewTestClient(t)
203 _, err := setupExec(t, tc, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("")
204 require.Error(t, err)
205 assert.Contains(t, err.Error(), "not a git repository")
206}
208func TestList_OutsideRepositoryWithGroup(t *testing.T) {
209 t.Parallel()
211 tc := gitlabtesting.NewTestClient(t)
212 tc.MockGroups.EXPECT().
213 ListServiceAccounts("my-group", gomock.Any(), gomock.Any(), gomock.Any()).
214 Return([]*gitlab.GroupServiceAccount{
215 {ID: 41, UserName: "service_account_group_1_aaa", Name: "Releaser"},
216 }, noMorePages(), nil)
218 out, err := setupExec(t, tc, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("--group my-group")
219 require.NoError(t, err)
221 assert.Contains(t, out.OutBuf.String(), "service_account_group_1_aaa")
222}
224// GITLAB_GROUP redirects the command with no flag passed. t.Setenv rules out
225// t.Parallel.
226func TestList_GroupFromEnvironment(t *testing.T) {
227 t.Setenv("GITLAB_GROUP", "env-group")
229 tc := gitlabtesting.NewTestClient(t)
230 tc.MockGroups.EXPECT().
231 ListServiceAccounts("env-group", gomock.Any(), gomock.Any(), gomock.Any()).
232 Return([]*gitlab.GroupServiceAccount{
233 {ID: 41, UserName: "service_account_group_1_aaa", Name: "Releaser"},
234 }, noMorePages(), nil)
236 out, err := setupExec(t, tc)("")
237 require.NoError(t, err)
239 assert.Contains(t, out.OutBuf.String(), "service_account_group_1_aaa")
240}
Diagnostics8 git calls · 229 ms
CallStatusSizeTime0229 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 KiB · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=4 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=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: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=44Open 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=1 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200322 B · hit11 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=11 git.response.bytes=322 git.response.cache=HIT git.response.duration_ms=11 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:0200134 B · 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=134 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=3fcd480e5469f6448a4017371b357b24d9427f9c 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=51a517112551adccec6c7e76beb43a08583ce98aOpen pack map
fetch2001.6 KiB208 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=208 git.response.bytes=1637 git.response.cache=MISS git.response.duration_ms=208 git.fetch.wants=65ae44e1a382b2784e90a65d5e2dba0b89011abc 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=1637 git.pkt.payload.max=1610 git.sideband.band1.bytes=1610 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
- a5e31b28faff3e6e
- Request
- 6017616f…
- 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