在 spark_submit handler 中增加 server-minted 校验:script_path 必须通过
本服务器 upload_file 的 UploadStore.Validate,否则立即返回清晰错误,避免
远程 agent 把自身本地路径转发给服务器侧 spark-submit 时出现难以定位的
FileNotFoundException。
校验对 nil UploadStore 是安全的:当 d.UploadStore 为 nil 时直接跳过,保留
不强制依赖 store 的测试或降级场景。当前 testDepsWithDataDir 始终会注入
UploadStore,所以测试走的是真实校验路径。
同步更新了 script_path 的工具描述,明确声明非 upload_file 铸造的路径会被
拒绝。这是对 7920dc9 中“path 永远在最后”规则的收紧——现在不仅位置固定,
而且必须是本机 uploads 目录下的有效上传文件。
测试调整:
- 新增 TestSparkSubmit_RejectsNonMintedPath:使用 t.TempDir() 下未通过
upload_file 写入的文件作为 script_path,断言返回错误并包含
"not from upload_file"。
- TestSparkSubmit_MissingRequiredField / TestSparkSubmit_BadSparkConfValue
原来使用 "/tmp/script.py" 作为占位路径,现在会先通过 UploadStore.Save
生成一个铸造路径,确保它们继续分别验证 master 缺失和 spark_conf 值类型
错误,而不是被新的路径守卫拦截。
Co-Authored-By: Claude <noreply@anthropic.com>
199 lines
5.3 KiB
Go
199 lines
5.3 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
|
|
"spark-mcp-go/internal/cluster"
|
|
)
|
|
|
|
func TestSparkSubmit_StructuredCommand(t *testing.T) {
|
|
deps, repo := testDepsWithDataDir(t)
|
|
deps.SparkSubmitTimeout = 5 * time.Second
|
|
store := deps.UploadStore
|
|
|
|
fileID, _, _, _, absPath, err := store.Save([]byte("print('hi')\n"), "hello.py")
|
|
if err != nil {
|
|
t.Fatalf("save upload: %v", err)
|
|
}
|
|
|
|
echoed := filepath.Join(t.TempDir(), "echoed")
|
|
echoScript := filepath.Join(t.TempDir(), "echo-args.sh")
|
|
if err := os.WriteFile(echoScript, []byte("#!/bin/sh\nfor arg do\n echo \"$arg\" >> \"$ECHO_FILE\"\ndone\n"), 0o755); err != nil {
|
|
t.Fatalf("write echo script: %v", err)
|
|
}
|
|
|
|
t.Setenv("ECHO_FILE", echoed)
|
|
|
|
createCluster(t, repo, &cluster.Cluster{
|
|
ID: "cluster-echo",
|
|
Name: "Echo",
|
|
IsActive: true,
|
|
AuthType: cluster.AuthNone,
|
|
SparkSubmitExecuteBin: echoScript,
|
|
})
|
|
|
|
req := newToolRequest(SparkSubmitName, map[string]any{
|
|
"cluster_id": "cluster-echo",
|
|
"master": "yarn",
|
|
"deploy_mode": "cluster",
|
|
"script_path": absPath,
|
|
"queue": "default",
|
|
"executor_memory": "2G",
|
|
"executor_cores": 1,
|
|
"num_executors": 4,
|
|
"spark_conf": map[string]any{"a": "1", "b": "2"},
|
|
"extra_args": map[string]any{"name": "wordcount-job"},
|
|
})
|
|
res, err := deps.SparkSubmitHandler(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("handler error: %v", err)
|
|
}
|
|
if res.IsError {
|
|
t.Fatalf("unexpected error result: %v", res.Content)
|
|
}
|
|
|
|
got, err := os.ReadFile(echoed)
|
|
if err != nil {
|
|
t.Fatalf("read echoed args: %v", err)
|
|
}
|
|
lines := strings.Split(strings.TrimSpace(string(got)), "\n")
|
|
|
|
want := []string{
|
|
echoScript,
|
|
"--master", "yarn",
|
|
"--queue", "default",
|
|
"--executor-memory", "2G",
|
|
"--executor-cores", "1",
|
|
"--num-executors", "4",
|
|
"--conf", "a=1",
|
|
"--conf", "b=2",
|
|
"--name", "wordcount-job",
|
|
absPath,
|
|
}
|
|
if len(lines) != len(want) {
|
|
t.Fatalf("lines=%v\nwant=%v", lines, want)
|
|
}
|
|
for i, l := range lines {
|
|
if l != want[i] {
|
|
t.Errorf("line[%d]=%q, want %q", i, l, want[i])
|
|
}
|
|
}
|
|
|
|
if lines[len(lines)-1] != absPath {
|
|
t.Errorf("last line=%q, want script_path %q", lines[len(lines)-1], absPath)
|
|
}
|
|
|
|
if !strings.HasSuffix(absPath, "/"+fileID) {
|
|
t.Errorf("absPath=%q does not end with fileID %q", absPath, fileID)
|
|
}
|
|
}
|
|
|
|
func TestSparkSubmit_MissingRequiredField(t *testing.T) {
|
|
deps, _ := testDepsWithDataDir(t)
|
|
store := deps.UploadStore
|
|
_, _, _, _, mintedPath, err := store.Save([]byte("# dummy\n"), "dummy.py")
|
|
if err != nil {
|
|
t.Fatalf("save upload: %v", err)
|
|
}
|
|
|
|
req := newToolRequest(SparkSubmitName, map[string]any{
|
|
"cluster_id": "cluster-echo",
|
|
"deploy_mode": "cluster",
|
|
"script_path": mintedPath,
|
|
"queue": "default",
|
|
"executor_memory": "2G",
|
|
"executor_cores": 1,
|
|
"num_executors": 4,
|
|
})
|
|
res, err := deps.SparkSubmitHandler(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("handler error: %v", err)
|
|
}
|
|
if !res.IsError {
|
|
t.Fatalf("expected error result, got: %v", res.Content)
|
|
}
|
|
text, ok := mcp.AsTextContent(res.Content[0])
|
|
if !ok {
|
|
t.Fatalf("content is not text: %T", res.Content[0])
|
|
}
|
|
if !strings.Contains(text.Text, "master") {
|
|
t.Errorf("error text=%q, want mention of master", text.Text)
|
|
}
|
|
}
|
|
|
|
func TestSparkSubmit_BadSparkConfValue(t *testing.T) {
|
|
deps, _ := testDepsWithDataDir(t)
|
|
store := deps.UploadStore
|
|
_, _, _, _, mintedPath, err := store.Save([]byte("# dummy\n"), "dummy.py")
|
|
if err != nil {
|
|
t.Fatalf("save upload: %v", err)
|
|
}
|
|
|
|
req := newToolRequest(SparkSubmitName, map[string]any{
|
|
"cluster_id": "cluster-echo",
|
|
"master": "yarn",
|
|
"deploy_mode": "cluster",
|
|
"script_path": mintedPath,
|
|
"queue": "default",
|
|
"executor_memory": "2G",
|
|
"executor_cores": 1,
|
|
"num_executors": 4,
|
|
"spark_conf": map[string]any{"a": 1},
|
|
})
|
|
res, err := deps.SparkSubmitHandler(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("handler error: %v", err)
|
|
}
|
|
if !res.IsError {
|
|
t.Fatalf("expected error result, got: %v", res.Content)
|
|
}
|
|
text, ok := mcp.AsTextContent(res.Content[0])
|
|
if !ok {
|
|
t.Fatalf("content is not text: %T", res.Content[0])
|
|
}
|
|
if !strings.Contains(text.Text, "spark_conf") {
|
|
t.Errorf("error text=%q, want mention of spark_conf", text.Text)
|
|
}
|
|
}
|
|
|
|
func TestSparkSubmit_RejectsNonMintedPath(t *testing.T) {
|
|
deps, _ := testDepsWithDataDir(t)
|
|
|
|
unminted := filepath.Join(t.TempDir(), "unminted.py")
|
|
if err := os.WriteFile(unminted, []byte("print('not from upload_file')\n"), 0o644); err != nil {
|
|
t.Fatalf("write unminted file: %v", err)
|
|
}
|
|
|
|
req := newToolRequest(SparkSubmitName, map[string]any{
|
|
"cluster_id": "cluster-echo",
|
|
"master": "yarn",
|
|
"deploy_mode": "cluster",
|
|
"script_path": unminted,
|
|
"queue": "default",
|
|
"executor_memory": "2G",
|
|
"executor_cores": 1,
|
|
"num_executors": 4,
|
|
})
|
|
res, err := deps.SparkSubmitHandler(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("handler error: %v", err)
|
|
}
|
|
if !res.IsError {
|
|
t.Fatalf("expected error result, got: %v", res.Content)
|
|
}
|
|
text, ok := mcp.AsTextContent(res.Content[0])
|
|
if !ok {
|
|
t.Fatalf("content is not text: %T", res.Content[0])
|
|
}
|
|
if !strings.Contains(text.Text, "not from upload_file") {
|
|
t.Errorf("error text=%q, want mention of not from upload_file", text.Text)
|
|
}
|
|
}
|