HaoTien commented on code in PR #12765:
URL: https://github.com/apache/apisix/pull/12765#discussion_r2647047500


##########
t/plugin/api-breaker2.t:
##########
@@ -0,0 +1,606 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity check for unhealthy-ratio policy
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.api-breaker")
+            local ok, err = plugin.check_schema({
+                break_response_code = 502,
+                policy = "unhealthy-ratio",
+                unhealthy = {
+                    http_statuses = {500},
+                    error_ratio = 0.5,
+                    min_request_threshold = 10,
+                    sliding_window_size = 300,
+                    permitted_number_of_calls_in_half_open_state = 3
+                },
+                healthy = {
+                    http_statuses = {200},
+                    success_ratio = 0.6
+                },
+            })
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+
+
+
+=== TEST 2: default configuration for unhealthy-ratio policy
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.api-breaker")
+            local conf = {
+                break_response_code = 502,
+                policy = "unhealthy-ratio"
+            }
+
+            local ok, err = plugin.check_schema(conf)
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say(require("toolkit.json").encode(conf))
+        }
+    }
+--- request
+GET /t
+--- response_body
+{"break_response_code":502,"healthy":{"http_statuses":[200],"success_ratio":0.6},"max_breaker_sec":300,"policy":"unhealthy-ratio","unhealthy":{"error_ratio":0.5,"http_statuses":[500],"min_request_threshold":10,"permitted_number_of_calls_in_half_open_state":3,"sliding_window_size":300}}
+
+
+
+=== TEST 3: bad error_ratio (too high)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": 1.5,
+                                "min_request_threshold": 10
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"unhealthy\" validation failed: property \"error_ratio\" validation 
failed: expected 1.5 to be at most 1"}
+
+
+
+=== TEST 4: bad error_ratio (negative)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": -0.1,
+                                "min_request_threshold": 10
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"unhealthy\" validation failed: property \"error_ratio\" validation 
failed: expected -0.1 to be at least 0"}
+
+
+
+=== TEST 5: bad min_request_threshold (zero)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": 0.5,
+                                "min_request_threshold": 0
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"unhealthy\" validation failed: property \"min_request_threshold\" 
validation failed: expected 0 to be at least 1"}
+
+
+
+=== TEST 6: bad sliding_window_size (too small)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": 0.5,
+                                "min_request_threshold": 10,
+                                "sliding_window_size": 5
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"unhealthy\" validation failed: property \"sliding_window_size\" 
validation failed: expected 5 to be at least 10"}
+
+
+
+=== TEST 7: bad sliding_window_size (too large)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": 0.5,
+                                "min_request_threshold": 10,
+                                "sliding_window_size": 4000
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"unhealthy\" validation failed: property \"sliding_window_size\" 
validation failed: expected 4000 to be at most 3600"}
+
+
+
+=== TEST 8: bad success_ratio (too high)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": 0.5,
+                                "min_request_threshold": 10
+                            },
+                            "healthy": {
+                                "http_statuses": [200],
+                                "success_ratio": 1.5
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"healthy\" validation failed: property \"success_ratio\" validation 
failed: expected 1.5 to be at most 1"}
+
+
+
+=== TEST 9: bad permitted_number_of_calls_in_half_open_state (too large)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "break_response_code": 502,
+                            "policy": "unhealthy-ratio",
+                            "unhealthy": {
+                                "http_statuses": [500],
+                                "error_ratio": 0.5,
+                                "min_request_threshold": 10,
+                                "sliding_window_size": 300,
+                                
"permitted_number_of_calls_in_half_open_state": 25
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.print(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin api-breaker err: 
property \"unhealthy\" validation failed: property 
\"permitted_number_of_calls_in_half_open_state\" validation failed: expected 25 
to be at most 20"}
+
+
+
+=== TEST 10: set route with unhealthy-ratio policy
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "api-breaker": {
+                                "break_response_code": 502,
+                                "break_response_body": "Upstream failure",
+                                "policy": "unhealthy-ratio",
+                                "max_breaker_sec": 10,
+                                "unhealthy": {
+                                    "http_statuses": [500, 503],
+                                    "error_ratio": 0.6,
+                                    "min_request_threshold": 3,
+                                    "sliding_window_size": 60,
+                                    
"permitted_number_of_calls_in_half_open_state": 2
+                                },
+                                "healthy": {
+                                    "http_statuses": [200],
+                                    "successes": 2
+                                }
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST $((${1}+1)): hit route (return 200)
+--- request
+GET /api_breaker
+--- response_body
+hello world
+
+
+
+=== TEST $((${1}+1)): hit route and return 500 (first failure)
+--- request
+GET /api_breaker?code=500
+--- error_code: 500
+--- response_body
+fault injection!

Review Comment:
   Hi, I updated the test cases and they all passed.
   
   ```
   root@colima:/apisix# prove -Itest-nginx/lib -I./ t/plugin/api-breaker2.t
   t/plugin/api-breaker2.t .. 1/? t/plugin/api-breaker2.t TEST 2: default 
configuration for unhealthy-ratio policy - timeout when waiting for the process 
108706 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 2: default configuration for unhealthy-ratio 
policy - WARNING: killing the child process 108706 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 4/? t/plugin/api-breaker2.t TEST 3: bad 
error_ratio (too high) - timeout when waiting for the process 108713 to exit at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 3: bad error_ratio (too high) - WARNING: 
killing the child process 108713 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 7/? t/plugin/api-breaker2.t TEST 4: bad 
error_ratio (negative) - timeout when waiting for the process 108720 to exit at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 4: bad error_ratio (negative) - WARNING: 
killing the child process 108720 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 10/? t/plugin/api-breaker2.t TEST 5: bad 
min_request_threshold (zero) - timeout when waiting for the process 108727 to 
exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 5: bad min_request_threshold (zero) - WARNING: 
killing the child process 108727 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 13/? t/plugin/api-breaker2.t TEST 6: bad 
sliding_window_size (too small) - timeout when waiting for the process 108734 
to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 6: bad sliding_window_size (too small) - 
WARNING: killing the child process 108734 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 16/? t/plugin/api-breaker2.t TEST 7: bad 
sliding_window_size (too large) - timeout when waiting for the process 108741 
to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 7: bad sliding_window_size (too large) - 
WARNING: killing the child process 108741 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 19/? t/plugin/api-breaker2.t TEST 8: bad 
success_ratio (too high) - timeout when waiting for the process 108748 to exit 
at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 8: bad success_ratio (too high) - WARNING: 
killing the child process 108748 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 22/? t/plugin/api-breaker2.t TEST 9: bad 
half_open_max_calls (too large) - timeout when waiting for the process 108755 
to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 9: bad half_open_max_calls (too large) - 
WARNING: killing the child process 108755 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 25/? t/plugin/api-breaker2.t TEST 10: set route 
with unhealthy-ratio policy - timeout when waiting for the process 108762 to 
exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 10: set route with unhealthy-ratio policy - 
WARNING: killing the child process 108762 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 28/? t/plugin/api-breaker2.t TEST $((${1}+1)): 
test ratio-based circuit breaker functionality - timeout when waiting for the 
process 108769 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 
668.
   t/plugin/api-breaker2.t TEST $((${1}+1)): test ratio-based circuit breaker 
functionality - WARNING: killing the child process 108769 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 34/? t/plugin/api-breaker2.t TEST $((${1}+1)): 
wait for circuit breaker to enter half-open state - timeout when waiting for 
the process 108776 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm 
line 668.
   t/plugin/api-breaker2.t TEST $((${1}+1)): wait for circuit breaker to enter 
half-open state - WARNING: killing the child process 108776 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 46/? t/plugin/api-breaker2.t TEST 16: test 
half-open state functionality - timeout when waiting for the process 108783 to 
exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 16: test half-open state functionality - 
WARNING: killing the child process 108783 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 49/? t/plugin/api-breaker2.t TEST 19: verify 
circuit breaker works with custom break_response_headers - timeout when waiting 
for the process 108790 to exit at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 19: verify circuit breaker works with custom 
break_response_headers - WARNING: killing the child process 108790 with 
force... at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 58/? t/plugin/api-breaker2.t TEST 20: trigger 
circuit breaker with custom headers (combined) - timeout when waiting for the 
process 108797 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 
668.
   t/plugin/api-breaker2.t TEST 20: trigger circuit breaker with custom headers 
(combined) - WARNING: killing the child process 108797 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 63/? t/plugin/api-breaker2.t TEST 23: setup route 
for sliding window expiration test - timeout when waiting for the process 
108804 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 23: setup route for sliding window expiration 
test - WARNING: killing the child process 108804 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 67/? t/plugin/api-breaker2.t TEST 24: test 
sliding window statistics reset after expiration - timeout when waiting for the 
process 108811 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 
668.
   t/plugin/api-breaker2.t TEST 24: test sliding window statistics reset after 
expiration - WARNING: killing the child process 108811 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 70/? t/plugin/api-breaker2.t TEST 25: setup route 
for half-open failure fallback test - timeout when waiting for the process 
108818 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 25: setup route for half-open failure fallback 
test - WARNING: killing the child process 108818 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 73/? t/plugin/api-breaker2.t TEST 26: test 
half-open state failure fallback to open state - timeout when waiting for the 
process 108825 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 
668.
   t/plugin/api-breaker2.t TEST 26: test half-open state failure fallback to 
open state - WARNING: killing the child process 108825 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 76/? t/plugin/api-breaker2.t TEST 27: setup route 
for half-open request limit test - timeout when waiting for the process 108832 
to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 27: setup route for half-open request limit 
test - WARNING: killing the child process 108832 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 79/? t/plugin/api-breaker2.t TEST 28: test 
half-open state request limit enforcement and header check - timeout when 
waiting for the process 108839 to exit at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   t/plugin/api-breaker2.t TEST 28: test half-open state request limit 
enforcement and header check - WARNING: killing the child process 108839 with 
force... at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. 82/? END - timeout when waiting for the process 
108846 to exit at /usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 668.
   END - WARNING: killing the child process 108846 with force... at 
/usr/local/share/perl/5.30.0/Test/Nginx/Util.pm line 707.
   t/plugin/api-breaker2.t .. ok    
   All tests successful.
   Files=1, Tests=84, 203 wallclock secs ( 0.04 usr  0.01 sys +  1.36 cusr  
0.98 csys =  2.39 CPU)
   Result: PASS
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to