-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathmetrics_visibility_test.go
More file actions
500 lines (457 loc) · 15.8 KB
/
metrics_visibility_test.go
File metadata and controls
500 lines (457 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"testing"
"time"
corecommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
)
type capturedRequest struct {
Method string
Path string
Body []byte
}
type visReq struct {
Method string
Path string
Body []byte
}
func startMockServer(t *testing.T) (*httptest.Server, chan capturedRequest) {
t.Helper()
ch := make(chan capturedRequest, 4)
handler := http.NewServeMux()
handler.HandleFunc("/artifactory/api/system/version", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"version":"7.200.0"}`))
})
handler.HandleFunc("/artifactory/api/system/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
// Call Home stub
handler.HandleFunc("/artifactory/api/system/usage", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
handler.HandleFunc("/jfconnect/api/v1/backoffice/metrics/log", func(w http.ResponseWriter, r *http.Request) {
defer func() {
_ = r.Body.Close()
}()
body, _ := io.ReadAll(r.Body)
ch <- capturedRequest{Method: r.Method, Path: r.URL.Path, Body: body}
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte("ok"))
})
srv := httptest.NewServer(handler)
return srv, ch
}
func startVisMockServer(t *testing.T) (*httptest.Server, chan visReq) {
t.Helper()
ch := make(chan visReq, 4)
mux := http.NewServeMux()
mux.HandleFunc("/artifactory/api/system/version", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"version":"7.200.0"}`))
})
mux.HandleFunc("/artifactory/api/system/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
mux.HandleFunc("/artifactory/api/system/usage", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
mux.HandleFunc("/jfconnect/api/v1/backoffice/metrics/log", func(w http.ResponseWriter, r *http.Request) {
defer func() {
_ = r.Body.Close()
}()
b, _ := io.ReadAll(r.Body)
ch <- visReq{Method: r.Method, Path: r.URL.Path, Body: b}
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte("ok"))
})
srv := httptest.NewServer(mux)
return srv, ch
}
// TestVisibility_AgentContext_E2E verifies that agent execution context
// (is_agent / agent / is_interactive) flows end-to-end from env vars through
// the metrics pipeline into the visibility wire payload.
//
// ExecutionContext memoizes via sync.Once for the process lifetime, and other
// tests in this binary trigger the metrics path first under `go test`, so we
// must reset the cache before re-evaluating env vars and reset again on
// cleanup so this test cannot leak agent state into anything that follows.
func TestVisibility_AgentContext_E2E(t *testing.T) {
corecommands.ResetExecutionContextForTest()
t.Cleanup(corecommands.ResetExecutionContextForTest)
t.Setenv("CURSOR_AGENT", "1")
srv, ch := startMockServer(t)
defer srv.Close()
home := t.TempDir()
t.Setenv("JFROG_CLI_HOME_DIR", home)
t.Setenv("JFROG_CLI_REPORT_USAGE", "true")
jf := coreTests.NewJfrogCli(execMain, "jf", "").WithoutCredentials()
platformURL := srv.URL + "/"
artURL := srv.URL + "/artifactory/"
if err := jf.Exec("c", "add", "agent-mock",
"--url", platformURL,
"--artifactory-url", artURL,
"--access-token", "dummy",
"--interactive=false",
"--enc-password=false",
); err != nil {
t.Fatalf("config add failed: %v", err)
}
if err := jf.Exec("c", "use", "agent-mock"); err != nil {
t.Fatalf("config use failed: %v", err)
}
if err := jf.Exec("rt", "ping", "--server-id", "agent-mock"); err != nil {
t.Fatalf("jf exec failed: %v", err)
}
select {
case req := <-ch:
if req.Path != "/jfconnect/api/v1/backoffice/metrics/log" {
t.Fatalf("unexpected path: %s", req.Path)
}
var payload struct {
Labels struct {
IsAgent string `json:"is_agent"`
Agent string `json:"agent"`
IsInteractive string `json:"is_interactive"`
} `json:"labels"`
}
if err := json.Unmarshal(req.Body, &payload); err != nil {
t.Fatalf("bad JSON: %v\nbody: %s", err, string(req.Body))
}
if payload.Labels.IsAgent != "true" {
t.Errorf("is_agent: got %q want %q (body: %s)", payload.Labels.IsAgent, "true", string(req.Body))
}
if payload.Labels.Agent != "cursor" {
t.Errorf("agent: got %q want %q (body: %s)", payload.Labels.Agent, "cursor", string(req.Body))
}
// Stdout is not a TTY under `go test`, so is_interactive should be "false".
if payload.Labels.IsInteractive != "false" {
t.Errorf("is_interactive: got %q want %q (body: %s)", payload.Labels.IsInteractive, "false", string(req.Body))
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout waiting for metrics POST")
}
}
// TestVisibility_NoAgent_E2E is the negative counterpart to
// TestVisibility_AgentContext_E2E: with no agent env var set, the wire payload
// must carry is_agent="false" and an empty agent name. ExecutionContext is
// reset because the positive test above memoizes IsAgent=true earlier in this
// binary.
func TestVisibility_NoAgent_E2E(t *testing.T) {
corecommands.ResetExecutionContextForTest()
t.Cleanup(corecommands.ResetExecutionContextForTest)
t.Setenv("CURSOR_AGENT", "")
t.Setenv("CLAUDECODE", "")
t.Setenv("AGENT", "")
srv, ch := startMockServer(t)
defer srv.Close()
home := t.TempDir()
t.Setenv("JFROG_CLI_HOME_DIR", home)
t.Setenv("JFROG_CLI_REPORT_USAGE", "true")
jf := coreTests.NewJfrogCli(execMain, "jf", "").WithoutCredentials()
platformURL := srv.URL + "/"
artURL := srv.URL + "/artifactory/"
if err := jf.Exec("c", "add", "noagent-mock",
"--url", platformURL,
"--artifactory-url", artURL,
"--access-token", "dummy",
"--interactive=false",
"--enc-password=false",
); err != nil {
t.Fatalf("config add failed: %v", err)
}
if err := jf.Exec("c", "use", "noagent-mock"); err != nil {
t.Fatalf("config use failed: %v", err)
}
if err := jf.Exec("rt", "ping", "--server-id", "noagent-mock"); err != nil {
t.Fatalf("jf exec failed: %v", err)
}
select {
case req := <-ch:
if req.Path != "/jfconnect/api/v1/backoffice/metrics/log" {
t.Fatalf("unexpected path: %s", req.Path)
}
var payload struct {
Labels struct {
IsAgent string `json:"is_agent"`
Agent string `json:"agent"`
} `json:"labels"`
}
if err := json.Unmarshal(req.Body, &payload); err != nil {
t.Fatalf("bad JSON: %v\nbody: %s", err, string(req.Body))
}
if payload.Labels.IsAgent != "false" {
t.Errorf("is_agent: got %q want %q (body: %s)", payload.Labels.IsAgent, "false", string(req.Body))
}
if payload.Labels.Agent != "" {
t.Errorf("agent: got %q want %q (body: %s)", payload.Labels.Agent, "", string(req.Body))
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout waiting for metrics POST")
}
}
func TestVisibilitySendUsage_RtCurl_E2E(t *testing.T) {
srv, ch := startMockServer(t)
defer srv.Close()
// Isolate CLI home and enable usage reporting
home := t.TempDir()
_ = os.Setenv("JFROG_CLI_HOME_DIR", home)
_ = os.Setenv("JFROG_CLI_REPORT_USAGE", "true")
jf := coreTests.NewJfrogCli(execMain, "jf", "").WithoutCredentials()
// Create mock server config pointing to the httptest server
platformURL := srv.URL + "/"
artURL := srv.URL + "/artifactory/"
if err := jf.Exec(
"c", "add", "mock",
"--url", platformURL,
"--artifactory-url", artURL,
"--access-token", "dummy",
"--interactive=false",
"--enc-password=false",
); err != nil {
t.Fatalf("config add failed: %v", err)
}
if err := jf.Exec("c", "use", "mock"); err != nil {
t.Fatalf("config use failed: %v", err)
}
// Run real CLI command pointing to mock Artifactory via server-id
if err := jf.Exec("rt", "curl", "-X", "POST", "/api/system/ping", "--server-id", "mock"); err != nil {
t.Fatalf("jf exec failed: %v", err)
}
// Assert metric was posted
select {
case req := <-ch:
if req.Method != http.MethodPost {
t.Fatalf("expected POST, got %s", req.Method)
}
if req.Path != "/jfconnect/api/v1/backoffice/metrics/log" {
t.Fatalf("unexpected path: %s", req.Path)
}
var payload struct {
Name string `json:"metrics_name"`
Labels struct {
Flags string `json:"flags"`
FeatureID string `json:"feature_id"`
} `json:"labels"`
}
if err := json.Unmarshal(req.Body, &payload); err != nil {
t.Fatalf("bad JSON: %v", err)
}
if payload.Name != "jfcli_commands_count" {
t.Fatalf("unexpected metric name: %s", payload.Name)
}
if payload.Labels.FeatureID != "rt_curl" {
t.Fatalf("unexpected feature_id: %s", payload.Labels.FeatureID)
}
// rt curl removes flags internally; expect empty
if payload.Labels.Flags != "" {
t.Fatalf("expected empty flags string, got %q", payload.Labels.Flags)
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout waiting for metrics POST")
}
}
func TestVisibilitySendUsage_RtPing_Flags(t *testing.T) {
srv, ch := startMockServer(t)
defer srv.Close()
// Isolate CLI home and enable usage reporting
home := t.TempDir()
_ = os.Setenv("JFROG_CLI_HOME_DIR", home)
_ = os.Setenv("JFROG_CLI_REPORT_USAGE", "true")
jf := coreTests.NewJfrogCli(execMain, "jf", "").WithoutCredentials()
// Create mock server config pointing to the httptest server
platformURL := srv.URL + "/"
artURL := srv.URL + "/artifactory/"
if err := jf.Exec(
"c", "add", "mock",
"--url", platformURL,
"--artifactory-url", artURL,
"--access-token", "dummy",
"--interactive=false",
"--enc-password=false",
); err != nil {
t.Fatalf("config add failed: %v", err)
}
if err := jf.Exec("c", "use", "mock"); err != nil {
t.Fatalf("config use failed: %v", err)
}
// Run ping with a flag to assert capture
err := jf.Exec("rt", "ping", "--server-id", "mock")
if err != nil {
t.Logf("jf exec failed: %v", err)
}
// Assert metric was posted with expected flags
select {
case req := <-ch:
if req.Path != "/jfconnect/api/v1/backoffice/metrics/log" {
t.Fatalf("unexpected path: %s", req.Path)
}
var payload struct {
Labels struct {
Flags string `json:"flags"`
} `json:"labels"`
}
if err := json.Unmarshal(req.Body, &payload); err != nil {
t.Fatalf("bad JSON: %v", err)
}
if payload.Labels.Flags != "server-id" {
t.Fatalf("flags mismatch: got %q want %q", payload.Labels.Flags, "server-id")
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout waiting for metrics POST")
}
}
func TestVisibility_GoBuild_Flags(t *testing.T) {
srv, ch := startVisMockServer(t)
defer srv.Close()
// Isolated home and enable usage
home := t.TempDir()
_ = os.Setenv("JFROG_CLI_HOME_DIR", home)
_ = os.Setenv("JFROG_CLI_REPORT_USAGE", "true")
jf := coreTests.NewJfrogCli(execMain, "jf", "").WithoutCredentials()
// Configure mock platform
platformURL := srv.URL + "/"
artURL := srv.URL + "/artifactory/"
if err := jf.Exec("c", "add", "mock", "--url", platformURL, "--artifactory-url", artURL, "--access-token", "dummy", "--interactive=false", "--enc-password=false"); err != nil {
t.Fatalf("config add failed: %v", err)
}
if err := jf.Exec("c", "use", "mock"); err != nil {
t.Fatalf("config use failed: %v", err)
}
// Create a minimal Go project
projDir := t.TempDir()
goMod := []byte("module example.com/jfvis\n\ngo 1.21\n")
mainGo := []byte("package main\nfunc main(){}\n")
if err := os.WriteFile(projDir+"/go.mod", goMod, 0o644); err != nil {
t.Fatalf("write go.mod: %v", err)
}
if err := os.WriteFile(projDir+"/main.go", mainGo, 0o644); err != nil {
t.Fatalf("write main.go: %v", err)
}
// Provide a minimal buildtools config so GoCmd passes verification
if err := os.MkdirAll(projDir+"/.jfrog/projects", 0o755); err != nil {
t.Fatalf("mkdir .jfrog/projects: %v", err)
}
goYaml := []byte("version: 1\n" +
"type: go\n" +
"resolver:\n" +
" repo: go-virtual\n" +
" serverId: mock\n" +
"deployer:\n" +
" repo: go-virtual\n" +
" serverId: mock\n")
if err := os.WriteFile(projDir+"/.jfrog/projects/go.yaml", goYaml, 0o644); err != nil {
t.Fatalf("write go.yaml: %v", err)
}
cwd, _ := os.Getwd()
defer func(dir string) {
err := os.Chdir(dir)
if err != nil {
t.Fatalf("Failed to restore working directory: %v", err)
}
}(cwd)
_ = os.Chdir(projDir)
// Run a buildtools command (go build) with flags; ignore execution error
_ = jf.Exec("go", "build", "--build-name", "test", "--build-number", "1", "--server-id", "mock")
select {
case req := <-ch:
if req.Path != "/jfconnect/api/v1/backoffice/metrics/log" {
t.Fatalf("unexpected path: %s", req.Path)
}
var p struct {
Labels struct {
Flags string `json:"flags"`
} `json:"labels"`
}
if err := json.Unmarshal(req.Body, &p); err != nil {
t.Fatalf("bad JSON: %v", err)
}
if p.Labels.Flags != "build-name,build-number,server-id" {
t.Fatalf("flags mismatch: got %q want %q", p.Labels.Flags, "build-name,build-number,server-id")
}
case <-time.After(15 * time.Second):
t.Fatal("timeout waiting for metric")
}
}
func TestVisibility_PackageAlias_Metrics(t *testing.T) {
// This test requires the ghost frog binary and alias setup.
homeDir := initGhostFrogTest(t)
srv, ch := startVisMockServer(t)
defer srv.Close()
// Install npm alias (creates symlink npm -> jf in alias bin dir)
installAliases(t, "npm")
// Configure the CLI to point at the mock server
platformURL := srv.URL + "/"
artURL := srv.URL + "/artifactory/"
out, err := runJfCommand(t, "c", "add", "mock", "--url", platformURL, "--artifactory-url", artURL,
"--access-token", "dummy", "--interactive=false", "--enc-password=false")
if err != nil {
t.Fatalf("config add failed: %s %v", out, err)
}
out, err = runJfCommand(t, "c", "use", "mock")
if err != nil {
t.Fatalf("config use failed: %s %v", out, err)
}
// Create a minimal npm project with JFrog config so "jf npm install" passes validation.
projDir := t.TempDir()
if err := os.WriteFile(projDir+"/package.json", []byte(`{"name":"test","version":"1.0.0"}`), 0o644); err != nil {
t.Fatalf("write package.json: %v", err)
}
if err := os.MkdirAll(projDir+"/.jfrog/projects", 0o755); err != nil {
t.Fatalf("mkdir .jfrog/projects: %v", err)
}
npmYaml := []byte("version: 1\ntype: npm\nresolver:\n repo: npm-virtual\n serverId: mock\ndeployer:\n repo: npm-virtual\n serverId: mock\n")
if err := os.WriteFile(projDir+"/.jfrog/projects/npm.yaml", npmYaml, 0o644); err != nil {
t.Fatalf("write npm.yaml: %v", err)
}
// Run the npm alias which triggers DispatchIfAlias -> runJFMode -> SetPackageAliasContext("npm")
// Then falls through to "jf npm install" which triggers metrics reporting.
npmPath := aliasToolPath(homeDir, "npm")
binDir := aliasBinDir(homeDir)
cmd := exec.Command(npmPath, "install")
cmd.Dir = projDir
cmd.Env = append(os.Environ(),
"JFROG_CLI_HOME_DIR="+homeDir,
"JFROG_CLI_REPORT_USAGE=true",
"JFROG_CLI_LOG_LEVEL=DEBUG",
"PATH="+binDir+string(os.PathListSeparator)+os.Getenv("PATH"),
)
cmdOut, _ := cmd.CombinedOutput()
t.Logf("npm alias output: %s", string(cmdOut))
select {
case req := <-ch:
if req.Path != "/jfconnect/api/v1/backoffice/metrics/log" {
t.Fatalf("unexpected path: %s", req.Path)
}
var payload struct {
Labels struct {
PackageAlias string `json:"package_alias"`
PackageManager string `json:"package_manager"`
} `json:"labels"`
}
t.Logf("RAW PAYLOAD: %s", string(req.Body))
if err := json.Unmarshal(req.Body, &payload); err != nil {
t.Fatalf("bad JSON: %v", err)
}
if payload.Labels.PackageAlias != "true" {
t.Errorf("expected package_alias=true, got %q", payload.Labels.PackageAlias)
}
if payload.Labels.PackageManager != "npm" {
t.Errorf("expected package_manager=npm, got %q", payload.Labels.PackageManager)
}
case <-time.After(15 * time.Second):
t.Fatal("timeout waiting for metrics POST")
}
}