GitLabgitlab-org
cli
main
1//go:build !integration
3package graph
5import (
6 "errors"
7 "log/slog"
8 "testing"
9 "time"
11 "github.com/d2lang/d2/d2format"
12 "github.com/d2lang/d2/lib/log"
13 "github.com/d2lang/d2/lib/textmeasure"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16 "go.uber.org/mock/gomock"
17 xoauth2 "golang.org/x/oauth2"
19 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
20 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
22 "gitlab.com/gitlab-org/cli/internal/api"
23 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
24)
26// KAS accepts only the "pat:" authorization scheme, so anything that is not a
27// personal or project access token has to be refused before the server starts.
28func TestGraph_refusesNonAccessTokenCredentials(t *testing.T) {
29 tests := []struct {
30 name string
31 authSource gitlab.AuthSource
32 expectedErr string
33 }{
34 {
35 name: "job token",
36 authSource: gitlab.JobTokenAuthSource{Token: "a-job-token"},
37 expectedErr: "supports authentication with only personal and project access tokens",
38 },
39 {
40 // The constraint this command exists to enforce: KAS would reject an
41 // OAuth token sent under the "pat:" scheme, so refuse it here instead.
42 // Credential maps both OAuth2 auth sources onto the same kind, which
43 // TestClient_Credential pins, so one shape covers the branch.
44 name: "OAuth2 session",
45 authSource: gitlab.OAuthTokenSource{TokenSource: &countingTokenSource{token: &xoauth2.Token{
46 AccessToken: "an-access-token",
47 Expiry: time.Now().Add(time.Hour),
48 }}},
49 expectedErr: "supports authentication with only personal and project access tokens",
50 },
51 {
52 // Holds no token at all, so it cannot be classified. The message names
53 // the requirement rather than a Go type.
54 name: "password credentials",
55 authSource: &gitlab.PasswordCredentialsAuthSource{Password: "a-password"},
56 expectedErr: "supports authentication with only personal and project access tokens",
57 },
58 {
59 // A host with no credentials gets the more actionable message instead.
60 name: "unauthenticated",
61 authSource: gitlab.Unauthenticated{},
62 expectedErr: "glab is not authenticated",
63 },
64 }
66 for _, tt := range tests {
67 t.Run(tt.name, func(t *testing.T) {
68 exec := cmdtest.SetupCmdForTest(
69 t,
70 NewCmdGraph,
71 false,
72 cmdtest.WithApiClient(cmdtest.NewTestAuthSourceApiClient(t, nil, tt.authSource, "gitlab.example.com")),
73 cmdtest.WithBaseRepo("OWNER", "REPO", "gitlab.example.com"),
74 )
76 _, err := exec("--agent 123")
77 require.Error(t, err)
78 assert.ErrorContains(t, err, tt.expectedErr)
79 })
80 }
81}
83// countingTokenSource records whether the command tried to read the token.
84type countingTokenSource struct {
85 token *xoauth2.Token
86 calls int
87}
89func (s *countingTokenSource) Token() (*xoauth2.Token, error) {
90 s.calls++
91 return s.token, nil
92}
94// An OAuth2 token is refused on kind alone, so the command must not renew a
95// token it is about to reject.
96func TestGraph_refusesOAuth2WithoutReadingTheToken(t *testing.T) {
97 ts := &countingTokenSource{token: &xoauth2.Token{
98 AccessToken: "an-access-token",
99 Expiry: time.Now().Add(time.Hour),
100 }}
102 exec := cmdtest.SetupCmdForTest(
103 t,
104 NewCmdGraph,
105 false,
106 cmdtest.WithApiClient(cmdtest.NewTestAuthSourceApiClient(
107 t, nil, gitlab.OAuthTokenSource{TokenSource: ts}, "gitlab.example.com",
108 )),
109 cmdtest.WithBaseRepo("OWNER", "REPO", "gitlab.example.com"),
110 )
112 _, err := exec("--agent 123")
114 require.ErrorIs(t, err, errUnsupportedToken)
115 assert.Zero(t, ts.calls, "the token was read for a credential the command refuses")
116}
118// The refusal cases above all fail before the command does any work, so cover
119// the other side: an access token gets past the check and the command proceeds
120// to talk to the API.
121func TestGraph_acceptsAccessToken(t *testing.T) {
122 ctrl := gomock.NewController(t)
123 tc := gitlabtesting.NewTestClientWithCtrl(ctrl, gitlab.WithBaseURL("https://gitlab.example.com"))
124 tc.MockMetadata.EXPECT().GetMetadata(gomock.Any()).Return(nil, nil, errors.New("metadata unavailable"))
126 exec := cmdtest.SetupCmdForTest(
127 t,
128 NewCmdGraph,
129 false,
130 cmdtest.WithApiClient(cmdtest.NewTestAuthSourceApiClient(
131 t, nil, gitlab.AccessTokenAuthSource{Token: "a-pat"}, "gitlab.example.com",
132 api.WithGitLabClient(tc.Client),
133 )),
134 cmdtest.WithBaseRepo("OWNER", "REPO", "gitlab.example.com"),
135 )
137 _, err := exec("--agent 123")
139 // Reaching the metadata call is the assertion: the credential was accepted.
140 require.Error(t, err)
141 require.NotErrorIs(t, err, errUnsupportedToken)
142 assert.ErrorContains(t, err, "GitLab metadata")
143}
145func TestGraph_rendersBuiltGraphToSVG(t *testing.T) {
146 ctx := log.With(t.Context(), slog.New(slog.DiscardHandler))
147 ios, _, _, _ := cmdtest.TestIOStreams()
148 b, err := newGraphBuilder(ctx, ios)
149 require.NoError(t, err)
151 deploy := vertex{Group: "apps", Version: "v1", Resource: "deployments", Namespace: "default", Name: "web"}
152 rs := vertex{Group: "apps", Version: "v1", Resource: "replicasets", Namespace: "default", Name: "web-1"}
153 err = b.applyActions([]jsonWatchGraphAction{
154 {SetVertex: &jsonSetVertex{Vertex: deploy}},
155 {SetVertex: &jsonSetVertex{Vertex: rs}},
156 {SetArc: &jsonSetArc{Arc: Arc{Source: rs, Destination: deploy, Type: ownerReferenceArcType}}},
157 })
158 require.NoError(t, err)
160 ruler, err := textmeasure.NewRuler()
161 require.NoError(t, err)
162 svg, err := renderSVG(ctx, ruler, d2format.Format(b.g.AST))
163 require.NoError(t, err)
164 assert.Contains(t, string(svg), "<svg")
165 assert.Contains(t, string(svg), "web-1")
166}
Diagnostics8 git calls · 254 ms
CallStatusSizeTime0254 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 KiB2 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=2 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=4 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=4 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=2 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200200 B · hit2 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=200 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4 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=200 git.pkt.payload.max=173 git.sideband.band1.bytes=173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=173 git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9Open 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=239 git.response.bytes=289 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=86a13f848f75f5d885c75aa02d7eff6ecdc7349d 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=7f5e9a5963305286f535c0b6b2cedf1990871d65Open pack map
fetch2002.0 KiB239 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2056 git.response.cache=MISS git.response.duration_ms=239 git.fetch.wants=7b8219ad32e646549042afb15a07122c3e90b147 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=2056 git.pkt.payload.max=2029 git.sideband.band1.bytes=2029 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
- a5e29c5759f2c465
- Request
- 17fb193f…
- 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