Skip to content

Commit a05b55d

Browse files
soulbirdsoulbird
andauthored
feat(ci): add zookeeper e2e test (#43)
Co-authored-by: soulbird <[email protected]>
1 parent 048b0d4 commit a05b55d

File tree

11 files changed

+143
-53
lines changed

11 files changed

+143
-53
lines changed

ci/apisix-seed/conf.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ discovery: # service discovery center
2323
connect: 2000 # default 2000ms
2424
send: 2000 # default 2000ms
2525
read: 5000 # default 5000ms
26+
zookeeper:
27+
hosts:
28+
- "172.50.238.40:2181"
29+
prefix: /zookeeper
30+
weight: 100 # default weight for node
31+
timeout: 10 # default 10s

test/e2e/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module e2e
33
go 1.17
44

55
require (
6+
github.com/go-zookeeper/zk v1.0.2
67
github.com/onsi/ginkgo/v2 v2.0.0
78
github.com/onsi/gomega v1.18.1
89
)

test/e2e/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
66
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
77
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
88
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
9+
github.com/go-zookeeper/zk v1.0.2 h1:4mx0EYENAdX/B/rbunjlt5+4RTA/a9SMHBRuSKdGxPM=
10+
github.com/go-zookeeper/zk v1.0.2/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
911
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1012
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
1113
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=

test/e2e/regcenter/nacos_test.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

test/e2e/regcenter/regcenter_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
var _ = BeforeSuite(func() {
1212
Expect(tools.CleanRoutes()).To(BeNil())
1313
Expect(tools.NewIRegCenter("nacos").Clean()).To(BeNil())
14-
14+
Expect(tools.NewIRegCenter("zookeeper").Clean()).To(BeNil())
1515
})
1616

1717
func TestRegcenter(t *testing.T) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package regcenter_test
2+
3+
import (
4+
"e2e/tools"
5+
"e2e/tools/common"
6+
"time"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
)
11+
12+
var _ = Describe("Normal test", Ordered, func() {
13+
Context("single route, one server", func() {
14+
15+
type normalCase struct {
16+
URI string
17+
Route *tools.Route
18+
Server *tools.SimServer
19+
Reg tools.IRegCenter
20+
ExpectBody string
21+
}
22+
23+
DescribeTable("general logic", Ordered,
24+
func(tc normalCase) {
25+
Expect(tools.CreateRoutes([]*tools.Route{tc.Route})).To(BeNil())
26+
//create sim server
27+
Expect(tools.CreateSimServer([]*tools.SimServer{tc.Server})).To(BeNil())
28+
29+
// upstream server online
30+
Expect(tc.Server.Register(tc.Reg)).To(BeNil())
31+
time.Sleep(3 * time.Second)
32+
status, body, err := common.Request(tc.URI)
33+
Expect(err).To(BeNil())
34+
Expect(status).To(Equal(200))
35+
Expect(body).To(Equal(tc.ExpectBody))
36+
// upstream server offline
37+
Expect(tc.Server.LogOut(tc.Reg)).To(BeNil())
38+
time.Sleep(3 * time.Second)
39+
status, _, err = common.Request(tc.URI)
40+
Expect(err).To(BeNil())
41+
Expect(status).To(Equal(503))
42+
},
43+
Entry("Nacos", normalCase{
44+
URI: "/test1",
45+
Route: tools.NewRoute("1", "/test1", "APISIX-NACOS", "nacos"),
46+
Server: tools.NewSimServer("0.0.0.0", "9990", "APISIX-NACOS"),
47+
Reg: tools.NewIRegCenter("nacos"),
48+
ExpectBody: "response: 0.0.0.0:9990",
49+
}),
50+
Entry("Zookeeper", normalCase{
51+
URI: "/test2",
52+
Route: tools.NewRoute("2", "/test2", "APISIX-ZK", "zookeeper"),
53+
Server: tools.NewSimServer("0.0.0.0", "9991", "APISIX-ZK"),
54+
Reg: tools.NewIRegCenter("zookeeper"),
55+
ExpectBody: "response: 0.0.0.0:9991",
56+
}),
57+
)
58+
})
59+
})

test/e2e/tools/common/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
const (
99
NACOS_HOST = "http://127.0.0.1:8848"
10+
ZK_HOST = "127.0.0.1:2181"
1011
APISIX_HOST = "http://127.0.0.1:9080"
1112
APISIX_TOKEN = "edd1c9f034335f136f87ad84b625c8f1"
1213
DOCKER_GATEWAY = "172.50.238.1"

test/e2e/tools/regcenter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func NewIRegCenter(name string) IRegCenter {
1616
switch name {
1717
case "nacos":
1818
return regcenter.NewNacos()
19+
case "zookeeper":
20+
return regcenter.NewZookeeper()
1921
}
2022
return nil
2123
}

test/e2e/tools/regcenter/nacos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (n *Nacos) deleteService(service string) error {
110110
if resp.StatusCode != 200 {
111111
return errors.New("delete service failed, serviceName=:" + service)
112112
}
113-
fmt.Println("delete service, serviceName=:" + service)
113+
fmt.Println("delete service, serviceName=" + service)
114114
return nil
115115
}
116116

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package regcenter
2+
3+
import (
4+
"e2e/tools/common"
5+
"fmt"
6+
"time"
7+
8+
"github.com/go-zookeeper/zk"
9+
)
10+
11+
type Zookeeper struct {
12+
conn *zk.Conn
13+
prefix string
14+
}
15+
16+
func NewZookeeper() *Zookeeper {
17+
conn, _, err := zk.Connect([]string{common.ZK_HOST}, time.Second*5)
18+
if err != nil {
19+
panic(err)
20+
}
21+
return &Zookeeper{
22+
conn: conn,
23+
prefix: "/zookeeper",
24+
}
25+
}
26+
27+
func (zookeeper *Zookeeper) Online(node *common.Node) error {
28+
nodeStr := `{"host":"` + common.DOCKER_GATEWAY + `","port":` + node.Port + `}`
29+
_, err := zookeeper.conn.Create(zookeeper.prefix+"/"+node.ServiceName, []byte(nodeStr), 0, zk.WorldACL(zk.PermAll))
30+
if err != nil {
31+
return err
32+
}
33+
34+
fmt.Println("register instance to Zookeeper: ", node.String())
35+
return err
36+
}
37+
38+
func (zookeeper *Zookeeper) Offline(node *common.Node) error {
39+
path := zookeeper.prefix + "/" + node.ServiceName
40+
_, stat, err := zookeeper.conn.Exists(path)
41+
if err != nil {
42+
return err
43+
}
44+
45+
fmt.Println("offline instance to Zookeeper: ", node.String())
46+
return zookeeper.conn.Delete(path, stat.Version)
47+
48+
}
49+
50+
func (zookeeper *Zookeeper) Clean() error {
51+
fmt.Println("clean all service form zookeeper...")
52+
children, stat, err := zookeeper.conn.Children(zookeeper.prefix)
53+
if err != nil {
54+
return err
55+
}
56+
for _, p := range children {
57+
if p == "config" || p == "quota" {
58+
continue
59+
}
60+
if err := zookeeper.conn.Delete(zookeeper.prefix+"/"+p, stat.Version); err != nil {
61+
return err
62+
}
63+
fmt.Println("delete service, serviceName=", p)
64+
}
65+
return nil
66+
}

0 commit comments

Comments
 (0)