MCPcopy Create free account
hub / github.com/astercloud/aster / testAliyun

Function testAliyun

examples/cloud-sandbox/main.go:33–103  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

31}
32
33func testAliyun(ctx context.Context) {
34 fmt.Println("=== Testing Aliyun AgentBay Sandbox ===")
35
36 // 创建阿里云沙箱
37 config := &cloud.AliyunConfig{
38 MCPEndpoint: os.Getenv("ALIYUN_MCP_ENDPOINT"),
39 AccessKeyID: os.Getenv("ALIYUN_ACCESS_KEY_ID"),
40 AccessKeySecret: os.Getenv("ALIYUN_ACCESS_KEY_SECRET"),
41 SecurityToken: os.Getenv("ALIYUN_SECURITY_TOKEN"),
42 Region: "cn-hangzhou",
43 WorkDir: "/workspace",
44 }
45
46 sb, err := cloud.NewAliyunSandbox(config)
47 if err != nil {
48 log.Fatalf("Failed to create Aliyun sandbox: %v", err)
49 }
50 defer func() { _ = sb.Dispose() }()
51
52 fmt.Printf("Sandbox created: %s\n\n", sb.Kind())
53
54 // 测试 1: 执行命令
55 fmt.Println("--- Test 1: Execute command ---")
56 result, err := sb.Exec(ctx, "echo 'Hello from Aliyun Sandbox'", nil)
57 if err != nil {
58 log.Fatalf("Exec failed: %v", err)
59 }
60 fmt.Printf("Stdout: %s\n", result.Stdout)
61 if result.Stderr != "" {
62 fmt.Printf("Stderr: %s\n", result.Stderr)
63 }
64 fmt.Printf("Exit Code: %d\n\n", result.Code)
65
66 // 测试 2: 写入文件
67 fmt.Println("--- Test 2: Write file ---")
68 fs := sb.FS()
69 err = fs.Write(ctx, "test.txt", "Hello World from Aliyun")
70 if err != nil {
71 log.Fatalf("Write failed: %v", err)
72 }
73 fmt.Println("File written successfully")
74
75 // 测试 3: 读取文件
76 fmt.Println("--- Test 3: Read file ---")
77 content, err := fs.Read(ctx, "test.txt")
78 if err != nil {
79 log.Fatalf("Read failed: %v", err)
80 }
81 fmt.Printf("Content: %s\n\n", content)
82
83 // 测试 4: 获取文件信息
84 fmt.Println("--- Test 4: File info ---")
85 info, err := fs.Stat(ctx, "test.txt")
86 if err != nil {
87 log.Fatalf("Stat failed: %v", err)
88 }
89 fmt.Printf("Path: %s\n", info.Path)
90 fmt.Printf("Size: %d bytes\n", info.Size)

Callers 1

mainFunction · 0.85

Calls 10

DisposeMethod · 0.95
KindMethod · 0.95
ExecMethod · 0.95
FSMethod · 0.95
NewAliyunSandboxFunction · 0.92
PrintfMethod · 0.80
WriteMethod · 0.65
ReadMethod · 0.65
StatMethod · 0.65
GlobMethod · 0.65

Tested by

no test coverage detected