GitLabgitlab-org

cli

main
ply6 git calls220 ms97 KiB4 cachedprotocol v2
158 lines4.1 KiBRaw
1//go:build !integration
3package oauth2
5import (
6 "testing"
7 "time"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "github.com/zalando/go-keyring"
12 "golang.org/x/oauth2"
14 "gitlab.com/gitlab-org/cli/internal/config"
17type stubConfig struct {
18 hosts map[string]map[string]string
19 getErr error
20 // writes counts calls to Write, when non-nil, so tests can assert that a
21 // persisted value was actually flushed rather than only held in memory.
22 writes *int
25func (s stubConfig) Get(host string, key string) (string, error) {
26 if s.getErr != nil {
27 return "", s.getErr
28 }
29 return s.hosts[host][key], nil
32func (s stubConfig) GetWithSource(host string, key string, _ bool) (string, string, error) {
33 if s.getErr != nil {
34 return "", "", s.getErr
35 }
36 return s.hosts[host][key], "", nil
39func (s stubConfig) Set(host string, key string, value string) error {
40 if _, ok := s.hosts[host]; !ok {
41 s.hosts[host] = make(map[string]string)
42 }
43 s.hosts[host][key] = value
45 return nil
48func (s stubConfig) Hosts() ([]string, error) { return nil, nil }
49func (s stubConfig) Aliases() (*config.AliasConfig, error) { return nil, nil }
50func (s stubConfig) Local() (*config.LocalConfig, error) { return nil, nil }
51func (s stubConfig) Write() error {
52 if s.writes != nil {
53 *s.writes++
54 }
55 return nil
57func (s stubConfig) WriteAll() error { return nil }
58func (s stubConfig) Reload() (config.Config, error) { return s, nil }
60func TestConfig_unmarshal(t *testing.T) {
61 tests := []struct {
62 name string
63 expiryDate string
64 expectedFormat string
65 }{
66 {
67 name: "RFC3339",
68 expiryDate: "2023-03-13T15:47:00Z",
69 expectedFormat: time.RFC3339,
70 },
71 {
72 name: "RFC822 with named timezone (legacy)",
73 expiryDate: "13 Mar 23 15:47 GMT",
74 expectedFormat: time.RFC822,
75 },
76 {
77 name: "RFC822Z with numeric offset (legacy, bug #7810)",
78 expiryDate: "03 Apr 25 21:50 +0530",
79 expectedFormat: time.RFC822Z,
80 },
81 }
83 for _, tt := range tests {
84 t.Run(tt.name, func(t *testing.T) {
85 cfg := stubConfig{
86 hosts: map[string]map[string]string{
87 "gitlab.com": {
88 "is_oauth2": "true",
89 "oauth2_refresh_token": "refresh_token",
90 "token": "access_token",
91 "oauth2_expiry_date": tt.expiryDate,
92 },
93 },
94 }
96 token, err := unmarshal("gitlab.com", cfg, true)
97 require.NoError(t, err)
99 assert.Equal(t, "refresh_token", token.RefreshToken)
100 assert.Equal(t, "access_token", token.AccessToken)
102 expectedDate, err := time.Parse(tt.expectedFormat, tt.expiryDate)
103 require.NoError(t, err)
105 assert.Equal(t, expectedDate, token.Expiry)
106 })
107 }
110func TestConfig_marshal(t *testing.T) {
111 cfg := stubConfig{
112 hosts: map[string]map[string]string{},
113 }
115 token := &oauth2.Token{
116 RefreshToken: "refresh_token",
117 AccessToken: "access_token",
118 ExpiresIn: 60,
119 Expiry: time.Now().Add(60 * time.Second),
120 }
122 err := marshal("gitlab.com", cfg, token)
123 require.NoError(t, err)
125 require.Equal(t, map[string]map[string]string{
126 "gitlab.com": {
127 "is_oauth2": "true",
128 "oauth2_refresh_token": "refresh_token",
129 "token": "access_token",
130 "oauth2_expiry_date": token.Expiry.Format(time.RFC3339),
131 },
132 }, cfg.hosts)
135func TestConfig_marshalPreservesKeyringRefreshTokenWhenReauthOmitsIt(t *testing.T) {
136 keyring.MockInit()
137 t.Cleanup(keyring.MockInit)
139 cfg := config.NewBlankConfig()
140 require.NoError(t, cfg.Set("gitlab.com", "use_keyring", "true"))
141 require.NoError(t, cfg.Set("gitlab.com", "oauth2_refresh_token", "existing-refresh-token"))
143 token := &oauth2.Token{
144 AccessToken: "new-access-token",
145 Expiry: time.Now().Add(time.Hour),
146 }
148 require.NoError(t, marshal("gitlab.com", cfg, token))
150 storedRefreshToken, err := keyring.Get("glab:gitlab.com:oauth2_refresh_token", "")
151 require.NoError(t, err)
152 assert.Equal(t, "existing-refresh-token", storedRefreshToken)
154 persistedToken, err := unmarshal("gitlab.com", cfg, true)
155 require.NoError(t, err)
156 assert.Equal(t, "existing-refresh-token", persistedToken.RefreshToken)
157 assert.Equal(t, "new-access-token", persistedToken.AccessToken)
Diagnostics6 git calls · 220 ms
CallStatusSizeTime0220 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 · hit2 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=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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=4273
git.response.cache=HIT
git.response.duration_ms=2
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:0200468 B · 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=468
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=c498f09dbd0cebbc59f9eff3b3ef8be66186fac6
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=468
git.pkt.payload.max=441
git.sideband.band1.bytes=441
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=441
git.pack.trailer=6420a98b3b2e923a6b61c550c00de326d620e479
Open pack map
fetch2001.4 KiB212 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=212
git.response.bytes=1391
git.response.cache=MISS
git.response.duration_ms=212
git.fetch.wants=01d887ad4d0c2843c550013b66844ee9e3d941d2
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=1391
git.pkt.payload.max=1364
git.sideband.band1.bytes=1364
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
a5e1900fe9225b65
Request
f5721a40…
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