|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +. ./t/cli/common.sh |
| 21 | + |
| 22 | +# Enable limit-req plugin |
| 23 | + |
| 24 | +rm logs/worker_events.sock || true |
| 25 | + |
| 26 | +echo ' |
| 27 | +nginx_config: |
| 28 | + worker_processes: 1 |
| 29 | + error_log_level: info |
| 30 | +deployment: |
| 31 | + admin: |
| 32 | + admin_key: |
| 33 | + - name: "admin" |
| 34 | + key: edd1c9f034335f136f87ad84b625c8f1 |
| 35 | + role: admin |
| 36 | +
|
| 37 | +apisix: |
| 38 | + enable_admin: true |
| 39 | + control: |
| 40 | + port: 9110 |
| 41 | +plugins: |
| 42 | + - limit-req |
| 43 | +' > conf/config.yaml |
| 44 | + |
| 45 | +make init |
| 46 | +make run |
| 47 | + |
| 48 | +admin_key="edd1c9f034335f136f87ad84b625c8f1" |
| 49 | + |
| 50 | +# Create a route with limit-req and redis policy |
| 51 | +# rate=1, burst=1 -> ttl = ceil(1/1) + 1 = 2s |
| 52 | +curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \ |
| 53 | + -H "X-API-KEY: $admin_key" \ |
| 54 | + -d '{ |
| 55 | + "methods": ["GET"], |
| 56 | + "uri": "/hello", |
| 57 | + "plugins": { |
| 58 | + "limit-req": { |
| 59 | + "rate": 1, |
| 60 | + "burst": 1, |
| 61 | + "key": "remote_addr", |
| 62 | + "policy": "redis", |
| 63 | + "redis_host": "127.0.0.1", |
| 64 | + "redis_timeout": 1000 |
| 65 | + } |
| 66 | + }, |
| 67 | + "upstream": { |
| 68 | + "type": "roundrobin", |
| 69 | + "nodes": { |
| 70 | + "127.0.0.1:1980": 1 |
| 71 | + } |
| 72 | + } |
| 73 | +}' |
| 74 | + |
| 75 | +if [ $? -ne 0 ]; then |
| 76 | + echo "failed: verify route creation" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +sleep 0.5 |
| 81 | + |
| 82 | +# Make a request to create the Redis keys |
| 83 | +curl -v http://127.0.0.1:9080/hello > /dev/null 2>&1 |
| 84 | + |
| 85 | +# Verify keys exist |
| 86 | +# Keys pattern: plugin-limit-req* |
| 87 | +keys=$(redis-cli keys "limit_req:*" | wc -l) |
| 88 | +if [ "$keys" -eq 0 ]; then |
| 89 | + echo "failed: keys not found in Redis immediately after request" |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | +echo "pass: keys found in Redis" |
| 93 | + |
| 94 | +# Wait for 3 seconds (TTL is 2s) |
| 95 | +echo "Waiting for 3s..." |
| 96 | +sleep 3 |
| 97 | + |
| 98 | +# Verify keys are gone |
| 99 | +keys_list=$(redis-cli keys "limit_req:*") |
| 100 | +keys_count=$(echo "$keys_list" | wc -l) |
| 101 | + |
| 102 | +if [ "$keys_count" -ne 0 ] && [ -n "$keys_list" ]; then |
| 103 | + echo "failed: keys still exist in Redis after TTL expiration" |
| 104 | + echo "Keys found:" |
| 105 | + echo "$keys_list" |
| 106 | + |
| 107 | + first_key=$(echo "$keys_list" | head -n 1) |
| 108 | + echo "TTL of $first_key:" |
| 109 | + redis-cli ttl "$first_key" |
| 110 | + |
| 111 | + exit 1 |
| 112 | +fi |
| 113 | + |
| 114 | +echo "pass: keys expired correctly" |
| 115 | +make stop |
0 commit comments