-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
935 lines (801 loc) · 25.1 KB
/
integration_test.go
File metadata and controls
935 lines (801 loc) · 25.1 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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
//go:build integration
// +build integration
package goexec
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/victoralfred/goexec/executor"
"github.com/victoralfred/goexec/resilience"
)
// TestIntegration_CompleteWorkflow tests the complete end-to-end workflow.
func TestIntegration_CompleteWorkflow(t *testing.T) {
ctx := context.Background()
// Create executor with default settings
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Test basic command execution
cmd, err := Cmd("/bin/echo", "hello", "world").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result, err := exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
if result.ExitCode != 0 {
t.Errorf("Expected exit code 0, got %d", result.ExitCode)
}
expectedOutput := "hello world\n"
if result.StdoutString() != expectedOutput {
t.Errorf("Expected output %q, got %q", expectedOutput, result.StdoutString())
}
if !result.Success() {
t.Error("Expected command to succeed")
}
if result.Duration == 0 {
t.Error("Expected non-zero duration")
}
}
// TestIntegration_EnvironmentMerging tests automatic environment merging.
func TestIntegration_EnvironmentMerging(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Test that minimal environment is provided and custom env variables are merged
cmd, err := Cmd("/usr/bin/env").
WithEnv("CUSTOM_VAR", "custom_value").
WithEnv("TEST_VAR", "test_value").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result, err := exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
if result.ExitCode != 0 {
t.Errorf("Expected exit code 0, got %d", result.ExitCode)
}
output := result.StdoutString()
// Verify minimal environment variables are present
minimalVars := []string{"PATH=", "LANG=", "LC_ALL=", "HOME=", "USER="}
for _, varName := range minimalVars {
if !strings.Contains(output, varName) {
t.Errorf("Expected minimal environment variable %q not found in output", varName)
}
}
// Verify custom environment variables are present
if !strings.Contains(output, "CUSTOM_VAR=custom_value") {
t.Error("Custom environment variable CUSTOM_VAR not found")
}
if !strings.Contains(output, "TEST_VAR=test_value") {
t.Error("Custom environment variable TEST_VAR not found")
}
// Verify custom PATH override works
cmd2, err := Cmd("/usr/bin/env").
WithEnv("PATH", "/custom/path:/usr/bin").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result2, err := exec.Execute(ctx, cmd2)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
if !strings.Contains(result2.StdoutString(), "PATH=/custom/path:/usr/bin") {
t.Error("Custom PATH override did not work")
}
}
// TestIntegration_AsyncExecution tests asynchronous execution with Future.
func TestIntegration_AsyncExecution(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
cmd, err := Cmd("/bin/echo", "async", "test").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
// Execute asynchronously
future := exec.ExecuteAsync(ctx, cmd)
// Test Done() channel
select {
case <-future.Done():
// Success - done channel closed
case <-time.After(5 * time.Second):
t.Fatal("Future did not complete within timeout")
}
// Get result
result, err := future.Wait()
if err != nil {
t.Fatalf("Async execution failed: %v", err)
}
if result.ExitCode != 0 {
t.Errorf("Expected exit code 0, got %d", result.ExitCode)
}
if !strings.Contains(result.StdoutString(), "async test") {
t.Errorf("Expected output to contain 'async test', got %q", result.StdoutString())
}
}
// TestIntegration_BatchExecution tests batch execution.
func TestIntegration_BatchExecution(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Create multiple commands
cmds := make([]*executor.Command, 5)
for i := 0; i < 5; i++ {
cmd, err := Cmd("/bin/echo", fmt.Sprintf("batch-%d", i)).Build()
if err != nil {
t.Fatalf("Failed to build command %d: %v", i, err)
}
cmds[i] = cmd
}
// Execute batch
results, err := exec.ExecuteBatch(ctx, cmds)
if err != nil {
t.Fatalf("Batch execution failed: %v", err)
}
if len(results) != 5 {
t.Errorf("Expected 5 results, got %d", len(results))
}
// Verify all commands succeeded
for i, result := range results {
if result.ExitCode != 0 {
t.Errorf("Command %d failed with exit code %d", i, result.ExitCode)
}
expected := fmt.Sprintf("batch-%d\n", i)
if result.StdoutString() != expected {
t.Errorf("Command %d: expected %q, got %q", i, expected, result.StdoutString())
}
}
}
// TestIntegration_Streaming tests streaming output.
func TestIntegration_Streaming(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
var stdoutBuf, stderrBuf bytes.Buffer
cmd, err := Cmd("/bin/echo", "streamed", "output").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
err = exec.Stream(ctx, cmd, &stdoutBuf, &stderrBuf)
if err != nil {
t.Fatalf("Streaming failed: %v", err)
}
if !strings.Contains(stdoutBuf.String(), "streamed output") {
t.Errorf("Expected 'streamed output' in stdout, got %q", stdoutBuf.String())
}
}
// TestIntegration_Timeout tests timeout handling.
func TestIntegration_Timeout(t *testing.T) {
ctx := context.Background()
exec, err := NewBuilder().
WithDefaultTimeout(100 * time.Millisecond).
Build()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
cmd, err := Cmd("/bin/sleep", "10").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result, err := exec.Execute(ctx, cmd)
// On timeout, the command may be killed, resulting in StatusKilled or StatusTimeout
if result == nil {
t.Error("Expected result even on timeout")
} else {
// Both StatusTimeout and StatusKilled are valid for timeout scenarios
if result.Status != executor.StatusTimeout && result.Status != executor.StatusKilled {
t.Errorf("Expected StatusTimeout or StatusKilled on timeout, got %v", result.Status)
}
}
}
// TestIntegration_PolicyValidation tests policy-based validation.
func TestIntegration_PolicyValidation(t *testing.T) {
ctx := context.Background()
// Create mock policy
mockPolicy := &mockPolicy{
validateFunc: func(ctx context.Context, cmd *executor.Command) (*executor.ValidationResult, error) {
if cmd.Binary == "/bin/echo" {
// Check if args match allowed pattern
if len(cmd.Args) > 0 && cmd.Args[0] == "hello" {
return &executor.ValidationResult{Allowed: true}, nil
}
return &executor.ValidationResult{
Allowed: false,
Reason: "Argument not in allowed list",
}, nil
}
return &executor.ValidationResult{Allowed: false}, nil
},
}
exec, err := NewBuilder().
WithPolicy(mockPolicy).
Build()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Test allowed command
allowedCmd, err := Cmd("/bin/echo", "hello").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result, err := exec.Execute(ctx, allowedCmd)
if err != nil {
t.Fatalf("Allowed command should execute: %v", err)
}
if result.Status == executor.StatusPolicyDenied {
t.Error("Allowed command was denied by policy")
}
// Test denied command
deniedCmd, err := Cmd("/bin/echo", "forbidden").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result2, err := exec.Execute(ctx, deniedCmd)
if err == nil {
t.Error("Expected policy violation error")
}
if result2 != nil && result2.Status != executor.StatusPolicyDenied {
t.Errorf("Expected StatusPolicyDenied, got %v", result2.Status)
}
}
// TestIntegration_RateLimiting tests rate limiting.
func TestIntegration_RateLimiting(t *testing.T) {
ctx := context.Background()
// Create rate limiter: allow 2 requests per second
rateLimiter := resilience.NewRateLimiter(resilience.RateLimiterConfig{
DefaultLimit: 2.0, // 2 requests per second
DefaultBurst: 2,
PerBinary: false,
})
exec, err := NewBuilder().
WithRateLimiter(rateLimiter).
Build()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
cmd, err := Cmd("/bin/echo", "rate", "limit", "test").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
// First two requests should succeed quickly
for i := 0; i < 2; i++ {
result, err := exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Request %d failed: %v", i, err)
}
if result.ExitCode != 0 {
t.Errorf("Request %d: expected exit code 0, got %d", i, result.ExitCode)
}
}
// Third request should be rate limited (will wait or timeout)
// We use a short timeout context to avoid waiting too long
limitedCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
result, err := exec.Execute(limitedCtx, cmd)
if err == nil {
// If it succeeded, it means rate limiter allowed it (after waiting)
// That's also valid behavior
if result.ExitCode != 0 {
t.Errorf("Request should succeed after rate limit wait: exit code %d", result.ExitCode)
}
} else {
// Expected: rate limited or timeout
if result != nil && result.Status != executor.StatusRateLimited {
// Timeout is also acceptable
if err != context.DeadlineExceeded {
t.Logf("Rate limiting test: got error %v (this may be expected)", err)
}
}
}
}
// TestIntegration_CircuitBreaker tests circuit breaker functionality.
func TestIntegration_CircuitBreaker(t *testing.T) {
ctx := context.Background()
circuitBreaker := resilience.NewCircuitBreaker(resilience.CircuitBreakerConfig{
FailureThreshold: 3,
SuccessThreshold: 2,
Timeout: 100 * time.Millisecond,
PerBinary: false,
})
exec, err := NewBuilder().
WithCircuitBreaker(circuitBreaker).
Build()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Use a command that will fail (non-existent binary for failures)
// For this test, we'll use a command that succeeds to test circuit breaker state
cmd, err := Cmd("/bin/echo", "circuit", "breaker", "test").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
// Execute multiple times - all should succeed
for i := 0; i < 5; i++ {
result, err := exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution %d failed: %v", i, err)
}
if result.ExitCode != 0 {
t.Errorf("Execution %d: expected exit code 0, got %d", i, result.ExitCode)
}
}
// Circuit breaker should remain closed (allowing requests) since all succeeded
// We can't easily test open state without causing actual failures,
// but we verified the circuit breaker is integrated
}
// TestIntegration_Hooks tests pre and post execution hooks.
func TestIntegration_Hooks(t *testing.T) {
ctx := context.Background()
var preExecuted, postExecuted int32
var preCmd, postCmd *executor.Command
var postResult *executor.Result
var postErr error
hook := &mockHook{
preExecuteFunc: func(ctx context.Context, cmd *executor.Command) (*executor.Command, error) {
atomic.AddInt32(&preExecuted, 1)
preCmd = cmd
// Modify command by adding an env var
modified := cmd.Clone()
modified.Env["HOOK_ADDED_VAR"] = "hook_value"
return modified, nil
},
postExecuteFunc: func(ctx context.Context, cmd *executor.Command, result *executor.Result, err error) error {
atomic.AddInt32(&postExecuted, 1)
postCmd = cmd
postResult = result
postErr = err
return nil
},
}
exec, err := NewBuilder().
WithHooks(hook).
Build()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
cmd, err := Cmd("/bin/echo", "hook", "test").
WithEnv("ORIGINAL_VAR", "original_value").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
_, err = exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
// Verify hooks were called
if atomic.LoadInt32(&preExecuted) != 1 {
t.Errorf("Expected pre-execute hook to be called once, got %d", atomic.LoadInt32(&preExecuted))
}
if atomic.LoadInt32(&postExecuted) != 1 {
t.Errorf("Expected post-execute hook to be called once, got %d", atomic.LoadInt32(&postExecuted))
}
// Verify hook received correct command
if preCmd == nil {
t.Error("Pre-execute hook did not receive command")
}
if postCmd == nil {
t.Error("Post-execute hook did not receive command")
}
// Verify hook modification (added env var) was applied
// Note: The actual execution uses the modified command from pre-hook
if postResult == nil {
t.Error("Post-execute hook did not receive result")
}
// Verify post-execute received correct result
if postResult.ExitCode != 0 {
t.Errorf("Post-execute hook received result with exit code %d", postResult.ExitCode)
}
if postErr != nil {
t.Errorf("Post-execute hook received error: %v", postErr)
}
}
// TestIntegration_Telemetry tests telemetry/metrics collection.
func TestIntegration_Telemetry(t *testing.T) {
ctx := context.Background()
// Create mock telemetry
var spanStarted, metricRecorded int32
telemetry := &mockTelemetry{
startSpanFunc: func(ctx context.Context, name string) (context.Context, func()) {
atomic.AddInt32(&spanStarted, 1)
return ctx, func() {}
},
recordMetricFunc: func(name string, value float64, labels map[string]string) {
atomic.AddInt32(&metricRecorded, 1)
},
}
exec, err := NewBuilder().
WithTelemetry(telemetry).
Build()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
cmd, err := Cmd("/bin/echo", "telemetry", "test").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
_, err = exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
// Give telemetry time to record
time.Sleep(50 * time.Millisecond)
// Verify telemetry was called
if atomic.LoadInt32(&spanStarted) == 0 {
t.Error("Expected telemetry span to be started")
}
if atomic.LoadInt32(&metricRecorded) == 0 {
t.Error("Expected telemetry metric to be recorded")
}
}
// TestIntegration_ConvenienceFunctions tests convenience functions.
func TestIntegration_ConvenienceFunctions(t *testing.T) {
ctx := context.Background()
// Test Execute convenience function
result, err := Execute(ctx, "/bin/echo", "convenience", "test")
if err != nil {
t.Fatalf("Execute convenience function failed: %v", err)
}
if result.ExitCode != 0 {
t.Errorf("Expected exit code 0, got %d", result.ExitCode)
}
if !strings.Contains(result.StdoutString(), "convenience test") {
t.Errorf("Expected 'convenience test' in output, got %q", result.StdoutString())
}
// Test ExecuteWithTimeout convenience function
result2, err := ExecuteWithTimeout(ctx, 5*time.Second, "/bin/echo", "timeout", "test")
if err != nil {
t.Fatalf("ExecuteWithTimeout convenience function failed: %v", err)
}
if result2.ExitCode != 0 {
t.Errorf("Expected exit code 0, got %d", result2.ExitCode)
}
// Test Stream convenience function
var stdoutBuf bytes.Buffer
err = Stream(ctx, &stdoutBuf, os.Stderr, "/bin/echo", "stream", "convenience")
if err != nil {
t.Fatalf("Stream convenience function failed: %v", err)
}
if !strings.Contains(stdoutBuf.String(), "stream convenience") {
t.Errorf("Expected 'stream convenience' in output, got %q", stdoutBuf.String())
}
}
// TestIntegration_ErrorHandling tests error handling scenarios.
func TestIntegration_ErrorHandling(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Test invalid command (non-existent binary)
cmd, err := Cmd("/nonexistent/binary", "arg").Build()
if err != nil {
t.Fatalf("Command should build (validation happens at execution): %v", err)
}
result, err := exec.Execute(ctx, cmd)
// Command should fail (binary not found)
if err == nil && result.ExitCode == 0 {
t.Error("Expected command with nonexistent binary to fail")
}
// Test command with non-zero exit code
cmd2, err := Cmd("/bin/sh", "-c", "exit 42").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result2, err := exec.Execute(ctx, cmd2)
if err != nil {
// Error is acceptable for non-zero exit
t.Logf("Command with exit code 42 returned error (acceptable): %v", err)
}
if result2 != nil && result2.ExitCode != 42 {
t.Errorf("Expected exit code 42, got %d", result2.ExitCode)
}
}
// TestIntegration_ResourceUsage tests resource usage tracking.
func TestIntegration_ResourceUsage(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
cmd, err := Cmd("/bin/echo", "resource", "usage").Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
result, err := exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
// Verify duration is tracked
if result.Duration == 0 {
t.Error("Expected non-zero duration")
}
// Verify resource usage is available if process state was captured
if result.ResourceUsage != nil {
totalCPU := result.ResourceUsage.TotalCPUTime()
if totalCPU < 0 {
t.Error("Expected non-negative CPU time")
}
}
}
// TestIntegration_ConcurrentExecution tests concurrent execution.
func TestIntegration_ConcurrentExecution(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
const numGoroutines = 10
var wg sync.WaitGroup
errors := make([]error, numGoroutines)
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
cmd, err := Cmd("/bin/echo", fmt.Sprintf("concurrent-%d", id)).Build()
if err != nil {
errors[id] = fmt.Errorf("build failed: %w", err)
return
}
result, err := exec.Execute(ctx, cmd)
if err != nil {
errors[id] = err
return
}
if result.ExitCode != 0 {
errors[id] = fmt.Errorf("exit code %d", result.ExitCode)
return
}
expected := fmt.Sprintf("concurrent-%d\n", id)
if result.StdoutString() != expected {
errors[id] = fmt.Errorf("unexpected output: %q", result.StdoutString())
}
}(i)
}
wg.Wait()
// Check for errors
for i, err := range errors {
if err != nil {
t.Errorf("Goroutine %d failed: %v", i, err)
}
}
}
// TestIntegration_CommandBuilder tests command builder features.
func TestIntegration_CommandBuilder(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
// Test all builder methods
cmd, err := Cmd("/bin/echo", "builder", "test").
WithWorkingDir("/tmp").
WithTimeout(5*time.Second).
WithEnv("BUILDER_VAR", "builder_value").
WithEnvMap(map[string]string{
"MAP_VAR1": "map_value1",
"MAP_VAR2": "map_value2",
}).
WithPriority(executor.PriorityHigh).
WithMetadata("test_metadata", "metadata_value").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
// Verify command properties
if cmd.WorkingDir != "/tmp" {
t.Errorf("Expected working dir /tmp, got %q", cmd.WorkingDir)
}
if cmd.Timeout != 5*time.Second {
t.Errorf("Expected timeout 5s, got %v", cmd.Timeout)
}
if cmd.Env["BUILDER_VAR"] != "builder_value" {
t.Error("Builder env var not set correctly")
}
if cmd.Env["MAP_VAR1"] != "map_value1" || cmd.Env["MAP_VAR2"] != "map_value2" {
t.Error("Env map not set correctly")
}
if cmd.Priority != executor.PriorityHigh {
t.Errorf("Expected PriorityHigh, got %v", cmd.Priority)
}
if cmd.Metadata["test_metadata"] != "metadata_value" {
t.Error("Metadata not set correctly")
}
// Execute the command
result, err := exec.Execute(ctx, cmd)
if err != nil {
t.Fatalf("Execution failed: %v", err)
}
if result.ExitCode != 0 {
t.Errorf("Expected exit code 0, got %d", result.ExitCode)
}
}
// TestIntegration_CommandClone tests command cloning.
func TestIntegration_CommandClone(t *testing.T) {
ctx := context.Background()
exec, err := New()
if err != nil {
t.Fatalf("Failed to create executor: %v", err)
}
defer func() {
if shutdownErr := exec.Shutdown(context.Background()); shutdownErr != nil {
t.Errorf("Shutdown failed: %v", shutdownErr)
}
}()
original, err := Cmd("/bin/echo", "original").
WithEnv("ORIGINAL_VAR", "original_value").
Build()
if err != nil {
t.Fatalf("Failed to build command: %v", err)
}
// Clone and modify
clone := original.Clone()
clone.Env["CLONE_VAR"] = "clone_value"
clone.Env["ORIGINAL_VAR"] = "modified_value"
// Verify original is unchanged
if original.Env["ORIGINAL_VAR"] != "original_value" {
t.Error("Original command was modified")
}
if original.Env["CLONE_VAR"] != "" {
t.Error("Original command should not have CLONE_VAR")
}
// Verify clone is independent
if clone.Env["ORIGINAL_VAR"] != "modified_value" {
t.Error("Clone modification did not work")
}
// Execute both
originalResult, err := exec.Execute(ctx, original)
if err != nil {
t.Fatalf("Original execution failed: %v", err)
}
cloneResult, err := exec.Execute(ctx, clone)
if err != nil {
t.Fatalf("Clone execution failed: %v", err)
}
if originalResult.ExitCode != 0 || cloneResult.ExitCode != 0 {
t.Error("Both commands should succeed")
}
}
// Mock types for testing
type mockPolicy struct {
validateFunc func(ctx context.Context, cmd *executor.Command) (*executor.ValidationResult, error)
}
func (m *mockPolicy) Validate(ctx context.Context, cmd *executor.Command) (*executor.ValidationResult, error) {
if m.validateFunc != nil {
return m.validateFunc(ctx, cmd)
}
return &executor.ValidationResult{Allowed: true}, nil
}
type mockHook struct {
preExecuteFunc func(ctx context.Context, cmd *executor.Command) (*executor.Command, error)
postExecuteFunc func(ctx context.Context, cmd *executor.Command, result *executor.Result, err error) error
}
func (m *mockHook) PreExecute(ctx context.Context, cmd *executor.Command) (*executor.Command, error) {
if m.preExecuteFunc != nil {
return m.preExecuteFunc(ctx, cmd)
}
return cmd, nil
}
func (m *mockHook) PostExecute(ctx context.Context, cmd *executor.Command, result *executor.Result, err error) error {
if m.postExecuteFunc != nil {
return m.postExecuteFunc(ctx, cmd, result, err)
}
return nil
}
type mockTelemetry struct {
startSpanFunc func(ctx context.Context, name string) (context.Context, func())
recordMetricFunc func(name string, value float64, labels map[string]string)
}
func (m *mockTelemetry) StartSpan(ctx context.Context, name string) (context.Context, func()) {
if m.startSpanFunc != nil {
return m.startSpanFunc(ctx, name)
}
return ctx, func() {}
}
func (m *mockTelemetry) RecordMetric(name string, value float64, labels map[string]string) {
if m.recordMetricFunc != nil {
m.recordMetricFunc(name, value, labels)
}
}
// Ensure mockTelemetry implements executor.Telemetry
var _ executor.Telemetry = (*mockTelemetry)(nil)
// Ensure mockHook implements executor.Hook
var _ executor.Hook = (*mockHook)(nil)