GitLabgitlab-org

cli

main
ply6 git calls32 ms98 KiB4 cachedprotocol v2
217 lines5.4 KiBRaw
1//go:build !integration
3package config
5import (
6 "go/ast"
7 "go/parser"
8 "go/token"
9 "io/fs"
10 "path/filepath"
11 "strconv"
12 "strings"
13 "testing"
15 "github.com/stretchr/testify/require"
18func TestKeySchema_CoversEveryKeyReadByTheCodebase(t *testing.T) {
19 t.Parallel()
21 walkSourceFiles(t, func(path string, fset *token.FileSet, file *ast.File) {
22 imports := importNames(file)
24 ast.Inspect(file, func(n ast.Node) bool {
25 key, ok := configKeyRead(n, imports)
26 if !ok {
27 return true
28 }
29 if findKeyDef(ConfigKeyEquivalence(key)) == nil {
30 t.Errorf("%s: config key %q is read here but has no KeyDef in KeySchema; add one to internal/config/schema.go",
31 fset.Position(n.Pos()), key)
32 }
33 return true
34 })
35 })
38// GLAB_* and GITLAB_* are glab's own namespaces, so every variable in them
39// that the CLI reads should turn up in the reference. Other prefixes are out
40// of scope: CI_*, TERM, GOPATH and friends are probes of someone else's
41// environment.
42func TestGlabEnvVars_AreDeclaredOrDocumented(t *testing.T) {
43 t.Parallel()
45 documented := map[string]struct{}{}
46 for _, ev := range EnvVars() {
47 documented[ev.Name] = struct{}{}
48 }
50 walkSourceFiles(t, func(path string, fset *token.FileSet, file *ast.File) {
51 ast.Inspect(file, func(n ast.Node) bool {
52 name, ok := envVarRead(n)
53 if !ok || !ownNamespace(name) {
54 return true
55 }
56 if _, ok := documented[name]; ok {
57 return true
58 }
59 t.Errorf("%s: %s is read here but is not in the environment variable reference; add a KeyDef in internal/config/schema.go or an entry in nonSchemaEnvVars in internal/config/envvars.go",
60 fset.Position(n.Pos()), name)
61 return true
62 })
63 })
66// TestEnvVars_CoverEveryDocumentedName guards the other direction: an entry
67// that no longer matches a real variable would document a name that does
68// nothing.
69func TestEnvVars_CoverEveryDocumentedName(t *testing.T) {
70 t.Parallel()
72 fromSchema := map[string]struct{}{}
73 for _, kd := range KeySchema {
74 for _, name := range EnvVarsForKey(kd) {
75 fromSchema[name] = struct{}{}
76 }
77 }
79 read := map[string]struct{}{}
80 walkSourceFiles(t, func(path string, fset *token.FileSet, file *ast.File) {
81 ast.Inspect(file, func(n ast.Node) bool {
82 if name, ok := envVarRead(n); ok {
83 read[name] = struct{}{}
84 }
85 return true
86 })
87 })
89 for _, ev := range nonSchemaEnvVars {
90 if _, ok := fromSchema[ev.Name]; ok {
91 t.Errorf("%s is listed in nonSchemaEnvVars but is already a KeySchema env var; drop the hand-written entry", ev.Name)
92 }
93 if _, ok := read[ev.Name]; !ok && ev.Name != "GITLAB_GROUP" {
94 // GITLAB_GROUP is resolved through flag binding rather than a
95 // literal os.Getenv, so the AST walk cannot see it.
96 t.Errorf("%s is documented in nonSchemaEnvVars but nothing reads it", ev.Name)
97 }
98 }
101// predefinedCIVars are set by GitLab CI rather than by a glab user, so they
102// belong in the CI documentation rather than in glab's own reference.
103var predefinedCIVars = map[string]struct{}{
104 "GITLAB_CI": {},
107func ownNamespace(name string) bool {
108 if _, ok := predefinedCIVars[name]; ok {
109 return false
110 }
111 return strings.HasPrefix(name, "GLAB_") || strings.HasPrefix(name, "GITLAB_")
114func walkSourceFiles(t *testing.T, fn func(path string, fset *token.FileSet, file *ast.File)) {
115 t.Helper()
117 for _, dir := range []string{"internal", "cmd"} {
118 err := filepath.WalkDir(filepath.Join("..", "..", dir), func(path string, d fs.DirEntry, err error) error {
119 if err != nil {
120 return err
121 }
122 if d.IsDir() {
123 if d.Name() == "vendor" || d.Name() == "testdata" {
124 return fs.SkipDir
125 }
126 return nil
127 }
128 if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
129 return nil
130 }
132 fset := token.NewFileSet()
133 file, err := parser.ParseFile(fset, path, nil, 0)
134 if err != nil {
135 return err
136 }
137 fn(path, fset, file)
138 return nil
139 })
140 require.NoError(t, err)
141 }
144func importNames(file *ast.File) map[string]struct{} {
145 out := map[string]struct{}{}
146 for _, imp := range file.Imports {
147 name := ""
148 if imp.Name != nil {
149 name = imp.Name.Name
150 } else if path, err := strconv.Unquote(imp.Path.Value); err == nil {
151 name = path[strings.LastIndex(path, "/")+1:]
152 }
153 if name != "" {
154 out[name] = struct{}{}
155 out[strings.TrimPrefix(name, "go-")] = struct{}{}
156 }
157 }
158 return out
161func configKeyRead(n ast.Node, imports map[string]struct{}) (string, bool) {
162 call, ok := n.(*ast.CallExpr)
163 if !ok || len(call.Args) < 2 {
164 return "", false
165 }
166 sel, ok := call.Fun.(*ast.SelectorExpr)
167 if !ok {
168 return "", false
169 }
170 if recv, ok := sel.X.(*ast.Ident); ok {
171 if _, isPkg := imports[recv.Name]; isPkg {
172 return "", false
173 }
174 }
175 switch sel.Sel.Name {
176 case "Get":
177 if len(call.Args) != 2 {
178 return "", false
179 }
180 case "GetWithSource":
181 if len(call.Args) != 3 {
182 return "", false
183 }
184 default:
185 return "", false
186 }
187 return stringArg(call.Args[1])
190func envVarRead(n ast.Node) (string, bool) {
191 call, ok := n.(*ast.CallExpr)
192 if !ok || len(call.Args) != 1 {
193 return "", false
194 }
195 sel, ok := call.Fun.(*ast.SelectorExpr)
196 if !ok {
197 return "", false
198 }
199 switch sel.Sel.Name {
200 case "Getenv", "LookupEnv", "IsEnvVarEnabled":
201 default:
202 return "", false
203 }
204 return stringArg(call.Args[0])
207func stringArg(arg ast.Expr) (string, bool) {
208 lit, ok := arg.(*ast.BasicLit)
209 if !ok || lit.Kind != token.STRING {
210 return "", false
211 }
212 v, err := strconv.Unquote(lit.Value)
213 if err != nil {
214 return "", false
215 }
216 return v, true
Diagnostics6 git calls · 32 ms
CallStatusSizeTime032 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 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=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 · 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=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.4 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=1391
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=e9da9edfdcf44933ee5fa121d411de326220161c
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
git.pack.objects=1
git.pack.bytes=1364
git.pack.trailer=bf750bd385ed98d15e1fea1289d3b00caa61d64c
Open pack map
fetch2001.9 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=1978
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=d55289e89eaec43261cfc7b70b93d56c1885b3fc
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=1978
git.pkt.payload.max=1951
git.sideband.band1.bytes=1951
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
a5e173f2fd293488
Request
14ae435a…
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