GitLabgitlab-org

cli

main
ply8 git calls332 ms99 KiB5 cachedprotocol v2
263 lines8.5 KiBRaw
1//go:build !integration
3package delete
5import (
6 "errors"
7 "testing"
9 "git.sr.ht/~timofurrer/ugh"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/api"
18 "gitlab.com/gitlab-org/cli/internal/cmdutils"
19 "gitlab.com/gitlab-org/cli/internal/iostreams"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
23func setupExec(t *testing.T, tc *gitlabtesting.TestClient, isTTY bool, 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, isTTY, opts...)
36func noMorePages() *gitlab.Response {
37 return &gitlab.Response{NextPage: 0}
40func TestDelete_Project(t *testing.T) {
41 t.Parallel()
43 tc := gitlabtesting.NewTestClient(t)
44 tc.MockProjects.EXPECT().
45 DeleteProjectServiceAccount("OWNER/REPO", int64(51), gomock.Cond(func(o *gitlab.DeleteProjectServiceAccountOptions) bool {
46 return o.HardDelete == nil
47 }), gomock.Any()).
48 Return(nil, nil)
50 out, err := setupExec(t, tc, false)("51 --yes")
51 require.NoError(t, err)
53 assert.Equal(t, "✓ Scheduled service account 51 of project OWNER/REPO for deletion.\n", out.OutBuf.String())
54 assert.Empty(t, out.ErrBuf.String())
57func TestDelete_Group(t *testing.T) {
58 t.Parallel()
60 tc := gitlabtesting.NewTestClient(t)
61 tc.MockGroups.EXPECT().
62 DeleteServiceAccount("group/sub-group", int64(41), gomock.Cond(func(o *gitlab.DeleteServiceAccountOptions) bool {
63 return o.HardDelete == nil
64 }), gomock.Any()).
65 Return(nil, nil)
67 out, err := setupExec(t, tc, false)("41 --yes --group group/sub-group")
68 require.NoError(t, err)
70 assert.Equal(t, "✓ Scheduled service account 41 of group group/sub-group for deletion.\n", out.OutBuf.String())
73func TestDelete_ByUsername(t *testing.T) {
74 t.Parallel()
76 tc := gitlabtesting.NewTestClient(t)
77 gomock.InOrder(
78 tc.MockProjects.EXPECT().
79 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
80 Return([]*gitlab.ProjectServiceAccount{
81 {ID: 51, Username: "service_account_project_2_aaa"},
82 {ID: 52, Username: "release-bot"},
83 }, noMorePages(), nil),
84 tc.MockProjects.EXPECT().
85 DeleteProjectServiceAccount("OWNER/REPO", int64(52), gomock.Any(), gomock.Any()).
86 Return(nil, nil),
87 )
89 out, err := setupExec(t, tc, false)("release-bot --yes")
90 require.NoError(t, err)
92 assert.Equal(t, "✓ Scheduled service account 52 of project OWNER/REPO for deletion.\n", out.OutBuf.String())
95func TestDelete_RequiresYesWhenNotInteractive(t *testing.T) {
96 t.Parallel()
98 tc := gitlabtesting.NewTestClient(t)
100 _, err := setupExec(t, tc, false)("51")
101 require.Error(t, err)
102 assert.Equal(t, "--yes required when not running interactively", err.Error())
105func TestDelete_UsernameNotFound(t *testing.T) {
106 t.Parallel()
108 tc := gitlabtesting.NewTestClient(t)
109 tc.MockProjects.EXPECT().
110 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
111 Return([]*gitlab.ProjectServiceAccount{}, noMorePages(), nil)
113 _, err := setupExec(t, tc, false)("missing --yes")
114 require.Error(t, err)
116 var exitErr *cmdutils.ExitError
117 require.ErrorAs(t, err, &exitErr)
118 assert.Equal(t, "failed to delete service account", exitErr.Details)
119 assert.Contains(t, err.Error(), `no service account with username "missing" in project OWNER/REPO`)
122func TestDelete_InvalidID(t *testing.T) {
123 t.Parallel()
125 tc := gitlabtesting.NewTestClient(t)
127 _, err := setupExec(t, tc, false)("0 --yes")
128 require.Error(t, err)
129 assert.Contains(t, err.Error(), `invalid service account ID "0"`)
132func TestDelete_APIError(t *testing.T) {
133 t.Parallel()
135 tc := gitlabtesting.NewTestClient(t)
136 tc.MockProjects.EXPECT().
137 DeleteProjectServiceAccount("OWNER/REPO", int64(51), gomock.Any(), gomock.Any()).
138 Return(nil, errors.New("404 Not Found"))
140 _, err := setupExec(t, tc, false)("51 --yes")
141 require.Error(t, err)
143 var exitErr *cmdutils.ExitError
144 require.ErrorAs(t, err, &exitErr)
145 assert.Equal(t, "failed to delete service account", exitErr.Details)
146 assert.Contains(t, err.Error(), "404 Not Found")
149func TestDelete_RequiresArgument(t *testing.T) {
150 t.Parallel()
152 tc := gitlabtesting.NewTestClient(t)
154 _, err := setupExec(t, tc, false)("--yes")
155 require.Error(t, err)
156 assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0")
159func TestDelete_RepoAndGroupAreMutuallyExclusive(t *testing.T) {
160 t.Parallel()
162 tc := gitlabtesting.NewTestClient(t)
164 _, err := setupExec(t, tc, false)("51 --yes --repo OWNER/REPO --group my-group")
165 require.Error(t, err)
166 assert.Contains(t, err.Error(), "none of the others can be")
169func TestDelete_OutsideRepositoryWithoutGroup(t *testing.T) {
170 t.Parallel()
172 tc := gitlabtesting.NewTestClient(t)
174 _, err := setupExec(t, tc, false, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("51 --yes")
175 require.Error(t, err)
176 assert.Contains(t, err.Error(), "not a git repository")
179func TestDelete_OutsideRepositoryWithGroup(t *testing.T) {
180 t.Parallel()
182 tc := gitlabtesting.NewTestClient(t)
183 tc.MockGroups.EXPECT().
184 DeleteServiceAccount("my-group", int64(41), gomock.Any(), gomock.Any()).
185 Return(nil, nil)
187 out, err := setupExec(t, tc, false, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("41 --yes --group my-group")
188 require.NoError(t, err)
190 assert.Contains(t, out.OutBuf.String(), "Scheduled service account 41 of group my-group for deletion.")
193func TestDelete_Prompt(t *testing.T) {
194 // NOTE: This test cannot run in parallel because the huh form library
195 // uses global state (charmbracelet/bubbles runeutil sanitizer).
197 t.Run("confirming deletes", func(t *testing.T) {
198 tc := gitlabtesting.NewTestClient(t)
199 tc.MockProjects.EXPECT().
200 DeleteProjectServiceAccount("OWNER/REPO", int64(51), gomock.Any(), gomock.Any()).
201 Return(nil, nil)
203 c := ugh.New(t)
204 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete service account 51?")).Do(ugh.Affirm)
206 out, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("51")
207 require.NoError(t, err)
209 assert.Contains(t, out.ErrBuf.String(), "This deletes service account 51 of project OWNER/REPO.")
210 assert.Contains(t, out.OutBuf.String(), "Scheduled service account 51 of project OWNER/REPO for deletion.")
211 })
213 t.Run("declining deletes nothing", func(t *testing.T) {
214 tc := gitlabtesting.NewTestClient(t)
215 tc.MockProjects.EXPECT().
216 DeleteProjectServiceAccount(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
217 Times(0)
219 c := ugh.New(t)
220 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete service account 51?")).Do(ugh.Reject)
222 _, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("51")
223 require.ErrorIs(t, err, iostreams.ErrUserCancelled)
224 })
226 // A username is resolved before the prompt, so the prompt names the ID that
227 // is about to be deleted.
228 t.Run("the prompt names the ID a username resolved to", func(t *testing.T) {
229 tc := gitlabtesting.NewTestClient(t)
230 tc.MockProjects.EXPECT().
231 ListProjectServiceAccounts("OWNER/REPO", gomock.Any(), gomock.Any(), gomock.Any()).
232 Return([]*gitlab.ProjectServiceAccount{{ID: 52, Username: "release-bot"}}, noMorePages(), nil)
233 tc.MockProjects.EXPECT().
234 DeleteProjectServiceAccount("OWNER/REPO", int64(52), gomock.Any(), gomock.Any()).
235 Return(nil, nil)
237 c := ugh.New(t)
238 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete service account 52?")).Do(ugh.Affirm)
240 _, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("release-bot")
241 require.NoError(t, err)
242 })
244 // GITLAB_GROUP redirects the command with no flag passed, so the advisory is
245 // the only place the caller sees which group loses the account.
246 t.Run("the advisory names a group from the environment", func(t *testing.T) {
247 t.Setenv("GITLAB_GROUP", "env-group")
249 tc := gitlabtesting.NewTestClient(t)
250 tc.MockGroups.EXPECT().
251 DeleteServiceAccount("env-group", int64(41), gomock.Any(), gomock.Any()).
252 Return(nil, nil)
254 c := ugh.New(t)
255 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete service account 41?")).Do(ugh.Affirm)
257 out, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("41")
258 require.NoError(t, err)
260 assert.Contains(t, out.ErrBuf.String(), "This deletes service account 41 of group env-group.")
261 assert.Contains(t, out.OutBuf.String(), "Scheduled service account 41 of group env-group for deletion.")
262 })
Diagnostics8 git calls · 332 ms
CallStatusSizeTime0332 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 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=2
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
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 · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=5
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 · hit4 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=1857
git.response.cache=HIT
git.response.duration_ms=4
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 · 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=322
git.response.cache=HIT
git.response.duration_ms=2
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=2a83c32378dd81a74f1a4c91ddfdaeb92b60b4d2
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=80dbef3c92c8a6ae4f8cf148bfcf64dd19aed5b5
Open pack map
fetch2001.9 KiB315 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=315
git.response.bytes=1983
git.response.cache=MISS
git.response.duration_ms=315
git.fetch.wants=0e2a586f3fa039d48a5dfae4bdde73d958692529
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=1983
git.pkt.payload.max=1956
git.sideband.band1.bytes=1956
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
a5e30030b816f3c9
Request
fd1068ef…
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