-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_integration_test.go
More file actions
162 lines (139 loc) · 4.65 KB
/
Copy pathmain_integration_test.go
File metadata and controls
162 lines (139 loc) · 4.65 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
package main
import (
"os"
"syscall"
"testing"
"file-shifter/config"
)
type fakeWorker struct {
started bool
stopped bool
done chan struct{}
}
func (w *fakeWorker) Start() {
w.started = true
<-w.done
}
func (w *fakeWorker) Stop() {
if !w.stopped {
w.stopped = true
close(w.done)
}
}
type fakeHealthMonitor struct {
started bool
stopped bool
}
func (h *fakeHealthMonitor) Start() {
h.started = true
}
func (h *fakeHealthMonitor) Stop() {
h.stopped = true
}
func TestRunApp_CLIValidationError(t *testing.T) {
code := runApp(
func() *config.CLIConfig { return &config.CLIConfig{LogLevel: "INVALID"} },
func() (*config.EnvConfig, error) { return &config.EnvConfig{}, nil },
func() error { return nil },
func(string, []config.OutputTarget, *config.EnvConfig) (workerService, error) {
return &fakeWorker{done: make(chan struct{})}, nil
},
func(workerService, string) healthService { return &fakeHealthMonitor{} },
func(chan<- os.Signal, ...os.Signal) {},
)
if code != 1 {
t.Fatalf("expected exit code 1 for invalid CLI config, got %d", code)
}
}
func TestRunApp_ApplyCLIError(t *testing.T) {
code := runApp(
func() *config.CLIConfig { return &config.CLIConfig{OutputsJSON: "{"} },
func() (*config.EnvConfig, error) { return &config.EnvConfig{}, nil },
func() error { return nil },
func(string, []config.OutputTarget, *config.EnvConfig) (workerService, error) {
return &fakeWorker{done: make(chan struct{})}, nil
},
func(workerService, string) healthService { return &fakeHealthMonitor{} },
func(chan<- os.Signal, ...os.Signal) {},
)
if code != 1 {
t.Fatalf("expected exit code 1 for invalid outputs JSON, got %d", code)
}
}
func TestRunApp_DefaultOutputsAndShutdownFlow(t *testing.T) {
worker := &fakeWorker{done: make(chan struct{})}
health := &fakeHealthMonitor{}
var capturedTargets []config.OutputTarget
var capturedInput string
code := runApp(
func() *config.CLIConfig { return &config.CLIConfig{} },
func() (*config.EnvConfig, error) { return nil, os.ErrNotExist },
func() error { return nil },
func(input string, targets []config.OutputTarget, _ *config.EnvConfig) (workerService, error) {
capturedInput = input
capturedTargets = targets
return worker, nil
},
func(_ workerService, _ string) healthService { return health },
func(ch chan<- os.Signal, _ ...os.Signal) {
go func() { ch <- syscall.SIGTERM }()
},
)
if code != 0 {
t.Fatalf("expected exit code 0, got %d", code)
}
if !worker.started || !worker.stopped {
t.Fatalf("expected worker start/stop to be called, started=%v stopped=%v", worker.started, worker.stopped)
}
if !health.started || !health.stopped {
t.Fatalf("expected health monitor start/stop to be called, started=%v stopped=%v", health.started, health.stopped)
}
if capturedInput == "" {
t.Fatal("expected input directory to be set by defaults")
}
if len(capturedTargets) != 1 || capturedTargets[0].Type != "filesystem" || capturedTargets[0].Path != "./output" {
t.Fatalf("expected default filesystem output target, got %+v", capturedTargets)
}
}
func TestRunApp_UsesConfiguredOutputsWithoutDefaultFallback(t *testing.T) {
worker := &fakeWorker{done: make(chan struct{})}
health := &fakeHealthMonitor{}
configured := &config.EnvConfig{}
configured.SetDefaults()
configured.Output = []config.OutputTarget{{Type: "filesystem", Path: "/tmp/custom"}}
var capturedTargets []config.OutputTarget
code := runApp(
func() *config.CLIConfig { return &config.CLIConfig{} },
func() (*config.EnvConfig, error) { return configured, nil },
func() error { return nil },
func(_ string, targets []config.OutputTarget, _ *config.EnvConfig) (workerService, error) {
capturedTargets = targets
return worker, nil
},
func(_ workerService, _ string) healthService { return health },
func(ch chan<- os.Signal, _ ...os.Signal) {
go func() { ch <- syscall.SIGINT }()
},
)
if code != 0 {
t.Fatalf("expected exit code 0, got %d", code)
}
if len(capturedTargets) != 1 || capturedTargets[0].Path != "/tmp/custom" {
t.Fatalf("expected configured outputs to be preserved, got %+v", capturedTargets)
}
}
func TestRunApp_WorkerCreationFailure(t *testing.T) {
code := runApp(
func() *config.CLIConfig { return &config.CLIConfig{} },
func() (*config.EnvConfig, error) { return &config.EnvConfig{}, nil },
func() error { return nil },
func(_ string, _ []config.OutputTarget, _ *config.EnvConfig) (workerService, error) {
return nil, os.ErrPermission
},
func(_ workerService, _ string) healthService { return &fakeHealthMonitor{} },
func(chan<- os.Signal, ...os.Signal) {},
)
if code != 1 {
t.Fatalf("expected exit code 1 for worker creation failure, got %d", code)
}
}