fault-injection

The abort attribute will directly return the specified HTTP code to the client and skips executing the subsequent Plugins.

The delay attribute delays a request and executes of the subsequent Plugins.

IMPORTANT

To use the fault-injection Plugin one of abort or delay must be specified.

fault-injection - 图2tip

vars can have expressions from lua-resty-expr and can flexibly implement AND/OR relationship between rules. For example:

You can enable the fault-injection Plugin on a specific Route as shown below:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 200,
  7. "body": "Fault Injection!"
  8. }
  9. }
  10. },
  11. "upstream": {
  12. "nodes": {
  13. "127.0.0.1:1980": 1
  14. },
  15. "type": "roundrobin"
  16. },
  17. "uri": "/hello"
  18. }'

Similarly, to enable a delay fault:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "delay": {
  6. "duration": 3
  7. }
  8. }
  9. },
  10. "upstream": {
  11. "nodes": {
  12. "127.0.0.1:1980": 1
  13. "type": "roundrobin"
  14. },
  15. }'

You can also enable the Plugin with both abort and delay which can have vars for matching:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 403,
  7. "body": "Fault Injection!\n",
  8. "vars": [
  9. [
  10. [ "arg_name","==","jack" ]
  11. ]
  12. ]
  13. },
  14. "delay": {
  15. "duration": 2,
  16. "vars": [
  17. [
  18. [ "http_age","==","18" ]
  19. ]
  20. ]
  21. }
  22. }
  23. },
  24. "upstream": {
  25. "nodes": {
  26. "127.0.0.1:1980": 1
  27. },
  28. "type": "roundrobin"
  29. },
  30. "uri": "/hello"
  31. }'

Once you have enabled the Plugin as shown above, you can make a request to the configured Route:

  1. HTTP/1.1 200 OK
  2. Date: Mon, 13 Jan 2020 13:50:04 GMT
  3. Content-Type: text/plain
  4. Connection: keep-alive
  5. Server: APISIX web server
  1. time curl http://127.0.0.1:9080/hello -i
  1. HTTP/1.1 200 OK
  2. Content-Type: application/octet-stream
  3. Content-Length: 6
  4. Connection: keep-alive
  5. Server: APISIX web server
  6. Date: Tue, 14 Jan 2020 14:30:54 GMT
  7. Last-Modified: Sat, 11 Jan 2020 12:46:21 GMT
  8. hello
  9. real 0m3.034s
  10. user 0m0.007s
  11. sys 0m0.010s

You can enable the fault-injection Plugin with the vars attribute to set specific rules:

Now, we can test the Route. First, we test with a different name argument:

  1. curl "http://127.0.0.1:9080/hello?name=allen" -i

You will get the expected response without the fault injected:

  1. HTTP/1.1 200 OK
  2. Content-Type: application/octet-stream
  3. Transfer-Encoding: chunked
  4. Connection: keep-alive
  5. Date: Wed, 20 Jan 2021 07:21:57 GMT
  6. Server: APISIX/2.2
  7. hello

Now if we set the name to match our configuration, the fault-injection Plugin is executed:

  1. curl "http://127.0.0.1:9080/hello?name=jack" -i
  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }