Add initial Temporal Workflow and Activity support with Go and Python integrations

This commit is contained in:
longpeng 2025-06-19 23:07:39 +08:00
parent 1dadcddff9
commit b656433faa
13 changed files with 175 additions and 0 deletions

28
Makefile Normal file
View File

@ -0,0 +1,28 @@
.PHONY: all gen_go gen_py clean
all: gen_go gen_py
gen_go:
@echo "Generating Go Protobuf code..."
mkdir -p server/gen/pb
protoc --proto_path=proto \
--go_out=server/gen/pb \
--go_opt=paths=source_relative \
--go-temporal_out=server/gen/pb \
--go-temporal_opt=paths=source_relative \
proto/*.proto
gen_py:
@echo "Generating Python Protobuf code..."
mkdir -p worker/gen
python3 -m grpc_tools.protoc \
--proto_path=proto \
--python_out=worker/gen \
--pyi_out=worker/gen \
proto/*.proto
clean:
@echo "Cleaning generated files..."
rm -rf server/gen
rm -rf worker/gen

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module beacon
go 1.24.4

70
proto/common_test.proto Normal file
View File

@ -0,0 +1,70 @@
syntax = "proto3";
package test_pb;
option go_package = "Beacon/server/gen/pb"; // your_module_path
// Python proto_gen common_test_pb2.py
// Python import proto_gen.common_test_pb2
// -------- --------
//
message TestRunInput {
string run_id = 1; // ID
string environment_url = 2; // URL
repeated string tags = 3; // "smoke", "regression"
bool run_api_tests = 4;
bool run_ui_tests = 5;
}
// API测试请求的参数
message ApiTestRequest {
string test_case_id = 1; // API测试用例ID
string endpoint = 2; // API路径
string http_method = 3; // "GET", "POST", etc.
map<string, string> headers = 4;
bytes request_body = 5; // JSON或其他二进制数据
int32 expected_status_code = 6;
}
// UI测试请求的参数
message UiTestRequest {
string test_case_id = 1; // UI测试用例ID
string url_path = 2; // base_url
string browser_type = 3; // "chromium", "firefox", "webkit"
bool headless = 4; //
map<string, string> user_data = 5; // Worker
}
// -------- --------
//
message BaseTestResult {
string test_case_id = 1;
bool success = 2;
string message = 3; // /
string log_output = 4; //
double duration_seconds = 5;
string error_details = 6; //
}
// API测试结果
message ApiTestResult {
BaseTestResult base_result = 1;
int32 actual_status_code = 2;
string response_body = 3;
}
// UI测试结果
message UiTestResult {
BaseTestResult base_result = 1;
string screenshot_url = 2; // URL (worker上传后返回)
string html_report_url = 3; // HTML报告URL
}
//
message TestRunOutput {
string run_id = 1;
bool overall_success = 2;
string completion_message = 3;
repeated ApiTestResult api_results = 4;
repeated UiTestResult ui_results = 5;
}

3
server/activity.go Normal file
View File

@ -0,0 +1,3 @@
package main
// 定义 Temporal Activity 接口

61
server/main.go Normal file
View File

@ -0,0 +1,61 @@
package main
// Go 服务端入口,触发 Workflow
import (
"context"
"fmt"
"log"
"time"
"server/gen/pb" // 替换为你的模块路径
"server/workflow"
"github.com/google/uuid"
"go.temporal.io/sdk/client"
)
func main() {
// 创建 Temporal 客户端
c, err := client.Dial(client.Options{
HostPort: client.Default : TemporalClientPort, // 根据你的 Temporal Server 配置
})
if err != nil {
log.Fatalf("Unable to create Temporal client: %v", err)
}
defer c.Close()
// 模拟一个触发测试的事件 (例如来自 Web UI 或 CI/CD)
runID := uuid.New().String()
testInput := &pb.TestRunInput{
RunId: runID,
EnvironmentUrl: "https://example.com",
Tags: []string{"smoke", "critical"},
RunApiTests: true,
RunUiTests: true,
}
workflowOptions := client.StartWorkflowOptions{
ID: "test_workflow_" + runID,
TaskQueue: "test-task-queue", // 保持与 Python Worker 一致
}
fmt.Printf("Starting TestRunWorkflow for run ID: %s\n", runID)
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, workflow.TestRunWorkflow, testInput)
if err != nil {
log.Fatalf("Unable to execute workflow: %v", err)
}
fmt.Printf("Workflow started. Workflow ID: %s, Run ID: %s\n", we.GetID(), we.GetRunID())
// 等待 Workflow 完成并获取结果
var result pb.TestRunOutput
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalf("Unable to get workflow result: %v", err)
}
fmt.Printf("Workflow finished. Overall Success: %t, Message: %s\n", result.OverallSuccess, result.CompletionMessage)
fmt.Printf("API Test Results: %+v\n", result.ApiResults)
fmt.Printf("UI Test Results: %+v\n", result.UiResults)
// 后续可以根据 result 生成报告、发送通知等
}

3
server/workflow.go Normal file
View File

@ -0,0 +1,3 @@
package main
// 定义 Temporal Workflow

1
worker/activities.py Normal file
View File

@ -0,0 +1 @@
# 实现 Temporal Activity 逻辑

1
worker/api_tests.py Normal file
View File

@ -0,0 +1 @@
# 接口测试具体实现

1
worker/gen/__init__.py Normal file
View File

@ -0,0 +1 @@
# Protobuf 生成的 Python 代码

1
worker/main.py Normal file
View File

@ -0,0 +1 @@
# Python Worker 入口,注册并运行 Activity

1
worker/requirements.txt Normal file
View File

@ -0,0 +1 @@
# Python 依赖

1
worker/ui_tests.py Normal file
View File

@ -0,0 +1 @@
# UI 测试具体实现 (使用 Playwright)

1
worker/utils.py Normal file
View File

@ -0,0 +1 @@
# 辅助函数 (例如截图、报告生成)