feat(exec): 本地执行 Provider + 放权终端沙箱(白名单/工作目录/超时/输出截断/默认关)+ 文件沙箱;config 增 ExecSandbox(默认关)
This commit is contained in:
1 parent
88dc12466d
commit
430fc4666b
11 files changed
+1199
-20
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// mockProvider is a configurable Provider used to prove the interface can be
|
||||
// mocked. It mocks the interface, not a concrete type, per the test
|
||||
// conventions.
|
||||
type mockProvider struct {
|
||||
execute func(ctx context.Context, req ExecRequest) (*ExecResult, error)
|
||||
}
|
||||
|
||||
func (m mockProvider) Execute(ctx context.Context, req ExecRequest) (*ExecResult, error) {
|
||||
if m.execute == nil {
|
||||
return &ExecResult{}, nil
|
||||
}
|
||||
return m.execute(ctx, req)
|
||||
}
|
||||
|
||||
// TestProvider_InterfaceConformance is a compile-time assertion that both the
|
||||
// mock and the real executor satisfy Provider.
|
||||
func TestProvider_InterfaceConformance(t *testing.T) {
|
||||
t.Parallel()
|
||||
var _ Provider = mockProvider{}
|
||||
var _ Provider = (*TerminalExecutor)(nil)
|
||||
}
|
||||
|
||||
// TestProvider_MockRoundTrip exercises the interface through a mock without any
|
||||
// real process.
|
||||
func TestProvider_MockRoundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
var got ExecRequest
|
||||
p := mockProvider{execute: func(ctx context.Context, req ExecRequest) (*ExecResult, error) {
|
||||
got = req
|
||||
return &ExecResult{Stdout: "ok"}, nil
|
||||
}}
|
||||
|
||||
res, err := p.Execute(context.Background(), ExecRequest{Command: []string{"echo", "hi"}})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute error: %v", err)
|
||||
}
|
||||
if res.Stdout != "ok" {
|
||||
t.Errorf("Stdout = %q, want ok", res.Stdout)
|
||||
}
|
||||
if len(got.Command) != 2 || got.Command[0] != "echo" {
|
||||
t.Errorf("got.Command = %v, want [echo hi]", got.Command)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user