-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
636 lines (550 loc) · 17.7 KB
/
server_test.go
File metadata and controls
636 lines (550 loc) · 17.7 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
package ts
import (
"context"
"errors"
"fmt"
"testing"
io "dappco.re/go/core/io"
"dappco.re/go/core/io/store"
"dappco.re/go/core/scm/manifest"
pb "dappco.re/go/core/ts/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// mockProcessRunner implements ProcessRunner for testing.
type mockProcessRunner struct {
started map[string]bool
nextID int
startErr error
killErr error
}
func newMockProcessRunner() *mockProcessRunner {
return &mockProcessRunner{started: make(map[string]bool)}
}
func (m *mockProcessRunner) Start(_ context.Context, command string, args ...string) (ProcessHandle, error) {
if m.startErr != nil {
return nil, m.startErr
}
if m.started == nil {
m.started = make(map[string]bool)
}
m.nextID++
id := fmt.Sprintf("proc-%d", m.nextID)
m.started[id] = true
return &mockProcessHandle{id: id}, nil
}
func (m *mockProcessRunner) Kill(id string) error {
if m.killErr != nil {
return m.killErr
}
if !m.started[id] {
return fmt.Errorf("process not found: %s", id)
}
delete(m.started, id)
return nil
}
type mockProcessHandle struct{ id string }
func (h *mockProcessHandle) Info() ProcessInfo { return ProcessInfo{ID: h.id} }
func newTestServer(t *testing.T) *Server {
t.Helper()
medium := io.NewMockMedium()
medium.Files["./data/test.txt"] = "hello"
st, err := store.New(":memory:")
require.NoError(t, err)
t.Cleanup(func() { st.Close() })
srv := NewServer(medium, st)
srv.RegisterModule(&manifest.Manifest{
Code: "test-mod",
Permissions: manifest.Permissions{
Read: []string{"./data/"},
Write: []string{"./data/"},
},
})
return srv
}
func TestFileRead_Good(t *testing.T) {
srv := newTestServer(t)
resp, err := srv.FileRead(context.Background(), &pb.FileReadRequest{
Path: "./data/test.txt", ModuleCode: "test-mod",
})
require.NoError(t, err)
assert.Equal(t, "hello", resp.Content)
}
func TestPing_Good(t *testing.T) {
srv := newTestServer(t)
resp, err := srv.Ping(context.Background(), &pb.PingRequest{})
require.NoError(t, err)
assert.True(t, resp.Ok)
}
func TestLocaleGet_Good(t *testing.T) {
medium := io.NewMockMedium()
medium.Files[".core/locales/en.json"] = `{"hello":"world"}`
st, err := store.New(":memory:")
require.NoError(t, err)
t.Cleanup(func() { st.Close() })
srv := NewServer(medium, st)
resp, err := srv.LocaleGet(context.Background(), &pb.LocaleGetRequest{Locale: "en"})
require.NoError(t, err)
assert.True(t, resp.Found)
assert.Equal(t, `{"hello":"world"}`, resp.Content)
}
func TestFileRead_Bad_PermissionDenied(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileRead(context.Background(), &pb.FileReadRequest{
Path: "./secrets/key.pem", ModuleCode: "test-mod",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestFileRead_Bad_UnknownModule(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileRead(context.Background(), &pb.FileReadRequest{
Path: "./data/test.txt", ModuleCode: "unknown",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "unknown module")
}
func TestFileWrite_Good(t *testing.T) {
srv := newTestServer(t)
resp, err := srv.FileWrite(context.Background(), &pb.FileWriteRequest{
Path: "./data/new.txt", Content: "world", ModuleCode: "test-mod",
})
require.NoError(t, err)
assert.True(t, resp.Ok)
}
func TestFileWrite_Bad_PermissionDenied(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileWrite(context.Background(), &pb.FileWriteRequest{
Path: "./secrets/bad.txt", Content: "nope", ModuleCode: "test-mod",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestFileList_Good(t *testing.T) {
medium := io.NewMockMedium()
medium.Files["./data/a.txt"] = "hello"
medium.Files["./data/sub/b.txt"] = "world"
st, err := store.New(":memory:")
require.NoError(t, err)
t.Cleanup(func() { st.Close() })
srv := NewServer(medium, st)
srv.RegisterModule(&manifest.Manifest{
Code: "list-mod",
Permissions: manifest.Permissions{
Read: []string{"./data/"},
},
})
resp, err := srv.FileList(context.Background(), &pb.FileListRequest{
Path: "./data",
ModuleCode: "list-mod",
})
require.NoError(t, err)
require.Len(t, resp.Entries, 2)
assert.Equal(t, "a.txt", resp.Entries[0].Name)
assert.False(t, resp.Entries[0].IsDir)
assert.EqualValues(t, len("hello"), resp.Entries[0].Size)
assert.Equal(t, "sub", resp.Entries[1].Name)
assert.True(t, resp.Entries[1].IsDir)
}
func TestFileList_Bad_PermissionDenied(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileList(context.Background(), &pb.FileListRequest{
Path: "./secrets",
ModuleCode: "test-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestFileList_Ugly_UnknownModule(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileList(context.Background(), &pb.FileListRequest{
Path: "./data",
ModuleCode: "missing",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown module")
}
func TestFileDelete_Good(t *testing.T) {
srv := newTestServer(t)
require.NotNil(t, srv)
medium := srv.medium.(*io.MockMedium)
medium.Files["./data/delete-me.txt"] = "bye"
resp, err := srv.FileDelete(context.Background(), &pb.FileDeleteRequest{
Path: "./data/delete-me.txt",
ModuleCode: "test-mod",
})
require.NoError(t, err)
assert.True(t, resp.Ok)
_, err = srv.FileRead(context.Background(), &pb.FileReadRequest{
Path: "./data/delete-me.txt",
ModuleCode: "test-mod",
})
assert.Error(t, err)
}
func TestFileDelete_Bad_PermissionDenied(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileDelete(context.Background(), &pb.FileDeleteRequest{
Path: "./secrets/delete-me.txt",
ModuleCode: "test-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestFileDelete_Ugly_UnknownModule(t *testing.T) {
srv := newTestServer(t)
_, err := srv.FileDelete(context.Background(), &pb.FileDeleteRequest{
Path: "./data/delete-me.txt",
ModuleCode: "missing",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown module")
}
func TestStoreGetSet_Good(t *testing.T) {
srv := newTestServer(t)
ctx := context.Background()
_, err := srv.StoreSet(ctx, &pb.StoreSetRequest{Group: "cfg", Key: "theme", Value: "dark"})
require.NoError(t, err)
resp, err := srv.StoreGet(ctx, &pb.StoreGetRequest{Group: "cfg", Key: "theme"})
require.NoError(t, err)
assert.True(t, resp.Found)
assert.Equal(t, "dark", resp.Value)
}
func TestStoreGetSet_Good_ModuleNamespace(t *testing.T) {
srv := newTestServer(t)
ctx := context.Background()
_, err := srv.StoreSet(ctx, &pb.StoreSetRequest{
Group: "shared",
Key: "theme",
Value: "dark",
ModuleCode: "test-mod",
})
require.NoError(t, err)
resp, err := srv.StoreGet(ctx, &pb.StoreGetRequest{
Group: "shared",
Key: "theme",
ModuleCode: "test-mod",
})
require.NoError(t, err)
assert.True(t, resp.Found)
assert.Equal(t, "dark", resp.Value)
}
func TestStoreGetSet_Bad_ReservedGroup(t *testing.T) {
srv := newTestServer(t)
ctx := context.Background()
_, err := srv.StoreSet(ctx, &pb.StoreSetRequest{
Group: "_coredeno",
Key: "theme",
Value: "dark",
ModuleCode: "test-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "reserved store group")
}
func TestStoreGet_Good_NotFound(t *testing.T) {
srv := newTestServer(t)
resp, err := srv.StoreGet(context.Background(), &pb.StoreGetRequest{Group: "cfg", Key: "missing"})
require.NoError(t, err)
assert.False(t, resp.Found)
}
func newTestServerWithProcess(t *testing.T) (*Server, *mockProcessRunner) {
t.Helper()
srv := newTestServer(t)
srv.RegisterModule(&manifest.Manifest{
Code: "runner-mod",
Permissions: manifest.Permissions{
Run: []string{"echo", "ls"},
},
})
pr := newMockProcessRunner()
srv.SetProcessRunner(pr)
return srv, pr
}
func TestProcessStart_Good(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
resp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo", Args: []string{"hello"}, ModuleCode: "runner-mod",
})
require.NoError(t, err)
assert.NotEmpty(t, resp.ProcessId)
}
func TestProcessStart_Bad_PermissionDenied(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "rm", Args: []string{"-rf", "/"}, ModuleCode: "runner-mod",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestProcessStart_Bad_NoProcessService(t *testing.T) {
srv := newTestServer(t)
srv.RegisterModule(&manifest.Manifest{
Code: "no-proc-mod",
Permissions: manifest.Permissions{Run: []string{"echo"}},
})
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo", ModuleCode: "no-proc-mod",
})
assert.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.Unimplemented, st.Code())
}
func TestProcessStart_Bad_MissingIdentity(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "module code required")
}
func TestProcessStart_Bad_BlankCommand(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: " ",
ModuleCode: "runner-mod",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}
func TestProcessStop_Good(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
// Start a process first
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo", ModuleCode: "runner-mod",
})
require.NoError(t, err)
// Stop it
resp, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "runner-mod",
})
require.NoError(t, err)
assert.True(t, resp.Ok)
}
func TestProcessStop_Bad_RequiresModuleCode(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: "nonexistent",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "module code required")
}
func TestProcessStop_Bad_BlankProcessID(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: " ",
ModuleCode: "runner-mod",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}
func TestProcessStop_Bad_OwnershipMismatch(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo", ModuleCode: "runner-mod",
})
require.NoError(t, err)
_, err = srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "other-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot stop")
}
func TestProcessStop_Bad_NotFound(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: "nonexistent",
ModuleCode: "runner-mod",
})
assert.Error(t, err)
}
func TestProcessStop_Bad_MissingModuleCode(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: "nonexistent",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "module code required")
}
func TestProcessStop_Bad_NoOwnerMapping(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: "missing",
ModuleCode: "runner-mod",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestProcessStop_Bad_OtherModule(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
srv.RegisterModule(&manifest.Manifest{
Code: "other-mod",
Permissions: manifest.Permissions{Run: []string{"echo"}},
})
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo", ModuleCode: "runner-mod",
})
require.NoError(t, err)
_, err = srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "other-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestUnregisterModule_Good_ClearsProcessOwnership(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo", ModuleCode: "runner-mod",
})
require.NoError(t, err)
require.NotEmpty(t, startResp.ProcessId)
srv.UnregisterModule("runner-mod")
srv.mu.RLock()
_, stillTracked := srv.processOwners[startResp.ProcessId]
srv.mu.RUnlock()
assert.False(t, stillTracked, "process ownership should be removed when the module is unregistered")
_, err = srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "runner-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestServer_RegisterModule_Ugly_NilAndBlankIgnored(t *testing.T) {
srv := newTestServer(t)
before := len(srv.manifests)
srv.RegisterModule(nil)
srv.RegisterModule(&manifest.Manifest{Code: " "})
srv.mu.RLock()
defer srv.mu.RUnlock()
assert.Len(t, srv.manifests, before)
}
func TestServer_UnregisterModule_Ugly_BlankNoop(t *testing.T) {
srv := newTestServer(t)
srv.RegisterModule(&manifest.Manifest{Code: "keep-me"})
srv.UnregisterModule(" ")
srv.mu.RLock()
defer srv.mu.RUnlock()
_, ok := srv.manifests["keep-me"]
assert.True(t, ok)
}
func TestServer_ProcessStart_Bad_ProcessServiceUnavailable(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
srv.SetProcessRunner(&mockProcessRunner{startErr: errProcessUnavailable})
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
ModuleCode: "runner-mod",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.Unimplemented, st.Code())
}
func TestServer_ProcessStart_Bad_StartErrorWrapped(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
srv.SetProcessRunner(&mockProcessRunner{startErr: errors.New("start failed")})
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
ModuleCode: "runner-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "process start")
assert.Contains(t, err.Error(), "start failed")
}
func TestServer_ProcessStop_Bad_KillErrorWrapped(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
runner := &mockProcessRunner{killErr: errors.New("kill failed")}
srv.SetProcessRunner(runner)
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
ModuleCode: "runner-mod",
})
require.NoError(t, err)
_, err = srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "runner-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "process stop")
assert.Contains(t, err.Error(), "kill failed")
}
func TestServer_ProcessStart_Bad_EmptyCommand(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: " ",
ModuleCode: "runner-mod",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
assert.Contains(t, st.Message(), "process command required")
}
func TestServer_ProcessStart_Ugly_MissingModuleCode(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
_, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.PermissionDenied, st.Code())
assert.Contains(t, st.Message(), "module code required")
}
func TestServer_ProcessStop_Good_OwnershipCleared(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
ModuleCode: "runner-mod",
})
require.NoError(t, err)
require.NotEmpty(t, startResp.ProcessId)
stopResp, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "runner-mod",
})
require.NoError(t, err)
assert.True(t, stopResp.Ok)
_, err = srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "runner-mod",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "permission denied")
}
func TestServer_ProcessStop_Bad_OwnershipDenied(t *testing.T) {
srv, _ := newTestServerWithProcess(t)
startResp, err := srv.ProcessStart(context.Background(), &pb.ProcessStartRequest{
Command: "echo",
ModuleCode: "runner-mod",
})
require.NoError(t, err)
_, err = srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: startResp.ProcessId,
ModuleCode: "other-mod",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.PermissionDenied, st.Code())
assert.Contains(t, st.Message(), "cannot stop")
}
func TestServer_ProcessStop_Ugly_NoRunner(t *testing.T) {
srv := newTestServer(t)
_, err := srv.ProcessStop(context.Background(), &pb.ProcessStopRequest{
ProcessId: "proc-1",
ModuleCode: "test-mod",
})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.Unimplemented, st.Code())
}