upstream-domain.t 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. log_level('info');
  20. worker_connections(256);
  21. no_root_location();
  22. no_shuffle();
  23. run_tests();
  24. __DATA__
  25. === TEST 1: set upstream(id: 1)
  26. --- config
  27. location /t {
  28. content_by_lua_block {
  29. local t = require("lib.test_admin").test
  30. local code, body = t('/apisix/admin/upstreams/1',
  31. ngx.HTTP_PUT,
  32. [[{
  33. "nodes": {
  34. "foo.com:80": 0,
  35. "127.0.0.1:1980": 1
  36. },
  37. "type": "roundrobin",
  38. "desc": "new upstream"
  39. }]]
  40. )
  41. if code >= 300 then
  42. ngx.status = code
  43. end
  44. ngx.say(body)
  45. }
  46. }
  47. --- request
  48. GET /t
  49. --- response_body
  50. passed
  51. === TEST 2: set route(id: 1)
  52. --- config
  53. location /t {
  54. content_by_lua_block {
  55. local t = require("lib.test_admin").test
  56. local code, body = t('/apisix/admin/routes/1',
  57. ngx.HTTP_PUT,
  58. [[{
  59. "uri": "/hello",
  60. "upstream_id": "1"
  61. }]]
  62. )
  63. if code >= 300 then
  64. ngx.status = code
  65. end
  66. ngx.say(body)
  67. }
  68. }
  69. --- request
  70. GET /t
  71. --- response_body
  72. passed
  73. === TEST 3: /not_found
  74. --- request
  75. GET /not_found
  76. --- error_code: 404
  77. --- response_body
  78. {"error_msg":"404 Route Not Found"}
  79. === TEST 4: hit routes
  80. --- request
  81. GET /hello
  82. --- response_body
  83. hello world
  84. --- error_log eval
  85. qr/dns resolver domain: foo.com to \d+.\d+.\d+.\d+/
  86. === TEST 5: set upstream(invalid node host)
  87. --- config
  88. location /t {
  89. content_by_lua_block {
  90. local t = require("lib.test_admin").test
  91. local code, body = t('/apisix/admin/upstreams/1',
  92. ngx.HTTP_PUT,
  93. [[{
  94. "nodes": {
  95. "test.comx:80": 0
  96. },
  97. "type": "roundrobin",
  98. "desc": "new upstream"
  99. }]]
  100. )
  101. if code >= 300 then
  102. ngx.status = code
  103. end
  104. ngx.say(body)
  105. }
  106. }
  107. --- request
  108. GET /t
  109. --- response_body
  110. passed
  111. === TEST 6:
  112. --- config
  113. location /t {
  114. content_by_lua_block {
  115. local t = require("lib.test_admin").test
  116. local function test()
  117. local code, body = t('/hello', ngx.HTTP_GET)
  118. ngx.say("status: ", code)
  119. end
  120. test()
  121. test()
  122. }
  123. }
  124. --- request
  125. GET /t
  126. --- response_body
  127. status: 503
  128. status: 503
  129. --- error_log
  130. failed to parse domain: test.comx
  131. failed to parse domain: test.comx
  132. --- timeout: 10
  133. === TEST 7: delete route
  134. --- config
  135. location /t {
  136. content_by_lua_block {
  137. ngx.sleep(0.3)
  138. local t = require("lib.test_admin").test
  139. local code, body = t('/apisix/admin/routes/1',
  140. ngx.HTTP_DELETE
  141. )
  142. if code >= 300 then
  143. ngx.status = code
  144. end
  145. ngx.say(body)
  146. }
  147. }
  148. --- request
  149. GET /t
  150. --- response_body
  151. passed
  152. === TEST 8: delete upstream
  153. --- config
  154. location /t {
  155. content_by_lua_block {
  156. ngx.sleep(0.3)
  157. local t = require("lib.test_admin").test
  158. local code, body = t('/apisix/admin/upstreams/1',
  159. ngx.HTTP_DELETE
  160. )
  161. if code >= 300 then
  162. ngx.status = code
  163. end
  164. ngx.say(body)
  165. }
  166. }
  167. --- request
  168. GET /t
  169. --- response_body
  170. passed
  171. === TEST 9: set upstream(with domain)
  172. --- config
  173. location /t {
  174. content_by_lua_block {
  175. local t = require("lib.test_admin").test
  176. local code, body = t('/apisix/admin/upstreams/1',
  177. ngx.HTTP_PUT,
  178. [[{
  179. "nodes": {
  180. "foo.com:80": 0,
  181. "127.0.0.1:1980": 1
  182. },
  183. "type": "roundrobin",
  184. "desc": "new upstream"
  185. }]]
  186. )
  187. if code >= 300 then
  188. ngx.status = code
  189. end
  190. ngx.say(body)
  191. }
  192. }
  193. --- request
  194. GET /t
  195. --- response_body
  196. passed
  197. === TEST 10: set empty service
  198. --- config
  199. location /t {
  200. content_by_lua_block {
  201. local t = require("lib.test_admin").test
  202. local code, body = t('/apisix/admin/services/1',
  203. ngx.HTTP_PUT,
  204. [[{
  205. "desc": "new service",
  206. "plugins": {
  207. "prometheus": {}
  208. }
  209. }]]
  210. )
  211. if code >= 300 then
  212. ngx.status = code
  213. end
  214. ngx.say(body)
  215. }
  216. }
  217. --- request
  218. GET /t
  219. --- response_body
  220. passed
  221. === TEST 11: set route(with upstream)
  222. --- config
  223. location /t {
  224. content_by_lua_block {
  225. local t = require("lib.test_admin").test
  226. local code, body = t('/apisix/admin/routes/1',
  227. ngx.HTTP_PUT,
  228. [[{
  229. "uri": "/hello",
  230. "upstream": {
  231. "nodes": {
  232. "foo.com": 0,
  233. "127.0.0.1:1980": 1
  234. },
  235. "type": "roundrobin",
  236. "desc": "new upstream"
  237. },
  238. "service_id": "1"
  239. }]]
  240. )
  241. if code >= 300 then
  242. ngx.status = code
  243. end
  244. ngx.say(body)
  245. }
  246. }
  247. --- request
  248. GET /t
  249. --- response_body
  250. passed
  251. === TEST 12: hit routes, parse the domain of upstream node
  252. --- request
  253. GET /hello
  254. --- response_body
  255. hello world
  256. --- error_log eval
  257. qr/dns resolver domain: foo.com to \d+.\d+.\d+.\d+/
  258. === TEST 13: set route(with upstream)
  259. --- config
  260. location /t {
  261. content_by_lua_block {
  262. local t = require("lib.test_admin").test
  263. local code, body = t('/apisix/admin/upstreams/1',
  264. ngx.HTTP_PUT,
  265. [[{
  266. "nodes": {
  267. "localhost:1981": 2,
  268. "127.0.0.1:1980": 1
  269. },
  270. "type": "roundrobin",
  271. "desc": "new upstream"
  272. }]]
  273. )
  274. if code >= 300 then
  275. ngx.status = code
  276. ngx.say(body)
  277. return
  278. end
  279. local code, body = t('/apisix/admin/routes/1',
  280. ngx.HTTP_PUT,
  281. [[{
  282. "uri": "/server_port",
  283. "service_id": "1",
  284. "upstream_id": "1"
  285. }]]
  286. )
  287. if code >= 300 then
  288. ngx.status = code
  289. end
  290. ngx.say(body)
  291. }
  292. }
  293. --- request
  294. GET /t
  295. --- response_body
  296. passed
  297. === TEST 14: roundrobin
  298. --- config
  299. location /t {
  300. content_by_lua_block {
  301. local t = require("lib.test_admin").test
  302. local bodys = {}
  303. for i = 1, 3 do
  304. local _, _, body = t('/server_port', ngx.HTTP_GET)
  305. bodys[i] = body
  306. end
  307. table.sort(bodys)
  308. ngx.say(table.concat(bodys, ", "))
  309. }
  310. }
  311. --- request
  312. GET /t
  313. --- response_body
  314. 1980, 1981, 1981
  315. === TEST 15: set route(with upstream)
  316. --- config
  317. location /t {
  318. content_by_lua_block {
  319. local t = require("lib.test_admin").test
  320. local code, body = t('/apisix/admin/routes/1',
  321. ngx.HTTP_PUT,
  322. [[{
  323. "uri": "/hello",
  324. "upstream": {
  325. "nodes": {
  326. "foo.com.": 0,
  327. "127.0.0.1:1980": 1
  328. },
  329. "type": "roundrobin",
  330. "desc": "new upstream"
  331. },
  332. "service_id": "1"
  333. }]]
  334. )
  335. if code >= 300 then
  336. ngx.status = code
  337. end
  338. ngx.say(body)
  339. }
  340. }
  341. --- request
  342. GET /t
  343. --- response_body
  344. passed
  345. === TEST 16: hit routes, parse the domain of upstream node
  346. --- request
  347. GET /hello
  348. --- response_body
  349. hello world
  350. --- error_log eval
  351. qr/dns resolver domain: foo.com. to \d+.\d+.\d+.\d+/