|
| 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 | +no_long_string(); |
| 21 | +no_root_location(); |
| 22 | +no_shuffle(); |
| 23 | +log_level("info"); |
| 24 | + |
| 25 | +add_block_preprocessor(sub { |
| 26 | + my ($block) = @_; |
| 27 | + |
| 28 | + if (!$block->yaml_config) { |
| 29 | + my $yaml_config = <<_EOC_; |
| 30 | +apisix: |
| 31 | + node_listen: 1984 |
| 32 | + config_center: yaml |
| 33 | + enable_admin: false |
| 34 | +_EOC_ |
| 35 | + |
| 36 | + $block->set_value("yaml_config", $yaml_config); |
| 37 | + } |
| 38 | + |
| 39 | + if (!$block->request) { |
| 40 | + $block->set_value("request", "GET /t"); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +run_tests; |
| 45 | + |
| 46 | +__DATA__ |
| 47 | +
|
| 48 | +=== TEST 1: services |
| 49 | +--- apisix_yaml |
| 50 | +services: |
| 51 | + - |
| 52 | + id: 200 |
| 53 | + upstream: |
| 54 | + nodes: |
| 55 | + "127.0.0.1:1980": 1 |
| 56 | + type: roundrobin |
| 57 | +#END |
| 58 | +--- config |
| 59 | + location /t { |
| 60 | + content_by_lua_block { |
| 61 | + local json = require("toolkit.json") |
| 62 | + local t = require("lib.test_admin") |
| 63 | + local code, body, res = t.test('/v1/services', |
| 64 | + ngx.HTTP_GET) |
| 65 | + res = json.decode(res) |
| 66 | + if res[1] then |
| 67 | + local data = {} |
| 68 | + data.id = res[1].value.id |
| 69 | + data.plugins = res[1].value.plugins |
| 70 | + data.upstream = res[1].value.upstream |
| 71 | + ngx.say(json.encode(data)) |
| 72 | + end |
| 73 | + return |
| 74 | + } |
| 75 | + } |
| 76 | +--- response_body |
| 77 | +{"id":"200","upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}} |
| 78 | +
|
| 79 | +
|
| 80 | +
|
| 81 | +=== TEST 2: multiple services |
| 82 | +--- apisix_yaml |
| 83 | +services: |
| 84 | + - |
| 85 | + id: 200 |
| 86 | + upstream: |
| 87 | + nodes: |
| 88 | + "127.0.0.1:1980": 1 |
| 89 | + type: roundrobin |
| 90 | + - |
| 91 | + id: 201 |
| 92 | + upstream: |
| 93 | + nodes: |
| 94 | + "127.0.0.2:1980": 1 |
| 95 | + type: roundrobin |
| 96 | +#END |
| 97 | +--- config |
| 98 | + location /t { |
| 99 | + content_by_lua_block { |
| 100 | + local json = require("toolkit.json") |
| 101 | + local t = require("lib.test_admin") |
| 102 | + local core = require("apisix.core") |
| 103 | + local code, body, res = t.test('/v1/services', |
| 104 | + ngx.HTTP_GET) |
| 105 | + res = json.decode(res) |
| 106 | + local g_data = {} |
| 107 | + for _, r in core.config_util.iterate_values(res) do |
| 108 | + local data = {} |
| 109 | + data.id = r.value.id |
| 110 | + data.plugins = r.value.plugins |
| 111 | + data.upstream = r.value.upstream |
| 112 | + core.table.insert(g_data, data) |
| 113 | + end |
| 114 | + ngx.say(json.encode(g_data)) |
| 115 | + return |
| 116 | + } |
| 117 | + } |
| 118 | +--- response_body |
| 119 | +[{"id":"200","upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}},{"id":"201","upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.2","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}}] |
| 120 | +
|
| 121 | +
|
| 122 | +
|
| 123 | +=== TEST 3: get service with id 5 |
| 124 | +--- apisix_yaml |
| 125 | +services: |
| 126 | + - |
| 127 | + id: 5 |
| 128 | + plugins: |
| 129 | + limit-count: |
| 130 | + count: 2 |
| 131 | + time_window: 60 |
| 132 | + rejected_code: 503 |
| 133 | + key: remote_addr |
| 134 | + upstream: |
| 135 | + nodes: |
| 136 | + "127.0.0.1:1980": 1 |
| 137 | + type: roundrobin |
| 138 | +#END |
| 139 | +--- config |
| 140 | + location /t { |
| 141 | + content_by_lua_block { |
| 142 | + local json = require("toolkit.json") |
| 143 | + local t = require("lib.test_admin") |
| 144 | + local code, body, res = t.test('/v1/service/5', |
| 145 | + ngx.HTTP_GET) |
| 146 | + res = json.decode(res) |
| 147 | + if res then |
| 148 | + local data = {} |
| 149 | + data.id = res.value.id |
| 150 | + data.plugins = res.value.plugins |
| 151 | + data.upstream = res.value.upstream |
| 152 | + ngx.say(json.encode(data)) |
| 153 | + end |
| 154 | + return |
| 155 | + } |
| 156 | + } |
| 157 | +--- response_body |
| 158 | +{"id":"5","plugins":{"limit-count":{"allow_degradation":false,"count":2,"key":"remote_addr","policy":"local","rejected_code":503,"show_limit_quota_header":true,"time_window":60}},"upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}} |
| 159 | +
|
| 160 | +
|
| 161 | +
|
| 162 | +=== TEST 4: services with invalid id |
| 163 | +--- apisix_yaml |
| 164 | +services: |
| 165 | + - |
| 166 | + id: 1 |
| 167 | + upstream: |
| 168 | + nodes: |
| 169 | + "127.0.0.1:1980": 1 |
| 170 | + type: roundrobin |
| 171 | +#END |
| 172 | +--- config |
| 173 | + location /t { |
| 174 | + content_by_lua_block { |
| 175 | + local json = require("toolkit.json") |
| 176 | + local t = require("lib.test_admin") |
| 177 | + local code, body, res = t.test('/v1/service/2', |
| 178 | + ngx.HTTP_GET) |
| 179 | + local data = {} |
| 180 | + data.status = code |
| 181 | + ngx.say(json.encode(data)) |
| 182 | + return |
| 183 | + } |
| 184 | + } |
| 185 | +--- response_body |
| 186 | +{"status":404} |
0 commit comments