-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_etcd_single.sh
More file actions
executable file
·156 lines (118 loc) · 3.16 KB
/
test_etcd_single.sh
File metadata and controls
executable file
·156 lines (118 loc) · 3.16 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
#!/bin/bash
# Phase 2 测试脚本:启动单节点集群并测试 etcd 兼容性
set -e
# Disable proxy to avoid interference with local connections
unset http_proxy
unset https_proxy
unset all_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
unset ALL_PROXY
echo "===== Phase 2 测试:Raft + etcd 兼容层 ====="
echo""
pre_dir=$(pwd)
l=$(pkill metastore >/dev/null 2>&1 || true)
# 清理
rm -rf raft-single-test
mkdir -p raft-cluster-test/node1
cd $pre_dir/../
# 编译
echo "1. 编译程序..."
make build
echo "✅ 编译成功"
echo ""
cd $pre_dir
cp $pre_dir/../metaStore raft-cluster-test/
cd raft-cluster-test
mkdir -p data/pebble/1
# 启动单节点
echo "2. 启动单节点集群(后台)..."
./metastore \
--member-id=1 \
--cluster=http://127.0.0.1:9021 \
--port=9121 \
--grpc-addr=:12379 \
--storage=pebble \
> node1/log.txt 2>&1 &
PID=$!
echo "节点 1 已启动 (PID: $PID)"
sleep 10
# 检查进程是否还在运行
if ! ps -p $PID > /dev/null; then
echo "❌ 节点启动失败,查看日志:"
cat node1/log.txt
exit 1
fi
echo "✅ 节点运行中"
echo "============================================"
echo "============================================"
# 测试 etcd 兼容性
echo "3. 测试 etcdctl..."
export ETCDCTL_API=3
export ETCDCTL_ENDPOINTS="localhost:12379"
chmod a+x $pre_dir/../tools/etcdctl
echo "测试 Cluster 服务..."
$pre_dir/../tools/etcdctl member list --write-out=table
echo ""
echo "测试 KV 操作..."
$pre_dir/../tools/etcdctl put cluster-key-2025 2025
$pre_dir/../tools/etcdctl get cluster-key-2025
$pre_dir/../tools/etcdctl put cluster-key-20251 2025
$pre_dir/../tools/etcdctl get cluster-key-20251
$pre_dir/../tools/etcdctl get cluster --prefix
$pre_dir/../tools/etcdctl del cluster-key-20251
$pre_dir/../tools/etcdctl get cluster --prefix
echo "✅ KV 操作测试通过"
echo "3. 测试 etcd clientv3..."
echo "--------------------------->>>>>>>>>>>>>>>>>>>>>>>"
# 创建简单测试程序
cat > test_client.go << 'GOEOF'
package main
import (
"context"
"fmt"
"log"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
)
func main() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:12379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
log.Fatal(err)
}
defer cli.Close()
ctx := context.Background()
// Test Put
fmt.Println("Testing Put...")
_, err = cli.Put(ctx, "test-key", "test-value")
if err != nil {
log.Fatalf("Put failed: %v", err)
}
fmt.Println("✅ Put OK")
time.Sleep(2 * time.Second) // 等待集群稳定
// Test Get
fmt.Println("Testing Get...")
resp, err := cli.Get(ctx, "test-key")
if err != nil {
log.Fatalf("Get failed: %v", err)
}
if len(resp.Kvs) == 0 {
log.Fatal("Key not found")
}
fmt.Printf("✅ Get OK: %s = %s\n", resp.Kvs[0].Key, resp.Kvs[0].Value)
fmt.Println("\n✅ 所有测试通过!")
}
GOEOF
go run test_client.go
echo ""
# 清理
echo "4. 清理..."
kill $PID 2>/dev/null || true
wait $PID 2>/dev/null || true
echo ""
echo "===== Phase 2 测试完成 ====="
cd $pre_dir
rm -rf raft-cluster-test