upstream-discovery-dynamic.t 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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('warn');
  20. no_root_location();
  21. no_shuffle();
  22. run_tests();
  23. __DATA__
  24. === TEST 1: dynamic host based discovery
  25. --- extra_yaml_config
  26. nginx_config:
  27. worker_processes: 1
  28. --- config
  29. location /t {
  30. content_by_lua_block {
  31. local t = require("lib.test_admin").test
  32. local discovery = require("apisix.discovery.init").discovery
  33. local core = require("apisix.core")
  34. discovery.demo_discover = {
  35. nodes = function()
  36. local demo_nodes_tab = {
  37. a = { host = "127.0.0.1", port = 1111 },
  38. b = { host = "127.0.0.1", port = 2222 }
  39. }
  40. local host = ngx.var.host
  41. local service_id = host:match("([^.]+).myhost.com")
  42. local demo_node = demo_nodes_tab[service_id]
  43. local node_list = core.table.new(1, 0)
  44. core.table.insert(node_list, {
  45. host = demo_node.host,
  46. port = tonumber(demo_node.port),
  47. weight = 100,
  48. })
  49. return node_list
  50. end
  51. }
  52. local code, body = t('/apisix/admin/services/',
  53. ngx.HTTP_PUT,
  54. [[{
  55. "id": "demo_service",
  56. "name": "demo_service",
  57. "upstream": {
  58. "discovery_type": "demo_discover",
  59. "service_name": "demo_service",
  60. "type": "roundrobin"
  61. }
  62. }]]
  63. )
  64. if code >= 300 then
  65. ngx.status = code
  66. end
  67. ngx.sleep(0.5)
  68. local code, body = t('/apisix/admin/routes/',
  69. ngx.HTTP_PUT,
  70. [[{
  71. "id": "demo_route",
  72. "name": "demo_route",
  73. "uri": "/*",
  74. "hosts":[
  75. "*.myhost.com"
  76. ],
  77. "service_id": "demo_service"
  78. }]]
  79. )
  80. if code >= 300 then
  81. ngx.status = code
  82. end
  83. ngx.sleep(0.5)
  84. local hosts = {
  85. "a.myhost.com",
  86. "a.myhost.com",
  87. "b.myhost.com",
  88. "b.myhost.com",
  89. "a.myhost.com",
  90. "b.myhost.com",
  91. "b.myhost.com",
  92. "a.myhost.com",
  93. "b.myhost.com",
  94. "a.myhost.com",
  95. }
  96. for i, url_host in ipairs(hosts) do
  97. local http = require "resty.http"
  98. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
  99. local httpc = http.new()
  100. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false, headers = {
  101. ["Host"] = url_host
  102. }})
  103. end
  104. }
  105. }
  106. --- request
  107. GET /t
  108. --- grep_error_log eval
  109. qr/upstream: \S+, host: \S+/
  110. --- grep_error_log_out
  111. upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
  112. upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
  113. upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
  114. upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
  115. upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
  116. upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
  117. upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
  118. upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
  119. upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
  120. upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"