-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain_server_test.go
More file actions
281 lines (263 loc) · 8.1 KB
/
Copy pathmain_server_test.go
File metadata and controls
281 lines (263 loc) · 8.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
package main
import (
"errors"
"net"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
"genericagent-admin-go/internal/config"
)
func TestRunCleanupWithTimeoutCompletes(t *testing.T) {
called := make(chan struct{})
if ok := runCleanupWithTimeout(func() { close(called) }, time.Second); !ok {
t.Fatal("runCleanupWithTimeout() timed out for completed cleanup")
}
select {
case <-called:
default:
t.Fatal("cleanup was not called")
}
}
func TestRunCleanupWithTimeoutStopsWaiting(t *testing.T) {
blocked := make(chan struct{})
started := make(chan struct{})
start := time.Now()
ok := runCleanupWithTimeout(func() {
close(started)
<-blocked
}, 100*time.Millisecond)
if ok {
close(blocked)
t.Fatal("runCleanupWithTimeout() completed while cleanup was blocked")
}
select {
case <-started:
default:
close(blocked)
t.Fatal("cleanup did not start")
}
if elapsed := time.Since(start); elapsed > time.Second {
close(blocked)
t.Fatalf("runCleanupWithTimeout() returned after %v, want within 1s", elapsed)
}
close(blocked)
}
func TestAppRootExplicitRootTakesPrecedence(t *testing.T) {
explicit := t.TempDir()
got, err := appRoot(explicit)
if err != nil {
t.Fatal(err)
}
want, err := filepath.Abs(explicit)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("appRoot(%q) = %q, want %q", explicit, got, want)
}
}
func portableConfigFixture(t *testing.T) (string, config.AppConfig) {
t.Helper()
cwd := t.TempDir()
gaRoot := filepath.Join(cwd, "GenericAgent")
pythonPath := filepath.Join(gaRoot, ".venv", "bin", "python")
if runtime.GOOS == "windows" {
pythonPath = filepath.Join(gaRoot, ".venv", "Scripts", "python.exe")
}
if err := os.MkdirAll(filepath.Dir(pythonPath), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(pythonPath, []byte("test"), 0o600); err != nil {
t.Fatal(err)
}
cfg := config.Default()
cfg.GARoot = gaRoot
cfg.PythonPath = pythonPath
cfg.EffectivePython = pythonPath
return cwd, cfg
}
func writePortableLayout(t *testing.T, root string) (string, string) {
t.Helper()
bootstrapPy := filepath.Join(root, "bootstrap.py")
pythonExe := filepath.Join(root, "python", "bin", "python3")
if runtime.GOOS == "windows" {
pythonExe = filepath.Join(root, "python", "python.exe")
}
if err := os.MkdirAll(filepath.Dir(pythonExe), 0o755); err != nil {
t.Fatal(err)
}
for _, path := range []string{bootstrapPy, pythonExe} {
if err := os.WriteFile(path, []byte("portable-test"), 0o755); err != nil {
t.Fatal(err)
}
}
return bootstrapPy, pythonExe
}
func TestResolvePortableBootstrapPrefersCanonicalLayout(t *testing.T) {
bundle := t.TempDir()
wantBootstrap, wantPython := writePortableLayout(t, bundle)
writePortableLayout(t, filepath.Join(bundle, "GenericAgent"))
bootstrapPy, pythonExe, ok := resolvePortableBootstrap(bundle)
if !ok {
t.Fatal("resolvePortableBootstrap() ok = false")
}
if bootstrapPy != wantBootstrap || pythonExe != wantPython {
t.Fatalf("resolvePortableBootstrap() = (%q, %q), want (%q, %q)", bootstrapPy, pythonExe, wantBootstrap, wantPython)
}
}
func TestResolvePortableBootstrapSupportsLegacyNestedLayout(t *testing.T) {
bundle := t.TempDir()
wantBootstrap, wantPython := writePortableLayout(t, filepath.Join(bundle, "GenericAgent"))
bootstrapPy, pythonExe, ok := resolvePortableBootstrap(bundle)
if !ok {
t.Fatal("resolvePortableBootstrap() ok = false")
}
if bootstrapPy != wantBootstrap || pythonExe != wantPython {
t.Fatalf("resolvePortableBootstrap() = (%q, %q), want (%q, %q)", bootstrapPy, pythonExe, wantBootstrap, wantPython)
}
}
func TestValidatePortableConfigAcceptsCurrentBundlePaths(t *testing.T) {
cwd, cfg := portableConfigFixture(t)
if err := validatePortableConfig(cwd, cfg); err != nil {
t.Fatalf("validatePortableConfig() error = %v", err)
}
}
func TestValidatePortableConfigRejectsIncompletePaths(t *testing.T) {
cwd, valid := portableConfigFixture(t)
tests := []struct {
name string
cfg config.AppConfig
}{
{name: "empty ga_root", cfg: func() config.AppConfig { cfg := valid; cfg.GARoot = ""; return cfg }()},
{name: "wrong ga_root", cfg: func() config.AppConfig { cfg := valid; cfg.GARoot = t.TempDir(); return cfg }()},
{name: "empty python_path", cfg: func() config.AppConfig { cfg := valid; cfg.PythonPath = ""; cfg.EffectivePython = ""; return cfg }()},
{name: "wrong python_path", cfg: func() config.AppConfig {
cfg := valid
cfg.PythonPath = filepath.Join(t.TempDir(), "python.exe")
cfg.EffectivePython = ""
return cfg
}()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := validatePortableConfig(cwd, tt.cfg); err == nil {
t.Fatal("validatePortableConfig() error = nil, want rejection")
}
})
}
}
func TestValidatePortableConfigRejectsPathsFromExistingOldBundle(t *testing.T) {
_, oldCfg := portableConfigFixture(t)
newCwd, _ := portableConfigFixture(t)
if err := validatePortableConfig(newCwd, oldCfg); err == nil {
t.Fatal("validatePortableConfig() accepted paths from an old bundle location")
}
}
func TestParseInternalLaunchArgs(t *testing.T) {
tests := []struct {
name string
args []string
want internalLaunchOptions
}{
{
name: "public arguments only",
args: []string{"--headless", "--port", "9000"},
want: internalLaunchOptions{PublicArgs: []string{"--headless", "--port", "9000"}},
},
{
name: "helper split form",
args: []string{"--headless", "--update-helper", `C:\\temp\\update.json`, "--no-browser"},
want: internalLaunchOptions{
HelperManifest: `C:\\temp\\update.json`,
PublicArgs: []string{"--headless", "--no-browser"},
},
},
{
name: "confirmation equal form",
args: []string{"--port=9001", "--update-confirm=operation-7"},
want: internalLaunchOptions{
ConfirmOperation: "operation-7",
PublicArgs: []string{"--port=9001"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseInternalLaunchArgs(tt.args)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("parseInternalLaunchArgs(%#v) = %#v, want %#v", tt.args, got, tt.want)
}
})
}
}
func TestParseInternalLaunchArgsRejectsUnsafeForms(t *testing.T) {
for _, args := range [][]string{
{"--update-helper"},
{"--update-helper="},
{"--update-confirm", ""},
{"--update-confirm=operation-1", "--update-confirm", "operation-2"},
{"--update-helper", "manifest.json", "--update-confirm", "operation-1"},
} {
t.Run(strings.Join(args, "_"), func(t *testing.T) {
if _, err := parseInternalLaunchArgs(args); err == nil {
t.Fatalf("parseInternalLaunchArgs(%#v) error = nil, want rejection", args)
}
})
}
}
func TestOpenListenerAndConfirmBindsBeforeConfirmation(t *testing.T) {
var listener net.Listener
var events []string
got, err := openListenerAndConfirm(func() (net.Listener, error) {
var openErr error
listener, openErr = net.Listen("tcp", "127.0.0.1:0")
events = append(events, "bound")
return listener, openErr
}, "operation-1", func(operationID string) error {
if listener == nil {
t.Fatal("confirmation ran before listener bind")
}
if operationID != "operation-1" {
t.Fatalf("confirmation operation = %q", operationID)
}
events = append(events, "confirmed")
return nil
})
if err != nil {
t.Fatal(err)
}
defer got.Close()
if !reflect.DeepEqual(events, []string{"bound", "confirmed"}) {
t.Fatalf("events = %#v", events)
}
}
func TestOpenListenerAndConfirmClosesListenerOnConfirmationFailure(t *testing.T) {
var listener net.Listener
confirmErr := errors.New("confirmation failed")
got, err := openListenerAndConfirm(func() (net.Listener, error) {
var openErr error
listener, openErr = net.Listen("tcp", "127.0.0.1:0")
return listener, openErr
}, "operation-2", func(string) error {
return confirmErr
})
if !errors.Is(err, confirmErr) {
t.Fatalf("openListenerAndConfirm() error = %v, want %v", err, confirmErr)
}
if got != nil {
t.Fatalf("openListenerAndConfirm() listener = %v, want nil", got)
}
if listener == nil {
t.Fatal("listener was never opened")
}
if closeErr := listener.Close(); closeErr == nil {
t.Fatal("listener remained open after confirmation failure")
}
}