|
| 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 | +log_level('info'); |
| 20 | +no_root_location(); |
| 21 | + |
| 22 | +add_block_preprocessor(sub { |
| 23 | + my ($block) = @_; |
| 24 | + |
| 25 | + if (!$block->error_log && !$block->no_error_log) { |
| 26 | + $block->set_value("no_error_log", "[error]\n[alert]"); |
| 27 | + } |
| 28 | + |
| 29 | + my $config = ($block->config // "") . <<_EOC_; |
| 30 | + location /hit { |
| 31 | + content_by_lua_block { |
| 32 | +
|
| 33 | + local sock = ngx.socket.tcp() |
| 34 | + local ok, err = sock:connect("127.0.0.1", 1985) |
| 35 | + if not ok then |
| 36 | + ngx.log(ngx.ERR, "failed to connect: ", err) |
| 37 | + return ngx.exit(503) |
| 38 | + end |
| 39 | +
|
| 40 | + local bytes, err = sock:send("mmm") |
| 41 | + if not bytes then |
| 42 | + ngx.log(ngx.ERR, "send stream request error: ", err) |
| 43 | + return ngx.exit(503) |
| 44 | + end |
| 45 | +
|
| 46 | + local data, err = sock:receive("*a") |
| 47 | + if not data then |
| 48 | + sock:close() |
| 49 | + return ngx.exit(503) |
| 50 | + end |
| 51 | + ngx.print(data) |
| 52 | + } |
| 53 | + } |
| 54 | +
|
| 55 | +_EOC_ |
| 56 | + |
| 57 | + $block->set_value("config", $config); |
| 58 | +}); |
| 59 | + |
| 60 | +run_tests(); |
| 61 | + |
| 62 | +__DATA__ |
| 63 | +
|
| 64 | +=== TEST 1: set stream route(id: 1) |
| 65 | +--- stream_enable |
| 66 | +--- config |
| 67 | + location /test { |
| 68 | + content_by_lua_block { |
| 69 | + local core = require("apisix.core") |
| 70 | + local dkjson = require("dkjson") |
| 71 | + local t = require("lib.test_admin").test |
| 72 | + local code, body = t('/apisix/admin/stream_routes/1', |
| 73 | + ngx.HTTP_PUT, |
| 74 | + [[{ |
| 75 | + "remote_addr": "127.0.0.1", |
| 76 | + "upstream": { |
| 77 | + "nodes": { |
| 78 | + "127.0.0.1:1995": 1 |
| 79 | + }, |
| 80 | + "type": "roundrobin", |
| 81 | + "checks": { |
| 82 | + "active": { |
| 83 | + "timeout": 40, |
| 84 | + "type": "tcp", |
| 85 | + "unhealthy": { |
| 86 | + "interval": 60, |
| 87 | + "failures": 2 |
| 88 | + }, |
| 89 | + "healthy": { |
| 90 | + "interval": 60, |
| 91 | + "successes": 2 |
| 92 | + }, |
| 93 | + "concurrency": 2 |
| 94 | + } |
| 95 | + }, |
| 96 | + "retries": 3, |
| 97 | + "timeout": { |
| 98 | + "read": 40, |
| 99 | + "send": 40, |
| 100 | + "connect": 40 |
| 101 | + } |
| 102 | + } |
| 103 | + }]] |
| 104 | + ) |
| 105 | +
|
| 106 | + if code >= 300 then |
| 107 | + ngx.status = code |
| 108 | + return |
| 109 | + end |
| 110 | +
|
| 111 | + local stream = t("/hit", ngx.HTTP_GET) |
| 112 | + if stream >= 300 then |
| 113 | + ngx.status = stream |
| 114 | + return |
| 115 | + end |
| 116 | +
|
| 117 | + ngx.sleep(3) |
| 118 | + local healthcheck, _, body = t("/v1/healthcheck", ngx.HTTP_GET) |
| 119 | + if healthcheck >= 300 then |
| 120 | + ngx.status = healthcheck |
| 121 | + return |
| 122 | + end |
| 123 | +
|
| 124 | + local healthcheck_data, err = core.json.decode(body) |
| 125 | + if not healthcheck_data then |
| 126 | + ngx.log(ngx.ERR, "failed to decode healthcheck data: ", err) |
| 127 | + return ngx.exit(503) |
| 128 | + end |
| 129 | + ngx.say(dkjson.encode(healthcheck_data)) |
| 130 | +
|
| 131 | + -- healthcheck of stream route |
| 132 | + local healthcheck, _, body = t("/v1/healthcheck/stream_routes/1", ngx.HTTP_GET) |
| 133 | + if healthcheck >= 300 then |
| 134 | + ngx.status = healthcheck |
| 135 | + return |
| 136 | + end |
| 137 | +
|
| 138 | + local healthcheck_data, err = core.json.decode(body) |
| 139 | + if not healthcheck_data then |
| 140 | + ngx.log(ngx.ERR, "failed to decode healthcheck data: ", err) |
| 141 | + return ngx.exit(503) |
| 142 | + end |
| 143 | + ngx.say(dkjson.encode(healthcheck_data)) |
| 144 | + } |
| 145 | + } |
| 146 | +--- timeout: 5 |
| 147 | +--- request |
| 148 | +GET /test |
| 149 | +--- response_body |
| 150 | +[{"name":"/apisix/stream_routes/1","nodes":[{"counter":{"http_failure":0,"success":0,"tcp_failure":0,"timeout_failure":0},"hostname":"127.0.0.1","ip":"127.0.0.1","port":1995,"status":"healthy"}],"type":"tcp"}] |
| 151 | +{"name":"/apisix/stream_routes/1","nodes":[{"counter":{"http_failure":0,"success":0,"tcp_failure":0,"timeout_failure":0},"hostname":"127.0.0.1","ip":"127.0.0.1","port":1995,"status":"healthy"}],"type":"tcp"} |
0 commit comments