forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_visibility_test.go
More file actions
284 lines (259 loc) · 8.32 KB
/
metrics_visibility_test.go
File metadata and controls
284 lines (259 loc) · 8.32 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
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
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/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
}
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")
}
}