-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
437 lines (398 loc) · 14.6 KB
/
Copy pathcli_test.go
File metadata and controls
437 lines (398 loc) · 14.6 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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
)
func TestRun_ShowsUsageOnMissingCommand(t *testing.T) {
code, _, stderrText := runCLI(t, []string{})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if !strings.Contains(stderrText, "Usage:") {
t.Fatalf("stderr missing usage: %s", stderrText)
}
if !strings.Contains(stderrText, "capture") || !strings.Contains(stderrText, "compare") {
t.Fatalf("stderr missing command list: %s", stderrText)
}
}
func TestRun_FlagExitPaths(t *testing.T) {
tests := []struct {
name string
args []string
wantStderr []string
}{
{
name: "capture help",
args: []string{"capture", "-h"},
wantStderr: []string{"Usage:", "capture", "compare"},
},
{
name: "compare help",
args: []string{"compare", "-h"},
wantStderr: []string{"Usage:", "capture", "compare"},
},
{
name: "capture invalid flag",
args: []string{"capture", "--bogus"},
wantStderr: []string{"flag provided but not defined", "Usage:"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, stdoutText, stderrText := runCLI(t, tt.args)
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if stdoutText != "" {
t.Fatalf("stdout = %q, want empty", stdoutText)
}
for _, want := range tt.wantStderr {
if !strings.Contains(stderrText, want) {
t.Fatalf("stderr missing %q: %s", want, stderrText)
}
}
})
}
}
func TestRun_CaptureAndCompareEndToEnd(t *testing.T) {
base := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
oldFactory := logsClientFactory
defer func() { logsClientFactory = oldFactory }()
factoryCalls := 0
logsClientFactory = func(region, profile string) (LogsClient, error) {
factoryCalls++
switch factoryCalls {
case 1:
return newCLIClient(base, "req-baseline", []string{"baseline log"}, false), nil
case 2:
return newCLIClient(base+10_000, "req-new", []string{"FATAL: database connection lost"}, true), nil
default:
t.Fatalf("unexpected logsClientFactory call %d", factoryCalls)
return nil, nil
}
}
outDir := t.TempDir()
code, captureStdout, captureStderr := runCLI(t, []string{"capture", "--function", "test-func", "--label", "baseline", "--count", "1", "--out", outDir})
if code != 0 {
t.Fatalf("baseline capture exit code = %d, stderr = %s", code, captureStderr)
}
if captureStderr != "" {
t.Fatalf("baseline capture stderr = %q, want empty", captureStderr)
}
if !strings.Contains(captureStdout, "Wrote snapshot") {
t.Fatalf("baseline capture stdout missing success message: %s", captureStdout)
}
code, _, captureStderr = runCLI(t, []string{"capture", "--function", "test-func", "--label", "new", "--count", "1", "--out", outDir})
if code != 0 {
t.Fatalf("new capture exit code = %d, stderr = %s", code, captureStderr)
}
if captureStderr != "" {
t.Fatalf("new capture stderr = %q, want empty", captureStderr)
}
baselinePath := filepath.Join(outDir, "test-func_baseline.json")
newPath := filepath.Join(outDir, "test-func_new.json")
data, err := os.ReadFile(baselinePath)
if err != nil {
t.Fatalf("failed to read baseline snapshot: %v", err)
}
if !strings.Contains(string(data), "\"memory_size_mb\"") || !strings.Contains(string(data), "\"max_memory_used_mb\"") {
t.Fatalf("snapshot did not use renamed memory fields: %s", string(data))
}
if strings.Contains(string(data), "\"mem_used_mb\"") || strings.Contains(string(data), "\"max_mem_mb\"") {
t.Fatalf("snapshot still contains legacy memory fields: %s", string(data))
}
code, compareStdout, compareStderr := runCLI(t, []string{"compare", "--a", baselinePath, "--b", newPath})
if code != 0 {
t.Fatalf("compare exit code = %d, stderr = %s", code, compareStderr)
}
if compareStderr != "" {
t.Fatalf("compare stderr = %q, want empty", compareStderr)
}
if !strings.Contains(compareStdout, "=== Comparison: test-func ===") {
t.Fatalf("compare stdout missing header: %s", compareStdout)
}
if !strings.Contains(compareStdout, "*** WARNING: Error count increased! ***") {
t.Fatalf("compare stdout missing error warning: %s", compareStdout)
}
}
func TestRun_CaptureOverwriteFlagAllowsExistingSnapshot(t *testing.T) {
base := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
oldFactory := logsClientFactory
defer func() { logsClientFactory = oldFactory }()
factoryCalls := 0
logsClientFactory = func(region, profile string) (LogsClient, error) {
factoryCalls++
switch factoryCalls {
case 1:
return newCLIClient(base, "req-original", []string{"original log"}, false), nil
case 2:
return newCLIClient(base+10_000, "req-new", []string{"new log"}, false), nil
default:
t.Fatalf("unexpected logsClientFactory call %d", factoryCalls)
return nil, nil
}
}
outDir := t.TempDir()
args := []string{"capture", "--function", "test-func", "--label", "baseline", "--count", "1", "--out", outDir}
code, _, stderrText := runCLI(t, args)
if code != 0 {
t.Fatalf("initial capture exit code = %d, stderr = %s", code, stderrText)
}
code, _, stderrText = runCLI(t, append(args, "--overwrite"))
if code != 0 {
t.Fatalf("overwrite capture exit code = %d, stderr = %s", code, stderrText)
}
var snap Snapshot
data, err := os.ReadFile(filepath.Join(outDir, "test-func_baseline.json"))
if err != nil {
t.Fatalf("failed to read snapshot: %v", err)
}
if err := json.Unmarshal(data, &snap); err != nil {
t.Fatalf("failed to unmarshal snapshot: %v", err)
}
if len(snap.Invocations) != 1 || snap.Invocations[0].RequestID != "req-new" {
t.Fatalf("captured invocations = %+v, want overwritten req-new snapshot", snap.Invocations)
}
}
func TestRun_CompareStrictFlag(t *testing.T) {
dir := t.TempDir()
writeSnapshotFile(t, filepath.Join(dir, "a.json"), Snapshot{FunctionName: "func-a", LogGroup: "/aws/lambda/func-a"})
writeSnapshotFile(t, filepath.Join(dir, "b.json"), Snapshot{FunctionName: "func-b", LogGroup: "/aws/lambda/func-b"})
code, stdoutText, stderrText := runCLI(t, []string{"compare", "--strict", "--a", filepath.Join(dir, "a.json"), "--b", filepath.Join(dir, "b.json")})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if stdoutText != "" {
t.Fatalf("stdout = %q, want empty", stdoutText)
}
if !strings.Contains(stderrText, "strict comparison failed") {
t.Fatalf("stderr missing strict failure: %s", stderrText)
}
}
func TestRun_CompareFailOnRegressionFlag(t *testing.T) {
code, stdoutText, stderrText := runCLI(t, []string{
"compare",
"--fail-on-regression",
"--a", "testdata/compare_baseline.json",
"--b", "testdata/compare_new.json",
})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if !strings.Contains(stdoutText, "*** NEW error patterns in new deployment: ***") {
t.Fatalf("stdout missing full compare report: %s", stdoutText)
}
if !strings.Contains(stderrText, "regression detected: 1 new error pattern(s)") {
t.Fatalf("stderr missing regression failure: %s", stderrText)
}
}
func TestRun_CompareJSONFlag(t *testing.T) {
code, stdoutText, stderrText := runCLI(t, []string{
"compare",
"--json",
"--a", "testdata/compare_baseline.json",
"--b", "testdata/compare_new.json",
})
if code != 0 {
t.Fatalf("exit code = %d, stderr = %s", code, stderrText)
}
if stderrText != "" {
t.Fatalf("stderr = %q, want empty", stderrText)
}
var summary compareSummary
if err := json.Unmarshal([]byte(stdoutText), &summary); err != nil {
t.Fatalf("stdout is not valid compare summary JSON: %v\n%s", err, stdoutText)
}
if summary.Title != "test-func" {
t.Fatalf("summary title = %q, want test-func", summary.Title)
}
}
func TestRun_CompareConfiguredRegressionGateFlag(t *testing.T) {
dir := t.TempDir()
fileA := filepath.Join(dir, "a.json")
fileB := filepath.Join(dir, "b.json")
writeSnapshotFile(t, fileA, Snapshot{
FunctionName: "func-a",
LogGroup: "/aws/lambda/func-a",
Invocations: []InvocationRecord{
{RequestID: "req-a", Timestamp: "2026-02-25T10:00:00Z", Duration: "100 ms"},
},
})
writeSnapshotFile(t, fileB, Snapshot{
FunctionName: "func-a",
LogGroup: "/aws/lambda/func-a",
Invocations: []InvocationRecord{
{RequestID: "req-b", Timestamp: "2026-02-25T10:01:00Z", Duration: "150 ms"},
},
})
code, stdoutText, stderrText := runCLI(t, []string{
"compare",
"--max-duration-regression-pct", "20",
"--a", fileA,
"--b", fileB,
})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if !strings.Contains(stdoutText, "=== Regression Gates ===") {
t.Fatalf("stdout missing regression gate section: %s", stdoutText)
}
if !strings.Contains(stderrText, "p90 duration increased by 50.0%") {
t.Fatalf("stderr missing duration regression reason: %s", stderrText)
}
}
func TestRun_CaptureDuplicateFunctionsFails(t *testing.T) {
oldFactory := logsClientFactory
defer func() { logsClientFactory = oldFactory }()
logsClientFactory = func(region, profile string) (LogsClient, error) {
t.Fatal("logsClientFactory should not be called when capture input validation fails")
return nil, nil
}
code, stdoutText, stderrText := runCLI(t, []string{"capture", "--function", "func-a, func-a", "--label", "baseline"})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if stdoutText != "" {
t.Fatalf("stdout = %q, want empty", stdoutText)
}
if !strings.Contains(stderrText, `--function contains duplicate name "func-a"`) {
t.Fatalf("stderr missing duplicate-name validation: %s", stderrText)
}
}
func TestRun_CaptureMultipleFunctionsReturnsFailureCount(t *testing.T) {
oldFactory := logsClientFactory
defer func() { logsClientFactory = oldFactory }()
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
logsClientFactory = func(region, profile string) (LogsClient, error) {
return &mockLogsClient{
describeLogStreamsFn: func(ctx context.Context, params *cloudwatchlogs.DescribeLogStreamsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogStreamsOutput, error) {
if aws.ToString(params.LogGroupName) == "/aws/lambda/fail-func" {
return nil, errors.New("describe failed")
}
return &cloudwatchlogs.DescribeLogStreamsOutput{
LogStreams: []types.LogStream{{LogStreamName: aws.String("stream-1")}},
}, nil
},
getLogEventsFn: func(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) {
return singleTailPage([]types.OutputLogEvent{
{Message: aws.String("START RequestId: req-ok Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("REPORT RequestId: req-ok\tDuration: 50 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 64 MB"), Timestamp: aws.Int64(ts + 100)},
})(ctx, params, optFns...)
},
}, nil
}
outDir := t.TempDir()
code, stdoutText, stderrText := runCLI(t, []string{
"capture",
"--function", "ok-func,fail-func",
"--label", "baseline",
"--count", "1",
"--out", outDir,
})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if !strings.Contains(stdoutText, "Wrote snapshot") {
t.Fatalf("stdout missing successful capture: %s", stdoutText)
}
if !strings.Contains(stderrText, "Error capturing fail-func: describe-log-streams: describe failed") {
t.Fatalf("stderr missing function capture error: %s", stderrText)
}
if !strings.Contains(stderrText, "1 capture(s) failed") {
t.Fatalf("stderr missing failure count: %s", stderrText)
}
if _, err := os.Stat(filepath.Join(outDir, "ok-func_baseline.json")); err != nil {
t.Fatalf("expected successful function snapshot: %v", err)
}
}
func TestRun_CaptureAWSClientInitFailure(t *testing.T) {
oldFactory := logsClientFactory
defer func() { logsClientFactory = oldFactory }()
logsClientFactory = func(region, profile string) (LogsClient, error) {
return nil, errors.New("boom")
}
code, stdoutText, stderrText := runCLI(t, []string{"capture", "--function", "func-a", "--label", "baseline"})
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if stdoutText != "" {
t.Fatalf("stdout = %q, want empty", stdoutText)
}
if !strings.Contains(stderrText, "Error initializing AWS client: boom") {
t.Fatalf("stderr missing AWS init failure: %s", stderrText)
}
}
func runCLI(t *testing.T, args []string) (int, string, string) {
t.Helper()
oldStdout := stdout
oldStderr := stderr
var outBuf, errBuf bytes.Buffer
stdout = &outBuf
stderr = &errBuf
defer func() {
stdout = oldStdout
stderr = oldStderr
}()
return run(args), outBuf.String(), errBuf.String()
}
func newCLIClient(ts int64, requestID string, logLines []string, isError bool) LogsClient {
return &mockLogsClient{
describeLogStreamsFn: func(ctx context.Context, params *cloudwatchlogs.DescribeLogStreamsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogStreamsOutput, error) {
return &cloudwatchlogs.DescribeLogStreamsOutput{
LogStreams: []types.LogStream{{LogStreamName: aws.String("stream-1")}},
}, nil
},
getLogEventsFn: func(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) {
if params.NextToken != nil {
return &cloudwatchlogs.GetLogEventsOutput{
NextBackwardToken: params.NextToken,
}, nil
}
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: " + requestID + " Version: $LATEST"), Timestamp: aws.Int64(ts)},
}
for i, line := range logLines {
events = append(events, types.OutputLogEvent{
Message: aws.String(line),
Timestamp: aws.Int64(ts + int64((i+1)*100)),
})
}
events = append(events, types.OutputLogEvent{
Message: aws.String("REPORT RequestId: " + requestID + "\tDuration: 50 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 64 MB"),
Timestamp: aws.Int64(ts + 1000),
})
if isError {
events = append(events, types.OutputLogEvent{
Message: aws.String("RequestId: " + requestID + " process exited before completing request"),
Timestamp: aws.Int64(ts + 1100),
})
}
latestFirst := append([]types.OutputLogEvent(nil), events...)
reverseLogEvents(latestFirst)
return &cloudwatchlogs.GetLogEventsOutput{
Events: latestFirst,
NextBackwardToken: aws.String("tail-1"),
}, nil
},
}
}
func writeSnapshotFile(t *testing.T, path string, snap Snapshot) {
t.Helper()
data, err := json.MarshalIndent(snap, "", " ")
if err != nil {
t.Fatalf("json.MarshalIndent error: %v", err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
t.Fatalf("os.WriteFile error: %v", err)
}
}