/
discovery_test.go
167 lines (149 loc) · 5.22 KB
/
discovery_test.go
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
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2021 CloudWeGo authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific
package test
import (
"context"
"net"
"testing"
"time"
"github.com/cloudwego/kitex/pkg/registry"
"github.com/cloudwego/kitex/pkg/rpcinfo"
zkregistry "github.com/kitex-contrib/registry-zookeeper/registry"
"github.com/kitex-contrib/registry-zookeeper/resolver"
"github.com/kitex-contrib/registry-zookeeper/utils"
"github.com/stretchr/testify/assert"
)
const (
testConfigName = "zoo.cfg"
)
func TestZookeeperDiscovery(t *testing.T) {
// start test server
testServer := startTestServer(t)
defer testServer.Stop()
// register
r, err := zkregistry.NewZookeeperRegistry([]string{"127.0.0.1:2181"}, 40*time.Second)
assert.Nil(t, err)
tags := map[string]string{"group": "blue", "idc": "hd1"}
addr, _ := net.ResolveTCPAddr("tcp", ":9999")
info := ®istry.Info{ServiceName: "product", Weight: 100, PayloadCodec: "thrift", Tags: tags, Addr: addr}
err = r.Register(info)
assert.Nil(t, err)
// resolve
res, err := resolver.NewZookeeperResolver([]string{"127.0.0.1:2181"}, 40*time.Second)
assert.Nil(t, err)
target := res.Target(context.Background(), rpcinfo.NewEndpointInfo("product", "", nil, nil))
result, err := res.Resolve(context.Background(), target)
assert.Nil(t, err)
// compare data
if len(result.Instances) == 0 {
t.Errorf("instance num mismatch, expect: %d, in fact: %d", 1, 0)
} else if len(result.Instances) == 1 {
instance := result.Instances[0]
host, port, err := net.SplitHostPort(instance.Address().String())
assert.Nil(t, err)
local, _ := utils.GetLocalIPv4Address()
if host != local {
t.Errorf("instance host is mismatch, expect: %s, in fact: %s", local, host)
}
if port != "9999" {
t.Errorf("instance port is mismatch, expect: %s, in fact: %s", "9999", port)
}
if info.Weight != instance.Weight() {
t.Errorf("instance weight is mismatch, expect: %d, in fact: %d", info.Weight, instance.Weight())
}
for k, v := range info.Tags {
if v1, exist := instance.Tag(k); !exist || v != v1 {
t.Errorf("instance tags is mismatch, expect k:v %s:%s, in fact k:v %s:%s", k, v, k, v1)
}
}
}
// deregister
err = r.Deregister(info)
assert.Nil(t, err)
// resolve again
result, err = res.Resolve(context.Background(), target)
assert.EqualError(t, err, "no instance remains for product")
}
func TestZookeeperResolverWithAuth(t *testing.T) {
// start test server
testServer := startTestServer(t)
defer testServer.Stop()
// register
r, err := zkregistry.NewZookeeperRegistryWithAuth([]string{"127.0.0.1:2181"}, 40*time.Second, "horizon", "horizon")
assert.Nil(t, err)
tags := map[string]string{"group": "blue", "idc": "hd1"}
addr, _ := net.ResolveTCPAddr("tcp", ":9999")
info := ®istry.Info{ServiceName: "product", Weight: 100, PayloadCodec: "thrift", Tags: tags, Addr: addr}
err = r.Register(info)
assert.Nil(t, err)
// resolve
res, err := resolver.NewZookeeperResolverWithAuth([]string{"127.0.0.1:2181"}, 40*time.Second, "horizon", "horizon")
assert.Nil(t, err)
target := res.Target(context.Background(), rpcinfo.NewEndpointInfo("product", "", nil, nil))
result, err := res.Resolve(context.Background(), target)
assert.Nil(t, err)
// compare data
if len(result.Instances) == 0 {
t.Errorf("instance num mismatch, expect: %d, in fact: %d", 1, 0)
} else if len(result.Instances) == 1 {
instance := result.Instances[0]
host, port, err := net.SplitHostPort(instance.Address().String())
assert.Nil(t, err)
local, _ := utils.GetLocalIPv4Address()
if host != local {
t.Errorf("instance host is mismatch, expect: %s, in fact: %s", local, host)
}
if port != "9999" {
t.Errorf("instance port is mismatch, expect: %s, in fact: %s", "9999", port)
}
if info.Weight != instance.Weight() {
t.Errorf("instance weight is mismatch, expect: %d, in fact: %d", info.Weight, instance.Weight())
}
for k, v := range info.Tags {
if v1, exist := instance.Tag(k); !exist || v != v1 {
t.Errorf("instance tags is mismatch, expect k:v %s:%s, in fact k:v %s:%s", k, v, k, v1)
}
}
}
// deregister
err = r.Deregister(info)
assert.Nil(t, err)
// resolve again
result, err = res.Resolve(context.Background(), target)
assert.EqualError(t, err, "no instance remains for product")
}
func startTestServer(t *testing.T) *server {
testServer, err := NewIntegrationTestServer(t, testConfigName, nil, nil)
requireNoError(t, err)
requireNoError(t, testServer.Start())
// wait server start
var ok bool
for i := 0; i < 10; i++ {
_, err := net.Dial("tcp", "127.0.0.1:2181")
if err == nil {
ok = true
break
}
time.Sleep(30 * time.Second)
}
if !ok {
panic("zookeeper server start failed")
}
return testServer
}
func requireNoError(t *testing.T, err error, msgAndArgs ...interface{}) {
if err != nil {
t.Logf("received unexpected error: %v", err)
t.Fatal(msgAndArgs...)
}
}