GitLabgitlab-org

cli

main
ply8 git calls365 ms100 KiB6 cachedprotocol v2
299 lines9.3 KiBRaw
1//go:build !integration
3package update
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"
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...)
36func noMorePages() *gitlab.Response {
37 return &gitlab.Response{NextPage: 0}
40// expectProjectUpdate returns an accessor for the options the command sent, so
41// the tests can assert on the request and not only on what is printed.
42func expectProjectUpdate(tc *gitlabtesting.TestClient, id int64, account *gitlab.ProjectServiceAccount) func() *gitlab.UpdateProjectServiceAccountOptions {
43 var sent *gitlab.UpdateProjectServiceAccountOptions
44 tc.MockProjects.EXPECT().
45 UpdateProjectServiceAccount("OWNER/REPO", id, gomock.Any(), gomock.Any()).
46 DoAndReturn(func(_ any, _ int64, opt *gitlab.UpdateProjectServiceAccountOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.ProjectServiceAccount, *gitlab.Response, error) {
47 sent = opt
48 return account, nil, nil
49 })
51 return func() *gitlab.UpdateProjectServiceAccountOptions { return sent }
54// No list expectation is set: an ID goes straight to the update.
55func TestUpdate_ProjectByID(t *testing.T) {
56 t.Parallel()
58 tc := gitlabtesting.NewTestClient(t)
59 sent := expectProjectUpdate(tc, 51, &gitlab.ProjectServiceAccount{
60 ID: 51,
61 Username: "service_account_project_2_aaa",
62 Name: "Release bot",
63 Email: "deployer@example.com",
64 })
66 out, err := setupExec(t, tc)(`51 --name "Release bot"`)
67 require.NoError(t, err)
69 require.NotNil(t, sent().Name)
70 assert.Equal(t, "Release bot", *sent().Name)
71 assert.Nil(t, sent().Username)
72 assert.Nil(t, sent().Email)
74 expected := heredoc.Doc(`
75 ID 51
76 Username service_account_project_2_aaa
77 Name Release bot
78 Email deployer@example.com
79 `)
80 assert.Equal(t, expected, out.OutBuf.String())
81 assert.Equal(t, "✓ Updated service account 51 in project OWNER/REPO.\n", out.ErrBuf.String())
84func TestUpdate_ByUsername(t *testing.T) {
85 t.Parallel()
87 tc := gitlabtesting.NewTestClient(t)
88 gomock.InOrder(
89 tc.MockProjects.EXPECT().
90 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
91 Return([]*gitlab.ProjectServiceAccount{
92 {ID: 51, Username: "service_account_project_2_aaa"},
93 {ID: 52, Username: "release-bot"},
94 }, noMorePages(), nil),
95 tc.MockProjects.EXPECT().
96 UpdateProjectServiceAccount("OWNER/REPO", int64(52), gomock.Any(), gomock.Any()).
97 Return(&gitlab.ProjectServiceAccount{ID: 52, Username: "release-bot-2"}, nil, nil),
98 )
100 out, err := setupExec(t, tc)("release-bot --username release-bot-2")
101 require.NoError(t, err)
103 assert.Contains(t, out.OutBuf.String(), "release-bot-2")
106func TestUpdate_AllFields(t *testing.T) {
107 t.Parallel()
109 tc := gitlabtesting.NewTestClient(t)
110 sent := expectProjectUpdate(tc, 51, &gitlab.ProjectServiceAccount{
111 ID: 51,
112 Username: "release-bot",
113 Name: "Release bot",
114 Email: "service_account_project_2_aaa@noreply.gitlab.example.com",
115 UnconfirmedEmail: "release-bot@example.com",
116 })
118 out, err := setupExec(t, tc)(`51 --name "Release bot" --username release-bot --email release-bot@example.com`)
119 require.NoError(t, err)
121 assert.Equal(t, "Release bot", *sent().Name)
122 assert.Equal(t, "release-bot", *sent().Username)
123 assert.Equal(t, "release-bot@example.com", *sent().Email)
125 expected := heredoc.Doc(`
126 ID 51
127 Username release-bot
128 Name Release bot
129 Email service_account_project_2_aaa@noreply.gitlab.example.com
130 Unconfirmed Email release-bot@example.com
131 `)
132 assert.Equal(t, expected, out.OutBuf.String())
135func TestUpdate_Group(t *testing.T) {
136 t.Parallel()
138 var sent *gitlab.UpdateServiceAccountOptions
139 tc := gitlabtesting.NewTestClient(t)
140 tc.MockGroups.EXPECT().
141 UpdateServiceAccount("group/sub-group", int64(41), gomock.Any(), gomock.Any()).
142 DoAndReturn(func(_ any, _ int64, opt *gitlab.UpdateServiceAccountOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.GroupServiceAccount, *gitlab.Response, error) {
143 sent = opt
144 return &gitlab.GroupServiceAccount{ID: 41, UserName: "service_account_group_1_aaa", Email: "deployer@example.com"}, nil, nil
145 })
147 out, err := setupExec(t, tc)("41 --email deployer@example.com --group group/sub-group")
148 require.NoError(t, err)
150 assert.Nil(t, sent.Name)
151 assert.Nil(t, sent.Username)
152 assert.Equal(t, "deployer@example.com", *sent.Email)
153 assert.Nil(t, sent.External)
154 assert.Equal(t, "✓ Updated service account 41 in group group/sub-group.\n", out.ErrBuf.String())
157func TestUpdate_JSON(t *testing.T) {
158 t.Parallel()
160 tc := gitlabtesting.NewTestClient(t)
161 expectProjectUpdate(tc, 51, &gitlab.ProjectServiceAccount{ID: 51, Username: "release-bot", Name: "Release bot"})
163 out, err := setupExec(t, tc)(`51 --name "Release bot" --output json`)
164 require.NoError(t, err)
166 var result gitlab.ProjectServiceAccount
167 require.NoError(t, json.Unmarshal(out.OutBuf.Bytes(), &result))
168 assert.Equal(t, int64(51), result.ID)
169 assert.Equal(t, "Release bot", result.Name)
170 assert.Empty(t, out.ErrBuf.String())
173func TestUpdate_RequiresAField(t *testing.T) {
174 t.Parallel()
176 tc := gitlabtesting.NewTestClient(t)
178 _, err := setupExec(t, tc)("51")
179 require.Error(t, err)
180 assert.Contains(t, err.Error(), "at least one of the flags in the group [name username email] is required")
183// No expectation is set, so the values are rejected before any API call.
184func TestUpdate_RejectsEmptyValues(t *testing.T) {
185 t.Parallel()
187 for _, tt := range []struct {
188 args string
189 want string
190 }{
191 {args: `51 --name ""`, want: "--name can't be empty"},
192 {args: `51 --username ""`, want: "--username can't be empty"},
193 {args: `51 --email " "`, want: "--email can't be empty"},
194 {args: `51 --name "Release bot" --email ""`, want: "--email can't be empty"},
195 } {
196 t.Run(tt.args, func(t *testing.T) {
197 t.Parallel()
199 tc := gitlabtesting.NewTestClient(t)
201 _, err := setupExec(t, tc)(tt.args)
202 require.Error(t, err)
204 var flagErr *cmdutils.FlagError
205 require.ErrorAs(t, err, &flagErr)
206 assert.Equal(t, tt.want, err.Error())
207 })
208 }
211func TestUpdate_UsernameNotFound(t *testing.T) {
212 t.Parallel()
214 tc := gitlabtesting.NewTestClient(t)
215 tc.MockProjects.EXPECT().
216 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
217 Return([]*gitlab.ProjectServiceAccount{}, noMorePages(), nil)
219 _, err := setupExec(t, tc)(`missing --name "Release bot"`)
220 require.Error(t, err)
222 var exitErr *cmdutils.ExitError
223 require.ErrorAs(t, err, &exitErr)
224 assert.Equal(t, "failed to update service account", exitErr.Details)
225 assert.Contains(t, err.Error(), `no service account with username "missing" in project OWNER/REPO`)
228func TestUpdate_InvalidID(t *testing.T) {
229 t.Parallel()
231 tc := gitlabtesting.NewTestClient(t)
233 _, err := setupExec(t, tc)(`0 --name "Release bot"`)
234 require.Error(t, err)
235 assert.Contains(t, err.Error(), `invalid service account ID "0"`)
238func TestUpdate_APIError(t *testing.T) {
239 t.Parallel()
241 tc := gitlabtesting.NewTestClient(t)
242 tc.MockProjects.EXPECT().
243 UpdateProjectServiceAccount("OWNER/REPO", int64(51), gomock.Any(), gomock.Any()).
244 Return(nil, nil, errors.New("400 Bad Request"))
246 _, err := setupExec(t, tc)("51 --email taken@example.com")
247 require.Error(t, err)
249 var exitErr *cmdutils.ExitError
250 require.ErrorAs(t, err, &exitErr)
251 assert.Equal(t, "failed to update service account", exitErr.Details)
252 assert.Contains(t, err.Error(), "400 Bad Request")
255func TestUpdate_RequiresArgument(t *testing.T) {
256 t.Parallel()
258 tc := gitlabtesting.NewTestClient(t)
260 _, err := setupExec(t, tc)(`--name "Release bot"`)
261 require.Error(t, err)
262 assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0")
265func TestUpdate_RepoAndGroupAreMutuallyExclusive(t *testing.T) {
266 t.Parallel()
268 tc := gitlabtesting.NewTestClient(t)
270 _, err := setupExec(t, tc)(`41 --name "Release bot" --repo OWNER/REPO --group my-group`)
271 require.Error(t, err)
272 assert.Contains(t, err.Error(), "none of the others can be")
275func TestUpdate_OutsideRepositoryWithoutGroup(t *testing.T) {
276 t.Parallel()
278 tc := gitlabtesting.NewTestClient(t)
280 _, err := setupExec(t, tc, cmdtest.WithBaseRepoError(errors.New("not a git repository")))(`41 --name "Release bot"`)
281 require.Error(t, err)
282 assert.Contains(t, err.Error(), "not a git repository")
285// GITLAB_GROUP redirects the command with no flag passed. t.Setenv rules out
286// t.Parallel.
287func TestUpdate_GroupFromEnvironment(t *testing.T) {
288 t.Setenv("GITLAB_GROUP", "env-group")
290 tc := gitlabtesting.NewTestClient(t)
291 tc.MockGroups.EXPECT().
292 UpdateServiceAccount("env-group", int64(41), gomock.Any(), gomock.Any()).
293 Return(&gitlab.GroupServiceAccount{ID: 41, UserName: "service_account_group_1_aaa"}, nil, nil)
295 out, err := setupExec(t, tc)(`41 --name "Release bot"`)
296 require.NoError(t, err)
298 assert.Equal(t, "✓ Updated service account 41 in group env-group.\n", out.ErrBuf.String())
Diagnostics8 git calls · 365 ms
CallStatusSizeTime0365 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 · hit3 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=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
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: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=3488e558d4708096e879feba968e084c249dbe22
Open pack map
fetchtree:0200138 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=138
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=35dba1ca18eda59e3093f7e3f8c83d3541f0fecc
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=138
git.pkt.payload.max=111
git.sideband.band1.bytes=111
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=111
git.pack.trailer=189ef6465c8f61b2f35e0fa326a1197ba29398ff
Open pack map
fetch2002.3 KiB350 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=350
git.response.bytes=2346
git.response.cache=MISS
git.response.duration_ms=350
git.fetch.wants=380f688710f76667068cebe4644c7f96dd2f9e43
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=2346
git.pkt.payload.max=2319
git.sideband.band1.bytes=2319
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
a5e2fef1abe9f3c9
Request
6c13d114…
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