|
| 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 | + |
| 19 | +repeat_each(1); |
| 20 | +log_level('info'); |
| 21 | +no_root_location(); |
| 22 | +no_shuffle(); |
| 23 | + |
| 24 | + |
| 25 | +add_block_preprocessor(sub { |
| 26 | + my ($block) = @_; |
| 27 | + |
| 28 | + my $http_config = $block->http_config // <<_EOC_; |
| 29 | +
|
| 30 | + server { |
| 31 | + listen 20999; |
| 32 | +
|
| 33 | + location / { |
| 34 | + content_by_lua_block { |
| 35 | + ngx.say("missing consul_kv services") |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +
|
| 40 | + server { |
| 41 | + listen 30511; |
| 42 | +
|
| 43 | + location /hello { |
| 44 | + content_by_lua_block { |
| 45 | + ngx.say("server 1") |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + server { |
| 50 | + listen 30512; |
| 51 | +
|
| 52 | + location /hello { |
| 53 | + content_by_lua_block { |
| 54 | + ngx.say("server 2") |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + server { |
| 59 | + listen 30513; |
| 60 | +
|
| 61 | + location /hello { |
| 62 | + content_by_lua_block { |
| 63 | + ngx.say("server 3") |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + server { |
| 68 | + listen 30514; |
| 69 | +
|
| 70 | + location /hello { |
| 71 | + content_by_lua_block { |
| 72 | + ngx.say("server 4") |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +_EOC_ |
| 77 | + |
| 78 | + $block->set_value("http_config", $http_config); |
| 79 | + |
| 80 | + if (!$block->stream_request) { |
| 81 | + $block->set_value("stream_request", "GET /hello HTTP/1.1\r\nHost: 127.0.0.1:1985\r\nConnection: close\r\n\r\n"); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +our $yaml_config = <<_EOC_; |
| 86 | +apisix: |
| 87 | + node_listen: 1984 |
| 88 | +deployment: |
| 89 | + role: data_plane |
| 90 | + role_data_plane: |
| 91 | + config_provider: yaml |
| 92 | +discovery: |
| 93 | + consul_kv: |
| 94 | + servers: |
| 95 | + - "http://127.0.0.1:8500" |
| 96 | + - "http://127.0.0.1:8600" |
| 97 | + prefix: "upstreams" |
| 98 | + skip_keys: |
| 99 | + - "upstreams/unused_api/" |
| 100 | + timeout: |
| 101 | + connect: 1000 |
| 102 | + read: 1000 |
| 103 | + wait: 60 |
| 104 | + weight: 1 |
| 105 | + fetch_interval: 1 |
| 106 | + keepalive: true |
| 107 | + default_service: |
| 108 | + host: "127.0.0.1" |
| 109 | + port: 20999 |
| 110 | + metadata: |
| 111 | + fail_timeout: 1 |
| 112 | + weight: 1 |
| 113 | + max_fails: 1 |
| 114 | +_EOC_ |
| 115 | + |
| 116 | + |
| 117 | +run_tests(); |
| 118 | + |
| 119 | +__DATA__ |
| 120 | +
|
| 121 | +=== TEST 1: prepare consul kv register nodes |
| 122 | +--- config |
| 123 | +location /consul1 { |
| 124 | + rewrite ^/consul1/(.*) /v1/kv/$1 break; |
| 125 | + proxy_pass http://127.0.0.1:8500; |
| 126 | +} |
| 127 | +
|
| 128 | +location /consul2 { |
| 129 | + rewrite ^/consul2/(.*) /v1/kv/$1 break; |
| 130 | + proxy_pass http://127.0.0.1:8600; |
| 131 | +} |
| 132 | +--- pipelined_requests eval |
| 133 | +[ |
| 134 | + "DELETE /consul1/upstreams/webpages/?recurse=true", |
| 135 | + "DELETE /consul2/upstreams/webpages/?recurse=true", |
| 136 | + "PUT /consul1/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}", |
| 137 | + "PUT /consul1/upstreams/webpages/127.0.0.1:30512\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}", |
| 138 | + "PUT /consul2/upstreams/webpages/127.0.0.1:30513\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}", |
| 139 | + "PUT /consul2/upstreams/webpages/127.0.0.1:30514\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}", |
| 140 | +] |
| 141 | +--- response_body eval |
| 142 | +["true", "true", "true", "true", "true", "true"] |
| 143 | +
|
| 144 | +
|
| 145 | +
|
| 146 | +=== TEST 2: test consul server 1 |
| 147 | +--- yaml_config eval: $::yaml_config |
| 148 | +--- apisix_yaml |
| 149 | +stream_routes: |
| 150 | + - server_addr: 127.0.0.1 |
| 151 | + server_port: 1985 |
| 152 | + id: 1 |
| 153 | + upstream: |
| 154 | + service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/ |
| 155 | + discovery_type: consul_kv |
| 156 | + type: roundrobin |
| 157 | +#END |
| 158 | +--- stream_response eval |
| 159 | +qr/server [1-2]/ |
| 160 | +--- no_error_log |
| 161 | +[error] |
| 162 | +
|
| 163 | +
|
| 164 | +
|
| 165 | +=== TEST 3: test consul server 2 |
| 166 | +--- yaml_config eval: $::yaml_config |
| 167 | +--- apisix_yaml |
| 168 | +stream_routes: |
| 169 | + - server_addr: 127.0.0.1 |
| 170 | + server_port: 1985 |
| 171 | + id: 1 |
| 172 | + upstream: |
| 173 | + service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/ |
| 174 | + discovery_type: consul_kv |
| 175 | + type: roundrobin |
| 176 | +#END |
| 177 | +--- stream_response eval |
| 178 | +qr/server [3-4]/ |
| 179 | +--- no_error_log |
| 180 | +[error] |
| 181 | +
|
| 182 | +
|
| 183 | +
|
| 184 | +=== TEST 4: test mini consul_kv config |
| 185 | +--- yaml_config |
| 186 | +apisix: |
| 187 | + node_listen: 1984 |
| 188 | +deployment: |
| 189 | + role: data_plane |
| 190 | + role_data_plane: |
| 191 | + config_provider: yaml |
| 192 | +discovery: |
| 193 | + consul_kv: |
| 194 | + servers: |
| 195 | + - "http://127.0.0.1:8500" |
| 196 | + - "http://127.0.0.1:6500" |
| 197 | +#END |
| 198 | +--- apisix_yaml |
| 199 | +stream_routes: |
| 200 | + - server_addr: 127.0.0.1 |
| 201 | + server_port: 1985 |
| 202 | + id: 1 |
| 203 | + upstream: |
| 204 | + service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/ |
| 205 | + discovery_type: consul_kv |
| 206 | + type: roundrobin |
| 207 | +#END |
| 208 | +--- stream_response eval |
| 209 | +qr/server [1-2]/ |
| 210 | +--- ignore_error_log |
| 211 | +
|
| 212 | +
|
| 213 | +
|
| 214 | +=== TEST 5: test invalid service name |
| 215 | +sometimes the consul key maybe deleted by mistake |
| 216 | +--- yaml_config eval: $::yaml_config |
| 217 | +--- apisix_yaml |
| 218 | +stream_routes: |
| 219 | + - server_addr: 127.0.0.1 |
| 220 | + server_port: 1985 |
| 221 | + id: 1 |
| 222 | + upstream: |
| 223 | + service_name: http://127.0.0.1:8600/v1/kv/upstreams/deleted_keys/ |
| 224 | + discovery_type: consul_kv |
| 225 | + type: roundrobin |
| 226 | +#END |
| 227 | +--- stream_response_like |
| 228 | +missing consul_kv services |
| 229 | +--- ignore_error_log |
| 230 | +
|
| 231 | +
|
| 232 | +
|
| 233 | +=== TEST 6: test skip keys |
| 234 | +skip some keys, return default nodes, get response: missing consul_kv services |
| 235 | +--- yaml_config |
| 236 | +apisix: |
| 237 | + node_listen: 1984 |
| 238 | +deployment: |
| 239 | + role: data_plane |
| 240 | + role_data_plane: |
| 241 | + config_provider: yaml |
| 242 | +discovery: |
| 243 | + consul_kv: |
| 244 | + servers: |
| 245 | + - "http://127.0.0.1:8600" |
| 246 | + prefix: "upstreams" |
| 247 | + skip_keys: |
| 248 | + - "upstreams/webpages/" |
| 249 | + default_service: |
| 250 | + host: "127.0.0.1" |
| 251 | + port: 20999 |
| 252 | + metadata: |
| 253 | + fail_timeout: 1 |
| 254 | + weight: 1 |
| 255 | + max_fails: 1 |
| 256 | +#END |
| 257 | +--- apisix_yaml |
| 258 | +stream_routes: |
| 259 | + - server_addr: 127.0.0.1 |
| 260 | + server_port: 1985 |
| 261 | + id: 1 |
| 262 | + upstream: |
| 263 | + service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/ |
| 264 | + discovery_type: consul_kv |
| 265 | + type: roundrobin |
| 266 | +#END |
| 267 | +--- stream_response_like |
| 268 | +missing consul_kv services |
| 269 | +--- ignore_error_log |
0 commit comments