-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
nacos2.t
317 lines (279 loc) · 8.62 KB
/
nacos2.t
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';
workers(3);
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->request) {
$block->set_value("request", "GET /t");
}
});
run_tests();
__DATA__
=== TEST 1: continue to get nacos data after failure in a service
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
nacos:
host:
- "http://127.0.0.1:20999"
prefix: "/nacos/v1/"
fetch_interval: 1
weight: 1
timeout:
connect: 2000
send: 2000
read: 5000
--- apisix_yaml
routes:
-
uri: /hello_
upstream:
service_name: NOT-NACOS
discovery_type: nacos
type: roundrobin
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- http_config
server {
listen 20999;
location / {
access_by_lua_block {
if not package.loaded.hit then
package.loaded.hit = true
ngx.exit(502)
end
}
proxy_pass http://127.0.0.1:8858;
}
}
--- request
GET /hello
--- response_body_like eval
qr/server [1-2]/
--- error_log
err:status = 502
=== TEST 2: change nacos server auth password
--- config
location /t {
content_by_lua_block {
local json = require("cjson")
local http = require("resty.http")
local httpc = http.new()
local nacos_host = "http://127.0.0.1:8848"
local res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/login", {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", password = "nacos"}),
})
if res.status ~= 200 then
ngx.say("nacos auth failed")
ngx.exit(401)
end
local res_json = json.decode(res.body)
res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/users?accessToken=" .. res_json["accessToken"], {
method = "PUT",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", newPassword = "nacos!@#$%^&*()[]"}),
})
if res.status ~= 200 then
ngx.say("nacos token auth failed")
ngx.say(res.body)
ngx.exit(401)
end
ngx.say("done")
}
}
--- response_body
done
=== TEST 3: test complex host
--- extra_yaml_config
discovery:
nacos:
host:
- "http://nacos:nacos!@#$%^&*()[]@127.0.0.1:8848"
fetch_interval: 1
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
--- no_error_log
[error, error]
=== TEST 4: restore nacos server auth password
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- config
location /t {
content_by_lua_block {
local json = require("cjson")
local http = require("resty.http")
local httpc = http.new()
local nacos_host = "http://127.0.0.1:8848"
local res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/login", {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", password = "nacos!@#$%^&*()[]"}),
})
if res.status ~= 200 then
ngx.say("nacos auth failed")
ngx.exit(401)
end
local res_json = json.decode(res.body)
res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/users?accessToken=" .. res_json["accessToken"], {
method = "PUT",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", newPassword = "nacos"}),
})
if res.status ~= 200 then
ngx.say("nacos token auth failed")
ngx.say(res.body)
ngx.exit(401)
end
ngx.say("done")
}
}
--- response_body
done
=== TEST 5: same service is registered in route, service and upstream, de-duplicate
--- yaml_config
apisix:
node_listen: 1984
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"scheme": "http",
"type": "roundrobin",
"discovery_args": {
"namespace_id": "public",
"group_name": "DEFAULT_GROUP"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
local code, body = t('/apisix/admin/services/1',
ngx.HTTP_PUT,
[[{
"upstream": {
"type": "roundrobin",
"scheme": "http",
"discovery_type": "nacos",
"pass_host": "pass",
"service_name": "APISIX-NACOS",
"discovery_args": {
"namespace_id": "public",
"group_name": "DEFAULT_GROUP"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"type": "roundrobin",
"scheme": "http",
"discovery_type": "nacos",
"pass_host": "pass",
"service_name": "APISIX-NACOS",
"discovery_args": {
"namespace_id": "public",
"group_name": "DEFAULT_GROUP"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.sleep(1.5)
local json_decode = require("toolkit.json").decode
local http = require "resty.http"
local httpc = http.new()
local dump_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/discovery/nacos/dump"
local res, err = httpc:request_uri(dump_uri, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
local body = json_decode(res.body)
local services = body.services
local service = services["public"]["DEFAULT_GROUP"]["APISIX-NACOS"]
local number = table.getn(service)
ngx.say(number)
}
}
--- response_body
2